Repository navigation
SAPI 5 Compatibility
Tonic compiles a freestanding multi-architecture Windows PE sapi.dll (x86_64-windows, i386-windows, and aarch64-windows) implementing the core Windows SAPI 5 COM interfaces for text-to-speech, finite-grammar command recognition, event notifications, and voice token enumeration.
Every COM vtable struct in src/com/abi.zig and src/sapi/*.zig is verified at compile time with assertVtableCount and assertVtableSlot to guarantee exact Windows ABI slot offsets on both 32-bit (stdcall/winapi) and 64-bit (win64/aarch64) targets.
| Export | Behavior |
|---|---|
DllMain |
Returns TRUE (1). |
DllGetClassObject |
Dispatches documented SAPI 5 CLSIDs to static IClassFactory singletons (CLASS_E_CLASSNOTAVAILABLE for unknown CLSIDs). |
DllCanUnloadNow |
Returns S_OK when atomic global live COM object count (g_object_count) and class factory lock count (g_lock_count) are both zero, else S_FALSE. |
DllRegisterServer |
Returns S_OK. |
DllUnregisterServer |
Returns S_OK. |
| Class Name | CLSID | Instantiated Object |
|---|---|---|
CLSID_SpVoice |
{96749377-3391-11D2-9EE3-00C04F797396} |
SpVoice (ISpVoice, ISpEventSource, ISpNotifySource, IUnknown) |
CLSID_SpInprocRecognizer |
{41B89B6B-9399-11D2-9623-00C04F8EE628} |
SpRecognizer with is_shared = 0 (ISpRecognizer, ISpProperties, IUnknown) |
CLSID_SpSharedRecognizer |
{3BEE4890-4FE9-4A37-8C1E-5E7E12791C1F} |
SpRecognizer with is_shared = 1 (ISpRecognizer, ISpProperties, IUnknown) |
CLSID_SpObjectTokenCategory |
{A910187F-0C7A-45AC-92CC-59EDAFB77B53} |
SpObjectTokenCategory (ISpObjectTokenCategory, ISpDataKey, IUnknown) |
CLSID_SpObjectToken |
{EF411752-3736-4CB4-9C8C-8EF4CCB58EFE} |
SpObjectToken initialized to the built-in Tonic English voice token |
Note
CLSID_SpStream ({715D9C59-4442-11D2-9605-00C04F8EE628}) is defined in src/com/abi.zig, but DllGetClassObject does not yet instantiate a standalone SpStream COM object. Callers can still pass their own ISequentialStream, IStream, or ISpStream implementation to ISpVoice::SetOutput and ISpVoice::SpeakStream.
| Interface | Inheritance Chain | Vtable Slots | Source File |
|---|---|---|---|
IUnknown |
Base | 3 (0..2) |
src/com/abi.zig |
IClassFactory |
IUnknown |
5 (0..4) |
src/com/abi.zig |
ISpNotifySink |
IUnknown |
4 (0..3) |
src/sapi/notify.zig |
ISpNotifyCallback |
Standalone callback vtable | 1 (0) |
src/sapi/notify.zig |
ISpVoice |
ISpEventSource -> ISpNotifySource -> IUnknown |
37 (0..36) |
src/sapi/voice.zig |
ISpRecognizer |
ISpProperties -> IUnknown |
23 (0..22) |
src/sapi/recognizer.zig |
ISpRecoContext |
ISpEventSource -> ISpNotifySource -> IUnknown |
31 (0..30) |
src/sapi/recognizer.zig |
ISpRecoGrammar |
ISpGrammarBuilder -> IUnknown |
29 (0..28) |
src/sapi/recognizer.zig |
ISpPhrase |
IUnknown |
7 (0..6) |
src/sapi/result.zig |
ISpRecoResult |
ISpPhrase -> IUnknown |
14 (0..13) |
src/sapi/result.zig |
ISpObjectTokenCategory |
ISpDataKey -> IUnknown |
21 (0..20) |
src/sapi/token.zig |
ISpObjectToken |
ISpDataKey -> IUnknown |
25 (0..24) |
src/sapi/token.zig |
IEnumSpObjectTokens |
IUnknown |
9 (0..8) |
src/sapi/token.zig |
-
Speech Synthesis & Routing:
-
Speak(pwcs, dwFlags, pulStreamNumber)decodes UTF-16LE text and handlesSPF_ASYNC(spawns a Win32 worker thread on Windows PE),SPF_PURGEBEFORESPEAK(increments purge generation, resets activewaveOut, waits up to 500 ms for in-flight async work, and clears pending events),SPF_IS_XML/SPF_PARSE_SSML/SPF_PARSE_SAPI(dispatches to the SSML/SAPI XML segment synthesizer), andSPF_IS_NOT_XML(forces plain-text synthesis). - If an output stream was bound via
SetOutput(queryingISequentialStream,IStream, orISpStream), synthesized 22050 Hz 16-bit mono PCM bytes are written directly to that stream. - Otherwise, on Windows PE, audio plays via dynamic
winmm.dll(WaveOutPlayer) unlessTONIC_NO_AUDIO_PLAYBACKis set. On native Linux tests/hosts, plain text probesHostBridge(libspeechd.so.2) with built-in formant fallback.
-
-
Playback Control & Status:
-
SetRate/GetRateclamps to[-10, +10];SetVolume/GetVolumeclamps to[0, 100]. -
PauseandResumemaintain a pause count and forward towaveOutPause/waveOutRestart. -
WaitUntilDone(msTimeout)waits on the voice's manual-reset completion event (SPRS_IS_SPEAKING->SPRS_DONE). -
GetStatuspopulatesSPVOICESTATUS(ulCurrentStream,ulLastStreamQueued,hrLastResult,dwRunningState,ulInputWordPos,ulInputWordLen). -
SpeakStreamreads up to 16 MiB from an inputISequentialStreamand forwards to any bound output stream.
-
-
Event Queue (
MAX_QUEUED_EVENTS = 64):- Queues
SPEI_START_INPUT_STREAMandSPEI_END_INPUT_STREAMby default, and emitsSPEI_WORD_BOUNDARY(with estimated audio byte offset, word position, and word length) whenever requested viaSetInterest.
- Queues
-
Grammar Authoring & Loading:
- Programmatic builder (
ISpGrammarBuilder):ResetGrammar,GetRule,CreateNewState,AddWordTransition(registers phrases directly intoGrammarRecognizer), andCommit. - File & Memory Loading:
LoadCmdFromFile(up to 2 MiB viaCreateFileW/ReadFile) andLoadCmdFromMemory(auto-detects UTF-16LE with/without BOM, UTF-8 with/without BOM, W3C SRGS XML, SAPI 5 XML, and plain phrase lists up to 256 KiB). Compiled binary SAPI CFG buffers are detected safely and returnS_OKwithout crashing. - Rule & Grammar States:
SetRuleState(matches by rule name or applies to all rules whenpszName == null),SetRuleIdState,SetGrammarState/GetGrammarState(SPGS_ENABLED,SPGS_DISABLED,SPGS_EXCLUSIVE), andSetContextState/GetContextState.
- Programmatic builder (
-
Live Capture & Recognition Dispatch:
- Transitioning
SetRecoStatetoSPRST_ACTIVEorSPRST_ACTIVE_ALWAYSon Windows PE startsWaveInCapture(16 kHz mono, 4x100 ms buffers) unlessTONIC_NO_MIC_CAPTUREis set. SettingSPRST_INACTIVEorSPRST_INACTIVE_WITH_PURGEstops capture. - Completed utterances are scored across up to
MAX_RECO_CONTEXTS = 16contexts andMAX_CTX_GRAMMARS = 16enabled grammars per context. Matches withconfidence >= 0.35allocate aSpRecoResult, push anSPEI_RECOGNITIONevent (SPET_LPARAM_IS_OBJECT, queue capacityMAX_CTX_EVENTS = 32), and triggerISpNotifySource. -
EmulateRecognitionextracts text from the suppliedISpPhrase, matches active grammar metadata when available, and dispatches anSPEI_RECOGNITIONevent withconfidence = 1.0.
- Transitioning
-
GetPhrase: PacksSPPHRASE, an array of per-wordSPPHRASEELEMENTentries (up to 128 words, withpszDisplayText,pszLexicalForm, per-word audio offsets, and confidence tier), and all UTF-16LE strings into a singleCoTaskMemAllocblock so callers can release the entire structure with oneCoTaskMemFreecall per Windows SAPI 5 ownership rules. -
GetText: Supports both whole-phrase (SP_GETWHOLEPHRASE) and word sub-range extraction intoCoTaskMemAlloc-allocated UTF-16LE strings. -
GetResultTimes: ComputesSPRECORESULTTIMES.ullLengthin 100-nanosecond units from captured byte count and sample rate. -
GetRecoContext: Returns anAddRef'd pointer to the originatingISpRecoContext(attached cleanly when dequeued viaGetEventsto avoid internal reference cycles). -
Confidence Tiers:
>= 0.72->SP_HIGH_CONFIDENCE(+1),>= 0.42->SP_NORMAL_CONFIDENCE(0),< 0.42->SP_LOW_CONFIDENCE(-1).
Shared by both ISpVoice and ISpRecoContext, supporting all 5 SAPI 5 notification modes:
-
Win32 Event:
SetNotifyWin32Event,WaitForNotifyEvent,GetNotifyEventHandle(realCreateEventWhandle on Windows PE; automatically reset when the event queue drains). -
Window Message:
SetNotifyWindowMessagedispatches via dynamicuser32.dll!PostMessageW. -
C Callback Function:
SetNotifyCallbackFunctioninvokesfn(wParam, lParam) callconv(.winapi). -
Callback Interface:
SetNotifyCallbackInterfaceinvokesISpNotifyCallback::NotifyCallback(wParam, lParam). -
Notify Sink COM Object:
SetNotifySinkqueriesIID_ISpNotifySink(with direct-pointer fallback) and invokesISpNotifySink::Notify()outside internal locks to prevent re-entrancy deadlocks.
SpObjectTokenCategory::EnumTokens returns an IEnumSpObjectTokens enumerator exposing 1 built-in voice token:
| Property | Value |
|---|---|
| Token ID | HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TonicEnglish |
Name (and default value) |
Tonic English |
Vendor |
Tonic |
Language |
409 (0x0409, English) |
Gender |
Neutral |
| Feature / Method | Current Behavior | Notes |
|---|---|---|
Binary SAPI CFG Grammars (SPBINARYGRAMMAR) |
LoadCmdFromMemory / LoadCmdFromFile detect binary blobs and return S_OK with 0 parsed rules |
Compiled binary CFG rule-graph deserialization is not yet implemented; SRGS XML, SAPI 5 XML, phrase lists, and AddWordTransition are supported. |
Open Dictation (LoadDictation) |
LoadDictation, UnloadDictation, SetDictationState return S_OK as no-ops |
Tonic focuses on closed-vocabulary finite command grammars rather than unconstrained dictation. |
ISpRecoResult Audio & Serialization |
GetAudio, SpeakAudio, Serialize, ScaleAudio, ISpPhrase::GetSerializedPhrase, and ISpRecoContext::DeserializeResult return E_NOTIMPL
|
Recognition results currently retain phrase text, rule metadata, confidence, and timing without raw PCM retention. |
| Recognition Alternates |
ISpRecoResult::GetAlternates returns 0 alternates (S_OK) |
Top-1 matching rule is returned. |
ISpRecoGrammar::SaveCmd |
Returns E_NOTIMPL
|
Binary grammar serialization is not yet implemented. |
| Voice Skip, Bookmarks, Phoneme & Viseme Events |
ISpVoice::Skip returns 0 skipped (S_OK); SPEI_TTS_BOOKMARK, SPEI_PHONEME, and SPEI_VISEME are not emitted |
SPEI_START_INPUT_STREAM, SPEI_WORD_BOUNDARY, and SPEI_END_INPUT_STREAM are emitted. |
CLSID_SpStream Factory |
Not registered in DllGetClassObject (CLASS_E_CLASSNOTAVAILABLE) |
External ISequentialStream/IStream pointers passed to SetOutput/SpeakStream work directly. |
Configuration UI (DisplayUI) |
IsUISupported sets FALSE (S_OK); DisplayUI returns E_NOTIMPL
|
Headless / non-GUI compatibility layer. |
Copyright (c) 2026 buntatoes. Tonic is a 100% standalone speech engine built from scratch and licensed under the Apache License 2.0. The Tonic, Pisco, Vodka, and Bitters names, wordmarks, and logos are All Rights Reserved.
Trademarks & Non-Affiliation: Windows and SAPI are trademarks or registered trademarks of Microsoft Corporation. Steam, Steam Play, and Proton are trademarks or registered trademarks of Valve Corporation. Linux, AMD, NVIDIA, Intel, and other third-party trademarks belong to their respective owners; Tonic is an independent open-source project and is not affiliated with, endorsed by, or sponsored by any of them.
Design & Engine
Build & Usage
Support
Links
-
Tonic Repository (
unstable/stable) - Pisco · Pisco Wiki
- Vodka · Vodka Wiki
- Bitters
- Apache-2.0 License