Stabilize monitor retune across waterfall click restarts

This commit is contained in:
Smittix
2026-02-23 23:39:50 +00:00
parent 8a0c39901f
commit 3b2a2ea645
3 changed files with 72 additions and 30 deletions
+28 -18
View File
@@ -1573,29 +1573,39 @@ def stream_audio() -> Response:
if not audio_running:
return Response(b'', mimetype='audio/wav', status=204)
def generate_shared():
global audio_running, audio_source
try:
from routes.waterfall_websocket import (
get_shared_capture_status,
def generate_shared():
global audio_running, audio_source
try:
from routes.waterfall_websocket import (
get_shared_capture_status,
read_shared_monitor_audio_chunk,
)
except Exception:
return
# Browser expects an immediate WAV header.
yield _wav_header(sample_rate=48000)
while audio_running and audio_source == 'waterfall':
chunk = read_shared_monitor_audio_chunk(timeout=1.0)
if chunk:
yield chunk
continue
shared = get_shared_capture_status()
if not shared.get('running') or not shared.get('monitor_enabled'):
audio_running = False
audio_source = 'process'
break
# Browser expects an immediate WAV header.
yield _wav_header(sample_rate=48000)
inactive_since: float | None = None
while audio_running and audio_source == 'waterfall':
chunk = read_shared_monitor_audio_chunk(timeout=1.0)
if chunk:
inactive_since = None
yield chunk
continue
shared = get_shared_capture_status()
if shared.get('running') and shared.get('monitor_enabled'):
inactive_since = None
continue
if inactive_since is None:
inactive_since = time.monotonic()
continue
if (time.monotonic() - inactive_since) < 4.0:
continue
if not shared.get('running') or not shared.get('monitor_enabled'):
audio_running = False
audio_source = 'process'
break
return Response(
generate_shared(),
+18 -5
View File
@@ -413,6 +413,10 @@ def init_waterfall_websocket(app: Flask):
cmd = data.get('cmd')
if cmd == 'start':
shared_before = get_shared_capture_status()
keep_monitor_enabled = bool(shared_before.get('monitor_enabled'))
keep_monitor_modulation = str(shared_before.get('monitor_modulation', 'wfm'))
keep_monitor_squelch = int(shared_before.get('monitor_squelch', 0) or 0)
# Stop any existing capture
was_restarting = iq_process is not None
stop_event.set()
@@ -441,6 +445,12 @@ def init_waterfall_websocket(app: Flask):
# Parse config
try:
center_freq_mhz = _parse_center_freq_mhz(data)
requested_vfo_mhz = float(
data.get(
'vfo_freq_mhz',
data.get('frequency_mhz', center_freq_mhz),
)
)
span_mhz = _parse_span_mhz(data)
gain_raw = data.get('gain')
if gain_raw is None or str(gain_raw).lower() == 'auto':
@@ -491,6 +501,9 @@ def init_waterfall_websocket(app: Flask):
effective_span_mhz = sample_rate / 1e6
start_freq = center_freq_mhz - effective_span_mhz / 2
end_freq = center_freq_mhz + effective_span_mhz / 2
target_vfo_mhz = requested_vfo_mhz
if not (start_freq <= target_vfo_mhz <= end_freq):
target_vfo_mhz = center_freq_mhz
# Claim the device (retry when restarting to allow
# the kernel time to release the USB handle).
@@ -591,10 +604,10 @@ def init_waterfall_websocket(app: Flask):
sample_rate=sample_rate,
)
_set_shared_monitor(
enabled=False,
frequency_mhz=center_freq_mhz,
modulation='wfm',
squelch=0,
enabled=keep_monitor_enabled,
frequency_mhz=target_vfo_mhz,
modulation=keep_monitor_modulation,
squelch=keep_monitor_squelch,
)
# Send started confirmation
@@ -608,7 +621,7 @@ def init_waterfall_websocket(app: Flask):
'effective_span_mhz': effective_span_mhz,
'db_min': db_min,
'db_max': db_max,
'vfo_freq_mhz': center_freq_mhz,
'vfo_freq_mhz': target_vfo_mhz,
}))
# Start reader thread — puts frames on queue, never calls ws.send()