Files
WebSpeak-client-for-TeamSpeak/web/src/components/web-client/AudioSettingsDialog.vue
T
OddTurtleHead 5d3af8dfbb fix(voice): keep both mic-test takes playable
Record the original take from a pre-denoise AudioContext tap instead of
the already-captured getUserMedia track, and wait for every MediaRecorder
onstop before publishing URLs so a late dry dataavailable is not dropped.
Show an explicit missing-take message when a tab has no playable URL,
and flush recorders before releasing standalone capture.
2026-12-03 23:57:06 +08:00

398 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div
class="modal-backdrop"
@click.self="emit('close')"
>
<section
class="settings-modal"
data-ws-part="voice.audio-settings"
ref="dialog"
tabindex="-1"
@keydown="onDialogKeydown"
role="dialog"
aria-modal="true"
:aria-labelledby="'settings-title'"
>
<div class="settings-main"
><header class="settings-header"
><h2 id="settings-title">{{ t("audioConfiguration") }}</h2
><button
class="round-icon"
:title="t('close')"
@click="emit('close')"
><Icon
name="close"
:size="19" /></button></header
><div class="settings-content">
<div class="settings-audio-card">
<section class="settings-section"
><h3
><Icon
name="mic"
:size="20"
/>
{{ t("inputDevice") }}</h3
><div class="settings-form-row"
><label
class="settings-label"
for="input-device"
>{{ t("microphone") }}</label
><AudioDeviceSelect
id="input-device"
:model-value="selectedInputDeviceId"
:options="inputDeviceOptions"
:label="t('microphone')"
:disabled="!inputDevices.length"
@change="onInputDeviceChange"
/></div
><p
v-if="audioSettingsError"
class="settings-error"
>{{ localizedMessage(audioSettingsError) }}</p
><p class="audio-diagnostic"
><span>{{ t("permission") }}</span
><strong :class="`permission-${audioPermission}`">{{
audioPermission === "granted"
? t("permissionGranted")
: audioPermission === "denied"
? t("permissionDenied")
: t("permissionUnknown")
}}</strong></p
><div class="microphone-control"
><div
><label class="settings-label">{{ t("microphoneState") }}</label
><p class="settings-hint">{{
microphoneMuted ? t("microphoneMutedHint") : t("microphoneActiveHint")
}}</p></div
><button
type="button"
class="microphone-toggle"
:class="{ muted: microphoneMuted }"
:aria-pressed="!microphoneMuted"
@click="toggleMicrophone"
><Icon
:name="microphoneMuted ? 'mic-off' : 'mic'"
:size="16"
/>
{{ microphoneMuted ? t("unmuteMic") : t("muteMic") }}</button
></div
><label class="settings-form-row">
<span><strong>{{ t("noiseSuppression") }}</strong><small>{{ t("noiseSuppressionHint") }}</small></span>
<select class="noise-suppression-level" :value="noiseSuppressionMode" :aria-label="t('noiseSuppression')" @change="onNoiseSuppressionChange">
<option value="off">{{ t("noiseSuppressionOff") }}</option>
<option value="browser">{{ t("noiseSuppressionStandard") }}</option>
<option value="rnnoise">{{ t("noiseSuppressionLight") }}</option>
<option value="gtcrn">{{ t("noiseSuppressionStrong") }}</option>
</select>
</label
><p class="settings-hint noise-suppression-status" :class="{ 'settings-error': Boolean(processingFallback) || Boolean(processingWarning) }">{{ processingStatus }}</p
><div class="settings-form-row settings-range-form-row"
><label class="settings-label">{{ t("inputVolume") }}</label
><div class="settings-form-control"
><div class="settings-range-row"
><strong>{{ Math.round(inputVolume * 100) }}%</strong></div
><input
class="settings-range"
type="range"
min="0"
max="100"
:value="inputVolume * 100"
:style="rangeStyle(inputVolume, 1)"
:aria-label="t('inputVolume')"
@input="onInputVolume"
/></div
></div
><div class="settings-form-row settings-range-form-row"
><label class="settings-label">{{ t("voxThreshold") }}</label
><div class="settings-form-control"
><div class="settings-range-row"
><strong>{{ (voxThreshold * 100).toFixed(1) }}%</strong></div
><input
class="settings-range"
type="range"
min="1"
max="80"
:value="voxThreshold * 1000"
:style="rangeStyle(voxThreshold, 0.08)"
:aria-label="t('voxThreshold')"
@input="onVoxThreshold"
/></div
></div
><div class="audio-level-row"
><span>{{ t("micLevel") }}</span
><strong>{{ Math.round(micLevel * 100) }}%</strong></div
><div class="audio-level-track"
><i :style="{ width: `${Math.round(micLevel * 100)}%` }"></i></div
><div class="mic-test"
><div class="mic-test-header"
><strong>{{ t("microphoneTest") }}</strong
><button
type="button"
@click="toggleMicTest"
>{{ microphoneTestActive ? t("stopTest") : t("startTest") }}</button
></div
><div class="meter"
><i
v-for="index in 24"
:key="index"
:class="{ active: microphoneTestActive && index <= micMeterBars }"
:style="{ height: `${meterBarHeight(index)}px` }"
></i></div
><div class="meter-labels"
><span>{{ t("silence") }}</span
><span>{{ t("optimal") }}</span
><span>{{ t("loud") }}</span></div
><p class="settings-hint">{{ t("localMicTestHint") }}</p
><div v-if="testAudioUrl || testAudioDryUrl" class="mic-test-compare">
<div class="mic-test-tabs" role="group" :aria-label="t('micTestCompareHint')">
<button type="button" :class="{ active: testVariant === 'processed' }" :aria-pressed="testVariant === 'processed'" @click="testVariant = 'processed'">{{ t("micTestDenoised") }}</button>
<button type="button" :class="{ active: testVariant === 'dry' }" :aria-pressed="testVariant === 'dry'" @click="testVariant = 'dry'">{{ t("micTestOriginal") }}</button>
</div>
<audio
v-if="testAudioSrc"
class="test-audio"
:src="testAudioSrc"
controls
:aria-label="t('microphoneTest')"
></audio>
<p
v-else
class="settings-hint mic-test-missing"
role="status"
>{{ t(testAudioMissingKey) }}</p>
<p class="settings-hint">{{ t("micTestCompareHint") }}</p>
</div></div
></section>
<div class="settings-separator"></div
><section class="settings-section"
><h3
><Icon
name="volume"
:size="20"
/>
{{ t("outputVolume") }}</h3
><div
v-if="outputDeviceSupported"
class="settings-form-row"
><label
class="settings-label"
for="output-device"
>{{ t("outputDevice") }}</label
><AudioDeviceSelect
id="output-device"
:model-value="selectedOutputDeviceId"
:options="outputDeviceOptions"
:label="t('outputDevice')"
:disabled="!outputDevices.length"
@change="onOutputDeviceChange"
/></div
><p
v-else
class="mode-note"
><Icon
name="info"
:size="16"
/><span>{{ t("outputDeviceUnsupported") }}</span></p
><div class="settings-form-row settings-range-form-row"
><label class="settings-label">{{ t("speakers") }}</label
><div class="settings-form-control"
><div class="settings-range-row"
><strong>{{ Math.round(outputVolume * 100) }}%</strong></div
><input
class="settings-range"
type="range"
min="0"
max="100"
:value="outputVolume * 100"
:style="rangeStyle(outputVolume, 1)"
:aria-label="t('outputVolume')"
@input="onOutputVolume"
/></div
></div
><div class="settings-form-row settings-range-form-row"
><label class="settings-label">{{ t("notificationVolume") }}</label
><div class="settings-form-control"
><div class="settings-range-row"
><strong>{{ Math.round(notificationVolume * 100) }}%</strong></div
><input
class="settings-range"
type="range"
min="0"
max="100"
:value="notificationVolume * 100"
:style="rangeStyle(notificationVolume, 1)"
:aria-label="t('notificationVolume')"
@input="onNotificationVolume"
/></div
></div
><div class="audio-diagnostic"
><span>{{ t("audioStatus") }}</span
><strong>{{
audioContextState === "running"
? microphoneError
? t("audioUnavailable")
: t("audioReady")
: audioContextState === "suspended"
? t("audioSuspended")
: t("audioUnknown")
}}</strong></div
><p
v-if="microphoneError"
class="settings-error"
>{{ localizedMessage(microphoneError) }}</p
><div class="mode-note"
><Icon
name="shield"
:size="16"
/><span>{{ t("audioPrivacy") }}</span></div
></section
></div
></div
><footer class="settings-footer"
><button
class="primary-button save-button"
@click="emit('close')"
>{{ t("done") }}</button
></footer
></div
>
</section>
</div>
</template>
<script setup lang="ts">
import { computed, ref, type CSSProperties } from "vue";
import { micTestPlaybackSource } from "../../voice/microphone-test.js";
import Icon from "../Icon.vue";
import AudioDeviceSelect from "./AudioDeviceSelect.vue";
import { useDialogFocus } from "../../composables/useDialogFocus.js";
import type { useVoiceWebSocket } from "../../composables/useVoiceWebSocket.js";
import type { useWebClientAudioControls } from "../../composables/useWebClientAudioControls.js";
type AudioSettingsState = Pick<ReturnType<typeof useVoiceWebSocket>,
| "inputDevices"
| "outputDevices"
| "selectedInputDeviceId"
| "selectedOutputDeviceId"
| "outputDeviceSupported"
| "audioPermission"
| "audioContextState"
| "microphoneMuted"
| "noiseSuppressionMode"
| "microphoneProcessing"
| "inputVolume"
| "outputVolume"
| "voxThreshold"
| "notificationVolume"
| "micLevel"
| "microphoneTestActive"
| "testAudioUrl"
| "testAudioDryUrl"
>;
type AudioSettingsControls = Pick<ReturnType<typeof useWebClientAudioControls>,
| "settingsError"
| "onInputVolume"
| "onNoiseSuppressionChange"
| "onOutputVolume"
| "onVoxThreshold"
| "onNotificationVolume"
| "onInputDeviceChange"
| "onOutputDeviceChange"
| "toggleMicTest"
| "micMeterBars"
| "meterBarHeight"
| "toggleMicrophone"
>;
const props = defineProps<{
model: AudioSettingsState;
controls: AudioSettingsControls;
microphoneError: string;
t: (key: string, variables?: Record<string, string | number>) => string;
localizedMessage: (message: string) => string;
rangeStyle: (value: number, max: number) => CSSProperties;
}>();
const emit = defineEmits<{ close: [] }>();
const dialog = ref<HTMLElement | null>(null);
const { onDialogKeydown } = useDialogFocus(dialog, () => emit("close"));
// The page owns the stable voice refs and audio controller; this dialog only presents them.
const {
inputDevices,
outputDevices,
selectedInputDeviceId,
selectedOutputDeviceId,
outputDeviceSupported,
audioPermission,
audioContextState,
microphoneMuted,
noiseSuppressionMode,
microphoneProcessing,
inputVolume,
outputVolume,
voxThreshold,
notificationVolume,
micLevel,
microphoneTestActive,
testAudioUrl,
testAudioDryUrl,
} = props.model;
const {
settingsError: audioSettingsError,
onInputVolume,
onNoiseSuppressionChange,
onOutputVolume,
onVoxThreshold,
onNotificationVolume,
onInputDeviceChange,
onOutputDeviceChange,
toggleMicTest,
micMeterBars,
meterBarHeight,
toggleMicrophone,
} = props.controls;
const inputDeviceOptions = computed(() => [
{ value: "", label: props.t("defaultMicrophone") },
...inputDevices.map((device, index) => ({
value: device.deviceId,
label: device.label || props.t("microphoneNumber", { index: index + 1 }),
})),
]);
const outputDeviceOptions = computed(() => [
{ value: "", label: props.t("defaultOutput") },
...outputDevices.map((device, index) => ({
value: device.deviceId,
label: device.label || props.t("speakerNumber", { index: index + 1 }),
})),
]);
const testVariant = ref<"processed" | "dry">("processed");
const testPlayback = computed(() => micTestPlaybackSource(testVariant.value, {
processed: testAudioUrl.value,
dry: testAudioDryUrl.value,
}));
const testAudioSrc = computed(() => testPlayback.value.src);
const testAudioMissingKey = computed(() => testPlayback.value.missingKey);
const processingFallback = computed(() => microphoneProcessing.fallback);
const processingWarning = computed(() => microphoneProcessing.warning);
// Reflect the engine that really ran, not the stored preference.
const processingStatus = computed(() => {
const fallback = microphoneProcessing.fallback;
if (fallback === "no-worklet") return props.t("noiseSuppressionFallbackNoWorklet");
if (fallback === "sample-rate") return props.t("noiseSuppressionFallbackSampleRate");
// A model that died after the browser suppressor was already disabled leaves
// no denoising at all: say so instead of promising a browser fallback.
if (fallback === "load-failed") {
return microphoneProcessing.engine === "off"
? props.t("noiseSuppressionModelInactive")
: props.t("noiseSuppressionFallbackLoad");
}
if (microphoneProcessing.warning === "browser-ns-active") return props.t("noiseSuppressionBrowserStacking");
if (microphoneProcessing.engine === "gtcrn") return `${props.t("noiseSuppressionActive")}:${props.t("noiseSuppressionEngineGtcrn")}`;
if (microphoneProcessing.engine === "rnnoise") return `${props.t("noiseSuppressionActive")}:${props.t("noiseSuppressionEngineRnnoise")}`;
if (microphoneProcessing.engine === "browser") return `${props.t("noiseSuppressionActive")}:${props.t("noiseSuppressionEngineBrowser")}`;
if (microphoneProcessing.requestedEngine !== "off") return props.t("noiseSuppressionPreparing");
return props.t("noiseSuppressionEngineOff");
});
</script>