Skip to content

Audio Engine

buntatoes edited this page Nov 28, 2026 · 1 revision

Audio Engine

Vodka provides a native Linux audio runtime (libvodka_audio.so and vodka-audio under vodka/src/audio/) paired with Win32 audio DLLs (mmdevapi.dll, dsound.dll, xaudio2_9.dll, winmm.dll) and ntdll audio thunks (audio_thunks.zig).

All host audio libraries are loaded dynamically via dlopen at runtime — no PipeWire or ALSA development headers are required to build Vodka, and headless/sandboxed environments automatically fall back to a deterministic software sink so tests and CI run cleanly.


Architecture Overview

flowchart LR
    Apps["Windows Apps / Games"] --> DLLs["mmdevapi.dll / xaudio2_9.dll / dsound.dll / winmm.dll"]
    DLLs --> Thunks["libvodka_ntdll.so (audio_thunks.zig / net_input_thunks.zig)"]
    Thunks --> AudioLib["libvodka_audio.so (unixlib.zig)"]
    AudioLib --> WASAPI["WASAPI Engine (wasapi.zig)"]
    WASAPI --> Ring["Lock-Free SPSC PCM Ring Buffer (ringbuf.zig)"]
    Ring --> PW["PipeWire Backend (libpipewire-0.3.so.0)"]
    Ring --> ALSA["ALSA PCM Backend (libasound.so.2)"]
    Ring --> Soft["Software Sink Fallback (Headless)"]
Loading

Core Modules (vodka/src/audio/)

1. Lock-Free SPSC PCM Ring Buffer & Mixer (ringbuf.zig)

  • PcmRingBuffer: Single-producer, single-consumer lock-free ring buffer with power-of-two frame capacity (minimum 64 frames) and atomic read_pos / write_pos indices plus atomic underruns and overruns counters.
  • Sample Formats (SampleFormat):
    • i16_le (S16_LE, 2 bytes/sample)
    • i24_le (S24_3LE, 3 bytes/sample)
    • i32_le (S32_LE, 4 bytes/sample)
    • f32_le (F32_LE, 4 bytes/sample)
  • Format Helpers & Mixing: Frame/byte/duration conversion (including Windows 100-nanosecond REFERENCE_TIME intervals), sample format conversion, multi-channel mixing (mono, stereo, 5.1), and soft-clipping gain/mute ramps.

2. Native WASAPI Engine (wasapi.zig)

Implements the Windows Audio Session API core state machine:

  • EndpointRegistry & AudioEndpoint: Tracks render (AudioDataFlow.render) and capture (AudioDataFlow.capture) endpoints, default device flags, active status, mix format, and backend tag.
  • AudioSession: Per-session volume/mute state and smooth gain ramping.
  • AudioClient: Supports .shared and .exclusive share modes, stream lifecycle (uninitialized, stopped, running, paused, reset), getCurrentPadding, getRenderBuffer / releaseRenderBuffer (honoring AUDCLNT_BUFFERFLAGS_SILENT), capture read cycles, and event-driven buffer readiness notifications.

3. Dynamic PipeWire Backend (pipewire.zig)

  • Dynamically loads libpipewire-0.3.so.0 via dlopen (RTLD_NOW | RTLD_LOCAL).
  • Resolves pw_init, pw_deinit, pw_get_library_version, pw_thread_loop_new/destroy/start/stop/lock/unlock/get_loop, and pw_stream_destroy/set_active.
  • Probes daemon socket reachability at $PIPEWIRE_RUNTIME_DIR/pipewire-0 (or $XDG_RUNTIME_DIR/pipewire-0).
  • Populates render/capture endpoints and falls back smoothly to the software ring buffer if the daemon socket is absent.

4. Dynamic ALSA PCM Backend (alsa.zig)

  • Dynamically loads libasound.so.2 (falling back to libasound.so) via dlopen.
  • Resolves snd_asoundlib_version, snd_pcm_open, snd_pcm_close, snd_pcm_set_params, snd_pcm_writei, snd_pcm_readi, snd_pcm_drain, snd_pcm_drop, snd_pcm_prepare, and snd_pcm_recover.
  • Supports playback and capture (SND_PCM_STREAM_PLAYBACK / SND_PCM_STREAM_CAPTURE), interleaved access, and S16_LE, S32_LE, and FLOAT_LE PCM formats.

5. C-ABI Shared Library (libvodka_audio.so / unixlib.zig)

Exports the host audio runtime boundary with up to 64 concurrent stream slots (MAX_STREAMS = 64) and automatic backend selection (PipeWire -> ALSA -> software):

  • vodka_audio_init() / vodka_audio_shutdown()
  • vodka_audio_backend_kind() (0 = software, 1 = PipeWire, 2 = ALSA)
  • vodka_audio_backend_version(buf, len)
  • vodka_audio_enum_endpoints(flow, out_array, max_count) using C-ABI CEndpointInfo
  • Stream open, start, stop, render write, capture read, and close entry points

XAudio2, DirectSound & WinMM Thunks

  • audio_thunks.zig (libvodka_ntdll.so): Probes PipeWire/ALSA, provides WASAPI IAudioClient / IAudioRenderClient COM vtables, and resolves XAudio2Create, DirectSoundCreate, and DirectSoundCreate8 directly for xaudio2_9.dll and dsound.dll.
  • net_input_thunks.zig (libvodka_ntdll.so): Bridges winmm.dll multimedia timing and audio APIs.
  • PE DLLs: mmdevapi.dll, audioses.dll, dsound.dll, xaudio2_9.dll (plus versioned XAudio2/X3DAudio/XAPOFX modules), and winmm.dll are cross-compiled for x86_64, i386, and aarch64 and verified via vodka_phase5_media_test.exe.

Diagnostic & Self-Test CLI (vodka-audio)

vodka-audio (zig-out/bin/vodka-audio) lets you inspect the host audio stack and verify WASAPI ring-buffer operation from the command line:

# Probe active backend (pipewire / alsa / software), library version, daemon socket, and endpoint count
./zig-out/bin/vodka-audio --probe

# List discovered render and capture audio endpoints with sample rate and channel count
./zig-out/bin/vodka-audio --list

# Run an end-to-end 48kHz stereo f32 440Hz sine-wave WASAPI render & drain self-test
./zig-out/bin/vodka-audio --selftest

# Print version or usage
./zig-out/bin/vodka-audio --version
./zig-out/bin/vodka-audio --help

Clone this wiki locally