Public
Edited
Oct 25, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function pitchToFrequency(pitch) {
return Math.pow(2, (pitch - tuning.note) / notesPerOctave) * tuning.frequency;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function makeTone(pitch) {
return new OscillatorNode(ctx, {
type: "sine", // Also try "triangle", "sawtooth", "square"
frequency: pitchToFrequency(pitch)
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
attackTime = 0.01
Insert cell
Insert cell
decayTime = 0.3
Insert cell
Insert cell
function makeEnvelope(startTime) {
const peak = startTime + attackTime;
const end = peak + decayTime;

const env = new GainNode(ctx, { gain: 0 });
env.gain.linearRampToValueAtTime(maxGain, peak);
env.gain.linearRampToValueAtTime(0, end);
return env;
}
Insert cell
Insert cell
function play(pitch) {
const tone = makeTone(pitch);
const env = makeEnvelope(ctx.currentTime);
tone.connect(env);
env.connect(ctx.destination);
tone.start();
return;
}
Insert cell
Insert cell
{
if (buttonClicks == 0) return;
play(midiNote);
}
Insert cell
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