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.

listRepos.js

In this example, we use SampleMgr to enumerate the default/factory sample sets known to SampleMgr. NB: this information is also available through the Sample Manager Dialog.

// list contents of default repolist.
const repolist = await SampleMgr.Init(); // default repolist
const reponames = repolist.GetRepoNames();
const banknames = repolist.GetBankNames();
console.log(`repolist has ${reponames.length} repos ${reponames}`);
console.log(`  ${banknames.length} banks\n${banknames.join("\n")}`);
for(let banknm of ["MIDIJS/FatBoy", "VCSL", "DrumKits/CasioVL1"])
{
    let voices = repolist.GetBank(banknm).GetVoiceNames();
    console.log(`bank ${banknm} has ${voices.length} voices: ${voices.slice(0,5)}...`);
}

samplemgr.js

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:

remotemap.js

Here, we present the means to query remote or local samplemap files in a Strudel-compatible fashion. Note that caching is not part of this example.

const percSounds = ["sd:0", "bd:0", "hh"];
const tonalSounds = ["electric_piano_1"];
const maps = [
    ["github:eddyflux/crate", percSounds],
    ["github:eddyflux/wax", percSounds],
    ["github:tidalcycles/dirt-samples", percSounds],
    ["Hz:MIDIJS/FluidR3GM", tonalSounds],
];
let [remoteMap, sounds] = maps[0];  // <-- select a map

console.log(`FetchSampleMap: ${remoteMap}`);
let sampleMap = await SampleMgr.FetchSampleMap(remoteMap);

// Optional: enumerate the remote URLs associated with the sounds refererenced.
let urls = SampleMgr.GetSampleURLs(sampleMap, sounds);
console.log(`resolving ${sounds} => ${JSON.stringify(urls, null, 2).slice(0,400)}...`);

Here's some example output:

12:13 note FetchSampleMap: github:eddyflux/crate
12:13 note resolving sd:0,bd:0,hh => {
"sd": [
"https://raw.githubusercontent.com/eddyflux/crate/main/crate_sd/snare-aprilshowers-1.wav"
],
"bd": [
"https://raw.githubusercontent.com/eddyflux/crate/main/crate_bd/kick-takecare-1.wav"
],
"hh": [
"https://raw.githubusercontent.com/eddyflux/crate/main/crate_hh/closedhh-deepfind.wav",
"https://raw.githubusercontent.com/eddyflux/crate/main/crate_hh/closedhh-loveso...

playremotemap.js

This example is not reproduced here since it's a page or more of code. Just right-click-copy this example and inspect and run it from your workspace. playremotemap is a continuation of the prior example that additionally ensures the sample files are cached locally. Next we initialize Hz.Samplo and finally play a few sounds.

See Also

SampleMgr API.

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