Skip to content

Commit

Permalink
fix: batch network requests in fetchAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
davwheat committed Dec 22, 2023
1 parent 960cec2 commit 7de3255
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/crunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,25 @@ export default class Crunker {
}

/**
* Asynchronously fetches multiple audio files and returns an array of AudioBuffers.
* Asynchronously fetches multiple audio files and returns an array of AudioBuffers,
* splitting into groups of 200 files to avoid resource exhaustion.
*/
async fetchAudio(...filepaths: CrunkerInputTypes[]): Promise<AudioBuffer[]> {
const buffers: AudioBuffer[] = [];
const groups = Math.ceil(filepaths.length / 200);

for (let i = 0; i < groups; i++) {
const group = filepaths.slice(i * 200, (i + 1) * 200);
buffers.push(...(await this._fetchAudio(...group)));
}

return buffers;
}

/**
* Asynchronously fetches multiple audio files and returns an array of AudioBuffers.
*/
private async _fetchAudio(...filepaths: CrunkerInputTypes[]): Promise<AudioBuffer[]> {
return await Promise.all(
filepaths.map(async (filepath) => {
let buffer: ArrayBuffer;
Expand Down

0 comments on commit 7de3255

Please sign in to comment.