Gorilla.Audio.STT.Listener Unit Reference

Overview

The Gorilla.Audio.STT.Listener unit contains the component applications work with: TGorillaSpeechToText, its voice command collection, and the two threads that keep recording and recognition off the main thread.

Class Hierarchy

TFmxObject
  └── TGorillaSpeechToText

TOwnedCollection
  └── TGorillaSTTCommands

TCollectionItem
  └── TGorillaSTTCommand

TThread
  ├── TGorillaSTTCaptureThread
  └── TGorillaSTTWorkerThread

Event Types

TGorillaSTTResultEvent = procedure(ASender : TObject;
  const AResult : TGorillaSTTResult) of object;

TGorillaSTTCommandEvent = procedure(ASender : TObject;
  const ACommand : TGorillaSTTCommand; const AConfidence : Single;
  const AResult : TGorillaSTTResult) of object;

TGorillaSTTErrorEvent = procedure(ASender : TObject;
  const AError : String) of object;

TGorillaSTTSpeechEvent = procedure(ASender : TObject;
  const ALevel : Single) of object;

Enumerations

TGorillaSTTWorkKind

What the recognition worker is supposed to do with a block of samples.

Value Description
wkSegment A complete utterance, recognized in one go
wkChunk Part of a running utterance, pushed into a streaming engine
wkFlush End of a streaming utterance, asks for the final result

TGorillaSTTEventKind

Kind of an event waiting to be raised on the main thread: ekSpeechStart, ekSpeechEnd, ekPartial, ekResult, ekError.

Records

TGorillaSTTWorkItem

One entry of the queue between capture and recognition: Kind, Samples, StartTime, Duration.

TGorillaSTTEventItem

One event a worker produced, waiting for the main thread to raise it: Kind, Data (owned by the queue), Text, Level. Events must never be raised from a worker thread — an application handler will touch the scene — so they are queued here and dispatched by Update().

TGorillaSTTCommand

One voice command with all the phrases that trigger it. For a game the raw text is rarely the goal: a command bundles the synonyms a player might use — "angriff", "attackieren", "greif an" — under a single name the application reacts to.

Member Type Default Description
Name String — The name reported by OnCommand
Phrases TStrings — The phrases that trigger this command, one per line
Enabled Boolean True A disabled command is never matched
Threshold Single 0 Similarity this command needs to fire; 0 uses the component's CommandThreshold
Tag NativeInt — Free for application use
Match(AText) Single — Best similarity of any phrase to a text: 1.0 literal hit, 0.0 nothing in common

TGorillaSTTCommands

Collection of all voice commands a component listens for.

Member Description
Add() Creates an empty command
Add(AName, APhrases) Creates a command together with its phrases
FindByName(AName) Command by name
AllPhrases() Every phrase of every enabled command, for use as a recognizer grammar
Items[AIndex] Typed default indexer

TGorillaSpeechToText

Speech input: microphone in, recognized text and voice commands out. Drop the component next to a TGorillaAudioManager, assign an engine, define your commands and switch Active on.

Public Methods

Method Description
ActiveEngine() The engine actually used; creates one of type EngineName when no component was assigned
ActiveAudioManager() The manager actually used; falls back to the first one next to this component
ActiveCapture() The capture backend, nil while inactive
ActiveVAD() The voice activity detector, nil while inactive
GetDevices() Names of all recording devices available
BeginListen() Starts collecting audio for push-to-talk
EndListen() Ends the push-to-talk recording and starts the recognition
Listen() Listens until one utterance was recognized, then stops — switches Mode to smSingleShot and Active on
Stop() Stops recording and discards everything not recognized yet
RecognizeFile(AFilename) Recognizes a wave file instead of the microphone; runs synchronously and does not touch the capture layer. The caller owns the result
SaveLastSegment(AFilename) Writes everything recorded since the last call into a wave file — the diagnostic for the capture layer
Update() Raises all queued events
DispatchResult(AResult) Reports a result through OnResult and the voice command matching; main thread only, the caller keeps ownership
MatchCommand(AResult, out ACommand, out AConfidence) Looks for a command inside a result
MatchCommand(AText, out ACommand, out AConfidence) Same for a plain text

In smPushToTalk everything between BeginListen() and EndListen() is one utterance and the detector is bypassed. In the other modes BeginListen() only switches recording on — the detector then decides where an utterance starts and ends, so EndListen() has nothing to trigger.

Public Properties

Property Type Description
IsListening Boolean True between BeginListen() and EndListen()
IsSpeaking Boolean True while the detector reports speech
Level Single Current input level, 0..1, ready for a level meter
DroppedSegments Int64 Utterances dropped because the recognizer could not keep up — anything above zero means the engine is too slow for the mode

Published Properties

Property Type Default Description
Engine TGorillaSTTEngine — Engine component doing the recognition
EngineName String — Engine created internally when Engine is empty; empty picks the first registered backend, e.g. Whisper
AudioManager TGorillaAudioManager — Manager whose backend is used for recording; empty uses the first manager of the same form
VADName String — Name of the voice activity detector; empty picks the highest priority one, which is Energy
Active Boolean False Starts and stops recording
Mode TGorillaSTTMode smContinuous How the component decides when to record
Device String — Recording device out of GetDevices(); empty uses the system default
SampleRate Integer 16000 Rate the device is opened with — leave it there unless the device refuses it
Language String — Language of the expected speech, e.g. de; empty asks the engine to detect it
Prompt String — Context hint handed to the engine; improves the spelling of unusual words noticeably
Translate Boolean False Lets the engine translate the result into English
PartialResults Boolean False Intermediate results while speaking; only with a streaming engine
UseGrammar Boolean False Hands the command phrases to the engine as a recognition grammar
Commands TGorillaSTTCommands — The voice commands this component listens for
CommandThreshold Single 0.75 Similarity a phrase needs to fire its command; 1.0 accepts only literal hits
AutoUpdate Boolean True Drives Update() from an internal timer
UpdateInterval Integer 16 Interval of that timer in ms
PollInterval Integer 20 Interval the capture thread polls the device with, in ms
MaxQueueLength Integer 8 Utterances that may wait for the recognizer; everything beyond is dropped and counted in DroppedSegments

UseGrammar raises the hit rate for voice commands dramatically with a grammar capable engine, at the price of no longer understanding anything else. Engines without grammar support get the phrases as a context prompt instead.

Events

Event Type Fired when
OnSpeechStart TGorillaSTTSpeechEvent The detector found the start of speech
OnSpeechEnd TGorillaSTTSpeechEvent An utterance ended
OnPartial TGorillaSTTResultEvent Intermediate result of a streaming engine
OnResult TGorillaSTTResultEvent An utterance was recognized
OnCommand TGorillaSTTCommandEvent A recognized text matched a command; fires after OnResult
OnError TGorillaSTTErrorEvent Recording or recognition failed

The result handed to OnPartial and OnResult is owned by the component and invalid after the handler returned — copy what you need.

Threads

Class Description
TGorillaSTTCaptureThread Reads the microphone, converts and segments its samples. Runs at a raised priority: a capture device that is not drained in time silently overwrites its own ring buffer, and the samples are gone
TGorillaSTTWorkerThread Runs the recognition of queued utterances

Constants

Constant Value Description
GORILLA_STT_COMMAND_THRESHOLD 0.75 Similarity a phrase needs to fire its command by default
GORILLA_STT_QUEUE_LENGTH 8 Utterances that may wait for the recognizer
GORILLA_STT_CHUNK_DURATION 0.25 Length of the sample block handed to a streaming engine, in seconds

See Also