GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

gizgame/voice

Voice speech articulation (PCM formant lip-sync) and microphone recording / hands-free VAD.

gizlang
import * as Voice from "gizgame/voice";

Types

Microphone

gizlang
class Microphone { mode: i64 = 0; told: bool = false; cuts: Vec<bool> = []; recorded: bytes = b""; loud: f64 = 0; since: f64 = -1; began: f64 = -1; spoke: f64 = -1; floor: f64 = 0.02; listen(on: bool, free: bool = false): void { const mode = on ? (free ? 2: 1): 0; if (mode === this.mode) { return; } if (this.mode> 0) { this.cuts.push(this.mode === 1); } this.mode = mode; this.since = -1; this.began = -1; this.spoke = -1; } voiced(): bool { return this.mode === 1 || this.began> = 0; } step(loud: f64, now: f64): void { this.loud = this.mode> 0 ? loud: 0; if (this.mode !== 2) { return; } if (this.since <0) { this.since = now; } if (this.loud> Math.max(0.08, this.floor * 2.5 + 0.03)) { if (this.began <0) { this.began = now; } this.spoke = now; } else { this.floor + = (this.loud - this.floor) * 0.05; } const voice = this.began> = 0; const quiet = now - (voice ? this.spoke: this.since); if (!(voice ? quiet> 700 || now - this.began> 12000: quiet> 1500)) { return; } this.cuts.push(voice && this.spoke - this.began> = 250); this.since = now; this.began = -1; this.spoke = -1; } takeRecording(): bytes { const out = this.recorded; this.recorded = b""; return out; } drainCues(out: mut Vec<HostCue>): void { for (const keep of this.cuts) { out.push({ kind: "cut", keep }); } this.cuts = []; if ((this.mode> 0) !== this.told) { this.told = this.mode> 0; out.push({ kind: "listen", on: this.told }); } } }

What the program hears (Game.listen). MODE: 0 off, 1 one recording until it goes off, 2 hands-free. CUTS are the recordings to end at the next cues (kept or dropped). Hands-free, in the host's milliseconds: SINCE the recording began, BEGAN and SPOKE a voice's first and last loud moment in it (-1: none yet), FLOOR the room's own level.

  • mode: i64
  • told: bool
  • cuts: Vec<bool>
  • recorded: bytes
  • loud: f64
  • since: f64
  • began: f64
  • spoke: f64
  • floor: f64

Functions

articulation

gizlang
function articulation(samples: bytes, rate: i64, channels: i64): Map<string, f64>

Estimates speech articulation from a short window of the playing PCM, using its first two formants. Low F1 closes the jaw. Low F2 rounds the lips and high F2 spreads them. Silence returns to the resting face.

Effects: fail, alloc.

Methods

Microphone.listen

gizlang
listen(on: bool, free: bool = false): void

Listens to the microphone while ON; FREE cuts each utterance automatically when the voice pauses.

Effects: fail, read, write, alloc.

Microphone.voiced

gizlang
voiced(): bool

Whether a voice sounds now: always while a held recording runs, hands-free from a voice's first loud moment.

Effects: fail, read.

Microphone.step

gizlang
step(loud: f64, now: f64): void

Updates loudness and, in hands-free mode, ends the recording when a voice finishes or stays silent.

Effects: fail, read, write, alloc.

Microphone.takeRecording

gizlang
takeRecording(): bytes

Returns the last recording once and clears it.

Effects: fail, read, write.

Microphone.drainCues

gizlang
drainCues(out: mut Vec<HostCue>): void

Appends pending cut and listen cues to OUT.

Effects: fail, read, write, alloc.