mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-25 20:48:27 +00:00
fixing async stuff
This commit is contained in:
parent
52628920d9
commit
5281c61f99
@ -86,7 +86,6 @@ let channelMerger, destinationGain;
|
|||||||
export function initializeAudioOutput() {
|
export function initializeAudioOutput() {
|
||||||
const audioContext = getAudioContext();
|
const audioContext = getAudioContext();
|
||||||
const maxChannelCount = audioContext.destination.maxChannelCount;
|
const maxChannelCount = audioContext.destination.maxChannelCount;
|
||||||
console.log(maxChannelCount);
|
|
||||||
audioContext.destination.channelCount = maxChannelCount;
|
audioContext.destination.channelCount = maxChannelCount;
|
||||||
channelMerger = new ChannelMergerNode(audioContext, { numberOfInputs: audioContext.destination.channelCount });
|
channelMerger = new ChannelMergerNode(audioContext, { numberOfInputs: audioContext.destination.channelCount });
|
||||||
destinationGain = new GainNode(audioContext);
|
destinationGain = new GainNode(audioContext);
|
||||||
|
|||||||
@ -1,70 +1,61 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect, useCallback } from 'react';
|
||||||
import { getAudioContext, initializeAudioOutput } from '@strudel.cycles/webaudio';
|
import { getAudioContext, initializeAudioOutput } from '@strudel.cycles/webaudio';
|
||||||
import { SelectInput } from './SelectInput';
|
import { SelectInput } from './SelectInput';
|
||||||
|
|
||||||
async function setAudioDevice(id) {
|
const initdevices = new Map();
|
||||||
const audioCtx = getAudioContext();
|
|
||||||
await audioCtx.setSinkId(id);
|
|
||||||
await initializeAudioOutput();
|
|
||||||
}
|
|
||||||
export function AudioDeviceSelector({ audioDeviceName, onChange }) {
|
export function AudioDeviceSelector({ audioDeviceName, onChange }) {
|
||||||
const [options, setOptions] = useState({});
|
const [devices, setDevices] = useState(initdevices);
|
||||||
const [optionsInitialized, setOptionsInitialized] = useState(false);
|
const devicesInitialized = devices.size > 0;
|
||||||
|
|
||||||
async function initializeOptions() {
|
const setAudioDevice = useCallback(async (id) => {
|
||||||
|
const audioCtx = getAudioContext();
|
||||||
|
await audioCtx.setSinkId(id);
|
||||||
|
initializeAudioOutput();
|
||||||
|
});
|
||||||
|
const initializedevices = useCallback(async () => {
|
||||||
await navigator.mediaDevices.getUserMedia({ audio: true });
|
await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
let devices = await navigator.mediaDevices.enumerateDevices();
|
let mediaDevices = await navigator.mediaDevices.enumerateDevices();
|
||||||
devices = devices.filter((device) => device.kind === 'audiooutput' && device.deviceId !== 'default');
|
mediaDevices = mediaDevices.filter((device) => device.kind === 'audiooutput' && device.deviceId !== 'default');
|
||||||
const optionsArray = [];
|
const devicesMap = new Map();
|
||||||
devices.forEach((device) => {
|
mediaDevices.forEach((device) => {
|
||||||
optionsArray.push([device.deviceId, device.label]);
|
devicesMap.set(device.label, device.deviceId);
|
||||||
});
|
});
|
||||||
const options = Object.fromEntries(optionsArray);
|
setDevices(devicesMap);
|
||||||
setOptions(options);
|
return devicesMap;
|
||||||
setOptionsInitialized(true);
|
}, []);
|
||||||
return options;
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!audioDeviceName.length || optionsInitialized) {
|
if (!audioDeviceName.length || devicesInitialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
initializedevices().then((devices) => {
|
||||||
(async () => {
|
const deviceID = devices.get(audioDeviceName);
|
||||||
const options = await initializeOptions();
|
|
||||||
|
|
||||||
const deviceID = Object.keys(options).find((id) => options[id] === audioDeviceName);
|
|
||||||
|
|
||||||
if (deviceID == null) {
|
if (deviceID == null) {
|
||||||
onChange('');
|
onChange('');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
setAudioDevice(deviceID);
|
||||||
await setAudioDevice(deviceID);
|
});
|
||||||
})();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
if (optionsInitialized) {
|
if (devicesInitialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(async () => {
|
initializedevices();
|
||||||
await initializeOptions();
|
|
||||||
})();
|
|
||||||
};
|
};
|
||||||
const onDeviceChange = (deviceID) => {
|
const onDeviceChange = (deviceName) => {
|
||||||
(async () => {
|
if (!devicesInitialized) {
|
||||||
const deviceName = options[deviceID];
|
return;
|
||||||
onChange(deviceName);
|
}
|
||||||
await setAudioDevice(deviceID);
|
const deviceID = devices.get(deviceName);
|
||||||
})();
|
onChange(deviceName);
|
||||||
|
deviceID && setAudioDevice(deviceID);
|
||||||
};
|
};
|
||||||
return (
|
const options = new Set();
|
||||||
<SelectInput
|
|
||||||
options={options}
|
Array.from(devices.keys()).forEach((deviceName) => {
|
||||||
onClick={onClick}
|
options.add({ id: deviceName, label: deviceName });
|
||||||
value={Object.keys(options).find((id) => options[id] === audioDeviceName)}
|
});
|
||||||
onChange={onDeviceChange}
|
return <SelectInput options={options} onClick={onClick} value={audioDeviceName} onChange={onDeviceChange} />;
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
// value: ID, options: Set<{id: ID, label: string}>, onChange: ID => null, onClick: event => void
|
||||||
export function SelectInput({ value, options, onChange }) {
|
export function SelectInput({ value, options, onChange, onClick }) {
|
||||||
return (
|
return (
|
||||||
<select
|
<select
|
||||||
|
onClick={onClick}
|
||||||
className="p-2 bg-background rounded-md text-foreground"
|
className="p-2 bg-background rounded-md text-foreground"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={(e) => onChange(e.target.value)}
|
onChange={(e) => onChange(e.target.value)}
|
||||||
>
|
>
|
||||||
{Object.entries(options).map(([k, label]) => (
|
{Array.from(options).map(({ id, label }) => (
|
||||||
<option key={k} className="bg-background" value={k}>
|
<option key={id} className="bg-background" value={id}>
|
||||||
{label}
|
{label}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@ -109,12 +109,14 @@ export function SettingsTab() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</FormItem> */}
|
</FormItem> */}
|
||||||
<FormItem label="Audio Device">
|
{AudioContext.prototype.setSinkId != null && (
|
||||||
<AudioDeviceSelector
|
<FormItem label="Audio Device">
|
||||||
audioDeviceName={audioDeviceName}
|
<AudioDeviceSelector
|
||||||
onChange={(audioDeviceName) => settingsMap.setKey('audioDeviceName', audioDeviceName)}
|
audioDeviceName={audioDeviceName}
|
||||||
/>
|
onChange={(audioDeviceName) => settingsMap.setKey('audioDeviceName', audioDeviceName)}
|
||||||
</FormItem>
|
/>
|
||||||
|
</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>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user