-
Notifications
You must be signed in to change notification settings - Fork 0
/
freedv.py
101 lines (70 loc) · 3.25 KB
/
freedv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from ctypes import *
import platform
class FreeDV700D:
def __init__(self):
libname = None
system = platform.system()
if system == 'Linux':
libname = './lib/libcodec2.so'
elif system == 'Windows':
libname = './lib/libcodec2.dll'
assert libname is not None
self.c_lib = CDLL(libname)
self.c_lib.freedv_open.argtype = [c_int]
self.c_lib.freedv_open.restype = c_void_p
self.c_lib.freedv_close.argtype = [c_void_p]
self.c_lib.freedv_close.restype = c_void_p
self.c_lib.freedv_get_n_max_speech_samples.argtype = [c_void_p]
self.c_lib.freedv_get_n_max_speech_samples.restype = c_int
self.c_lib.freedv_get_n_speech_samples.argtype = [c_void_p]
self.c_lib.freedv_get_n_speech_samples.restype = c_int
self.c_lib.freedv_get_n_nom_modem_samples.argtype = [c_void_p]
self.c_lib.freedv_get_n_nom_modem_samples.restype = c_int
self.c_lib.freedv_get_sync.argtype = [c_void_p]
self.c_lib.freedv_get_sync.restype = c_int
self.c_lib.freedv_get_rx_status.argtype = [c_void_p]
self.c_lib.freedv_get_rx_status.restype = c_int
self.c_lib.freedv_nin.argtype = [c_void_p]
self.c_lib.freedv_nin.restype = c_int
self.c_lib.freedv_rx.argtype = [c_void_p, c_char_p, c_char_p]
self.c_lib.freedv_rx.restype = c_int
self.c_lib.freedv_tx.argtype = [c_void_p, c_char_p, c_char_p]
self.c_lib.freedv_tx.restype = c_void_p
FREEDV_MODE_700D = 7 # from freedv_api.h
self.freedv = cast(self.c_lib.freedv_open(FREEDV_MODE_700D), c_void_p)
self.n_speech_samples = self.c_lib.freedv_get_n_speech_samples(self.freedv)
self.n_max_speech_samples = self.c_lib.freedv_get_n_max_speech_samples(self.freedv)
self.speech_out = create_string_buffer(2 * self.n_max_speech_samples)
self.n_nom_modem_samples = self.c_lib.freedv_get_n_nom_modem_samples(self.freedv)
self.mod_out = create_string_buffer(2 * self.n_nom_modem_samples)
self.analog_listen = False
def close(self):
self.c_lib.freedv_close(self.freedv)
def get_sync(self):
return self.c_lib.freedv_get_sync(self.freedv)
def get_rx_status(self):
return self.c_lib.freedv_get_rx_status(self.freedv)
def get_nin(self):
return self.c_lib.freedv_nin(self.freedv)
def get_n_speech_samples(self):
return self.n_speech_samples
def listen_to_analog(self, val: bool):
self.analog_listen = val
def rx(self, demod_in):
nin = self.get_nin()
assert len(demod_in) == nin * 2
nout = self.c_lib.freedv_rx(self.freedv, self.speech_out, demod_in)
# if self.get_rx_status() == 6 or self.analog_listen:
# return self.speech_out[:nout * 2]
#
# else:
# return b'\x00\x00'
rx_status = self.get_rx_status()
if rx_status != 0 and rx_status != 10 or self.analog_listen:
return self.speech_out[:nout * 2]
else:
return b'\x00\x00'
def tx(self, speech_in):
assert len(speech_in) == self.get_n_speech_samples() * 2
self.c_lib.freedv_tx(self.freedv, self.mod_out, speech_in)
return self.mod_out