GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ats-mini-controller/radio_bridge.py at main · jsawyerdev/ats-mini-controller

Expand file tree

/

radio_bridge.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
689 lines (598 loc) · 28.4 KB
/

radio_bridge.py

Copy path

File metadata and controls

689 lines (598 loc) · 28.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
"""RTL-SDR USB dongle bridge: tunes an RTL2832U-based SDR by directly setting
its center frequency and demodulates AM/FM/CW in software from the raw IQ
stream. There is no firmware, no serial protocol, and no fixed band list to
navigate - a retune is a single register write, unlike the ATS Mini's
multi-second band-search this replaced.
FM broadcast (88-108 MHz) is within the R820T tuner's normal PLL range
(~24 MHz-1.7 GHz). AM/mediumwave (530-1700 kHz) and shortwave (1.6-30 MHz)
are both below that floor, so those bands are received via the RTL2832U's
direct-sampling ADC input instead of the normal tuner path. Direct sampling
is a standard RTL2832U capability (confirmed working on this dongle: it
enables and tunes without error), but whether this generic board's external
antenna connector is electrically routed to the selected ADC input with
adequate sensitivity remains unverified.
Author: James Sawyer / JSLabs - https://www.jamessawyer.co.uk/ | https://labs.jamessawyer.co.uk/
"""
from __future__ import annotations
import asyncio
import io
import logging
import threading
import time
from collections import deque
from dataclasses import dataclass
import numpy as np
from rtlsdr import RtlSdr
from scipy.signal import butter, sosfilt, sosfilt_zi, welch
logger = logging.getLogger(__name__)
IQ_SAMPLE_RATE = 960_000 # Hz; well within the RTL2832U's reliable high-rate range
AUDIO_RATE = 48_000 # Hz; matches cw_decoder/cw_monitor's expected samplerate
_DECIMATION = IQ_SAMPLE_RATE // AUDIO_RATE # 20
CHUNK_IQ_SAMPLES = 32_768 # ~34 ms per read
# The R820T's PLL can't lock below roughly this; anything under it needs
# direct sampling instead of the normal tuner path.
DIRECT_SAMPLING_THRESHOLD_HZ = 28_800_000
DIRECT_SAMPLING_MODE = 2 # Q-branch; the commonly-wired input on most boards
# One of the R820T's supported gain steps (confirmed via `rtl_test -t` on
# this dongle: 0.0 ... 49.6 dB in 29 steps). Fixed rather than "auto" - see
# the comment in RtlSdrBridge.__init__.
DEFAULT_GAIN_DB = 40.2
SUPPORTED_GAIN_DB = (19.7, 28.0, 36.4, 40.2, 49.6)
CW_TONE_LOWPASS_HZ = (
2_000 # keeps cw_decoder's 300-1200Hz search band, rejects the rest
)
AM_AUDIO_BW_HZ = 5_000
FM_AUDIO_BW_HZ = 15_000
# Precomputed once rather than rebuilt every chunk (butter() isn't free),
# and shared with the persistent-zi handling in _decimate_lowpass below.
_AM_SOS = butter(4, AM_AUDIO_BW_HZ, btype="low", fs=IQ_SAMPLE_RATE, output="sos")
_FM_SOS = butter(4, FM_AUDIO_BW_HZ, btype="low", fs=IQ_SAMPLE_RATE, output="sos")
_CW_SOS = butter(4, CW_TONE_LOWPASS_HZ, btype="low", fs=IQ_SAMPLE_RATE, output="sos")
# RF channel bandwidths used to compare the tuned channel with nearby noise.
FM_CHANNEL_BW_HZ = 200_000 # broadcast FM: +/-75kHz deviation plus guard
AM_CHANNEL_BW_HZ = 10_000 # AM/SW broadcast: roughly double the audio bandwidth
CW_CHANNEL_BW_HZ = 4_000 # narrow - just enough to contain the beat tone
_AUDIO_HISTORY_S = 60.0 # ring buffer depth for capture_audio()
def find_rtlsdr_device() -> bool:
"""True if an RTL-SDR dongle is currently plugged in and openable."""
try:
sdr = RtlSdr()
except Exception: # noqa: BLE001 -- USB backends raise several unrelated exception types
return False
sdr.close()
return True
@dataclass(frozen=True)
class RadioStatus:
frequency_hz: int
band: str # SW | AM | FM - coarse tag, also selects the demod mode
mode: str # AM | FM | CW - demod actually in use
signal_db: int # in-channel power above the nearby RF noise floor
noise_dbfs: int # nearby RF noise floor, relative dBFS (diagnostic only)
audio_dbfs: int # demodulated audio RMS; -120 means digital silence
volume: int # software playback gain, 0-100
gain_db: float # tuner gain; bypassed while direct sampling is active
ppm: int # oscillator correction applied by librtlsdr
direct_sampling: bool
seq: int
@dataclass
class _AmDemodState:
filter_zi: np.ndarray | None = None
level: float = 0.02
@dataclass
class _IqRecording:
output: io.BufferedWriter
remaining_samples: int
complete: threading.Event
written_samples: int = 0
error: str | None = None
class _AudioBus:
"""Ring buffer of demodulated audio, shared by every consumer (the CW
decoder's blocking captures, the signal/status readout, and any number of
live websocket listeners). Unlike the old hardware passthrough, none of
these need exclusive access - it's all just reads of the same stream."""
def __init__(
self, rate: int = AUDIO_RATE, history_s: float = _AUDIO_HISTORY_S
) -> None:
self._rate = rate
self._max_samples = int(history_s * rate)
self._chunks: deque[np.ndarray] = deque()
self._buffered_samples = 0
self._total_produced = 0
self._cond = threading.Condition()
def publish(self, chunk: np.ndarray) -> None:
with self._cond:
self._chunks.append(chunk)
self._buffered_samples += len(chunk)
self._total_produced += len(chunk)
while self._buffered_samples - len(self._chunks[0]) > self._max_samples:
self._buffered_samples -= len(self._chunks.popleft())
self._cond.notify_all()
def capture(self, seconds: float) -> np.ndarray:
"""Block until `seconds` of audio produced after this call has
accumulated, then return it - a drop-in replacement for the old
sd.rec()-based blocking capture."""
n_needed = int(seconds * self._rate)
deadline = time.monotonic() + seconds + 5.0 # generous safety margin
with self._cond:
start_total = self._total_produced
while self._total_produced - start_total < n_needed:
remaining = deadline - time.monotonic()
if remaining <= 0 or not self._cond.wait(timeout=remaining):
break
produced = self._total_produced - start_total
if produced <= 0:
return np.zeros(0, dtype=np.float32)
audio = np.concatenate(list(self._chunks))
n = min(produced, n_needed, len(audio))
return audio[-n:]
def _trimmed_mean(values: np.ndarray, keep_fraction: float = 0.9) -> float:
"""Mean without the strongest bins, which may contain adjacent stations."""
if values.size == 0:
return 1e-12
keep = max(1, int(values.size * keep_fraction))
return float(np.mean(np.partition(values, keep - 1)[:keep]))
def _estimate_signal(iq: np.ndarray, channel_bw_hz: float) -> tuple[int, int]:
"""Return in-channel power above local RF noise and the noise floor.
Absolute IQ power rises with gain, antenna noise, and interference, so it
cannot establish that a station is present. This periodogram compares the
tuned channel with nearby spectrum captured at the same instant. Removing
the IQ mean and a narrow centre notch prevents the RTL2832U DC spike from
being mistaken for a carrier.
"""
if len(iq) < 32:
return 0, -120
centered = iq - np.mean(iq)
window = np.hanning(len(centered))
spectrum = np.fft.fftshift(np.fft.fft(centered * window))
power = np.abs(spectrum) ** 2 / max(float(np.sum(window**2)), 1.0)
frequencies = np.fft.fftshift(np.fft.fftfreq(len(centered), 1 / IQ_SAMPLE_RATE))
bin_hz = IQ_SAMPLE_RATE / len(centered)
dc_notch_hz = 3 * bin_hz
signal_mask = (np.abs(frequencies) <= channel_bw_hz / 2) & (
np.abs(frequencies) >= dc_notch_hz
)
noise_mask = (np.abs(frequencies) >= channel_bw_hz * 0.75) & (
np.abs(frequencies) <= IQ_SAMPLE_RATE * 0.45
)
signal_power = _trimmed_mean(power[signal_mask], keep_fraction=0.98)
noise_power = _trimmed_mean(power[noise_mask])
channel_excess_db = 10 * np.log10(signal_power / max(noise_power, 1e-12))
# AM/CW carriers occupy only a handful of bins and would be diluted by a
# channel-wide mean. Compare equal high percentiles as a second detector;
# noise produces similar tails in both regions, while a carrier does not.
signal_peak = float(np.percentile(power[signal_mask], 99.9))
noise_peak = float(np.percentile(power[noise_mask], 99.9))
carrier_excess_db = 10 * np.log10(signal_peak / max(noise_peak, 1e-12))
signal_db = round(np.clip(max(channel_excess_db, carrier_excess_db), 0, 60))
noise_dbfs = round(10 * np.log10(max(noise_power, 1e-12)))
return signal_db, noise_dbfs
def _audio_level_dbfs(audio: np.ndarray) -> int:
"""RMS level of demodulated audio, with deterministic silence handling."""
if audio.size == 0:
return -120
rms = float(np.sqrt(np.mean(np.square(audio, dtype=np.float64))))
return round(np.clip(20 * np.log10(max(rms, 1e-6)), -120, 0))
def audio_content_db(audio: np.ndarray, bandwidth_hz: int) -> int:
"""Return spectral structure above flat noise, independent of audio gain.
Welch averaging is essential here. A single short periodogram gives white
noise an apparently uneven spectrum and previously produced false content
scores. At least one second of audio makes stationary broadband hiss nearly
flat while speech, music, and tones retain concentrated spectral energy.
"""
if audio.size < AUDIO_RATE or float(np.max(np.abs(audio))) < 1e-6:
return 0
segment_samples = min(8192, audio.size)
frequencies, power = welch(
audio,
fs=AUDIO_RATE,
nperseg=segment_samples,
noverlap=segment_samples // 2,
)
power = power[(frequencies >= 80) & (frequencies <= bandwidth_hz)]
arithmetic_mean = float(np.mean(power))
if power.size == 0 or arithmetic_mean <= 1e-20:
return 0
floor = max(arithmetic_mean * 1e-12, 1e-24)
geometric_mean = float(np.exp(np.mean(np.log(np.maximum(power, floor)))))
return round(np.clip(-10 * np.log10(geometric_mean / arithmetic_mean), 0, 60))
def _decimate_lowpass(
x: np.ndarray, sos: np.ndarray, zi: np.ndarray | None
) -> tuple[np.ndarray, np.ndarray]:
"""Lowpass + decimate, carrying filter state (zi) across calls.
Without this, sosfilt() implicitly starts from zero state on every
call, which - since this runs once per ~34ms audio chunk - puts a
small discontinuity at every chunk boundary: audible as a persistent
low-level click/crackle laid over the real signal, worse on anything
already weak. Passing the previous call's final state (zf) back in as
the next call's zi makes the filter treat the stream as continuous.
"""
if zi is None:
zi = sosfilt_zi(sos) * x[0] # init as if already at rest at the first sample
if np.iscomplexobj(x):
zi = zi.astype(complex)
y, zf = sosfilt(sos, x, zi=zi)
return y[::_DECIMATION], zf
def _demod_am(iq: np.ndarray, state: _AmDemodState) -> tuple[np.ndarray, _AmDemodState]:
envelope = np.abs(iq)
envelope = envelope - np.mean(envelope) # strip the DC/carrier component
decimated, zf = _decimate_lowpass(envelope, _AM_SOS, state.filter_zi)
rms = float(np.sqrt(np.mean(np.square(decimated, dtype=np.float64))))
coefficient = 0.25 if rms > state.level else 0.02
level = coefficient * rms + (1 - coefficient) * state.level
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
"am demod: iq |mean|=%.6g iq_max=%.6g env_std=%.6g agc_level=%.6g",
float(np.mean(np.abs(iq))),
float(np.max(np.abs(iq))),
float(np.std(envelope)),
level,
)
gain = min(20.0, 0.2 / max(level, 0.001))
audio = np.clip(decimated * gain, -1.0, 1.0).astype(np.float32)
return audio, _AmDemodState(zf, level)
def spectrum_dbfs(iq: np.ndarray, bins: int = 512) -> list[float]:
"""Return a bounded, display-oriented FFT row."""
if iq.size < bins or bins <= 0:
return []
sample = iq[-bins:] - np.mean(iq[-bins:])
window = np.hanning(bins)
power = np.abs(np.fft.fftshift(np.fft.fft(sample * window))) ** 2 / max(
float(np.sum(window)) ** 2, 1.0
)
db = 10 * np.log10(np.maximum(power, 1e-12))
return np.clip(db, -120, 0).astype(float).tolist()
def carrier_offset_hz(iq: np.ndarray, expected_offset_hz: int) -> float:
"""Estimate a narrow carrier near a known offset from the tuned centre."""
if iq.size < 4096:
raise ValueError("at least 4096 IQ samples are required")
centered = iq - np.mean(iq)
window = np.hanning(iq.size)
power = np.abs(np.fft.fftshift(np.fft.fft(centered * window))) ** 2
frequencies = np.fft.fftshift(np.fft.fftfreq(iq.size, 1 / IQ_SAMPLE_RATE))
search = np.abs(frequencies - expected_offset_hz) <= 20_000
if not np.any(search):
raise ValueError("reference offset is outside the captured spectrum")
candidate_power = power[search]
peak = int(np.argmax(candidate_power))
noise = float(np.median(candidate_power))
if float(candidate_power[peak]) < max(noise * 20, 1e-12):
raise ValueError("no unambiguous reference carrier found")
return float(frequencies[search][peak])
def _demod_fm(
iq: np.ndarray, prev_sample: complex | None, zi: np.ndarray | None
) -> tuple[np.ndarray, complex, np.ndarray]:
iq_full = np.concatenate([[prev_sample], iq]) if prev_sample is not None else iq
# Polar discriminator: instantaneous frequency is the phase difference
# between consecutive samples.
demod = np.angle(iq_full[1:] * np.conj(iq_full[:-1]))
decimated, zf = _decimate_lowpass(demod, _FM_SOS, zi)
peak = float(np.max(np.abs(decimated))) + 1e-9
audio = (decimated / max(peak, 0.3)).astype(np.float32)
return audio, iq[-1], zf
def _demod_cw_tone(
iq: np.ndarray, zi: np.ndarray | None
) -> tuple[np.ndarray, np.ndarray]:
"""tune_cw() physically tunes tone_hz off the carrier, so the keyed
carrier beats at exactly that offset in our own baseband - a lowpass
isolates it from everything else in the capture, and the real part is
an audible tone whenever the carrier is present. Fed straight into
cw_decoder.analyze() unchanged, same as it consumed the ATS Mini's own
BFO output before."""
decimated, zf = _decimate_lowpass(iq, _CW_SOS, zi)
audio = np.real(decimated)
peak = float(np.max(np.abs(audio))) + 1e-9
# Same direct-sampling gain gap as _demod_am() - see its comment.
return (audio / max(peak, 0.0005)).astype(np.float32), zf
class RtlSdrBridge:
"""Owns the RTL-SDR device and a background thread that continuously
reads IQ samples, demodulates them, updates status, and feeds both
blocking capture_audio() callers and any live websocket audio
subscribers from one shared stream."""
def __init__(self, device_index: int = 0) -> None:
self._device_index = device_index
self._sdr = RtlSdr(device_index)
self._sdr.sample_rate = IQ_SAMPLE_RATE
# Fixed gain, not "auto": hardware AGC actively normalizes IQ power
# to fill the ADC's dynamic range regardless of actual input signal
# strength, which also moves the measured noise floor. Fixed gain keeps
# noise-relative measurements comparable between tuning operations.
self._gain_db = DEFAULT_GAIN_DB
self._ppm = 0
self._applied_ppm = 0
self._sdr.gain = self._gain_db
self._direct_sampling_active = False
self._state_lock = threading.Lock()
self._frequency_hz = 10_000_000
self._band = "SW"
self._mode = "AM"
self._volume = 70
self._status_lock = threading.Lock()
self._latest_status: RadioStatus | None = None
self._seq = 0
self._audio_bus = _AudioBus()
self._iq_lock = threading.Lock()
self._latest_iq = np.zeros(0, dtype=np.complex64)
self._latest_spectrum: list[float] = []
self._recording: _IqRecording | None = None
self._subscribers_lock = threading.Lock()
self._subscribers: dict[
int, tuple[asyncio.AbstractEventLoop, asyncio.Queue]
] = {}
self._next_subscriber_id = 0
self._stop = threading.Event()
self._retune = threading.Event()
self._worker = threading.Thread(
target=self._run, daemon=True, name="rtlsdr-demod"
)
self._worker.start()
def close(self) -> None:
self._stop.set()
self._worker.join(timeout=2)
with self._iq_lock:
if self._recording is not None:
self._recording.error = "radio closed"
self._recording.output.close()
self._recording.complete.set()
self._recording = None
self._sdr.close()
# ---- tuning -------------------------------------------------------
def _apply_frequency(self, frequency_hz: int) -> None:
with self._state_lock:
gain_db = self._gain_db
ppm = self._ppm
needs_direct = frequency_hz < DIRECT_SAMPLING_THRESHOLD_HZ
if needs_direct != self._direct_sampling_active:
try:
self._sdr.set_direct_sampling(
DIRECT_SAMPLING_MODE if needs_direct else 0
)
except Exception: # noqa: BLE001 -- librtlsdr exposes backend-specific errors
# Some librtlsdr/R820T combinations apply the mode change but
# return LIBUSB_ERROR_IO while reinitialising the tuner. Reopen
# the device into a known state instead of retrying forever
# with a dead audio stream.
logger.warning(
"Reopening RTL-SDR after direct-sampling mode switch failed"
)
self._sdr.close()
self._sdr = RtlSdr(self._device_index)
self._sdr.sample_rate = IQ_SAMPLE_RATE
self._sdr.gain = gain_db
self._applied_ppm = 0
if needs_direct:
self._sdr.set_direct_sampling(DIRECT_SAMPLING_MODE)
self._direct_sampling_active = needs_direct
logger.debug(
"direct sampling %s at %d Hz",
"enabled" if needs_direct else "disabled",
frequency_hz,
)
if ppm != self._applied_ppm:
self._sdr.freq_correction = ppm
self._applied_ppm = ppm
if not needs_direct:
self._sdr.gain = gain_db
self._sdr.center_freq = frequency_hz
def tune_to(self, frequency_hz: int, band: str) -> bool:
"""Direct tune - no band search, unlike the firmware this replaced."""
band = band.upper()
mode = "FM" if band == "FM" else "AM"
with self._state_lock:
self._frequency_hz = frequency_hz
self._band = band if band in ("SW", "AM", "FM") else "SW"
self._mode = mode
self._retune.set()
logger.debug("tune_to: %d Hz band=%s mode=%s", frequency_hz, self._band, mode)
return True
def tune_cw(
self,
carrier_hz: int,
band_label: str = "",
mode: str = "USB",
tone_hz: int = 700,
) -> bool:
"""Tune tone_hz off the carrier so a keyed CW signal beats at an
audible pitch - the RF-vs-image sideband distinction USB/LSB
implies on a real receiver doesn't apply here, since we choose
which side of the carrier to sit on ourselves rather than relying
on hardware sideband selection."""
offset = tone_hz if mode.upper() != "LSB" else -tone_hz
with self._state_lock:
self._frequency_hz = carrier_hz + offset
self._band = "SW"
self._mode = "CW"
self._retune.set()
logger.debug(
"tune_cw: carrier=%d Hz offset=%+d -> %d Hz (%s)",
carrier_hz,
offset,
carrier_hz + offset,
band_label,
)
return True
def set_mode(self, mode: str) -> bool:
with self._state_lock:
self._mode = mode.upper()
return True
def set_gain(self, gain_db: float) -> None:
if gain_db not in SUPPORTED_GAIN_DB:
raise ValueError(f"unsupported tuner gain: {gain_db}")
with self._state_lock:
self._gain_db = gain_db
self._retune.set()
def set_frequency_correction(self, ppm: int) -> None:
if not -200 <= ppm <= 200:
raise ValueError("frequency correction must be between -200 and 200 ppm")
with self._state_lock:
self._ppm = ppm
self._retune.set()
def spectrum(self) -> list[float]:
with self._iq_lock:
return list(self._latest_spectrum)
def estimate_reference(self, expected_offset_hz: int) -> float:
with self._iq_lock:
iq = self._latest_iq.copy()
return carrier_offset_hz(iq, expected_offset_hz)
def record_iq(self, output: io.BufferedWriter, seconds: float) -> int:
requested_samples = round(seconds * IQ_SAMPLE_RATE)
recording = _IqRecording(output, requested_samples, threading.Event())
with self._iq_lock:
if self._recording is not None:
raise RuntimeError("an IQ recording is already running")
self._recording = recording
if not recording.complete.wait(timeout=seconds + 5):
with self._iq_lock:
if self._recording is recording:
self._recording = None
recording.error = "IQ recording timed out"
recording.output.close()
raise TimeoutError(recording.error)
if recording.error:
raise RuntimeError(recording.error)
return recording.written_samples
def volume_up(self) -> None:
with self._state_lock:
self._volume = min(100, self._volume + 5)
def volume_down(self) -> None:
with self._state_lock:
self._volume = max(0, self._volume - 5)
def is_tuning(self) -> bool:
"""Always False: a direct RTL-SDR retune is effectively instant, so
there's no multi-second band-search state to report."""
return False
def latest_status(self) -> RadioStatus | None:
with self._status_lock:
return self._latest_status
# ---- audio ----------------------------------------------------------
def capture_audio(self, seconds: float) -> np.ndarray:
return self._audio_bus.capture(seconds)
def subscribe_audio(
self, loop: asyncio.AbstractEventLoop
) -> tuple[int, asyncio.Queue]:
queue: asyncio.Queue = asyncio.Queue(maxsize=50)
with self._subscribers_lock:
subscriber_id = self._next_subscriber_id
self._next_subscriber_id += 1
self._subscribers[subscriber_id] = (loop, queue)
return subscriber_id, queue
def unsubscribe_audio(self, subscriber_id: int) -> None:
with self._subscribers_lock:
self._subscribers.pop(subscriber_id, None)
def _publish_to_subscribers(self, pcm_bytes: bytes) -> None:
with self._subscribers_lock:
subs = list(self._subscribers.items())
for subscriber_id, (loop, queue) in subs:
try:
loop.call_soon_threadsafe(queue.put_nowait, pcm_bytes)
except RuntimeError:
pass # event loop closed; disconnect handler will clean up
# ---- worker loop ------------------------------------------------------
def _run(self) -> None:
with self._state_lock:
freq, band, mode = self._frequency_hz, self._band, self._mode
self._retune.set()
prev_fm_sample: complex | None = None
filter_zi: np.ndarray | None = None
am_state = _AmDemodState()
signal_history: deque[tuple[int, int, int]] = deque(maxlen=12)
spectrum_countdown = 0
while not self._stop.is_set():
# The whole iteration is one try/except: this thread has no
# external supervisor, so an uncaught exception anywhere in here
# (a bad retune, a transient read glitch, a DSP edge case) would
# otherwise silently and permanently kill the radio until the
# container restarts - log it and keep going instead.
try:
if self._retune.is_set():
with self._iq_lock:
if self._recording is not None:
self._recording.error = "radio retuned during IQ recording"
self._recording.output.close()
self._recording.complete.set()
self._recording = None
with self._state_lock:
freq, band, mode = self._frequency_hz, self._band, self._mode
self._apply_frequency(freq)
prev_fm_sample = None
filter_zi = (
None # a new frequency isn't a continuation of the old stream
)
am_state = _AmDemodState()
signal_history.clear()
self._retune.clear()
iq = self._sdr.read_samples(CHUNK_IQ_SAMPLES)
spectrum_countdown -= 1
next_spectrum = None
if spectrum_countdown <= 0:
next_spectrum = spectrum_dbfs(iq)
spectrum_countdown = 6
with self._iq_lock:
self._latest_iq = iq.astype(np.complex64, copy=True)
if next_spectrum is not None:
self._latest_spectrum = next_spectrum
recording = self._recording
if recording is not None:
count = min(recording.remaining_samples, iq.size)
values = np.empty(count * 2, dtype=np.int8)
values[0::2] = np.clip(np.real(iq[:count]) * 127, -128, 127)
values[1::2] = np.clip(np.imag(iq[:count]) * 127, -128, 127)
try:
recording.output.write(values.tobytes())
recording.written_samples += count
recording.remaining_samples -= count
if recording.remaining_samples == 0:
recording.output.close()
recording.complete.set()
self._recording = None
except OSError as exc:
recording.error = str(exc)
recording.output.close()
recording.complete.set()
self._recording = None
if mode == "FM":
audio, prev_fm_sample, filter_zi = _demod_fm(
iq, prev_fm_sample, filter_zi
)
metrics = _estimate_signal(iq, FM_CHANNEL_BW_HZ)
elif mode == "CW":
audio, filter_zi = _demod_cw_tone(iq, filter_zi)
metrics = _estimate_signal(iq, CW_CHANNEL_BW_HZ)
else:
audio, am_state = _demod_am(iq, am_state)
metrics = _estimate_signal(iq, AM_CHANNEL_BW_HZ)
audio_dbfs = _audio_level_dbfs(audio)
signal_history.append((*metrics, audio_dbfs))
signal_db = round(
float(np.median([item[0] for item in signal_history]))
)
noise_dbfs = round(
float(np.median([item[1] for item in signal_history]))
)
audio_dbfs = round(
float(np.median([item[2] for item in signal_history]))
)
with self._state_lock:
volume = self._volume
gain_db = self._gain_db
ppm = self._ppm
audio = np.clip(audio * (volume / 100.0), -1.0, 1.0)
self._audio_bus.publish(audio)
self._publish_to_subscribers((audio * 32767).astype(np.int16).tobytes())
with self._status_lock:
self._seq += 1
self._latest_status = RadioStatus(
frequency_hz=freq,
band=band,
mode=mode,
signal_db=signal_db,
noise_dbfs=noise_dbfs,
audio_dbfs=audio_dbfs,
volume=volume,
gain_db=gain_db,
ppm=ppm,
direct_sampling=self._direct_sampling_active,
seq=self._seq,
)
except Exception:
logger.exception("RTL-SDR worker iteration failed")
time.sleep(0.1)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.