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

22
package-lock.json generated
View File

@ -10093,6 +10093,14 @@
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
"dev": true
},
"node_modules/sfumato": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/sfumato/-/sfumato-0.1.2.tgz",
"integrity": "sha512-j2s5BLUS5VUNtaK1l+v+yal3XjjV7JXCQIwE5Xs4yiQ3HJ+2Fc/dd3IkkrVHn0AJO2epShSWVoP3GnE0TvPdMg==",
"dependencies": {
"soundfont2": "^0.4.0"
}
},
"node_modules/shallow-clone": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.1.tgz",
@ -12251,7 +12259,8 @@
"license": "AGPL-3.0-or-later",
"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"
@ -14150,7 +14159,8 @@
"requires": {
"@strudel.cycles/core": "*",
"@strudel.cycles/webaudio": "^0.1.4",
"node-fetch": "^3.2.6"
"node-fetch": "^3.2.6",
"sfumato": "^0.1.2"
},
"dependencies": {
"node-fetch": {
@ -20352,6 +20362,14 @@
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
"dev": true
},
"sfumato": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/sfumato/-/sfumato-0.1.2.tgz",
"integrity": "sha512-j2s5BLUS5VUNtaK1l+v+yal3XjjV7JXCQIwE5Xs4yiQ3HJ+2Fc/dd3IkkrVHn0AJO2epShSWVoP3GnE0TvPdMg==",
"requires": {
"soundfont2": "^0.4.0"
}
},
"shallow-clone": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.1.tgz",

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