Skip to content

Commit

Permalink
Adding back some doc
Browse files Browse the repository at this point in the history
  • Loading branch information
slkzgm committed Dec 30, 2024
1 parent dbb07a3 commit 89cb1f4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/spaces/plugins/SttTtsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ interface PluginConfig {
}>;
}

/**
* MVP plugin for speech-to-text (OpenAI) + conversation + TTS (ElevenLabs)
* Approach:
* - Collect each speaker's unmuted PCM in a memory buffer (only if above silence threshold)
* - On speaker mute -> flush STT -> GPT -> TTS -> push to Janus
*/
export class SttTtsPlugin implements Plugin {
private space?: Space;
private janus?: JanusClient;
Expand All @@ -39,9 +45,19 @@ export class SttTtsPlugin implements Plugin {
content: string;
}> = [];

// For buffering PCM data from each speaker
/**
* userId => arrayOfChunks (PCM Int16)
*/
private pcmBuffers = new Map<string, Int16Array[]>();

/**
* Track mute states: userId => boolean (true=unmuted)
*/
private speakerUnmuted = new Map<string, boolean>();

/**
* For ignoring near-silence frames (if amplitude < threshold)
*/
private silenceThreshold = 50;

// TTS queue for sequentially speaking
Expand Down Expand Up @@ -101,6 +117,9 @@ export class SttTtsPlugin implements Plugin {
);
}

/**
* Called whenever we receive PCM from a speaker
*/
onAudioData(data: AudioDataWithUser): void {
if (!this.speakerUnmuted.get(data.userId)) return;

Expand All @@ -121,6 +140,9 @@ export class SttTtsPlugin implements Plugin {
arr.push(data.samples);
}

/**
* On speaker mute => flush STT => GPT => TTS => push to Janus
*/
private async handleMute(userId: string): Promise<void> {
this.speakerUnmuted.set(userId, false);
const chunks = this.pcmBuffers.get(userId) || [];
Expand Down Expand Up @@ -399,6 +421,7 @@ export class SttTtsPlugin implements Plugin {
samples: Int16Array,
sampleRate: number,
): Promise<void> {
// TODO: Check if better than 480 fixed
const FRAME_SIZE = Math.floor(sampleRate * 0.01); // 10ms frames => 480 @48kHz

for (
Expand Down

0 comments on commit 89cb1f4

Please sign in to comment.