mediabunny 1.7.5 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8164,6 +8164,40 @@ ${cue.notes ?? ""}`;
8164
8164
  }
8165
8165
  this.timestamp = newTimestamp;
8166
8166
  }
8167
+ /** @internal */
8168
+ static *_fromAudioBuffer(audioBuffer, timestamp) {
8169
+ if (!(audioBuffer instanceof AudioBuffer)) {
8170
+ throw new TypeError("audioBuffer must be an AudioBuffer.");
8171
+ }
8172
+ const MAX_FLOAT_COUNT = 48e3 * 5;
8173
+ const numberOfChannels = audioBuffer.numberOfChannels;
8174
+ const sampleRate = audioBuffer.sampleRate;
8175
+ const totalFrames = audioBuffer.length;
8176
+ const maxFramesPerChunk = Math.floor(MAX_FLOAT_COUNT / numberOfChannels);
8177
+ let currentRelativeFrame = 0;
8178
+ let remainingFrames = totalFrames;
8179
+ while (remainingFrames > 0) {
8180
+ const framesToCopy = Math.min(maxFramesPerChunk, remainingFrames);
8181
+ const chunkData = new Float32Array(numberOfChannels * framesToCopy);
8182
+ for (let channel = 0; channel < numberOfChannels; channel++) {
8183
+ audioBuffer.copyFromChannel(
8184
+ chunkData.subarray(channel * framesToCopy, (channel + 1) * framesToCopy),
8185
+ channel,
8186
+ currentRelativeFrame
8187
+ );
8188
+ }
8189
+ yield new _AudioSample({
8190
+ format: "f32-planar",
8191
+ sampleRate,
8192
+ numberOfFrames: framesToCopy,
8193
+ numberOfChannels,
8194
+ timestamp: timestamp + currentRelativeFrame / sampleRate,
8195
+ data: chunkData
8196
+ });
8197
+ currentRelativeFrame += framesToCopy;
8198
+ remainingFrames -= framesToCopy;
8199
+ }
8200
+ }
8167
8201
  /**
8168
8202
  * Creates AudioSamples from an AudioBuffer, starting at the given timestamp in seconds. Typically creates exactly
8169
8203
  * one sample, but may create multiple if the AudioBuffer is exceedingly large.
@@ -8172,7 +8206,7 @@ ${cue.notes ?? ""}`;
8172
8206
  if (!(audioBuffer instanceof AudioBuffer)) {
8173
8207
  throw new TypeError("audioBuffer must be an AudioBuffer.");
8174
8208
  }
8175
- const MAX_FLOAT_COUNT = 64 * 1024 * 1024;
8209
+ const MAX_FLOAT_COUNT = 48e3 * 5;
8176
8210
  const numberOfChannels = audioBuffer.numberOfChannels;
8177
8211
  const sampleRate = audioBuffer.sampleRate;
8178
8212
  const totalFrames = audioBuffer.length;
@@ -8185,7 +8219,7 @@ ${cue.notes ?? ""}`;
8185
8219
  const chunkData = new Float32Array(numberOfChannels * framesToCopy);
8186
8220
  for (let channel = 0; channel < numberOfChannels; channel++) {
8187
8221
  audioBuffer.copyFromChannel(
8188
- chunkData.subarray(channel * framesToCopy, channel * framesToCopy + framesToCopy),
8222
+ chunkData.subarray(channel * framesToCopy, (channel + 1) * framesToCopy),
8189
8223
  channel,
8190
8224
  currentRelativeFrame
8191
8225
  );
@@ -11527,14 +11561,15 @@ ${cue.notes ?? ""}`;
11527
11561
  * @returns A Promise that resolves once the output is ready to receive more samples. You should await this Promise
11528
11562
  * to respect writer and encoder backpressure.
11529
11563
  */
11530
- add(audioBuffer) {
11564
+ async add(audioBuffer) {
11531
11565
  if (!(audioBuffer instanceof AudioBuffer)) {
11532
11566
  throw new TypeError("audioBuffer must be an AudioBuffer.");
11533
11567
  }
11534
- const audioSamples = AudioSample.fromAudioBuffer(audioBuffer, this._accumulatedTime);
11535
- const promises = audioSamples.map((sample) => this._encoder.add(sample, true));
11568
+ const iterator = AudioSample._fromAudioBuffer(audioBuffer, this._accumulatedTime);
11536
11569
  this._accumulatedTime += audioBuffer.duration;
11537
- return Promise.all(promises);
11570
+ for (const audioSample of iterator) {
11571
+ await this._encoder.add(audioSample, true);
11572
+ }
11538
11573
  }
11539
11574
  /** @internal */
11540
11575
  _flushAndClose(forceClose) {
@@ -11619,9 +11654,9 @@ ${cue.notes ?? ""}`;
11619
11654
  let audioReceived = false;
11620
11655
  let totalDuration = 0;
11621
11656
  this._scriptProcessorNode.onaudioprocess = (event) => {
11622
- const audioSamples = AudioSample.fromAudioBuffer(event.inputBuffer, totalDuration);
11657
+ const iterator = AudioSample._fromAudioBuffer(event.inputBuffer, totalDuration);
11623
11658
  totalDuration += event.inputBuffer.duration;
11624
- for (const audioSample of audioSamples) {
11659
+ for (const audioSample of iterator) {
11625
11660
  if (!audioReceived) {
11626
11661
  audioReceived = true;
11627
11662
  const muxer = this._connectedTrack.output._muxer;
@@ -12231,7 +12266,10 @@ ${cue.notes ?? ""}`;
12231
12266
  }
12232
12267
  } else if (rangeResponse.status === 200) {
12233
12268
  this._fullData = await rangeResponse.arrayBuffer();
12234
- return this._fullData.byteLength;
12269
+ if (this._fullData.byteLength !== 1) {
12270
+ return this._fullData.byteLength;
12271
+ } else {
12272
+ }
12235
12273
  }
12236
12274
  const { response } = await this._makeRequest();
12237
12275
  return response.byteLength;