Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: make soundfont base url configurable #1040

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/soundfonts/fontloader.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { persistentMap } from '@nanostores/persistent';
import { noteToMidi, freqToMidi, getSoundIndex } from '@strudel/core';
import {
getAudioContext,
Expand All @@ -9,14 +10,28 @@ import {
} from '@strudel/webaudio';
import gm from './gm.mjs';

export const defaultFontloaderConfig = {
soundfontUrl: 'https://felixroos.github.io/webaudiofontdata/sound'
}

export const fontloaderConfigMap = persistentMap('strudel-config-fontloader', defaultFontloaderConfig);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if a persistentMap is best. wouldn't a simple global variable be enough? it would make sure the default fallback is used in the next session, so there is no hidden state stored in the browser. The fact that an alternative url is used would be visible in the code at all times

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, I'll change that.


export function setSoundfontUrl(obj) {
fontloaderConfigMap.setKey('soundfontUrl', JSON.stringify(obj));
}

export function getSoundfontUrl() {
return JSON.parse(fontloaderConfigMap.get().soundfontUrl);
}

let loadCache = {};
async function loadFont(name) {
if (loadCache[name]) {
return loadCache[name];
}
const load = async () => {
// TODO: make soundfont source configurable
const url = `https://felixroos.github.io/webaudiofontdata/sound/${name}.js`;
const url = `${getSoundfontUrl()}/${name}.js`;
const preset = await fetch(url).then((res) => res.text());
let [_, data] = preset.split('={');
return eval('{' + data);
Expand Down
4 changes: 2 additions & 2 deletions packages/soundfonts/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getFontBufferSource, registerSoundfonts } from './fontloader.mjs';
import { getFontBufferSource, registerSoundfonts, setSoundfontUrl } from './fontloader.mjs';
import * as soundfontList from './list.mjs';
import { startPresetNote } from 'sfumato';
import { loadSoundfont } from './sfumato.mjs';

export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList, registerSoundfonts };
export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList, registerSoundfonts, setSoundfontUrl };
Loading