Skip to content

Commit

Permalink
Merge pull request #216 from ctrlaltdavid/fix/audio-choppiness
Browse files Browse the repository at this point in the history
Fix audio output choppiness
  • Loading branch information
ctrlaltdavid authored Oct 5, 2022
2 parents 99a3abf + 1dd9534 commit d71ab90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/domain/audio/InboundAudioStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class InboundAudioStream {
#_selectedCodecName = "";
#_decoder = null;

// WEBRTC TODO: Remove when have logger with "once" function.
#_haveWarnedWriteDroppableSilentFrames = false;


/* eslint-disable */
// @ts-ignore
Expand Down Expand Up @@ -127,12 +124,14 @@ class InboundAudioStream {
#writeDroppableSilentFrames(silentFrames: number): void {
// C++ int writeDroppableSilentFrames(int silentFrames)

// WEBRTC TODO: Address further C++ code.
if (!this.#_haveWarnedWriteDroppableSilentFrames) {
console.warn("InboundAudioStream.#writeDroppableSilentFrames() not implemented. Frames:", silentFrames);
this.#_haveWarnedWriteDroppableSilentFrames = true;
}
// WEBRTC TODO: Address further C++ code. Fade toward silence.

// WEBRTC TODO: Address further C++ code. Only write as many silent frames as the AudioOutputProcessor's jitter buffer
// warrants.

// Write silent audio frames.
const silentBuffer = new Int16Array(silentFrames); // Is initialized to 0s.
this.#_audioOutput.writeData(silentBuffer);
}

#parseAudioData(packetData: DataView): number {
Expand Down
10 changes: 7 additions & 3 deletions src/domain/worklets/AudioOutputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
class AudioOutputProcessor extends AudioWorkletProcessor {

// Buffer blocks of audio data so that they can be played back smoothly.
// FIXME: All these fields should be private (#s) but Firefox is handling transpiled code with them (Sep 2021).
// FIXME: All these fields should be private (#s) but Firefox isn't handling transpiled code with them (Sep 2021).
_audioBuffer: Int16Array[] = [];
readonly MAX_AUDIO_BUFFER_LENGTH = 16; // The maximum number of audio blocks to buffer.
readonly MIN_AUDIO_BUFFER_LENGTH = 4; // The minimum number of audio blocks to have before starting to play them.
// MAX_AUDIO_BUFFER_LENGTH = AudioClient.#RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES
readonly MAX_AUDIO_BUFFER_LENGTH = 100; // The maximum number of audio blocks to buffer
readonly MIN_AUDIO_BUFFER_LENGTH = 50; // The minimum number of audio blocks to have before starting to play them.
_isPlaying = false; // Is playing audio blocks from the buffer.


Expand All @@ -57,6 +58,7 @@ class AudioOutputProcessor extends AudioWorkletProcessor {

// If we've reached the maximum buffer size, skip some of the audio blocks.
if (this._audioBuffer.length > this.MAX_AUDIO_BUFFER_LENGTH) {
// console.log("AudioOutputProcessor: Overflowed", this.MAX_AUDIO_BUFFER_LENGTH);
while (this._audioBuffer.length > this.MIN_AUDIO_BUFFER_LENGTH) {
this._audioBuffer.shift();
}
Expand All @@ -79,6 +81,8 @@ class AudioOutputProcessor extends AudioWorkletProcessor {
* @param {Float32Array[][]} inputList - Input PCM audio samples. <em>Not used.</em>
* @param {Float32Array[][]} outputList - Output PCM audio samples.
* @param {Record<string, Float32Array>} parameters - Processing parameters. <em>Not used.</em>
* @returns {boolean} <code>true</code> to keep the processor node alive, <code>false</code> to let the browser terminate
* the node.
*/
// eslint-disable-next-line
// @ts-ignore
Expand Down

0 comments on commit d71ab90

Please sign in to comment.