@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.
Read the guide: how to use it →
Classes
CamerlengoSpeech class
class CamerlengoSpeech implements SpeechBackendconstructor(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 MicCaptureOne capture: from start until stop (the samples) or cancel (nothing). It ends by itself at maxSeconds, keeping what it has.
level: number | the loudness of the last chunk, 0..1 (root mean square): a meter for the game's UI |
get capturing(): boolean | true while samples are coming in |
onLimit?: () => void | called when the capture ended by itself at the limit |
constructor(private open: OpenMicrophone = browserMicrophone, readonly maxSeconds = 60) | |
async start(): Promise<void> | |
stop(): PcmAudio | Ends the capture: the samples collected (mono). |
cancel(): void | Ends the capture and drops what it had. |
packages/speech/src/capture.ts:60
SpeechError class
class SpeechError extends Errorconstructor(readonly code: SpeechErrorCode, message: string) |
packages/speech/src/clip.ts:26
SpeechService class
class SpeechService implements Servicereadonly 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(): SpeechBackend | the 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 = {}): SpeechLine | Says 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(): boolean | true 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: OpenMicrophoneThe 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): SpeechClipA 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): SpeechClipCaptured 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): Uint8Arraypackages/speech/src/clip.ts:72
installSpeech function
function installSpeech(world: World, opts: SpeechOptions = {}): SpeechServiceRegisters 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): stringThe 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) => stringThe 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): numberThe loudest sample's magnitude.
packages/speech/src/clip.ts:53
toBase64 function
function toBase64(bytes: Uint8Array): stringpackages/speech/src/clip.ts:67
toSpeechPcm function
function toSpeechPcm(pcm: PcmAudio, rate = 16000): Float32ArrayMono, 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 CamerlengoSpeechOptionsclient: CamerlengoClient | |
session: () => string | null | the session that pays; read on every call (sign-in, renewal). Null: refused with signed-out, nothing sent |
key?: string | the key with the ai:tts / ai:stt scopes (default: the client's appKey) |
packages/speech/src/backend.ts:25
LineResult interface
interface LineResultinterrupted: boolean | |
error?: SpeechError |
packages/speech/src/service.ts:51
Listening interface
interface Listeningreadonly level: number | the loudness now, 0..1 |
readonly capturing: boolean | |
stop(): Promise<string> | ends the capture and recognises it |
cancel(): void | ends the capture and sends nothing |
packages/speech/src/service.ts:61
PcmSource interface
interface PcmSourceA 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 RecognizeOptionslanguage?: string | the language spoken, a code (en, ru); without it the recognizer guesses |
timeout?: number | seconds the backend waits for the recognizer (Camerlengo: default 30, at most 600) |
packages/speech/src/backend.ts:13
SpeakOptions interface
interface SpeakOptions extends SynthesizeOptionsinterrupt?: boolean | cut what this speaker is saying and has queued, then say this |
volume?: number | |
spatial?: Spatial | |
cache?: boolean | false: do not take the clip from the cache or keep it there |
packages/speech/src/service.ts:43
SpeechBackend interface
interface SpeechBackendsynthesize(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 SpeechClipbytes: Uint8Array | |
format: SoundFormat | |
duration: number | seconds |
packages/speech/src/clip.ts:6
SpeechLine interface
interface SpeechLinereadonly 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: boolean | true while it sounds |
stop(): void |
packages/speech/src/service.ts:52
SpeechOptions interface
interface SpeechOptionsbackend?: SpeechBackend | another backend altogether (the developer's own server); the options below are then unused |
client?: CamerlengoClient | the Camerlengo client (default: the AccountService's) |
session?: () => string | null | who pays (default: the AccountService's session; a guest is refused with signed-out) |
key?: string | the key with the ai:tts / ai:stt scopes (default: the client's appKey) |
voice?: string | default voice and language of synthesize/speak/listen (default alloy, en) |
language?: string | |
microphone?: OpenMicrophone | the microphone (default: the browser's) and the longest capture, s (default 60) |
maxSeconds?: number | |
silence?: number | a 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?: number | synthesised clips kept (default 64) |
spatial?: Spatial | how 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 SynthesizeOptionsvoice?: string | an 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?: string | the 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