Merge branch 'main' into feature/1khz_testtone

This commit is contained in:
2026-04-10 07:55:36 +00:00
15 changed files with 13556 additions and 174 deletions

18
poetry.lock generated
View File

@@ -1804,6 +1804,22 @@ files = [
{file = "protobuf-6.30.2.tar.gz", hash = "sha256:35c859ae076d8c56054c25b59e5e59638d86545ed6e2b6efac6be0b6ea3ba048"},
]
[[package]]
name = "pyalsaaudio"
version = "0.11.0"
description = "ALSA bindings"
optional = false
python-versions = "*"
groups = ["main"]
files = []
develop = false
[package.source]
type = "git"
url = "ssh://git@gitea.summitwave.work:222/auracaster/sw_pyalsaaudio.git"
reference = "b3d11582e03df6929b2e7acbaa1306afc7b8a6bc"
resolved_reference = "b3d11582e03df6929b2e7acbaa1306afc7b8a6bc"
[[package]]
name = "pyarrow"
version = "20.0.0"
@@ -2947,4 +2963,4 @@ test = ["pytest", "pytest-asyncio"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.11"
content-hash = "e39f622c983015c1a1c86236114c339044130db172cd420eecdd17f546af20de"
content-hash = "7bccf2978170ead195e1e8cff151823a5276823195a239622186fcec830154d9"

View File

@@ -18,7 +18,8 @@ dependencies = [
"python-dotenv (>=1.1.1,<2.0.0)",
"smbus2 (>=0.5.0,<0.6.0)",
"samplerate (>=0.2.2,<0.3.0)",
"rpi-gpio (>=0.7.1,<0.8.0)"
"rpi-gpio (>=0.7.1,<0.8.0)",
"pyalsaaudio @ git+ssh://git@gitea.summitwave.work:222/auracaster/sw_pyalsaaudio.git@b3d11582e03df6929b2e7acbaa1306afc7b8a6bc"
]
[project.optional-dependencies]

View File

@@ -55,7 +55,6 @@ class AuracastBigConfig(BaseModel):
precode_wav: bool = False
iso_que_len: int = 64
num_bis: int = 1 # 1 = mono (FRONT_LEFT), 2 = stereo (FRONT_LEFT + FRONT_RIGHT)
input_gain_db: float = 0.0 # Software gain boost in dB applied before LC3 encoding (0 = off, max 20)
class AuracastBigConfigDeu(AuracastBigConfig):
id: int = 12
@@ -112,4 +111,5 @@ class AuracastConfigGroup(AuracastGlobalConfig):
bigs: List[AuracastBigConfig] = [
AuracastBigConfigDeu(),
]
analog_gain: int = 50 # ADC gain level for analog mode (10-60%)
analog_gain_db_left: float = 0.0 # ADC gain level for analog mode left channel (-12 to 18 dB)
analog_gain_db_right: float = 0.0 # ADC gain level for analog mode right channel (-12 to 18 dB)

View File

@@ -30,6 +30,7 @@ import time
import threading
import numpy as np # for audio down-mix
import samplerate
import os
import lc3 # type: ignore # pylint: disable=E0401
@@ -56,7 +57,7 @@ from auracast.utils.webrtc_audio_input import WebRTCAudioInput
# Patch sounddevice.InputStream globally to use low-latency settings
import sounddevice as sd
import alsaaudio
from collections import deque
@@ -139,96 +140,146 @@ class AlsaArecordAudioInput(audio_io.AudioInput):
self._proc = None
class ModSoundDeviceAudioInput(audio_io.SoundDeviceAudioInput):
"""Patched SoundDeviceAudioInput with low-latency capture and adaptive resampling."""
class PyAlsaAudioInput(audio_io.ThreadedAudioInput):
"""PyALSA audio input with non-blocking reads - supports mono/stereo."""
def _open(self):
"""Create RawInputStream with low-latency parameters and initialize ring buffer."""
dev_info = sd.query_devices(self._device)
hostapis = sd.query_hostapis()
api_index = dev_info.get('hostapi')
api_name = hostapis[api_index]['name'] if isinstance(api_index, int) and 0 <= api_index < len(hostapis) else 'unknown'
pa_ver = sd.get_portaudio_version()
def __init__(self, device, pcm_format: audio_io.PcmFormat):
super().__init__()
logging.info("PyALSA: device = %s", device)
self._device = str(device) if not isinstance(device, str) else device
if self._device.isdigit():
self._device = 'default' if self._device == '0' else f'hw:{self._device}'
self._pcm_format = pcm_format
self._pcm = None
self._actual_channels = None
self._periodsize = None
self._hw_channels = None
self._first_read = True
self._resampler = None
self._resampler_buffer = np.empty(0, dtype=np.float32)
logging.info(
"SoundDevice backend=%s device='%s' (id=%s) ch=%s default_low_input_latency=%.4f default_high_input_latency=%.4f portaudio=%s",
api_name,
dev_info.get('name'),
self._device,
dev_info.get('max_input_channels'),
float(dev_info.get('default_low_input_latency') or 0.0),
float(dev_info.get('default_high_input_latency') or 0.0),
pa_ver[1] if isinstance(pa_ver, tuple) and len(pa_ver) >= 2 else pa_ver,
)
# Create RawInputStream with injected low-latency parameters
# Target ~2 ms blocksize (48 kHz -> 96 frames). For other rates, keep ~2 ms.
_sr = int(self._pcm_format.sample_rate)
self.counter=0
self.max_avail=0
self.logfile_name="available_samples.txt"
self.blocksize = 120
if os.path.exists(self.logfile_name):
os.remove(self.logfile_name)
self._stream = sd.RawInputStream(
samplerate=self._pcm_format.sample_rate,
def _open(self) -> audio_io.PcmFormat:
ALSA_PERIODSIZE = 240
ALSA_PERIODS = 4
ALSA_MODE = alsaaudio.PCM_NONBLOCK
requested_rate = int(self._pcm_format.sample_rate)
requested_channels = int(self._pcm_format.channels)
self._periodsize = ALSA_PERIODSIZE
self._pcm = alsaaudio.PCM(
type=alsaaudio.PCM_CAPTURE,
mode=ALSA_MODE,
device=self._device,
channels=self._pcm_format.channels,
dtype='int16',
blocksize=self.blocksize,
latency=0.004,
periods=ALSA_PERIODS,
)
self._stream.start()
self._pcm.setchannels(requested_channels)
self._pcm.setformat(alsaaudio.PCM_FORMAT_S16_LE)
actual_rate = self._pcm.setrate(requested_rate)
self._pcm.setperiodsize(ALSA_PERIODSIZE)
logging.info("PyALSA: device=%s rate=%d ch=%d periodsize=%d (%.1fms) periods=%d mode=%s",
self._device, actual_rate, requested_channels, ALSA_PERIODSIZE,
(ALSA_PERIODSIZE / actual_rate) * 1000, ALSA_PERIODS, ALSA_MODE)
if actual_rate != requested_rate:
logging.warning("PyALSA: Sample rate mismatch! requested=%d actual=%d", requested_rate, actual_rate)
self._actual_channels = requested_channels
self._resampler = samplerate.Resampler('sinc_fastest', channels=requested_channels)
self._resampler_buffer = np.empty(0, dtype=np.float32)
self._bang_bang = 0
return audio_io.PcmFormat(
audio_io.PcmFormat.Endianness.LITTLE,
audio_io.PcmFormat.SampleType.INT16,
self._pcm_format.sample_rate,
1,
actual_rate,
requested_channels,
)
def _read(self, frame_size: int) -> bytes:
"""Read PCM samples from the stream."""
try:
avail = self._pcm.avail()
logging.debug("PyALSA: avail before read: %d", avail)
length, data = self._pcm.read_sw(frame_size + self._bang_bang)
avail = self._pcm.avail()
SETPOINT = 120
TOLERANCE = 40
if avail < SETPOINT - TOLERANCE:
self._bang_bang = -1
elif avail > SETPOINT + TOLERANCE:
self._bang_bang = 1
else:
self._bang_bang = 0
logging.debug("PyALSA: read length=%d, data length=%d, avail=%d, bang_bang=%d", length, len(data), avail, self._bang_bang)
#if self.counter % 50 == 0:
frame_size = frame_size + 1 # consume samples a little faster to avoid latency akkumulation
if length > 0:
if self._first_read:
expected_mono = self._periodsize * 2
expected_stereo = self._periodsize * 2 * 2
# self._hw_channels = 2 if len(data) == expected_stereo else 1
self._hw_channels = self._actual_channels
logging.info("PyALSA first read: bytes=%d detected_hw_channels=%d requested_channels=%d",
len(data), self._hw_channels, self._actual_channels)
self._first_read = False
if self._hw_channels == 2 and self._actual_channels == 1:
pcm_stereo = np.frombuffer(data, dtype=np.int16)
pcm_mono = pcm_stereo[::2]
data = pcm_mono.tobytes()
actual_samples = len(data) // (2 * self._actual_channels)
ratio = frame_size / actual_samples
pcm_f32 = np.frombuffer(data, dtype=np.int16).astype(np.float32) / 32768.0
if self._actual_channels > 1:
pcm_f32 = pcm_f32.reshape(-1, self._actual_channels)
resampled = self._resampler.process(pcm_f32, ratio, end_of_input=False)
if self._actual_channels > 1:
resampled = resampled.reshape(-1)
self._resampler_buffer = np.concatenate([self._resampler_buffer, resampled])
else:
logging.warning("PyALSA: No data read from ALSA")
self._resampler_buffer = np.concatenate([
self._resampler_buffer,
np.zeros(frame_size * self._actual_channels, dtype=np.float32),
])
except alsaaudio.ALSAAudioError as e:
logging.error("PyALSA: ALSA read error: %s", e)
self._resampler_buffer = np.concatenate([
self._resampler_buffer,
np.zeros(frame_size * self._actual_channels, dtype=np.float32),
])
except Exception as e:
logging.error("PyALSA: Unexpected error in _read: %s", e, exc_info=True)
self._resampler_buffer = np.concatenate([
self._resampler_buffer,
np.zeros(frame_size * self._actual_channels, dtype=np.float32),
])
pcm_buffer, overflowed = self._stream.read(frame_size)
if overflowed:
logging.warning("SoundDeviceAudioInput: overflowed")
needed = frame_size * self._actual_channels
if len(self._resampler_buffer) < needed:
pad = np.zeros(needed - len(self._resampler_buffer), dtype=np.float32)
self._resampler_buffer = np.concatenate([self._resampler_buffer, pad])
logging.debug("PyALSA: padded buffer with %d samples", needed - len(self._resampler_buffer))
n_available = self._stream.read_available
output = self._resampler_buffer[:needed]
self._resampler_buffer = self._resampler_buffer[needed:]
# adapt = n_available > 20
# if adapt:
# pcm_extra, overflowed = self._stream.read(3)
# logging.info('consuming extra samples, available was %d', n_available)
# if overflowed:
# logging.warning("SoundDeviceAudioInput: overflowed")
# out = bytes(pcm_buffer) + bytes(pcm_extra)
# else:
out = bytes(pcm_buffer)
logging.debug("PyALSA: resampler_buffer remaining=%d", len(self._resampler_buffer))
return np.clip(output * 32767.0, -32768, 32767).astype(np.int16).tobytes()
self.max_avail = max(self.max_avail, n_available)
#Diagnostics
#with open(self.logfile_name, "a", encoding="utf-8") as f:
# f.write(f"{n_available}, {adapt}, {round(self._runavg, 2)}, {overflowed}\n")
def _close(self) -> None:
if self._pcm:
self._pcm.close()
self._pcm = None
if self.counter % 500 == 0:
logging.info(
"read available=%d, max=%d, latency:%d",
n_available, self.max_avail, self._stream.latency
)
self.max_avail = 0
self.counter += 1
return out
audio_io.SoundDeviceAudioInput = ModSoundDeviceAudioInput
audio_io.SoundDeviceAudioInput = PyAlsaAudioInput
# modified from bumble
class ModWaveAudioInput(audio_io.ThreadedAudioInput):
@@ -538,7 +589,7 @@ async def init_broadcast(
def on_flow():
data_packet_queue = iso_queue.data_packet_queue
print(
logging.info(
f'\rPACKETS: pending={data_packet_queue.pending}, '
f'queued={data_packet_queue.queued}, '
f'completed={data_packet_queue.completed}',
@@ -828,13 +879,6 @@ class Streamer():
big['encoder'] = encoders[0]
big['precoded'] = False
# Pre-compute software gain multiplier from dB config (0 dB = 1.0 = no change)
gain_db = getattr(big_config[i], 'input_gain_db', 0.0)
gain_db = max(0.0, min(20.0, float(gain_db)))
big['_gain_linear'] = 10.0 ** (gain_db / 20.0) if gain_db > 0 else 0.0
if big['_gain_linear'] > 0.0:
logging.info("Software gain for BIG %d: +%.1f dB (linear %.3f)", i, gain_db, big['_gain_linear'])
logging.info("Streaming audio...")
bigs = self.bigs
self.is_streaming = True
@@ -885,14 +929,6 @@ class Streamer():
stream_finished[i] = True
continue
# Apply software gain boost if configured (> 0 dB)
gain_lin = big.get('_gain_linear', 0.0)
if gain_lin > 0.0:
pcm_arr = np.frombuffer(pcm_frame, dtype=np.int16).astype(np.float32)
pcm_arr *= gain_lin
np.clip(pcm_arr, -32768, 32767, out=pcm_arr)
pcm_frame = pcm_arr.astype(np.int16).tobytes()
# Compute RMS audio level (normalized 0.0-1.0) for level monitoring
pcm_samples = np.frombuffer(pcm_frame, dtype=np.int16).astype(np.float32)
rms = np.sqrt(np.mean(pcm_samples ** 2)) / 32768.0 if len(pcm_samples) > 0 else 0.0

View File

@@ -143,6 +143,10 @@ async def main():
level=os.environ.get('LOG_LEVEL', logging.DEBUG),
format='%(module)s.py:%(lineno)d %(levelname)s: %(message)s'
)
# Enable debug logging for bumble
# logging.getLogger('bumble').setLevel(logging.DEBUG)
os.chdir(os.path.dirname(__file__))
global_conf = auracast_config.AuracastGlobalConfig(

View File

@@ -198,35 +198,64 @@ else:
start_stream, stop_stream = render_stream_controls(is_streaming, "Start Auracast", "Stop Auracast", running_mode, secondary_is_streaming)
# Analog gain control (only for Analog mode, placed below start button)
analog_gain_value = 50 # default (ALSA 10-60 range)
software_boost_db = 0 # default
analog_gain_db_left = 0 # default (dB)
analog_gain_db_right = 0 # default (dB)
if audio_mode == "Analog":
saved_analog_gain = saved_settings.get('analog_gain', 50)
# Convert persisted ALSA value (10-60) to display value (0-100)
saved_display = int(round((saved_analog_gain - 10) * 100 / 50))
saved_display = max(0, min(100, saved_display))
analog_gain_display = st.slider(
"Analog Input Gain",
min_value=0,
max_value=100,
value=saved_display,
step=5,
disabled=is_streaming,
format="%d%%",
help="ADC gain level for both analog inputs. Default is 80%."
)
# Map display value (0-100) back to ALSA range (10-60)
analog_gain_value = int(round(10 + analog_gain_display * 50 / 100))
saved_boost = saved_settings.get('software_boost_db', 0)
software_boost_db = st.slider(
"Boost",
min_value=0,
max_value=20,
value=min(int(saved_boost), 20),
step=1,
disabled=is_streaming,
help="Digital gain boost applied before encoding (0-20 dB). Use this when the line-level signal is too quiet even at max ADC gain. Higher values may cause clipping on loud signals."
if '_analog_gain_db_left' not in st.session_state:
st.session_state['_analog_gain_db_left'] = max(-12, min(18, int(saved_settings.get('analog_gain_db_left', 0))))
if '_analog_gain_db_right' not in st.session_state:
st.session_state['_analog_gain_db_right'] = max(-12, min(18, int(saved_settings.get('analog_gain_db_right', 0))))
if '_gain_link_channels' not in st.session_state:
st.session_state['_gain_link_channels'] = True
link_channels = st.checkbox(
"Link audio channel gain",
key='_gain_link_channels',
help="When enabled, Ch 2 mirrors Ch 1."
)
_gain_col1, _gain_col2 = st.columns(2)
with _gain_col1:
analog_gain_db_left = st.slider(
"Ch 1 Input Gain",
min_value=-12,
max_value=18,
key='_analog_gain_db_left',
step=1,
format="%d dB",
help="ADC gain for channel 1 (-12 to 18 dB). Default is 0 dB."
)
with _gain_col2:
if link_channels:
st.session_state['_analog_gain_db_right'] = analog_gain_db_left
elif st.session_state.get('_prev_gain_link_channels', True):
# Transition: just unlinked — seed Ch 2 from Ch 1 so it doesn't jump to min
st.session_state['_analog_gain_db_right'] = analog_gain_db_left
st.session_state['_prev_gain_link_channels'] = link_channels
analog_gain_db_right = st.slider(
"Ch 2 Input Gain",
min_value=-12,
max_value=18,
key='_analog_gain_db_right',
step=1,
format="%d dB",
disabled=link_channels,
help="Uncheck 'Link audio channel gain' to adjust Ch 2 independently." if link_channels else "ADC gain for channel 2 (-12 to 18 dB). Default is 0 dB."
)
# Apply gain live while streaming whenever either slider value changes
if is_streaming:
prev_left = st.session_state.get('_prev_analog_gain_db_left')
prev_right = st.session_state.get('_prev_analog_gain_db_right')
if prev_left != analog_gain_db_left or prev_right != analog_gain_db_right:
try:
requests.post(
f"{BACKEND_URL}/adc_gain",
json={"gain_db_left": analog_gain_db_left, "gain_db_right": analog_gain_db_right},
timeout=1,
)
except Exception:
pass
st.session_state['_prev_analog_gain_db_left'] = analog_gain_db_left
st.session_state['_prev_analog_gain_db_right'] = analog_gain_db_right
# Audio level monitor (checkbox, not persisted across reloads)
show_level_monitor = st.checkbox("Audio level monitor", value=False, disabled=not is_streaming,
@@ -695,8 +724,8 @@ else:
'immediate_rendering': immediate_rendering2,
'presentation_delay_ms': presentation_delay_ms2,
'qos_preset': qos_preset2,
'analog_gain': analog_gain_value,
'software_boost_db': software_boost_db,
'analog_gain_db_left': analog_gain_db_left,
'analog_gain_db_right': analog_gain_db_right,
}
radio1_cfg = {
@@ -712,8 +741,8 @@ else:
'presentation_delay_ms': presentation_delay_ms1,
'qos_preset': qos_preset1,
'stereo_mode': stereo_enabled,
'analog_gain': analog_gain_value,
'software_boost_db': software_boost_db,
'analog_gain_db_left': analog_gain_db_left,
'analog_gain_db_right': analog_gain_db_right,
}
if audio_mode == "Network - Dante":
@@ -1606,7 +1635,8 @@ if start_stream:
immediate_rendering=bool(cfg['immediate_rendering']),
presentation_delay_us=int(cfg['presentation_delay_ms'] * 1000),
qos_config=QOS_PRESET_MAP[cfg['qos_preset']],
analog_gain=cfg.get('analog_gain', 50),
analog_gain_db_left=cfg.get('analog_gain_db_left', 0.0),
analog_gain_db_right=cfg.get('analog_gain_db_right', 0.0),
bigs=[
auracast_config.AuracastBigConfig(
code=(cfg['stream_passwort'].strip() or None),
@@ -1619,7 +1649,6 @@ if start_stream:
sampling_frequency=q['rate'],
octets_per_frame=q['octets'],
num_bis=channels, # 1=mono, 2=stereo - this determines the behavior
input_gain_db=float(cfg.get('software_boost_db', 0)),
)
],
)

View File

@@ -3,6 +3,7 @@ TODO: in the future the multicaster objects should run in their own threads or e
"""
import os
import re
import logging as log
import json
from datetime import datetime
@@ -73,6 +74,10 @@ def _led_off():
except Exception:
pass
# Configure bumble debug logging
# log.getLogger('bumble').setLevel(log.DEBUG)
# make sure pipewire sets latency
# Primary and secondary persisted settings files
STREAM_SETTINGS_FILE1 = os.path.join(os.path.dirname(__file__), 'stream_settings.json')
@@ -289,16 +294,18 @@ async def _init_i2c_on_startup() -> None:
log.warning("Exception running i2cget (%s): %s", " ".join(read_cmd), e, exc_info=True)
async def _set_adc_level(gain_percent: int = 50) -> None:
"""Set ADC mixer level.
async def _set_adc_level(gain_db_left: float = 0.0, gain_db_right: float = 0.0) -> None:
"""Set ADC mixer gain in dB for left and right channels independently.
Runs: amixer -c 1 set 'ADC' x%
Runs: amixer -c 2 sset ADC {gain_db_left}dB,{gain_db_right}dB
Args:
gain_percent: Gain level 10-60, default 50 (above 60% causes clipping/silence)
gain_db_left: Left channel gain in dB (-12 to 18), default 0
gain_db_right: Right channel gain in dB (-12 to 18), default 0
"""
gain_percent = max(10, min(60, gain_percent))
cmd = ["amixer", "-c", "1", "set", "ADC", f"{gain_percent}%"]
gain_db_left = max(-12.0, min(18.0, gain_db_left))
gain_db_right = max(-12.0, min(18.0, gain_db_right))
cmd = ["amixer", "-c", "2", "sset", "ADC", "--", f"{int(gain_db_left)}dB,{int(gain_db_right)}dB"]
try:
proc = await asyncio.create_subprocess_exec(
*cmd,
@@ -307,15 +314,46 @@ async def _set_adc_level(gain_percent: int = 50) -> None:
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
log.warning(
log.error(
"amixer ADC level command failed (rc=%s): %s",
proc.returncode,
(stderr or b"" ).decode(errors="ignore").strip(),
)
else:
log.info("amixer ADC level set successfully: %s", (stdout or b"" ).decode(errors="ignore").strip())
read_proc = await asyncio.create_subprocess_exec(
"amixer", "-c", "2", "sget", "ADC",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
read_stdout, read_stderr = await read_proc.communicate()
if read_proc.returncode != 0:
log.error(
"amixer ADC sget failed (rc=%s): %s",
read_proc.returncode,
(read_stderr or b"").decode(errors="ignore").strip(),
)
else:
sget_output = (read_stdout or b"").decode(errors="ignore")
actual = {}
for line in sget_output.splitlines():
for ch_key, ch_name in (("left", "Front Left"), ("right", "Front Right")):
if ch_name in line:
m = re.search(r'\[(-?\d+(?:\.\d+)?)dB\]', line)
if m:
actual[ch_key] = round(float(m.group(1)))
expected_left = int(gain_db_left)
expected_right = int(gain_db_right)
if actual.get("left") != expected_left or actual.get("right") != expected_right:
mismatch = (
f"ADC level mismatch after set: expected L={expected_left}dB R={expected_right}dB, "
f"got L={actual.get('left')}dB R={actual.get('right')}dB"
)
log.error(mismatch)
else:
log.info("ADC level set successfully: L=%sdB R=%sdB", expected_left, expected_right)
except Exception as e:
log.warning("Exception running amixer ADC level command: %s", e, exc_info=True)
log.error("Exception running amixer ADC level command: %s", e, exc_info=True)
async def _stop_all() -> bool:
@@ -395,8 +433,9 @@ async def init_radio(transport: str, conf: auracast_config.AuracastConfigGroup,
if input_device_name in ('ch1', 'ch2'):
audio_mode_persist = 'Analog'
# Set ADC gain level for analog mode
analog_gain = getattr(conf, 'analog_gain', 50)
await _set_adc_level(analog_gain)
analog_gain_db_left = getattr(conf, 'analog_gain_db_left', 0.0)
analog_gain_db_right = getattr(conf, 'analog_gain_db_right', 0.0)
await _set_adc_level(analog_gain_db_left, analog_gain_db_right)
elif input_device_name in dante_channels:
audio_mode_persist = 'Network - Dante'
else:
@@ -439,18 +478,15 @@ async def init_radio(transport: str, conf: auracast_config.AuracastConfigGroup,
if is_stereo and sel == 'ch1':
# Stereo mode: use ALSA directly to capture both channels from hardware
# ch1=left (channel 0), ch2=right (channel 1)
big.audio_source = 'alsa:hw:CARD=i2s,DEV=0'
big.audio_source = 'device:hw:2'
big.input_format = f"int16le,{hardware_capture_rate},2"
log.info("Configured analog stereo input: using ALSA hw:CARD=i2s,DEV=0 with ch1=left, ch2=right")
elif is_stereo and sel == 'ch2':
# Skip ch2 in stereo mode as it's already captured as part of stereo pair
continue
else:
# Mono mode: individual channel capture
device_index = resolve_input_device_index(sel)
if device_index is None:
raise HTTPException(status_code=400, detail=f"Audio device '{sel}' not found.")
big.audio_source = f'device:{device_index}'
# Mono mode: use dsnoop virtual device directly (ch1=left, ch2=right)
big.audio_source = f'device:{sel}'
big.input_format = f"int16le,{hardware_capture_rate},1"
continue
@@ -554,8 +590,8 @@ async def init_radio(transport: str, conf: auracast_config.AuracastConfigGroup,
'immediate_rendering': getattr(conf, 'immediate_rendering', False),
'assisted_listening_stream': getattr(conf, 'assisted_listening_stream', False),
'analog_stereo_mode': getattr(conf.bigs[0], 'analog_stereo_mode', False) if conf.bigs else False,
'analog_gain': getattr(conf, 'analog_gain', 50),
'software_boost_db': getattr(conf.bigs[0], 'input_gain_db', 0.0) if conf.bigs else 0.0,
'analog_gain_db_left': getattr(conf, 'analog_gain_db_left', 0.0),
'analog_gain_db_right': getattr(conf, 'analog_gain_db_right', 0.0),
'stream_password': (conf.bigs[0].code if conf.bigs and getattr(conf.bigs[0], 'code', None) else None),
'big_ids': [getattr(big, 'id', DEFAULT_BIG_ID) for big in conf.bigs],
'big_random_addresses': [getattr(big, 'random_address', DEFAULT_RANDOM_ADDRESS) for big in conf.bigs],
@@ -629,6 +665,28 @@ async def stop_audio():
log.error("Exception in /stop_audio: %s", traceback.format_exc())
raise HTTPException(status_code=500, detail=str(e))
@app.post("/adc_gain")
async def set_adc_gain(payload: dict):
"""Set ADC gain in dB for left and right channels without restarting the stream.
Body: {"gain_db_left": float, "gain_db_right": float}
"""
try:
gain_db_left = float(payload.get("gain_db_left", 0.0))
gain_db_right = float(payload.get("gain_db_right", 0.0))
await _set_adc_level(gain_db_left, gain_db_right)
# Persist the new values so they survive a restart
for load_fn, save_fn in [(load_stream_settings, save_stream_settings), (load_stream_settings2, save_stream_settings2)]:
s = load_fn() or {}
if s:
s['analog_gain_db_left'] = gain_db_left
s['analog_gain_db_right'] = gain_db_right
save_fn(s)
return {"status": "ok", "gain_db_left": gain_db_left, "gain_db_right": gain_db_right}
except Exception as e:
log.error("Exception in /adc_gain: %s", traceback.format_exc())
raise HTTPException(status_code=500, detail=str(e))
@app.post("/stream_lc3")
async def send_audio(audio_data: dict[str, str]):
"""Sends a block of pre-coded LC3 audio via the worker."""
@@ -825,7 +883,6 @@ async def _autostart_from_settings():
iso_que_len=1,
sampling_frequency=rate,
octets_per_frame=octets,
input_gain_db=float(settings.get('software_boost_db', 0)),
)
]
conf = auracast_config.AuracastConfigGroup(
@@ -835,7 +892,8 @@ async def _autostart_from_settings():
immediate_rendering=immediate_rendering,
assisted_listening_stream=assisted_listening_stream,
presentation_delay_us=pres_delay if pres_delay is not None else 40000,
analog_gain=settings.get('analog_gain', 50),
analog_gain_db_left=settings.get('analog_gain_db_left', 0.0),
analog_gain_db_right=settings.get('analog_gain_db_right', 0.0),
bigs=bigs,
)
# Set num_bis for stereo mode if needed
@@ -980,7 +1038,6 @@ async def _autostart_from_settings():
iso_que_len=1,
sampling_frequency=rate,
octets_per_frame=octets,
input_gain_db=float(settings.get('software_boost_db', 0)),
)
]
conf = auracast_config.AuracastConfigGroup(
@@ -990,7 +1047,8 @@ async def _autostart_from_settings():
immediate_rendering=immediate_rendering,
assisted_listening_stream=assisted_listening_stream,
presentation_delay_us=pres_delay if pres_delay is not None else 40000,
analog_gain=settings.get('analog_gain', 50),
analog_gain_db_left=settings.get('analog_gain_db_left', 0.0),
analog_gain_db_right=settings.get('analog_gain_db_right', 0.0),
bigs=bigs,
)
conf.qos_config = QOS_PRESET_MAP.get(saved_qos_preset, QOS_PRESET_MAP["Fast"])
@@ -1015,7 +1073,7 @@ async def _startup_autostart_event():
_led_off()
# Run install_asoundconf.sh script
script_path = os.path.join(os.path.dirname(__file__), '..', 'misc', 'install_asoundconf.sh')
script_path = os.path.join(os.path.dirname(__file__), '..', '..', 'misc', 'install_asoundconf.sh')
try:
log.info("[STARTUP] Running install_asoundconf.sh script")
result = subprocess.run(['bash', script_path], capture_output=True, text=True, check=True)
@@ -1029,8 +1087,8 @@ async def _startup_autostart_event():
_load_led_settings()
_init_settings_cache_from_disk()
await _init_i2c_on_startup()
# Ensure ADC mixer level is set at startup (default 50%)
await _set_adc_level(50)
# Ensure ADC mixer level is set at startup (default 0 dB)
await _set_adc_level(0.0, 0.0)
refresh_pw_cache()
log.info("[STARTUP] Scheduling autostart task")
asyncio.create_task(_autostart_from_settings())
@@ -1321,26 +1379,12 @@ async def system_update():
log.error("git checkout failed: %s", stderr.decode())
raise HTTPException(status_code=500, detail=f"git checkout failed: {stderr.decode()}")
# 2. Run poetry install (use full path as poetry is in user's ~/.local/bin)
poetry_path = os.path.expanduser("~/.local/bin/poetry")
proc = await asyncio.create_subprocess_exec(
poetry_path, "install",
cwd=project_root,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
log.error("poetry install failed: %s", stderr.decode())
raise HTTPException(status_code=500, detail=f"poetry install failed: {stderr.decode()}")
# 3. Restart services via the update script
update_script = os.path.join(project_root, 'src', 'service', 'update_and_run_server_and_frontend.sh')
proc = await asyncio.create_subprocess_exec(
# 2. Hand off remaining work to the (now-updated) system_update.sh script
update_script = os.path.join(os.path.dirname(__file__), 'system_update.sh')
log.info("Handing off to system_update.sh...")
await asyncio.create_subprocess_exec(
"bash", update_script,
cwd=project_root,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
# Don't wait for completion as we'll be restarted
await asyncio.sleep(0.5)

View File

@@ -0,0 +1,90 @@
#!/bin/bash
# system_update.sh - Runs after git checkout in the Python system_update endpoint.
# Called with the current working directory = project root.
# All output is also written to /tmp/system_update.log for debugging.
exec > >(tee -a /tmp/system_update.log) 2>&1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
POETRY="$HOME/.local/bin/poetry"
OPENOCD_SRC="$HOME/sw_openocd"
OPENOCD_REPO="ssh://git@gitea.summitwave.work:222/auracaster/sw_openocd.git"
OPENOCD_BRANCH="change-8818"
OPENOCD_MARKER="$OPENOCD_SRC/.last_built_commit"
OPENOCD_DIR="$PROJECT_ROOT/src/openocd"
echo "[system_update] Starting post-checkout update. project_root=$PROJECT_ROOT"
# 1. poetry install
echo "[system_update] Running poetry install..."
(cd "$PROJECT_ROOT" && "$POETRY" install)
if [ $? -ne 0 ]; then
echo "[system_update] ERROR: poetry install failed"
exit 1
fi
# 2. Clone/update and build sw_openocd if needed
if [ ! -d "$OPENOCD_SRC" ]; then
echo "[system_update] Installing sw_openocd build dependencies..."
sudo apt install -y git build-essential libtool autoconf texinfo \
libusb-1.0-0-dev libftdi1-dev libhidapi-dev pkg-config || \
echo "[system_update] WARNING: apt install deps had errors, continuing"
sudo apt-get install -y pkg-config libjim-dev || \
echo "[system_update] WARNING: apt-get install libjim-dev had errors, continuing"
echo "[system_update] Cloning sw_openocd branch $OPENOCD_BRANCH..."
git clone --branch "$OPENOCD_BRANCH" --single-branch "$OPENOCD_REPO" "$OPENOCD_SRC"
if [ $? -ne 0 ]; then
echo "[system_update] ERROR: git clone sw_openocd failed"
exit 1
fi
else
echo "[system_update] Updating sw_openocd..."
git -C "$OPENOCD_SRC" fetch origin "$OPENOCD_BRANCH"
git -C "$OPENOCD_SRC" checkout "$OPENOCD_BRANCH"
git -C "$OPENOCD_SRC" pull
fi
OPENOCD_COMMIT=$(git -C "$OPENOCD_SRC" rev-parse HEAD)
LAST_BUILT=""
[ -f "$OPENOCD_MARKER" ] && LAST_BUILT=$(cat "$OPENOCD_MARKER")
if [ "$OPENOCD_COMMIT" != "$LAST_BUILT" ]; then
echo "[system_update] Building sw_openocd (commit $OPENOCD_COMMIT)..."
(cd "$OPENOCD_SRC" && ./bootstrap)
if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd bootstrap failed"; exit 1; fi
(cd "$OPENOCD_SRC" && ./configure --enable-bcm2835gpio --enable-sysfsgpio)
if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd configure failed"; exit 1; fi
(cd "$OPENOCD_SRC" && make)
if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd make failed"; exit 1; fi
(cd "$OPENOCD_SRC" && sudo make install)
if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd make install failed"; exit 1; fi
echo "$OPENOCD_COMMIT" > "$OPENOCD_MARKER"
echo "[system_update] sw_openocd built and installed (commit $OPENOCD_COMMIT)"
else
echo "[system_update] sw_openocd up to date (commit $OPENOCD_COMMIT), skipping build"
fi
# 3. Flash firmware to both SWD interfaces
FLASH_SCRIPT="$OPENOCD_DIR/flash.sh"
HEX_FILE="$OPENOCD_DIR/merged.hex"
for IFACE in swd0 swd1; do
echo "[system_update] Flashing $IFACE..."
(cd "$OPENOCD_DIR" && bash "$FLASH_SCRIPT" -i "$IFACE" -f "$HEX_FILE")
if [ $? -ne 0 ]; then
echo "[system_update] ERROR: flash $IFACE failed"
exit 1
fi
echo "[system_update] Flash $IFACE complete"
done
# 4. Restart services (this will kill this process too)
echo "[system_update] Restarting services..."
bash "$PROJECT_ROOT/src/service/update_and_run_server_and_frontend.sh"

View File

@@ -6,8 +6,8 @@ pcm.ch1 {
channels 2
rate 48000
format S16_LE
period_size 120
buffer_size 240
period_size 240
buffer_size 960
}
bindings.0 0
}
@@ -21,8 +21,8 @@ pcm.ch2 {
channels 2
rate 48000
format S16_LE
period_size 120
buffer_size 240
period_size 240
buffer_size 960
}
bindings.0 1
}

View File

@@ -1 +1,2 @@
sudo cp src/misc/asound.conf /etc/asound.conf
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
sudo cp "$SCRIPT_DIR/asound.conf" /etc/asound.conf

40
src/openocd/flash.sh Normal file
View File

@@ -0,0 +1,40 @@
#!/bin/bash
set -e
INTERFACE="swd0"
HEX_FILE=""
usage() {
echo "Usage: $0 -f <hex_file> [-i swd0|swd1]"
exit 1
}
while getopts "f:i:h" opt; do
case "$opt" in
f) HEX_FILE="$OPTARG" ;;
i)
if [[ "$OPTARG" == "swd0" || "$OPTARG" == "swd1" ]]; then
INTERFACE="$OPTARG"
else
usage
fi
;;
h) usage ;;
*) usage ;;
esac
done
[[ -n "$HEX_FILE" ]] || usage
[[ -f "$HEX_FILE" ]] || { echo "HEX file not found: $HEX_FILE"; exit 1; }
sudo openocd \
-f ./raspberrypi-${INTERFACE}.cfg \
-c "init" \
-c "reset init" \
-c "flash banks" \
-c "flash write_image $HEX_FILE" \
-c "verify_image $HEX_FILE" \
-c "reset run" \
-c "shutdown"
echo "Flashing complete."

13111
src/openocd/merged.hex Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -5,4 +5,9 @@ adapter gpio swdio 26
#adapter gpio trst 26
#reset_config trst_only
source [find target/nordic/nrf54l.cfg]
flash bank $_CHIPNAME.flash nrf54 0x00000000 0 0 0 $_TARGETNAME
adapter speed 1000

View File

@@ -5,4 +5,9 @@ adapter gpio swdio 24
#adapter gpio trst 27
#reset_config trst_only
source [find target/nordic/nrf54l.cfg]
flash bank $_CHIPNAME.flash nrf54 0x00000000 0 0 0 $_TARGETNAME
adapter speed 1000

0
src/service/update_and_run_server_and_frontend.sh Normal file → Executable file
View File