diff --git a/editor/index.js b/editor/index.js index 86dbcaf..45be8aa 100644 --- a/editor/index.js +++ b/editor/index.js @@ -527,22 +527,17 @@ async function showSaveDialog() { const numSamplesNeeded = sampleRate * numSeconds | 0; const numChannels = 2; const wavMaker = new WavMaker(sampleRate, numChannels); - const context = ByteBeatNode.createContext(); - const stack = ByteBeatNode.createStack(); + const context = await g_byteBeat.createContext(); + const stack = await g_byteBeat.createStack(); for (let i = 0; i < numSamplesNeeded; i += sampleRate) { const start = i; const end = Math.min(i + sampleRate, numSamplesNeeded); - const output = [ - new Float32Array(end - start), - new Float32Array(end - start), - ]; - for (let j = start; j < end; ++j) { - for (let ch = 0; ch < numChannels; ++ch) { - const s = g_byteBeat.getSampleForTime(j, context, stack, ch); - output[ch][j - i] = s; - } + const dataP = []; + for (let channel = 0; channel < numChannels; ++channel) { + dataP.push(g_byteBeat.getSamplesForTimeRange(start, end, end - start, context, stack, channel)); } - wavMaker.addData(output); + const data = await Promise.all(dataP); + wavMaker.addData(data); await wait(); } const blob = wavMaker.getWavBlob(); diff --git a/js/wavmaker.js b/js/wavmaker.js index fcfa80f..30a4458 100644 --- a/js/wavmaker.js +++ b/js/wavmaker.js @@ -24,8 +24,8 @@ class WavMaker { } getWavBlob() { const {numSamples, numChannels, sampleRate, blobs} = this; - var output = new Uint8Array(44); - var view = new DataView(output.buffer); + const output = new Uint8Array(44); + const view = new DataView(output.buffer); writeString(view, 0, 'RIFF'); // RIFF identifier view.setUint32(4, 36 + this.numSamples * 2, true); // RIFF chunk length @@ -39,7 +39,7 @@ class WavMaker { view.setUint16(32, numChannels * 2, true); // block align (channel count * bytes per sample) view.setUint16(34, 16, true); // bits per sample writeString(view, 36, 'data'); // data chunk identifier - view.setUint32(40, numSamples * numChannels* 2, true); // data chunk length + view.setUint32(40, numSamples * numChannels * 2, true); // data chunk length return new Blob([new Blob([output]), ...blobs], {type: 'audio/wav'}); }