2020-12-14 11:44:13 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 13 : 45 : 58 2020
2020-12-15 12:42:46 +00:00
@author : DJ2LS
2020-12-14 11:44:13 +00:00
"""
import ctypes
from ctypes import *
import pathlib
import binascii #required for string.to_bytes() function
import sys
2020-12-15 17:22:44 +00:00
2020-12-14 11:44:13 +00:00
def main ( ) :
modem = FreeDV ( ) #Load FreeDV Class as "modem"
2020-12-15 17:11:53 +00:00
2020-12-22 11:35:49 +00:00
data = b ' HALLO ' #byte string which will be send
2020-12-22 11:58:25 +00:00
modem . Modulate ( data ) #Call Modulate function, which modulates data and prints it to the terminal
2020-12-15 14:38:15 +00:00
2020-12-14 11:44:13 +00:00
class FreeDV ( ) :
def __init__ ( self ) :
2020-12-22 11:35:49 +00:00
self . mode = 12 # define mode
2020-12-14 11:44:13 +00:00
libname = pathlib . Path ( ) . absolute ( ) / " libcodec2.so "
self . c_lib = ctypes . CDLL ( libname ) # Load FreeDV shared libary
2020-12-22 11:35:49 +00:00
self . freedv = self . c_lib . freedv_open ( self . mode ) #Set FreeDV waveform ( 10,11,12 --> DATAC1-3 )
2020-12-14 11:44:13 +00:00
self . bytes_per_frame = int ( self . c_lib . freedv_get_bits_per_modem_frame ( self . freedv ) / 8 ) #get bytes per frame from selected waveform
self . payload_per_frame = self . bytes_per_frame - 2 #get frame payload because of 2byte CRC16 checksum
2020-12-15 17:27:14 +00:00
self . n_tx_modem_samples = self . c_lib . freedv_get_n_tx_modem_samples ( self . freedv ) * 2 #get n_tx_modem_samples which defines the size of the modulation object
2020-12-14 11:44:13 +00:00
# MODULATION-OUT OBJECT
def ModulationOut ( self ) :
2020-12-15 17:11:53 +00:00
return ( c_short * self . n_tx_modem_samples ) ##
2020-12-14 11:44:13 +00:00
# Pointer for changing buffer data type
def FrameBytes ( self ) :
2020-12-15 17:27:14 +00:00
return ( c_ubyte * self . bytes_per_frame )
2020-12-14 11:44:13 +00:00
# Modulate function which returns modulated data
def Modulate ( self , data_out ) :
mod_out = self . ModulationOut ( ) ( ) # new modulation object and get pointer to it
2020-12-15 14:36:16 +00:00
data_list = [ data_out [ i : i + self . payload_per_frame ] for i in range ( 0 , len ( data_out ) , self . payload_per_frame ) ] # split incomming bytes to size of 30bytes, create a list and loop through it
data_list_length = len ( data_list )
for i in range ( data_list_length ) : # LOOP THROUGH DATA LIST
2020-12-22 11:35:49 +00:00
if self . mode < 10 : # don't generate CRC16 for modes 0 - 9
buffer = bytearray ( self . bytes_per_frame ) # use this if no CRC16 checksum is required
buffer [ : len ( data_list [ i ] ) ] = data_list [ i ] # set buffersize to length of data which will be send
if self . mode > = 10 : #generate CRC16 for modes 10-12..
2020-12-15 17:11:53 +00:00
2020-12-22 11:35:49 +00:00
buffer = bytearray ( self . payload_per_frame ) # use this if CRC16 checksum is required ( DATA1-3)
buffer [ : len ( data_list [ i ] ) ] = data_list [ i ] # set buffersize to length of data which will be send
crc = c_ushort ( self . c_lib . freedv_gen_crc16 ( bytes ( buffer ) , self . payload_per_frame ) ) # generate CRC16
crc = crc . value . to_bytes ( 2 , byteorder = ' big ' ) # convert crc to 2 byte hex string
buffer + = crc # append crc16 to buffer
2020-12-22 11:58:25 +00:00
data = self . FrameBytes ( ) . from_buffer_copy ( buffer ) #change data format from bytearray to ctypes.u_byte and copy from buffer to data
2020-12-15 17:27:14 +00:00
2020-12-15 17:22:44 +00:00
self . c_lib . freedv_rawdatatx ( self . freedv , mod_out , data ) # modulate DATA and safe it into mod_out pointer
2020-12-14 11:44:13 +00:00
2020-12-15 17:22:44 +00:00
sys . stdout . buffer . write ( mod_out ) # print data to terminal for piping the output to other programs
sys . stdout . flush ( ) # flushing stdout
2020-12-14 11:44:13 +00:00
2020-12-15 17:11:53 +00:00
main ( )