Public
Edited
Nov 3, 2023
3 forks
Importers
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
test = {
const button = htl.html`<button>TEST`;
button.onclick = () => {
instrument.triggerAttackRelease("G3", 0.5);
instrument.triggerAttackRelease("C3", 0.5, "+0.5");
};
return button;
}
Insert cell
Tone = require("tone")
Insert cell
instrumentTable = ({
"bass-electric": FileAttachment("bass-electric.zip"),
bassoon: FileAttachment("bassoon.zip"),
cello: FileAttachment("cello@1.zip"),
clarinet: FileAttachment("clarinet.zip"),
contrabass: FileAttachment("contrabass.zip"),
flute: FileAttachment("flute.zip"),
"french-horn": FileAttachment("french-horn.zip"),
"guitar-acoustic": FileAttachment("guitar-acoustic.zip"),
"guitar-electric": FileAttachment("guitar-electric.zip"),
"guitar-nylon": FileAttachment("guitar-nylon.zip"),
harmonium: FileAttachment("harmonium.zip"),
harp: FileAttachment("harp.zip"),
organ: FileAttachment("organ.zip"),
piano: FileAttachment("piano.zip"),
saxophone: FileAttachment("saxophone.zip"),
trombone: FileAttachment("trombone.zip"),
trumpet: FileAttachment("trumpet.zip"),
tuba: FileAttachment("tuba.zip"),
violin: FileAttachment("violin.zip"),
xylophone: FileAttachment("xylophone.zip")
})
Insert cell
instrumentNames = Object.keys(instrumentTable)
Insert cell
loadInstruments = {
const loadOneInstrument = async function (instrumentName) {
const zip = await instrumentTable[instrumentName].zip();
const s = await samples(zip);
const promise = new Promise((resolve, reject) => {
let sampler;
sampler = new Tone.Sampler(s, () => resolve(sampler));
});
const instrument = await promise;
instrument.toDestination();
return instrument;
};
const loadAll = async function (instrumentNames) {
let instruments = {};
for (let name of instrumentNames) {
instruments[name] = await loadOneInstrument(name);
}
return instruments;
};
return loadAll;
}
Insert cell
samples = async function (zip) {
const results = await Promise.allSettled(
zip.filenames.map((name) => zip.file(name).url())
);
const samples = {};
results.forEach((r, i) => {
const name = zip.filenames[i];
if (!name.includes("ogg")) return;
const note = name.replaceAll(/(^.*\/)|(\.ogg)/g, "").replace("s", "#");
samples[note] = r.value;
});
return samples;
}
Insert cell
instrument = {
const instruments = await loadInstruments([instrumentName]);
const instrument = instruments[instrumentName];
invalidation.then(() => {
instrument.releaseAll();
instrument.dispose();
});
return instrument;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more