1
0
mirror of https://git.exozy.me/a/Never-Gonna-Give-Beep-Up synced 2024-11-24 22:17:29 +00:00

Try mic program again

This commit is contained in:
Anthony Wang 2022-10-03 22:26:47 -04:00
parent fa498f76c8
commit af5e812489
No known key found for this signature in database
GPG Key ID: 42A5B952E6DD8D38

46
mic.py
View File

@ -6,35 +6,35 @@ audio = pyaudio.PyAudio()
stream = audio.open( stream = audio.open(
format = pyaudio.paInt16, format = pyaudio.paInt16,
channels = 1, channels = 1,
rate=88200, rate=200000,
input=True input=True
) )
# https://stackoverflow.com/questions/45908268/how-to-know-the-frequency-of-audio-from-microphone # 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 # https://stackoverflow.com/questions/3694918/how-to-extract-frequency-associated-with-fft-values-in-python
curfreq = 0 playingfreq = 0
stop = False lastfreq = 0
while True: while True:
chunk = stream.read(4096) chunk = stream.read(10000)
w = np.fft.fft(list(chunk))[10:200] 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)) i = 10+np.argmax(np.abs(w))
mag = abs(w[i-10]) mag = abs(w[i-10])
freq = abs(2 * f[i] * 88200) freq = abs(2 * f[i] * 200000)
if mag > 100000 and curfreq == 0: if mag <= 200000:
# Detected mic input freq = 0
# Start new freq
stop = False print(freq)
curfreq = freq
print('Starting', curfreq) if playingfreq != 0 and abs(freq-playingfreq) > 20 and abs(lastfreq-playingfreq) > 20:
get('http://10.242.6.228:5000/startfreq/' + str(curfreq)) # Stop playing
elif mag <= 100000 or abs(curfreq-freq) > 20: print('Stopping', playingfreq)
if stop and curfreq != 0: get('http://10.242.6.228:5000/stopfreq/' + str(playingfreq))
print('Stopping', curfreq) playingfreq = 0
get('http://10.242.6.228:5000/stopfreq/' + str(curfreq)) if freq != 0 and playingfreq == 0 and abs(freq-lastfreq) <= 20:
curfreq = 0 # Nothing playing and freq matches last freq
else: print('Starting', freq)
# Stop next round get('http://10.242.6.228:5000/startfreq/' + str(freq))
stop = True playingfreq = freq
lastfreq = freq