FERRETGame engine over Babylon.js
Ferret

@ferret/speech

Text to speech and speech to text on the Camerlengo API: lines spoken at an entity on the voice bus, a cache of synthesised lines, microphone capture and recognition, by the signed-in player's session; events and speech.* commands.

27 exports · source in packages/speech/src

Classes

CamerlengoSpeech class

class CamerlengoSpeech implements SpeechBackend
constructor(private opts: CamerlengoSpeechOptions)
async synthesize(text: string, o: { voice: string; language: string }): Promise<SpeechClip>
async recognize(clip: SpeechClip, o: RecognizeOptions): Promise<string>

packages/speech/src/backend.ts:46

MicCapture class

class MicCapture

One capture: from start until stop (the samples) or cancel (nothing). It ends by itself at maxSeconds, keeping what it has.

level: numberthe loudness of the last chunk, 0..1 (root mean square): a meter for the game's UI
get capturing(): booleantrue while samples are coming in
onLimit?: () => voidcalled when the capture ended by itself at the limit
constructor(private open: OpenMicrophone = browserMicrophone, readonly maxSeconds = 60)
async start(): Promise<void>
stop(): PcmAudioEnds the capture: the samples collected (mono).
cancel(): voidEnds the capture and drops what it had.

packages/speech/src/capture.ts:60

SpeechError class

class SpeechError extends Error
constructor(readonly code: SpeechErrorCode, message: string)

packages/speech/src/clip.ts:26

SpeechService class

class SpeechService implements Service
readonly name: "speech"
readonly sent: { synthesize: number; recognize: number; }the clips that went to the backend and came back from it (a meter for tests and the status command)
constructor(readonly opts: SpeechOptions = {})
init(world: World)
fixedUpdate()Called before the components' fixed update, at the fixed rate.
dispose()
backend(): SpeechBackendthe backend the calls go to
synthesize(text: string, o: SynthesizeOptions & { cache?: boolean } = {}): Promise<SpeechClip>The line as a clip; nothing is played.
get cached(): number
speak(entity: Entity | null, text: string, o: SpeakOptions = {}): SpeechLineSays a line: on the voice bus at the entity (null: not positioned). Synthesis starts at once; the line waits for the speaker's earlier lines.
hush(entity: Entity | null)Cuts every line of a speaker (null: the unpositioned ones).
lines(): Record<string, { text: string; playing: boolean }[]>lines sounding and waiting, by speaker ('' for unpositioned)
async listen(o: RecognizeOptions = {}): Promise<Listening>Opens the microphone and captures until stop() (recognise) or cancel() (nothing sent). One capture at a time.
get listening(): booleantrue while the microphone captures
async recognize(input: SpeechClip | PcmAudio | Uint8Array, o: RecognizeOptions = {}): Promise<string>The text of a clip (a capture, a file, a synthesised line), or of samples.

packages/speech/src/service.ts:77

Functions

browserMicrophone function

function browserMicrophone: OpenMicrophone

The browser's microphone (echo cancellation and noise suppression on). Throws SpeechError no-microphone or mic-denied.

packages/speech/src/capture.ts:21

clipFromBytes function

function clipFromBytes(bytes: Uint8Array): SpeechClip

A clip from the bytes of a WAV or MP3 file (its length read from the header, nothing decoded).

packages/speech/src/clip.ts:31

clipFromPcm function

function clipFromPcm(pcm: PcmAudio, format: SoundFormat = 'mp3', rate = 16000): SpeechClip

Captured samples as a speech clip: mono at 16 kHz, MP3 (default) or WAV.

packages/speech/src/clip.ts:60

fromBase64 function

function fromBase64(b64: string): Uint8Array

packages/speech/src/clip.ts:72

installSpeech function

function installSpeech(world: World, opts: SpeechOptions = {}): SpeechService

