Compare commits

...

8 Commits

Author SHA1 Message Date
Martín (Netux) Rodríguez 753b08edf0 Update global audio volume exclusively through useReplContext effect
This ensures the audio volume is synced across tabs, since changing the volume in one tab would retrigger the effect in the other tabs.

It also means we can remove the explicit call to `setGlobalAudioVolume()` from <SettingsTab>
2024-09-03 18:24:48 -03:00
Martín (Netux) Rodríguez ccb1256aa2 Adjust settings number slider input width according to max contents
This is a bit overkill, but this way we can somewhat adjust the width of the input so no symbols are cut off (either by the input being too small, or the up/down arrows from the browser).

I am not happy about having to use `calc()` with some magic numbers either, but this is what looked nicest to me and had the least effort.
2024-09-03 18:21:04 -03:00
Martín (Netux) Rodríguez 657d89cb98 Fix settings number slider browser up arrow not increasing the value correctly when step < 1
If we keep flooring the value, but have a step of, e.g., 0.1, then we are never going to reach the next whole number value.

This applies specifically to the Audio Volume slider.
2024-09-03 18:12:42 -03:00
Martín (Netux) Rodríguez 5797c42753 Calculate global gain with a better approximation of human perceived loudness
Taken from https://www.dr-lex.be/info-stuff/volumecontrols.html
2024-09-03 17:58:52 -03:00
Martín (Netux) Rodríguez afa55535ba Disable Audio Volume slider when Audio Engine Target is OSC 2024-09-03 15:42:41 -03:00
Martín (Netux) Rodríguez 1c94074ed6 Run codeformat 2024-09-03 15:23:20 -03:00
Martín (Netux) Rodríguez d2b6b257c0 Merge remote-tracking branch 'tidalcycles/main' into global-volume-slider 2024-09-03 14:40:03 -03:00
Martín (Netux) Rodríguez c6e1f758be Add Audio Volume setting to REPL
Allows for adjusting the global volume/gain of the REPL. Set default global value to 50%.

Acts similarily to `gain()`, but without modifying the code, so passerbys who don't know how to use the tool can adjust the volume too).

