forked from noio/Autotune-the-Everythings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabPitch.py
90 lines (71 loc) · 1.92 KB
/
grabPitch.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
import sys
from aubio.task import *
from music21 import *
from util import *
def getKey(filename, midinotes=True):
#
# Tries to estimate the key for an audio file
#
# returns a list of frequencies
#
nfo("Start getKey")
nfo("Calculating pitches")
mode = "yin"
bufsize = 4096
hopsize = float(bufsize) / 2
params = taskparams()
params.samplerate = float(sndfile(filename).samplerate())
params.hopsize = int(float(bufsize) / 2)
params.bufsize = bufsize
params.step = params.samplerate/float(hopsize)
if midinotes:
params.pitchmax = 83
params.units = "midi"
else:
params.pitchmax = 1000
params.units = "freq"
wplot,oplots,titles = [],[],[]
pitch = []
params.pitchmode = mode
filetask = taskpitch(filename,params=params)
pitch = filetask.compute_all()
nfo("Calculated pitches")
pitchlist = []
notes = stream.Stream()
i=0
for schnoof in pitch:
schnooi = int(schnoof)
if i%2==1 and schnooi!=-1:
pitchlist.append(schnooi)
n = note.Note()
n.pitch.midi = schnooi
n.quarterLength = 1
notes.append(n)
i+=1
nfo("KrumhanslKessler")
base = analysis.discrete.analyzeStream(notes, 'KrumhanslKessler').getScale().transpose(-36).pitches
pitches = []
nfo("KrumhanslKesslerized", True)
if midinotes:
for y in base:
n = note.Note()
n.pitch = y
if not n.pitch.midi in pitches:
pitches.append(n.pitch.midi)
for i in range(5):
n.pitch.midi += 12
if not n.pitch.midi in pitches:
pitches.append(n.pitch.midi)
else:
for y in base:
n = note.Note()
n.pitch = y
if not n.pitch.frequency in pitches:
pitches.append(n.pitch.frequency)
for i in range(5):
n.pitch.midi += 12
if not n.pitch.frequency in pitches:
pitches.append(n.pitch.frequency)
pitches.sort()
return pitches