Better preloading

This commit is contained in:
Nikita 2025-10-22 16:19:58 +03:00
parent daea207aff
commit 5e27358fe8

View file

@ -23,6 +23,7 @@ import {
setMultiChannelOrbits, setMultiChannelOrbits,
resetGlobalEffects, resetGlobalEffects,
getDefaultValue, getDefaultValue,
getSampleBuffer,
} from 'superdough'; } from 'superdough';
import './supradough.mjs'; import './supradough.mjs';
import { workletUrl } from 'supradough'; import { workletUrl } from 'supradough';
@ -62,45 +63,47 @@ export async function renderPatternAudio(pattern, cps, begin, end, sampleRate, d
await Promise.all([ await Promise.all([
loadModules(), loadModules(),
prebake(), prebake(),
haps.map(async (h) => { ]);
let s;
if (h.value.s) { haps.forEach(async (h) => {
if (h.value.bank) { let s;
s = `${h.value.bank}_${h.value.s}`; if (h.value.s) {
} else { if (h.value.bank) {
s = h.value.s; s = `${h.value.bank}_${h.value.s}`;
} } else {
// let bank = getSound(s).data.samples; s = h.value.s;
let sample = getSound(s);
if (sample.data.type == "soundfont") {
sample.data.fonts.forEach(async (font) => {
await getFontBufferSource(font, h.value, audioContext)
})
}
if (sample.data.type == "sample") {
let url;
if (Array.isArray(sample)) {
url = sample.data.samples[i % sample.data.samples.length];
} else if (typeof sample === 'object') {
console.log(sample.data.samples)
url = Object.values(sample.data.samples).flat()[getDefaultValue("i") % Object.values(sample.data.samples).length];
}
await loadBuffer(url, audioContext, s, 0);
} // TODO: waveform
} }
}) // let bank = getSound(s).data.samples;
], let sample = getSound(s);
); if (sample.data.type == "soundfont") {
haps.forEach(async (hap) => { for (let index = 0; index < sample.data.fonts.length; index++) {
const font = array[index];
await getFontBufferSource(font, h.value, audioContext)
}
}
if (sample.data.type == "sample") {
for (let index = 0; index < sample.data.samples.length; index++) {
const sample = array[index];
await getSampleBuffer(h.value, bank, sample)
}
} // TODO: waveform
}
})
for (let index = 0; index < haps.length; index++) {
const hap = haps[index];
if (hap.hasOnset()) { if (hap.hasOnset()) {
hap.ensureObjectValue(); await hap.ensureObjectValue();
console.log("dough")
await superdough(hap.value, hap.whole.begin.valueOf() / cps, hap.duration, cps, hap.whole?.begin.valueOf()); await superdough(hap.value, hap.whole.begin.valueOf() / cps, hap.duration, cps, hap.whole?.begin.valueOf());
} }
}); }
console.log("starting rendering")
return audioContext return audioContext
.startRendering() .startRendering()
.then((renderedBuffer) => { .then((renderedBuffer) => {
console.log('downloading')
const wavBuffer = audioBufferToWav(renderedBuffer); const wavBuffer = audioBufferToWav(renderedBuffer);
const blob = new Blob([wavBuffer], { type: 'audio/wav' }); const blob = new Blob([wavBuffer], { type: 'audio/wav' });
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);