aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2022-10-03 22:26:47 -0400
committerAnthony Wang2022-10-03 22:26:47 -0400
commitaf5e81248958bf7f57f2cb56742bef96c1942e70 (patch)
tree1ed4753b20fd76d46ec86551a8828f68e97787ad
parentfa498f76c8d7eb44cb23670d9c4b11af36e772fb (diff)
Try mic program again
-rw-r--r--mic.py46
1 files changed, 23 insertions, 23 deletions
diff --git a/mic.py b/mic.py
index 18b73e8..0c9aaf1 100644
--- a/mic.py
+++ b/mic.py
@@ -6,35 +6,35 @@ audio = pyaudio.PyAudio()
stream = audio.open(
format = pyaudio.paInt16,
channels = 1,
- rate=88200,
+ rate=200000,
input=True
)
# https://stackoverflow.com/questions/45908268/how-to-know-the-frequency-of-audio-from-microphone
-# https://stackoverflow.com/questions/3694918/how-to-extract-frequency-associated-with-fft-values-in-pytho
-curfreq = 0
-stop = False
-
+# https://stackoverflow.com/questions/3694918/how-to-extract-frequency-associated-with-fft-values-in-python
+playingfreq = 0
+lastfreq = 0
while True:
- chunk = stream.read(4096)
+ chunk = stream.read(10000)
w = np.fft.fft(list(chunk))[10:200]
- f = np.fft.fftfreq(8192)
+ f = np.fft.fftfreq(20000)
i = 10+np.argmax(np.abs(w))
mag = abs(w[i-10])
- freq = abs(2 * f[i] * 88200)
+ freq = abs(2 * f[i] * 200000)
- if mag > 100000 and curfreq == 0:
- # Detected mic input
- # Start new freq
- stop = False
- curfreq = freq
- print('Starting', curfreq)
- get('http://10.242.6.228:5000/startfreq/' + str(curfreq))
- elif mag <= 100000 or abs(curfreq-freq) > 20:
- if stop and curfreq != 0:
- print('Stopping', curfreq)
- get('http://10.242.6.228:5000/stopfreq/' + str(curfreq))
- curfreq = 0
- else:
- # Stop next round
- stop = True
+ if mag <= 200000:
+ freq = 0
+
+ print(freq)
+
+ if playingfreq != 0 and abs(freq-playingfreq) > 20 and abs(lastfreq-playingfreq) > 20:
+ # Stop playing
+ print('Stopping', playingfreq)
+ get('http://10.242.6.228:5000/stopfreq/' + str(playingfreq))
+ playingfreq = 0
+ if freq != 0 and playingfreq == 0 and abs(freq-lastfreq) <= 20:
+ # Nothing playing and freq matches last freq
+ print('Starting', freq)
+ get('http://10.242.6.228:5000/startfreq/' + str(freq))
+ playingfreq = freq
+ lastfreq = freq