diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-193.local> | 2023-12-14 18:18:07 -0500 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-193.local> | 2023-12-14 18:18:07 -0500 |
commit | f21474ef27f630d093a116b82fc2147b775eb832 (patch) | |
tree | 92e15d41f2012f4a110f1eb96d203ff98c8ebb21 /Sender.py | |
parent | 25aa2dd624776f460c7424431b62958f9c524e3d (diff) |
utils and threading updates
Diffstat (limited to 'Sender.py')
-rw-r--r-- | Sender.py | 82 |
1 files changed, 32 insertions, 50 deletions
@@ -29,36 +29,6 @@ def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100, p=None): stream.stop_stream() stream.close() - # p.terminate() - - -# def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100): -# p = pyaudio.PyAudio() - -# # Generate sample -# samples = np.sin(2 * np.pi * np.arange(samplingRate * duration) * freq / samplingRate) - -# # Normalize -# max_amplitude = np.max(np.abs(samples)) -# normalized_samples = (amplitude / max_amplitude) * samples - -# # clip to [0.0, 1.0] -# normalized_samples = np.clip(normalized_samples, 0.0, 1.0) - -# samples_bytes = normalized_samples.astype(np.float32).tobytes() - -# stream = p.open(format=pyaudio.paFloat32, -# channels=1, -# rate=samplingRate, -# output=True) - -# stream.write(samples_bytes) - -# # Stop and close the stream -# stream.stop_stream() -# stream.close() - -# p.terminate() """ Use threads to play multiple frequencies simultaneously. @@ -153,22 +123,26 @@ class LinkLayer: self.p = pyaudio.PyAudio() self.isReceiving = False self.isEstablished = False + self.byte_per_transmit = 8 + self.freq_range = 1000 def transmit_string(self, data): data_list = string_to_binary(data) - for i in range(len(data_list)): - freq_map = {} - start_freq = 18000 - for j in range(len(data_list[i])): - if data_list[i][j] == "0": - freq_map[start_freq + j * 250] = 0.0 + # for i in range(len(data_list)): + # freq_map = {} + # start_freq = 18000 + # for j in range(len(data_list[i])): + # if data_list[i][j] == "0": + # freq_map[start_freq + j * 250] = 0.0 - if data_list[i][j] == "1": - freq_map[start_freq + j * 250] = 1.0 + # if data_list[i][j] == "1": + # freq_map[start_freq + j * 250] = 1.0 - # print(freq_map) - play_frequencies_separately(freq_map, duration=0.5) + # # print(freq_map) + # play_frequencies_separately(freq_map, duration=0.5) + play_data(data_list, self.start_freq, self.freq_range, self.byte_per_transmit, self.p) + def receive_string(self, data): binary = ['0'] * 8 @@ -187,24 +161,32 @@ class LinkLayer: while True: if not self.isReceiving: user_input = input("Enter data to send: ") - if user_input == "exit": - self.send_postamble() + if user_input == "exit" or user_input == "q": + self.exit() self.transmit_string(user_input) else: print("Currently receiving data, please wait...") + def exit(self): + self.stream.stop_stream() + self.stream.close() + self.p.terminate() -link_layer = LinkLayer() - -# Create a thread for sending data -send_thread = threading.Thread(target=link_layer.send_data) - -# Start the threads -send_thread.start() - # take in range width, the number of bytes, and the bytes themselves, and starting freq # cmdline args: data, start freq, bytes per transmit, frequency range # 18500, 1000 range # vlistener takes in no data. + +def main(): + link_layer = LinkLayer() + + # Create a thread for sending data + send_thread = threading.Thread(target=link_layer.send_data) + + # Start the threads + send_thread.start() + +if __name__ == "__main__": + main()
\ No newline at end of file |