2020-12-23 16:48:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Wed Dec 23 07:04:24 2020
|
|
|
|
|
|
|
|
@author: DJ2LS
|
|
|
|
"""
|
2022-12-26 11:49:01 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
|
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
|
2023-11-09 16:14:22 +00:00
|
|
|
import queue
|
2022-05-11 22:10:59 +00:00
|
|
|
import time
|
|
|
|
import codec2
|
2022-05-22 16:38:56 +00:00
|
|
|
import numpy as np
|
|
|
|
import sounddevice as sd
|
|
|
|
import structlog
|
2023-02-14 21:02:30 +00:00
|
|
|
import tci
|
2023-10-29 22:10:12 +00:00
|
|
|
import cw
|
2023-11-07 14:20:09 +00:00
|
|
|
import audio
|
2023-11-29 16:35:23 +00:00
|
|
|
import demodulator
|
2024-03-12 17:39:30 +00:00
|
|
|
import modulator
|
2021-07-10 21:27:33 +00:00
|
|
|
|
2023-12-16 11:54:16 +00:00
|
|
|
TESTMODE = False
|
|
|
|
|
2022-05-22 16:38:56 +00:00
|
|
|
class RF:
|
|
|
|
"""Class to encapsulate interactions between the audio device and codec2"""
|
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
log = structlog.get_logger("RF")
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2024-01-12 14:51:23 +00:00
|
|
|
def __init__(self, config, event_manager, fft_queue, service_queue, states, radio_manager) -> None:
|
2023-11-08 13:24:54 +00:00
|
|
|
self.config = config
|
2023-11-09 21:11:53 +00:00
|
|
|
self.service_queue = service_queue
|
2023-11-11 19:01:15 +00:00
|
|
|
self.states = states
|
2024-01-04 14:46:58 +00:00
|
|
|
self.event_manager = event_manager
|
2024-01-12 14:51:23 +00:00
|
|
|
self.radio = radio_manager
|
2022-02-02 20:12:16 +00:00
|
|
|
self.sampler_avg = 0
|
|
|
|
self.buffer_avg = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2023-11-07 14:20:09 +00:00
|
|
|
# these are crc ids now
|
2023-11-11 19:01:15 +00:00
|
|
|
self.audio_input_device = config['AUDIO']['input_device']
|
|
|
|
self.audio_output_device = config['AUDIO']['output_device']
|
2023-11-07 10:36:49 +00:00
|
|
|
|
2024-03-12 17:39:30 +00:00
|
|
|
|
2023-11-17 09:22:45 +00:00
|
|
|
|
2023-11-18 16:55:49 +00:00
|
|
|
self.radiocontrol = config['RADIO']['control']
|
|
|
|
self.rigctld_ip = config['RIGCTLD']['ip']
|
|
|
|
self.rigctld_port = config['RIGCTLD']['port']
|
2023-11-17 09:37:50 +00:00
|
|
|
|
2023-11-12 22:22:53 +00:00
|
|
|
self.tci_ip = config['TCI']['tci_ip']
|
|
|
|
self.tci_port = config['TCI']['tci_port']
|
|
|
|
|
2024-03-12 17:39:30 +00:00
|
|
|
self.tx_audio_level = config['AUDIO']['tx_audio_level']
|
2024-03-12 18:48:50 +00:00
|
|
|
self.rx_audio_level = config['AUDIO']['rx_audio_level']
|
2024-03-12 17:39:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
self.ptt_state = False
|
2024-03-15 08:45:51 +00:00
|
|
|
self.enqueuing_audio = False # set to True, while we are processing audio
|
2023-11-11 10:09:19 +00:00
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
self.AUDIO_SAMPLE_RATE = 48000
|
2024-03-12 17:39:30 +00:00
|
|
|
self.modem_sample_rate = codec2.api.FREEDV_FS_8000
|
2023-03-29 09:21:58 +00:00
|
|
|
|
2022-05-22 16:38:56 +00:00
|
|
|
# 8192 Let's do some tests with very small chunks for TX
|
2024-03-11 18:52:03 +00:00
|
|
|
#self.AUDIO_FRAMES_PER_BUFFER_TX = 1200 if self.radiocontrol in ["tci"] else 2400 * 2
|
2024-03-12 17:39:30 +00:00
|
|
|
# 8 * (self.AUDIO_SAMPLE_RATE/self.modem_sample_rate) == 48
|
2021-10-03 14:31:34 +00:00
|
|
|
self.AUDIO_CHANNELS = 1
|
2022-05-22 16:38:56 +00:00
|
|
|
self.MODE = 0
|
2023-11-28 22:46:55 +00:00
|
|
|
self.rms_counter = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
self.audio_out_queue = queue.Queue()
|
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Make sure our resampler will work
|
2024-03-12 17:39:30 +00:00
|
|
|
assert (self.AUDIO_SAMPLE_RATE / self.modem_sample_rate) == codec2.api.FDMDV_OS_48 # type: ignore
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2023-11-29 16:35:23 +00:00
|
|
|
self.audio_received_queue = queue.Queue()
|
|
|
|
self.data_queue_received = queue.Queue()
|
2023-11-09 16:14:22 +00:00
|
|
|
self.fft_queue = fft_queue
|
|
|
|
|
2023-11-29 16:35:23 +00:00
|
|
|
self.demodulator = demodulator.Demodulator(self.config,
|
|
|
|
self.audio_received_queue,
|
|
|
|
self.data_queue_received,
|
|
|
|
self.states,
|
2023-12-16 10:05:53 +00:00
|
|
|
self.event_manager,
|
2024-03-11 18:52:03 +00:00
|
|
|
self.service_queue,
|
2023-12-16 10:05:53 +00:00
|
|
|
self.fft_queue
|
|
|
|
)
|
2023-11-29 16:35:23 +00:00
|
|
|
|
2024-03-12 17:39:30 +00:00
|
|
|
self.modulator = modulator.Modulator(self.config)
|
|
|
|
|
2023-12-21 16:47:48 +00:00
|
|
|
|
2023-11-26 21:24:09 +00:00
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
def tci_tx_callback(self, audio_48k) -> None:
|
|
|
|
self.radio.set_ptt(True)
|
|
|
|
self.event_manager.send_ptt_change(True)
|
|
|
|
self.tci_module.push_audio(audio_48k)
|
2022-05-22 16:38:56 +00:00
|
|
|
|
2023-11-11 09:41:16 +00:00
|
|
|
def start_modem(self):
|
2023-12-21 18:42:35 +00:00
|
|
|
if TESTMODE:
|
|
|
|
return True
|
|
|
|
elif self.radiocontrol.lower() == "tci":
|
|
|
|
if not self.init_tci():
|
|
|
|
return False
|
2023-11-11 09:41:16 +00:00
|
|
|
else:
|
2023-12-21 18:42:35 +00:00
|
|
|
if not self.init_audio():
|
|
|
|
raise RuntimeError("Unable to init audio devices")
|
|
|
|
self.demodulator.start(self.sd_input_stream)
|
2023-11-26 21:24:09 +00:00
|
|
|
|
2023-12-21 18:42:35 +00:00
|
|
|
return True
|
2023-11-11 09:41:16 +00:00
|
|
|
|
|
|
|
def stop_modem(self):
|
|
|
|
try:
|
|
|
|
# let's stop the modem service
|
|
|
|
self.service_queue.put("stop")
|
|
|
|
# simulate audio class active state for reducing cli output
|
|
|
|
# self.stream = lambda: None
|
|
|
|
# self.stream.active = False
|
|
|
|
# self.stream.stop
|
2023-11-26 21:24:09 +00:00
|
|
|
|
2023-11-11 09:41:16 +00:00
|
|
|
except Exception:
|
2023-11-11 10:09:19 +00:00
|
|
|
self.log.error("[MDM] Error stopping modem")
|
2023-11-11 09:41:16 +00:00
|
|
|
|
2023-11-08 13:24:54 +00:00
|
|
|
def init_audio(self):
|
2023-11-11 09:53:01 +00:00
|
|
|
self.log.info(f"[MDM] init: get audio devices", input_device=self.audio_input_device,
|
|
|
|
output_device=self.audio_output_device)
|
2023-11-11 09:41:16 +00:00
|
|
|
try:
|
2023-11-11 09:53:01 +00:00
|
|
|
result = audio.get_device_index_from_crc(self.audio_input_device, True)
|
|
|
|
if result is None:
|
|
|
|
raise ValueError("Invalid input device")
|
|
|
|
else:
|
|
|
|
in_dev_index, in_dev_name = result
|
|
|
|
|
|
|
|
result = audio.get_device_index_from_crc(self.audio_output_device, False)
|
|
|
|
if result is None:
|
|
|
|
raise ValueError("Invalid output device")
|
|
|
|
else:
|
|
|
|
out_dev_index, out_dev_name = result
|
|
|
|
|
|
|
|
self.log.info(f"[MDM] init: receiving audio from '{in_dev_name}'")
|
|
|
|
self.log.info(f"[MDM] init: transmiting audio on '{out_dev_name}'")
|
|
|
|
self.log.debug("[MDM] init: starting pyaudio callback and decoding threads")
|
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
sd.default.samplerate = self.AUDIO_SAMPLE_RATE
|
|
|
|
sd.default.device = (in_dev_index, out_dev_index)
|
|
|
|
|
2023-11-11 09:53:01 +00:00
|
|
|
# init codec2 resampler
|
|
|
|
self.resampler = codec2.resampler()
|
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
# SoundDevice audio input stream
|
|
|
|
self.sd_input_stream = sd.InputStream(
|
2023-11-11 09:53:01 +00:00
|
|
|
channels=1,
|
|
|
|
dtype="int16",
|
2024-03-12 18:48:50 +00:00
|
|
|
callback=self.sd_input_audio_callback,
|
2023-12-15 17:25:01 +00:00
|
|
|
device=in_dev_index,
|
|
|
|
samplerate=self.AUDIO_SAMPLE_RATE,
|
2023-11-11 09:53:01 +00:00
|
|
|
blocksize=4800,
|
|
|
|
)
|
2023-12-15 17:25:01 +00:00
|
|
|
self.sd_input_stream.start()
|
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
self.sd_output_stream = sd.OutputStream(
|
|
|
|
channels=1,
|
|
|
|
dtype="int16",
|
|
|
|
callback=self.sd_output_audio_callback,
|
|
|
|
device=out_dev_index,
|
|
|
|
samplerate=self.AUDIO_SAMPLE_RATE,
|
|
|
|
blocksize=4800,
|
|
|
|
)
|
|
|
|
self.sd_output_stream.start()
|
2023-11-11 09:53:01 +00:00
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
return True
|
2023-11-11 09:53:01 +00:00
|
|
|
|
|
|
|
except Exception as audioerr:
|
|
|
|
self.log.error("[MDM] init: starting pyaudio callback failed", e=audioerr)
|
2023-11-11 09:41:16 +00:00
|
|
|
self.stop_modem()
|
2023-11-11 09:53:01 +00:00
|
|
|
return False
|
|
|
|
|
2023-11-11 09:41:16 +00:00
|
|
|
def init_tci(self):
|
|
|
|
# placeholder area for processing audio via TCI
|
|
|
|
# https://github.com/maksimus1210/TCI
|
2023-11-12 22:22:53 +00:00
|
|
|
self.log.warning("[MDM] [TCI] Not yet fully implemented", ip=self.tci_ip, port=self.tci_port)
|
2023-11-11 09:41:16 +00:00
|
|
|
|
|
|
|
# we are trying this by simulating an audio stream Object like with mkfifo
|
|
|
|
class Object:
|
|
|
|
"""An object for simulating audio stream"""
|
|
|
|
active = True
|
|
|
|
|
|
|
|
self.stream = Object()
|
|
|
|
|
|
|
|
# lets init TCI module
|
2023-12-15 23:51:57 +00:00
|
|
|
self.tci_module = tci.TCICtrl(self.audio_received_queue)
|
2023-11-11 09:41:16 +00:00
|
|
|
|
2023-12-16 11:54:16 +00:00
|
|
|
return True
|
2022-05-22 16:38:56 +00:00
|
|
|
|
2023-12-25 09:31:00 +00:00
|
|
|
|
|
|
|
|
2023-05-17 13:23:49 +00:00
|
|
|
def transmit_morse(self, repeats, repeat_delay, frames):
|
2023-11-22 20:54:50 +00:00
|
|
|
self.states.waitForTransmission()
|
|
|
|
self.states.setTransmitting(True)
|
2023-05-17 13:23:49 +00:00
|
|
|
# if we're transmitting FreeDATA signals, reset channel busy state
|
|
|
|
self.log.debug(
|
|
|
|
"[MDM] TRANSMIT", mode="MORSE"
|
|
|
|
)
|
|
|
|
start_of_transmission = time.time()
|
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
txbuffer_out = cw.MorseCodePlayer().text_to_signal(self.config['STATION'].mycall)
|
2023-05-17 13:23:49 +00:00
|
|
|
|
2024-03-12 18:48:50 +00:00
|
|
|
# transmit audio
|
2024-03-12 15:33:40 +00:00
|
|
|
self.enqueue_audio_out(txbuffer_out)
|
2023-05-17 13:23:49 +00:00
|
|
|
|
|
|
|
end_of_transmission = time.time()
|
|
|
|
transmission_time = end_of_transmission - start_of_transmission
|
|
|
|
self.log.debug("[MDM] ON AIR TIME", time=transmission_time)
|
|
|
|
|
2023-11-08 13:24:54 +00:00
|
|
|
|
2024-03-12 18:48:50 +00:00
|
|
|
def transmit(
|
|
|
|
self, mode, repeats: int, repeat_delay: int, frames: bytearray
|
|
|
|
) -> bool:
|
|
|
|
|
|
|
|
self.demodulator.reset_data_sync()
|
|
|
|
|
|
|
|
# Wait for some other thread that might be transmitting
|
|
|
|
self.states.waitForTransmission()
|
|
|
|
self.states.setTransmitting(True)
|
|
|
|
# self.states.channel_busy_event.wait()
|
|
|
|
|
|
|
|
start_of_transmission = time.time()
|
|
|
|
txbuffer = self.modulator.create_burst(mode, repeats, repeat_delay, frames)
|
|
|
|
|
|
|
|
# Re-sample back up to 48k (resampler works on np.int16)
|
|
|
|
x = np.frombuffer(txbuffer, dtype=np.int16)
|
|
|
|
x = audio.set_audio_volume(x, self.tx_audio_level)
|
|
|
|
|
|
|
|
if self.radiocontrol not in ["tci"]:
|
|
|
|
txbuffer_out = self.resampler.resample8_to_48(x)
|
|
|
|
else:
|
|
|
|
txbuffer_out = x
|
|
|
|
|
|
|
|
# transmit audio
|
|
|
|
self.enqueue_audio_out(txbuffer_out)
|
|
|
|
|
|
|
|
end_of_transmission = time.time()
|
|
|
|
transmission_time = end_of_transmission - start_of_transmission
|
|
|
|
self.log.debug("[MDM] ON AIR TIME", time=transmission_time)
|
|
|
|
|
2023-11-08 13:24:54 +00:00
|
|
|
|
2022-10-10 07:00:45 +00:00
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
def enqueue_audio_out(self, audio_48k) -> None:
|
2024-03-15 08:45:51 +00:00
|
|
|
self.enqueuing_audio = True
|
|
|
|
|
2024-03-12 15:33:40 +00:00
|
|
|
if not self.states.isTransmitting():
|
|
|
|
self.states.setTransmitting(True)
|
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
self.radio.set_ptt(True)
|
|
|
|
self.event_manager.send_ptt_change(True)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
if self.radiocontrol in ["tci"]:
|
|
|
|
self.tci_tx_callback(audio_48k)
|
2023-12-25 09:31:00 +00:00
|
|
|
# we need to wait manually for tci processing
|
|
|
|
self.tci_module.wait_until_transmitted(audio_48k)
|
2023-12-15 17:25:01 +00:00
|
|
|
else:
|
2024-03-12 15:33:40 +00:00
|
|
|
# slice audio data to needed blocklength
|
|
|
|
block_size = 4800
|
|
|
|
pad_length = -len(audio_48k) % block_size
|
|
|
|
padded_data = np.pad(audio_48k, (0, pad_length), mode='constant')
|
|
|
|
sliced_audio_data = padded_data.reshape(-1, block_size)
|
|
|
|
# add each block to audio out queue
|
|
|
|
for block in sliced_audio_data:
|
|
|
|
self.audio_out_queue.put(block)
|
|
|
|
|
2024-03-15 08:45:51 +00:00
|
|
|
self.enqueuing_audio = False
|
2024-03-12 15:33:40 +00:00
|
|
|
self.states.transmitting_event.wait()
|
|
|
|
|
|
|
|
self.radio.set_ptt(False)
|
|
|
|
self.event_manager.send_ptt_change(False)
|
|
|
|
|
2023-12-15 17:25:01 +00:00
|
|
|
return
|
2024-03-12 15:33:40 +00:00
|
|
|
|
|
|
|
def sd_output_audio_callback(self, outdata: np.ndarray, frames: int, time, status) -> None:
|
|
|
|
try:
|
|
|
|
if not self.audio_out_queue.empty():
|
|
|
|
chunk = self.audio_out_queue.get_nowait()
|
|
|
|
audio.calculate_fft(chunk, self.fft_queue, self.states)
|
|
|
|
outdata[:] = chunk.reshape(outdata.shape)
|
|
|
|
|
|
|
|
else:
|
2024-03-15 08:45:51 +00:00
|
|
|
# reset transmitting state only, if we are not actively processing audio
|
|
|
|
# for avoiding a ptt toggle state bug
|
|
|
|
if not self.enqueuing_audio:
|
|
|
|
self.states.setTransmitting(False)
|
2024-03-12 15:33:40 +00:00
|
|
|
# Fill with zeros if the queue is empty
|
|
|
|
outdata.fill(0)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("[AUDIO STATUS]", status=status, time=time, frames=frames, e=e)
|
|
|
|
outdata.fill(0)
|
2024-03-12 17:39:30 +00:00
|
|
|
|
2024-03-12 18:48:50 +00:00
|
|
|
def sd_input_audio_callback(self, indata: np.ndarray, frames: int, time, status) -> None:
|
|
|
|
if status:
|
|
|
|
self.log.warning("[AUDIO STATUS]", status=status, time=time, frames=frames)
|
|
|
|
# FIXME on windows input overflows crashing the rx audio stream. Lets restart the server then
|
2024-03-13 21:06:59 +00:00
|
|
|
#if status.input_overflow:
|
|
|
|
# self.service_queue.put("restart")
|
2024-03-12 18:48:50 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
audio_48k = np.frombuffer(indata, dtype=np.int16)
|
|
|
|
audio_8k = self.resampler.resample48_to_8(audio_48k)
|
|
|
|
|
|
|
|
audio_8k_level_adjusted = audio.set_audio_volume(audio_8k, self.rx_audio_level)
|
|
|
|
|
|
|
|
if not self.states.isTransmitting():
|
|
|
|
audio.calculate_fft(audio_8k_level_adjusted, self.fft_queue, self.states)
|
|
|
|
|
|
|
|
length_audio_8k_level_adjusted = len(audio_8k_level_adjusted)
|
|
|
|
# Avoid buffer overflow by filling only if buffer for
|
|
|
|
# selected datachannel mode is not full
|
|
|
|
index = 0
|
|
|
|
for mode in self.demodulator.MODE_DICT:
|
|
|
|
mode_data = self.demodulator.MODE_DICT[mode]
|
|
|
|
audiobuffer = mode_data['audio_buffer']
|
|
|
|
decode = mode_data['decode']
|
|
|
|
index += 1
|
|
|
|
if audiobuffer:
|
|
|
|
if (audiobuffer.nbuffer + length_audio_8k_level_adjusted) > audiobuffer.size:
|
|
|
|
self.demodulator.buffer_overflow_counter[index] += 1
|
|
|
|
self.event_manager.send_buffer_overflow(self.demodulator.buffer_overflow_counter)
|
|
|
|
elif decode:
|
|
|
|
audiobuffer.push(audio_8k_level_adjusted)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("[AUDIO EXCEPTION]", status=status, time=time, frames=frames, e=e)
|
2024-03-12 17:39:30 +00:00
|
|
|
|