From a6d60dea1c8d71532f2c63914d7c591a2c5f54e0 Mon Sep 17 00:00:00 2001 From: dj2ls Date: Sun, 19 Dec 2021 15:04:35 +0100 Subject: [PATCH] initial callback test crashes at the moment within resambler --- CMakeLists.txt | 10 + .../test_callback_rx.py | 178 ++++++++++++++++++ test/001_highsnr_stdio_audio/test_virtual3.sh | 23 +++ 3 files changed, 211 insertions(+) create mode 100644 test/001_highsnr_stdio_audio/test_callback_rx.py create mode 100755 test/001_highsnr_stdio_audio/test_virtual3.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 94967586..56979c0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,5 +89,15 @@ add_test(NAME 001_highsnr_virtual3_P_P_MM cd ${CMAKE_CURRENT_SOURCE_DIR}/test/001_highsnr_stdio_audio; ./test_virtual_mm.sh") set_tests_properties(001_highsnr_virtual3_P_P_MM PROPERTIES PASS_REGULAR_EXPRESSION "DATAC0: 2/4 DATAC1: 2/4 DATAC3: 2/4") + +# let Python do audio I/O via pyaudio callback mode +add_test(NAME 001_highsnr_virtual4_P_P_SM_CB + COMMAND sh -c "export LD_LIBRARY_PATH=${CODEC2_BUILD_DIR}/src; + PATH=$PATH:${CODEC2_BUILD_DIR}/src; + cd ${CMAKE_CURRENT_SOURCE_DIR}/test/001_highsnr_stdio_audio; + ./test_virtual3.sh") + set_tests_properties(001_highsnr_virtual4_P_P_SM_CB PROPERTIES PASS_REGULAR_EXPRESSION "RECEIVED BURSTS: 3 RECEIVED FRAMES: 6 RX_ERRORS: 0") + + endif() diff --git a/test/001_highsnr_stdio_audio/test_callback_rx.py b/test/001_highsnr_stdio_audio/test_callback_rx.py new file mode 100644 index 00000000..9c61d224 --- /dev/null +++ b/test/001_highsnr_stdio_audio/test_callback_rx.py @@ -0,0 +1,178 @@ +#!/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 +import pyaudio +import sys +import logging +import time +import threading +import sys +import argparse +import numpy as np +sys.path.insert(0,'..') +import codec2 + +#--------------------------------------------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('--mode', dest="FREEDV_MODE", type=str, choices=['datac0', 'datac1', 'datac3']) +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", default=10, 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_args() + +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() + +N_BURSTS = args.N_BURSTS +N_FRAMES_PER_BURST = args.N_FRAMES_PER_BURST +AUDIO_INPUT_DEVICE = args.AUDIO_INPUT_DEVICE +MODE = codec2.FREEDV_MODE[args.FREEDV_MODE].value +DEBUGGING_MODE = args.DEBUGGING_MODE +TIMEOUT = args.TIMEOUT + +# AUDIO PARAMETERS +AUDIO_FRAMES_PER_BUFFER = 2400*2 # <- consider increasing if you get nread_exceptions > 0 +MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000 +AUDIO_SAMPLE_RATE_RX = 48000 + +# make sure our resampler will work +assert (AUDIO_SAMPLE_RATE_RX / MODEM_SAMPLE_RATE) == codec2.api.FDMDV_OS_48 + + +# ------------------------------------------------ PYAUDIO CALLBACK +def callback(data_in48k, frame_count, time_info, status): + x = np.frombuffer(data_in48k, dtype=np.int16) + x.tofile(frx) + x = resampler.resample48_to_8(x) + audio_buffer.push(x) + return (None, pyaudio.paContinue) + + +# check if we want to use an audio device then do an pyaudio init +if AUDIO_INPUT_DEVICE != -1: + p = pyaudio.PyAudio() + # 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) + + stream_rx = p.open(format=pyaudio.paInt16, + channels=1, + rate=AUDIO_SAMPLE_RATE_RX, + frames_per_buffer=AUDIO_FRAMES_PER_BUFFER, + input=True, + output=False, + input_device_index=AUDIO_INPUT_DEVICE, + stream_callback=callback + ) + try: + print(f"starting pyaudio callback", file=sys.stderr) + stream_rx.start_stream() + except Exception as e: + print(f"pyAudio error: {e}", file=sys.stderr) +# ---------------------------------------------------------------- + +# DATA CHANNEL INITIALISATION + +# open codec2 instance +freedv = cast(codec2.api.freedv_open(MODE), c_void_p) + +# get number of bytes per frame for mode +bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(freedv)/8) +payload_bytes_per_frame = bytes_per_frame -2 + +n_max_modem_samples = codec2.api.freedv_get_n_max_modem_samples(freedv) +bytes_out = create_string_buffer(bytes_per_frame * 2) + +codec2.api.freedv_set_frames_per_burst(freedv,N_FRAMES_PER_BURST) + +total_n_bytes = 0 +rx_total_frames = 0 +rx_frames = 0 +rx_bursts = 0 +rx_errors = 0 +nread_exceptions = 0 +timeout = time.time() + TIMEOUT +receive = True +audio_buffer = codec2.audio_buffer(AUDIO_FRAMES_PER_BUFFER*2) +resampler = codec2.resampler() + +# 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 +frx = open("rx48_callback.raw", mode='wb') + +# initial number of samples we need +nin = codec2.api.freedv_nin(freedv) +while receive and time.time() < timeout: + + # when we have enough samples call FreeDV Rx + while audio_buffer.nbuffer >= nin: + + # demodulate audio + nbytes = codec2.api.freedv_rawdatarx(freedv, bytes_out, audio_buffer.buffer.ctypes) + audio_buffer.pop(nin) + + # call me on every loop! + nin = codec2.api.freedv_nin(freedv) + + 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: + rx_status = codec2.api.rx_sync_flags_to_text[rx_status] + print("nin: %5d rx_status: %4s naudio_buffer: %4d" % \ + (nin,rx_status,audio_buffer.nbuffer), file=sys.stderr) + + if nbytes: + total_n_bytes = total_n_bytes + nbytes + + if nbytes == bytes_per_frame: + rx_total_frames = rx_total_frames + 1 + rx_frames = rx_frames + 1 + + if rx_frames == N_FRAMES_PER_BURST: + rx_frames = 0 + rx_bursts = rx_bursts + 1 + + if rx_bursts == N_BURSTS: + receive = False + + if time.time() >= timeout: + print("TIMEOUT REACHED") + +if nread_exceptions: + print("nread_exceptions %d - receive audio lost! Consider increasing Pyaudio frames_per_buffer..." % \ + nread_exceptions, file=sys.stderr) +print(f"RECEIVED BURSTS: {rx_bursts} RECEIVED FRAMES: {rx_total_frames} RX_ERRORS: {rx_errors}", file=sys.stderr) +frx.close() + +# cloese pyaudio instance +stream_rx.close() +p.terminate() diff --git a/test/001_highsnr_stdio_audio/test_virtual3.sh b/test/001_highsnr_stdio_audio/test_virtual3.sh new file mode 100755 index 00000000..9a4d5493 --- /dev/null +++ b/test/001_highsnr_stdio_audio/test_virtual3.sh @@ -0,0 +1,23 @@ +#!/bin/bash -x +# Run a test using the virtual sound cards, Python audio I/O + +function check_alsa_loopback { + lsmod | grep snd_aloop >> /dev/null + if [ $? -eq 1 ]; then + echo "ALSA loopback device not present. Please install with:" + echo + echo " sudo modprobe snd-aloop index=1,2 enable=1,1 pcm_substreams=1,1 id=CHAT1,CHAT2" + exit 1 + fi +} + +check_alsa_loopback + +# make sure all child processes are killed when we exit +trap 'jobs -p | xargs -r kill' EXIT + +python3 test_callback_rx.py --mode datac0 --frames 2 --bursts 3 --audiodev -2 --debug & +rx_pid=$! +#sleep 1 +python3 test_tx.py --mode datac0 --frames 2 --bursts 3 --audiodev -2 +wait ${rx_pid}