From 0530c91ecd4abdc713a0c4a796ef6e53a23e38a2 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Sun, 28 Dec 2025 18:11:04 +0100 Subject: [PATCH] Remove setTimestamp for AudioSample, add override options to its clone method --- src/decode.ts | 3 ++- src/media-source.ts | 3 ++- src/sample.ts | 20 ++++++++------------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/decode.ts b/src/decode.ts index d67d8ed..7410626 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -650,7 +650,8 @@ export class AudioDecoderWrapper extends DecoderWrapper { // Round the timestamp to the sample rate const sampleRate = decoderConfig.sampleRate; - sample.setTimestamp(Math.round(preciseTimestamp * sampleRate) / sampleRate); + // @ts-expect-error Readonly + sample.timestamp = Math.round(preciseTimestamp * sampleRate) / sampleRate; onSample(sample); }; diff --git a/src/media-source.ts b/src/media-source.ts index 7b5cae6..6a73abf 100644 --- a/src/media-source.ts +++ b/src/media-source.ts @@ -2003,7 +2003,8 @@ export class MediaStreamAudioTrackSource extends AudioSource { return; } - audioSample.setTimestamp(currentTimestamp + this._pauseOffset); + // @ts-expect-error Readonly + audioSample.timestamp = currentTimestamp + this._pauseOffset; void this._encoder.add(audioSample, true) .catch((error) => { diff --git a/src/sample.ts b/src/sample.ts index d30e101..4a179d7 100644 --- a/src/sample.ts +++ b/src/sample.ts @@ -1217,14 +1217,20 @@ export class AudioSample implements Disposable { } /** Clones this audio sample. */ - clone(): AudioSample { + clone(override?: { + timestamp?: number; + }): AudioSample { if (this._closed) { throw new Error('AudioSample is closed.'); } + const timestamp = override?.timestamp ?? this.timestamp; + if (isAudioData(this._data)) { const sample = new AudioSample(this._data.clone()); - sample.setTimestamp(this.timestamp); // Make sure the timestamp is precise (beyond microsecond accuracy) + + // @ts-expect-error Readonly + sample.timestamp = timestamp; // Make sure the timestamp is precise (beyond microsecond accuracy) return sample; } else { @@ -1342,16 +1348,6 @@ export class AudioSample implements Disposable { return audioBuffer; } - /** Sets the presentation timestamp of this audio sample, in seconds. */ - setTimestamp(newTimestamp: number) { - if (!Number.isFinite(newTimestamp)) { - throw new TypeError('newTimestamp must be a number.'); - } - - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (this.timestamp as number) = newTimestamp; - } - /** Calls `.close()`. */ [Symbol.dispose]() { this.close();