mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 10:04:33 +02:00
'Refactored by Sourcery'
This commit is contained in:
parent
9b17a680e5
commit
7b23769b55
|
@ -7,10 +7,7 @@ for c in colormaps:
|
||||||
cmap_name = c
|
cmap_name = c
|
||||||
cmap = plt.get_cmap(cmap_name)
|
cmap = plt.get_cmap(cmap_name)
|
||||||
|
|
||||||
colors = []
|
colors = [[int(round(255 * x)) for x in cmap(i)[:3]] for i in range(256)]
|
||||||
for i in range(256):
|
|
||||||
colors.append([int(round(255 * x)) for x in cmap(i)[:3]])
|
|
||||||
|
|
||||||
print(f'var {c} = {colors}')
|
print(f'var {c} = {colors}')
|
||||||
|
|
||||||
print(f'var colormaps = [{", ".join(colormaps)}];')
|
print(f'var colormaps = [{", ".join(colormaps)}];')
|
||||||
|
|
30
test/ping.py
30
test/ping.py
|
@ -85,11 +85,11 @@ def receive():
|
||||||
bytes_out = ctypes.c_ubyte * bytes_per_frame # bytes_per_frame
|
bytes_out = ctypes.c_ubyte * bytes_per_frame # bytes_per_frame
|
||||||
bytes_out = bytes_out() # get pointer from bytes_out
|
bytes_out = bytes_out() # get pointer from bytes_out
|
||||||
|
|
||||||
total_n_bytes = 0
|
|
||||||
rx_total_frames = 0
|
rx_total_frames = 0
|
||||||
rx_frames = 0
|
rx_frames = 0
|
||||||
rx_bursts = 0
|
rx_bursts = 0
|
||||||
receive = True
|
receive = True
|
||||||
|
total_n_bytes = 0
|
||||||
while receive:
|
while receive:
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ def receive():
|
||||||
nin_converted = int(nin * (AUDIO_SAMPLE_RATE_RX / MODEM_SAMPLE_RATE))
|
nin_converted = int(nin * (AUDIO_SAMPLE_RATE_RX / MODEM_SAMPLE_RATE))
|
||||||
if DEBUGGING_MODE:
|
if DEBUGGING_MODE:
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
print("NIN: " + str(nin) + " [ " + str(nin_converted) + " ]")
|
print(f"NIN: {str(nin)} [ {nin_converted} ]")
|
||||||
|
|
||||||
data_in = stream_rx.read(nin_converted, exception_on_overflow=False)
|
data_in = stream_rx.read(nin_converted, exception_on_overflow=False)
|
||||||
data_in = data_in.rstrip(b"\x00")
|
data_in = data_in.rstrip(b"\x00")
|
||||||
|
@ -110,7 +110,7 @@ def receive():
|
||||||
nbytes = c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
nbytes = c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
||||||
total_n_bytes = total_n_bytes + nbytes
|
total_n_bytes = total_n_bytes + nbytes
|
||||||
if DEBUGGING_MODE:
|
if DEBUGGING_MODE:
|
||||||
print("SYNC: " + str(c_lib.freedv_get_rx_status(freedv)))
|
print(f"SYNC: {str(c_lib.freedv_get_rx_status(freedv))}")
|
||||||
|
|
||||||
if nbytes == bytes_per_frame:
|
if nbytes == bytes_per_frame:
|
||||||
rx_total_frames = rx_total_frames + 1
|
rx_total_frames = rx_total_frames + 1
|
||||||
|
@ -127,15 +127,7 @@ def receive():
|
||||||
n_total_frame = bytes_out[3]
|
n_total_frame = bytes_out[3]
|
||||||
|
|
||||||
print(
|
print(
|
||||||
"RX | PONG | BURST ["
|
f"RX | PONG | BURST [{str(burst)}/{str(n_total_burst)}] FRAME [{str(frame)}/{str(n_total_frame)}]"
|
||||||
+ str(burst)
|
|
||||||
+ "/"
|
|
||||||
+ str(n_total_burst)
|
|
||||||
+ "] FRAME ["
|
|
||||||
+ str(frame)
|
|
||||||
+ "/"
|
|
||||||
+ str(n_total_frame)
|
|
||||||
+ "]"
|
|
||||||
)
|
)
|
||||||
print("-----------------------------------------------------------------")
|
print("-----------------------------------------------------------------")
|
||||||
c_lib.freedv_set_sync(freedv, 0)
|
c_lib.freedv_set_sync(freedv, 0)
|
||||||
|
@ -151,7 +143,6 @@ RECEIVE.start()
|
||||||
c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
||||||
freedv = c_lib.freedv_open(FREEDV_TX_MODE)
|
freedv = c_lib.freedv_open(FREEDV_TX_MODE)
|
||||||
bytes_per_frame = int(c_lib.freedv_get_bits_per_modem_frame(freedv) / 8)
|
bytes_per_frame = int(c_lib.freedv_get_bits_per_modem_frame(freedv) / 8)
|
||||||
payload_per_frame = bytes_per_frame - 2
|
|
||||||
n_nom_modem_samples = c_lib.freedv_get_n_nom_modem_samples(freedv)
|
n_nom_modem_samples = c_lib.freedv_get_n_nom_modem_samples(freedv)
|
||||||
n_tx_modem_samples = c_lib.freedv_get_n_tx_modem_samples(
|
n_tx_modem_samples = c_lib.freedv_get_n_tx_modem_samples(
|
||||||
freedv
|
freedv
|
||||||
|
@ -165,9 +156,10 @@ mod_out_preamble = ctypes.c_short * (
|
||||||
mod_out_preamble = mod_out_preamble()
|
mod_out_preamble = mod_out_preamble()
|
||||||
|
|
||||||
|
|
||||||
print("BURSTS: " + str(N_BURSTS) + " FRAMES: " + str(N_FRAMES_PER_BURST))
|
print(f"BURSTS: {str(N_BURSTS)} FRAMES: {str(N_FRAMES_PER_BURST)}")
|
||||||
print("-----------------------------------------------------------------")
|
print("-----------------------------------------------------------------")
|
||||||
|
|
||||||
|
payload_per_frame = bytes_per_frame - 2
|
||||||
for i in range(N_BURSTS):
|
for i in range(N_BURSTS):
|
||||||
|
|
||||||
c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble)
|
||||||
|
@ -204,15 +196,7 @@ for i in range(N_BURSTS):
|
||||||
txbuffer += bytes(mod_out)
|
txbuffer += bytes(mod_out)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
"TX | PING | BURST ["
|
f"TX | PING | BURST [{str(i + 1)}/{str(N_BURSTS)}] FRAME [{str(n + 1)}/{str(N_FRAMES_PER_BURST)}]"
|
||||||
+ str(i + 1)
|
|
||||||
+ "/"
|
|
||||||
+ str(N_BURSTS)
|
|
||||||
+ "] FRAME ["
|
|
||||||
+ str(n + 1)
|
|
||||||
+ "/"
|
|
||||||
+ str(N_FRAMES_PER_BURST)
|
|
||||||
+ "]"
|
|
||||||
)
|
)
|
||||||
stream_tx.write(bytes(txbuffer))
|
stream_tx.write(bytes(txbuffer))
|
||||||
ACK_TIMEOUT = time.time() + 3
|
ACK_TIMEOUT = time.time() + 3
|
||||||
|
|
24
test/pong.py
24
test/pong.py
|
@ -81,7 +81,7 @@ c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte)
|
||||||
def send_pong(burst,n_total_burst,frame,n_total_frame):
|
def send_pong(burst,n_total_burst,frame,n_total_frame):
|
||||||
|
|
||||||
data_out = bytearray()
|
data_out = bytearray()
|
||||||
data_out[0:1] = bytes([burst])
|
data_out[:1] = bytes([burst])
|
||||||
data_out[1:2] = bytes([n_total_burst])
|
data_out[1:2] = bytes([n_total_burst])
|
||||||
data_out[2:3] = bytes([frame])
|
data_out[2:3] = bytes([frame])
|
||||||
data_out[4:5] = bytes([n_total_frame])
|
data_out[4:5] = bytes([n_total_frame])
|
||||||
|
@ -139,7 +139,7 @@ while receive:
|
||||||
nin_converted = int(nin*(AUDIO_SAMPLE_RATE_RX/MODEM_SAMPLE_RATE))
|
nin_converted = int(nin*(AUDIO_SAMPLE_RATE_RX/MODEM_SAMPLE_RATE))
|
||||||
if DEBUGGING_MODE:
|
if DEBUGGING_MODE:
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
print("NIN: " + str(nin) + " [ " + str(nin_converted) + " ]")
|
print(f"NIN: {str(nin)} [ {nin_converted} ]")
|
||||||
|
|
||||||
data_in = stream_rx.read(nin_converted, exception_on_overflow = False)
|
data_in = stream_rx.read(nin_converted, exception_on_overflow = False)
|
||||||
data_in = data_in.rstrip(b'\x00')
|
data_in = data_in.rstrip(b'\x00')
|
||||||
|
@ -148,17 +148,19 @@ while receive:
|
||||||
nbytes = c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
nbytes = c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio
|
||||||
|
|
||||||
if DEBUGGING_MODE:
|
if DEBUGGING_MODE:
|
||||||
print("SYNC: " + str(c_lib.freedv_get_rx_status(freedv)))
|
print(f"SYNC: {str(c_lib.freedv_get_rx_status(freedv))}")
|
||||||
|
|
||||||
if nbytes == bytes_per_frame:
|
if nbytes == bytes_per_frame:
|
||||||
|
|
||||||
burst = bytes_out[0]
|
burst = bytes_out[0]
|
||||||
n_total_burst = bytes_out[1]
|
n_total_burst = bytes_out[1]
|
||||||
frame = bytes_out[2]
|
frame = bytes_out[2]
|
||||||
n_total_frame = bytes_out[3]
|
n_total_frame = bytes_out[3]
|
||||||
print("RX | BURST [" + str(burst) + "/" + str(n_total_burst) + "] FRAME [" + str(frame) + "/" + str(n_total_frame) + "] >>> SENDING PONG")
|
print(
|
||||||
|
f"RX | BURST [{str(burst)}/{str(n_total_burst)}] FRAME [{str(frame)}/{str(n_total_frame)}] >>> SENDING PONG"
|
||||||
|
)
|
||||||
|
|
||||||
TRANSMIT_PONG = threading.Thread(target=send_pong, args=[burst,n_total_burst,frame,n_total_frame], name="SEND PONG")
|
TRANSMIT_PONG = threading.Thread(target=send_pong, args=[burst,n_total_burst,frame,n_total_frame], name="SEND PONG")
|
||||||
TRANSMIT_PONG.start()
|
TRANSMIT_PONG.start()
|
||||||
|
|
||||||
c_lib.freedv_set_sync(freedv,0)
|
c_lib.freedv_set_sync(freedv,0)
|
||||||
|
|
|
@ -77,10 +77,12 @@ class Test:
|
||||||
self.p = pyaudio.PyAudio()
|
self.p = pyaudio.PyAudio()
|
||||||
# auto search for loopback devices
|
# auto search for loopback devices
|
||||||
if self.AUDIO_INPUT_DEVICE == -2:
|
if self.AUDIO_INPUT_DEVICE == -2:
|
||||||
loopback_list = []
|
loopback_list = [
|
||||||
for dev in range(self.p.get_device_count()):
|
dev
|
||||||
if "Loopback: PCM" in self.p.get_device_info_by_index(dev)["name"]:
|
for dev in range(self.p.get_device_count())
|
||||||
loopback_list.append(dev)
|
if "Loopback: PCM"
|
||||||
|
in self.p.get_device_info_by_index(dev)["name"]
|
||||||
|
]
|
||||||
if len(loopback_list) >= 2:
|
if len(loopback_list) >= 2:
|
||||||
# 0 = RX 1 = TX
|
# 0 = RX 1 = TX
|
||||||
self.AUDIO_INPUT_DEVICE = loopback_list[0]
|
self.AUDIO_INPUT_DEVICE = loopback_list[0]
|
||||||
|
@ -287,7 +289,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_rx.start_stream()
|
self.stream_rx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -219,7 +219,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_rx.start_stream()
|
self.stream_rx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -138,7 +138,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_tx.start_stream()
|
self.stream_tx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -81,10 +81,12 @@ class Test:
|
||||||
self.p = pyaudio.PyAudio()
|
self.p = pyaudio.PyAudio()
|
||||||
# auto search for loopback devices
|
# auto search for loopback devices
|
||||||
if self.AUDIO_INPUT_DEVICE == -2:
|
if self.AUDIO_INPUT_DEVICE == -2:
|
||||||
loopback_list = []
|
loopback_list = [
|
||||||
for dev in range(self.p.get_device_count()):
|
dev
|
||||||
if "Loopback: PCM" in self.p.get_device_info_by_index(dev)["name"]:
|
for dev in range(self.p.get_device_count())
|
||||||
loopback_list.append(dev)
|
if "Loopback: PCM"
|
||||||
|
in self.p.get_device_info_by_index(dev)["name"]
|
||||||
|
]
|
||||||
if len(loopback_list) >= 2:
|
if len(loopback_list) >= 2:
|
||||||
self.AUDIO_INPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
self.AUDIO_INPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
||||||
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
||||||
|
@ -185,7 +187,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_rx.start_stream()
|
self.stream_rx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -81,10 +81,12 @@ class Test:
|
||||||
self.p = pyaudio.PyAudio()
|
self.p = pyaudio.PyAudio()
|
||||||
# auto search for loopback devices
|
# auto search for loopback devices
|
||||||
if self.AUDIO_INPUT_DEVICE == -2:
|
if self.AUDIO_INPUT_DEVICE == -2:
|
||||||
loopback_list = []
|
loopback_list = [
|
||||||
for dev in range(self.p.get_device_count()):
|
dev
|
||||||
if "Loopback: PCM" in self.p.get_device_info_by_index(dev)["name"]:
|
for dev in range(self.p.get_device_count())
|
||||||
loopback_list.append(dev)
|
if "Loopback: PCM"
|
||||||
|
in self.p.get_device_info_by_index(dev)["name"]
|
||||||
|
]
|
||||||
if len(loopback_list) >= 2:
|
if len(loopback_list) >= 2:
|
||||||
self.AUDIO_INPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
self.AUDIO_INPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
||||||
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
||||||
|
@ -147,7 +149,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_rx.start_stream()
|
self.stream_rx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -87,10 +87,12 @@ class Test:
|
||||||
self.p = pyaudio.PyAudio()
|
self.p = pyaudio.PyAudio()
|
||||||
# auto search for loopback devices
|
# auto search for loopback devices
|
||||||
if self.AUDIO_OUTPUT_DEVICE == -2:
|
if self.AUDIO_OUTPUT_DEVICE == -2:
|
||||||
loopback_list = []
|
loopback_list = [
|
||||||
for dev in range( self.p.get_device_count()):
|
dev
|
||||||
if "Loopback: PCM" in self.p.get_device_info_by_index(dev)["name"]:
|
for dev in range(self.p.get_device_count())
|
||||||
loopback_list.append(dev)
|
if "Loopback: PCM"
|
||||||
|
in self.p.get_device_info_by_index(dev)["name"]
|
||||||
|
]
|
||||||
if len(loopback_list) >= 2:
|
if len(loopback_list) >= 2:
|
||||||
self.AUDIO_OUTPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
self.AUDIO_OUTPUT_DEVICE = loopback_list[0] # 0 = RX 1 = TX
|
||||||
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
print(f"loopback_list rx: {loopback_list}", file=sys.stderr)
|
||||||
|
@ -148,7 +150,7 @@ class Test:
|
||||||
|
|
||||||
def run_audio(self):
|
def run_audio(self):
|
||||||
try:
|
try:
|
||||||
print(f"starting pyaudio callback", file=sys.stderr)
|
print("starting pyaudio callback", file=sys.stderr)
|
||||||
self.stream_tx.start_stream()
|
self.stream_tx.start_stream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"pyAudio error: {e}", file=sys.stderr)
|
print(f"pyAudio error: {e}", file=sys.stderr)
|
||||||
|
|
|
@ -26,8 +26,6 @@ from tnc import codec2
|
||||||
|
|
||||||
|
|
||||||
def test_mm_tx():
|
def test_mm_tx():
|
||||||
# AUDIO PARAMETERS
|
|
||||||
AUDIO_FRAMES_PER_BUFFER = 2400
|
|
||||||
MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000
|
MODEM_SAMPLE_RATE = codec2.api.FREEDV_FS_8000
|
||||||
AUDIO_SAMPLE_RATE_TX = 48000
|
AUDIO_SAMPLE_RATE_TX = 48000
|
||||||
assert (AUDIO_SAMPLE_RATE_TX % MODEM_SAMPLE_RATE) == 0
|
assert (AUDIO_SAMPLE_RATE_TX % MODEM_SAMPLE_RATE) == 0
|
||||||
|
@ -72,6 +70,8 @@ def test_mm_tx():
|
||||||
else:
|
else:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
# AUDIO PARAMETERS
|
||||||
|
AUDIO_FRAMES_PER_BUFFER = 2400
|
||||||
# pyaudio init
|
# pyaudio init
|
||||||
stream_tx = p_audio.open(
|
stream_tx = p_audio.open(
|
||||||
format=pyaudio.paInt16,
|
format=pyaudio.paInt16,
|
||||||
|
|
154
test/util_rx.py
154
test/util_rx.py
|
@ -121,99 +121,93 @@ def util_rx():
|
||||||
time_start = 0
|
time_start = 0
|
||||||
time_end = 0
|
time_end = 0
|
||||||
|
|
||||||
# Copy received 48 kHz to a file. Listen to this file with:
|
with open("rx48.raw", mode="wb") as frx:
|
||||||
# aplay -r 48000 -f S16_LE rx48.raw
|
# initial number of samples we need
|
||||||
# Corruption of this file is a good way to detect audio card issues
|
nin = codec2.api.freedv_nin(freedv)
|
||||||
frx = open("rx48.raw", mode="wb")
|
while receive and time.time() < timeout:
|
||||||
|
if AUDIO_INPUT_DEVICE != -1:
|
||||||
|
try:
|
||||||
|
# data_in48k = stream_rx.read(AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = True)
|
||||||
|
data_in48k, overflowed = stream_rx.read(AUDIO_FRAMES_PER_BUFFER) # type: ignore
|
||||||
|
except OSError as err:
|
||||||
|
print(err, file=sys.stderr)
|
||||||
|
# if str(err).find("Input overflowed") != -1:
|
||||||
|
# nread_exceptions += 1
|
||||||
|
# if str(err).find("Stream closed") != -1:
|
||||||
|
# print("Ending...")
|
||||||
|
# receive = False
|
||||||
|
else:
|
||||||
|
data_in48k = sys.stdin.buffer.read(AUDIO_FRAMES_PER_BUFFER * 2)
|
||||||
|
|
||||||
# initial number of samples we need
|
# insert samples in buffer
|
||||||
nin = codec2.api.freedv_nin(freedv)
|
x = np.frombuffer(data_in48k, dtype=np.int16) # type: ignore
|
||||||
while receive and time.time() < timeout:
|
# print(x)
|
||||||
if AUDIO_INPUT_DEVICE != -1:
|
# x = data_in48k
|
||||||
try:
|
x.tofile(frx)
|
||||||
# data_in48k = stream_rx.read(AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = True)
|
if len(x) != AUDIO_FRAMES_PER_BUFFER:
|
||||||
data_in48k, overflowed = stream_rx.read(AUDIO_FRAMES_PER_BUFFER) # type: ignore
|
receive = False
|
||||||
except OSError as err:
|
x = resampler.resample48_to_8(x)
|
||||||
print(err, file=sys.stderr)
|
audio_buffer.push(x)
|
||||||
# if str(err).find("Input overflowed") != -1:
|
|
||||||
# nread_exceptions += 1
|
|
||||||
# if str(err).find("Stream closed") != -1:
|
|
||||||
# print("Ending...")
|
|
||||||
# receive = False
|
|
||||||
else:
|
|
||||||
data_in48k = sys.stdin.buffer.read(AUDIO_FRAMES_PER_BUFFER * 2)
|
|
||||||
|
|
||||||
# insert samples in buffer
|
# when we have enough samples call FreeDV Rx
|
||||||
x = np.frombuffer(data_in48k, dtype=np.int16) # type: ignore
|
while audio_buffer.nbuffer >= nin:
|
||||||
# print(x)
|
# start time measurement
|
||||||
# x = data_in48k
|
time_start = time.time()
|
||||||
x.tofile(frx)
|
# demodulate audio
|
||||||
if len(x) != AUDIO_FRAMES_PER_BUFFER:
|
nbytes = codec2.api.freedv_rawdatarx(
|
||||||
receive = False
|
freedv, bytes_out, audio_buffer.buffer.ctypes
|
||||||
x = resampler.resample48_to_8(x)
|
|
||||||
audio_buffer.push(x)
|
|
||||||
|
|
||||||
# when we have enough samples call FreeDV Rx
|
|
||||||
while audio_buffer.nbuffer >= nin:
|
|
||||||
# start time measurement
|
|
||||||
time_start = time.time()
|
|
||||||
# demodulate audio
|
|
||||||
nbytes = codec2.api.freedv_rawdatarx(
|
|
||||||
freedv, bytes_out, audio_buffer.buffer.ctypes
|
|
||||||
)
|
|
||||||
time_end = time.time()
|
|
||||||
|
|
||||||
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] # type: ignore
|
|
||||||
time_needed = time_end - time_start
|
|
||||||
|
|
||||||
print(
|
|
||||||
f"nin: {nin:5d} rx_status: {rx_status:4s} "
|
|
||||||
f"naudio_buffer: {audio_buffer.nbuffer:4d} time: {time_needed:4f}",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
)
|
||||||
|
time_end = time.time()
|
||||||
|
|
||||||
if nbytes:
|
audio_buffer.pop(nin)
|
||||||
total_n_bytes += nbytes
|
|
||||||
|
|
||||||
if nbytes == bytes_per_frame:
|
# call me on every loop!
|
||||||
rx_total_frames += 1
|
nin = codec2.api.freedv_nin(freedv)
|
||||||
rx_frames += 1
|
|
||||||
|
|
||||||
if rx_frames == N_FRAMES_PER_BURST:
|
rx_status = codec2.api.freedv_get_rx_status(freedv)
|
||||||
rx_frames = 0
|
if rx_status & codec2.api.FREEDV_RX_BIT_ERRORS:
|
||||||
rx_bursts += 1
|
rx_errors = rx_errors + 1
|
||||||
|
if DEBUGGING_MODE:
|
||||||
|
rx_status = codec2.api.rx_sync_flags_to_text[rx_status] # type: ignore
|
||||||
|
time_needed = time_end - time_start
|
||||||
|
|
||||||
if rx_bursts == N_BURSTS:
|
print(
|
||||||
receive = False
|
f"nin: {nin:5d} rx_status: {rx_status:4s} "
|
||||||
|
f"naudio_buffer: {audio_buffer.nbuffer:4d} time: {time_needed:4f}",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
|
||||||
if time.time() >= timeout:
|
if nbytes:
|
||||||
print("TIMEOUT REACHED")
|
total_n_bytes += nbytes
|
||||||
|
|
||||||
time.sleep(0.01)
|
if nbytes == bytes_per_frame:
|
||||||
|
rx_total_frames += 1
|
||||||
|
rx_frames += 1
|
||||||
|
|
||||||
if nread_exceptions:
|
if rx_frames == N_FRAMES_PER_BURST:
|
||||||
|
rx_frames = 0
|
||||||
|
rx_bursts += 1
|
||||||
|
|
||||||
|
if rx_bursts == N_BURSTS:
|
||||||
|
receive = False
|
||||||
|
|
||||||
|
if time.time() >= timeout:
|
||||||
|
print("TIMEOUT REACHED")
|
||||||
|
|
||||||
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
if nread_exceptions:
|
||||||
|
print(
|
||||||
|
f"nread_exceptions {nread_exceptions:d} - receive audio lost! "
|
||||||
|
"Consider increasing Pyaudio frames_per_buffer...",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
print(
|
print(
|
||||||
f"nread_exceptions {nread_exceptions:d} - receive audio lost! "
|
f"RECEIVED BURSTS: {rx_bursts} "
|
||||||
"Consider increasing Pyaudio frames_per_buffer...",
|
f"RECEIVED FRAMES: {rx_total_frames} "
|
||||||
|
f"RX_ERRORS: {rx_errors}",
|
||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
print(
|
|
||||||
f"RECEIVED BURSTS: {rx_bursts} "
|
|
||||||
f"RECEIVED FRAMES: {rx_total_frames} "
|
|
||||||
f"RX_ERRORS: {rx_errors}",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
frx.close()
|
|
||||||
|
|
||||||
# and at last check if we had an opened audio instance and close it
|
# and at last check if we had an opened audio instance and close it
|
||||||
if AUDIO_INPUT_DEVICE != -1:
|
if AUDIO_INPUT_DEVICE != -1:
|
||||||
sd._terminate()
|
sd._terminate()
|
||||||
|
|
|
@ -166,23 +166,15 @@ class DAEMON:
|
||||||
self.log.warning("[DMN] Starting TNC", rig=data[5], port=data[6])
|
self.log.warning("[DMN] Starting TNC", rig=data[5], port=data[6])
|
||||||
|
|
||||||
# list of parameters, necessary for running subprocess command as a list
|
# list of parameters, necessary for running subprocess command as a list
|
||||||
options = []
|
options = ["--port", str(static.DAEMONPORT - 1)]
|
||||||
|
|
||||||
options.append("--port")
|
|
||||||
options.append(str(static.DAEMONPORT - 1))
|
|
||||||
# create an additional list entry for parameters not covered by gui
|
# create an additional list entry for parameters not covered by gui
|
||||||
data[50] = int(static.DAEMONPORT - 1)
|
data[50] = int(static.DAEMONPORT - 1)
|
||||||
|
|
||||||
options.append("--mycall")
|
options.append("--mycall")
|
||||||
options.append(data[1])
|
options.extend((data[1], "--mygrid"))
|
||||||
|
options.extend((data[2], "--rx"))
|
||||||
options.append("--mygrid")
|
options.extend((data[3], "--tx"))
|
||||||
options.append(data[2])
|
|
||||||
|
|
||||||
options.append("--rx")
|
|
||||||
options.append(data[3])
|
|
||||||
|
|
||||||
options.append("--tx")
|
|
||||||
options.append(data[4])
|
options.append(data[4])
|
||||||
|
|
||||||
# if radiocontrol != disabled
|
# if radiocontrol != disabled
|
||||||
|
@ -196,9 +188,7 @@ class DAEMON:
|
||||||
|
|
||||||
if data[5] == "rigctld":
|
if data[5] == "rigctld":
|
||||||
options.append("--rigctld_ip")
|
options.append("--rigctld_ip")
|
||||||
options.append(data[6])
|
options.extend((data[6], "--rigctld_port"))
|
||||||
|
|
||||||
options.append("--rigctld_port")
|
|
||||||
options.append(data[7])
|
options.append(data[7])
|
||||||
|
|
||||||
if data[8] == "True":
|
if data[8] == "True":
|
||||||
|
@ -211,16 +201,8 @@ class DAEMON:
|
||||||
options.append("--500hz")
|
options.append("--500hz")
|
||||||
|
|
||||||
options.append("--tuning_range_fmin")
|
options.append("--tuning_range_fmin")
|
||||||
options.append(data[11])
|
options.extend((data[11], "--tuning_range_fmax"))
|
||||||
|
options.extend((data[12], "--tx-audio-level"))
|
||||||
options.append("--tuning_range_fmax")
|
|
||||||
options.append(data[12])
|
|
||||||
|
|
||||||
# overriding FSK mode
|
|
||||||
# if data[13] == "True":
|
|
||||||
# options.append("--fsk")
|
|
||||||
|
|
||||||
options.append("--tx-audio-level")
|
|
||||||
options.append(data[14])
|
options.append(data[14])
|
||||||
|
|
||||||
if data[15] == "True":
|
if data[15] == "True":
|
||||||
|
@ -233,9 +215,7 @@ class DAEMON:
|
||||||
options.append("--explorer")
|
options.append("--explorer")
|
||||||
|
|
||||||
options.append("--ssid")
|
options.append("--ssid")
|
||||||
for i in data[18]:
|
options.extend(str(i) for i in data[18])
|
||||||
options.append(str(i))
|
|
||||||
|
|
||||||
if data[19] == "True":
|
if data[19] == "True":
|
||||||
options.append("--tune")
|
options.append("--tune")
|
||||||
|
|
||||||
|
@ -258,14 +238,12 @@ class DAEMON:
|
||||||
# extends the sys module by a flag frozen=True and sets the app
|
# extends the sys module by a flag frozen=True and sets the app
|
||||||
# path into variable _MEIPASS'.
|
# path into variable _MEIPASS'.
|
||||||
application_path = sys._MEIPASS
|
application_path = sys._MEIPASS
|
||||||
command.append(application_path + '/freedata-tnc')
|
command.append(f'{application_path}/freedata-tnc')
|
||||||
|
|
||||||
else:
|
elif sys.platform in ["linux", "darwin"]:
|
||||||
|
command.append("./freedata-tnc")
|
||||||
if sys.platform in ["linux", "darwin"]:
|
elif sys.platform in ["win32", "win64"]:
|
||||||
command.append("./freedata-tnc")
|
command.append("freedata-tnc.exe")
|
||||||
elif sys.platform in ["win32", "win64"]:
|
|
||||||
command.append("freedata-tnc.exe")
|
|
||||||
|
|
||||||
command += options
|
command += options
|
||||||
proc = subprocess.Popen(command)
|
proc = subprocess.Popen(command)
|
||||||
|
@ -331,9 +309,7 @@ class DAEMON:
|
||||||
# hamlib_version = rig.hamlib_version
|
# hamlib_version = rig.hamlib_version
|
||||||
|
|
||||||
hamlib.set_ptt(True)
|
hamlib.set_ptt(True)
|
||||||
pttstate = hamlib.get_ptt()
|
if pttstate := hamlib.get_ptt():
|
||||||
|
|
||||||
if pttstate:
|
|
||||||
self.log.info("[DMN] Hamlib PTT", status="SUCCESS")
|
self.log.info("[DMN] Hamlib PTT", status="SUCCESS")
|
||||||
response = {"command": "test_hamlib", "result": "SUCCESS"}
|
response = {"command": "test_hamlib", "result": "SUCCESS"}
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -249,7 +249,10 @@ class DATA:
|
||||||
#
|
#
|
||||||
# send transmission queued information once
|
# send transmission queued information once
|
||||||
if static.ARQ_STATE or static.IS_CODEC2_TRAFFIC:
|
if static.ARQ_STATE or static.IS_CODEC2_TRAFFIC:
|
||||||
self.log.debug(f"[TNC] TX DISPATCHER - waiting with processing command ", arq_state=static.ARQ_STATE)
|
self.log.debug(
|
||||||
|
"[TNC] TX DISPATCHER - waiting with processing command ",
|
||||||
|
arq_state=static.ARQ_STATE,
|
||||||
|
)
|
||||||
|
|
||||||
self.send_data_to_socket_queue(
|
self.send_data_to_socket_queue(
|
||||||
freedata="tnc-message",
|
freedata="tnc-message",
|
||||||
|
|
11
tnc/sock.py
11
tnc/sock.py
|
@ -561,12 +561,12 @@ def process_tnc_commands(data):
|
||||||
|
|
||||||
if received_json["type"] == "get" and received_json["command"] == "rx_buffer":
|
if received_json["type"] == "get" and received_json["command"] == "rx_buffer":
|
||||||
try:
|
try:
|
||||||
output = {
|
|
||||||
"command": "rx_buffer",
|
|
||||||
"data-array": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
if not RX_BUFFER.empty():
|
if not RX_BUFFER.empty():
|
||||||
|
output = {
|
||||||
|
"command": "rx_buffer",
|
||||||
|
"data-array": [],
|
||||||
|
}
|
||||||
|
|
||||||
for _buffer_length in range(RX_BUFFER.qsize()):
|
for _buffer_length in range(RX_BUFFER.qsize()):
|
||||||
base64_data = RX_BUFFER.queue[_buffer_length][4]
|
base64_data = RX_BUFFER.queue[_buffer_length][4]
|
||||||
output["data-array"].append(
|
output["data-array"].append(
|
||||||
|
@ -632,7 +632,6 @@ def process_tnc_commands(data):
|
||||||
command=received_json,
|
command=received_json,
|
||||||
)
|
)
|
||||||
|
|
||||||
# exception, if JSON cant be decoded
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
log.error("[SCK] JSON decoding error", e=err)
|
log.error("[SCK] JSON decoding error", e=err)
|
||||||
|
|
||||||
|
|
10
tnc/stats.py
10
tnc/stats.py
|
@ -22,16 +22,10 @@ class stats():
|
||||||
self.explorer_url = "https://api.freedata.app/stats.php"
|
self.explorer_url = "https://api.freedata.app/stats.php"
|
||||||
|
|
||||||
def push(self, frame_nack_counter, status, duration):
|
def push(self, frame_nack_counter, status, duration):
|
||||||
if status in ["crc_error"]:
|
crcerror = status in ["crc_error"]
|
||||||
crcerror = True
|
|
||||||
else:
|
|
||||||
crcerror = False
|
|
||||||
|
|
||||||
# get avg snr
|
# get avg snr
|
||||||
try:
|
try:
|
||||||
snr_raw = []
|
snr_raw = [item["snr"] for item in static.SPEED_LIST]
|
||||||
for item in static.SPEED_LIST:
|
|
||||||
snr_raw.append(item["snr"])
|
|
||||||
avg_snr = round(sum(snr_raw) / len(snr_raw), 2 )
|
avg_snr = round(sum(snr_raw) / len(snr_raw), 2 )
|
||||||
except Exception:
|
except Exception:
|
||||||
avg_snr = 0
|
avg_snr = 0
|
||||||
|
|
|
@ -6,6 +6,7 @@ Created on Fri Dec 11 21:53:35 2020
|
||||||
@author: parallels
|
@author: parallels
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
@ -13,7 +14,7 @@ import time
|
||||||
|
|
||||||
# --------------------------------------------GET PARAMETER INPUTS
|
# --------------------------------------------GET PARAMETER INPUTS
|
||||||
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
||||||
parser.add_argument('--port', dest="socket_port", default=3000, help="Set the port, the socket is listening on.", type=int)
|
parser.add_argument('--port', dest="socket_port", default=3000, help="Set the port, the socket is listening on.", type=int)
|
||||||
parser.add_argument('--data', dest="data", default=False, help="data", type=str)
|
parser.add_argument('--data', dest="data", default=False, help="data", type=str)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -29,6 +30,6 @@ while True:
|
||||||
sock.connect((ip, port))
|
sock.connect((ip, port))
|
||||||
sock.sendall(bytes(message, 'utf-8') + b'\n')
|
sock.sendall(bytes(message, 'utf-8') + b'\n')
|
||||||
response = str(sock.recv(1024), 'utf-8')
|
response = str(sock.recv(1024), 'utf-8')
|
||||||
print("CMD: {}".format(response))
|
print(f"CMD: {response}")
|
||||||
False
|
False
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in a new issue