Bass to MIDI
@webgamekit/bass-midi listens to a bass plugged into an audio interface and reports each note
it hears, live, as a MIDI note event. The editor's Instrument tab
(?tab=instrument) is a small front end for it, for a bass or a guitar:
- The Instrument dropdown picks Bass (the default) or Guitar; the page remembers
it. A link can name it too:
?tab=instrument&instrument=guitar. The calibration, synth sound, tuner, fretboard and downloaded take follow the instrument. What follows describes the bass; a guitar works the same on its six strings.

Trying it without an instrument
Add demo=1 to the address and press Play: the tab listens to a looping take of synthetic
plucks instead of your input, on the default calibration.
- Guitar: strudel.cnotv.xyz/?tab=instrument&instrument=guitar&demo=1
plays
E0 A2 D2 G0 B1 e0 e12. - Bass: strudel.cnotv.xyz/?tab=instrument&instrument=bass&demo=1
plays
E0 E12 A0 D5 G0 G12.
Monitor plays the take at its own level, without the boost a real input gets. A calibration pluck taken in the demo is not saved.
-
Play (beside the gate) starts and stops listening.
-
The note sounding now shows as its tablature position:
E0for the open low E,E17for A2. Pitch alone cannot tell which string was played, so the position is on the lowest string that reaches the note, where the hand stays when working up one string. All frets marks every place the note can be played on the 24-fret fretboard and lists them all (E17, A12, D7, G2). MIDI adds the MIDI number (E17 · MIDI 45). Both apply to the log too, and the page remembers them. -
Tune (the gauge) shows the nearest open string and how many cents it is sharp or flat, with a meter; within 3 cents it reads in tune. It starts listening if needed.
-
Each finished note goes into the note log. Download MIDI saves the take as a
.midfile and Copy JSON copies it as a list of{ midi, time, duration, velocity }, both timed from the first note, for a DAW or another library to load. The input settings sit above the log, collapsed. -
Monitor and Synth play the bass back through the default output. Monitor passes the raw input through, boosted 30 times at 100%, since an instrument input reaches the browser far quieter than a mixed sound. Synth plays each detected note on a Strudel sound picked beside it, Electric bass finger until another is chosen; any synth, soundfont or instrument preset works. A synth note starts when the note is detected, a little after the pluck, and fades out when it ends. Both sliders go to 200%. Monitor starts at 100%, so the bass is heard as soon as listening starts, and Synth at 0; the page remembers them and the sound.



