mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
moved tx audio to class __init__
This commit is contained in:
parent
508c01ffed
commit
0efc20defa
1 changed files with 46 additions and 37 deletions
67
modem.py
67
modem.py
|
@ -33,7 +33,6 @@ class RF():
|
||||||
self.c_lib = ctypes.CDLL(libname)
|
self.c_lib = ctypes.CDLL(libname)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#--------------------------------------------OPEN AUDIO CHANNEL RX
|
#--------------------------------------------OPEN AUDIO CHANNEL RX
|
||||||
|
|
||||||
self.p = pyaudio.PyAudio()
|
self.p = pyaudio.PyAudio()
|
||||||
|
@ -48,13 +47,13 @@ class RF():
|
||||||
#--------------------------------------------OPEN AUDIO CHANNEL TX
|
#--------------------------------------------OPEN AUDIO CHANNEL TX
|
||||||
|
|
||||||
#self.p = pyaudio.PyAudio()
|
#self.p = pyaudio.PyAudio()
|
||||||
#self.stream_tx = self.p.open(format=pyaudio.paInt16,
|
self.stream_tx = self.p.open(format=pyaudio.paInt16,
|
||||||
# channels=1,
|
channels=1,
|
||||||
# rate=static.AUDIO_SAMPLE_RATE,
|
rate=static.AUDIO_SAMPLE_RATE,
|
||||||
# frames_per_buffer=2048, #n_nom_modem_samples
|
frames_per_buffer=static.AUDIO_FRAMES_PER_BUFFER, #n_nom_modem_samples
|
||||||
# output=True,
|
output=True,
|
||||||
# output_device_index=static.AUDIO_OUTPUT_DEVICE, #static.AUDIO_OUTPUT_DEVICE
|
output_device_index=static.AUDIO_OUTPUT_DEVICE, #static.AUDIO_OUTPUT_DEVICE
|
||||||
# )
|
)
|
||||||
|
|
||||||
|
|
||||||
#--------------------------------------------START AUDIO THREAD
|
#--------------------------------------------START AUDIO THREAD
|
||||||
|
@ -76,18 +75,25 @@ class RF():
|
||||||
FREEDV_DATAC3_THREAD = threading.Thread(target=self.receive, args=[12], name="DATAC3 Decoder")
|
FREEDV_DATAC3_THREAD = threading.Thread(target=self.receive, args=[12], name="DATAC3 Decoder")
|
||||||
FREEDV_DATAC3_THREAD.start()
|
FREEDV_DATAC3_THREAD.start()
|
||||||
|
|
||||||
|
#time.sleep(2)
|
||||||
|
#self.transmit(7,b'000000000000')
|
||||||
|
#self.transmit(7,b'ABCDEFGHIJKL')
|
||||||
|
|
||||||
#self.transmit(7,b'12345')
|
|
||||||
#--------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def audio_listen(self):
|
def audio_listen(self):
|
||||||
print("STARTING AUDIO LISTENER")
|
print("STARTING AUDIO LISTENER")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
data = self.stream_rx.read(static.AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = False)
|
data = self.stream_rx.read(static.AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = False)
|
||||||
static.AUDIO_BUFFER += data
|
#static.AUDIO_BUFFER += data
|
||||||
#static.AUDIO_BUFFER += data.strip(b'\x00')
|
static.AUDIO_BUFFER += data.strip(b'\x00')
|
||||||
|
|
||||||
|
rms = audioop.rms(data,2)
|
||||||
|
print(rms)
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------------
|
||||||
# GET DATA AND MODULATE IT
|
# GET DATA AND MODULATE IT
|
||||||
|
@ -105,6 +111,11 @@ class RF():
|
||||||
mod_out = ctypes.c_short * n_tx_modem_samples
|
mod_out = ctypes.c_short * n_tx_modem_samples
|
||||||
mod_out = mod_out()
|
mod_out = mod_out()
|
||||||
|
|
||||||
|
if mode < 10:
|
||||||
|
##preamble = bytes(payload_per_frame)
|
||||||
|
preamble = b'111111111111'
|
||||||
|
data_out = preamble + data_out
|
||||||
|
#data_out += data_out
|
||||||
|
|
||||||
data_list = [data_out[i:i+payload_per_frame] for i in range(0, len(data_out), payload_per_frame)] # split incomming bytes to size of 30bytes, create a list and loop through it
|
data_list = [data_out[i:i+payload_per_frame] for i in range(0, len(data_out), payload_per_frame)] # split incomming bytes to size of 30bytes, create a list and loop through it
|
||||||
data_list_length = len(data_list)
|
data_list_length = len(data_list)
|
||||||
|
@ -128,25 +139,25 @@ class RF():
|
||||||
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
data = (ctypes.c_ubyte * bytes_per_frame).from_buffer_copy(buffer)
|
||||||
|
|
||||||
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
|
self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer
|
||||||
print(bytes(mod_out))
|
#print(bytes(mod_out).strip(b'\x00'))
|
||||||
|
|
||||||
p = pyaudio.PyAudio()
|
#p = pyaudio.PyAudio()
|
||||||
stream_tx = p.open(format=pyaudio.paInt16,
|
#stream_tx = p.open(format=pyaudio.paInt16,
|
||||||
channels=static.AUDIO_CHANNELS,
|
# channels=static.AUDIO_CHANNELS,
|
||||||
rate=static.AUDIO_SAMPLE_RATE,
|
# rate=static.AUDIO_SAMPLE_RATE,
|
||||||
frames_per_buffer=n_nom_modem_samples,
|
# frames_per_buffer=n_nom_modem_samples,
|
||||||
output=True,
|
# output=True,
|
||||||
output_device_index=0, #static.AUDIO_OUTPUT_DEVICE
|
# output_device_index=1, #static.AUDIO_OUTPUT_DEVICE
|
||||||
)
|
# )
|
||||||
|
|
||||||
audio = audioop.ratecv(mod_out,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE, static.TX_SAMPLE_STATE)
|
audio = audioop.ratecv(mod_out,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE, static.TX_SAMPLE_STATE)
|
||||||
stream_tx.write(audio[0])
|
self.stream_tx.write(audio[0])
|
||||||
|
|
||||||
|
|
||||||
print("KILL")
|
#print("KILL")
|
||||||
stream_tx.stop_stream()
|
#stream_tx.stop_stream()
|
||||||
stream_tx.close()
|
#stream_tx.close()
|
||||||
p.terminate()
|
#p.terminate()
|
||||||
|
|
||||||
return mod_out
|
return mod_out
|
||||||
|
|
||||||
|
@ -167,7 +178,7 @@ class RF():
|
||||||
bytes_out = bytes_out() #get pointer from bytes_out
|
bytes_out = bytes_out() #get pointer from bytes_out
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while static.MODEM_RECEIVE == True: # Listne to audio until data arrives
|
while True: # Listne to audio until data arrives
|
||||||
time.sleep(0.05) # here we reduce CPU load
|
time.sleep(0.05) # here we reduce CPU load
|
||||||
|
|
||||||
nin = self.c_lib.freedv_nin(freedv)
|
nin = self.c_lib.freedv_nin(freedv)
|
||||||
|
@ -202,17 +213,15 @@ class RF():
|
||||||
|
|
||||||
# --------------- END DEBUGGING OUTPTUT -------------------------------------------
|
# --------------- END DEBUGGING OUTPTUT -------------------------------------------
|
||||||
|
|
||||||
self.c_lib.freedv_set_sync(freedv, 0) #FORCE UNSYNC
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
||||||
|
|
||||||
frametype = int.from_bytes(bytes(bytes_out[:1]), "big")
|
frametype = int.from_bytes(bytes(bytes_out[:1]), "big")
|
||||||
if 50 >= frametype >= 10 and len(bytes_out) > 30: # --> The length check filters out random strings without CRC
|
if 50 >= frametype >= 10 and len(bytes_out) > 30: # --> The length check filters out random strings without CRC
|
||||||
static.AUDIO_BUFFER = bytearray()
|
static.AUDIO_BUFFER = bytearray()
|
||||||
print("MODE: " + str(mode) + " DATA: " + str(bytes(bytes_out[:-2])))
|
print("MODE: " + str(mode) + " DATA: " + str(bytes(bytes_out[:-2])))
|
||||||
arq.data_received(bytes(bytes_out[:-2])) #send payload data to arq checker without CRC16
|
arq.data_received(bytes(bytes_out[:-2])) #send payload data to arq checker without CRC16
|
||||||
|
self.c_lib.freedv_set_sync(freedv, 0) #FORCE UNSYNC
|
||||||
else:
|
else:
|
||||||
print("MODE: " + str(mode) + " DATA: " + str(bytes(bytes_out)))
|
print("MODE: " + str(mode) + " DATA: " + str(bytes(bytes_out)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue