aboutsummaryrefslogtreecommitdiff
path: root/Recv.py
diff options
context:
space:
mode:
Diffstat (limited to 'Recv.py')
-rw-r--r--Recv.py94
1 files changed, 50 insertions, 44 deletions
diff --git a/Recv.py b/Recv.py
index 7da9f06..76de3e8 100644
--- a/Recv.py
+++ b/Recv.py
@@ -1,47 +1,53 @@
-import utils as u
-import time
-from collections import Counter
+import struct
-def main():
- p = u.pyaudio.PyAudio()
- start_freq = 19800
- freq_range = 200
- bytes_per_transmit = 1
-
- stream = p.open(
- format=u.pyaudio.paInt32,
- channels=1,
- rate=44100,
- input=True,
- output=True,
- frames_per_buffer=2048 * 2,
- )
-
- char_counter = Counter()
- start_time = time.time()
- word = ''
-
- try:
+import numpy as np
+import pyaudio
+import threading
+from utils import *
+
+
+class Recv:
+ def __init__(self, start_freq=19500):
+ self.start_freq = start_freq
+ self.freq_range = 500
+ self.sampling_rate = 44100
+ self.p = pyaudio.PyAudio()
+ self.bytes_per_transmit = 1
+
+
+ # TODO: use stream to send back the data
+ self.CHUNK = 2048 * 2
+ self.FORMAT = pyaudio.paInt32
+ self.CHANNELS = 1
+ self.RATE = 44100
+ self.pause = False
+ # stream object
+ self.p = pyaudio.PyAudio()
+ self.stream = self.p.open(
+ format=self.FORMAT,
+ channels=self.CHANNELS,
+ rate=self.RATE,
+ input=True,
+ output=True,
+ frames_per_buffer=self.CHUNK,
+ )
+
+ def read_audio_stream(self):
+ data = self.stream.read(self.CHUNK)
+ data_int = struct.unpack(str(self.CHUNK) + 'i', data)
+ return data_int
+
+ def listen(self):
while True:
- current_time = time.time()
- if current_time - start_time >= 1: # Every second
- # Find the most common character
- most_common_char, _ = char_counter.most_common(1)[0] if char_counter else ('', 0)
- print(f"Most common character in the last second: {most_common_char}")
- word += most_common_char
- print(f"Accumulated word: {word}")
- char_counter.clear() # Reset for the next second
- start_time = current_time
-
- data, success = u.receive_data(stream, start_freq, freq_range, bytes_per_transmit)
- if success:
- char_counter[data] += 1
-
- except KeyboardInterrupt:
- print("Stopping...")
- finally:
- stream.stop_stream()
- stream.close()
- p.terminate()
+ data = self.read_audio_stream()
+ recv_freq_range = self.freq_range / 2
+ wave_to_bits(data, self.start_freq, recv_freq_range, self.bytes_per_transmit)
+
+
+def main():
+ recv = Recv()
+ recv.listen()
+
+
if __name__ == "__main__":
- main() \ No newline at end of file
+ main()