mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-15 23:58:33 +00:00
working
This commit is contained in:
parent
ac2e450e38
commit
52628920d9
@ -31,8 +31,6 @@ let audioContext;
|
||||
export const getAudioContext = () => {
|
||||
if (!audioContext) {
|
||||
audioContext = new AudioContext();
|
||||
const maxChannelCount = audioContext.destination.maxChannelCount;
|
||||
audioContext.destination.channelCount = maxChannelCount;
|
||||
}
|
||||
return audioContext;
|
||||
};
|
||||
@ -85,14 +83,22 @@ const maxfeedback = 0.98;
|
||||
|
||||
let channelMerger, destinationGain;
|
||||
|
||||
export function initializeAudioOutput() {
|
||||
const audioContext = getAudioContext();
|
||||
const maxChannelCount = audioContext.destination.maxChannelCount;
|
||||
console.log(maxChannelCount);
|
||||
audioContext.destination.channelCount = maxChannelCount;
|
||||
channelMerger = new ChannelMergerNode(audioContext, { numberOfInputs: audioContext.destination.channelCount });
|
||||
destinationGain = new GainNode(audioContext);
|
||||
channelMerger.connect(destinationGain);
|
||||
destinationGain.connect(audioContext.destination);
|
||||
}
|
||||
|
||||
// input: AudioNode, channels: ?Array<int>
|
||||
export const connectToDestination = (input, channels = [0, 1]) => {
|
||||
const ctx = getAudioContext();
|
||||
if (channelMerger == null) {
|
||||
channelMerger = new ChannelMergerNode(ctx, { numberOfInputs: ctx.destination.channelCount });
|
||||
destinationGain = new GainNode(ctx);
|
||||
channelMerger.connect(destinationGain);
|
||||
destinationGain.connect(ctx.destination);
|
||||
initializeAudioOutput();
|
||||
}
|
||||
//This upmix can be removed if correct channel counts are set throughout the app,
|
||||
// and then strudel could theoretically support surround sound audio files
|
||||
@ -114,6 +120,7 @@ export const panic = () => {
|
||||
}
|
||||
destinationGain.gain.linearRampToValueAtTime(0, getAudioContext().currentTime + 0.01);
|
||||
destinationGain = null;
|
||||
channelMerger == null;
|
||||
};
|
||||
|
||||
function getDelay(orbit, delaytime, delayfeedback, t) {
|
||||
|
||||
70
website/src/repl/panel/AudioDeviceSelector.jsx
Normal file
70
website/src/repl/panel/AudioDeviceSelector.jsx
Normal file
@ -0,0 +1,70 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { getAudioContext, initializeAudioOutput } from '@strudel.cycles/webaudio';
|
||||
import { SelectInput } from './SelectInput';
|
||||
|
||||
async function setAudioDevice(id) {
|
||||
const audioCtx = getAudioContext();
|
||||
await audioCtx.setSinkId(id);
|
||||
await initializeAudioOutput();
|
||||
}
|
||||
export function AudioDeviceSelector({ audioDeviceName, onChange }) {
|
||||
const [options, setOptions] = useState({});
|
||||
const [optionsInitialized, setOptionsInitialized] = useState(false);
|
||||
|
||||
async function initializeOptions() {
|
||||
await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
let devices = await navigator.mediaDevices.enumerateDevices();
|
||||
devices = devices.filter((device) => device.kind === 'audiooutput' && device.deviceId !== 'default');
|
||||
const optionsArray = [];
|
||||
devices.forEach((device) => {
|
||||
optionsArray.push([device.deviceId, device.label]);
|
||||
});
|
||||
const options = Object.fromEntries(optionsArray);
|
||||
setOptions(options);
|
||||
setOptionsInitialized(true);
|
||||
return options;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!audioDeviceName.length || optionsInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const options = await initializeOptions();
|
||||
|
||||
const deviceID = Object.keys(options).find((id) => options[id] === audioDeviceName);
|
||||
|
||||
if (deviceID == null) {
|
||||
onChange('');
|
||||
return;
|
||||
}
|
||||
|
||||
await setAudioDevice(deviceID);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const onClick = () => {
|
||||
if (optionsInitialized) {
|
||||
return;
|
||||
}
|
||||
(async () => {
|
||||
await initializeOptions();
|
||||
})();
|
||||
};
|
||||
const onDeviceChange = (deviceID) => {
|
||||
(async () => {
|
||||
const deviceName = options[deviceID];
|
||||
onChange(deviceName);
|
||||
await setAudioDevice(deviceID);
|
||||
})();
|
||||
};
|
||||
return (
|
||||
<SelectInput
|
||||
options={options}
|
||||
onClick={onClick}
|
||||
value={Object.keys(options).find((id) => options[id] === audioDeviceName)}
|
||||
onChange={onDeviceChange}
|
||||
/>
|
||||
);
|
||||
}
|
||||
17
website/src/repl/panel/SelectInput.jsx
Normal file
17
website/src/repl/panel/SelectInput.jsx
Normal file
@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
|
||||
export function SelectInput({ value, options, onChange }) {
|
||||
return (
|
||||
<select
|
||||
className="p-2 bg-background rounded-md text-foreground"
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
>
|
||||
{Object.entries(options).map(([k, label]) => (
|
||||
<option key={k} className="bg-background" value={k}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
);
|
||||
}
|
||||
@ -2,6 +2,7 @@ import React from 'react';
|
||||
import { defaultSettings, settingsMap, useSettings } from '../../settings.mjs';
|
||||
import { themes } from '../themes.mjs';
|
||||
import { ButtonGroup } from './Forms.jsx';
|
||||
import { AudioDeviceSelector } from './AudioDeviceSelector.jsx';
|
||||
|
||||
function Checkbox({ label, value, onChange }) {
|
||||
return (
|
||||
@ -85,6 +86,7 @@ export function SettingsTab() {
|
||||
fontSize,
|
||||
fontFamily,
|
||||
panelPosition,
|
||||
audioDeviceName,
|
||||
} = useSettings();
|
||||
|
||||
return (
|
||||
@ -107,6 +109,12 @@ export function SettingsTab() {
|
||||
</button>
|
||||
</div>
|
||||
</FormItem> */}
|
||||
<FormItem label="Audio Device">
|
||||
<AudioDeviceSelector
|
||||
audioDeviceName={audioDeviceName}
|
||||
onChange={(audioDeviceName) => settingsMap.setKey('audioDeviceName', audioDeviceName)}
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="Theme">
|
||||
<SelectInput options={themeOptions} value={theme} onChange={(theme) => settingsMap.setKey('theme', theme)} />
|
||||
</FormItem>
|
||||
|
||||
@ -20,6 +20,7 @@ export const defaultSettings = {
|
||||
panelPosition: 'bottom',
|
||||
userPatterns: '{}',
|
||||
activePattern: '',
|
||||
audioDeviceName: '',
|
||||
};
|
||||
|
||||
export const settingsMap = persistentMap('strudel-settings', defaultSettings);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user