-
Notifications
You must be signed in to change notification settings - Fork 1
/
yin_pitch.py
executable file
·46 lines (34 loc) · 1.02 KB
/
yin_pitch.py
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
#!/usr/bin/env python3
import sys
from aubio import source, pitch
if len(sys.argv) < 2:
print("Usage: %s <filename> [samplerate]" % sys.argv[0])
sys.exit(1)
filename = sys.argv[1]
samplerate = 44100
if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
win_s = 2048 # fft size
hop_s = 2048 # hop size #512 #win_s/4
s = source(filename, samplerate, hop_s)
samplerate = s.samplerate
tolerance = 0.2
pitch_o = pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("midi")
pitch_o.set_tolerance(tolerance)
pitches = []
confidences = []
# total number of frames read
total_frames = 0
while True:
samples, read = s()
pitch = pitch_o(samples)[0]
pitch = int(round(pitch))
confidence = pitch_o.get_confidence()
if confidence < 0.99: pitch = 0
# print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence))
pitches += [pitch]
confidences += [confidence]
total_frames += read
if read < hop_s: break
if 0: sys.exit(0)
sys.stdout.write(str(pitches).replace(' ', '').strip('[]'))