ENGINEERING NOTES
Building a 3 MB Offline Audio Synthesizer for Windows in Rust & Tauri v2
I built Resona because I wanted a distraction-free Windows focus tool that synthesizes binaural beats, isochronic tones, and colored noise in real time—without YouTube ads, monthly subscriptions, or a 300 MB Electron runtime. The shipped x64 MSIX package ended up at 2.88 MB, runs 100% offline, and passed Microsoft Store certification on the first try across v1.0, v1.1, and v1.2.
Along the way I hit four surprisingly subtle Windows + Rust audio/platform issues that rarely show up in tutorials. Here is what actually happened and how each one was solved.
1. Replacing cpal with Native WASAPI Shared-Mode Push
cpal worked immediately on wired Realtek speakers, but on Bluetooth earbuds
pure tones produced occasional faint crackles. Inspecting cpal 0.15's Windows
WASAPI path surfaced four compounding issues:
-
BufferSize::DefaultinitializesIAudioClientwithhnsBufferDuration = 0(~10 ms), leaving very little cushion for 20–40 ms Bluetooth A2DP radio bursts. -
Hardcoded
AUDCLNT_STREAMFLAGS_EVENTCALLBACKcoupled with starting the stream without pre-filling the shared ring buffer caused initial jitter. -
A thread-priority quirk in
cpal 0.15.3passesGetCurrentThreadId()(a thread ID) instead ofGetCurrentThread()(a handle) toSetThreadPriority. -
Channel masks defaulted to
KSAUDIO_SPEAKER_DIRECTOUTrather than passing Windows' exactGetMixFormat()back verbatim.
Writing a compact ~250-line native Windows WASAPI Shared-Mode Push backend directly with the
windows crate solved all four: pass GetMixFormat() verbatim,
request a 150 ms shared ring buffer (flags = 0, no event callback), pre-fill
the buffer before calling Start(), push frames on a 15 ms cadence (~135 ms
headroom), and elevate the audio thread with real
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) plus
Windows MMCSS "Pro Audio" (AvSetMmThreadCharacteristicsW).
2. The Weird One: Bluetooth SBC Subband Starvation on Pure Sine Waves
Even with zero buffer underruns on native WASAPI Push, budget Bluetooth earbuds still
clicked occasionally on pure tones with zero background noise—yet played noise-only
or tone+pink-noise completely cleanly. Generating a static .wav of a pure
200/215 Hz binaural sine and playing it in Windows Media Player reproduced the exact same
crackles on Bluetooth while playing 100% cleanly on wired speakers.
Rather than polluting studio/wired/USB audio with global dither, Resona queries the active
audio endpoint's read-only DEVPKEY_Device_EnumeratorName property. Only when
Windows reports a Bluetooth enumerator (BTHENUM / BTHHFENUM)
and the user is playing a pure tone with zero background noise, the Windows
output layer gently fades in an empirically calibrated, imperceptible band-limited comfort
floor (18.8–20.2 kHz at 0.009 amplitude, 50 ms ramp). Wired, USB,
and speaker outputs stay bit-exact untouched.
3. Why f32 Ramp Accumulators Break on 2-Minute DSP Glides
Resona's DSP core (resona-audio-core) runs with zero allocations and zero locks
on the audio thread, using per-sample linear ramps for click-free frequency and gain
transitions. In v1.2, users requested gradual 30 s, 60 s, and 120 s stage transition ramps
(for example gliding slowly from 15 Hz Beta down to 10 Hz Alpha).
With an f32 accumulator, a 5 Hz shift over 120 seconds at 48 kHz
(5,760,000 samples) has a per-sample step of 8.68e-7. At
200.0 Hz, the ULP of f32 is 1.52e-5—meaning
200.0_f32 + 8.68e-7_f32 == 200.0_f32 on every single sample! The frequency
would sit frozen for 119.999 seconds and then snap abruptly on the final sample.
struct Ramp {
value: f64,
target: f64,
step: f64,
left: u32,
}
impl Ramp {
fn set(&mut self, target: f32, samples: u32) {
let t = target as f64;
if self.target == t { return; }
self.target = t;
self.left = samples.max(1);
self.step = (t - self.value) / self.left as f64;
}
#[inline]
fn next(&mut self) -> f32 {
if self.left > 0 {
self.left -= 1;
self.value = if self.left == 0 { self.target } else { self.value + self.step };
}
self.value as f32
}
}
Upgrading the internal ramp state to f64 keeps 120-second glides monotonic and
exact even at 192 kHz (23M samples) with zero overhead in steady state.
4. Deadlock-Free Microsoft Store Durable IAP in Tauri v2
Switching Resona v1.2 to a Free Forever core + optional one-time Resona Pro
durable add-on meant integrating Windows.Services.Store.StoreContext in Rust.
Two WinRT desktop rules are critical here:
-
Desktop (
runFullTrust) MSIX apps must bindStoreContextto their mainHWNDviawindows::Win32::UI::Shell::IInitializeWithWindow. -
Never call
RequestPurchaseAsync(...).get()synchronously from an STA thread or a tokio blocking worker without a Win32 message pump—the native Store checkout dialog can deadlock waiting to marshal back to the apartment.
The clean Tauri v2 pattern splits the purchase into three stages: resolve the add-on
StoreId and localized price on a background MTA thread via
GetAssociatedStoreProductsAsync(["Durable"]); kick off
StoreContext::GetDefault() +
IInitializeWithWindow::Initialize(hwnd) + RequestPurchaseAsync
inside window.run_on_main_thread(...) using a non-blocking
AsyncOperationCompletedHandler that signals a channel and returns in <1 ms;
and verify ownership offline via GetAppLicenseAsync().AddOnLicenses() (backed
by Windows' signed local Store license cache).
Try the Finished Build
Resona's core audio engine—binaural beats, isochronic tones, White/Pink/Brown noise, unlimited session durations, sleep timer, and system tray—is free forever on Windows with zero ads or accounts.
Get Resona Free on Microsoft Store Hear Live Browser Preview