From ef083f3740516570e64112ed0aa6bb8220a88604 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Mon, 3 Oct 2022 17:54:32 -0400 Subject: [PATCH] Use pysinewave (terrible library :(((())))) --- beepemu | 30 ++++++++++++++++++++++++++++++ mic.py | 14 +++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100755 beepemu diff --git a/beepemu b/beepemu new file mode 100755 index 0000000..b74d197 --- /dev/null +++ b/beepemu @@ -0,0 +1,30 @@ +#!/usr/bin/python + +import math +import sys +import time +from pysinewave import SineWave + +i = 1 +f = 0 +l = 0 +while i < len(sys.argv): + if sys.argv[i] == '-f': + i += 1 + f = float(sys.argv[i]) + elif sys.argv[i] == '-l': + i += 1 + l = float(sys.argv[i])/1000 + elif sys.argv[i] == '-D': + i += 1 + time.sleep(float(sys.argv[i])/1000) + if f != 0 and l != 0: + print(f, l) + s = SineWave(pitch_per_second=1000) + s.set_frequency(f) + s.play() + time.sleep(l) + s.stop() + f = 0 + l = 0 + i += 1 diff --git a/mic.py b/mic.py index 9b518f3..18b73e8 100644 --- a/mic.py +++ b/mic.py @@ -6,7 +6,7 @@ audio = pyaudio.PyAudio() stream = audio.open( format = pyaudio.paInt16, channels = 1, - rate=44100, + rate=88200, input=True ) @@ -16,21 +16,21 @@ curfreq = 0 stop = False while True: - chunk = stream.read(2048) - w = np.fft.fft(list(chunk))[10:100] - f = np.fft.fftfreq(4196) + chunk = stream.read(4096) + w = np.fft.fft(list(chunk))[10:200] + f = np.fft.fftfreq(8192) i = 10+np.argmax(np.abs(w)) mag = abs(w[i-10]) - freq = abs(2 * f[i] * 44100) + freq = abs(2 * f[i] * 88200) - if mag > 50000 and curfreq == 0: + 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 <= 50000 or abs(curfreq-freq) > 20: + 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))