/
standard_pitchdemo.cpp
354 lines (274 loc) · 12.6 KB
/
standard_pitchdemo.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
*
* This file is part of Essentia
*
* Essentia is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation (FSF), either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the Affero GNU General Public License
* version 3 along with this program. If not, see http://www.gnu.org/licenses/
*/
#include <iostream>
#include <essentia/algorithmfactory.h>
#include <essentia/essentiamath.h> // for the isSilent function
#include <essentia/pool.h>
using namespace std;
using namespace essentia;
using namespace standard;
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Error: wrong number of arguments" << endl;
cout << "Usage: " << argv[0] << " input_audiofile output_csvfile [0...5 algorithm]" << endl;
cout << "0: monophonic pitch estimation Yin FFT" << endl;
cout << "1: monophonic pitch estimation Melodia" << endl;
cout << "2: monophonic note transcription" << endl;
cout << "3: multi pitch estimation Klapuri" << endl;
cout << "4: multi pitch estimation Melodia" << endl;
cout << "5: predominant pitch estimation Melodia" << endl;
exit(1);
}
int algo=atoi(argv[3]);
/////// PARAMS //////////////
int framesize = 2048;
int hopsize = 128;
int sr = 44100;
// register the algorithms in the factory(ies)
essentia::init();
// instanciate factory
AlgorithmFactory& factory = AlgorithmFactory::instance();
// audio loader (we always need it...)
Algorithm* audioload = factory.create("MonoLoader",
"filename", argv[1],
"sampleRate", sr,
"downmix", "mix");
vector<Real> audio;
audioload->output("audio").set(audio);
audioload->compute();
switch (algo) {
/* ------ monophonic pitch Yin FFT ----------- */
case 0:
{
cout << "monophonic pitch estimation Yin FFT" << endl;
// create algorithms
Algorithm* frameCutter = factory.create("FrameCutter",
"frameSize", framesize,
"hopSize", hopsize,
"startFromZero", false);
Algorithm* window = factory.create("Windowing",
"type", "hann",
"zeroPadding", 0);
Algorithm* spectrum = factory.create("Spectrum",
"size", framesize);
Algorithm* pitchDetect = factory.create("PitchYinFFT",
"frameSize", framesize,
"sampleRate", sr);
// configure frameCutter:
vector<Real> frame;
frameCutter->input("signal").set(audio);
frameCutter->output("frame").set(frame);
// configure windowing:
vector<Real> windowedframe;
window->input("frame").set(frame);
window->output("frame").set(windowedframe);
// configure spectrum:
vector<Real> spec;
spectrum->input("frame").set(windowedframe);
spectrum->output("spectrum").set(spec);
// configure pitch extraction:
Real thisPitch = 0., thisConf = 0;
Real localTime=0.;
vector<Real> allPitches, allConf, time;
pitchDetect->input("spectrum").set(spec);
pitchDetect->output("pitch").set(thisPitch);
pitchDetect->output("pitchConfidence").set(thisConf);
// process:
while (true) {
frameCutter->compute();
if (!frame.size())
break;
if (isSilent(frame))
continue;
window->compute();
spectrum->compute();
pitchDetect->compute();
allPitches.push_back(thisPitch);
localTime+=float(hopsize)/float(sr);
time.push_back(localTime);
allConf.push_back(thisConf);
}
// write to csv file
ofstream outfile(argv[2]);
cout << "time pitch [Hz] pitch confidence" << endl;
for (int i=0; i<(int)time.size(); i++){
outfile << time[i] << ", " << allPitches[i] << ", " << allConf[i] << endl;
}
outfile.close();
// clean up
delete frameCutter;
delete spectrum;
delete pitchDetect;
break;
}
/* -------------------------------------------- */
case 1:
{
cout << "1: monophonic pitch estimation Melodia" << endl;
// create algorithms
Algorithm* pitchDetect = factory.create("PitchMelodia",
"sampleRate", sr, "hopSize", hopsize, "frameSize", framesize);
// configure
vector<Real> pitch, pitchConfidence;
pitchDetect->input("signal").set(audio);
pitchDetect->output("pitch").set(pitch);
pitchDetect->output("pitchConfidence").set(pitchConfidence);
// process
pitchDetect->compute();
// write to csv file
ofstream outfile(argv[2]);
cout << "time pitch [Hz] pitch confidence" << endl;
for (int i=0; i<(int)pitch.size(); i++){
outfile << float(i)*float(hopsize)/float(sr) << ", " << pitch[i] << ", " << pitchConfidence[i] << endl;
}
outfile.close();
// clean up
delete pitchDetect;
break;
}
case 2:
{
cout << "2: monophonic note transcription" << endl;
// create algorithms
Algorithm* pitchDetect = factory.create("PitchMelodia",
"sampleRate", sr, "hopSize", hopsize, "frameSize", framesize);
Algorithm* noteSeg = factory.create("PitchContourSegmentation", "sampleRate", sr, "hopSize", hopsize);
// configure pitch detection
vector<Real> pitch, pitchConfidence;
pitchDetect->input("signal").set(audio);
pitchDetect->output("pitch").set(pitch);
pitchDetect->output("pitchConfidence").set(pitchConfidence);
// configure note segmentation
vector<Real> onset, duration, MIDIpitch;
noteSeg->input("pitch").set(pitch);
noteSeg->input("signal").set(audio);
noteSeg->output("onset").set(onset);
noteSeg->output("duration").set(duration);
noteSeg->output("MIDIpitch").set(MIDIpitch);
// process:
// extract monophonic melody using the MELODIA algorithm
pitchDetect->compute();
// segment the resulting contour into discrete note events
noteSeg->compute();
// write to csv file
ofstream outfile(argv[2]);
cout << "onset [s] duration [s] MIDI pitch" << endl;
for (int i=0; i<(int)MIDIpitch.size(); i++){
outfile << onset[i] << ", " << duration[i] << ", " << MIDIpitch[i] << endl;
}
outfile.close();
// clean up
delete pitchDetect;
delete noteSeg;
break;
}
case 3:
{
cout << "3: multi pitch estimation Klapuri" << endl;
// create algorithm
Algorithm* multiPitch = factory.create("MultiPitchKlapuri", "sampleRate", sr);
// configure algorithm
vector<vector<Real> >pitchMulti;
multiPitch->input("signal").set(audio);
multiPitch->output("pitch").set(pitchMulti);
// process:
multiPitch->compute();
// write to file
ofstream outfile(argv[2]);
cout << "time pitch [Hz]" << endl;
for (int ii=0; ii<(int)pitchMulti.size(); ii++){
for (int jj=0; jj<(int)pitchMulti[ii].size(); jj++){
outfile << float(ii)*hopsize/sr << ", " << pitchMulti[ii][jj] << endl;
}
}
outfile.close();
// clean up
delete multiPitch;
break;
}
case 4:
{
cout << "4: multi pitch estimation Melodia" << endl;
// create algorithms
Algorithm* predmelMulti = factory.create("MultiPitchMelodia", "sampleRate", sr, "frameSize", framesize, "hopSize", hopsize);
Algorithm* el = factory.create("EqualLoudness","sampleRate", sr);
// configure
vector<Real> audioEQ;
vector<vector<Real> >pitchMulti;
el->input("signal").set(audio);
el->output("signal").set(audioEQ);
predmelMulti->input("signal").set(audioEQ);
predmelMulti->output("pitch").set(pitchMulti);
// process:
el->compute();
predmelMulti->compute();
// write to file
ofstream outfile(argv[2]);
cout << "time pitch [Hz]" << endl;
for (int ii=0; ii<(int)pitchMulti.size(); ii++){
for (int jj=0; jj<(int)pitchMulti[ii].size(); jj++){
outfile << float(ii)*float(hopsize)/float(sr) << ", " << pitchMulti[ii][jj] << endl;
}
}
outfile.close();
break;
}
case 5:
{
cout << "5: predominant pitch estimation Melodia" << endl;
// create algorithms
Algorithm* pitchDetect = factory.create("PredominantPitchMelodia",
"sampleRate", sr, "hopSize", hopsize, "frameSize", framesize);
// configure pitch detection
vector<Real> pitch, pitchConfidence;
pitchDetect->input("signal").set(audio);
pitchDetect->output("pitch").set(pitch);
pitchDetect->output("pitchConfidence").set(pitchConfidence);
// process:
// extract predominant melody using the MELODIA algorithm
pitchDetect->compute();
// write to csv file
ofstream outfile(argv[2]);
cout << "time [s] pitch [Hz] pitch confidence" << endl;
for (int i=0; i<(int)pitch.size(); i++){
outfile << float(i)*hopsize/sr << ", " << pitch[i] << ", " << pitchConfidence[i] << endl;
}
outfile.close();
// clean up
delete pitchDetect;
break;
}
default:
{
cout << "Usage: " << argv[0] << " input_audiofile output_csvfile [0...5 algorithm]" << endl;
cout << "0: monophonic pitch estimation Yin FFT" << endl;
cout << "1: monophonic pitch estimation Melodia" << endl;
cout << "2: multi pitch estimation Klapuri" << endl;
cout << "3: multi pitch estimation Melodia" << endl;
cout << "4: predominant pitch estimation Melodia" << endl;
cout << "5: monophonic note transcription" << endl;
break;
}
}
// clean up
delete audioload;
// shut down essentia
essentia::shutdown();
return 0;
}