Skip to content

Audio Player Service

This service is responsible for playing all sounds on the frontend. This includes:

  • LLM audio response (voice chat)
  • Text to speech

service AudioPlayerService

To facilitate audio streaming from LLM voice responses, it implements an audio queue system. Audio blobs are played in the sequence they are added in. This implementation was due to how audio files of the LLM response was received on the frontend.

Assumption

The current implementation generally assumes that there will only be one source of audio, voice LLM and TTS included.

Attributes

private attr audioElement

HTMLMediaElementWithCaptureStream source of audio being played.

private attr queue

Blob[] contains audio files to be played.

attr $stream

BehaviorSubject<MediaStream|null> keeping track of the current audio stream. This will be consumed by the waveform visualiser.

attr $playing

BehaviorSubject<boolean> to keep track of the current playing state.

Methods

private meth Play Next in Queue

private playNextInQueue(): void
Description
This method will play the next audio in the queue.

meth Get Audio Stream

getAudioStream(): BehaviorSubject<MediaStream|null>
Description
Public method to retrieve the BehaviorSubject to track the current audio stream source.
Returns
BehaviorSubject<MediaStream|null>

meth Play

play(...blob: Blob[]): void
Description
Method to add an audio file to the queue to be played.
Parameters
...blob (...Blob[]): Destructed array of Blobs to be played. Will be added to a queue.

meth Force Play

forcePlayAndReplace(blob: Blob): void
Description
This method is to clear the current queue of audio files, and play the provided Blob audio.
Parameters
blob (Blob): Audio file to be played

meth Stop and Clear

stopAndClear(): void
Description
This method clears the current queue, stops the audio from being played and updates local states.

meth Play Start Voice Audio

playStartVoiceAudio(): void
Description
This method plays a pre-defined audio file (startvoice.mp3) from the app's assets, used to signal the start of voice interaction.

meth Play Stop Voice Audio

playStopVoiceAudio(): void
Description
This method plays a pre-defined audio file (stopvoice.mp3) from the app's assets, used to signal the end of voice interaction.
Updated 23 Oct 2024
Contributor Ong Tsien Jin