working
This commit is contained in:
parent
ac2e450e38
commit
52628920d9
5 changed files with 109 additions and 6 deletions
|
|
@ -31,8 +31,6 @@ let audioContext;
|
||||||
export const getAudioContext = () => {
|
export const getAudioContext = () => {
|
||||||
if (!audioContext) {
|
if (!audioContext) {
|
||||||
audioContext = new AudioContext();
|
audioContext = new AudioContext();
|
||||||
const maxChannelCount = audioContext.destination.maxChannelCount;
|
|
||||||
audioContext.destination.channelCount = maxChannelCount;
|
|
||||||
}
|
}
|
||||||
return audioContext;
|
return audioContext;
|
||||||
};
|
};
|
||||||
|
|
@ -85,14 +83,22 @@ const maxfeedback = 0.98;
|
||||||
|
|
||||||
let channelMerger, destinationGain;
|
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>
|
// input: AudioNode, channels: ?Array<int>
|
||||||
export const connectToDestination = (input, channels = [0, 1]) => {
|
export const connectToDestination = (input, channels = [0, 1]) => {
|
||||||
const ctx = getAudioContext();
|
const ctx = getAudioContext();
|
||||||
if (channelMerger == null) {
|
if (channelMerger == null) {
|
||||||
channelMerger = new ChannelMergerNode(ctx, { numberOfInputs: ctx.destination.channelCount });
|
initializeAudioOutput();
|
||||||
destinationGain = new GainNode(ctx);
|
|
||||||
channelMerger.connect(destinationGain);
|
|
||||||
destinationGain.connect(ctx.destination);
|
|
||||||
}
|
}
|
||||||
//This upmix can be removed if correct channel counts are set throughout the app,
|
//This upmix can be removed if correct channel counts are set throughout the app,
|
||||||
// and then strudel could theoretically support surround sound audio files
|
// 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.gain.linearRampToValueAtTime(0, getAudioContext().currentTime + 0.01);
|
||||||
destinationGain = null;
|
destinationGain = null;
|
||||||
|
channelMerger == null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getDelay(orbit, delaytime, delayfeedback, t) {
|
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 { defaultSettings, settingsMap, useSettings } from '../../settings.mjs';
|
||||||
import { themes } from '../themes.mjs';
|
import { themes } from '../themes.mjs';
|
||||||
import { ButtonGroup } from './Forms.jsx';
|
import { ButtonGroup } from './Forms.jsx';
|
||||||
|
import { AudioDeviceSelector } from './AudioDeviceSelector.jsx';
|
||||||
|
|
||||||
function Checkbox({ label, value, onChange }) {
|
function Checkbox({ label, value, onChange }) {
|
||||||
return (
|
return (
|
||||||
|
|
@ -85,6 +86,7 @@ export function SettingsTab() {
|
||||||
fontSize,
|
fontSize,
|
||||||
fontFamily,
|
fontFamily,
|
||||||
panelPosition,
|
panelPosition,
|
||||||
|
audioDeviceName,
|
||||||
} = useSettings();
|
} = useSettings();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -107,6 +109,12 @@ export function SettingsTab() {
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</FormItem> */}
|
</FormItem> */}
|
||||||
|
<FormItem label="Audio Device">
|
||||||
|
<AudioDeviceSelector
|
||||||
|
audioDeviceName={audioDeviceName}
|
||||||
|
onChange={(audioDeviceName) => settingsMap.setKey('audioDeviceName', audioDeviceName)}
|
||||||
|
/>
|
||||||
|
</FormItem>
|
||||||
<FormItem label="Theme">
|
<FormItem label="Theme">
|
||||||
<SelectInput options={themeOptions} value={theme} onChange={(theme) => settingsMap.setKey('theme', theme)} />
|
<SelectInput options={themeOptions} value={theme} onChange={(theme) => settingsMap.setKey('theme', theme)} />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ export const defaultSettings = {
|
||||||
panelPosition: 'bottom',
|
panelPosition: 'bottom',
|
||||||
userPatterns: '{}',
|
userPatterns: '{}',
|
||||||
activePattern: '',
|
activePattern: '',
|
||||||
|
audioDeviceName: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const settingsMap = persistentMap('strudel-settings', defaultSettings);
|
export const settingsMap = persistentMap('strudel-settings', defaultSettings);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue