Skip to content

Commit

Permalink
improve audio playbacks transition for smooth interruptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoudz committed May 24, 2024
1 parent 69057d9 commit 2d1355f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/core/AiAssistantEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class AiAssistantEngine extends EventEmitter {
startProcessing = async (): Promise<void> => {
Logger.log('F: startProcessing');

// Lower the volume of any currently playing audio
this.audioPlayer.setVolume(0.5);

// Reset the assistant before starting processing
this._resetEngine();

Expand Down Expand Up @@ -252,6 +255,8 @@ class AiAssistantEngine extends EventEmitter {
}

// ----[ Step 4: Play AI Audio Reply ]----
// Stop any currently playing audio
this.audioPlayer.stopCurrentSound();
// Handle audio response if available as a Stream
if (response.data.shouldStreamAudioReply) {
this._handleAudioStreamResponse(response.data.outputTextReply);
Expand Down
21 changes: 18 additions & 3 deletions src/core/AudioPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/core/AudioPlayer.ts

import { Howl } from 'howler';
import { Howl, Howler } from 'howler';
import config from './config';
import Logger from './Logger';

Expand All @@ -11,6 +11,7 @@ interface SoundCallback {
class AudioPlayer {
private readonly startSound: Howl;
private readonly endSound: Howl;
private currentSound: Howl | null = null;

constructor() {
// Specify volume at 50% for start and end tones (0.5 = 50%)
Expand Down Expand Up @@ -82,7 +83,7 @@ class AudioPlayer {
): void => {
Logger.log('F: playAiReplyFromUrl');
// Play AI reply at 100% volume (1.0 = 100%)
this.playSound(audioFileUrl, callback, 1.0);
this.currentSound = this.playSound(audioFileUrl, callback, 1.0);
};

playStartTone = (): void => {
Expand All @@ -93,6 +94,19 @@ class AudioPlayer {
this.endSound.play();
};

setVolume = (volume: number): void => {
if (this.currentSound) {
this.currentSound.volume(volume);
}
};

stopCurrentSound = (): void => {
if (this.currentSound) {
this.currentSound.stop();
this.currentSound = null;
}
};

private initializeSound = (soundFileUrl: string, volume = 1.0): Howl => {
return new Howl({
src: [soundFileUrl],
Expand All @@ -108,7 +122,7 @@ class AudioPlayer {
soundFileUrl: string,
callback?: SoundCallback,
volume?: number,
): void => {
): Howl => {
Logger.log('F: playSound');
const sound = new Howl({
src: [soundFileUrl],
Expand All @@ -123,6 +137,7 @@ class AudioPlayer {
});

sound.play();
return sound;
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/AudioRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class AudioRecorder {
this.handleStop = this.handleStop.bind(this);
}

public async test_startRecording(): Promise<Blob> {
async test_startRecording(): Promise<Blob> {
return new Promise((resolve, reject) => {
reject(new Error('TEST Error starting recording...'));
});
}

public async startRecording(): Promise<Blob> {
async startRecording(): Promise<Blob> {
Logger.log('F: startRecording');
try {
if (
Expand Down
4 changes: 2 additions & 2 deletions src/core/SpeechRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class SpeechRecognizer {
this.isInitialized = true;
}

public async test_startListening(): Promise<string> {
async test_startListening(): Promise<string> {
return new Promise((resolve, reject) => {
reject(new Error('TEST Error starting listening...'));
});
}

public async startListening(): Promise<string> {
async startListening(): Promise<string> {
Logger.log('F: startListening');

if (!this.isInitialized) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class User {
this.generatedUserId = this._generateEndUserId();
}

public getEndUserDetails(): EndUserDetails {
getEndUserDetails(): EndUserDetails {
return {
endUserAgent: navigator.userAgent,
generatedEndUserId: this.generatedUserId,
Expand Down

0 comments on commit 2d1355f

Please sign in to comment.