aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Doan <daviddoan@Davids-MacBook-Pro-193.local>2023-12-14 18:18:38 -0500
committerDavid Doan <daviddoan@Davids-MacBook-Pro-193.local>2023-12-14 18:18:38 -0500
commit7da49af8e3d7910ba6d5caba46c114bdf0418358 (patch)
tree734f5a5ca429ec0ce63ab03e0991834a040668b5
parentf21474ef27f630d093a116b82fc2147b775eb832 (diff)
parent3ca213d829b6c56beb764eae430a78736d73eeca (diff)
utils and threading updates
-rw-r--r--Sender.py33
-rw-r--r--visualize.py34
2 files changed, 54 insertions, 13 deletions
diff --git a/Sender.py b/Sender.py
index 16c419a..8ebe503 100644
--- a/Sender.py
+++ b/Sender.py
@@ -12,11 +12,9 @@ Play a single frequency.
:param samplingRate: Sampling rate in Hz.
"""
def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100, p=None):
-
# Generate sample for the given frequency as a float32 array
samples = (amplitude * np.sin(2*np.pi*np.arange(samplingRate*duration)*freq/samplingRate)).astype(np.float32).tobytes()
-
# Open stream
stream = p.open(format=pyaudio.paFloat32,
channels=1,
@@ -29,6 +27,36 @@ 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.
@@ -39,6 +67,7 @@ Use threads to play multiple frequencies simultaneously.
"""
def play_frequencies_separately(freq_map, duration=1.0, samplingRate=44100):
p = pyaudio.PyAudio()
+
threads = []
for freq, amplitude in freq_map.items():
thread = threading.Thread(target=play_frequency, args=(freq, amplitude, duration, samplingRate, p))
diff --git a/visualize.py b/visualize.py
index d1b2a08..7a86373 100644
--- a/visualize.py
+++ b/visualize.py
@@ -43,13 +43,27 @@ class Test(object):
scaled_spectrum = np.abs(spectrum)
scaled_spectrum = scaled_spectrum / (np.linalg.norm(scaled_spectrum) + 1e-16)
- # get the index of the max
- np_max = max(scaled_spectrum)
- max_index = np.where(scaled_spectrum == np_max)[0][0]
-
- # the index corresponds to the following linspace
- index_to_freq = np.linspace(0, self.RATE, self.CHUNK)
- return index_to_freq[max_index], scaled_spectrum
+ # FIXME: update to self values, given if ur a sender or receiver
+ starting_freq = 18000
+ end_freq = 20000
+ freq_to_index_ratio = (self.CHUNK - 1) / self.RATE
+ # only accept the scaled spectrum from our starting range to 20000 Hz
+ starting_range_index = int(starting_freq * freq_to_index_ratio)
+ ending_range_index = int(end_freq * freq_to_index_ratio)
+ print(starting_freq, end_freq, starting_range_index, ending_range_index)
+ restricted_spectrum = scaled_spectrum[starting_range_index:ending_range_index + 1]
+
+ # get the n indices of the max peaks, within our confined spectrum
+ # FIXME: update to self values
+ bytes = 1
+ num_bits = bytes * 8 + 2
+ top8_indices = np.argpartition(restricted_spectrum, -num_bits)[-num_bits:]
+
+ # map the top 8 to indices to frequencies
+ top8_freqs = [int((top8_indices[i] + starting_freq) / freq_to_index_ratio) for i in range(len(top8_indices))]
+
+ # print(index_to_freq[max_index], max_index, max_index * self.RATE / (self.CHUNK - 1))
+ return top8_freqs[0], scaled_spectrum
def read_audio_stream(self):
data = self.stream.read(self.CHUNK)
@@ -61,10 +75,8 @@ class Test(object):
while not self.pause:
waveform = self.read_audio_stream()
freq_max, scaled_spectrum = self.get_fundamental_frequency(waveform)
- if 250 < freq_max < 550:
- if freq_max not in self.seenvalues:
- print(freq_max)
- self.seenvalues.add(freq_max)
+ if freq_max not in self.seenvalues:
+ print(freq_max)
# update figure canvas if wanted
if graphics: