-
Notifications
You must be signed in to change notification settings - Fork 1
/
essentia_chord_utils.py
144 lines (124 loc) · 5.15 KB
/
essentia_chord_utils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import re
import os
import numpy as np
from essentia.streaming import *
class ChordSegment :
startTime = 0.0
endTime = 0.0
symbol = ''
def __init__(self, startTime, endTime, symbol):
self.startTime = startTime
self.endTime = endTime
self.symbol = symbol
def __repr__(self):
return str(self.startTime) + '\t' + str(self.endTime) + '\t' + self.symbol
def mergeSegments(chordSegments) :
if (len(chordSegments) < 2) :
return chordSegments
res = []
currentSegment = chordSegments[0]
for segment in chordSegments[1:] :
if (segment.symbol == currentSegment.symbol):
currentSegment.endTime = segment.endTime
else:
res.append(currentSegment)
currentSegment = segment
res.append(currentSegment)
return res
def convertChordLabels(chordSegments) :
for segment in chordSegments :
segment.symbol = re.sub('m$', ':min', segment.symbol)
return chordSegments
def toMirexLab(startTime, endTime, onsets, symbols, strengths) :
if (len(onsets) < len(symbols) or len(symbols) != len(strengths)) :
raise ValueError("inappropriate lists lengths")
if (len(onsets) == len(symbols)) :
onsets = np.concatenate((onsets, [endTime]))
res = []
if (startTime < onsets[0]) :
res.append(ChordSegment(startTime, onsets[0], 'N'))
for i in xrange(len(symbols)) :
sym = symbols[i] if strengths[i] > 0 else 'N'
res.append(ChordSegment(onsets[i], onsets[i+1], sym))
if (res[-1].endTime < endTime) :
res.append(ChordSegment(res[-1].endTime, endTime, 'N'))
return convertChordLabels(mergeSegments(res))
def processFiles(inputDir, outputDir, processFunction) :
for file in [f for f in os.listdir(inputDir) if os.path.isfile(os.path.join(inputDir, f))] :
name, ext = os.path.splitext(file)
processFunction(os.path.join(inputDir, file), os.path.join(outputDir, name + '.lab'))
def tuning(infile):
chordHopSize = 2048
frameSize = 8192
loader = MonoLoader(filename=infile)
framecutter = FrameCutter(hopSize=chordHopSize, frameSize=frameSize)
windowing = Windowing(type="blackmanharris62")
spectrum = Spectrum()
spectralpeaks = SpectralPeaks(orderBy="frequency",
magnitudeThreshold=1e-05,
minFrequency=40,
maxFrequency=5000,
maxPeaks=10000)
tuning = TuningFrequency()
# use pool to store data
pool = essentia.Pool()
# connect algorithms together
loader.audio >> framecutter.signal
framecutter.frame >> windowing.frame >> spectrum.frame
spectrum.spectrum >> spectralpeaks.spectrum
spectralpeaks.magnitudes >> tuning.magnitudes
spectralpeaks.frequencies >> tuning.frequencies
tuning.tuningFrequency >> (pool, 'tonal.tuningFrequency')
tuning.tuningCents >> (pool, 'tonal.tuningCents')
# network is ready, run it
print 'Processing audio file...', infile
essentia.run(loader)
return np.average(pool['tonal.tuningFrequency'])
def smooth(x, window_len=11, window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
"""
y = np.zeros(x.shape)
for i in range(np.size(x,1)):
if np.size(x, 0) < window_len:
raise ValueError, "Input vector needs to be bigger than window size."
if window_len < 3:
return x
if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
xx = x[:, i]
s = np.r_[xx[window_len - 1:0:-1], xx, xx[-1:-window_len:-1]]
if window == 'flat': # moving average
w = np.ones(window_len, 'd')
else:
w = eval('np.' + window + '(window_len)')
start = int(window_len / 2)
end = start + len(xx)
y[:,i] = np.convolve(w / w.sum(), s, mode='valid')[start:end]
return y
def loadBeatsAndChroma(infile):
print 'Loading npz file...', infile
az = np.load(infile)
ls = az['arr_0']
beats = az['arr_1']
chroma = az['arr_2']
return ls[0], beats, chroma