Imagine the constraint: you're building a web game for Telegram Mini Apps. Your initial bundle budget is ruthless—no more than 300–500 KB, or users on spotty mobile connections will close the tab before it finishes loading. At the same time, the gameplay hinges on a meditative atmosphere backed by classical nocturnes (Chopin, Satie, Debussy, Tchaikovsky). Even compressed MP3s would blow the bundle up to 30–50 MB. And what if you also want the sound of every move to harmonize interactively with the music?
While building the block puzzle game Blocturne, developer Sergey Rozhkov (@gR3Y) found an elegant way out: he ditched audio files completely and built the entire sound engine on the browser's Web Audio API.
I dug into the technical internals of the project, and it reveals several subtle engineering traps that await anyone building complex web audio.
Notes Instead of Bytes: Packing 19 Pieces into 8 Kilobytes
Rather than storing pre-rendered audio tracks, the engine works directly with musical notation:
- Parsing MIDI into compact TypeScript structures. Machine-readable scores from open-source projects like Mutopia and MuseScore were cleaned up and sliced into musical phrases. All 19 classical pieces—stored as arrays of notes and rests—take up just 8 KB in the codebase.
- A sampled bank with pitch interpolation. To avoid flat-sounding pure oscillators, a real piano (Kawai samples from FreePats) was sampled in minor-third intervals. The mono AAC file came in at around 440 KB. In-between semitones are played on the fly simply by adjusting the audio node's
playbackRate. And while that file loads in the background, the game starts smoothly on lightweight oscillators. - Harmonic move binding. Player action sounds (placing a block, clearing a line) pull their key from the left-hand part of whichever piece is currently playing. Drop a tile, and the click rings out as a harmonic note inside a Chopin chord rather than a random beep.
Rhythmic Magic: Time Quantization
The biggest risk with interactive audio is cacophony. If a player taps rapidly out of tempo, lovely music turns into noise.
The fix was Time Quantization. Instead of firing immediately on pointerdown, a move's sound is snapped to a 1/32nd-note grid of the current measure (q(32)):
// Compute the nearest 1/32nd note boundary after `now`
private q(div: 16 | 32 = 32): number {
const now = this.audioCtx.currentTime;
const stepDuration = this.stepLen() * (16 / div);
const ref = this.files ? this.files.gridRef : this.nextTime;
const k = Math.floor((ref - now) / stepDuration);
// Return the scheduled timestamp for starting the note
return Math.max(now, ref - k * stepDuration);
}
A micro-delay of up to 70–100 ms isn't perceived by the human brain as input lag—instead, it feels remarkably satisfying, as if you're effortlessly tapping in perfect time with a complex piano piece.
The pluck itself then fires at that exact quantized timestamp:
function playAdaptivePluck(ctx: AudioContext, frequency: number, scheduledTime: number): void {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = "sine";
osc.frequency.setValueAtTime(frequency, scheduledTime);
// Exponential volume decay for the pluck
gain.gain.setValueAtTime(0.4, scheduledTime);
gain.gain.exponentialRampToValueAtTime(0.0001, scheduledTime + 0.35);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(scheduledTime);
osc.stop(scheduledTime + 0.35);
}
The Android Node Ambush and Hybrid Pre-Rendering
On desktop Chrome, everything ran flawlessly. The moment the game was tested on budget Android phones (like the POCO M4 Pro), though, it turned into a nightmare: audio stuttered, crackled, and choked.
Profiling showed it wasn't a raw CPU shortage. Android's WebAudio engine simply couldn't keep up with creating and destroying thousands of short-lived audio nodes (OscillatorNode, GainNode, StereoPannerNode). JavaScript's garbage collector (GC) paused to clean up, starving the continuous audio buffer. We ran into similar mobile runtime constraints when looking at audio capture in Manifest V3 and single-stream translation, and mobile WebAudio hits the same wall.
Instead of pure on-the-fly procedural synthesis on mobile, the game switched to a hybrid approach: before a piece starts, a background OfflineAudioContext quickly renders the base score into a single continuous buffer, leaving the live AudioContext to play only individual player-move notes. On top of that, a heavy ConvolverNode reverb was replaced with a lightweight chain of eight comb delay filters.
The result kept the tiny bundle size, full responsiveness to player moves, and a rock-solid 60 FPS even on entry-level phones. It's a great lesson: procedural audio works wonders as long as you respect mobile garbage collectors.

![Node.JS [ru]](/api/digests/it_development/daily/20261019/assets/sources/we-use-js.jpg?v=bf10c5582422a79e168ea74e644a1cc5b66afc9ff54835527b184a48f1dd7bbf)