feat: add gain control for USB and webapp microphone inputs with persistence - to be tested

This commit is contained in:
2025-06-18 16:34:28 +02:00
parent 22d27ce2f5
commit 0bf87c85b7
4 changed files with 200 additions and 93 deletions

View File

@@ -14,14 +14,27 @@ PTIME = 40
BACKEND_URL = "http://localhost:5000"
# Try loading persisted settings from backend
# This is the correct place to define saved_settings before it's used for defaults
saved_settings = {}
try:
resp = requests.get(f"{BACKEND_URL}/status", timeout=1)
if resp.status_code == 200:
saved_settings = resp.json()
except Exception:
# If backend is not available or error, saved_settings will be empty dict
# Defaults will be used for gain values in this case.
saved_settings = {}
# Initialize gain session states
# This must come AFTER saved_settings is populated.
default_webapp_gain = float(saved_settings.get('webapp_mic_gain', 1.0))
if 'webapp_mic_gain' not in st.session_state:
st.session_state.webapp_mic_gain = default_webapp_gain
default_usb_gain = float(saved_settings.get('usb_mic_gain', 1.0))
if 'usb_mic_gain' not in st.session_state:
st.session_state.usb_mic_gain = default_usb_gain
st.title("🎙️ Auracast Audio Mode Control")
# Audio mode selection with persisted default
@@ -53,9 +66,12 @@ if audio_mode in ["Webapp", "USB"]:
language = st.text_input("Language (ISO 639-3)", value=default_lang)
# Gain slider for Webapp mode
if audio_mode == "Webapp":
mic_gain = st.slider("Microphone Gain", 0.0, 4.0, 1.0, 0.1, help="Adjust microphone volume sent to Auracast")
else:
mic_gain = 1.0
st.session_state.webapp_mic_gain = st.slider(
"Microphone Gain", 0.0, 4.0, st.session_state.webapp_mic_gain, 0.1,
help="Adjust microphone volume sent to Auracast (applied by browser)"
)
# For USB mode, gain slider is defined below.
# The variable 'mic_gain' for JS is sourced from st.session_state.webapp_mic_gain within Webapp mode logic.
# Input device selection for USB mode
if audio_mode == "USB":
@@ -87,6 +103,16 @@ if audio_mode in ["Webapp", "USB"]:
st.rerun()
# We send only the numeric/card identifier (before :) or 'default'
input_device = selected_option.split(":", 1)[0] if ":" in selected_option else selected_option
# USB Microphone Gain Slider
st.session_state.usb_mic_gain = st.slider(
"Microphone Gain (USB)",
min_value=0.0,
max_value=4.0,
value=st.session_state.usb_mic_gain, # Use session state value
step=0.1,
help="Adjust microphone volume for USB input (applied by server)"
)
else:
input_device = None
start_stream = st.button("Start Auracast")
@@ -96,7 +122,7 @@ if audio_mode in ["Webapp", "USB"]:
if audio_mode == "Webapp" and st.session_state.get('stream_started'):
update_js = f"""
<script>
if (window.gainNode) {{ window.gainNode.gain.value = {mic_gain}; }}
if (window.gainNode) {{ window.gainNode.gain.value = {st.session_state.webapp_mic_gain}; }}
</script>
"""
st.components.v1.html(update_js, height=0)
@@ -122,6 +148,17 @@ if audio_mode in ["Webapp", "USB"]:
import time; time.sleep(1)
# Prepare config using the model (do NOT send qos_config, only relevant fields)
q = quality_map[quality]
# Determine audio_source based on mode and gain settings
if audio_mode == "USB":
current_usb_gain = st.session_state.get('usb_mic_gain', 1.0) # Use .get for safety
audio_source_str = f"device:{input_device},gain={current_usb_gain}"
elif audio_mode == "Webapp":
audio_source_str = "webrtc"
# Webapp gain is handled client-side by JS using st.session_state.webapp_mic_gain
else: # Assuming a 'network' mode or other future modes
audio_source_str = "network" # Default or handle other modes
config = auracast_config.AuracastConfigGroup(
auracast_sampling_rate_hz=q['rate'],
octets_per_frame=q['octets'],
@@ -131,11 +168,7 @@ if audio_mode in ["Webapp", "USB"]:
name=stream_name,
program_info=f"{stream_name} {quality}",
language=language,
audio_source=(
f"device:{input_device}" if audio_mode == "USB" else (
"webrtc" if audio_mode == "Webapp" else "network"
)
),
audio_source=audio_source_str, # Use the constructed string
input_format=(f"int16le,{q['rate']},1" if audio_mode == "USB" else "auto"),
iso_que_len=1, # TODO: this should be way less to decrease delay
sampling_frequency=q['rate'],
@@ -160,7 +193,7 @@ if audio_mode in ["Webapp", "USB"]:
(async () => {{
if (window.webrtc_started) return; // Prevent re-init on rerun
window.webrtc_started = true;
const GAIN_VALUE = {mic_gain};
const GAIN_VALUE = {st.session_state.webapp_mic_gain};
const pc = new RTCPeerConnection(); // No STUN needed for localhost
const micStream = await navigator.mediaDevices.getUserMedia({{audio:true}});
// Create Web Audio gain processing