The volume slider uses a logarithmic scale, so it adjusts better to human sound perception.
2024-08-18 02:14:03 -03:00
5 changed files with 78 additions and 7 deletions
+8
View File
@@ -147,6 +147,14 @@ export function initializeAudioOutput() {
destinationGain.connect(audioContext.destination);
}
export function setGlobalGain(gain) {
if (destinationGain == null) {
initializeAudioOutput();
}
destinationGain.gain.value = gain;
}
// input: AudioNode, channels: ?Array<int>
export const connectToDestination = (input, channels = [0, 1]) => {
const ctx = getAudioContext();
@@ -1,6 +1,7 @@
import { defaultSettings, settingsMap, useSettings } from '../../../settings.mjs';
import { useMemo } from 'react';
import { audioEngineTargets, defaultSettings, settingsMap, useSettings } from '../../../settings.mjs';
import { themes } from '@strudel/codemirror';
import { isUdels } from '../../util.mjs';
import { isUdels, setGlobalAudioVolume } from '../../util.mjs';
import { ButtonGroup } from './Forms.jsx';
import { AudioDeviceSelector } from './AudioDeviceSelector.jsx';
import { AudioEngineTargetSelector } from './AudioEngineTargetSelector.jsx';
@@ -31,23 +32,47 @@ function SelectInput({ value, options, onChange }) {
);
}
function NumberSlider({ value, onChange, step = 1, ...rest }) {
function NumberSlider({ value, onChange, min, max, step = 1, ...rest }) {
const fractionalDigits = useMemo(() => {
const stepStr = step.toString();
const decimalPointIdx = stepStr.indexOf('.');
if (decimalPointIdx < 0) {
return 0;
}
return stepStr.slice(decimalPointIdx + 1).length;
}, [step]);
const textInputCharWidth = useMemo(() => {
const maxValueWholePartLength = Math.floor(max).toString().length;
return maxValueWholePartLength + '.'.length + fractionalDigits;
}, [max, fractionalDigits]);
return (
<div className="flex space-x-2 gap-1">
<input
className="p-2 grow"
type="range"
value={value}
min={min}
max={max}
step={step}
onChange={(e) => onChange(Number(e.target.value))}
{...rest}
/>
<input
type="number"
value={value}
style={{
// approximate text size + some leeway for the default padding + some space between the browser's up/down arrows and the input's value
width: `calc(${textInputCharWidth}ch + 2 * 0.75rem + 1rem)`,
}}
value={Number(value).toFixed(fractionalDigits)}
min={min}
max={max}
step={step}
className="w-16 bg-background rounded-md"
className="bg-background rounded-md"
onChange={(e) => onChange(Number(e.target.value))}
{...rest}
/>
</div>
);
@@ -101,6 +126,7 @@ export function SettingsTab({ started }) {
panelPosition,
audioDeviceName,
audioEngineTarget,
audioVolume,
} = useSettings();
const shouldAlwaysSync = isUdels();
const canChangeAudioDevice = AudioContext.prototype.setSinkId != null;
@@ -135,6 +161,19 @@ export function SettingsTab({ started }) {
}}
/>
</FormItem>
<FormItem label="Audio Volume">
{audioEngineTarget === audioEngineTargets.osc && (
<span class="text-sm italic">Has no effect when Audio Engine Target is OSC</span>
)}
<NumberSlider
value={audioVolume}
onChange={(audioVolume) => settingsMap.setKey('audioVolume', audioVolume)}
min={0}
max={100}
step={0.1}
disabled={audioEngineTarget === audioEngineTargets.osc}
/>
</FormItem>
<FormItem label="Theme">
<SelectInput options={themeOptions} value={theme} onChange={(theme) => settingsMap.setKey('theme', theme)} />
</FormItem>
+5 -1
View File
@@ -14,7 +14,7 @@ import {
resetLoadedSounds,
initAudioOnFirstClick,
} from '@strudel/webaudio';
import { getAudioDevices, setAudioDevice, setVersionDefaultsFrom } from './util.mjs';
import { getAudioDevices, setAudioDevice, setGlobalAudioVolume, setVersionDefaultsFrom } from './util.mjs';
import { StrudelMirror, defaultSettings } from '@strudel/codemirror';
import { clearHydra } from '@strudel/hydra';
import { useCallback, useEffect, useRef, useState } from 'react';
@@ -162,6 +162,7 @@ export function useReplContext() {
// on first load, set stored audio device if possible
useEffect(() => {
const { audioDeviceName } = _settings;
if (audioDeviceName !== defaultAudioDeviceName) {
getAudioDevices().then((devices) => {
const deviceID = devices.get(audioDeviceName);
@@ -173,6 +174,9 @@ export function useReplContext() {
}
}, []);
// set stored audio volume
useEffect(() => setGlobalAudioVolume(_settings.audioVolume), [_settings.audioVolume]);
//
// UI Actions
//
+20 -1
View File
@@ -1,6 +1,12 @@
import { evalScope, hash2code, logger } from '@strudel/core';
import { settingPatterns, defaultAudioDeviceName } from '../settings.mjs';
import { getAudioContext, initializeAudioOutput, setDefaultAudioContext, setVersionDefaults } from '@strudel/webaudio';
import {
getAudioContext,
initializeAudioOutput,
setDefaultAudioContext,
setGlobalGain,
setVersionDefaults,
} from '@strudel/webaudio';
import { getMetadata } from '../metadata_parser';
import { isTauri } from '../tauri.mjs';
import './Repl.css';
@@ -188,6 +194,19 @@ export const setAudioDevice = async (id) => {
initializeAudioOutput();
};
const NATURAL_LOG_10 = Math.log(1000);
const LOW_VOLUME_CONSTANT = Math.exp(0.1 * NATURAL_LOG_10);
export const setGlobalAudioVolume = (volume) => {
// Gain is calculated to adjust the volume to a logarithmic scale to match how us humans perceive loudness.
// Formula is taken from https://www.dr-lex.be/info-stuff/volumecontrols.html
volume /= 100; // [0, 1]
let gain = volume >= 0.1 ? 0.001 * Math.exp(NATURAL_LOG_10 * volume) : volume * 10 * 0.001 * LOW_VOLUME_CONSTANT;
gain = Math.max(0, Math.min(gain, 1)); // just in case
setGlobalGain(gain);
};
export function setVersionDefaultsFrom(code) {
try {
const metadata = getMetadata(code);
+1
View File
@@ -34,6 +34,7 @@ export const defaultSettings = {
userPatterns: '{}',
audioDeviceName: defaultAudioDeviceName,
audioEngineTarget: audioEngineTargets.webaudio,
audioVolume: 50,
};
let search = null;