Using the library
The tab is built on @webgamekit/bass-midi, published on npm. Installing it, how notes map to
MIDI, how one note sits on several frets, the full API and recipes for recording, practice and
a rhythm game are in Using @webgamekit/bass-midi.
How it works
The detection is a Rust crate in packages/bass-midi/crate, compiled to WebAssembly and run in
an AudioWorklet, so it works on the audio thread and never waits on the page.
- The input is low-passed and decimated to about 12 kHz: a bass's fundamentals stay under 400 Hz, so nothing is lost and the next step costs a sixteenth as much.
- Every 5 ms, the McLeod pitch method reads the pitch of the last 64 ms. It takes the first strong period rather than the strongest, which keeps the bass's loud harmonics from reading as the wrong octave. The McLeod pitch method below explains it.
- A segmenter turns those readings into notes. A note starts when the level is above the gate and three readings in a row agree; it averages them before rounding to a semitone, so a note played a little sharp does not read as the one above. It ends when the level drops below the release level, a level jump marks a new pluck, or the pitch moves to another note without one (a slide or hammer-on). That move must be at least three quarters of a semitone, so a wobble cannot flicker between neighbours; it must happen while the string is still above the gate; and an exact octave is ignored, because it is the string's second harmonic taking over as the fundamental fades, not a new note.
After a pluck only the samples since that pluck are analysed, so the previous note cannot leak into the new one. A high note is recognised within about 20 ms; the low E needs about 50 ms, since one and a half periods of it must arrive before its pitch can be read.
The McLeod pitch method
A plucked string repeats the same wave shape over and over. The time one repeat takes is the period, and the pitch is one over the period: the low E repeats every 24.3 ms, which is 41.2 Hz. So finding the pitch means finding the period. The McLeod pitch method (MPM), from Philip McLeod and Geoff Wyvill's paper A Smarter Way to Find Pitch (2005), does this in three steps.
1. Compare the wave with a shifted copy of itself
Slide a copy of the window along by a lag τ (tau) and measure how well the copy lines up with
the original. MPM uses the normalised square difference function (NSDF):
2 · Σ x[j] · x[j + τ]
n(τ) = ───────────────────────────────────
Σ ( x[j]² + x[j + τ]² )
The sums run over every sample where the original and the shifted copy overlap. n(τ) is
always between -1 and 1:
- 1 means the copy lines up exactly, so
τis a whole number of periods; - 0 means no relation;
- -1 means the copy is upside down, as at half a period of a sine.
The bottom line is what makes this better than plain autocorrelation (the top line alone). It divides by the energy of the same overlap, so the result does not depend on how loud the note is, and it does not shrink at long lags where fewer samples overlap. A soft low E and a loud high G give equally tall peaks.
2. Take the first strong peak, not the tallest
A wave that repeats every period also repeats every two periods, three periods, and so on.
n(τ) therefore has a peak at every multiple of the period, and on a real string the later ones
can be just as tall. Taking the tallest peak would sometimes pick two periods and report the note
an octave too low.
MPM looks only at the peaks after the curve first goes negative, keeping the highest point of each positive lobe (its "key maxima"). It then takes the first key maximum that reaches 90 % of the tallest one. That is the shortest lag at which the wave clearly repeats: one period.
Bass harmonics push the other way. A string's second harmonic repeats every half period, which
could suggest the octave above. But at half a period the fundamental itself is upside down, so
n(τ) is negative there and that lag never becomes a key maximum.
3. Refine the peak between samples
The lag is counted in whole samples, which is too coarse for high notes. At 12 kHz the G4 at the 24th fret has a period of about 30 samples, and the gap between 30 and 31 samples is 57 cents, over half a semitone. A parabola fitted through the peak and its two neighbours finds the true top between samples. The pitch is the sample rate divided by that refined lag.
The height of the peak is the clarity: close to 1 for a clean, steady note, low for noise,
string scrape or the moment two notes overlap. A reading counts only if its clarity reaches the
clarity parameter (0.8 by default), so noise never becomes a note.
The code is in packages/bass-midi/crate/src/mpm.rs.
Calibration
Pickups and interfaces differ. Every knob has a default and can change while listening:
| Parameter | Default | What it does |
|---|---|---|
gateDb | -45 | Level a silent string must rise above to start a note |
releaseDb | -55 | Level a sounding note must fall below to end |
clarity | 0.8 | How periodic the signal must be to count as a pitch (0 to 1) |
stableHops | 3 | 5 ms readings a semitone must hold before it becomes a note |
a4Hz | 440 | Tuning reference |
minNoteMs | 60 | Shortest note a re-pluck may cut off |
repluckJumpDb | 6 | Level rise that counts as a new pluck of the same note |
Raise the gate if string noise or hum starts notes; lower it if soft notes are missed.
Checking playback without a bass
Monitor and Synth can be checked headless and muted, with no bass and nothing through the
speakers. Headless Chromium never resolves getUserMedia here, even with its fake media flags,
so the check replaces it before the page loads with a stream played from a second
AudioContext:
navigator.mediaDevices.getUserMedia = async () => {
const sourceContext = new AudioContext()
const pluck = new OscillatorNode(sourceContext, { type: 'sawtooth', frequency: 55 })
const envelope = new GainNode(sourceContext, { gain: 0 })
const stream = sourceContext.createMediaStreamDestination()
pluck.connect(envelope).connect(stream)
pluck.start()
Array.from({ length: 6 }, (_, index) => {
const time = sourceContext.currentTime + 0.3 + index * 0.6
envelope.gain.setValueAtTime(0.5, time)
envelope.gain.setTargetAtTime(0, time + 0.35, 0.02)
})
return stream.stream
}
That is six plucks of an A1 (55 Hz) sawtooth, each held 0.35 seconds, 0.6 seconds apart. To hear what the tab is given, listen to bass-test-input.wav, rendered from the same recipe.
- Monitor: wrap
AudioNode.prototype.connectso every node connected to a destination also feeds anAnalyserNode, start listening, and read the peak. The monitor's peak is the input's times its gain, which is 30 at 100%. - Synth: wrap
AudioBufferSourceNode.prototype.startand record each start'splaybackRate. Six plucks start seven voices: one silent, which loads the sample, and one per pluck. The default instrument's A sample is at 110 Hz, so a rate of 0.5 is the A1 played. - The instrument itself: render it through the export with
scripts/strudel-check.mjs(see Checking a sound without listening), for examplenote("e1 a1").s("gm_electric_bass_finger"), which gives bass-synth-electric-bass-finger.wav and this spectrogram:

Keep the page's volume at 0 (strudel-editor-volume in localStorage) as for any agent run;
the headless browser is muted as well.
Changing the Rust code
The built src/bass_midi.wasm is committed, so the site builds without Rust. After changing
the crate, rebuild it and commit the result:
pnpm build:wasm
The rust CI job runs cargo test, cargo fmt --check and cargo clippy, rebuilds the
module, and fails if the committed one differs. The toolchain version is pinned in
crate/rust-toolchain.toml, which keeps the build byte for byte the same on every machine.
Limits
- One note at a time: chords and double stops are not detected.
- A 4-string bass (E1 to G4) or a 6-string guitar (E2 to E6), in standard tuning. A 5-string bass or a drop tuning needs other open strings, and a low one a longer analysis window.
- The guitar's default calibration is estimated from the stiffness of a 10 to 46 electric string set, not measured on a real guitar, so treat its string guess as a hint and calibrate your own.