feat(pcm): increase buffer size for smoother playback

This commit is contained in:
Simon Chan 2023-12-08 15:52:25 +08:00
parent 4d6af115b0
commit 06e6f044e5
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
2 changed files with 14 additions and 16 deletions

View file

@ -21,6 +21,7 @@ abstract class SourceProcessor<T>
#chunkSampleCounts: number[] = []; #chunkSampleCounts: number[] = [];
#totalSampleCount = 0; #totalSampleCount = 0;
#starting = true;
#speedUp = false; #speedUp = false;
#readOffset = 0; #readOffset = 0;
#inputOffset = 0; #inputOffset = 0;
@ -33,8 +34,7 @@ abstract class SourceProcessor<T>
this.#readBuffer = new Float32Array(this.channelCount); this.#readBuffer = new Float32Array(this.channelCount);
this.port.onmessage = (event) => { this.port.onmessage = (event) => {
while (this.#totalSampleCount > 16000) { while (this.#totalSampleCount > 0.35 * 48000) {
console.log("[Audio] Buffer overflow, dropping samples");
this.#chunks.shift(); this.#chunks.shift();
const count = this.#chunkSampleCounts.shift()!; const count = this.#chunkSampleCounts.shift()!;
this.#totalSampleCount -= count; this.#totalSampleCount -= count;
@ -46,8 +46,7 @@ abstract class SourceProcessor<T>
this.#chunkSampleCounts.push(length); this.#chunkSampleCounts.push(length);
this.#totalSampleCount += length; this.#totalSampleCount += length;
if (!this.#speedUp && this.#totalSampleCount > 8000) { if (!this.#speedUp && this.#totalSampleCount > 0.25 * 48000) {
console.log("[Audio] Speeding up");
this.#speedUp = true; this.#speedUp = true;
this.#readOffset = 0; this.#readOffset = 0;
this.#inputOffset = 0; this.#inputOffset = 0;
@ -59,10 +58,17 @@ abstract class SourceProcessor<T>
protected abstract createSource(data: ArrayBuffer[]): [T, number]; protected abstract createSource(data: ArrayBuffer[]): [T, number];
process(_inputs: Float32Array[][], [outputs]: Float32Array[][]) { process(_inputs: Float32Array[][], [outputs]: Float32Array[][]) {
// Stop speeding up when buffer is below 0.05s if (this.#starting) {
if (this.#speedUp && this.#totalSampleCount < 3000) { if (this.#totalSampleCount < 0.1 * 48000) {
console.log("[Audio] Restoring normal speed"); return true;
} else {
this.#starting = false;
}
}
if (this.#speedUp && this.#totalSampleCount < 0.15 * 48000) {
this.#speedUp = false; this.#speedUp = false;
this.#starting = true;
} }
const outputLength = outputs![0]!.length; const outputLength = outputs![0]!.length;
@ -153,14 +159,6 @@ abstract class SourceProcessor<T>
this.#chunks.shift(); this.#chunks.shift();
this.#chunkSampleCounts.shift(); this.#chunkSampleCounts.shift();
} }
if (outputIndex < outputLength) {
console.log(
`[Audio] Buffer underflow, inserting silence: ${
outputLength - outputIndex
} samples`,
);
}
} }
#read(offset: number) { #read(offset: number) {

View file

@ -37,7 +37,7 @@ export class ScrcpyAudioCodec implements ScrcpyOptionValue {
"raw", "raw",
0x00_72_61_77, 0x00_72_61_77,
"audio/raw", "audio/raw",
"1", "",
); );
readonly optionValue: string; readonly optionValue: string;