feat(voice): implement real-time voice-to-voice streaming engine
Introduce a complete low-level audio processing and streaming pipeline for live voice conversations. - Implement `VoiceAudioEngine` using `AudioWorklet` for low-latency PCM16 capture and playback. - Add `pcm-capture` and `pcm-playback` worklets for high-performance audio processing outside the main thread. - Implement `VoiceStreamService` to manage the voice-to-voice state machine (LISTENING, PROCESSING, SPEAKING, INTERRUPTED). - Add `VoiceApiService` for managing conversation and session lifecycle via REST and WebSocket. - Create `VoiceStreamPanelComponent` to display real-time transcripts, status indicators, and the animated voice orb. - Integrate voice streaming into the `AiChatInputComponent` with support for seamless UI transitions between text and voice modes.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Mic capture: downsamples the context rate to `targetRate` (default 16 kHz),
|
||||
* converts to PCM16 mono and posts fixed-size frames (default 20 ms) to the main thread.
|
||||
* Message: { pcm: ArrayBuffer (Int16), rms: number (0..1) }
|
||||
*/
|
||||
class PcmCaptureProcessor extends AudioWorkletProcessor {
|
||||
constructor(options) {
|
||||
super();
|
||||
const o = (options && options.processorOptions) || {};
|
||||
this.targetRate = o.targetRate || 16000;
|
||||
this.frameSamples = o.frameSamples || 320;
|
||||
this.ratio = sampleRate / this.targetRate; // input samples per output sample
|
||||
this.pos = 0;
|
||||
this.sum = 0;
|
||||
this.count = 0;
|
||||
this.frame = new Float32Array(this.frameSamples);
|
||||
this.filled = 0;
|
||||
}
|
||||
|
||||
process(inputs) {
|
||||
const channel = inputs[0] && inputs[0][0];
|
||||
if (!channel) return true;
|
||||
|
||||
for (let i = 0; i < channel.length; i++) {
|
||||
this.sum += channel[i];
|
||||
this.count++;
|
||||
this.pos += 1;
|
||||
if (this.pos >= this.ratio) {
|
||||
// box-filter average of the input samples that fall into one output sample
|
||||
this.frame[this.filled++] = this.sum / this.count;
|
||||
this.sum = 0;
|
||||
this.count = 0;
|
||||
this.pos -= this.ratio;
|
||||
if (this.filled === this.frameSamples) this.flush();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
flush() {
|
||||
const out = new Int16Array(this.frameSamples);
|
||||
let energy = 0;
|
||||
for (let i = 0; i < this.frameSamples; i++) {
|
||||
const v = Math.max(-1, Math.min(1, this.frame[i]));
|
||||
energy += v * v;
|
||||
out[i] = v < 0 ? v * 0x8000 : v * 0x7fff;
|
||||
}
|
||||
this.filled = 0;
|
||||
this.port.postMessage({ pcm: out.buffer, rms: Math.sqrt(energy / this.frameSamples) }, [out.buffer]);
|
||||
}
|
||||
}
|
||||
|
||||
registerProcessor('pcm-capture', PcmCaptureProcessor);
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Streaming PCM playback with a ring buffer.
|
||||
* Messages in: { type: 'push', pcm: ArrayBuffer (Int16 mono), sampleRate }
|
||||
* { type: 'clear' } barge-in: drop everything immediately
|
||||
* { type: 'flush' } end of turn: play what is left, then report 'idle'
|
||||
* Messages out: { type: 'idle' } buffer ran dry after playing
|
||||
*/
|
||||
class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
this.capacity = Math.floor(sampleRate * 30);
|
||||
this.buf = new Float32Array(this.capacity);
|
||||
this.r = 0;
|
||||
this.w = 0;
|
||||
this.count = 0;
|
||||
this.prebuffer = Math.floor(sampleRate * 0.08); // jitter buffer before (re)starting
|
||||
this.started = false;
|
||||
this.prev = 0;
|
||||
this.pos = 1;
|
||||
this.port.onmessage = (e) => {
|
||||
const m = e.data;
|
||||
if (!m) return;
|
||||
if (m.type === 'push') this.push(new Int16Array(m.pcm), m.sampleRate);
|
||||
else if (m.type === 'clear') this.clear();
|
||||
else if (m.type === 'flush') this.flush();
|
||||
};
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.r = this.w = this.count = 0;
|
||||
this.started = false;
|
||||
this.prev = 0;
|
||||
this.pos = 1;
|
||||
}
|
||||
|
||||
flush() {
|
||||
if (this.count === 0) {
|
||||
this.started = false;
|
||||
this.port.postMessage({ type: 'idle' });
|
||||
} else {
|
||||
this.started = true; // short tail: do not wait for the prebuffer
|
||||
}
|
||||
}
|
||||
|
||||
push(int16, srcRate) {
|
||||
const n = int16.length;
|
||||
if (!n) return;
|
||||
const ratio = srcRate / sampleRate; // source samples per output sample
|
||||
const src = new Float32Array(n + 1);
|
||||
src[0] = this.prev;
|
||||
for (let i = 0; i < n; i++) src[i + 1] = int16[i] / 32768;
|
||||
|
||||
let pos = this.pos;
|
||||
while (pos < n) {
|
||||
const i = Math.floor(pos);
|
||||
const frac = pos - i;
|
||||
this.write(src[i] * (1 - frac) + src[i + 1] * frac);
|
||||
pos += ratio;
|
||||
}
|
||||
this.prev = src[n];
|
||||
this.pos = pos - n;
|
||||
}
|
||||
|
||||
write(v) {
|
||||
if (this.count === this.capacity) {
|
||||
this.r = (this.r + 1) % this.capacity; // overflow: drop the oldest sample
|
||||
this.count--;
|
||||
}
|
||||
this.buf[this.w] = v;
|
||||
this.w = (this.w + 1) % this.capacity;
|
||||
this.count++;
|
||||
}
|
||||
|
||||
process(inputs, outputs) {
|
||||
const out = outputs[0];
|
||||
const ch0 = out[0];
|
||||
|
||||
if (!this.started && this.count >= this.prebuffer) this.started = true;
|
||||
|
||||
if (this.started) {
|
||||
for (let i = 0; i < ch0.length; i++) {
|
||||
if (this.count > 0) {
|
||||
ch0[i] = this.buf[this.r];
|
||||
this.r = (this.r + 1) % this.capacity;
|
||||
this.count--;
|
||||
} else {
|
||||
ch0[i] = 0;
|
||||
}
|
||||
}
|
||||
if (this.count === 0) {
|
||||
this.started = false;
|
||||
this.port.postMessage({ type: 'idle' });
|
||||
}
|
||||
} else {
|
||||
ch0.fill(0);
|
||||
}
|
||||
|
||||
for (let c = 1; c < out.length; c++) out[c].set(ch0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
registerProcessor('pcm-playback', PcmPlaybackProcessor);
|
||||
Reference in New Issue
Block a user