Published
Edited
Feb 6, 2021
2 stars
Insert cell
Insert cell
Tone = require('tone')
Insert cell
Insert cell
oscs = makeOscs(20)
Insert cell
Insert cell
stop(0)
Insert cell
Insert cell
Insert cell
function makeOscs(num) {
let oscs = [];
for (let i = 1; i <= num; i++) {
const osc = new Tone.Oscillator().toDestination();
osc.frequency.value = 440*i;
osc.volume.value = loudness(1/i).toFixed(1);
// Above formula from http://www.sengpielaudio.com/calculator-levelchange.htm
oscs.push(osc);
}
return oscs;
}
Insert cell
function loudness(ratio) {
return 10 * Math.log2(ratio);
}
Insert cell
function start(num) {
for (let i = 0; i < num; i++) {
oscs[i].start();
}
return `Starting ${num} oscillators`
}
Insert cell
function stop(num) {
if (num == 0) {
oscs.forEach(osc => osc.stop());
return "Stopping all oscillators"
} else {
for (let i = num; i >= 0; i--) {
if (oscs[i]) oscs[i].stop();
}
return `Stopping ${num} oscillators`
}
}
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