began soundfont implementation

This commit is contained in:
Felix Roos 2022-06-20 20:02:55 +02:00
parent b353db67d4
commit 82d6103907
8 changed files with 2144 additions and 263 deletions

View file

@ -27,6 +27,7 @@ const generic_params = [
*
*/
['s', 's', 'sound'],
['sf', 'sf', 'soundfont'],
/**
* The note or sample number to choose for a synth or sampleset
* Note names currently not working yet, but will hopefully soon. Just stick to numbers for now

View file

@ -0,0 +1,13 @@
// this script converts a soundfont into a json file, it has not been not used yet
import fetch from 'node-fetch';
const name = '0000_JCLive';
const js = await fetch(`https://surikov.github.io/webaudiofontdata/sound/${name}_sf2_file.js`).then((res) =>
res.text(),
);
// console.log(js);
let [_, data] = js.split('_sf2_file=');
data = eval(data);
console.log(JSON.stringify(data));

View file

@ -0,0 +1,108 @@
let loadCache = {};
async function loadFont(name) {
if (loadCache[name]) {
return loadCache[name];
}
const load = async () => {
// TODO: make soundfont source configurable
const url = `https://surikov.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);
};
loadCache[name] = load();
return loadCache[name];
}
let bufferCache = {};
export async function getFontBufferSource(name, pitch, ac) {
const key = `${name}:::${pitch}`;
if (bufferCache[key]) {
return (await bufferCache[key])();
}
// console.log('load buffer', key);
const load = async () => {
const preset = await loadFont(name);
if (!preset) {
throw new Error(`Could not load soundfont ${name}`);
}
const zone = findZone(preset, pitch);
if (!zone) {
throw new Error('no soundfont zone found for preset ', name, 'pitch', pitch);
}
const buffer = await getBuffer(zone, ac);
if (!buffer) {
throw new Error(`no soundfont buffer found for preset ${name}, pitch: ${pitch}`);
}
return () => {
const src = ac.createBufferSource();
src.buffer = buffer;
const baseDetune = zone.originalPitch - 100.0 * zone.coarseTune - zone.fineTune;
const playbackRate = 1.0 * Math.pow(2, (100.0 * pitch - baseDetune) / 1200.0);
// src detune?
src.playbackRate.value = playbackRate;
const loop = zone.loopStart > 1 && zone.loopStart < zone.loopEnd;
if (!loop) {
/* const waveDuration = duration + this.afterTime;
if (waveDuration > zone.buffer.duration / playbackRate) {
waveDuration = zone.buffer.duration / playbackRate;
// TODO: do sth with waveduration
} */
} else {
src.loop = true;
/* src.loopStart = zone.loopStart / zone.sampleRate + (zone.delay ? zone.delay : 0);
src.loopEnd = zone.loopEnd / zone.sampleRate + (zone.delay ? zone.delay : 0); */
}
return src;
};
};
bufferCache[key] = load(); // dont await here to cache promise immediately!
return (await bufferCache[key])();
}
function findZone(preset, pitch) {
return preset.find((zone) => {
return zone.keyRangeLow <= pitch && zone.keyRangeHigh + 1 >= pitch;
});
}
// promisified version of https://github.com/felixroos/webaudiofont/blob/c6f97249b60dcfafc20fca5bb381294a6b2f8f51/npm/dist/WebAudioFontPlayer.js#L740
async function getBuffer(zone, audioContext) {
if (zone.sample) {
console.warn('zone.sample untested!');
const decoded = atob(zone.sample);
zone.buffer = audioContext.createBuffer(1, decoded.length / 2, zone.sampleRate);
const float32Array = zone.buffer.getChannelData(0);
let b1, b2, n;
for (var i = 0; i < decoded.length / 2; i++) {
b1 = decoded.charCodeAt(i * 2);
b2 = decoded.charCodeAt(i * 2 + 1);
if (b1 < 0) {
b1 = 256 + b1;
}
if (b2 < 0) {
b2 = 256 + b2;
}
n = b2 * 256 + b1;
if (n >= 65536 / 2) {
n = n - 65536;
}
float32Array[i] = n / 65536.0;
}
} else {
if (zone.file) {
const datalen = zone.file.length;
const arraybuffer = new ArrayBuffer(datalen);
const view = new Uint8Array(arraybuffer);
const decoded = atob(zone.file);
let b;
for (let i = 0; i < decoded.length; i++) {
b = decoded.charCodeAt(i);
view[i] = b;
}
return new Promise((resolve) => audioContext.decodeAudioData(arraybuffer, resolve));
}
}
}

