Gorilla.Audio.STT Unit Reference

Overview

The Gorilla.Audio.STT unit defines the backend independent core of the speech recognition subsystem: the recognition request, the result with its word timeline, the abstract TGorillaSTTEngine and the registry that makes backends selectable by name.

Class Hierarchy

TPersistent
  └── TGorillaSTTResult

TFmxObject
  └── TGorillaSTTEngine (abstract)
        ├── TGorillaWhisperEngine   (Gorilla.Audio.STT.Whisper)
        └── TGorillaVoskEngine      (Gorilla.Audio.STT.Vosk)

TGorillaSTTEngineRegistry (class with class methods only)

Exception Classes

EGorillaSTTException = class(Exception);

Enumerations

TGorillaSTTMode

How the listener component decides when to record.

Value Description
smPushToTalk Records between BeginListen() and EndListen(), the release triggers the recognition. The most reliable mode in a noisy room
smContinuous Records permanently and lets the voice activity detector cut the stream into utterances
smSingleShot Records until the detector found exactly one utterance, then stops

Records

TGorillaSTTRequest

Everything an engine needs to recognize one utterance.

Field Type Description
Samples TArray<Single> Mono float samples of the utterance, normally 16 kHz
SampleRate Integer Sample rate of Samples
Language String Language code such as de or en; empty means auto detection, which only whisper.cpp supports
Prompt String Context hint, e.g. the proper names of the game world; whisper.cpp uses it as its initial prompt
Translate Boolean whisper.cpp only: translate the result into English
MaxAlternatives Integer Alternative readings to report; 0 and 1 both mean "only the best one"
Grammar phrases TArray<String> Phrases the recognizer should restrict itself to; only honoured by engines whose SupportsGrammar() is true
Method Description
Create(ASamples, ...) Constructor
Duration() Length of the audio in seconds
IsValid() True when the request carries audio to recognize

TGorillaSTTWord

A single recognized word with its position in the audio.

Field Description
Text The word as the recognizer spelled it
StartTime Start inside the utterance, in seconds
EndTime End inside the utterance, in seconds
Confidence How sure the recognizer is, 0..1
Duration() Length of the word

TGorillaSTTAlternative

An alternative reading of the same audio — its text and confidence.

TGorillaSTTResult

What an engine understood.

Properties

Property Type Description
Text String The recognized sentence
Words TGorillaSTTWords Word timings, empty when the backend does not report them
Alternatives TArray<TGorillaSTTAlternative> Alternative readings, empty when none were requested
Language String Language the engine detected or was told to use
Confidence Single Overall confidence, 0..1
IsPartial Boolean True for an intermediate result of a streaming backend; always followed by a final one
EngineName String Backend that produced the result
Duration Single Length of the recognized audio in seconds
StartTime Single Position of the utterance in the microphone stream, in seconds
WordCount Integer Number of words in the timeline
IsEmpty Boolean True when nothing was understood

Methods

Method Description
Clear() Empties the result
AddWord(AWord) / AddWord(AText, ...) Appends a word to the timeline
AddAlternative(AText, AConfidence) Appends an alternative reading
RebuildTextFromWords() Rebuilds Text out of the word timeline, for backends that only report words
RebuildConfidenceFromWords() Averages the word confidences into Confidence
OffsetWordTimes(AOffset) Shifts all word timings by seconds
Assign(ASource) Standard TPersistent assignment

TGorillaSTTEngine

Abstract base class of all speech recognition backends. Recognize() is called from the worker thread of TGorillaSpeechToText; a backend that cannot work in parallel overrides IsThreadSafe() and returns false, in which case the caller serializes all requests.

The streaming entry points Feed() and Flush() work for every backend: the default implementation collects the samples and runs DoRecognize() once Flush() is called. Only a backend that really recognizes incrementally — Vosk does — overrides DoFeed() and DoFlush().

Class Methods

Method Description
GetEngineName() Unique name of the backend (abstract)
IsSupported() False when the backend can never work on the current platform, e.g. because it needs to spawn a process

Capability Queries

Method Description
IsAvailable() True when the backend is completely configured and ready to listen
IsThreadSafe() False when the backend must not be called from several threads
SupportsStreaming() True when the backend reports intermediate results while the user is still speaking
SupportsGrammar() True when the backend can restrict itself to a list of phrases
GetLanguages() Language codes the backend is able to recognize

Recognition

Method Description
Recognize(ARequest) Recognizes a complete utterance; the caller owns the result, nil on failure (LastError holds the reason)
Recognize(ASamples, ...) Convenience overload for a block of 16 kHz mono samples
RecognizeFile(AFilename, ...) Convenience overload reading the audio from a wave file — handy to test a backend without a microphone
Feed(ASamples) Pushes samples into a running recognition, returns a partial result or nil
Flush() Ends a running recognition and returns the final result
ResetStream() Drops everything a running recognition collected
SetGrammar(APhrases) Restricts the recognizer to a phrase list; an empty array recognizes free text again
GetGrammar() The phrase list set by SetGrammar()
Property Description
LastError Reason of the last failed operation

Published Properties

Property Type Default Description
DefaultLanguage String — Language used when a request does not name one
WordTimings Boolean True Asks the backend for word level timings; costs a little time and is only needed for subtitles or lipsync

Protected Methods (for implementers)

Method Description
DoRecognize(ARequest, AResult) Recognizes a complete utterance (abstract)
DoFeed(ASamples, AResult) Pushes samples into a running recognition; the default only buffers
DoFlush(AResult) Ends a running recognition; the default recognizes everything buffered
DoResetStream() Drops the state of a running recognition
DoGrammarChanged() Called after Grammar was changed
FinalizeResult(ARequest, AResult) Fills in everything the backend left empty and normalizes the text
SetError(AMessage) Stores an error message and returns false

TGorillaSTTEngineRegistry

Backends register themselves in the initialization section of their unit, so adding the unit to a project makes the engine selectable by name at design-time and at runtime.

Class Method Description
RegisterEngine(AClass) Registers a backend
UnregisterEngine(AClass) Removes it again
FindEngineClass(AName) Resolves a backend class by engine name
CreateEngine(AName, AOwner) Creates an instance; raises EGorillaSTTException for an unknown name, an empty name picks the first supported backend
EngineNames() Names of all registered backends
DefaultEngineName() Name of the first backend supported on this platform

Constants

Constant Value Description
GORILLA_STT_MAX_UTTERANCE 30.0 Longest utterance a non-streaming engine is handed at once, in seconds. whisper.cpp works on 30 second windows, anything above gets split
GORILLA_STT_SAMPLERATE 16000 Sample rate the recognizers work on (in Gorilla.Audio.STT.Utils)

See Also