2021-12-11 21:39:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Wed Dec 23 07:04:24 2020
|
|
|
|
|
|
|
|
@author: DJ2LS
|
|
|
|
"""
|
|
|
|
|
|
|
|
import ctypes
|
|
|
|
from ctypes import *
|
|
|
|
import pathlib
|
2022-04-30 10:27:14 +00:00
|
|
|
import sounddevice as sd
|
2021-12-11 21:39:31 +00:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import sys
|
|
|
|
import argparse
|
2021-12-15 03:38:53 +00:00
|
|
|
import numpy as np
|
2021-12-20 10:10:41 +00:00
|
|
|
sys.path.insert(0,'..')
|
2021-12-19 14:14:52 +00:00
|
|
|
from tnc import codec2
|
|
|
|
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
#--------------------------------------------GET PARAMETER INPUTS
|
|
|
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
2021-12-15 00:42:31 +00:00
|
|
|
parser.add_argument('--bursts', dest="N_BURSTS", default=1, type=int)
|
|
|
|
parser.add_argument('--framesperburst', dest="N_FRAMES_PER_BURST", default=1, type=int)
|
2021-12-13 18:00:38 +00:00
|
|
|
parser.add_argument('--mode', dest="FREEDV_MODE", type=str, choices=['datac0', 'datac1', 'datac3'])
|
2021-12-14 22:56:55 +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")
|
2021-12-11 21:39:31 +00:00
|
|
|
parser.add_argument('--debug', dest="DEBUGGING_MODE", action="store_true")
|
2021-12-14 22:07:01 +00:00
|
|
|
parser.add_argument('--timeout', dest="TIMEOUT", default=10, type=int, help="Timeout (seconds) before test ends")
|
2021-12-14 02:05:19 +00:00
|
|
|
parser.add_argument('--list', dest="LIST", action="store_true", help="list audio devices by number and exit")
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-12-14 02:05:19 +00:00
|
|
|
if args.LIST:
|
2022-04-30 10:27:14 +00:00
|
|
|
|
|
|
|
devices = sd.query_devices(device=None, kind=None)
|
|
|
|
index = 0
|
|
|
|
for device in devices:
|
|
|
|
print(f"{index} {device['name']}")
|
|
|
|
index += 1
|
|
|
|
sd._terminate()
|
2021-12-14 02:05:19 +00:00
|
|
|
quit()
|
|
|
|
|
2021-12-11 21:39:31 +00:00
|
|
|
N_BURSTS = args.N_BURSTS
|
|
|
|
N_FRAMES_PER_BURST = args.N_FRAMES_PER_BURST
|
2021-12-14 02:05:19 +00:00
|
|
|
AUDIO_INPUT_DEVICE = args.AUDIO_INPUT_DEVICE
|
2021-12-13 18:00:38 +00:00
|
|
|
MODE = codec2.FREEDV_MODE[args.FREEDV_MODE].value
|
2021-12-11 21:39:31 +00:00
|
|
|
DEBUGGING_MODE = args.DEBUGGING_MODE
|
2021-12-14 22:07:01 +00:00
|
|
|
TIMEOUT = args.TIMEOUT
|
2021-12-12 12:54:23 +00:00
|
|
|
|
|
|
|
# AUDIO PARAMETERS
|
2021-12-16 07:22:42 +00:00
|
|
|
AUDIO_FRAMES_PER_BUFFER = 2400*2 # <- consider increasing if you get nread_exceptions > 0
|
2021-12-13 18:00:38 +00:00
|
|
|
MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000
|
2021-12-16 01:26:39 +00:00
|
|
|
AUDIO_SAMPLE_RATE_RX = 48000
|
|
|
|
|
|
|
|
# make sure our resampler will work
|
2021-12-16 01:40:03 +00:00
|
|
|
assert (AUDIO_SAMPLE_RATE_RX / MODEM_SAMPLE_RATE) == codec2.api.FDMDV_OS_48
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 12:54:23 +00:00
|
|
|
# check if we want to use an audio device then do an pyaudio init
|
2021-12-14 03:12:47 +00:00
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
2021-12-14 22:07:01 +00:00
|
|
|
# auto search for loopback devices
|
|
|
|
if AUDIO_INPUT_DEVICE == -2:
|
|
|
|
loopback_list = []
|
2022-04-30 10:27:14 +00:00
|
|
|
|
|
|
|
devices = sd.query_devices(device=None, kind=None)
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
if 'Loopback: PCM' in device['name']:
|
|
|
|
print(index)
|
|
|
|
loopback_list.append(index)
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
if len(loopback_list) >= 1:
|
2021-12-14 22:07:01 +00:00
|
|
|
AUDIO_INPUT_DEVICE = loopback_list[0] #0 = RX 1 = TX
|
2022-04-30 10:27:14 +00:00
|
|
|
print(f"loopback_list tx: {loopback_list}", file=sys.stderr)
|
2021-12-14 22:07:01 +00:00
|
|
|
else:
|
2022-04-30 10:27:14 +00:00
|
|
|
print("not enough audio loopback devices ready...")
|
|
|
|
print("you should wait about 30 seconds...")
|
|
|
|
|
|
|
|
sd._terminate()
|
|
|
|
quit()
|
|
|
|
print(f"AUDIO INPUT DEVICE: {AUDIO_INPUT_DEVICE}", file=sys.stderr)
|
|
|
|
|
|
|
|
# audio stream init
|
|
|
|
stream_rx = sd.RawStream(channels=1, dtype='int16', device=AUDIO_INPUT_DEVICE, samplerate = AUDIO_SAMPLE_RATE_RX, blocksize=4800)
|
|
|
|
stream_rx.start()
|
|
|
|
|
2021-12-12 12:54:23 +00:00
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2021-12-14 02:05:19 +00:00
|
|
|
# DATA CHANNEL INITIALISATION
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 12:54:23 +00:00
|
|
|
# open codec2 instance
|
2021-12-12 20:52:03 +00:00
|
|
|
freedv = cast(codec2.api.freedv_open(MODE), c_void_p)
|
2021-12-12 12:54:23 +00:00
|
|
|
|
|
|
|
# get number of bytes per frame for mode
|
2021-12-12 20:52:03 +00:00
|
|
|
bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(freedv)/8)
|
2021-12-12 12:54:23 +00:00
|
|
|
payload_bytes_per_frame = bytes_per_frame -2
|
|
|
|
|
2021-12-12 20:52:03 +00:00
|
|
|
n_max_modem_samples = codec2.api.freedv_get_n_max_modem_samples(freedv)
|
2021-12-20 14:57:32 +00:00
|
|
|
bytes_out = create_string_buffer(bytes_per_frame)
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 20:52:03 +00:00
|
|
|
codec2.api.freedv_set_frames_per_burst(freedv,N_FRAMES_PER_BURST)
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
total_n_bytes = 0
|
|
|
|
rx_total_frames = 0
|
|
|
|
rx_frames = 0
|
|
|
|
rx_bursts = 0
|
2021-12-14 20:30:20 +00:00
|
|
|
rx_errors = 0
|
2021-12-16 07:22:42 +00:00
|
|
|
nread_exceptions = 0
|
2021-12-14 22:07:01 +00:00
|
|
|
timeout = time.time() + TIMEOUT
|
2021-12-11 21:39:31 +00:00
|
|
|
receive = True
|
2021-12-16 07:22:42 +00:00
|
|
|
audio_buffer = codec2.audio_buffer(AUDIO_FRAMES_PER_BUFFER*2)
|
2021-12-16 03:24:59 +00:00
|
|
|
resampler = codec2.resampler()
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2022-01-10 09:51:26 +00:00
|
|
|
# time meassurement
|
|
|
|
time_start = 0
|
|
|
|
time_end = 0
|
|
|
|
|
2021-12-16 07:22:42 +00:00
|
|
|
# Copy received 48 kHz to a file. Listen to this file with:
|
|
|
|
# aplay -r 48000 -f S16_LE rx48.raw
|
|
|
|
# Corruption of this file is a good way to detect audio card issues
|
|
|
|
frx = open("rx48.raw", mode='wb')
|
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
# initial number of samples we need
|
|
|
|
nin = codec2.api.freedv_nin(freedv)
|
|
|
|
while receive and time.time() < timeout:
|
2021-12-16 07:22:42 +00:00
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
|
|
|
try:
|
2022-04-30 10:27:14 +00:00
|
|
|
#data_in48k = stream_rx.read(AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = True)
|
|
|
|
data_in48k, overflowed = stream_rx.read(AUDIO_FRAMES_PER_BUFFER)
|
2021-12-16 07:22:42 +00:00
|
|
|
except OSError as err:
|
|
|
|
print(err, file=sys.stderr)
|
2022-04-30 10:27:14 +00:00
|
|
|
#if str(err).find("Input overflowed") != -1:
|
|
|
|
# nread_exceptions += 1
|
|
|
|
#if str(err).find("Stream closed") != -1:
|
|
|
|
# print("Ending...")
|
|
|
|
# receive = False
|
2021-12-11 21:39:31 +00:00
|
|
|
else:
|
2021-12-16 01:26:39 +00:00
|
|
|
data_in48k = sys.stdin.buffer.read(AUDIO_FRAMES_PER_BUFFER*2)
|
2021-12-16 07:22:42 +00:00
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
# insert samples in buffer
|
2021-12-16 01:40:03 +00:00
|
|
|
x = np.frombuffer(data_in48k, dtype=np.int16)
|
2022-04-30 10:27:14 +00:00
|
|
|
#print(x)
|
|
|
|
#x = data_in48k
|
2021-12-16 11:10:30 +00:00
|
|
|
x.tofile(frx)
|
2021-12-16 01:40:03 +00:00
|
|
|
if len(x) != AUDIO_FRAMES_PER_BUFFER:
|
2021-12-16 03:24:59 +00:00
|
|
|
receive = False
|
|
|
|
x = resampler.resample48_to_8(x)
|
2021-12-15 04:08:29 +00:00
|
|
|
audio_buffer.push(x)
|
2021-12-15 03:38:53 +00:00
|
|
|
|
|
|
|
# when we have enough samples call FreeDV Rx
|
2021-12-15 04:08:29 +00:00
|
|
|
while audio_buffer.nbuffer >= nin:
|
2022-01-10 09:51:26 +00:00
|
|
|
# start time measurement
|
|
|
|
time_start = time.time()
|
2021-12-15 04:35:30 +00:00
|
|
|
# demodulate audio
|
2021-12-15 04:08:29 +00:00
|
|
|
nbytes = codec2.api.freedv_rawdatarx(freedv, bytes_out, audio_buffer.buffer.ctypes)
|
2022-01-10 09:51:26 +00:00
|
|
|
time_end = time.time()
|
|
|
|
|
2021-12-15 04:08:29 +00:00
|
|
|
audio_buffer.pop(nin)
|
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
# call me on every loop!
|
|
|
|
nin = codec2.api.freedv_nin(freedv)
|
|
|
|
|
2021-12-15 00:42:31 +00:00
|
|
|
rx_status = codec2.api.freedv_get_rx_status(freedv)
|
|
|
|
if rx_status & codec2.api.FREEDV_RX_BIT_ERRORS:
|
|
|
|
rx_errors = rx_errors + 1
|
|
|
|
if DEBUGGING_MODE:
|
2021-12-15 03:38:53 +00:00
|
|
|
rx_status = codec2.api.rx_sync_flags_to_text[rx_status]
|
2022-01-10 09:51:26 +00:00
|
|
|
time_needed = time_end - time_start
|
|
|
|
|
|
|
|
print("nin: %5d rx_status: %4s naudio_buffer: %4d time: %4s" % \
|
|
|
|
(nin,rx_status,audio_buffer.nbuffer, time_needed), file=sys.stderr)
|
2021-12-14 22:07:01 +00:00
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
if nbytes:
|
|
|
|
total_n_bytes = total_n_bytes + nbytes
|
2021-12-14 22:07:01 +00:00
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
if nbytes == bytes_per_frame:
|
|
|
|
rx_total_frames = rx_total_frames + 1
|
|
|
|
rx_frames = rx_frames + 1
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
if rx_frames == N_FRAMES_PER_BURST:
|
|
|
|
rx_frames = 0
|
|
|
|
rx_bursts = rx_bursts + 1
|
2021-12-15 00:42:31 +00:00
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
if rx_bursts == N_BURSTS:
|
2021-12-16 07:22:42 +00:00
|
|
|
receive = False
|
|
|
|
|
2021-12-15 03:38:53 +00:00
|
|
|
if time.time() >= timeout:
|
|
|
|
print("TIMEOUT REACHED")
|
2021-12-16 07:22:42 +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-14 20:30:20 +00:00
|
|
|
print(f"RECEIVED BURSTS: {rx_bursts} RECEIVED FRAMES: {rx_total_frames} RX_ERRORS: {rx_errors}", file=sys.stderr)
|
2021-12-16 07:22:42 +00:00
|
|
|
frx.close()
|
2021-12-12 12:54:23 +00:00
|
|
|
|
2022-04-30 10:27:14 +00:00
|
|
|
|
|
|
|
# and at last check if we had an opened audio instance and close it
|
|
|
|
if AUDIO_INPUT_DEVICE != -1:
|
|
|
|
sd._terminate()
|
|
|
|
|
2022-04-15 09:29:40 +00:00
|
|
|
|