View file

@ -1,29 +1,6 @@
import { Pattern } from '@strudel.cycles/core';
import { getAudioContext } from '@strudel.cycles/webaudio';
// import * as WebAudioFontPlayer from 'webaudiofont';
import * as WebAudioFont from 'webaudiofont';
console.log('WebAudioFont:!', WebAudioFont);
// https://surikov.github.io/webaudiofont/npm/dist/WebAudioFontPlayer.js
// https://surikov.github.io/webaudiofontdata/sound/0000_JCLive_sf2_file.js
import { getFontBufferSource } from './fontloader.mjs';
import * as soundfontList from './list.mjs';
// https://github.com/surikov/webaudiofont#dynamic-loading
/* function changeInstrument(path, name) {
player.loader.startLoad(audioContext, path, name);
player.loader.waitLoad(function () {
instr = window[name];
});
} */
Pattern.prototype.soundfont = function (soundfont) {
const ac = getAudioContext();
return this.onTrigger((t, hap, ct) => {
// const url = `https://surikov.github.io/webaudiofontdata/sound/${soundfont}_sf2_file.js`;
// changeInstrument('https://surikov.github.io/webaudiofontdata/sound/0290_Aspirin_sf2_file.js','_tone_0290_Aspirin_sf2_file');
console.log('url', url);
// const player = WebAudioFontPlayer();
// console.log('soundfont', url, player);
/* var selectedPreset = _tone_0000_JCLive_sf2_file;
player.loader.decodeAfterLoading(ac, '_tone_0000_JCLive_sf2_file');
player.queueWaveTable(ac, ac.destination, selectedPreset, 0, 55, 3.5); */
});
};
globalThis.getFontBufferSource = getFontBufferSource;
globalThis.soundfontList = soundfontList;
globalThis.soundfontList = soundfontList;

1767
packages/soundfonts/list.mjs Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/tidalcycles/strudel.git"
@ -24,8 +25,10 @@
},
"homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": {
"webaudiofont": "^3.0.1",
"@strudel.cycles/core": "*",
"@strudel.cycles/webaudio": "*"
},
"devDependencies": {
"node-fetch": "^3.2.6"
}
}

View file

