Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/client/src/VoiceConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class VoiceConversation extends BaseConversation {
try {
await wakeLock?.release();
wakeLock = null;
} catch (_e) {}
} catch (_e) { }
throw error;
}
}
Expand All @@ -114,7 +114,7 @@ export class VoiceConversation extends BaseConversation {
try {
await this.wakeLock?.release();
this.wakeLock = null;
} catch (_e) {}
} catch (_e) { }

await this.input.close();
await this.output.close();
Expand All @@ -125,13 +125,16 @@ export class VoiceConversation extends BaseConversation {
this.fadeOutAudio();
}

protected override handleAudio(event: AgentAudioEvent) {
protected override async handleAudio(event: AgentAudioEvent) {
if (this.lastInterruptTimestamp <= event.audio_event.event_id) {
this.options.onAudio?.(event.audio_event.audio_base_64);

// Only play audio through the output worklet for WebSocket connections
// WebRTC connections handle audio playback directly through LiveKit tracks
if (!(this.connection instanceof WebRTCConnection)) {
if (this.output.context.state === 'suspended') {
await this.output.context.resume();
}
this.addAudioBase64Chunk(event.audio_event.audio_base_64);
}

Expand Down
10 changes: 9 additions & 1 deletion packages/client/src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export class Output {

await context.resume();

// Browser may suspend the context in certain situations e.g. switching tabs, sleep, timeout
// This listener will resume the context when it is suspended
context.addEventListener('statechange', async () => {
if (context && context.state === 'suspended') {
await context.resume();
}
});

// Set initial output device if provided
if (outputDeviceId && audioElement.setSinkId) {
await audioElement.setSinkId(outputDeviceId);
Expand Down Expand Up @@ -80,7 +88,7 @@ export class Output {
public readonly gain: GainNode,
public readonly worklet: AudioWorkletNode,
public readonly audioElement: HTMLAudioElement
) {}
) { }

public async setOutputDevice(deviceId?: string): Promise<void> {
if (!("setSinkId" in HTMLAudioElement.prototype)) {
Expand Down
Loading