cleaning up
This commit is contained in:
parent
3fd5fdf1ee
commit
1f62d85d8b
2 changed files with 22 additions and 45 deletions
|
|
@ -23,13 +23,6 @@ export function registerSound(key, onTrigger, data = {}) {
|
||||||
export function getSound(s) {
|
export function getSound(s) {
|
||||||
return soundMap.get()[s];
|
return soundMap.get()[s];
|
||||||
}
|
}
|
||||||
// sounds with only one active channel will get upmixed to two, stereo sounds should remain unaffected
|
|
||||||
// There might be a better way to do this
|
|
||||||
export const upMixToStereo = (ctx, node) => {
|
|
||||||
const stereo = new StereoPannerNode(ctx);
|
|
||||||
node.connect(stereo);
|
|
||||||
return stereo;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const resetLoadedSounds = () => soundMap.set({});
|
export const resetLoadedSounds = () => soundMap.set({});
|
||||||
|
|
||||||
|
|
@ -45,28 +38,6 @@ export const getAudioContext = () => {
|
||||||
};
|
};
|
||||||
// outputs: Map<int, GainNode>
|
// outputs: Map<int, GainNode>
|
||||||
const outputs = new Map();
|
const outputs = new Map();
|
||||||
let channelMerger;
|
|
||||||
// channels: Array<int>
|
|
||||||
const getDestinations = (channels) => {
|
|
||||||
const ctx = getAudioContext();
|
|
||||||
if (channelMerger == null) {
|
|
||||||
channelMerger = new ChannelMergerNode(ctx, { numberOfInputs: ctx.destination.channelCount });
|
|
||||||
channelMerger.connect(ctx.destination);
|
|
||||||
}
|
|
||||||
channels.forEach((ch) => {
|
|
||||||
if (!outputs.has(ch)) {
|
|
||||||
const gain = new GainNode(ctx, {
|
|
||||||
channelInterpretation: 'discrete',
|
|
||||||
channelCount: 1,
|
|
||||||
channelCountMode: 'explicit',
|
|
||||||
});
|
|
||||||
gain.connect(channelMerger, 0, ch);
|
|
||||||
outputs.set(ch, gain);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return channels.map((ch) => outputs.get(ch));
|
|
||||||
};
|
|
||||||
|
|
||||||
export const panic = () => {
|
export const panic = () => {
|
||||||
outputs.forEach((output) => {
|
outputs.forEach((output) => {
|
||||||
|
|
@ -120,20 +91,26 @@ export async function initAudioOnFirstClick(options) {
|
||||||
|
|
||||||
let delays = {};
|
let delays = {};
|
||||||
const maxfeedback = 0.98;
|
const maxfeedback = 0.98;
|
||||||
//input: audioNode, channels: Array<int>
|
|
||||||
const connectToOutputs = (input, channels = [0, 1]) => {
|
|
||||||
const outputs = getDestinations(channels);
|
|
||||||
const ctx = getAudioContext();
|
|
||||||
|
|
||||||
//This upmix can be avoided if correct channel counts are set throughout the app,
|
let channelMerger;
|
||||||
//could theoretically support surround sound audio files if that work was done
|
// input: AudioNode, channels: ?Array<int>
|
||||||
const stereo = upMixToStereo(ctx, input);
|
export const connectToDestination = (input, channels = [0, 1]) => {
|
||||||
|
const ctx = getAudioContext();
|
||||||
|
if (channelMerger == null) {
|
||||||
|
channelMerger = new ChannelMergerNode(ctx, { numberOfInputs: ctx.destination.channelCount });
|
||||||
|
channelMerger.connect(ctx.destination);
|
||||||
|
}
|
||||||
|
//This upmix can be removed if correct channel counts are set throughout the app,
|
||||||
|
// and then strudel could theoretically support surround sound audio files
|
||||||
|
const stereoMix = new StereoPannerNode(ctx);
|
||||||
|
input.connect(stereoMix);
|
||||||
|
|
||||||
const splitter = new ChannelSplitterNode(ctx, {
|
const splitter = new ChannelSplitterNode(ctx, {
|
||||||
numberOfOutputs: stereo.channelCount,
|
numberOfOutputs: stereoMix.channelCount,
|
||||||
});
|
});
|
||||||
stereo.connect(splitter);
|
stereoMix.connect(splitter);
|
||||||
outputs.forEach((output, i) => {
|
channels.map((ch, i) => {
|
||||||
splitter.connect(output, i % stereo.channelCount, 0);
|
splitter.connect(channelMerger, i % stereoMix.channelCount, ch);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -146,7 +123,7 @@ function getDelay(orbit, delaytime, delayfeedback, t) {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
const dly = ac.createFeedbackDelay(1, delaytime, delayfeedback);
|
const dly = ac.createFeedbackDelay(1, delaytime, delayfeedback);
|
||||||
dly.start?.(t); // for some reason, this throws when audion extension is installed..
|
dly.start?.(t); // for some reason, this throws when audion extension is installed..
|
||||||
connectToOutputs(dly, [0, 1]);
|
connectToDestination(dly, [0, 1]);
|
||||||
delays[orbit] = dly;
|
delays[orbit] = dly;
|
||||||
}
|
}
|
||||||
delays[orbit].delayTime.value !== delaytime && delays[orbit].delayTime.setValueAtTime(delaytime, t);
|
delays[orbit].delayTime.value !== delaytime && delays[orbit].delayTime.setValueAtTime(delaytime, t);
|
||||||
|
|
@ -205,7 +182,7 @@ function getReverb(orbit, duration, fade, lp, dim, ir) {
|
||||||
if (!reverbs[orbit]) {
|
if (!reverbs[orbit]) {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
const reverb = ac.createReverb(duration, fade, lp, dim, ir);
|
const reverb = ac.createReverb(duration, fade, lp, dim, ir);
|
||||||
connectToOutputs(reverb, [0, 1]);
|
connectToDestination(reverb, [0, 1]);
|
||||||
reverbs[orbit] = reverb;
|
reverbs[orbit] = reverb;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
|
|
@ -480,7 +457,7 @@ export const superdough = async (value, deadline, hapDuration) => {
|
||||||
// last gain
|
// last gain
|
||||||
const post = new GainNode(ac, { gain: postgain });
|
const post = new GainNode(ac, { gain: postgain });
|
||||||
chain.push(post);
|
chain.push(post);
|
||||||
connectToOutputs(post, channels);
|
connectToDestination(post, channels);
|
||||||
|
|
||||||
// delay
|
// delay
|
||||||
let delaySend;
|
let delaySend;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import React, { useMemo, useCallback, useLayoutEffect, useRef, useState } from '
|
||||||
import { Reference } from './Reference';
|
import { Reference } from './Reference';
|
||||||
import { themes } from './themes.mjs';
|
import { themes } from './themes.mjs';
|
||||||
import { useSettings, settingsMap, setActiveFooter, defaultSettings } from '../settings.mjs';
|
import { useSettings, settingsMap, setActiveFooter, defaultSettings } from '../settings.mjs';
|
||||||
import { getAudioContext, soundMap, upMixToStereo } from '@strudel.cycles/webaudio';
|
import { getAudioContext, soundMap, connectToDestination } from '@strudel.cycles/webaudio';
|
||||||
import { useStore } from '@nanostores/react';
|
import { useStore } from '@nanostores/react';
|
||||||
import { FilesTab } from './FilesTab';
|
import { FilesTab } from './FilesTab';
|
||||||
|
|
||||||
|
|
@ -271,7 +271,7 @@ function SoundsTab() {
|
||||||
const onended = () => trigRef.current?.node?.disconnect();
|
const onended = () => trigRef.current?.node?.disconnect();
|
||||||
trigRef.current = Promise.resolve(onTrigger(time, params, onended));
|
trigRef.current = Promise.resolve(onTrigger(time, params, onended));
|
||||||
trigRef.current.then((ref) => {
|
trigRef.current.then((ref) => {
|
||||||
upMixToStereo(ctx, ref?.node).connect(ctx.destination);
|
connectToDestination(ref?.node);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue