mirror of
https://github.com/eliasstepanik/strudel.git
synced 2026-01-11 05:38:35 +00:00
max poly
This commit is contained in:
parent
e55b7c6ce3
commit
8962e5a4f0
@ -14,6 +14,10 @@ import { map } from 'nanostores';
|
|||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
import { loadBuffer } from './sampler.mjs';
|
import { loadBuffer } from './sampler.mjs';
|
||||||
|
|
||||||
|
let maxPolyphony = 128;
|
||||||
|
export function setMaxPolyphony(polyphony) {
|
||||||
|
maxPolyphony = polyphony;
|
||||||
|
}
|
||||||
export const soundMap = map();
|
export const soundMap = map();
|
||||||
|
|
||||||
export function registerSound(key, onTrigger, data = {}) {
|
export function registerSound(key, onTrigger, data = {}) {
|
||||||
@ -162,7 +166,8 @@ function loadWorklets() {
|
|||||||
|
|
||||||
// this function should be called on first user interaction (to avoid console warning)
|
// this function should be called on first user interaction (to avoid console warning)
|
||||||
export async function initAudio(options = {}) {
|
export async function initAudio(options = {}) {
|
||||||
const { disableWorklets = false } = options;
|
const { disableWorklets = false, polyphony = maxPolyphony } = options;
|
||||||
|
setMaxPolyphony(polyphony);
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -374,8 +379,7 @@ export function resetGlobalEffects() {
|
|||||||
analysersData = {};
|
analysersData = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
let allAudioNodeChains = [];
|
let allAudioNodeChains = new Map();
|
||||||
let maxPolyphony = 12;
|
|
||||||
|
|
||||||
export const superdough = async (value, t, hapDuration) => {
|
export const superdough = async (value, t, hapDuration) => {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
@ -475,30 +479,30 @@ export const superdough = async (value, t, hapDuration) => {
|
|||||||
} = value;
|
} = value;
|
||||||
|
|
||||||
gain = nanFallback(gain, 1);
|
gain = nanFallback(gain, 1);
|
||||||
console.info(allAudioNodeChains.length)
|
|
||||||
|
|
||||||
for (let i = 0; i <= allAudioNodeChains.length - maxPolyphony; i++) {
|
const chainID = Math.round(Math.random() * 10000);
|
||||||
const chain = allAudioNodeChains.shift()
|
|
||||||
chain.forEach(node => node?.disconnect())
|
|
||||||
}
|
|
||||||
// if (allAudioNodeChains.length > maxPolyphony) {
|
|
||||||
|
|
||||||
|
// oldest audio nodes will be removed if maximum polyphony is exceeded
|
||||||
// allAudioNodeChains.slice(0, allAudioNodeChains.length - maxPolyphony).flat().forEach((node) => {
|
for (let i = 0; i <= allAudioNodeChains.size - maxPolyphony; i++) {
|
||||||
// node.disconnect();
|
const ch = allAudioNodeChains.entries().next();
|
||||||
// });
|
const key = ch.value[0];
|
||||||
// console.info(allAudioNodes.length)
|
const chain = ch.value[1];
|
||||||
// }
|
if (key == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
chain?.forEach((node) => node?.disconnect());
|
||||||
|
allAudioNodeChains.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
//music programs/audio gear usually increments inputs/outputs from 1, so imitate that behavior
|
//music programs/audio gear usually increments inputs/outputs from 1, so imitate that behavior
|
||||||
channels = (Array.isArray(channels) ? channels : [channels]).map((ch) => ch - 1);
|
channels = (Array.isArray(channels) ? channels : [channels]).map((ch) => ch - 1);
|
||||||
|
|
||||||
gain *= velocity; // velocity currently only multiplies with gain. it might do other things in the future
|
gain *= velocity; // velocity currently only multiplies with gain. it might do other things in the future
|
||||||
let audioNodes = [];
|
|
||||||
// audio nodes that will be disconnected when the source has ended
|
|
||||||
|
|
||||||
const onended = () => {
|
const onended = () => {
|
||||||
audioNodes.forEach((n) => n?.disconnect());
|
const chain = allAudioNodeChains.get(chainID);
|
||||||
|
chain?.forEach((n) => n?.disconnect());
|
||||||
|
allAudioNodeChains.delete(chainID);
|
||||||
};
|
};
|
||||||
if (bank && s) {
|
if (bank && s) {
|
||||||
s = `${bank}_${s}`;
|
s = `${bank}_${s}`;
|
||||||
@ -672,11 +676,7 @@ export const superdough = async (value, t, hapDuration) => {
|
|||||||
|
|
||||||
// connect chain elements together
|
// connect chain elements together
|
||||||
chain.slice(1).reduce((last, current) => last.connect(current), chain[0]);
|
chain.slice(1).reduce((last, current) => last.connect(current), chain[0]);
|
||||||
|
allAudioNodeChains.set(chainID, [...chain, delaySend, reverbSend, analyserSend]);
|
||||||
// audioNodes = all the node that should be disconnected in onended callback
|
|
||||||
// this is crucial for performance
|
|
||||||
audioNodes = chain.concat([delaySend, reverbSend, analyserSend]);
|
|
||||||
allAudioNodeChains.push(audioNodes)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const superdoughTrigger = (t, hap, ct, cps) => {
|
export const superdoughTrigger = (t, hap, ct, cps) => {
|
||||||
|
|||||||
@ -36,11 +36,11 @@ import './Repl.css';
|
|||||||
import { setInterval, clearInterval } from 'worker-timers';
|
import { setInterval, clearInterval } from 'worker-timers';
|
||||||
import { getMetadata } from '../metadata_parser';
|
import { getMetadata } from '../metadata_parser';
|
||||||
|
|
||||||
const { latestCode } = settingsMap.get();
|
const { latestCode, maxPolyphony } = settingsMap.get();
|
||||||
let modulesLoading, presets, drawContext, clearCanvas, audioReady;
|
let modulesLoading, presets, drawContext, clearCanvas, audioReady;
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
audioReady = initAudioOnFirstClick();
|
audioReady = initAudioOnFirstClick({ maxPolyphony });
|
||||||
modulesLoading = loadModules();
|
modulesLoading = loadModules();
|
||||||
presets = prebake();
|
presets = prebake();
|
||||||
drawContext = getDrawContext();
|
drawContext = getDrawContext();
|
||||||
|
|||||||
@ -40,6 +40,7 @@ export const defaultSettings = {
|
|||||||
audioEngineTarget: audioEngineTargets.webaudio,
|
audioEngineTarget: audioEngineTargets.webaudio,
|
||||||
isButtonRowHidden: false,
|
isButtonRowHidden: false,
|
||||||
isCSSAnimationDisabled: false,
|
isCSSAnimationDisabled: false,
|
||||||
|
maxPolyphony: 128,
|
||||||
};
|
};
|
||||||
|
|
||||||
let search = null;
|
let search = null;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user