Examples / SampleMgr

Right-click to copy examples to your workspace


Wherein we show the use of SampleMgr to ensure your samples files are in order.

In this simple example, we use SampleMgr to ensure that the "FatBoy/accordian" samples are loaded into your workspace. Next we provide them to Hz.Samplo via its LoadPreset method. Finally we play a few notes.

let bank = "FatBoy";
let voice = "accordion";
let voiceref = `${bank}_${voice}`;

// wait for samples to be downloaded (cached, if necessary).
let asamps = await SampleMgr.PreloadSamples(bank, voice);

// setup our audio pipeline
let scene = await Ascene.BeginFiber(this);
let inst = scene.NewAnode("Hz.Samplo"); // has Reverb built-in.
let dac = scene.GetDAC();
scene.Chain(inst, dac);
await scene.Sync(); // <--- wait for node creation
inst.SetParam("/gain/Gain", 12);
inst.SetParam("/reverb/Model", 1);
inst.Show();

// wait for samples to load into instrument
await inst.LoadPreset(asamps.AsPreset(), voiceref); 

let run = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60];
let notedur = scene.Seconds(.15);
for(let i=0;i<5;i++)
{
    for(let n of run)
    {
        inst.NoteOn(n, .5 + .5 * Math.random());
        await scene.Wait(notedur);
        inst.NoteOff(n, 0);
    }
}

The first time you run this script you should see the log activity associated with the downloading of the sample files for FatBoy/accordion.

Files are downloaded to your current Hz workspace cache which defaults to ~/Documents/Hz/_cache/....

The second time you run this script, files are found to be present and the log shows this:

Here's what the GUI looks like:

See Also

SampleMgr API.

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