Fix MP3 encoder emitting incorrect timestamps, fixed overaggressive audio sample gap filling (fixes #233)

This commit is contained in:
Vanilagy
2025-12-05 12:41:36 +01:00
parent 3dd7a9cb58
commit 5b703a1c78
3 changed files with 31 additions and 15 deletions
+8 -1
View File
@@ -22,7 +22,7 @@ class Mp3Encoder extends CustomAudioEncoder {
private buffer = new Uint8Array(2 ** 16);
private currentBufferOffset = 0;
private currentTimestamp = 0;
private currentTimestamp: number | null = null;
private chunkMetadata: EncodedAudioChunkMetadata = {};
static override supports(codec: AudioCodec, config: AudioDecoderConfig): boolean {
@@ -79,6 +79,11 @@ class Mp3Encoder extends CustomAudioEncoder {
}
async encode(audioSample: AudioSample) {
if (this.currentTimestamp === null) {
// The first sample's timestamp determines where we start
this.currentTimestamp = audioSample.timestamp;
}
const sizePerChannel = audioSample.allocationSize({
format: 's16-planar',
planeIndex: 0,
@@ -123,6 +128,8 @@ class Mp3Encoder extends CustomAudioEncoder {
* these chunks and extract the MP3 frames only when they're complete.
*/
private digestOutput(bytes: Uint8Array) {
assert(this.currentTimestamp !== null);
const requiredBufferSize = this.currentBufferOffset + bytes.length;
if (requiredBufferSize > this.buffer.length) {
// Grow the buffer to the required size