Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: batch network requests in fetchAudio #102

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[] = [];
davwheat marked this conversation as resolved.
Show resolved Hide resolved
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
Loading