2021-12-13 20:11:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import pyaudio
|
|
|
|
import audioop
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import ctypes
|
|
|
|
from ctypes import *
|
|
|
|
import pathlib
|
|
|
|
sys.path.insert(0,'..')
|
|
|
|
import codec2
|
2021-12-15 04:35:30 +00:00
|
|
|
import numpy as np
|
2021-12-13 20:11:09 +00:00
|
|
|
|
|
|
|
#--------------------------------------------GET PARAMETER INPUTS
|
|
|
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
2021-12-16 11:10:30 +00:00
|
|
|
parser.add_argument('--bursts', dest="N_BURSTS", default=1, type=int)
|
2021-12-14 22:07:01 +00:00
|
|
|
parser.add_argument('--framesperburst', dest="N_FRAMES_PER_BURST", default=1, type=int)
|
2021-12-14 03:54:45 +00:00
|
|
|
parser.add_argument('--audiodev', dest="AUDIO_INPUT_DEVICE", default=-1, type=int, help="audio device number to use")
|
2021-12-13 20:11:09 +00:00
|
|
|
parser.add_argument('--debug', dest="DEBUGGING_MODE", action="store_true")
|
2021-12-14 03:54:45 +00:00
|
|
|
parser.add_argument('--list', dest="LIST", action="store_true", help="list audio devices by number and exit")
|
|
|
|
parser.add_argument('--timeout', dest="TIMEOUT", default=10, type=int, help="Timeout (seconds) before test ends")
|
2021-12-13 20:11:09 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-12-14 03:54:45 +00:00
|
|
|
if args.LIST:
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
for dev in range(0,p.get_device_count()):
|
|
|
|
print("audiodev: ", dev, p.get_device_info_by_index(dev)["name"])
|
|
|
|
quit()
|
|
|
|
|
2021-12-13 20:11:09 +00:00
|
|
|
N_BURSTS = args.N_BURSTS
|
|
|
|
N_FRAMES_PER_BURST = args.N_FRAMES_PER_BURST
|
2021-12-14 03:54:45 +00:00
|
|
|
AUDIO_INPUT_DEVICE = args.AUDIO_INPUT_DEVICE
|
2021-12-14 22:07:01 +00:00
|
|
|
DEBUGGING_MODE = args.DEBUGGING_MODE
|
2021-12-14 03:54:45 +00:00
|
|
|
TIMEOUT = args.TIMEOUT
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-14 03:54:45 +00:00
|
|
|
# AUDIO PARAMETERS
|
2021-12-16 11:10:30 +00:00
|
|
|
AUDIO_FRAMES_PER_BUFFER = 2400*2
|
2021-12-14 03:54:45 +00:00
|
|
|
MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000
|
2021-12-16 11:10:30 +00:00
|
|
|
AUDIO_SAMPLE_RATE_RX = 48000
|
|
|
|
# make sure our resampler will work
|
|
|
|
assert (AUDIO_SAMPLE_RATE_RX / MODEM_SAMPLE_RATE) == codec2.api.FDMDV_OS_48
|
2021-12-13 20:11:09 +00:00
|
|
|
|
|
|
|
# SET COUNTERS
|
|
|
|
rx_total_frames_datac0 = 0
|
|
|
|
rx_frames_datac0 = 0
|
|
|
|
rx_bursts_datac0 = 0
|
|
|
|
|
|
|
|
rx_total_frames_datac1 = 0
|
|
|
|
rx_frames_datac1 = 0
|
|
|
|
rx_bursts_datac1 = 0
|
|
|
|
|
|
|
|
rx_total_frames_datac3 = 0
|
|
|
|
rx_frames_datac3 = 0
|
|
|
|
rx_bursts_datac3 = 0
|
|
|
|
|
|
|
|
|
|
|
|
# open codec2 instance
|
2021-12-15 04:35:30 +00:00
|
|
|
datac0_freedv = cast(codec2.api.freedv_open(codec2.api.FREEDV_MODE_DATAC0), c_void_p)
|
2021-12-13 20:11:09 +00:00
|
|
|
datac0_bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(datac0_freedv)/8)
|
|
|
|
datac0_n_max_modem_samples = codec2.api.freedv_get_n_max_modem_samples(datac0_freedv)
|
|
|
|
datac0_bytes_out = create_string_buffer(datac0_bytes_per_frame * 2)
|
|
|
|
codec2.api.freedv_set_frames_per_burst(datac0_freedv,N_FRAMES_PER_BURST)
|
2021-12-15 06:12:38 +00:00
|
|
|
datac0_buffer = codec2.audio_buffer(2*AUDIO_FRAMES_PER_BUFFER)
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
datac1_freedv = cast(codec2.api.freedv_open(codec2.api.FREEDV_MODE_DATAC1), c_void_p)
|
2021-12-13 20:11:09 +00:00
|
|
|
datac1_bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(datac1_freedv)/8)
|
|
|
|
datac1_n_max_modem_samples = codec2.api.freedv_get_n_max_modem_samples(datac1_freedv)
|
|
|
|
datac1_bytes_out = create_string_buffer(datac1_bytes_per_frame * 2)
|
|
|
|
codec2.api.freedv_set_frames_per_burst(datac1_freedv,N_FRAMES_PER_BURST)
|
2021-12-15 06:12:38 +00:00
|
|
|
datac1_buffer = codec2.audio_buffer(2*AUDIO_FRAMES_PER_BUFFER)
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
datac3_freedv = cast(codec2.api.freedv_open(codec2.api.FREEDV_MODE_DATAC3), c_void_p)
|
2021-12-13 20:11:09 +00:00
|
|
|
datac3_bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(datac3_freedv)/8)
|
|
|
|
datac3_n_max_modem_samples = codec2.api.freedv_get_n_max_modem_samples(datac3_freedv)
|
|
|
|
datac3_bytes_out = create_string_buffer(datac3_bytes_per_frame * 2)
|
|
|
|
codec2.api.freedv_set_frames_per_burst(datac3_freedv,N_FRAMES_PER_BURST)
|
2021-12-15 06:12:38 +00:00
|
|
|
datac3_buffer = codec2.audio_buffer(2*AUDIO_FRAMES_PER_BUFFER)
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-16 11:10:30 +00:00
|
|
|
resampler = codec2.resampler()
|
|
|
|
|
2021-12-13 20:11:09 +00:00
|
|
|
# check if we want to use an audio device then do an pyaudio init
|
2021-12-14 03:54:45 +00:00
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
2021-12-13 20:11:09 +00:00
|
|
|
p = pyaudio.PyAudio()
|
2021-12-14 22:07:01 +00:00
|
|
|
# auto search for loopback devices
|
|
|
|
if AUDIO_INPUT_DEVICE == -2:
|
|
|
|
loopback_list = []
|
|
|
|
for dev in range(0,p.get_device_count()):
|
|
|
|
if 'Loopback: PCM' in p.get_device_info_by_index(dev)["name"]:
|
|
|
|
loopback_list.append(dev)
|
|
|
|
if len(loopback_list) >= 2:
|
|
|
|
AUDIO_INPUT_DEVICE = loopback_list[0] #0 = RX 1 = TX
|
|
|
|
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
|
|
|
else:
|
|
|
|
quit()
|
|
|
|
|
|
|
|
print(f"AUDIO INPUT DEVICE: {AUDIO_INPUT_DEVICE} DEVICE: {p.get_device_info_by_index(AUDIO_INPUT_DEVICE)['name']} AUDIO SAMPLE RATE: {AUDIO_SAMPLE_RATE_RX}", file=sys.stderr)
|
2021-12-13 20:11:09 +00:00
|
|
|
stream_rx = p.open(format=pyaudio.paInt16,
|
|
|
|
channels=1,
|
2021-12-14 03:54:45 +00:00
|
|
|
rate=AUDIO_SAMPLE_RATE_RX,
|
|
|
|
frames_per_buffer=AUDIO_FRAMES_PER_BUFFER,
|
2021-12-13 20:11:09 +00:00
|
|
|
input=True,
|
2021-12-14 03:54:45 +00:00
|
|
|
input_device_index=AUDIO_INPUT_DEVICE
|
2021-12-13 20:11:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-14 03:54:45 +00:00
|
|
|
timeout = time.time() + TIMEOUT
|
2021-12-15 04:35:30 +00:00
|
|
|
print(time.time(),TIMEOUT, timeout)
|
2021-12-13 20:11:09 +00:00
|
|
|
receive = True
|
2021-12-16 11:10:30 +00:00
|
|
|
nread_exceptions = 0
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
# initial nin values
|
|
|
|
datac0_nin = codec2.api.freedv_nin(datac0_freedv)
|
|
|
|
datac1_nin = codec2.api.freedv_nin(datac1_freedv)
|
|
|
|
datac3_nin = codec2.api.freedv_nin(datac3_freedv)
|
2021-12-14 03:54:45 +00:00
|
|
|
|
2021-12-15 06:12:38 +00:00
|
|
|
def print_stats():
|
|
|
|
if DEBUGGING_MODE:
|
|
|
|
datac0_rxstatus = codec2.api.freedv_get_rx_status(datac0_freedv)
|
|
|
|
datac0_rxstatus = codec2.api.rx_sync_flags_to_text[datac0_rxstatus]
|
|
|
|
|
|
|
|
datac1_rxstatus = codec2.api.freedv_get_rx_status(datac1_freedv)
|
|
|
|
datac1_rxstatus = codec2.api.rx_sync_flags_to_text[datac1_rxstatus]
|
|
|
|
|
|
|
|
datac3_rxstatus = codec2.api.freedv_get_rx_status(datac3_freedv)
|
|
|
|
datac3_rxstatus = codec2.api.rx_sync_flags_to_text[datac3_rxstatus]
|
|
|
|
|
|
|
|
print("NIN0: %5d RX_STATUS0: %4s NIN1: %5d RX_STATUS1: %4s NIN3: %5d RX_STATUS3: %4s" % \
|
|
|
|
(datac0_nin, datac0_rxstatus, datac1_nin, datac1_rxstatus, datac3_nin, datac3_rxstatus),
|
|
|
|
file=sys.stderr)
|
|
|
|
|
2021-12-13 20:11:09 +00:00
|
|
|
while receive and time.time() < timeout:
|
2021-12-15 04:35:30 +00:00
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
2021-12-16 11:10:30 +00:00
|
|
|
try:
|
|
|
|
data_in48k = stream_rx.read(AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = True)
|
|
|
|
except OSError as err:
|
|
|
|
print(err, file=sys.stderr)
|
2021-12-16 19:46:40 +00:00
|
|
|
if str(err).find("Input overflowed") != -1:
|
2021-12-16 11:10:30 +00:00
|
|
|
nread_exceptions += 1
|
2021-12-16 19:46:40 +00:00
|
|
|
if str(err).find("Stream closed") != -1:
|
|
|
|
print("Ending....")
|
2021-12-16 11:10:30 +00:00
|
|
|
receive = False
|
2021-12-13 20:11:09 +00:00
|
|
|
else:
|
2021-12-16 11:10:30 +00:00
|
|
|
data_in48k = sys.stdin.buffer.read(AUDIO_FRAMES_PER_BUFFER*2)
|
2021-12-14 22:07:01 +00:00
|
|
|
|
2021-12-16 11:10:30 +00:00
|
|
|
# insert samples in buffer
|
|
|
|
x = np.frombuffer(data_in48k, dtype=np.int16)
|
|
|
|
if len(x) != AUDIO_FRAMES_PER_BUFFER:
|
2021-12-16 19:46:40 +00:00
|
|
|
print("len(x)",len(x))
|
2021-12-15 04:35:30 +00:00
|
|
|
receive = False
|
2021-12-16 11:10:30 +00:00
|
|
|
x = resampler.resample48_to_8(x)
|
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
datac0_buffer.push(x)
|
|
|
|
datac1_buffer.push(x)
|
|
|
|
datac3_buffer.push(x)
|
2021-12-15 06:12:38 +00:00
|
|
|
print_something = False
|
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
while datac0_buffer.nbuffer >= datac0_nin:
|
|
|
|
# demodulate audio
|
|
|
|
nbytes = codec2.api.freedv_rawdatarx(datac0_freedv, datac0_bytes_out, datac0_buffer.buffer.ctypes)
|
|
|
|
datac0_buffer.pop(datac0_nin)
|
|
|
|
datac0_nin = codec2.api.freedv_nin(datac0_freedv)
|
2021-12-13 20:11:09 +00:00
|
|
|
if nbytes == datac0_bytes_per_frame:
|
|
|
|
rx_total_frames_datac0 = rx_total_frames_datac0 + 1
|
|
|
|
rx_frames_datac0 = rx_frames_datac0 + 1
|
|
|
|
|
|
|
|
if rx_frames_datac0 == N_FRAMES_PER_BURST:
|
|
|
|
rx_frames_datac0 = 0
|
|
|
|
rx_bursts_datac0 = rx_bursts_datac0 + 1
|
2021-12-15 06:12:38 +00:00
|
|
|
print_stats()
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
while datac1_buffer.nbuffer >= datac1_nin:
|
|
|
|
# demodulate audio
|
|
|
|
nbytes = codec2.api.freedv_rawdatarx(datac1_freedv, datac1_bytes_out, datac1_buffer.buffer.ctypes)
|
|
|
|
datac1_buffer.pop(datac1_nin)
|
|
|
|
datac1_nin = codec2.api.freedv_nin(datac1_freedv)
|
2021-12-13 20:11:09 +00:00
|
|
|
if nbytes == datac1_bytes_per_frame:
|
|
|
|
rx_total_frames_datac1 = rx_total_frames_datac1 + 1
|
|
|
|
rx_frames_datac1 = rx_frames_datac1 + 1
|
|
|
|
|
|
|
|
if rx_frames_datac1 == N_FRAMES_PER_BURST:
|
|
|
|
rx_frames_datac1 = 0
|
|
|
|
rx_bursts_datac1 = rx_bursts_datac1 + 1
|
2021-12-15 06:12:38 +00:00
|
|
|
print_stats()
|
2021-12-13 20:11:09 +00:00
|
|
|
|
2021-12-15 04:35:30 +00:00
|
|
|
while datac3_buffer.nbuffer >= datac3_nin:
|
|
|
|
# demodulate audio
|
|
|
|
nbytes = codec2.api.freedv_rawdatarx(datac3_freedv, datac3_bytes_out, datac3_buffer.buffer.ctypes)
|
|
|
|
datac3_buffer.pop(datac3_nin)
|
|
|
|
datac3_nin = codec2.api.freedv_nin(datac3_freedv)
|
2021-12-13 20:11:09 +00:00
|
|
|
if nbytes == datac3_bytes_per_frame:
|
|
|
|
rx_total_frames_datac3 = rx_total_frames_datac3 + 1
|
|
|
|
rx_frames_datac3 = rx_frames_datac3 + 1
|
|
|
|
|
|
|
|
if rx_frames_datac3 == N_FRAMES_PER_BURST:
|
|
|
|
rx_frames_datac3 = 0
|
|
|
|
rx_bursts_datac3 = rx_bursts_datac3 + 1
|
2021-12-15 06:12:38 +00:00
|
|
|
print_stats()
|
2021-12-14 22:07:01 +00:00
|
|
|
|
2021-12-13 20:11:09 +00:00
|
|
|
if rx_bursts_datac0 == N_BURSTS and rx_bursts_datac1 == N_BURSTS and rx_bursts_datac3 == N_BURSTS:
|
|
|
|
receive = False
|
|
|
|
|
2021-12-16 11:10:30 +00:00
|
|
|
if nread_exceptions:
|
|
|
|
print("nread_exceptions %d - receive audio lost! Consider increasing Pyaudio frames_per_buffer..." % \
|
|
|
|
nread_exceptions, file=sys.stderr)
|
2021-12-13 20:11:09 +00:00
|
|
|
# INFO IF WE REACHED TIMEOUT
|
|
|
|
if time.time() > timeout:
|
|
|
|
print(f"TIMEOUT REACHED", file=sys.stderr)
|
|
|
|
|
|
|
|
print(f"DATAC0: {rx_bursts_datac0}/{rx_total_frames_datac0} DATAC1: {rx_bursts_datac1}/{rx_total_frames_datac1} DATAC3: {rx_bursts_datac3}/{rx_total_frames_datac3}", file=sys.stderr)
|
2021-12-14 22:07:01 +00:00
|
|
|
|
2021-12-16 19:46:40 +00:00
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
|
|
|
stream_rx.close()
|
|
|
|
p.terminate()
|