add basic usb input mode functionallity

This commit is contained in:
2025-06-15 17:28:48 +02:00
parent d54d18987a
commit 005c3b550e
5 changed files with 287 additions and 91 deletions

View File

@@ -3,6 +3,7 @@ from itertools import filterfalse
import streamlit as st
import requests
from auracast import auracast_config
import logging as log
# Global: desired packetization time in ms for Opus (should match backend)
PTIME = 40
@@ -43,15 +44,44 @@ if audio_mode in ["Webapp", "USB"]:
quality = "High (48kHz)"
default_name = saved_settings.get('channel_names', ["Broadcast0"])[0]
default_lang = saved_settings.get('languages', ["deu"])[0]
default_input = saved_settings.get('input_device') or 'default'
stream_name = st.text_input("Channel Name", value=default_name)
language = st.text_input("Language (ISO 639-3)", value=default_lang)
# Input device selection for USB mode
if audio_mode == "USB":
try:
import sounddevice as sd # type: ignore
devs = sd.query_devices()
log.info('Found audio devices: %s', devs)
input_options = [
f"{idx}:{d['name']}"
for idx, d in enumerate(devs)
if d.get('max_input_channels', 0) > 0 and ("(hw:" in d['name'].lower() or "usb" in d['name'].lower())
]
except Exception:
input_options = []
if not input_options:
st.error("No hardware audio input devices found.")
st.stop()
if default_input not in input_options:
default_input = input_options[0]
selected_option = st.selectbox("Input Device", input_options, index=input_options.index(default_input))
# We send only the numeric/card identifier (before :) or 'default'
input_device = selected_option.split(":", 1)[0] if ":" in selected_option else selected_option
else:
input_device = None
start_stream = st.button("Start Auracast")
if start_stream:
# Prepare config using the model (do NOT send qos_config, only relevant fields)
q = quality_map[quality]
config = auracast_config.AuracastConfigGroup(
auracast_sampling_rate_hz=q['rate'],
octets_per_frame=q['octets'],
transport="auto",
bigs = [
auracast_config.AuracastBigConfig(
@@ -59,11 +89,11 @@ if audio_mode in ["Webapp", "USB"]:
program_info=f"{stream_name} {quality}",
language=language,
audio_source=(
"webrtc" if audio_mode == "Webapp" else (
"usb" if audio_mode == "USB" else "network"
f"device:{input_device}" if audio_mode == "USB" else (
"webrtc" if audio_mode == "Webapp" else "network"
)
),
input_format="auto",
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'],
octets_per_frame=q['octets'],
@@ -73,7 +103,7 @@ if audio_mode in ["Webapp", "USB"]:
try:
r = requests.post(f"{BACKEND_URL}/init", json=config.model_dump())
if r.status_code == 200:
st.success("Stream initialized!")
st.success("Stream Started!")
else:
st.error(f"Failed to initialize: {r.text}")
except Exception as e:
@@ -127,3 +157,7 @@ else:
# else:
# st.error("Could not fetch advertised streams.")
log.basicConfig(
level=log.DEBUG,
format='%(module)s.py:%(lineno)d %(levelname)s: %(message)s'
)