Skip to content

Commit

Permalink
fix save, kind of
Browse files Browse the repository at this point in the history
Save takes a screenshot which currnetly requires the visualizers
to be running. Can fix that later. As is it, you can save.

I don't know enough about .wav format to know why the result
is softer than the original.
  • Loading branch information
greggman committed Oct 15, 2024
1 parent 99d8f04 commit f5ad7cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
19 changes: 7 additions & 12 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions js/wavmaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'});
}
Expand Down

0 comments on commit f5ad7cc

Please sign in to comment.