Building a sample-free synth in the browser
A digital instrument playable in a web page needs three things: markup for the keys, CSS to make them look and respond like keys, and JavaScript that turns note names into audio. This implementation uses plain HTML, CSS and JavaScript, one library, no audio samples. The sound is generated through the AudioContext API.
Keyboard layout and markup
The keybed follows a standard western computer keyboard: every letter from A through ; maps to a natural note (white key), and the row above handles sharps and flats (black keys). The range runs from C₃ to E₄, a little over an octave.
Each key stores its note value in a custom note attribute for easy access from JavaScript, and prints the corresponding computer key on the keycap so users know what to press.
<ul id="keyboard">
<li note="C" class="white">A</li>
<li note="C#" class="black">W</li>
<li note="D" class="white offset">S</li>
<li note="D#" class="black">E</li>
<li note="E" class="white offset">D</li>
<li note="F" class="white">F</li>
<li note="F#" class="black">T</li>
<li note="G" class="white offset">G</li>
<li note="G#" class="black">Y</li>
<li note="A" class="white offset">H</li>
<li note="A#" class="black">U</li>
<li note="B" class="white offset">J</li>
<li note="C2" class="white">K</li>
<li note="C#2" class="black">O</li>
<li note="D2" class="white offset">L</li>
<li note="D#2" class="black">P</li>
<li note="E2" class="white offset">;</li>
</ul>
Styling the keyboard
The stylesheet starts with color variables — changing --keyboard and --keyboard-border alters the result substantially. Key styling, particularly the pressed states, takes its inspiration from the CodePen by zastrow.
:root {
--keyboard: hsl(300, 100%, 16%);
--keyboard-shadow: hsla(19, 50%, 66%, 0.2);
--keyboard-border: hsl(20, 91%, 5%);
--black-10: hsla(0, 0%, 0%, 0.1);
--black-20: hsla(0, 0%, 0%, 0.2);
--black-30: hsla(0, 0%, 0%, 0.3);
--black-50: hsla(0, 0%, 0%, 0.5);
--black-60: hsla(0, 0%, 0%, 0.6);
--white-20: hsla(0, 0%, 100%, 0.2);
--white-50: hsla(0, 0%, 100%, 0.5);
--white-80: hsla(0, 0%, 100%, 0.8);
}
.white,
.black {
position: relative;
float: left;
display: flex;
justify-content: center;
align-items: flex-end;
padding: 0.5rem 0;
user-select: none;
cursor: pointer;
}
Rounding the first and last key with a specific border radius keeps the design organic — with sharp corners, the top left and top right of the keybed look unnatural.
#keyboard li:first-child {
border-radius: 5px 0 5px 5px;
}
#keyboard li:last-child {
border-radius: 0 5px 5px 5px;
}
White keys sit at z-index: 1 and black keys at z-index: 2. A "pressed" class, applied by JavaScript, drives the active styling.
.white {
height: 12.5rem;
width: 3.5rem;
z-index: 1;
border-left: 1px solid hsl(0, 0%, 73%);
border-bottom: 1px solid hsl(0, 0%, 73%);
border-radius: 0 0 5px 5px;
box-shadow: -1px 0 0 var(--white-80) inset, 0 0 5px hsl(0, 0%, 80%) inset,
0 0 3px var(--black-20);
background: linear-gradient(to bottom, hsl(0, 0%, 93%) 0%, white 100%);
color: var(--black-30);
}
.black {
height: 8rem;
width: 2rem;
margin: 0 0 0 -1rem;
z-index: 2;
border: 1px solid black;
border-radius: 0 0 3px 3px;
box-shadow: -1px -1px 2px var(--white-20) inset,
0 -5px 2px 3px var(--black-60) inset, 0 2px 4px var(--black-50);
background: linear-gradient(45deg, hsl(0, 0%, 13%) 0%, hsl(0, 0%, 33%) 100%);
color: var(--white-50);
}
Some white keys must shift left to sit under the black keys; those carry an "offset" class so the CSS stays simple.
.offset {
margin: 0 0 0 -1rem;
}
#keyboard {
height: 15.25rem;
width: 41rem;
margin: 0.5rem auto;
padding: 3rem 0 0 3rem;
position: relative;
border: 1px solid var(--keyboard-border);
border-radius: 1rem;
background-color: var(--keyboard);
box-shadow: 0 0 50px var(--black-50) inset, 0 1px var(--keyboard-shadow) inset,
0 5px 15px var(--black-50);
}
Pitch from note names
New audio contexts come from:
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
The note elements are queried and stored in an object keyed by the computer key that plays them; each entry holds the note name and an octaveOffset used to compute pitch.
const getElementByNote = (note) =>
note && document.querySelector(`[note="${note}"]`);
const keys = {
A: { element: getElementByNote("C"), note: "C", octaveOffset: 0 },
W: { element: getElementByNote("C#"), note: "C#", octaveOffset: 0 },
S: { element: getElementByNote("D"), note: "D", octaveOffset: 0 },
E: { element: getElementByNote("D#"), note: "D#", octaveOffset: 0 },
D: { element: getElementByNote("E"), note: "E", octaveOffset: 0 },
F: { element: getElementByNote("F"), note: "F", octaveOffset: 0 },
T: { element: getElementByNote("F#"), note: "F#", octaveOffset: 0 },
G: { element: getElementByNote("G"), note: "G", octaveOffset: 0 },
Y: { element: getElementByNote("G#"), note: "G#", octaveOffset: 0 },
H: { element: getElementByNote("A"), note: "A", octaveOffset: 1 },
U: { element: getElementByNote("A#"), note: "A#", octaveOffset: 1 },
J: { element: getElementByNote("B"), note: "B", octaveOffset: 1 },
K: { element: getElementByNote("C2"), note: "C", octaveOffset: 1 },
O: { element: getElementByNote("C#2"), note: "C#", octaveOffset: 1 },
L: { element: getElementByNote("D2"), note: "D", octaveOffset: 1 },
P: { element: getElementByNote("D#2"), note: "D#", octaveOffset: 1 },
semicolon: { element: getElementByNote("E2"), note: "E", octaveOffset: 1 }
};
Frequencies follow x * 2^(y / 12), where x is the Hz value of a reference note — typically A₄ at 440Hz — and y is the count of semitones above or below it.
const getHz = (note = "A", octave = 4) => {
const A4 = 440;
let N = 0;
switch (note) {
default:
case "A":
N = 0;
break;
case "A#":
case "Bb":
N = 1;
break;
case "B":
N = 2;
break;
case "C":
N = 3;
break;
case "C#":
case "Db":
N = 4;
break;
case "D":
N = 5;
break;
case "D#":
case "Eb":
N = 6;
break;
case "E":
N = 7;
break;
case "F":
N = 8;
break;
case "F#":
case "Gb":
N = 9;
break;
case "G":
N = 10;
break;
case "G#":
case "Ab":
N = 11;
break;
}
N += 12 * (octave - 4);
return A4 * Math.pow(2, N / 12);
};
The function accepts flats even though the rest of the code uses only sharps, keeping it reusable elsewhere. A# and Bb describe the same pitch; the choice between them matters only in the context of a particular key.
Triggering and releasing sound
A Map tracks which notes are currently playing. Its unique-key constraint stops the same note firing more than once on a single press, and since a user clicks only one key at a time, the value can be stored as a string.
const pressedNotes = new Map();
let clickedKey = "";
Two functions cover playback: one fires on keydown or mousedown, the other on keyup or mouseup. Each key gets its own oscillator, its own gain node for volume, and its own waveform for timbre. The example uses "triangle"; "sine", "sawtooth" and "square" are the other options.
const playKey = (key) => {
if (!keys[key]) {
return;
}
const osc = audioContext.createOscillator();
const noteGainNode = audioContext.createGain();
noteGainNode.connect(audioContext.destination);
noteGainNode.gain.value = 0.5;
osc.connect(noteGainNode);
osc.type = "triangle";
const freq = getHz(keys[key].note, (keys[key].octaveOffset || 0) + 4);
if (Number.isFinite(freq)) {
osc.frequency.value = freq;
}
keys[key].element.classList.add("pressed");
pressedNotes.set(key, osc);
pressedNotes.get(key).start();
};
Stopping a key lets the note ring out for a set time after release — around two seconds — and applies the visual change.
const stopKey = (key) => {
if (!keys[key]) {
return;
}
keys[key].element.classList.remove("pressed");
const osc = pressedNotes.get(key);
if (osc) {
setTimeout(() => {
osc.stop();
}, 2000);
pressedNotes.delete(key);
}
};
document.addEventListener("keydown", (e) => {
const eventKey = e.key.toUpperCase();
const key = eventKey === ";" ? "semicolon" : eventKey;
if (!key || pressedNotes.get(key)) {
return;
}
playKey(key);
});
document.addEventListener("keyup", (e) => {
const eventKey = e.key.toUpperCase();
const key = eventKey === ";" ? "semicolon" : eventKey;
if (!key) {
return;
}
stopKey(key);
});
for (const [key, { element }] of Object.entries(keys)) {
element.addEventListener("mousedown", () => {
playKey(key);
clickedKey = key;
});
}
document.addEventListener("mouseup", () => {
stopKey(clickedKey);
});
Most listeners attach to the document, but the click listeners use the keys object to target the already-queried elements. The highest note gets special handling: the ";" key must be converted to the spelled-out "semicolon" used as its key in keys.
Taming the tone
As built, the synth sounds shrill. Dropping the keyboard an octave is one fix, applied to the expression assigned to the freq constant:
const freq = getHz(keys[key].note, (keys[key].octaveOffset || 0) + 3);
A click at the start and end of each sound comes from abrupt gain changes. Music production describes these with attack (how fast a sound rises from silence to full volume), decay (time from peak to sustained volume) and release (how long a sound takes to fade after it stops being played). The noteGainNode's gain property exposes exponentialRampToValueAtTime, which shapes all three. Swapping in this version of playKey produces a much nicer plucky sound:
const playKey = (key) => {
if (!keys[key]) {
return;
}
const osc = audioContext.createOscillator();
const noteGainNode = audioContext.createGain();
noteGainNode.connect(audioContext.destination);
const zeroGain = 0.00001;
const maxGain = 0.5;
const sustainedGain = 0.001;
noteGainNode.gain.value = zeroGain;
const setAttack = () =>
noteGainNode.gain.exponentialRampToValueAtTime(
maxGain,
audioContext.currentTime + 0.01
);
const setDecay = () =>
noteGainNode.gain.exponentialRampToValueAtTime(
sustainedGain,
audioContext.currentTime + 1
);
const setRelease = () =>
noteGainNode.gain.exponentialRampToValueAtTime(
zeroGain,
audioContext.currentTime + 2
);
setAttack();
setDecay();
setRelease();
osc.connect(noteGainNode);
osc.type = "triangle";
const freq = getHz(keys[key].note, (keys[key].octaveOffset || 0) - 1);
if (Number.isFinite(freq)) {
osc.frequency.value = freq;
}
keys[key].element.classList.add("pressed");
pressedNotes.set(key, osc);
pressedNotes.get(key).start();
};
The values passed to setAttack, setDecay and setRelease are stylistic choices rather than fixed rules; adjusting them changes the character of the sound. The synth is now working and ready to host. Further directions include volume control, octave switching, waveform selection, reverb or a low pass filter, or layering multiple oscillators per note. For implementing music theory concepts on the web more broadly, the source code of the tonal npm package is worth reading.