Registers the service; options say who pays and with what (default: the signed-in player's session).

packages/speech/src/service.ts:280

languageCode function

function languageCode(l: string): string

The language as the STT command takes it (a code): en-US -> en; a name is looked up.

packages/speech/src/backend.ts:40

languageName function

function languageName: (l: string) => string

The language as the TTS command takes it (a name); a name given stays as it is.

packages/speech/src/backend.ts:38

peak function

function peak(samples: Float32Array): number

The loudest sample's magnitude.

packages/speech/src/clip.ts:53

toBase64 function

function toBase64(bytes: Uint8Array): string

packages/speech/src/clip.ts:67

toSpeechPcm function

function toSpeechPcm(pcm: PcmAudio, rate = 16000): Float32Array

Mono, at rate (linear interpolation; a box average first when going down, so the highs do not fold back).

packages/speech/src/clip.ts:37

Interfaces

CamerlengoSpeechOptions interface

interface CamerlengoSpeechOptions
client: CamerlengoClient
session: () => string | nullthe session that pays; read on every call (sign-in, renewal). Null: refused with signed-out, nothing sent
key?: stringthe key with the ai:tts / ai:stt scopes (default: the client's appKey)

packages/speech/src/backend.ts:25

LineResult interface

interface LineResult
interrupted: boolean
error?: SpeechError

packages/speech/src/service.ts:51

Listening interface

interface Listening
readonly level: numberthe loudness now, 0..1
readonly capturing: boolean
stop(): Promise<string>ends the capture and recognises it
cancel(): voidends the capture and sends nothing

packages/speech/src/service.ts:61

PcmSource interface

interface PcmSource

A running source of mono samples. onData is set by the capture; close releases the device.

readonly rate: number
onData: ((chunk: Float32Array) => void) | null
close(): void

packages/speech/src/capture.ts:8

RecognizeOptions interface

interface RecognizeOptions
language?: stringthe language spoken, a code (en, ru); without it the recognizer guesses
timeout?: numberseconds the backend waits for the recognizer (Camerlengo: default 30, at most 600)

packages/speech/src/backend.ts:13

SpeakOptions interface

interface SpeakOptions extends SynthesizeOptions
interrupt?: booleancut what this speaker is saying and has queued, then say this
volume?: number
spatial?: Spatial
cache?: booleanfalse: do not take the clip from the cache or keep it there

packages/speech/src/service.ts:43

SpeechBackend interface

interface SpeechBackend
synthesize(text: string, o: Required<Pick<SynthesizeOptions, 'voice' | 'language'>>): Promise<SpeechClip>
recognize(clip: SpeechClip, o: RecognizeOptions): Promise<string>

packages/speech/src/backend.ts:20

SpeechClip interface

interface SpeechClip
bytes: Uint8Array
format: SoundFormat
duration: numberseconds

packages/speech/src/clip.ts:6

SpeechLine interface

interface SpeechLine
readonly text: string
readonly speaker: string | null
readonly done: Promise<LineResult>resolves when the line is over: played out, interrupted, or failed (never rejects)
readonly playing: booleantrue while it sounds
stop(): void

packages/speech/src/service.ts:52

SpeechOptions interface

interface SpeechOptions
backend?: SpeechBackendanother backend altogether (the developer's own server); the options below are then unused
client?: CamerlengoClientthe Camerlengo client (default: the AccountService's)
session?: () => string | nullwho pays (default: the AccountService's session; a guest is refused with signed-out)
key?: stringthe key with the ai:tts / ai:stt scopes (default: the client's appKey)
voice?: stringdefault voice and language of synthesize/speak/listen (default alloy, en)
language?: string
microphone?: OpenMicrophonethe microphone (default: the browser's) and the longest capture, s (default 60)
maxSeconds?: number
silence?: numbera capture whose loudest sample is below this is silence: not sent, empty (default 0.01)
upload?: 'mp3' | 'wav'what a capture is uploaded as (default mp3: smaller, and recognition is paid by size)
cacheSize?: numbersynthesised clips kept (default 64)
spatial?: Spatialhow a positioned line fades with distance (default min 2 m, max 40 m) and its volume
volume?: number

packages/speech/src/service.ts:17

SynthesizeOptions interface

interface SynthesizeOptions
voice?: stringan OpenAI voice name (alloy, echo, fable, onyx, nova, shimmer: a male or female voice of the language) or an exact Edge voice name (ru-RU-SvetlanaNeural)
language?: stringthe text's language: a code (en, ru) or a name (English). Always sent: without it the server spends a language-detection call on every line

packages/speech/src/backend.ts:7

Types

OpenMicrophone type

type OpenMicrophone = () => Promise<PcmSource>

packages/speech/src/capture.ts:13

SpeechErrorCode type

type SpeechErrorCode = | 'signed-out' // no session and no other credentials: nothing was sent | 'no-funds' // the wallet that pays is too low (402) | 'forbidden' // the key may not do this (997) | 'backend' // any other refusal of the backend | 'network' // the backend could not be reached | 'no-microphone' // no microphone, or no way to capture in this host | 'mic-denied' // the player refused the microphone | 'busy' // a capture is already running | 'empty' // silence, or nothing was recognised | 'no-audio' // speak() without an AudioService | 'audio-locked'

packages/speech/src/clip.ts:13