@ -39,6 +39,54 @@ const getADSR = (attack, decay, sustain, release, velocity, begin, end) => {
return gainNode;
};
const getOscillator = ({ s, freq, t, duration, release }) => {
// make oscillator
const o = getAudioContext().createOscillator();
o.type = s || 'triangle';
o.frequency.value = Number(freq);
o.start(t);
o.stop(t + duration + release);
return o;
};
const getSoundfontKey = (s) => {
if (!globalThis.soundfontList) {
// soundfont package not loaded
return false;
}
if (globalThis.soundfontList?.instruments?.includes(s)) {
return s;
}
// check if s is one of the soundfonts, which are loaded into globalThis, to avoid coupling both packages
const nameIndex = globalThis.soundfontList?.instrumentNames?.indexOf(s);
// convert number nameIndex (0-128) to 3 digit string (001-128)
const name = nameIndex < 10 ? `00${nameIndex}` : nameIndex < 100 ? `0${nameIndex}` : nameIndex;
if (nameIndex !== -1) {
// TODO: indices of instrumentNames do not seem to match instruments
s = globalThis.soundfontList.instruments.find((instrument) => instrument.startsWith(name));
// console.log('match', nameIndex, s);
}
return globalThis.soundfontList?.instruments?.[nameIndex];
};
const getSampleBufferSource = async (s, n) => {
const ac = getAudioContext();
// is sample from loaded samples(..)
const samples = getLoadedSamples();
if (!samples) {
throw new Error('no samples loaded');
}
const bank = samples?.[s];
if (!bank) {
throw new Error('sample not found:', s, 'try one of ' + Object.keys(samples));
}
const sampleUrl = bank[n % bank.length];
const buffer = await loadBuffer(sampleUrl, ac);
const bufferSource = ac.createBufferSource();
bufferSource.buffer = buffer;
return bufferSource;
};
Pattern.prototype.out = function () {
return this.onTrigger(async (t, hap, ct) => {
const ac = getAudioContext();
@ -48,6 +96,7 @@ Pattern.prototype.out = function () {
let {
freq,
s,
sf,
n = 0,
gain = 1,
cutoff,
@ -67,20 +116,16 @@ Pattern.prototype.out = function () {
} = hap.value;
// the chain will hold all audio nodes that connect to each other
const chain = [];
if (typeof n === 'string') {
n = toMidi(n); // e.g. c3 => 48
}
if (!s || ['sine', 'square', 'triangle', 'sawtooth'].includes(s)) {
// get frequency
if (!freq && typeof n === 'number') {
freq = fromMidi(n); // + 48);
}
if (!freq && typeof n === 'string') {
freq = fromMidi(toMidi(n));
}
// make oscillator
const o = ac.createOscillator();
o.type = s || 'triangle';
o.frequency.value = Number(freq);
o.start(t);
o.stop(t + hap.duration + release);
const o = getOscillator({ t, s, freq, duration: hap.duration, release });
chain.push(o);
// level down oscillators as they are really loud compared to samples i've tested
const g = ac.createGain();
@ -92,42 +137,57 @@ Pattern.prototype.out = function () {
chain.push(adsr);
} else {
// load sample
const samples = getLoadedSamples();
if (!samples) {
console.warn('no samples loaded');
if (speed === 0) {
// no playback
return;
}
const bank = samples?.[s];
if (!bank) {
console.warn('sample not found:', s, 'try one of ' + Object.keys(samples));
if (!s) {
console.warn('no sample specified');
return;
} else {
if (speed === 0) {
// no playback
return;
}
if (!s) {
console.warn('no sample specified');
return;
}
const bank = samples[s];
const sampleUrl = bank[n % bank.length];
let buffer = await loadBuffer(sampleUrl, ac);
if (ac.currentTime > t) {
console.warn('sample still loading:', s, n);
return;
}
const src = ac.createBufferSource();
src.buffer = buffer;
src.playbackRate.value = Math.abs(speed);
// TODO: nudge, unit, cut, loop
}
const soundfont = getSoundfontKey(s);
let bufferSource;
let duration = src.buffer.duration;
const offset = begin * duration;
duration = ((end - begin) * duration) / Math.abs(speed);
src.start(t, offset, duration);
src.stop(t + duration);
chain.push(src);
try {
if (soundfont) {
// is soundfont
bufferSource = await globalThis.getFontBufferSource(soundfont, n, ac);
} else {
// is sample from loaded samples(..)
bufferSource = await getSampleBufferSource(s, n);
}
} catch (err) {
console.warn(err);
return;
}
// asny stuff above took too long?
if (ac.currentTime > t) {
console.warn('sample still loading:', s, n);
return;
}
if (!bufferSource) {
console.warn('no buffer source');
return;
}
// bufferSource.playbackRate.value = Math.abs(speed) * playbackRate;
bufferSource.playbackRate.value = Math.abs(speed) * bufferSource.playbackRate.value;
// TODO: nudge, unit, cut, loop
let duration = soundfont ? hap.duration : bufferSource.buffer.duration;
// let duration = bufferSource.buffer.duration;
const offset = begin * duration;
duration = ((end - begin) * duration) / Math.abs(speed);
bufferSource.start(t, offset, duration);
bufferSource.stop(t + duration);
chain.push(bufferSource);
if (soundfont) {
const env = ac.createGain();
env.gain.value = 1;
const fadeLength = 0.1;
env.gain.value = 0.5;
env.gain.setValueAtTime(0.5, t + duration - fadeLength);
env.gain.linearRampToValueAtTime(0, t + duration);
chain.push(env);
}
}
// filters