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 },
);
});
}

View file

@ -1,6 +1,6 @@
import { getFontBufferSource } from './fontloader.mjs';
import { getFontBufferSource, registerSoundfonts } from './fontloader.mjs';
import * as soundfontList from './list.mjs';
import { startPresetNote } from 'sfumato';
import { loadSoundfont } from './sfumato.mjs';
export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList };
export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList, registerSoundfonts };

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
import { Pattern, getPlayableNoteValue, toMidi } from '@strudel.cycles/core';
import { getAudioContext } from '@strudel.cycles/webaudio';
import { getAudioContext, registerSound } from '@strudel.cycles/webaudio';
import { loadSoundfont as _loadSoundfont, startPresetNote } from 'sfumato';
Pattern.prototype.soundfont = function (sf, n = 0) {
@ -21,5 +21,29 @@ export function loadSoundfont(url) {
}
const sf = _loadSoundfont(url);
soundfontCache.set(url, sf);
/*sf.then((font) => {
font.presets.forEach((preset) => {
console.log('preset', preset.header.name);
registerSound(
preset.header.name.replaceAll(' ', '_'),
(time, value, onended) => {
const ctx = getAudioContext();
let { note } = value; // freq ?
const p = font.presets.find((p) => p.header.name === preset.header.name);
if (!p) {
throw new Error('preset not found');
}
const deadline = time; // - ctx.currentTime;
const args = [ctx, p, toMidi(note), deadline];
const stop = startPresetNote(...args);
return { node: undefined, stop };
},
{ type: 'soundfont' },
);
});
//console.log('f', f);
});*/
return sf;
}