-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencyDetection.py
More file actions
55 lines (45 loc) · 1.78 KB
/
frequencyDetection.py
File metadata and controls
55 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pyaudio
import numpy as np
from scipy.signal import find_peaks
def frequencyToNote(freq):
A4 = 440.0
C0 = A4 * 2**(-4.75)
noteNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
if freq == 0:
return "No note"
h = round(12 * np.log2(freq / C0))
octave = h // 12
n = h % 12
return noteNames[n] + str(octave)
def evaluatePitch(app, noteList):
data = app.stream.read(1024) #Chat GPT
framesData = np.frombuffer(data, dtype=np.int16) #Chat GPT
# Apply a window function
window = np.hanning(len(framesData)) #Chat GPT
framesData_windowed = framesData * window #Chat GPT
# Perform FFT
freq_data = np.fft.fft(framesData_windowed) #Chat GPT
freq_magnitude = np.abs(freq_data) #Chat GPT
# Find peaks in the frequency magnitude
peaks, properties = find_peaks(freq_magnitude[:len(freq_magnitude)//2], height=app.confidence_threshold, distance=5) #Chat GPT
if peaks.size > 0:
peak_index = peaks[np.argmax(properties['peak_heights'])] #Chat GPT
peak_freq = abs(np.fft.fftfreq(len(framesData), 1/44100)[peak_index]) #Chat GPT
note = frequencyToNote(peak_freq)
noteList.pop(0)
noteList.append(note)
if(noteList[0] == noteList[1] == noteList[2]):
app.noteDetected = True
return noteList[0]
else:
app.noteDetected = False
return None
def evaluateColor(note):
if(note == None):
return None
elif (note[:-1] == 'C' or note[:-1] == 'C#' or note[:-1] == 'F#' or note[:-1] == 'G'):
return 'red'
elif (note[:-1] == 'D' or note[:-1] == 'D#' or note[:-1] == 'G#' or note[:-1] == 'A'):
return 'blue'
elif (note[:-1] == 'E' or note[:-1] == 'F' or note[:-1] == 'A#' or note[:-1] == 'B'):
return 'green'