198 lines
8.8 KiB
JavaScript
198 lines
8.8 KiB
JavaScript
window.Kronick = window.Kronick || {};
|
|
Kronick.state = Kronick.state || {};
|
|
|
|
const AUDIO_DEFAULTS = {
|
|
voiceActivationEnabled: true,
|
|
vadThreshold: -66,
|
|
vadHoldMs: 200,
|
|
noiseSuppressionEnabled: true,
|
|
noiseSuppressionMode: 'hybrid',
|
|
rnnoiseStrength: 100,
|
|
echoCancellation: true,
|
|
autoGainControl: true,
|
|
spatialAudioEnabled: true,
|
|
spatialWidth: 100,
|
|
spatialDepth: 100,
|
|
spatialFalloff: 100
|
|
};
|
|
|
|
const AUDIO_KEYS = {
|
|
voiceActivationEnabled: 'kronick_voice_activation_enabled',
|
|
vadThreshold: 'kronick_vad_threshold',
|
|
vadHoldMs: 'kronick_vad_hold',
|
|
noiseSuppressionEnabled: 'kronick_noise_suppression_enabled',
|
|
noiseSuppressionMode: 'kronick_noise_suppression_mode',
|
|
rnnoiseStrength: 'kronick_rnnoise_strength',
|
|
echoCancellation: 'kronick_echo_cancellation',
|
|
autoGainControl: 'kronick_auto_gain',
|
|
spatialAudioEnabled: 'kronick_spatial_audio_enabled',
|
|
spatialWidth: 'kronick_spatial_width',
|
|
spatialDepth: 'kronick_spatial_depth',
|
|
spatialFalloff: 'kronick_spatial_falloff'
|
|
};
|
|
|
|
function readBool(key, fallback) {
|
|
const value = localStorage.getItem(key);
|
|
return value === null ? fallback : value === 'true';
|
|
}
|
|
|
|
function readNumber(key, fallback) {
|
|
const value = Number(localStorage.getItem(key));
|
|
return Number.isFinite(value) ? value : fallback;
|
|
}
|
|
|
|
Kronick.loadAudioSettings = function loadAudioSettings() {
|
|
Kronick.state.voiceActivationEnabled = readBool(AUDIO_KEYS.voiceActivationEnabled, AUDIO_DEFAULTS.voiceActivationEnabled);
|
|
Kronick.state.vadThreshold = readNumber(AUDIO_KEYS.vadThreshold, AUDIO_DEFAULTS.vadThreshold);
|
|
Kronick.state.vadHoldMs = readNumber(AUDIO_KEYS.vadHoldMs, AUDIO_DEFAULTS.vadHoldMs);
|
|
Kronick.state.noiseSuppressionEnabled = readBool(AUDIO_KEYS.noiseSuppressionEnabled, AUDIO_DEFAULTS.noiseSuppressionEnabled);
|
|
Kronick.state.noiseSuppressionMode = localStorage.getItem(AUDIO_KEYS.noiseSuppressionMode) || AUDIO_DEFAULTS.noiseSuppressionMode;
|
|
Kronick.state.rnnoiseStrength = readNumber(AUDIO_KEYS.rnnoiseStrength, AUDIO_DEFAULTS.rnnoiseStrength);
|
|
Kronick.state.echoCancellation = readBool(AUDIO_KEYS.echoCancellation, AUDIO_DEFAULTS.echoCancellation);
|
|
Kronick.state.autoGainControl = readBool(AUDIO_KEYS.autoGainControl, AUDIO_DEFAULTS.autoGainControl);
|
|
Kronick.state.spatialAudioEnabled = readBool(AUDIO_KEYS.spatialAudioEnabled, AUDIO_DEFAULTS.spatialAudioEnabled);
|
|
Kronick.state.spatialWidth = readNumber(AUDIO_KEYS.spatialWidth, AUDIO_DEFAULTS.spatialWidth);
|
|
Kronick.state.spatialDepth = readNumber(AUDIO_KEYS.spatialDepth, AUDIO_DEFAULTS.spatialDepth);
|
|
Kronick.state.spatialFalloff = readNumber(AUDIO_KEYS.spatialFalloff, AUDIO_DEFAULTS.spatialFalloff);
|
|
};
|
|
|
|
Kronick.loadAudioSettings();
|
|
|
|
function saveSetting(stateKey, value) {
|
|
Kronick.state[stateKey] = value;
|
|
localStorage.setItem(AUDIO_KEYS[stateKey], String(value));
|
|
}
|
|
|
|
function bindCheckbox(id, stateKey, onChange) {
|
|
const input = document.getElementById(id);
|
|
if (!input) return;
|
|
input.checked = Boolean(Kronick.state[stateKey]);
|
|
input.addEventListener('change', () => {
|
|
saveSetting(stateKey, input.checked);
|
|
onChange?.(input.checked);
|
|
});
|
|
}
|
|
|
|
function bindRange(id, valueId, stateKey, formatter, onChange) {
|
|
const input = document.getElementById(id);
|
|
const value = document.getElementById(valueId);
|
|
if (!input) return;
|
|
input.value = Kronick.state[stateKey];
|
|
const render = () => {
|
|
const parsed = Number(input.value);
|
|
if (value) value.textContent = formatter(parsed);
|
|
saveSetting(stateKey, parsed);
|
|
onChange?.(parsed);
|
|
};
|
|
if (value) value.textContent = formatter(Number(input.value));
|
|
input.addEventListener('input', render);
|
|
}
|
|
|
|
Kronick.updateNoiseModeUi = function updateNoiseModeUi() {
|
|
const enabled = Boolean(Kronick.state.noiseSuppressionEnabled);
|
|
const select = document.getElementById('noiseSuppressionMode');
|
|
const strength = document.getElementById('rnnoiseStrengthControl');
|
|
const help = document.getElementById('noiseModeHelp');
|
|
const mode = enabled ? Kronick.state.noiseSuppressionMode : 'off';
|
|
|
|
if (select) {
|
|
select.disabled = !enabled;
|
|
if (enabled) select.value = Kronick.state.noiseSuppressionMode;
|
|
}
|
|
|
|
if (strength) {
|
|
strength.hidden = !enabled || !['rnnoise', 'hybrid'].includes(mode);
|
|
}
|
|
|
|
const descriptions = {
|
|
off: 'Noise filtering is bypassed. Echo cancellation and auto gain can still be controlled below.',
|
|
browser: 'Uses the browser/WebRTC noise filter. Lowest overhead and usually best for headsets.',
|
|
rnnoise: 'Uses the local RNNoise neural filter. Better for fans, HVAC and steady background noise.',
|
|
hybrid: 'Runs browser cleanup first, then RNNoise. Strongest filtering, but can be too aggressive on some microphones.'
|
|
};
|
|
if (help) help.textContent = descriptions[mode] || descriptions.hybrid;
|
|
};
|
|
|
|
Kronick.refreshSpatialAudio = function refreshSpatialAudio() {
|
|
if (!Kronick.state.participantPanners) return;
|
|
Kronick.state.participantPanners.forEach((panner, sid) => {
|
|
panner.rolloffFactor = Kronick.state.spatialAudioEnabled
|
|
? Math.max(0, Kronick.state.spatialFalloff / 100)
|
|
: 0;
|
|
if (typeof Kronick.updateSpatialAudioPosition === 'function') {
|
|
Kronick.updateSpatialAudioPosition(sid);
|
|
}
|
|
});
|
|
};
|
|
|
|
Kronick.updateVoiceMeter = function updateVoiceMeter(db) {
|
|
const fill = document.getElementById('voiceMeterFill');
|
|
const label = document.getElementById('voiceMeterDb');
|
|
const marker = document.getElementById('voiceThresholdMarker');
|
|
const min = -80;
|
|
const max = -10;
|
|
const clamped = Math.max(min, Math.min(max, Number.isFinite(db) ? db : min));
|
|
const percent = ((clamped - min) / (max - min)) * 100;
|
|
const thresholdPercent = ((Kronick.state.vadThreshold - min) / (max - min)) * 100;
|
|
if (fill) fill.style.width = `${percent}%`;
|
|
if (marker) marker.style.left = `${Math.max(0, Math.min(100, thresholdPercent))}%`;
|
|
if (label) label.textContent = Number.isFinite(db) && db > -79.9 ? `${db.toFixed(1)} dB` : '-∞ dB';
|
|
};
|
|
|
|
Kronick.initAudioSettings = function initAudioSettings() {
|
|
bindCheckbox('voiceActivationToggle', 'voiceActivationEnabled');
|
|
bindRange('vadThreshold', 'vadThresholdVal', 'vadThreshold', value => `${value} dB`, () => Kronick.updateVoiceMeter(Kronick.state.currentMicDb ?? -80));
|
|
bindRange('vadHold', 'vadHoldVal', 'vadHoldMs', value => `${value} ms`);
|
|
|
|
bindCheckbox('noiseSuppressionToggle', 'noiseSuppressionEnabled', Kronick.updateNoiseModeUi);
|
|
bindRange('rnnoiseStrength', 'rnnoiseStrengthVal', 'rnnoiseStrength', value => `${value}%`, value => {
|
|
const wet = Kronick.state.rnnoiseWetGain;
|
|
const dry = Kronick.state.rnnoiseDryGain;
|
|
if (!wet || !dry) return;
|
|
const mix = Math.max(0, Math.min(1, value / 100));
|
|
wet.gain.setTargetAtTime(mix, wet.context.currentTime, 0.015);
|
|
dry.gain.setTargetAtTime(1 - mix, dry.context.currentTime, 0.015);
|
|
});
|
|
bindCheckbox('echoCancellationToggle', 'echoCancellation');
|
|
bindCheckbox('autoGainToggle', 'autoGainControl');
|
|
|
|
const mode = document.getElementById('noiseSuppressionMode');
|
|
if (mode) {
|
|
mode.value = Kronick.state.noiseSuppressionMode;
|
|
mode.addEventListener('change', () => {
|
|
saveSetting('noiseSuppressionMode', mode.value);
|
|
Kronick.updateNoiseModeUi();
|
|
});
|
|
}
|
|
|
|
const applyButton = document.getElementById('applyAudioProcessingBtn');
|
|
if (applyButton) {
|
|
applyButton.addEventListener('click', async () => {
|
|
if (typeof Kronick.applyAudioProcessingSettings !== 'function') return;
|
|
applyButton.disabled = true;
|
|
const original = applyButton.textContent;
|
|
applyButton.textContent = 'Applying…';
|
|
try {
|
|
const applied = await Kronick.applyAudioProcessingSettings();
|
|
applyButton.textContent = applied ? 'Applied' : 'Saved for next join';
|
|
} catch (error) {
|
|
console.error('Could not apply audio processing settings:', error);
|
|
applyButton.textContent = 'Apply failed';
|
|
} finally {
|
|
window.setTimeout(() => {
|
|
applyButton.textContent = original;
|
|
applyButton.disabled = false;
|
|
}, 1200);
|
|
}
|
|
});
|
|
}
|
|
|
|
bindCheckbox('spatialAudioToggle', 'spatialAudioEnabled', Kronick.refreshSpatialAudio);
|
|
bindRange('spatialWidth', 'spatialWidthVal', 'spatialWidth', value => `${value}%`, Kronick.refreshSpatialAudio);
|
|
bindRange('spatialDepth', 'spatialDepthVal', 'spatialDepth', value => `${value}%`, Kronick.refreshSpatialAudio);
|
|
bindRange('spatialFalloff', 'spatialFalloffVal', 'spatialFalloff', value => `${value}%`, Kronick.refreshSpatialAudio);
|
|
|
|
Kronick.updateNoiseModeUi();
|
|
Kronick.updateVoiceMeter(-80);
|
|
};
|