2021-12-19 14:04:35 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Wed Dec 23 07:04:24 2020
|
|
|
|
|
|
|
|
@author: DJ2LS
|
|
|
|
"""
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
import argparse
|
2021-12-19 14:04:35 +00:00
|
|
|
import ctypes
|
|
|
|
import sys
|
|
|
|
import time
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-19 14:04:35 +00:00
|
|
|
import numpy as np
|
2022-05-21 23:04:17 +00:00
|
|
|
import pyaudio
|
2021-12-19 14:04:35 +00:00
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
sys.path.insert(0, "..")
|
2023-10-20 12:12:20 +00:00
|
|
|
from modem import codec2
|
2021-12-19 14:04:35 +00:00
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
|
|
|
parser = argparse.ArgumentParser(description="FreeDATA audio test")
|
|
|
|
parser.add_argument("--bursts", dest="N_BURSTS", default=1, type=int)
|
|
|
|
parser.add_argument("--framesperburst", dest="N_FRAMES_PER_BURST", default=1, type=int)
|
|
|
|
parser.add_argument(
|
2023-04-21 13:04:09 +00:00
|
|
|
"--mode", dest="FREEDV_MODE", type=str, choices=["datac13", "datac1", "datac3"]
|
2022-05-21 23:04:17 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--audiodev",
|
|
|
|
dest="AUDIO_INPUT_DEVICE",
|
|
|
|
default=-1,
|
|
|
|
type=int,
|
|
|
|
help="audio device number to use, use -2 to automatically select a loopback device",
|
|
|
|
)
|
|
|
|
parser.add_argument("--debug", dest="DEBUGGING_MODE", action="store_true")
|
|
|
|
parser.add_argument(
|
|
|
|
"--timeout",
|
|
|
|
dest="TIMEOUT",
|
2023-04-21 14:18:02 +00:00
|
|
|
default=60,
|
2022-05-21 23:04:17 +00:00
|
|
|
type=int,
|
|
|
|
help="Timeout (seconds) before test ends",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--list",
|
|
|
|
dest="LIST",
|
|
|
|
action="store_true",
|
|
|
|
help="list audio devices by number and exit",
|
|
|
|
)
|
|
|
|
|
|
|
|
args, _ = parser.parse_known_args()
|
2021-12-19 14:04:35 +00:00
|
|
|
|
|
|
|
if args.LIST:
|
|
|
|
p = pyaudio.PyAudio()
|
2022-05-21 23:04:17 +00:00
|
|
|
for dev in range(p.get_device_count()):
|
2021-12-19 14:04:35 +00:00
|
|
|
print("audiodev: ", dev, p.get_device_info_by_index(dev)["name"])
|
2022-05-21 23:04:17 +00:00
|
|
|
sys.exit()
|
2021-12-20 09:22:55 +00:00
|
|
|
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
class Test:
|
2021-12-20 09:22:55 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.N_BURSTS = args.N_BURSTS
|
|
|
|
self.N_FRAMES_PER_BURST = args.N_FRAMES_PER_BURST
|
|
|
|
self.AUDIO_INPUT_DEVICE = args.AUDIO_INPUT_DEVICE
|
|
|
|
self.MODE = codec2.FREEDV_MODE[args.FREEDV_MODE].value
|
|
|
|
self.DEBUGGING_MODE = args.DEBUGGING_MODE
|
|
|
|
self.TIMEOUT = args.TIMEOUT
|
|
|
|
|
|
|
|
# AUDIO PARAMETERS
|
2022-05-21 23:04:17 +00:00
|
|
|
self.AUDIO_FRAMES_PER_BUFFER = (
|
|
|
|
2400 * 2
|
|
|
|
) # <- consider increasing if you get nread_exceptions > 0
|
2021-12-20 09:22:55 +00:00
|
|
|
self.MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000
|
|
|
|
self.AUDIO_SAMPLE_RATE_RX = 48000
|
|
|
|
|
|
|
|
# make sure our resampler will work
|
2022-05-21 23:04:17 +00:00
|
|
|
assert (
|
|
|
|
self.AUDIO_SAMPLE_RATE_RX / self.MODEM_SAMPLE_RATE
|
|
|
|
) == codec2.api.FDMDV_OS_48
|
2021-12-20 09:22:55 +00:00
|
|
|
|
2022-05-23 12:26:14 +00:00
|
|
|
# check if we want to use an audio device then do a pyaudio init
|
2022-05-21 23:04:17 +00:00
|
|
|
if self.AUDIO_INPUT_DEVICE != -1:
|
2021-12-20 09:22:55 +00:00
|
|
|
self.p = pyaudio.PyAudio()
|
|
|
|
# auto search for loopback devices
|
|
|
|
if self.AUDIO_INPUT_DEVICE == -2:
|
2023-02-09 12:26:25 +00:00
|
|
|
loopback_list = [
|
|
|
|
dev
|
|
|
|
for dev in range(self.p.get_device_count())
|
|
|
|
if "Loopback: PCM"
|
|
|
|
in self.p.get_device_info_by_index(dev)["name"]
|
|
|
|
]
|
2021-12-20 09:22:55 +00:00
|
|
|
if len(loopback_list) >= 2:
|
2022-05-21 23:04:17 +00:00
|
|
|
self.AUDIO_INPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
2021-12-20 09:22:55 +00:00
|
|
|
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
|
|
|
else:
|
2022-05-21 23:04:17 +00:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
print(
|
|
|
|
f"AUDIO INPUT DEVICE: {self.AUDIO_INPUT_DEVICE} DEVICE: {self.p.get_device_info_by_index(self.AUDIO_INPUT_DEVICE)['name']} \
|
|
|
|
AUDIO SAMPLE RATE: {self.AUDIO_SAMPLE_RATE_RX}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.stream_rx = self.p.open(
|
|
|
|
format=pyaudio.paInt16,
|
|
|
|
channels=1,
|
|
|
|
rate=self.AUDIO_SAMPLE_RATE_RX,
|
|
|
|
frames_per_buffer=self.AUDIO_FRAMES_PER_BUFFER,
|
|
|
|
input=True,
|
|
|
|
output=False,
|
|
|
|
input_device_index=self.AUDIO_INPUT_DEVICE,
|
|
|
|
stream_callback=self.callback,
|
|
|
|
)
|
|
|
|
|
|
|
|
# open codec2 instance
|
|
|
|
self.freedv = ctypes.cast(codec2.api.freedv_open(self.MODE), ctypes.c_void_p)
|
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
# get number of bytes per frame for mode
|
2022-05-21 23:04:17 +00:00
|
|
|
self.bytes_per_frame = int(
|
|
|
|
codec2.api.freedv_get_bits_per_modem_frame(self.freedv) / 8
|
|
|
|
)
|
|
|
|
|
|
|
|
self.bytes_out = ctypes.create_string_buffer(self.bytes_per_frame)
|
|
|
|
|
|
|
|
codec2.api.freedv_set_frames_per_burst(self.freedv, self.N_FRAMES_PER_BURST)
|
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
self.total_n_bytes = 0
|
|
|
|
self.rx_total_frames = 0
|
|
|
|
self.rx_frames = 0
|
|
|
|
self.rx_bursts = 0
|
|
|
|
self.rx_errors = 0
|
|
|
|
self.nread_exceptions = 0
|
|
|
|
self.timeout = time.time() + self.TIMEOUT
|
|
|
|
self.receive = True
|
2022-05-21 23:04:17 +00:00
|
|
|
self.audio_buffer = codec2.audio_buffer(self.AUDIO_FRAMES_PER_BUFFER * 2)
|
2021-12-20 09:22:55 +00:00
|
|
|
self.resampler = codec2.resampler()
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
# Copy received 48 kHz to a file. Listen to this file with:
|
|
|
|
# aplay -r 48000 -f S16_LE rx48_callback.raw
|
|
|
|
# Corruption of this file is a good way to detect audio card issues
|
2022-05-21 23:04:17 +00:00
|
|
|
self.frx = open("rx48_callback.raw", mode="wb")
|
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
def callback(self, data_in48k, frame_count, time_info, status):
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
x = np.frombuffer(data_in48k, dtype=np.int16)
|
|
|
|
x.tofile(self.frx)
|
2022-05-21 23:04:17 +00:00
|
|
|
x = self.resampler.resample48_to_8(x)
|
2021-12-20 09:22:55 +00:00
|
|
|
self.audio_buffer.push(x)
|
|
|
|
|
|
|
|
# when we have enough samples call FreeDV Rx
|
|
|
|
nin = codec2.api.freedv_nin(self.freedv)
|
|
|
|
while self.audio_buffer.nbuffer >= nin:
|
|
|
|
|
|
|
|
# demodulate audio
|
2022-05-21 23:04:17 +00:00
|
|
|
nbytes = codec2.api.freedv_rawdatarx(
|
|
|
|
self.freedv, self.bytes_out, self.audio_buffer.buffer.ctypes
|
|
|
|
)
|
2021-12-20 09:22:55 +00:00
|
|
|
self.audio_buffer.pop(nin)
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
# call me on every loop!
|
|
|
|
nin = codec2.api.freedv_nin(self.freedv)
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
rx_status = codec2.api.freedv_get_rx_status(self.freedv)
|
2021-12-20 09:22:55 +00:00
|
|
|
if rx_status & codec2.api.FREEDV_RX_BIT_ERRORS:
|
|
|
|
self.rx_errors = self.rx_errors + 1
|
2022-05-21 23:04:17 +00:00
|
|
|
if self.DEBUGGING_MODE:
|
|
|
|
rx_status = codec2.api.rx_sync_flags_to_text[rx_status]
|
|
|
|
print(
|
|
|
|
"nin: %5d rx_status: %4s naudio_buffer: %4d"
|
|
|
|
% (nin, rx_status, self.audio_buffer.nbuffer),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-12-20 09:22:55 +00:00
|
|
|
|
|
|
|
if nbytes:
|
|
|
|
self.total_n_bytes = self.total_n_bytes + nbytes
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
if nbytes == self.bytes_per_frame:
|
|
|
|
self.rx_total_frames = self.rx_total_frames + 1
|
|
|
|
self.rx_frames = self.rx_frames + 1
|
|
|
|
|
|
|
|
if self.rx_frames == self.N_FRAMES_PER_BURST:
|
|
|
|
self.rx_frames = 0
|
|
|
|
self.rx_bursts = self.rx_bursts + 1
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
if self.rx_bursts == self.N_BURSTS:
|
|
|
|
self.receive = False
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
return (None, pyaudio.paContinue)
|
|
|
|
|
|
|
|
def run_audio(self):
|
2022-05-21 23:04:17 +00:00
|
|
|
try:
|
2023-02-09 12:26:25 +00:00
|
|
|
print("starting pyaudio callback", file=sys.stderr)
|
2021-12-20 09:22:55 +00:00
|
|
|
self.stream_rx.start_stream()
|
|
|
|
except Exception as e:
|
2022-05-21 23:04:17 +00:00
|
|
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
2021-12-20 09:22:55 +00:00
|
|
|
|
|
|
|
while self.receive and time.time() < self.timeout:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if time.time() >= self.timeout:
|
|
|
|
print("TIMEOUT REACHED")
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
if self.nread_exceptions:
|
2022-05-21 23:04:17 +00:00
|
|
|
print(
|
|
|
|
"nread_exceptions %d - receive audio lost! Consider increasing Pyaudio frames_per_buffer..."
|
|
|
|
% self.nread_exceptions,
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
f"RECEIVED BURSTS: {self.rx_bursts} RECEIVED FRAMES: {self.rx_total_frames} RX_ERRORS: {self.rx_errors}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-12-20 09:22:55 +00:00
|
|
|
self.frx.close()
|
|
|
|
|
|
|
|
# cloese pyaudio instance
|
|
|
|
self.stream_rx.close()
|
|
|
|
self.p.terminate()
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
|
2021-12-20 09:22:55 +00:00
|
|
|
test = Test()
|
2022-05-21 23:04:17 +00:00
|
|
|
test.run_audio()
|