register soundfonts as sounds too

This commit is contained in:
Felix Roos 2023-03-09 22:48:40 +01:00
parent 6f5d096e6d
commit ac148b2f32
7 changed files with 421 additions and 379 deletions

View file

@ -1,4 +1,6 @@
import { toMidi } from '@strudel.cycles/core';
import { getAudioContext, registerSound } from '@strudel.cycles/webaudio';
import { instruments } from './list.mjs';
let loadCache = {};
async function loadFont(name) {
@ -8,7 +10,6 @@ async function loadFont(name) {
const load = async () => {
// TODO: make soundfont source configurable
const url = `https://felixroos.github.io/webaudiofontdata/sound/${name}.js`;
console.log('load font', name, url);
const preset = await fetch(url).then((res) => res.text());
let [_, data] = preset.split('={');
return eval('{' + data);
@ -114,3 +115,24 @@ async function getBuffer(zone, audioContext) {
}
}
}
export function registerSoundfonts() {
instruments.forEach((instrument) => {
registerSound(
instrument,
async (time, value, onended) => {
const { note, n } = value;
const ctx = getAudioContext();
const bufferSource = await getFontBufferSource(instrument, note || n, ctx);
bufferSource.start(time);
const stop = (time) => bufferSource.stop(time);
bufferSource.onended = () => {
bufferSource.disconnect();
onended();
};
return { node: bufferSource, stop };
},
{ type: 'soundfont', prebake: true },
);
});
}