1
0
mirror of https://git.exozy.me/a/Never-Gonna-Give-Beep-Up synced 2024-11-22 11:27:30 +00:00
Never-Gonna-Give-Beep-Up/mic.py

41 lines
1.1 KiB
Python
Raw Normal View History

2022-10-03 17:56:46 +00:00
import numpy as np
import pyaudio
from requests import get
audio = pyaudio.PyAudio()
stream = audio.open(
format = pyaudio.paInt16,
channels = 1,
rate=88200,
2022-10-03 17:56:46 +00:00
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
while True:
chunk = stream.read(4096)
w = np.fft.fft(list(chunk))[10:200]
f = np.fft.fftfreq(8192)
2022-10-03 17:56:46 +00:00
i = 10+np.argmax(np.abs(w))
mag = abs(w[i-10])
freq = abs(2 * f[i] * 88200)
2022-10-03 17:56:46 +00:00
if mag > 100000 and curfreq == 0:
2022-10-03 17:56:46 +00:00
# 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:
2022-10-03 17:56:46 +00:00
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