Merge pull request #183 from tidalcycles/soundfont-file-support

Soundfont file support
This commit is contained in:
Felix Roos 2022-08-14 16:04:14 +02:00 committed by GitHub
commit f2b2f4f226
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 6 deletions

View file

@ -50,6 +50,9 @@ export const mod = (n, m) => ((n % m) + m) % m;
export const getPlayableNoteValue = (hap) => {
let { value: note, context } = hap;
if (typeof note === 'object' && !Array.isArray(note)) {
note = note.note || note.n || note.value;
}
// if value is number => interpret as midi number as long as its not marked as frequency
if (typeof note === 'number' && context.type !== 'frequency') {
note = fromMidi(hap.value);

View file

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

View file

@ -23,7 +23,8 @@
"homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": {
"@strudel.cycles/core": "*",
"@strudel.cycles/webaudio": "^0.1.4"
"@strudel.cycles/webaudio": "^0.1.4",
"sfumato": "^0.1.2"
},
"devDependencies": {
"node-fetch": "^3.2.6"

View file

@ -0,0 +1,24 @@
import { Pattern } from '@strudel.cycles/core';
import { loadSoundfont as _loadSoundfont, startPresetNote } from 'sfumato';
Pattern.prototype.soundfont = function (sf, n = 0) {
return this.onTrigger((t, h, ct) => {
const ctx = getAudioContext();
const note = getPlayableNoteValue(h);
const preset = sf.presets[n % sf.presets.length];
const deadline = ctx.currentTime + t - ct;
const args = [ctx, preset, toMidi(note), deadline];
const stop = startPresetNote(...args);
stop(deadline + h.duration);
});
};
const soundfontCache = new Map();
export function loadSoundfont(url) {
if (soundfontCache.get(url)) {
return soundfontCache.get(url);
}
const sf = _loadSoundfont(url);
soundfontCache.set(url, sf);
return sf;
}