instrawave-compose#
Compose rhythms with your voice, or with anything else you can do near a microphone. You teach the app a handful of named voices by example (say "doo" five times, tap the desk five times), then record a take in one pass: "doo baa doo-doo doo". The take is segmented into hits, each hit is classified back onto the voices, and the result is a timeline of timed, weighted events that renders to audio through an instrument bound to each voice.
A limitation of most rhythm entry tools is the keyboard: tapping a pad or clicking a grid discards the timing and dynamics a person can produce vocally without effort. Here the performed take is the source of truth. Times are kept exactly as performed (there is no quantization grid), and each hit's measured loudness becomes the note's strength, taken relative to the loudest hit in the take and compressed so soft hits stay audible while the ordering is preserved.
Voices, takes, and the timeline#
A voice is a named class of sound plus its training examples. Recording into a voice runs an onset detector (spectral flux against a local mean, with a sensitivity control, since a soft syllable and a sharp tap sit at very different flux levels) and opens the clip in an editor: the detected hits are markers on the waveform, auditioned by click, selected in groups by dragging or shift-clicking, and excluded with a button or the X key, so a cough or a stray detection is visible rather than silently poisoning the label. Clips persist (audio in IndexedDB, detections and features in localStorage) and reopen in the same editor later; deleting a clip removes its hits from the voice. Each committed hit is stored as a small feature vector: spectral centroid and rolloff, band energy ratios, zero-crossing rate, attack and decay times. Loudness is deliberately kept out of this vector; it is expression, not identity, so playing softly does not change which voice a hit lands on.
A take goes through the same detector, and each hit is classified by distance-weighted nearest neighbours over the pooled training set, z-scored per dimension. The timeline shows one lane per voice with a marker per hit, bar height showing strength. The classifier will sometimes be wrong, and the fix is one gesture: drag the marker to the right lane. "Learn" folds the corrected hit back into that voice's training set, so the labelling improves with use; there is no model to refit.
Takes are not persisted; the take is a performance, and re-recording one is the cheap operation. During playback each hit flashes as the playhead crosses it, ring size following the hit's strength, so the dynamics are visible in motion as well as in the bar heights.
Rendering#
The seam is src/render/types.ts: events in, audio out. Each voice is
bound either to a placeholder WebAudio synth preset (kick, snare, hat,
tom) or to an instrawave instrument (drum, dulcimer,
chime) with per-voice tuning overrides, edited as sliders under the voice.
Playback touches the GPU only when at least one bound instrument is
instrawave; a purely synth arrangement never asks for a device.
The instrawave instruments are held at their default geometry and
accuracy, so each has exactly one bake, computed on demand from the bake
bar (with progress and a cancel) and kept for the session. All per-voice
character travels as tuning, which is free: every voice bound to the same
instrument shares its bake, and one render call carries all their events,
each hit an event {t, set: {...tuning, vel}} with the voice's overrides
and its strength scaling the excitation (vel, or amp for the plucked
dulcimer; the equations are linear, so this is a pure scale). Traces come
back at the solver rate, are resampled by linear interpolation, levelled
per instrument (a pressure trace carries no absolute scale worth keeping
against a synth's), and mixed; relative volumes within one instrument stay
physical. Mixes are cached against the exact events and bindings, so
replaying an unchanged take is free.
Loudness to excitation strength is the only feature mapping for now. Duration to decay, vocal pitch to drum pitch, and brightness to strike position are natural next steps and the event structure leaves room for them.
Honest limitations#
Onset detection is offline and frame-based; timing is good to a few milliseconds on sharp attacks and worse on breathy ones. The classifier is nearest-neighbour over hand-picked features, which separates a dozen distinct sounds well but has no notion of a hit it has never seen; an unfamiliar sound lands on whatever is closest. Overlapping hits closer than 50 ms merge into one. The browser's microphone processing is asked to stand down (automatic gain control would rewrite the very dynamics being tracked), but a system-level AGC outside the browser will still flatten them. Bakes are not persisted (the drum's is a couple of hundred MB), so the first instrawave playback of a session pays the precompute, about 8 s for the drum on the development machine. A hit learned from a take stores only its features, so it cannot be re-auditioned later; clips can. A multi-event instrawave render costs one synthesis pass per event over the whole trace, so a very dense take renders slowly.
Running it#
npm install # expects ../instrawave alongside, per the vault layout
npm run dev # the page: voices, take, timeline, playback
npm test # onset, feature, classifier checks on synthetic audio
npm run build