Skip to content

Building and Testing

buntatoes edited this page Nov 29, 2026 · 1 revision

Building and Testing

Tonic uses a single pure-Zig build.zig build graph with zero external C toolchains, zero Python scripts, and zero bundled third-party libraries.


Prerequisites

  • Operating System: Linux (x86_64 primary development host; cross-compilation to Windows PE targets is handled natively by Zig).
  • Zig: 0.17.1+ installed and available on PATH.
  • Optional Runtime Dependency (Linux only): speech-dispatcher (libspeechd.so.2) is not required at build time or runtime; Tonic probes it dynamically via dlopen when present and falls back to its built-in formant synthesizer when absent.

Branches

Branch Purpose
unstable Default branch on GitHub for active development (0.1.0-dev).
stable Promoted branch tracked by Pisco as the tonic submodule.

Build Commands & Options

git clone https://github.com/buntatoes/tonic.git
cd tonic

# Standard release-safe build: native Linux CLI, libtonic.so, and all 3 Windows PE sapi.dll targets
zig build -Doptimize=ReleaseSafe

# Debug build (preserves debug symbols; disables stripping)
zig build

# Host Linux CLI and libtonic.so only (skip Windows PE cross-compilation)
zig build -Doptimize=ReleaseSafe -Dpe-dlls=false

# Build only specific Windows PE architectures
zig build -Doptimize=ReleaseSafe -Dpe-archs=x86_64,i386

build.zig Options Reference

Option Default Description
-Doptimize=<mode> Debug Standard Zig optimization mode (Debug, ReleaseSafe, ReleaseFast, ReleaseSmall). Non-debug builds strip symbols (strip = (optimize != .debug)).
-Dtarget=<triple> Native host Target triple for the native CLI (tonic) and shared library (libtonic.so).
-Dpe-dlls=<bool> true Enables or disables cross-compilation of multi-architecture Windows PE sapi.dll targets.
-Dpe-archs=<list> all Comma-separated subset of Windows PE architectures to build (x86_64, i386, aarch64, or all).

Target Linkage Details

  • Native Linux (tonic, libtonic.so): Links libc on Linux (link_libc = true) so the optional runtime dlopen("libspeechd.so.2") bridge and POSIX CLI file I/O work cleanly.
  • Windows PE (sapi.dll): Cross-compiled to x86_64-windows-gnu, i386-windows-gnu, and aarch64-windows-gnu completely freestanding (link_libc = false), using std.debug.no_panic in non-test builds and dynamically resolving winmm.dll, user32.dll, and ole32.dll at runtime.

zig-out/ Artifact Layout

Running zig build -Doptimize=ReleaseSafe produces the following output tree:

zig-out/
├── bin/
│   └── tonic                          # Native Linux CLI (`info`, `speak`, `recognize`)
└── lib/
    ├── libtonic.so                    # Native Linux C ABI shared library
    └── tonic/
        ├── x86_64-windows/
        │   └── sapi.dll               # 64-bit x86 Windows PE SAPI 5 DLL
        ├── i386-windows/
        │   └── sapi.dll               # 32-bit x86 Windows PE SAPI 5 DLL
        └── aarch64-windows/
            └── sapi.dll               # 64-bit ARM Windows PE SAPI 5 DLL

Running the Unit Test Suite

Run all host unit tests across every module:

zig build test --summary all

The test suite compiles src/root.zig with transitive test discovery across every submodule:

Module What the Unit Tests Verify
src/root.zig Module exports, tonic_version, tonic_sample_rate, and tonic_synthesize_pcm16 / tonic_free_pcm16 C ABI round-trip.
src/com/abi.zig 16-byte Guid layout, SAPI 5 CLSID/IID uniqueness, ComRefCount atomic add/release lifecycle, and HResult helpers.
src/tts/phonemes.zig English number normalization (0..999), abbreviation expansion, exception lexicon lookup, digraphs, punctuation pauses, and formant target invariants (0 < F1 < F2 < F3).
src/tts/synth.zig Non-silent bounded PCM16 synthesis, rate duration scaling, zero-volume muting, and 44-byte RIFF WAV header validation.
src/tts/ssml.zig <rate>, <volume>, <pitch>, <prosody>, <break>, and <silence> parsing, XML entity decoding, exact silence sample insertion, and pitch scaling effects.
src/asr/mfcc.zig Radix-2 FFT peak bin accuracy, 25 ms / 10 ms framing, finite outputs, CMVN zero-mean / unit-variance normalization on C1..C12, and non-zero Delta MFCC energy.
src/asr/srgs.zig W3C SRGS XML (<grammar>, <one-of>, <ruleref>, <tag> stripping), SAPI 5 XML (<GRAMMAR>, <L>, <P>, <O>, <RULEREF>), cyclic <ruleref> termination, plain phrase lists, UTF-16LE decoding, and binary CFG safety.
src/asr/grammar.zig Command matching across speed/pitch variations with noisy lead/trail padding, and adaptive VAD rejection of pure silence and steady stationary noise.
src/host/bridge.zig Safe libspeechd.so.2 runtime probing and built-in formant synthesizer fallback.
src/sapi/types.zig UTF-8 <-> UTF-16LE round-trips and simple XML stripping.
src/sapi/token.zig ISpObjectTokenCategory::EnumTokens, IEnumSpObjectTokens::GetCount/Item, and TonicEnglish token ID.
src/sapi/notify.zig All 5 ISpNotifySource delivery mechanisms (Win32 event, window message, C callback function, ISpNotifyCallback, and ISpNotifySink).
src/sapi/winmm_audio.zig CoTaskMemAlloc/CoTaskMemFree, UTF-16 task memory allocation, WinEvent auto-reset semantics, WaveOutPlayer playback/purge, and WaveInCapture block simulation.
src/sapi/result.zig ISpPhrase/ISpRecoResult QueryInterface, whole and sub-range GetText, single-block CoTaskMemAlloc GetPhrase layout, and GetResultTimes.
src/sapi/voice.zig ISpVoice rate/volume clamping, default voice token, synchronous and SPF_ASYNC Speak, WaitUntilDone, SPVOICESTATUS, SSML synthesis, SPEI_WORD_BOUNDARY events, Pause/Resume, and SPF_PURGEBEFORESPEAK.
src/sapi/recognizer.zig ISpRecognizer -> ISpRecoContext -> ISpRecoGrammar COM pipeline, AddWordTransition, LoadCmdFromMemory (UTF-8 SRGS, UTF-16LE SAPI XML, binary CFG safety), feedPcm16 -> GetEvents -> ISpRecoResult, EmulateRecognition, and streaming processCaptureBlock VAD flushing.
src/sapi/root.zig DllGetClassObject / IClassFactory instantiation for documented CLSIDs, DllCanUnloadNow tracking, and rejection of unknown CLSIDs.
src/game_fixes.zig Strict executable-basename isolation of per-game compatibility flags.

Clone this wiki locally