gabisrau commited on
Commit
51a076c
·
1 Parent(s): 9b423d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py CHANGED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from math import log2, pow
2
+
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from scipy.fftpack import fft
6
+
7
+ import gradio as gr
8
+
9
+ A4 = 440
10
+ C0 = A4 * pow(2, -4.75)
11
+ name = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
12
+
13
+
14
+ def get_pitch(freq):
15
+ h = round(12 * log2(freq / C0))
16
+ n = h % 12
17
+ return name[n]
18
+
19
+
20
+ def main_note(audio):
21
+ rate, y = audio
22
+ if len(y.shape) == 2:
23
+ y = y.T[0]
24
+ N = len(y)
25
+ T = 1.0 / rate
26
+ x = np.linspace(0.0, N * T, N)
27
+ yf = fft(y)
28
+ yf2 = 2.0 / N * np.abs(yf[0 : N // 2])
29
+ xf = np.linspace(0.0, 1.0 / (2.0 * T), N // 2)
30
+
31
+ volume_per_pitch = {}
32
+ total_volume = np.sum(yf2)
33
+ for freq, volume in zip(xf, yf2):
34
+ if freq == 0:
35
+ continue
36
+ pitch = get_pitch(freq)
37
+ if pitch not in volume_per_pitch:
38
+ volume_per_pitch[pitch] = 0
39
+ volume_per_pitch[pitch] += 1.0 * volume / total_volume
40
+ return volume_per_pitch
41
+
42
+
43
+ iface = gr.Interface(
44
+ main_note,
45
+ "audio",
46
+ gr.outputs.Label(num_top_classes=4),
47
+ examples=[
48
+ ["audio/recording1.wav"],
49
+ ["audio/cantina.wav"],
50
+ ],
51
+ interpretation="default",
52
+ )
53
+
54
+ iface.launch()