Skip to content

CLI and Runtime Usage

buntatoes edited this page Nov 29, 2026 · 1 revision

CLI and Runtime Usage

Tonic provides a native Linux command-line tool (tonic), a native Linux C ABI shared library (libtonic.so), and runtime environment controls that apply to both native Linux execution and Windows PE sapi.dll.


Native Linux CLI (tonic)

Built to zig-out/bin/tonic (and installed into Pisco's files/bin/tonic):

Usage:
  tonic info
  tonic speak "<text>" [--wav <out.wav>] [--rate <-10..10>] [--volume <0..100>] [--ssml]
  tonic recognize <input.wav> --grammar <file.xml> [--phrase "<cmd>" ...]
  tonic --version
  tonic --help

tonic info

Prints Tonic's version, synthesis/ASR configuration, phoneme inventory size, supported grammar formats, SAPI 5 frontend summary, and live Linux host speech-dispatcher (libspeechd.so.2) probe status:

./zig-out/bin/tonic info

Example output when speech-dispatcher is absent or disabled:

Tonic 0.1.0-dev
  License:            Apache-2.0 (from-scratch pure Zig, zero bundled GPLv3 libraries)
  TTS Engine:         Klatt-style 3-formant resonator synthesizer (22050 Hz, 16-bit PCM / f32) + SSML/SAPI XML
  Phoneme Inventory:  42 ARPAbet-style phonemes + English G2P & number normalizer
  ASR Engine:         Radix-2 FFT (512-pt), 26-band Mel, 26-dim CMVN+Delta MFCC, adaptive VAD, multi-rate DTW
  Grammar Formats:    W3C SRGS XML, SAPI 5 XML, and plain phrase lists
  SAPI 5 Frontend:    sapi.dll (ISpVoice, ISpRecognizer, ISpRecoContext, ISpRecoGrammar, ISpRecoResult, ISpNotifySource)
  Active Host Bridge: builtin-formant (pure Zig)
  Host Probe Disabled: no
  speechd Library:    not found (using built-in formant synth)

tonic speak

Synthesizes English plain text or SSML/SAPI 5 XML speech:

Flag Description
"<text>" Positional UTF-8 text or XML markup to synthesize.
--wav <out.wav> Writes a 22050 Hz, 16-bit signed mono RIFF WAV file to <out.wav> using the built-in formant synthesizer.
--rate <-10..10> SAPI speaking rate (clamped to -10..10, default 0).
--volume <0..100> SAPI volume level (clamped to 0..100, default 100).
--ssml Forces SSML / SAPI 5 XML parsing even if the input does not begin with <. (XML input starting with < and containing > is automatically detected.)

Playback Behavior Without --wav:

  • For plain text (non-SSML), tonic speak first attempts to speak via the optional Linux host speech-dispatcher bridge (libspeechd.so.2) unless TONIC_NO_HOST_SPEECH is set.
  • If speech-dispatcher is unavailable, disabled, or SSML markup is active, Tonic runs the built-in formant synthesizer and prints the synthesized sample count and duration (pass --wav <file.wav> to save audio to disk).
# Render plain text to WAV
./zig-out/bin/tonic speak "Systems online. All 42 sensors ready." --wav output.wav

# Render SSML with explicit pause and prosody adjustments
./zig-out/bin/tonic speak '<speak>Shields <break time="200ms"/> <prosody pitch="high" rate="fast">online</prosody></speak>' --wav ssml.wav

# Force built-in synthesis without probing host speech-dispatcher
TONIC_NO_HOST_SPEECH=1 ./zig-out/bin/tonic speak "Hello from Tonic." --rate 2 --volume 90 --wav hello.wav

tonic recognize

Scores an uncompressed 16-bit PCM RIFF WAV recording (any sample rate; multi-channel WAVs are automatically averaged to mono; up to 32 MiB) against a finite command grammar:

Flag Description
<input.wav> or --wav / -w <input.wav> Input 16-bit PCM RIFF WAV file.
--grammar / -g <file> Grammar file (up to 2 MiB): W3C SRGS XML, Microsoft SAPI 5 XML, or a newline-separated phrase list (# comments ignored).
--phrase / -p "<cmd>" Adds an inline command phrase (may be repeated; can be used with or without --grammar).
# Recognize a WAV recording using an SRGS or SAPI 5 XML grammar file
./zig-out/bin/tonic recognize command.wav --grammar commands.xml

# Recognize using inline phrases only
./zig-out/bin/tonic recognize command.wav --phrase "fire missile" --phrase "open cargo bay" --phrase "raise shields"

Exit codes:

  • 0 — Success (or command matched in recognize).
  • 1 — Runtime error (file I/O error, unsupported WAV format, empty grammar, or no match detected in recognize).
  • 2 — Invalid CLI arguments or unknown subcommand.

Native Shared Library (libtonic.so) C ABI

zig-out/lib/libtonic.so exposes four C-linkage entry points (src/root.zig) for native Linux hosts and integrations:

#include <stddef.h>
#include <stdint.h>

const char *tonic_version(void);
uint32_t    tonic_sample_rate(void); /* returns 22050 */

/*
 * Synthesizes UTF-8 text into a newly allocated 16-bit mono PCM buffer at 22050 Hz.
 * Returns 0 on success, -1 on null pointer arguments, -2 on synthesis/allocation failure.
 * Caller must free *out_samples using tonic_free_pcm16(*out_samples, *out_count).
 */
int32_t tonic_synthesize_pcm16(
    const char *text,
    int32_t     rate,        /* -10..+10 */
    uint16_t    volume,      /* 0..100 */
    int16_t   **out_samples,
    size_t     *out_count
);

void tonic_free_pcm16(int16_t *ptr, size_t count);

Minimal C example:

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>

extern const char *tonic_version(void);
extern uint32_t tonic_sample_rate(void);
extern int32_t tonic_synthesize_pcm16(const char *, int32_t, uint16_t, int16_t **, size_t *);
extern void tonic_free_pcm16(int16_t *, size_t);

int main(void) {
    int16_t *samples = NULL;
    size_t count = 0;
    if (tonic_synthesize_pcm16("Tonic ready.", 0, 100, &samples, &count) == 0) {
        printf("Tonic %s synthesized %zu samples @ %u Hz\n",
               tonic_version(), count, tonic_sample_rate());
        tonic_free_pcm16(samples, count);
    }
    return 0;
}

Environment Variables

All boolean flags treat any non-empty value other than "0" (for example "1") as enabled.

Variable Scope Effect
TONIC_NO_HOST_SPEECH Linux (tonic, libtonic.so) Disables the runtime dlopen("libspeechd.so.2") probe and forces Tonic's built-in pure-Zig Klatt formant synthesizer.
TONIC_NO_AUDIO_PLAYBACK Windows PE (sapi.dll) Skips winmm.dll waveOut audio playback in ISpVoice::Speak while still synthesizing PCM and delivering all SAPI 5 events/status updates (useful for headless testing or muting SAPI TTS).
TONIC_NO_MIC_CAPTURE Windows PE (sapi.dll) Prevents ISpRecognizer::SetRecoState(SPRST_ACTIVE) from opening winmm.dll waveIn microphone capture (useful in headless environments or when testing with EmulateRecognition).

Optional Linux speech-dispatcher Bridge

On Linux, src/host/bridge.zig avoids bundling any copyleft speech synthesis libraries by probing the host system dynamically at runtime:

  1. Checks TONIC_NO_HOST_SPEECH. If enabled, immediately selects .builtin_formant.
  2. Calls dlopen("libspeechd.so.2", RTLD_LAZY) and resolves spd_open, spd_say, spd_set_voice_rate, spd_set_volume, and spd_close.
  3. Connects with spd_open("tonic", "sapi", null, SPD_MODE_SINGLE).
  4. Maps SAPI rate (-10..+10) linearly to speech-dispatcher rate (-100..+100) and SAPI volume (0..100) linearly to speech-dispatcher volume (-100..+100).
  5. If libspeechd.so.2 is not installed or the speech-dispatcher daemon is not running, Tonic falls back cleanly to its built-in Klatt formant synthesizer with zero errors.

Clone this wiki locally