Last active
March 29, 2023 15:35
-
-
Save willwade/890b7cabe64542f8b11196223e1af16d to your computer and use it in GitHub Desktop.
Detect sounds to Keypress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.io.wavfile as wavfile | |
import scipy.signal as signal | |
# Load the audio file | |
fs, data = wavfile.read('sound_file.wav') | |
# Calculate the spectrogram | |
f, t, Sxx = signal.spectrogram(data, fs) | |
# Find the indices of the frequency bins that correspond to the minimum and maximum frequencies | |
min_idx = 0 | |
max_idx = len(f) - 1 | |
for i in range(len(f)): | |
if f[i] >= min_freq: | |
min_idx = i | |
break | |
for i in range(len(f)-1, -1, -1): | |
if f[i] <= max_freq: | |
max_idx = i | |
break | |
# Calculate the minimum and maximum frequencies | |
min_freq = f[min_idx] | |
max_freq = f[max_idx] | |
print('Minimum frequency:', min_freq) | |
print('Maximum frequency:', max_freq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sounddevice as sd | |
import numpy as np | |
import librosa | |
import pynput.keyboard as keyboard | |
# Set the minimum and maximum frequencies for the "ahh" sound | |
min_freq = 500 | |
max_freq = 2000 | |
# Set the minimum and maximum frequencies for the "ssss" sound | |
min_freq_s = 1000 | |
max_freq_s = 4000 | |
# Define the function to detect the "ahh" sound | |
def detect_ahh(indata, frames, time, status): | |
# Compute the mel spectrogram | |
S = librosa.feature.melspectrogram(y=indata[:, 0], sr=44100, n_mels=64, fmin=min_freq, fmax=max_freq) | |
# Flatten the spectrogram | |
S = S.flatten() | |
# Compute the RMS (root mean square) energy of the sound | |
rms = np.sqrt(np.mean(np.square(indata[:, 0]))) | |
# Check if the energy of the sound is above a certain threshold | |
if rms > 0.01: | |
# Compute the mean of the mel spectrogram | |
S_mean = np.mean(S) | |
# Check if the mean of the mel spectrogram is above a certain threshold | |
if S_mean > 50: | |
# Press the F1 key | |
keyboard.Controller().press(keyboard.Key.f1) | |
# Define the function to detect the "ssss" sound | |
def detect_ssss(indata, frames, time, status): | |
# Compute the mel spectrogram | |
S = librosa.feature.melspectrogram(y=indata[:, 0], sr=44100, n_mels=64, fmin=min_freq_s, fmax=max_freq_s) | |
# Flatten the spectrogram | |
S = S.flatten() | |
# Compute the RMS (root mean square) energy of the sound | |
rms = np.sqrt(np.mean(np.square(indata[:, 0]))) | |
# Check if the energy of the sound is above a certain threshold | |
if rms > 0.01: | |
# Compute the mean of the mel spectrogram | |
S_mean = np.mean(S) | |
# Check if the mean of the mel spectrogram is above a certain threshold | |
if S_mean > 50: | |
# Press the F2 key | |
keyboard.Controller().press(keyboard.Key.f2) | |
# Start the microphone recording | |
with sd.InputStream(callback=detect_ahh, blocksize=2048): | |
with sd.InputStream(callback=detect_ssss, blocksize=2048): | |
sd.sleep(10000) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
librosa==0.8.1 | |
numpy==1.21.5 | |
pynput==1.7.3 | |
pyaudio==0.2.11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment