use nanostore for soundmap

+ rename tab samples to sounds
+ listed sounds are now reactive
This commit is contained in:
Felix Roos 2023-03-06 23:11:09 +01:00
parent 6059c69995
commit b08a0b8102
8 changed files with 34 additions and 31 deletions

View file

@ -34,7 +34,8 @@
},
"homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": {
"@strudel.cycles/core": "workspace:*"
"@strudel.cycles/core": "workspace:*",
"nanostores": "^0.7.4"
},
"devDependencies": {
"vite": "^3.2.2"

View file

@ -179,11 +179,6 @@ export async function onTriggerSample(options, bank) {
// no playback
return;
}
if (!s) {
// is this check really needed?
console.warn('no sample specified');
return;
}
//const soundfont = getSoundfontKey(s);
let bufferSource;

View file

@ -2,7 +2,7 @@ import { fromMidi, toMidi } from '@strudel.cycles/core';
import { setSound } from './webaudio.mjs';
import { getOscillator, gainNode, getADSR } from './helpers.mjs';
export function loadSynthSounds() {
export function registerSynthSounds() {
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
setSound(wave, ({ hap, duration, t }) => {
// destructure adsr here, because the default should be different for synths and samples

View file

@ -11,15 +11,14 @@ const { Pattern } = strudel;
import './vowel.mjs';
import workletsUrl from './worklets.mjs?url';
import { getFilter, gainNode } from './helpers.mjs';
import { map } from 'nanostores';
// export const getAudioContext = () => Tone.getContext().rawContext;
export const soundMap = new Map();
export const soundMap = map();
// onTrigger = ({ hap: Hap, t: number, deadline: number, duration: number, cps: number }) => AudioNode
export function setSound(key, onTrigger) {
soundMap.set(key, onTrigger);
soundMap.setKey(key, onTrigger);
}
export const resetLoadedSounds = () => soundMap.clear();
export const resetLoadedSounds = () => soundMap.set({});
let audioContext;
export const getAudioContext = () => {
@ -161,14 +160,17 @@ export const webaudioOutput = async (hap, deadline, hapDuration, cps) => {
if (bank && s) {
s = `${bank}_${s}`;
}
if (soundMap.has(s)) {
const node = await soundMap.get(s)({ hap, t, deadline, duration: hapDuration, cps });
chain.push(node);
} else if (source) {
chain.push(source({ hap, t, deadline, duration: hapDuration, cps }));
// get source AudioNode
let node;
const options = { hap, t, deadline, duration: hapDuration, cps };
if (source) {
node = source(options);
} else if (soundMap.get()[s]) {
node = await soundMap.get()[s](options);
} else {
throw new Error(`sound ${s} not found! Is it loaded?`);
}
chain.push(node);
// gain stage
chain.push(gainNode(gain));