aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Doan <daviddoan@Davids-MacBook-Pro-193.local>2023-12-14 18:18:07 -0500
committerDavid Doan <daviddoan@Davids-MacBook-Pro-193.local>2023-12-14 18:18:07 -0500
commitf21474ef27f630d093a116b82fc2147b775eb832 (patch)
tree92e15d41f2012f4a110f1eb96d203ff98c8ebb21
parent25aa2dd624776f460c7424431b62958f9c524e3d (diff)
utils and threading updates
-rw-r--r--Sender.py82
-rw-r--r--__pycache__/utils.cpython-38.pycbin853 -> 1744 bytes
-rw-r--r--utils.py75
3 files changed, 101 insertions, 56 deletions
diff --git a/Sender.py b/Sender.py
index ca0183e..16c419a 100644
--- a/Sender.py
+++ b/Sender.py
@@ -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
diff --git a/__pycache__/utils.cpython-38.pyc b/__pycache__/utils.cpython-38.pyc
index ae3eb9b..b73d0a1 100644
--- a/__pycache__/utils.cpython-38.pyc
+++ b/__pycache__/utils.cpython-38.pyc
Binary files differ
diff --git a/utils.py b/utils.py
index ffa671f..f11adb3 100644
--- a/utils.py
+++ b/utils.py
@@ -2,15 +2,20 @@
# given the cmdline arg, turns the byte sequencies into a list of frequencies, and vice versa
# 1875 1924 +24, -25, range/2, 1, flipping new info 2 sending or not
+import numpy as np
+import pyaudio
+import threading
def make_frequencies_map(start_freq, freq_step, byte_per_transmit, is_sender=True):
- freq_list = [None] * (byte_per_transmit + 3)
+ # start_freq += 1500
+ freq_list = []
sender_range = freq_step // 2
plus_minus = sender_range // (byte_per_transmit + 3)
for i in range(byte_per_transmit + 3):
- freq_list[i] = start_freq + i * plus_minus
+ # print(start_freq + i * plus_minus, sender_range, plus_minus)
+ freq_list.append(start_freq + i * plus_minus)
if not is_sender:
freq_list[i] += sender_range
@@ -19,13 +24,71 @@ def make_frequencies_map(start_freq, freq_step, byte_per_transmit, is_sender=Tru
def frequencies_to_bytes(frequencies, start_freq, freq_step, byte_per_transmit, is_sender=True):
freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit, is_sender)
- byte_list = [None] * len(frequencies)
+ byte_list = []
sender_range = freq_step // 2
plus_minus = sender_range // (byte_per_transmit + 3)
for i in range(len(frequencies)):
- byte_list[i] = 0
- if frequencies[i] <= freq_list[i] < frequencies[i] + plus_minus - 1: byte_list[i] = 1
+ if frequencies[i] <= freq_list[i] < frequencies[i] + plus_minus - 1:
+ byte_list.append("1")
+ else:
+ byte_list.append("0")
+
+ return byte_list
+
+def play_frequency(freq, p, duration=1.0, samplingRate=44100):
+ amplitude = 1.0 # Maximum amplitude
+ samples = (amplitude * np.sin(2 * np.pi * np.arange(samplingRate * duration) * freq / samplingRate)).astype(np.float32).tobytes()
+ stream = p.open(format=pyaudio.paFloat32, channels=1, rate=samplingRate, output=True)
+ stream.write(samples)
+
+
+ # thread for listening here
+
+ stream.stop_stream()
+ stream.close()
+
+# def play_data(data, start_freq, freq_step, byte_per_transmit):
+# p = pyaudio.PyAudio()
+# freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit)
+# print(freq_list, data)
+
+# def play_thread(freq):
+# play_frequency(freq, p=p)
+
+# threads = []
+# for item in data:
+# for i, bit in enumerate(item):
+# if bit == '1':
+# thread = threading.Thread(target=play_thread, args=(freq_list[i],))
+# threads.append(thread)
+# thread.start()
+
+# for thread in threads:
+# thread.join()
+
+# p.terminate()
+
+def play_data(data, start_freq, freq_step, byte_per_transmit, p):
+ freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit)
+
+
+ threads = []
+ for item in data:
+ for i, bit in enumerate(item):
+ if bit == '1':
+ thread = threading.Thread(target=play_frequency, args=(freq_list[i], p, 1.0))
+ threads.append(thread)
+ thread.start()
+
+ for thread in threads:
+ thread.join()
+
+
+# def listen_for_confirmation(stream):
+# # Logic to listen and decode the confirmation data
+# # This function should run in its own thread
+
+# recieved_data = stream.read(1024)
- return byte_list \ No newline at end of file