diff --git a/modem.py b/modem.py index 7a9d9ff6..0b2b984b 100644 --- a/modem.py +++ b/modem.py @@ -32,8 +32,7 @@ class RF(): libname = pathlib.Path().absolute() / "codec2/build_linux/src/libcodec2.so" self.c_lib = ctypes.CDLL(libname) - - + #--------------------------------------------OPEN AUDIO CHANNEL RX self.p = pyaudio.PyAudio() @@ -48,13 +47,13 @@ class RF(): #--------------------------------------------OPEN AUDIO CHANNEL TX #self.p = pyaudio.PyAudio() - #self.stream_tx = self.p.open(format=pyaudio.paInt16, - # channels=1, - # rate=static.AUDIO_SAMPLE_RATE, - # frames_per_buffer=2048, #n_nom_modem_samples - # output=True, - # output_device_index=static.AUDIO_OUTPUT_DEVICE, #static.AUDIO_OUTPUT_DEVICE - # ) + self.stream_tx = self.p.open(format=pyaudio.paInt16, + channels=1, + rate=static.AUDIO_SAMPLE_RATE, + frames_per_buffer=static.AUDIO_FRAMES_PER_BUFFER, #n_nom_modem_samples + output=True, + output_device_index=static.AUDIO_OUTPUT_DEVICE, #static.AUDIO_OUTPUT_DEVICE + ) #--------------------------------------------START AUDIO THREAD @@ -72,22 +71,29 @@ class RF(): FREEDV_DATAC2_THREAD = threading.Thread(target=self.receive, args=[11], name="DATAC2 Decoder") FREEDV_DATAC2_THREAD.start() - + FREEDV_DATAC3_THREAD = threading.Thread(target=self.receive, args=[12], name="DATAC3 Decoder") FREEDV_DATAC3_THREAD.start() - - #self.transmit(7,b'12345') + #time.sleep(2) + #self.transmit(7,b'000000000000') + #self.transmit(7,b'ABCDEFGHIJKL') + #-------------------------------------------------------------------------------------------------------- - + + + def audio_listen(self): print("STARTING AUDIO LISTENER") while True: time.sleep(0.05) data = self.stream_rx.read(static.AUDIO_FRAMES_PER_BUFFER, exception_on_overflow = False) - static.AUDIO_BUFFER += data - #static.AUDIO_BUFFER += data.strip(b'\x00') + #static.AUDIO_BUFFER += data + static.AUDIO_BUFFER += data.strip(b'\x00') + + rms = audioop.rms(data,2) + print(rms) #-------------------------------------------------------------------------------------------------------- # GET DATA AND MODULATE IT @@ -105,13 +111,18 @@ class RF(): mod_out = ctypes.c_short * n_tx_modem_samples 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_length = len(data_list) for i in range(data_list_length): # LOOP THROUGH DATA LIST if mode < 10: # don't generate CRC16 for modes 0 - 9 - + buffer = bytearray(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 print("buffer for ACK: " + str(buffer)) @@ -128,25 +139,25 @@ class RF(): 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 - print(bytes(mod_out)) + #print(bytes(mod_out).strip(b'\x00')) - p = pyaudio.PyAudio() - stream_tx = p.open(format=pyaudio.paInt16, - channels=static.AUDIO_CHANNELS, - rate=static.AUDIO_SAMPLE_RATE, - frames_per_buffer=n_nom_modem_samples, - output=True, - output_device_index=0, #static.AUDIO_OUTPUT_DEVICE - ) + #p = pyaudio.PyAudio() + #stream_tx = p.open(format=pyaudio.paInt16, + # channels=static.AUDIO_CHANNELS, + # rate=static.AUDIO_SAMPLE_RATE, + # frames_per_buffer=n_nom_modem_samples, + # output=True, + # 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) - stream_tx.write(audio[0]) + audio = audioop.ratecv(mod_out,2,1,static.MODEM_SAMPLE_RATE, static.AUDIO_SAMPLE_RATE, static.TX_SAMPLE_STATE) + self.stream_tx.write(audio[0]) - print("KILL") - stream_tx.stop_stream() - stream_tx.close() - p.terminate() + #print("KILL") + #stream_tx.stop_stream() + #stream_tx.close() + #p.terminate() return mod_out @@ -167,7 +178,7 @@ class RF(): bytes_out = bytes_out() #get pointer from bytes_out 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 nin = self.c_lib.freedv_nin(freedv) @@ -202,17 +213,15 @@ class RF(): # --------------- END DEBUGGING OUTPTUT ------------------------------------------- - self.c_lib.freedv_set_sync(freedv, 0) #FORCE UNSYNC - # CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------ - 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 static.AUDIO_BUFFER = bytearray() 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 + self.c_lib.freedv_set_sync(freedv, 0) #FORCE UNSYNC else: print("MODE: " + str(mode) + " DATA: " + str(bytes(bytes_out))) @@ -236,4 +245,4 @@ class RF(): if len(static.AUDIO_BUFFER) > i: # WE WILL LOOP THROUGH OUR DATA BUFFER WHILE OUR BUFFER IS BIGGER THAN THE CHUNK POSITION i = (nin*2) + i else: - i = 0 \ No newline at end of file + i = 0