2021-12-11 21:39:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
import ctypes
|
|
|
|
from ctypes import *
|
|
|
|
import pathlib
|
|
|
|
import pyaudio
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import audioop
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# GET PARAMETER INPUTS
|
2021-12-11 21:39:31 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
|
|
|
parser.add_argument('--bursts', dest="N_BURSTS", default=0, type=int)
|
2021-12-12 11:09:13 +00:00
|
|
|
parser.add_argument('--framesperburst', dest="N_FRAMES_PER_BURST", default=0, type=int)
|
2021-12-11 21:39:31 +00:00
|
|
|
parser.add_argument('--delay', dest="DELAY_BETWEEN_BURSTS", default=0, type=int)
|
2021-12-12 12:54:23 +00:00
|
|
|
parser.add_argument('--mode', dest="FREEDV_MODE", default=14, type=int)
|
2021-12-11 21:39:31 +00:00
|
|
|
parser.add_argument('--audiooutput', dest="AUDIO_OUTPUT", default=0, type=int)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
N_BURSTS = args.N_BURSTS
|
|
|
|
N_FRAMES_PER_BURST = args.N_FRAMES_PER_BURST
|
|
|
|
DELAY_BETWEEN_BURSTS = args.DELAY_BETWEEN_BURSTS/1000
|
|
|
|
AUDIO_OUTPUT_DEVICE = args.AUDIO_OUTPUT
|
2021-12-12 11:09:13 +00:00
|
|
|
MODE = args.FREEDV_MODE
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 12:54:23 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# AUDIO PARAMETERS
|
2021-12-11 21:39:31 +00:00
|
|
|
AUDIO_FRAMES_PER_BUFFER = 2048
|
|
|
|
MODEM_SAMPLE_RATE = 8000
|
2021-12-12 11:09:13 +00:00
|
|
|
AUDIO_SAMPLE_RATE_TX = 48000
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# check if we want to use an audio device then do an pyaudio init
|
2021-12-12 12:54:23 +00:00
|
|
|
if AUDIO_OUTPUT_DEVICE != 0:
|
2021-12-12 11:09:13 +00:00
|
|
|
# pyaudio init
|
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
stream_tx = p.open(format=pyaudio.paInt16,
|
|
|
|
channels=1,
|
|
|
|
rate=AUDIO_SAMPLE_RATE_TX,
|
|
|
|
frames_per_buffer=AUDIO_FRAMES_PER_BUFFER, #n_nom_modem_samples
|
|
|
|
output=True,
|
|
|
|
output_device_index=AUDIO_OUTPUT_DEVICE,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# data binary string
|
2021-12-11 21:39:31 +00:00
|
|
|
data_out = b'HELLO WORLD!'
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# LOAD FREEDV
|
2021-12-11 21:39:31 +00:00
|
|
|
libname = "libcodec2.so"
|
|
|
|
c_lib = ctypes.CDLL(libname)
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# ctypes function init
|
|
|
|
|
|
|
|
c_lib.freedv_open.argype = [c_int]
|
|
|
|
c_lib.freedv_open.restype = c_void_p
|
|
|
|
|
|
|
|
c_lib.freedv_get_bits_per_modem_frame.argtype = [c_void_p]
|
|
|
|
c_lib.freedv_get_bits_per_modem_frame.restype = c_int
|
|
|
|
|
|
|
|
c_lib.freedv_get_n_tx_preamble_modem_samples.argtype = [c_void_p]
|
|
|
|
c_lib.freedv_get_n_tx_preamble_modem_samples.restype = c_int
|
|
|
|
|
|
|
|
c_lib.freedv_get_n_tx_postamble_modem_samples.argtype = [c_void_p]
|
|
|
|
c_lib.freedv_get_n_tx_postamble_modem_samples.restype = c_int
|
|
|
|
|
|
|
|
c_lib.freedv_gen_crc16.argtype = [c_void_p, c_int]
|
|
|
|
c_lib.freedv_gen_crc16.restype = c_void_p
|
|
|
|
|
|
|
|
c_lib.freedv_nin.argtype = [c_void_p]
|
|
|
|
c_lib.freedv_nin.restype = c_int
|
|
|
|
|
|
|
|
c_lib.freedv_rawdatatx.argtype = [c_void_p, c_char_p, c_char_p]
|
|
|
|
c_lib.freedv_rawdatatx.restype = c_int
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# open codec2 instance
|
|
|
|
freedv = cast(c_lib.freedv_open(MODE), c_void_p)
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# get number of bytes per frame for mode
|
2021-12-11 21:39:31 +00:00
|
|
|
bytes_per_frame = int(c_lib.freedv_get_bits_per_modem_frame(freedv)/8)
|
2021-12-11 22:50:53 +00:00
|
|
|
payload_bytes_per_frame = bytes_per_frame -2
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# init buffer for data
|
|
|
|
n_tx_modem_samples = c_lib.freedv_get_n_tx_modem_samples(freedv)
|
|
|
|
mod_out = create_string_buffer(n_tx_modem_samples * 2)
|
|
|
|
|
|
|
|
# init buffer for preample
|
|
|
|
n_tx_preamble_modem_samples = c_lib.freedv_get_n_tx_preamble_modem_samples(freedv)
|
|
|
|
mod_out_preamble = create_string_buffer(n_tx_preamble_modem_samples * 2)
|
2021-12-11 22:50:53 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# init buffer for postamble
|
|
|
|
n_tx_postamble_modem_samples = c_lib.freedv_get_n_tx_postamble_modem_samples(freedv)
|
|
|
|
mod_out_postamble = create_string_buffer(n_tx_postamble_modem_samples * 2)
|
2021-12-11 22:50:53 +00:00
|
|
|
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# create buffer for data
|
2021-12-11 22:50:53 +00:00
|
|
|
buffer = bytearray(payload_bytes_per_frame) # use this if CRC16 checksum is required ( DATA1-3)
|
2021-12-11 21:39:31 +00:00
|
|
|
buffer[:len(data_out)] = data_out # set buffersize to length of data which will be send
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# create crc for data frame - we are using the crc function shipped with codec2 to avoid
|
|
|
|
# crc algorithm incompatibilities
|
2021-12-11 22:50:53 +00:00
|
|
|
crc = ctypes.c_ushort(c_lib.freedv_gen_crc16(bytes(buffer), payload_bytes_per_frame)) # generate CRC16
|
2021-12-11 21:39:31 +00:00
|
|
|
crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string
|
|
|
|
buffer += crc # append crc16 to buffer
|
|
|
|
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
print(f"TOTAL BURSTS: {N_BURSTS} TOTAL FRAMES_PER_BURST: {N_FRAMES_PER_BURST}", file=sys.stderr)
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
for i in range(1,N_BURSTS+1):
|
2021-12-11 22:50:53 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# write preamble to txbuffer
|
2021-12-11 22:50:53 +00:00
|
|
|
c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
2021-12-12 11:09:13 +00:00
|
|
|
txbuffer = bytes(mod_out_preamble)
|
2021-12-11 21:39:31 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# create modulaton for N = FRAMESPERBURST and append it to txbuffer
|
|
|
|
for n in range(1,N_FRAMES_PER_BURST+1):
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
2021-12-11 22:50:53 +00:00
|
|
|
c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and save it into mod_out pointer
|
2021-12-11 21:39:31 +00:00
|
|
|
|
|
|
|
txbuffer += bytes(mod_out)
|
2021-12-12 11:09:13 +00:00
|
|
|
|
|
|
|
print(f"BURST: {i}/{N_BURSTS} FRAME: {n}/{N_FRAMES_PER_BURST}", file=sys.stderr)
|
|
|
|
|
|
|
|
# append postamble to txbuffer
|
2021-12-11 22:50:53 +00:00
|
|
|
c_lib.freedv_rawdatapostambletx(freedv, mod_out_postamble)
|
|
|
|
txbuffer += bytes(mod_out_postamble)
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# append a delay between bursts as audio silence
|
2021-12-11 23:15:23 +00:00
|
|
|
samples_delay = int(MODEM_SAMPLE_RATE*DELAY_BETWEEN_BURSTS)
|
2021-12-12 11:09:13 +00:00
|
|
|
mod_out_silence = create_string_buffer(samples_delay)
|
2021-12-11 22:50:53 +00:00
|
|
|
txbuffer += bytes(mod_out_silence)
|
2021-12-12 11:09:13 +00:00
|
|
|
print(f"samples_delay: {samples_delay} DELAY_BETWEEN_BURSTS: {DELAY_BETWEEN_BURSTS}", file=sys.stderr)
|
2021-12-11 22:50:53 +00:00
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
# check if we want to use an audio device or stdout
|
2021-12-12 12:54:23 +00:00
|
|
|
if AUDIO_OUTPUT_DEVICE != 0:
|
2021-12-12 11:09:13 +00:00
|
|
|
|
|
|
|
# sample rate conversion from 8000Hz to 48000Hz
|
2021-12-11 21:39:31 +00:00
|
|
|
audio = audioop.ratecv(txbuffer,2,1,MODEM_SAMPLE_RATE, AUDIO_SAMPLE_RATE_TX, None)
|
|
|
|
stream_tx.write(audio[0])
|
2021-12-12 11:09:13 +00:00
|
|
|
|
2021-12-11 21:39:31 +00:00
|
|
|
else:
|
2021-12-12 11:09:13 +00:00
|
|
|
# print data to terminal for piping the output to other programs
|
|
|
|
sys.stdout.buffer.write(txbuffer)
|
2021-12-11 21:39:31 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
2021-12-12 11:09:13 +00:00
|
|
|
|
|
|
|
# and at last check if we had an openend pyaudio instance and close it
|
2021-12-12 12:54:23 +00:00
|
|
|
if AUDIO_OUTPUT_DEVICE != 0:
|
2021-12-11 21:39:31 +00:00
|
|
|
stream_tx.close()
|
|
|
|
p.terminate()
|