Examples / AudioIO / Audio File Out

Right-click to copy examples to your workspace


This example opens the current audio output device and sends it a simple audio stream generated by Hz.Osc. The resulting performance is recorded to a file within your workspace's _tmp directory.

audioout.js

// Example of writing audio to a file.
let ascene = await Ascene.BeginFiber(this);
let dac = ascene.GetDAC();
let osc = ascene.NewAnode("Hz.Osc");
// obtain a file pathname within the current workspace.
let ts = Util.GetFileTimestamp(); // our filename will be relatively unique.
let outputFilePath = await ResolveWSFile(`_ws_/_tmp/audiofileout.${ts}.wav`, false);
let preset = {filename: outputFilePath};
let fileout = ascene.NewAnode("Hz.AudioFileOut", {preset});
ascene.Chain(osc, dac, fileout);
await ascene.Sync();
for(let i=0;i<100;i++)
{
    let dur = ascene.Seconds(.05 + .2 * Math.random());
    osc.Note(Math.floor(30 + 60*Math.random()), .15 + .4*Math.random(), dur);
    await ascene.Wait(dur);
    yield
}
await Aengine.Close();
// exercise: eliminate clicks with voices or Hz.Syntho.
console.log("Done.");

See Also

Hz.AudioFileOut

home .. topics .. interface .. reference .. examples .. tipjar