1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 QM Vamp Plugin Set
5
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 /*transcription vamp plugin: Ruohua Zhou, Josh Reiss, josh.reiss@elec.qmul.ac.uk */
16
17 #include "Transcription.h"
18 #include <vamp-sdk/PluginAdapter.h>
19 #include <math.h>
20 #include <stdlib.h>
21
22 using std::string;
23 using std::vector;
24 using std::cerr;
25 using std::endl;
26
27 const double EualCurve960[960] = {
28 83.750025,83.532690,83.315770,83.099260,82.883159,82.667463,82.452170,82.237276,82.022779,81.808675,
29 81.594963,81.381639,81.168699,80.956142,80.743964,80.532163,80.320735,80.109677,79.898987,79.688663,79.478700,79.269096,79.059848,78.850953,
30 78.642408,78.434211,78.226359,78.018848,77.811676,77.604839,77.398336,77.192162,76.986316,76.780794,76.575593,76.370710,76.166143,75.961889,
31 75.757945,75.554307,75.350973,75.147940,74.945205,74.742766,74.540618,74.338761,74.137189,73.935902,73.734895,73.534166,73.333712,73.133529,
32 72.933616,72.733970,72.534586,72.335463,72.136598,71.937987,71.739628,71.541517,71.343653,71.146032,70.948650,70.751506,70.554597,70.357919,
33 70.161469,69.965245,69.769244,69.573462,69.377898,69.182548,68.987408,68.792477,68.597752,68.403228,68.208905,68.014781,67.820873,67.627197,
34 67.433772,67.240617,67.047749,66.855187,66.662949,66.471053,66.279516,66.088358,65.897597,65.707250,65.517336,65.327873,65.138879,64.950373,
35 64.762372,64.574894,64.387959,64.201583,64.015785,63.830584,63.645997,63.462043,63.278739,63.096105,62.914158,62.732915,62.552397,62.372620,
36 62.193602,62.015363,61.837920,61.661291,61.485494,61.310549,61.136471,60.963274,60.790941,60.619447,60.448770,60.278885,60.109770,59.941401,
37 59.773755,59.606807,59.440536,59.274916,59.109924,58.945538,58.781733,58.618486,58.455773,58.293572,58.131858,57.970608,57.809799,57.649407,
38 57.489408,57.329780,57.170498,57.011539,56.852880,56.694496,56.536366,56.378464,56.220768,56.063255,55.905900,55.748680,55.591571,55.434551,
39 55.277595,55.120681,54.963784,54.806886,54.649983,54.493077,54.336169,54.179261,54.022353,53.865448,53.708546,53.551650,53.394759,53.237877,
40 53.081003,52.924139,52.767287,52.610448,52.453624,52.296815,52.140023,51.983250,51.826496,51.669763,51.513053,51.356366,51.199705,51.043070,
41 50.886463,50.729885,50.573337,50.416821,50.260338,50.103890,49.947478,49.791103,49.634766,49.478469,49.322214,49.166001,49.009832,48.853710,
42 48.697648,48.541659,48.385757,48.229958,48.074273,47.918719,47.763308,47.608055,47.452974,47.298080,47.143385,46.988904,46.834652,46.680642,
43 46.526889,46.373405,46.220207,46.067307,45.914720,45.762460,45.610540,45.458976,45.307780,45.156968,45.006553,44.856549,44.706971,44.557832,
44 44.409146,44.260928,44.113192,43.965951,43.819220,43.673013,43.527344,43.382227,43.237676,43.093703,42.950305,42.807478,42.665218,42.523520,
45 42.382381,42.241794,42.101757,41.962264,41.823311,41.684894,41.547008,41.409648,41.272811,41.136491,41.000685,40.865387,40.730594,40.596301,
46 40.462503,40.329195,40.196375,40.064036,39.932175,39.800787,39.669867,39.539412,39.409417,39.279876,39.150787,39.022143,38.893942,38.766178,
47 38.638846,38.511944,38.385465,38.259405,38.133761,38.008525,37.883679,37.759203,37.635076,37.511278,37.387789,37.264588,37.141656,37.018971,
48 36.896513,36.774262,36.652197,36.530298,36.408545,36.286918,36.165395,36.043957,35.922583,35.801253,35.679947,35.558643,35.437322,35.315964,
49 35.194547,35.073052,34.951458,34.829745,34.707892,34.585879,34.463686,34.341293,34.218678,34.095822,33.972704,33.849303,33.725600,33.601574,
50 33.477205,33.352481,33.227425,33.102069,32.976445,32.850585,32.724520,32.598284,32.471906,32.345420,32.218858,32.092250,31.965629,31.839028,
51 31.712477,31.586009,31.459655,31.333448,31.207419,31.081601,30.956024,30.830722,30.705725,30.581067,30.456777,30.332890,30.209436,30.086447,
52 29.963955,29.841993,29.720591,29.599783,29.479599,29.360071,29.241233,29.123114,29.005748,28.889166,28.773400,28.658474,28.544378,28.431095,
53 28.318607,28.206897,28.095947,27.985740,27.876257,27.767481,27.659396,27.551982,27.445224,27.339102,27.233599,27.128699,27.024383,26.920633,
54 26.817433,26.714764,26.612609,26.510951,26.409772,26.309053,26.208779,26.108930,26.009491,25.910442,25.811766,25.713446,25.615465,25.517804,
55 25.420446,25.323374,25.226570,25.130016,25.033695,24.937589,24.841681,24.745955,24.650409,24.555043,24.459856,24.364847,24.270016,24.175363,
56 24.080887,23.986588,23.892466,23.798520,23.704751,23.611156,23.517737,23.424492,23.331422,23.238526,23.145803,23.053253,22.960877,22.868672,
57 22.776640,22.684779,22.593090,22.501572,22.410224,22.319046,22.228038,22.137200,22.046530,21.956029,21.865697,21.775532,21.685535,21.595704,
58 21.506041,21.416544,21.327213,21.238047,21.149047,21.060211,20.971540,20.883034,20.794691,20.706512,20.618496,20.530642,20.442952,20.355423,
59 20.268057,20.180852,20.093808,20.006925,19.920202,19.833640,19.747237,19.660994,19.574910,19.488985,19.403218,19.317610,19.232159,19.146866,
60 19.061729,18.976750,18.891927,18.807260,18.722749,18.638393,18.554193,18.470147,18.386255,18.302518,18.218934,18.135504,18.052227,17.969105,
61 17.886151,17.803379,17.720805,17.638444,17.556310,17.474419,17.392786,17.311425,17.230351,17.149581,17.069127,16.989007,16.909233,16.829822,
62 16.750789,16.672148,16.593914,16.516103,16.438729,16.361808,16.285354,16.209382,16.133907,16.058945,15.984510,15.910617,15.837282,15.764518,
63 15.692342,15.620768,15.549811,15.479486,15.409809,15.340793,15.272455,15.204808,15.137869,15.071646,15.006129,14.941300,14.877144,14.813643,
64 14.750781,14.688540,14.626906,14.565860,14.505386,14.445467,14.386088,14.327231,14.268879,14.211016,14.153626,14.096691,14.040195,13.984121,
65 13.928453,13.873174,13.818267,13.763716,13.709504,13.655615,13.602031,13.548736,13.495714,13.442948,13.390420,13.338115,13.286016,13.234107,
66 13.182369,13.130788,13.079346,13.028026,12.976813,12.925693,12.874671,12.823756,12.772958,12.722285,12.671746,12.621351,12.571107,12.521025,
67 12.471113,12.421380,12.371835,12.322488,12.273346,12.224419,12.175717,12.127248,12.079020,12.031044,11.983328,11.935880,11.888711,11.841828,
68 11.795242,11.748960,11.702993,11.657348,11.612035,11.567063,11.522441,11.478178,11.434282,11.390764,11.347631,11.304893,11.262558,11.220637,
69 11.179137,11.138068,11.097437,11.057252,11.017521,10.978252,10.939452,10.901129,10.863290,10.825944,10.789098,10.752760,10.716937,10.681638,
70 10.646869,10.612640,10.578956,10.545827,10.513259,10.481261,10.449840,10.419004,10.388760,10.359117,10.330082,10.301663,10.273867,10.246702,
71 10.220176,10.194296,10.169071,10.144508,10.120615,10.097399,10.074868,10.053030,10.031892,10.011463,9.991749,9.972762,9.954523,9.937056,9.920385,
72 9.904534,9.889527,9.875389,9.862144,9.849815,9.838428,9.828005,9.818572,9.810152,9.802770,9.796449,9.791214,9.787089,9.784099,9.782266,9.781616,
73 9.782172,9.783959,9.787001,9.791322,9.796946,9.803897,9.812200,9.821878,9.832956,9.845457,9.859407,9.874829,9.891747,9.910185,9.930168,9.951720,
74 9.974864,9.999625,10.026008,10.053933,10.083304,10.114023,10.145991,10.179112,10.213287,10.248419,10.284410,10.321161,10.358576,10.396556,10.435004,
75 10.473821,10.512911,10.552175,10.591516,10.630835,10.670035,10.709018,10.747686,10.785942,10.823688,10.860826,10.897258,10.932886,10.967613,11.001341,
76 11.033972,11.065408,11.095552,11.124305,11.151570,11.177249,11.201245,11.223459,11.243793,11.262151,11.278450,11.292676,11.304827,11.314906,11.322913,
77 11.328848,11.332713,11.334508,11.334233,11.331889,11.327477,11.320998,11.312453,11.301841,11.289164,11.274422,11.257616,11.238747,11.217816,11.194822,
78 11.169767,11.142652,11.113476,11.082241,11.048948,11.013597,10.976189,10.936724,10.895203,10.851627,10.805996,10.758312,10.708574,10.656784,10.602942,
79 10.547049,10.489106,10.429113,10.367082,10.303073,10.237155,10.169399,10.099876,10.028655,9.955807,9.881403,9.805512,9.728206,9.649554,9.569627,9.488495,
80 9.406228,9.322897,9.238573,9.153325,9.067225,8.980341,8.892745,8.804508,8.715698,8.626388,8.536646,8.446544,8.356152,8.265539,8.174778,8.083937,7.993087,
81 7.902299,7.811643,7.721190,7.631008,7.541170,7.451746,7.362804,7.274417,7.186644,7.099504,7.013003,6.927151,6.841956,6.757424,6.673565,6.590385,6.507894,
82 6.426099,6.345008,6.264629,6.184970,6.106039,6.027843,5.950392,5.873692,5.797752,5.722579,5.648183,5.574570,5.501748,5.429727,5.358512,5.288114,5.218538,
83 5.149794,5.081890,5.014832,4.948630,4.883292,4.818824,4.755236,4.692535,4.630729,4.569826,4.509834,4.450761,4.392616,4.335415,4.279172,4.223905,4.169630,
84 4.116362,4.064118,4.012914,3.962766,3.913691,3.865703,3.818820,3.773058,3.728432,3.684960,3.642656,3.601538,3.561621,3.522921,3.485455,3.449239,3.414289,
85 3.380620,3.348250,3.317194,3.287469,3.259090,3.232074,3.206437,3.182194,3.159363,3.137959,3.117999,3.099498,3.082473,3.066939,3.052914,3.040413,3.029451,
86 3.020039,3.012186,3.005904,3.001201,2.998087,2.996571,2.996665,2.998377,3.001718,3.006696,3.013323,3.021607,3.031559,3.043187,3.056503,3.071516,3.088235,
87 3.106671,3.126833,3.148731,3.172374,3.197773,3.224938,3.253877,3.284601,3.317120,3.351444,3.387581,3.425543,3.465339,3.506978,3.550470,3.595826,3.643054,
88 3.692166,3.743169,3.796075,3.850896,3.907655,3.966377,4.027088,4.089815,4.154581,4.221415,4.290340,4.361382,4.434569,4.509924,4.587474,4.667245,4.749261,
89 4.833550,4.920136,5.009046,5.100305,5.193938,5.289972,5.388432,5.489343,5.592732,5.698625,5.807046,5.918022,6.031578,6.147741,6.266535,6.387986,6.512121,
90 6.638964,6.768542,6.900880,7.036004,7.173939,7.314712,7.458348,7.604856,7.754175,7.906227,8.060936,8.218223,8.378012,8.540225,8.704784,8.871612,9.040631,
91 9.211765,9.384934,9.560063,9.737073,9.915888,10.096429,10.278619,10.462380,10.647636,10.834309,11.022321,11.211594,11.402052,11.593616,11.786210,11.979755,
92 12.174175,12.369392,12.565329,12.761907,12.959049,13.156679,13.354718,13.553089,13.751715,13.950518,14.149420,14.348345,14.547211,14.745925,14.944391,
93 15.142512,15.340191,15.537333,15.733840,15.929615,16.124564
94 };
95 void Transcribe(int Len,int inputLen,double *SoundIn,double *out,double *outArray2,double *outArray3,double SampleRate);
96
Transcription(float inputSampleRate)97 Transcription::Transcription(float inputSampleRate) :
98 Plugin(inputSampleRate),
99 m_stepSize(0)
100 {
101 m_SoundIn=0;
102 m_SampleN=0;
103 m_AllocN = 0;
104 m_Excess = false;
105 }
106
~Transcription()107 Transcription::~Transcription()
108 {
109 free(m_SoundIn);
110 }
111
112 string
getIdentifier() const113 Transcription::getIdentifier() const
114 {
115 return "qm-transcription";
116 }
117
118 string
getName() const119 Transcription::getName() const
120 {
121 return "Polyphonic Transcription";
122 }
123
124 string
getDescription() const125 Transcription::getDescription() const
126 {
127 return "Transcribe the input audio to estimated notes";
128 }
129
130 string
getMaker() const131 Transcription::getMaker() const
132 {
133 return "Queen Mary, University of London";
134 }
135
136 int
getPluginVersion() const137 Transcription::getPluginVersion() const
138 {
139 return 1;
140 }
141
142 string
getCopyright() const143 Transcription::getCopyright() const
144 {
145 return "Plugin by Dr. Ruohua Zhou. Copyright (c) 2008-2009 QMUL - All Rights Reserved";
146 }
147
148 size_t
getPreferredStepSize() const149 Transcription::getPreferredStepSize() const
150 {
151 return 441;
152 }
153
154 size_t
getPreferredBlockSize() const155 Transcription::getPreferredBlockSize() const
156 {
157 return 441;
158 }
159
160 bool
initialise(size_t channels,size_t stepSize,size_t blockSize)161 Transcription::initialise(size_t channels, size_t stepSize, size_t blockSize)
162 {
163 if (channels < getMinChannelCount() ||
164 channels > getMaxChannelCount()) return false;
165
166 if (m_inputSampleRate < 4410 || m_inputSampleRate > 441000) return false;
167
168 m_stepSize = std::min(stepSize, blockSize);
169 m_blockSize=blockSize;
170
171 m_SampleN = 0;
172
173 return true;
174 }
175
176 void
reset()177 Transcription::reset()
178 {
179 free(m_SoundIn);
180 m_SoundIn = 0;
181 m_SampleN = 0;
182 m_AllocN = 0;
183 m_Excess = false;
184 m_Base = Vamp::RealTime();
185 }
186
187 Transcription::OutputList
getOutputDescriptors() const188 Transcription::getOutputDescriptors() const
189 {
190 OutputList list;
191
192 OutputDescriptor zc;
193 zc.identifier = "transcription";
194 zc.name = "Transcription";
195 zc.description = "Estimated note pitch (MIDI note number from 0 to 127)";
196 zc.unit = "MIDI units";
197 zc.hasFixedBinCount = true;
198 zc.binCount = 1;
199 zc.hasKnownExtents = true;
200 zc.minValue = 0;
201 zc.maxValue = 127;
202 zc.isQuantized = true;
203 zc.quantizeStep = 1.0;
204 zc.hasDuration = true;
205 zc.sampleType = OutputDescriptor::VariableSampleRate;
206
207 /* no -- this is the result of a confusion between bin indices and values
208 {
209
210 zc.binNames.push_back("A0");// MIDI Note 21
211 zc.binNames.push_back("A0#");
212 zc.binNames.push_back("B0");
213 zc.binNames.push_back("C1");
214 zc.binNames.push_back("C1#");
215 zc.binNames.push_back("D1");
216 zc.binNames.push_back("D1#");
217 zc.binNames.push_back("E1");
218 zc.binNames.push_back("F1");
219 zc.binNames.push_back("F1#");
220 zc.binNames.push_back("G1");
221 zc.binNames.push_back("G1#");
222
223 zc.binNames.push_back("A1");// MIDI Note 33
224 zc.binNames.push_back("A1#");
225 zc.binNames.push_back("B1");
226 zc.binNames.push_back("C2");
227 zc.binNames.push_back("C2#");
228 zc.binNames.push_back("D2");
229 zc.binNames.push_back("D2#");
230 zc.binNames.push_back("E2");
231 zc.binNames.push_back("F2");
232 zc.binNames.push_back("F2#");
233 zc.binNames.push_back("G2");
234 zc.binNames.push_back("G2#");
235
236 zc.binNames.push_back("A2");// MIDI Note 45
237 zc.binNames.push_back("A2#");
238 zc.binNames.push_back("B3");
239 zc.binNames.push_back("C3");
240 zc.binNames.push_back("C3#");
241 zc.binNames.push_back("D3");
242 zc.binNames.push_back("D3#");
243 zc.binNames.push_back("E3");
244 zc.binNames.push_back("F3");
245 zc.binNames.push_back("F3#");
246 zc.binNames.push_back("G3");
247 zc.binNames.push_back("G3#");
248
249 zc.binNames.push_back("A3");// MIDI Note 57
250 zc.binNames.push_back("A3#");
251 zc.binNames.push_back("B0");
252 zc.binNames.push_back("C4");
253 zc.binNames.push_back("C4#");
254 zc.binNames.push_back("D4");
255 zc.binNames.push_back("D4#");
256 zc.binNames.push_back("E4");
257 zc.binNames.push_back("F4");
258 zc.binNames.push_back("F4#");
259 zc.binNames.push_back("G4");
260 zc.binNames.push_back("G4#");
261
262 zc.binNames.push_back("A4");// MIDI Note 69
263 zc.binNames.push_back("A4#");
264 zc.binNames.push_back("B0");
265 zc.binNames.push_back("C5");
266 zc.binNames.push_back("C5#");
267 zc.binNames.push_back("D5");
268 zc.binNames.push_back("D5#");
269 zc.binNames.push_back("E5");
270 zc.binNames.push_back("F5");
271 zc.binNames.push_back("F5#");
272 zc.binNames.push_back("G5");
273 zc.binNames.push_back("G5#");
274
275 zc.binNames.push_back("A5");// MIDI Note 85
276 zc.binNames.push_back("A5#");
277 zc.binNames.push_back("B0");
278 zc.binNames.push_back("C6");
279 zc.binNames.push_back("C6#");
280 zc.binNames.push_back("D6");
281 zc.binNames.push_back("D6#");
282 zc.binNames.push_back("E6");
283 zc.binNames.push_back("F6");
284 zc.binNames.push_back("F6#");
285 zc.binNames.push_back("G6");
286 zc.binNames.push_back("G6#");
287
288 zc.binNames.push_back("A6");// MIDI Note 93
289 zc.binNames.push_back("A6#");
290 zc.binNames.push_back("B0");
291 zc.binNames.push_back("C7");
292 zc.binNames.push_back("C7#");
293 zc.binNames.push_back("D7");
294 zc.binNames.push_back("D7#");
295 zc.binNames.push_back("E7");
296 zc.binNames.push_back("F7");
297 zc.binNames.push_back("F7#");
298 zc.binNames.push_back("G7");
299 zc.binNames.push_back("G7#");
300
301 zc.binNames.push_back("A7");// MIDI Note 105
302 zc.binNames.push_back("A7#");
303 zc.binNames.push_back("B0");
304 zc.binNames.push_back("C8");
305 }
306 */
307
308 list.push_back(zc);
309
310 /* zc.identifier = "Transcriptions";
311 zc.name = "Polyphonic Transcription";
312 zc.description = "Polyphonic Music Transcription";
313 zc.unit = "";
314 zc.hasFixedBinCount = true;
315 zc.binCount = 0;
316 zc.sampleType = OutputDescriptor::VariableSampleRate;
317 zc.sampleRate = m_inputSampleRate;
318 list.push_back(zc);*/
319
320 return list;
321 }
322
323 Transcription::FeatureSet
process(const float * const * inputBuffers,Vamp::RealTime timestamp)324 Transcription::process(const float *const *inputBuffers,
325 Vamp::RealTime timestamp)
326 {
327 if (m_stepSize == 0) {
328 cerr << "ERROR: Transcription::process: "
329 << "Transcription has not been initialised"
330 << endl;
331 return FeatureSet();
332 }
333
334 if (m_SampleN == 0) {
335 m_Base = timestamp;
336 }
337
338 if (m_Excess) return FeatureSet();
339
340 for (int i = 0; i < m_blockSize;i++) {
341
342 if (m_SampleN >= m_AllocN) {
343 int newsize = m_AllocN * 2;
344 if (newsize < 10000) newsize = 10000;
345 double *newbuf = (double *)realloc(m_SoundIn, newsize * sizeof(double));
346 if (!newbuf) {
347 m_Excess = true;
348 break;
349 }
350 m_SoundIn = newbuf;
351 m_AllocN = newsize;
352 }
353
354 m_SoundIn[m_SampleN]=inputBuffers[0][i];
355 m_SampleN=m_SampleN+1;
356 }
357
358 return FeatureSet();
359 }
360
361 Transcription::FeatureSet
getRemainingFeatures()362 Transcription::getRemainingFeatures()
363 {
364 FeatureSet returnFeatures;
365
366 double * OutArray;
367 double *OutArray2;
368 double *hello1;
369 double *hello2;
370 int Msec;
371 int i;
372 int j;
373 int n;
374
375 Msec=(int)(100*m_SampleN/m_inputSampleRate);
376
377 if (Msec < 100) return returnFeatures;
378
379 OutArray=(double *)malloc(3*3000*sizeof(double));
380 OutArray2=(double *)malloc(88*Msec*sizeof(double));
381 hello1=(double *)malloc(112*Msec*sizeof(double));
382 hello2=(double *)malloc(112*Msec*sizeof(double));
383
384 for (j = 0; j <Msec; j++) {
385
386 for(n=0;n<88;n++)
387 {
388
389 OutArray2[j*88+n]=0;
390 }
391
392 }
393
394
395 Transcribe(Msec,m_SampleN,m_SoundIn,hello1,hello2,OutArray,m_inputSampleRate);
396 int start;
397 int endd;
398
399
400 /* for (i = 0; i < 3000; i++) {
401
402 if((OutArray[3*i]>0)&&(OutArray[3*i]<88))
403 {
404 start=OutArray[3*i+1];endd=OutArray[3*i+2];
405 for(j=start;j<(start+0.05);j=j+0.01)
406 {
407
408 Feature feature;
409 Vamp::RealTime ts;
410
411 feature.hasTimestamp = true;
412
413 feature.timestamp =ts.fromSeconds(j);
414 feature.values.push_back(OutArray[3*i]+21);
415 returnFeatures[0].push_back(feature);
416
417 }
418
419
420
421 }
422 else
423 {
424
425 break;
426 }
427
428
429 }
430
431 */
432
433
434 for (i = 0; i < 3000; i++) {
435
436 if((OutArray[3*i]>0)&&(OutArray[3*i]<88))
437 {
438 start=100*OutArray[3*i+1];
439 endd=100*OutArray[3*i+2]-5;
440 for(j=start;j<endd;j++)
441 {
442 n=OutArray[3*i];
443 OutArray2[j*88+n]=OutArray[3*i];
444 }
445
446 }
447 else
448 {
449
450 break;
451 }
452
453
454 }
455
456 double starts[88];
457 for (n = 0; n < 88; ++n) starts[n] = -1.0;
458
459 for (j = 0; j <Msec; j++) {
460
461 for(n=0;n<88;n++)
462 {
463 if(OutArray2[j*88+n]>0)
464 {
465
466 if (starts[n] < 0.)
467 {
468 starts[n] = j * 0.01;
469 }
470 }
471 else
472 {
473 if (starts[n] > 0.)
474 {
475 Feature feature;
476 feature.hasTimestamp = true;
477 feature.timestamp = m_Base + Vamp::RealTime::fromSeconds(starts[n]);
478 feature.hasDuration = true;
479 feature.duration = Vamp::RealTime::fromSeconds(j * 0.01 - starts[n]);
480 feature.values.push_back(n+20);
481 returnFeatures[0].push_back(feature);
482
483 starts[n] = -1.0;
484 }
485 }
486 }
487 }
488
489
490 for(n=0;n<88;n++)
491 {
492 if (starts[n] > 0.)
493 {
494 Feature feature;
495 feature.hasTimestamp = true;
496 feature.timestamp = m_Base + Vamp::RealTime::fromSeconds(starts[n]);
497 feature.hasDuration = true;
498 feature.duration = Vamp::RealTime::fromSeconds(j * 0.01 - starts[n]);
499 feature.values.push_back(n+20);
500 returnFeatures[0].push_back(feature);
501
502 starts[n] = -1.0;
503 }
504 }
505
506 free(OutArray2);
507 free(OutArray);
508
509 free(hello1);
510 free(hello2);
511
512 return returnFeatures;
513
514 }
515
516
517
518
519
sofacomplexMex(double * y,double * z,int ncols,double StartNote,double NoteInterval1,double NoteNum,double C,double D,double SR)520 void sofacomplexMex(double *y, double *z, int ncols,double StartNote,double NoteInterval1,double NoteNum,double C,double D,double SR)
521 {
522 int mseconds,i,el,count,count2;
523 double Snote,NoteInterval,NoteN;
524 double *signs;
525 double *rwork;
526 double freq,R,gain,gainI,gainII,coefI,coefM;
527 double output,input,outputI,outputM;
528 double *x;
529 double *sum,*sum2;
530 double power;
531
532 //SR=44100;
533 Snote=StartNote;
534 NoteInterval=NoteInterval1;
535 NoteN=NoteNum;
536
537 signs=(double*)malloc((int)NoteN*5*sizeof(double));
538
539 for (i = 0; i <NoteN; i++) {
540
541 freq=exp((log(2.0))*(Snote+i*NoteInterval-69)/12)*440;
542 R=exp(-(D+C*freq*2*3.1415926)/(SR*3.1415926));
543 gain=(1*(sqrt(1+R*R-2*R*cos(2*freq*2*3.1415926/SR)))-1*R*(sqrt(1+R*R-2*R*cos(2*freq*2*3.1415926/SR))))/sin(freq*2*3.1415926/SR);
544 gainI=-2*R*cos(freq*2*3.1415926/SR);
545 gainII =R*R ;
546 coefI=cos(freq*2*3.1415926/SR);
547 coefM=sin(freq*2*3.1415926/SR);
548
549 signs[i*5+0]=gain*gain;
550 signs[i*5+1]=gainI;
551 signs[i*5+2]=gainII;
552 signs[i*5+3]=coefI;
553 signs[i*5+4]=coefM;
554
555 }
556
557 x=(double*)malloc((int)NoteN*2*sizeof(double));
558 rwork=(double*)malloc((int)NoteN*sizeof(double));
559 sum=(double*)malloc((int)NoteN*sizeof(double));
560 sum2=(double*)malloc((int)NoteN*sizeof(double));
561 mseconds=(int)(100*ncols/SR);
562 power=0;
563 for (i=0;i<mseconds*(int)(SR/100);i++)
564 {
565 power=power+y[i]*y[i];
566 }
567 power=sqrt(power);
568 for(i=0;i<NoteN*2;i++)
569 x[i]=0;
570 for (i=0;i<NoteN;i++)
571 {
572 sum[i]=0;
573 sum2[i]=0;
574 };
575 count=0;
576 count2=0;
577 for (i=0;i<(mseconds*(int)(SR/100));i++)
578 {
579 count=count+1;
580 input=y[i];
581 for(el=0;el<NoteN;el++)
582 {
583 output=(input-signs[5*el+1]*x[2*el+0]-signs[5*el+2]*x[2*el+1]);
584 outputI=output-signs[5*el+3]*x[2*el+0];
585 outputM=signs[5*el+4]*x[2*el+0];
586 sum[el]=sum[el]+signs[5*el+0]*(outputI*outputI+ outputM*outputM);
587 rwork[el]=output;
588 x[el+el+1]=x[el+el+0];
589 x[el+el+0]=rwork[el];
590
591 }
592 if(count==(int)(SR/100))
593 {
594 for(el=0;el<NoteN;el++)
595 {
596 *(z+count2*(int)NoteN+el)=1000000*(sum[el]+sum2[el])/(2*(int)(SR/100))+0.00001;
597 sum2[el]=sum[el];
598 sum[el]=0;
599 }
600 count2=count2+1;
601 count=0;
602 }
603
604 }
605 for (i=0;i<NoteN;i++)
606 {
607 sum[i]=0;
608 sum2[i]=0;
609 };
610 for (el=0;el<NoteN;el++)
611 {
612 for (i=0;i<mseconds;i++)
613 {
614 sum[el]=sum[el]+*(z+i*(int)NoteN+el);
615 }
616
617 }
618
619 free(x);
620 free(rwork);
621 free(sum);
622 free(sum2);
623 free(signs);
624
625 }
626
FindMaxN(double * InputArray,int InputLen,int MaxOrder)627 void FindMaxN( double *InputArray, int InputLen,int MaxOrder)
628 {
629 int i,j,MaxIndex = 0;
630 double MaxValue;
631 double *In2;
632
633 In2=(double*)malloc(InputLen*sizeof(double));
634 for (i=0;i<InputLen;i++)
635 {
636 In2[i]=InputArray[i];
637 InputArray[i]=0;
638 }
639 for (i=0;i<MaxOrder;i++)
640 {
641 MaxValue=0;
642 for (j=0;j<InputLen;j++)
643 {
644 if(In2[j]>MaxValue)
645 {
646 MaxValue=In2[j];
647 MaxIndex=j;
648 }
649 }
650 InputArray[MaxIndex]=In2[MaxIndex];
651 In2[MaxIndex]=0;
652 }
653
654 free(In2);
655 }
656
SumF(double * InputArray,int Start,int End)657 double SumF(double *InputArray,int Start, int End)
658 {
659 double Value;
660 int i;
661 Value=0;
662 for (i=Start;i<(End+1);i++)
663 {
664 Value=Value+InputArray[i];
665 }
666
667 return Value;
668
669 }
670
round10(int x)671 int round10(int x)
672 {
673 int I,I2;
674 I=((int)(x/10));
675 I2=x-I*10;
676
677 if(I2>5)
678 return (I+1);
679 else
680 return I;
681
682 }
683
684
ConToPitch1250(double * In,int InLen)685 void ConToPitch1250(double *In, int InLen)
686 {
687 int i,j,k, nn,col;
688 double *Out;
689 const int A[12]={0, 120, 190, 240, 279, 310, 337, 360, 380, 399, 415, 430};
690 Out=(double*)malloc(InLen*sizeof(double));
691
692
693 col=InLen;
694
695 for (i=0;i<col;i++)
696 {
697 Out[i]=0;
698 }
699
700 for (i=0;i<col;i++)
701 {
702 k=0;
703 nn=5;
704 for (j=0;j<nn;j++)
705 {
706 if((i+A[j])<col)
707 {
708 k=k+1;
709 Out[i]=Out[i]+In[i+A[j]];
710 }
711
712 if((i+A[j])>(col-1))
713 {
714 k=k+1;
715 Out[i]=Out[i]+In[col-1];
716 }
717 }
718 if(k>0)
719 {
720 Out[i]=Out[i]/k;
721 }
722 }
723 for (i=0;i<col;i++)
724 {
725 In[i]=Out[i];
726 }
727
728
729 free(Out);
730 }
731
Norm1(double * In,int InLen)732 void Norm1(double *In, int InLen)
733 {
734 double MaxValue;
735 int i;
736 double *Out;
737 Out=(double*)malloc(InLen*sizeof(double));
738
739 MaxValue=In[0];
740 for (i=1;i<InLen;i++)
741 {
742 if(In[i]>MaxValue)
743 MaxValue=In[i];
744 }
745
746 for (i=0;i<InLen;i++)
747 {
748 Out[i]=In[i]-MaxValue;
749 }
750
751 for (i=0;i<InLen;i++)
752 {
753 In[i]=Out[i];
754 }
755
756 free(Out);
757 }
758
Smooth(double * In,int InLen,int smoothLen)759 void Smooth(double *In, int InLen,int smoothLen)
760 {
761 double sum;
762 int i,j,nn,n,count;
763 double *Out;
764 Out=(double*)malloc(InLen*sizeof(double));
765 nn=InLen;
766 n=(smoothLen-1)/2;
767 for (i=0;i<nn;i++)
768 {
769 sum=0;
770 count=0;
771 for (j=0;j<(n+1);j++)
772 {
773 if ((i-j)>-1)
774 {
775 sum=sum+In[i-j];
776 count=count+1;
777 }
778 }
779
780 for (j=1;j<(n+1);j++)
781 {
782 if ((i+j)<nn)
783 {
784 sum=sum+In[i+j];
785 count=count+1;
786 }
787 }
788 Out[i]=sum/count;
789 }
790 for (i=0;i<InLen;i++)
791 In[i]=Out[i];
792
793 free(Out);
794 }
795
796
FindPeaks(double * In,int InLen,double * Out1,double * Out2,int,int db2,int db3)797 void FindPeaks(double *In, int InLen,double *Out1,double *Out2, int /* db */, int db2, int db3)
798 {
799 int i,lastout;
800 for (i=0;i<InLen;i++)
801 {
802 Out1[i]=0;
803 Out2[1]=0;
804 }
805
806 for (i=20;i<(InLen-20-1);i++)
807 {
808 if( /**/ ((In[i]>(db2+In[i-6]))||(In[i]>(db2+In[i+6]))
809 ||(In[i]>(db3+In[i+20]))||(In[i]>(db3+In[i-20])))
810 /*&&(In[i]>db)*/&&(In[i]>In[i+3])&&(In[i]>In[i-3])
811 &&(In[i]>In[i+2])&&(In[i]>In[i-2])
812 &&(In[i]>In[i+1])&&(In[i]>In[i-1]))
813 {
814 Out1[i]=In[i];
815 Out2[i]=1;
816 }
817
818 }
819
820 lastout=1;
821 for(i=0;i<InLen;i++)
822 {
823
824 if(Out2[i]==1)
825 {
826 if((i-lastout)<5)
827 {
828 if(Out1[i]>Out1[lastout])
829 {
830 Out2[lastout]=0;
831 Out1[lastout]=0;
832 lastout=i;
833 }
834 else
835 {
836 Out2[i]=0;
837 Out1[i]=0;
838 }
839
840 }
841 else
842 {
843 lastout=i;
844 }
845 }
846
847 }
848
849 }
850
851
ConFrom1050To960(double * In,double * out,int InputLen)852 void ConFrom1050To960(double *In, double *out, int InputLen)
853 {
854 int i,j;
855
856 for(i=0;i<960;i++)
857 {
858 for (j=0;j<InputLen;j++)
859 {
860 out[i+j*960]=In[i+j*1050];
861
862 }
863 }
864
865
866 }
867
Move(double * InputArray,int InputLen,int m)868 void Move( double *InputArray, int InputLen,int m)
869 {
870 int i;
871 double *OutArray;
872
873 OutArray=(double *)malloc(InputLen*sizeof(double));
874 for (i=0;i<InputLen;i++)
875 OutArray[i]=0;
876
877 for (i=0;i<InputLen;i++)
878 { if(((i+m)>-1)&&((i+m)<InputLen))
879 OutArray[i+m]=InputArray[i];
880 }
881
882 for (i=0;i<InputLen;i++)
883 {
884 InputArray[i]=OutArray[i];
885 }
886
887 free(OutArray);
888 }
889
890
SumArray(double * InputArray,int InputHLen,int InputVLen)891 double SumArray( double *InputArray, int InputHLen, int InputVLen)
892 {
893 int i;
894 int j;
895 double sum;
896 int count;
897 count=0;
898 sum=0;
899 for (j=0;j<InputHLen;j++)
900 {
901 for (i=0;i<InputVLen;i++)
902 {
903 count=count+1;
904 sum=sum+InputArray[i+j*InputVLen];
905
906 }
907 }
908 return sum;
909 }
910
Sum(double * InputArray,int InputHLen)911 double Sum( double *InputArray, int InputHLen)
912 {
913 int i;
914 double sum;
915 int count;
916 count=0;
917 sum=0;
918 for (i=0;i<InputHLen;i++)
919 {
920 count=count+1;
921 sum=sum+InputArray[i];
922
923 }
924 return sum;
925 }
926
MeanV2(double * InputArray,int InputHLen,int InputVLen,double * OutArray)927 void MeanV2( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
928 {
929 int i;
930 int j;
931 double sum;
932 for (i=0;i<InputVLen;i++)
933 {
934 sum=0;
935 for (j=0;j<InputHLen;j++)
936 {
937
938 sum=sum+InputArray[i+j*InputVLen];
939
940 }
941 OutArray[i]=sum/InputHLen;
942 }
943
944 }
945
SumV(double * InputArray,int InputHLen,int InputVLen,double * OutArray)946 void SumV( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
947 {
948 int i;
949 int j;
950 double sum;
951
952 for (j=0;j<InputHLen;j++)
953 {
954 sum=0;
955 for (i=0;i<InputVLen;i++)
956 {
957
958 sum=sum+InputArray[i+j*InputVLen];
959
960 }
961 OutArray[j]=sum;
962 }
963
964 }
965
966
967
SumV2(double * InputArray,int InputHLen,int InputVLen,double * OutArray)968 void SumV2( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
969 {
970 int i;
971 int j;
972 double sum;
973 for (i=0;i<InputVLen;i++)
974 {
975 sum=0;
976 for (j=0;j<InputHLen;j++)
977 {
978
979 sum=sum+InputArray[i+j*InputVLen];
980
981 }
982 OutArray[i]=sum;
983 }
984
985 }
986
MaxV(double * InputArray,int InputHLen,int InputVLen,double * OutArray)987 void MaxV( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
988 {
989 int i;
990 int j;
991 double MaxVal;
992
993 for (j=0;j<InputHLen;j++)
994 {
995 MaxVal=InputArray[j*InputVLen];
996 for (i=0;i<InputVLen;i++)
997 {
998 if(InputArray[i+j*InputVLen]>MaxVal)
999 {
1000 MaxVal=InputArray[i+j*InputVLen];
1001 }
1002
1003 }
1004 OutArray[j]=MaxVal;
1005 }
1006
1007 }
1008
MaxV2(double * InputArray,int InputHLen,int InputVLen,double * OutArray)1009 void MaxV2( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
1010 {
1011 int i;
1012 int j;
1013 double MaxVal;
1014 for (i=0;i<InputVLen;i++)
1015 {
1016 MaxVal=InputArray[i];
1017 for (j=0;j<InputHLen;j++)
1018 {
1019 if(InputArray[i+j*InputVLen]>MaxVal)
1020 {
1021 MaxVal=InputArray[i+j*InputVLen];
1022 }
1023
1024 }
1025 OutArray[i]=MaxVal;
1026 }
1027
1028 }
1029
1030
1031
MinArray(double * InputArray,int InputHLen,int InputVLen,double MinValue)1032 void MinArray( double *InputArray, int InputHLen, int InputVLen, double MinValue)
1033 {
1034 int i;
1035 int j;
1036
1037 for (i=0;i<InputVLen;i++)
1038 {
1039 for (j=0;j<InputHLen;j++)
1040 {
1041 if(InputArray[i+j*InputVLen]<MinValue)
1042 InputArray[i+j*InputVLen]=MinValue;
1043
1044 }
1045
1046 }
1047
1048 }
1049
1050
MaxArray(double * InputArray,int InputHLen,int InputVLen,double MaxValue)1051 void MaxArray( double *InputArray, int InputHLen, int InputVLen, double MaxValue)
1052 {
1053 int i;
1054 int j;
1055
1056 for (i=0;i<InputVLen;i++)
1057 {
1058 for (j=0;j<InputHLen;j++)
1059 {
1060 if(InputArray[i+j*InputVLen]>MaxValue)
1061 InputArray[i+j*InputVLen]=MaxValue;
1062
1063 }
1064
1065 }
1066
1067 }
1068
GetMaxValue(double * InputArray,int InputHLen,int InputVLen)1069 double GetMaxValue( double *InputArray, int InputHLen, int InputVLen)
1070 {
1071 int i;
1072 int j;
1073
1074 double MaxValue;
1075 MaxValue=InputArray[0];
1076 for (i=0;i<InputVLen;i++)
1077 {
1078 for (j=0;j<InputHLen;j++)
1079 {
1080 if(InputArray[i*InputHLen+j]>MaxValue)
1081 MaxValue=InputArray[i*InputHLen+j];
1082
1083 }
1084
1085 }
1086
1087 return MaxValue;
1088 }
1089
RemoveNoise(double * InputArray,int InputHLen,int InputVLen)1090 void RemoveNoise( double *InputArray, int InputHLen, int InputVLen)
1091 {
1092 int i;
1093 int j;
1094
1095 for (i=0;i<InputVLen;i++)
1096 {
1097 for (j=0;j<InputHLen;j++)
1098 {
1099
1100 InputArray[i+j*InputVLen]=InputArray[i+j*InputVLen]-EualCurve960[i];
1101
1102 }
1103
1104 }
1105
1106 }
MeanArray(double * InputArray,int InputHLen,int InputVLen)1107 double MeanArray( double *InputArray, int InputHLen, int InputVLen)
1108 {
1109 int i;
1110 int j;
1111 double sum;
1112 int count;
1113 count=0;
1114 sum=0;
1115 for (j=0;j<InputHLen;j++)
1116 {
1117 for (i=0;i<InputVLen;i++)
1118 {
1119 count=count+1;
1120 sum=sum+InputArray[i+j*InputVLen];
1121
1122 }
1123 }
1124 return sum/count;
1125 }
Mydiff(double * InputArray,int InputHLen,int InputVLen,int n)1126 void Mydiff( double *InputArray, int InputHLen, int InputVLen,int n)
1127 {
1128 int i;
1129 int j;
1130 double * OutArray;
1131
1132 OutArray=(double*)malloc(InputHLen*InputVLen*sizeof(double));
1133
1134 for (i=0;i<InputVLen;i++)
1135 {
1136 for (j=n;j<InputHLen;j++)
1137 {
1138 OutArray[i+j*InputVLen]=InputArray[i+j*InputVLen]-InputArray[i+(j-n)*InputVLen];
1139
1140 }
1141 }
1142
1143 for (i=0;i<InputVLen;i++)
1144 {
1145 for (j=n;j<InputHLen;j++)
1146 {
1147 InputArray[i+j*InputVLen]=OutArray[i+j*InputVLen];
1148
1149 }
1150 }
1151
1152 for (i=0;i<InputVLen;i++)
1153 {
1154 for (j=0;j<n;j++)
1155 {
1156 InputArray[i+j*InputVLen]=0;
1157
1158 }
1159 }
1160
1161 free(OutArray);
1162 }
1163
PeakDetect(double * In,int InLen)1164 void PeakDetect(double *In, int InLen)
1165 {
1166 int i;
1167 double *Out1;
1168
1169 Out1=(double*)malloc(InLen*sizeof(double));
1170 for (i=0;i<InLen;i++)
1171 {
1172 Out1[i]=0;
1173 }
1174
1175
1176
1177 for (i=2;i<(InLen-3);i++)
1178 {
1179 if( (In[i]>In[i+2])&&(In[i]>In[i-2])
1180 &&(In[i]>In[i+1])&&(In[i]>In[i-1]))
1181 {
1182 Out1[i]=In[i];
1183 }
1184
1185 }
1186
1187 for(i=0;i<InLen;i++)
1188 {
1189
1190 In[i]=Out1[i];
1191 }
1192
1193 free(Out1);
1194 }
MeanV(double * InputArray,int InputHLen,int InputVLen,double * OutArray)1195 void MeanV( double *InputArray, int InputHLen, int InputVLen, double *OutArray)
1196 {
1197 int i;
1198 int j;
1199 double sum;
1200
1201 for (j=0;j<InputHLen;j++)
1202 {
1203 sum=0;
1204 for (i=0;i<InputVLen;i++)
1205 {
1206
1207 sum=sum+InputArray[i+j*InputVLen];
1208
1209 }
1210 OutArray[j]=sum/InputVLen;
1211 }
1212
1213 }
Edetect(double * InputArray,int InputHLen,int InputVLen,double MinT,double db1,double * OutOne)1214 void Edetect(double *InputArray, int InputHLen, int InputVLen, double MinT, double db1,double *OutOne)
1215 {
1216 int i;
1217 int j;
1218 double MaxValue;
1219
1220 // printf(" Starting Energy Onset Detection.. %f\n",db1);
1221 RemoveNoise(InputArray, InputHLen, InputVLen);
1222
1223
1224 MaxValue=GetMaxValue(InputArray,InputHLen,InputVLen);
1225
1226 for (i=0;i<InputVLen;i++)
1227 {
1228 for (j=0;j<InputHLen;j++)
1229 {
1230 InputArray[i*InputHLen+j]=InputArray[i*InputHLen+j]-MaxValue;
1231
1232 }
1233
1234 }
1235
1236 MinArray(InputArray, InputHLen, InputVLen, -100);
1237 Mydiff(InputArray, InputHLen, InputVLen,3);
1238 MinArray(InputArray, InputHLen, InputVLen, MinT);
1239
1240 for (i=0;i<InputVLen;i++)
1241 {
1242 for (j=0;j<InputHLen;j++)
1243 {
1244 InputArray[i*InputHLen+j]=InputArray[i*InputHLen+j]-MinT;
1245
1246 }
1247
1248 }
1249
1250 MeanV(InputArray,InputHLen,InputVLen,OutOne);
1251 Smooth(OutOne, InputHLen,3);
1252 Smooth(OutOne, InputHLen,3);
1253 Move(OutOne,InputHLen,-2);
1254 PeakDetect(OutOne,InputHLen);
1255 MinArray(OutOne, InputHLen,1, db1);
1256
1257 for (j=0;j<InputHLen;j++)
1258 {
1259 OutOne[j]=OutOne[j]-db1;
1260
1261 }
1262 }
1263
1264
1265
OnsetDetection2(double * In,int InputLen,double * OutOne,double a,double b)1266 void OnsetDetection2(double *In,int InputLen,double *OutOne,double a,double b)
1267 {
1268 int mseconds;
1269 double *Input;
1270
1271
1272 mseconds=InputLen;
1273
1274 Input=(double *)malloc(mseconds*960*sizeof(double));
1275
1276 ConFrom1050To960(In,Input,InputLen);
1277
1278
1279
1280
1281 if(a>0)
1282 {
1283 Edetect(Input,mseconds,960, a,b,OutOne);
1284 }
1285
1286
1287 free(Input);
1288
1289 }
1290
PitchEstimation(double * In,int,double * OutArray,double * OutArray2)1291 void PitchEstimation(double *In, int /* InLen */, double *OutArray,double *OutArray2)
1292 {
1293 double *xx,*x,*y,*y1,*PeakPitch1, *PeakPitch2,*PeakInput1, *PeakInput2;
1294 double *out,*outValue;
1295 double *output,*output1;
1296 int *outc;
1297 double temp;
1298 int i,sumI;
1299 int Len;
1300
1301 Len=1050;
1302 xx=(double*)malloc(Len*sizeof(double));
1303 x=(double*)malloc(Len*sizeof(double));
1304 y=(double*)malloc(Len*sizeof(double));
1305 y1=(double*)malloc(Len*sizeof(double));
1306 PeakPitch1=(double*)malloc(Len*sizeof(double));
1307 PeakPitch2=(double*)malloc(Len*sizeof(double));
1308 PeakInput1=(double*)malloc(Len*sizeof(double));
1309 PeakInput2=(double*)malloc(Len*sizeof(double));
1310 out=(double*)malloc(Len*sizeof(double));
1311 outValue=(double*)malloc(Len*sizeof(double));
1312 output=(double*)malloc(112*sizeof(double));
1313 output1=(double*)malloc(112*sizeof(double));
1314 outc=(int*)malloc(112*sizeof(int));
1315 // yI=(double*)malloc(12*sizeof(double));
1316
1317
1318 for (i=0;i<Len;i++)
1319 {
1320 x[i]=In[i];
1321 }
1322
1323 for (i=0;i<Len;i++)
1324 {
1325 y1[i]=x[i];
1326 }
1327
1328 ConToPitch1250(y1,Len);
1329
1330 for (i=0;i<Len;i++)
1331 {
1332 y[i]=y1[i];
1333 }
1334
1335 Smooth(y,Len,30);
1336
1337 for (i=0;i<Len;i++)
1338 {
1339 y1[i]=y1[i]-y[i];
1340 }
1341
1342 for (i=0;i<Len;i++)
1343 {
1344 y1[i]=y1[i]+20;
1345 }
1346
1347 temp=0;
1348 for (i=0;i<Len;i++)
1349 {
1350 temp=temp+x[i];
1351 }
1352 temp=temp/Len;
1353 for (i=0;i<Len;i++)
1354 {
1355 y[i]=x[i]-temp;
1356 }
1357
1358
1359 for (i=0;i<Len;i++)
1360 {
1361 PeakPitch2[i]=0;
1362 PeakPitch1[i]=0;
1363 PeakInput1[i]=0;
1364 PeakInput2[i]=0;
1365 }
1366 FindPeaks(y1, Len,PeakPitch1,PeakPitch2, 0, -1000, -1000);
1367 FindPeaks(y, Len,PeakInput1,PeakInput2, 0, 6, 15);
1368
1369
1370
1371 sumI=0;
1372 for (i=0;i<Len;i++)
1373 {
1374 sumI=sumI+PeakPitch2[i];
1375 }
1376
1377
1378 if (sumI>12)
1379 {
1380 FindMaxN(PeakPitch1,Len,12);
1381
1382 for (i=0;i<Len;i++)
1383 {
1384 if(PeakPitch1[i]==0)
1385 {
1386 PeakPitch2[i]=0;
1387 }
1388 }
1389
1390 }
1391
1392 for (i=0;i<Len;i++)
1393 {
1394 out[i]=0;
1395 outValue[i]=0;
1396 }
1397
1398 for (i=0;i<(Len-300);i++)
1399 {
1400 if(PeakPitch2[i]==1)
1401
1402 {
1403 if (
1404 ((SumF(PeakInput2,i-4,i+4)>0)&&(SumF(PeakInput2,i+120-4,i+120+4)>0))
1405 ||((SumF(PeakInput2,i-4,i+4)>0)&&(SumF(PeakInput2,i+190-4,i+190+4)>0))
1406 ||((SumF(PeakInput2,i+190-4,i+190+4)>0)&&(SumF(PeakInput2,i+120-4,i+120+4)>0))
1407 )
1408 {
1409 out[i]=1;
1410 outValue[i]=y1[i];
1411
1412 }
1413 }
1414 }
1415
1416 for (i=0;i<112;i++)
1417 {
1418 output[i]=0;
1419 outc[i]=0;
1420 }
1421
1422
1423
1424 for (i=0;i<Len;i++)
1425 {
1426 if(out[i]==1)
1427 {
1428 output[20+round10(i+1)-1]=1;
1429 outc[20+round10(i+1)-1]=i;
1430 }
1431 }
1432
1433
1434 for (i=0;i<112;i++)
1435 {
1436 output1[i]=output[i];
1437 }
1438
1439
1440
1441 for (i=20;i<(112-28);i++)
1442 {
1443 if((output[i]>0)&&(SumF(PeakInput2,outc[i]-5,outc[i]+5)==0))
1444 {
1445 output1[i]=0;
1446 }
1447 }
1448
1449
1450 for (i=0;i<112;i++)
1451 {
1452 OutArray[i]=0;
1453 OutArray2[i]=0;
1454
1455 }
1456
1457 for(i=20;i<105;i++)
1458 {
1459 if(output1[i]==1)
1460 {
1461 OutArray[i]=outc[i]+200+2;
1462 OutArray2[i]=y[outc[i]];
1463 }
1464 }
1465
1466 free(xx); // xx=(double*)malloc(Len*sizeof(double));
1467 free(x); // x=(double*)malloc(Len*sizeof(double));
1468 free(y); // y=(double*)malloc(Len*sizeof(double));
1469 free(y1); // y1=(double*)malloc(Len*sizeof(double));
1470 free(PeakPitch1); //=(double*)malloc(Len*sizeof(double));
1471 free(PeakPitch2); //=(double*)malloc(Len*sizeof(double));
1472 free(PeakInput1); //=(double*)malloc(Len*sizeof(double));
1473 free(PeakInput2); //=(double*)malloc(Len*sizeof(double));
1474 free(out); //=(double*)malloc(Len*sizeof(double));
1475 free(outValue); //=(double*)malloc(Len*sizeof(double));
1476 free(output); //=(double*)malloc(112*sizeof(double));
1477 free(output1); //=(double*)malloc(112*sizeof(double));
1478 free(outc); //=(double*)malloc(112*sizeof(int));
1479 //free(yI); //=(double*)malloc(12*sizeof(int));
1480 // printf(" end free \n");
1481 }
1482
DoMultiPitch(double * In,int RLen,int CLen,double * Out1,double * Out2)1483 void DoMultiPitch(double *In, int RLen,int CLen, double *Out1, double *Out2)
1484 {
1485
1486 int i, j;
1487 double *sum1,*mean1;
1488 double MaxV;
1489 double *OutArray1, *OutArray2,*tempArray;
1490
1491 OutArray1=(double *)malloc(112*sizeof(double));
1492 OutArray2=(double *)malloc(112*sizeof(double));
1493 tempArray=(double *)malloc(RLen*sizeof(double));
1494
1495 sum1=(double*)malloc(CLen*sizeof(double));
1496 mean1=(double*)malloc(CLen*sizeof(double));
1497
1498 for (j=0;j<CLen;j++)
1499 {
1500 sum1[j]=0;
1501 for (i=0;i<RLen;i++)
1502 {
1503 sum1[j]=sum1[j]+In[j*RLen+i];
1504 }
1505
1506 mean1[j]=sum1[j]/CLen;
1507 }
1508 MaxV=mean1[0];
1509 for (j=0;j<CLen;j++)
1510 {
1511 if(mean1[j]>MaxV)
1512 {
1513 MaxV=mean1[j];
1514 }
1515 }
1516
1517 for (j=0;j<CLen;j++)
1518 {
1519 mean1[j]=mean1[j]-MaxV;
1520 }
1521
1522
1523 for (j=0;j<CLen;j++)
1524 {
1525
1526 for (i=0;i<112;i++)
1527 {
1528
1529 OutArray1[i]=0;
1530 OutArray2[i]=0;;
1531
1532 }
1533 MaxV=In[j*RLen];
1534 for(i=0;i<RLen;i++)
1535 {
1536 tempArray[i]=In[j*RLen+i];
1537 if(tempArray[i]>MaxV)
1538 MaxV=tempArray[i];
1539 }
1540
1541 if(mean1[j]>-55)
1542 {
1543
1544 PitchEstimation(tempArray,RLen,OutArray1,OutArray2);
1545
1546 for(i=0;i<112;i++)
1547 {
1548 if(OutArray1[i]>0)
1549 {
1550 if((MaxV-tempArray[(int)OutArray1[i]-201-1])>40)
1551 {
1552 OutArray1[i]=0;
1553 OutArray2[i]=0;
1554 }
1555
1556 }
1557 }
1558
1559 }
1560
1561 for (i=0;i<112;i++)
1562 {
1563
1564 Out1[j*112+i]=OutArray1[i];
1565 Out2[j*112+i]=OutArray2[i];
1566
1567 }
1568
1569 }
1570
1571 free(OutArray1);
1572 free(OutArray2);
1573 free(tempArray);
1574 free(sum1);
1575 free(mean1);
1576 }
1577
1578
OnsetToArray(double * In,int Len,double * OutStart,double * OutEnd)1579 int OnsetToArray(double *In, int Len, double *OutStart,double *OutEnd)
1580 {
1581 int count,i;
1582
1583 count=0;
1584
1585 for (i=0;i<Len;i++)
1586 {
1587 if(In[i]>0)
1588 {
1589 OutStart[count]=i+1;
1590 if(count>0)
1591 {
1592 OutEnd[count-1]=i+1;
1593 }
1594 count=count+1;
1595 }
1596 }
1597 if (count>0)
1598 {
1599 OutEnd[count-1]=Len;
1600 }
1601 return count;
1602
1603 }
dbfunction(double * InputArray,int InputHLen,int InputVLen,double * OutArray)1604 void dbfunction( double *InputArray, int InputHLen, int InputVLen,double *OutArray)
1605 {
1606 int i;
1607 int j;
1608
1609 for (i=0;i<InputVLen;i++)
1610 {
1611 for (j=0;j<InputHLen;j++)
1612 {
1613 OutArray[i*InputHLen+j]=20*log10(InputArray[i*InputHLen+j]);
1614
1615 }
1616
1617 }
1618 }
1619
Transcribe(int Len,int inputLen,double * SoundIn,double * out,double * outArray2,double * outArray3,double SampleRate)1620 void Transcribe(int Len,int inputLen,double *SoundIn,double *out,double *outArray2,double *outArray3,double SampleRate)
1621 {
1622 int OnsetN;
1623 int i,j,k;
1624 int count;
1625 int index;
1626 double *OutStart,*OutEnd;
1627 int start,endd,startb=1,start2,endd2;
1628 double *A1,*A2,*A3,*A4,*A5,*A6,*D,*D2;
1629 double *A6A;
1630 double *out2, *PitchOut1,*PitchOut2,*PitchOut3;
1631 double sum,maxV,maxVal;
1632 double *tempArray;
1633 double temp;
1634 double p;
1635 double M1,M2;
1636 double *In;
1637 int Len2;
1638 double *dbs,*ss,*dbs1,*jj;
1639 int TempInt;
1640
1641
1642 A1=(double *)malloc(112*sizeof(double));
1643 A2=(double *)malloc(112*sizeof(double));
1644 A3=(double *)malloc(112*sizeof(double));
1645 A4=(double *)malloc(112*sizeof(double));
1646 A5=(double *)malloc(112*sizeof(double));
1647 A6=(double *)malloc(112*sizeof(double));
1648 D=(double *)malloc(112*sizeof(double));
1649 D2=(double *)malloc(112*sizeof(double));
1650 PitchOut1=(double *)malloc(112*Len*sizeof(double));
1651 PitchOut2=(double *)malloc(112*Len*sizeof(double));
1652 PitchOut3=(double *)malloc(112*Len*sizeof(double));
1653 OutStart=(double *)malloc(Len*sizeof(double));
1654 OutEnd=(double *)malloc(Len*sizeof(double));
1655 tempArray=(double *)malloc(sizeof(double)*Len);
1656 In=(double *)malloc(sizeof(double)*Len);
1657 dbs=(double *)malloc(1050*sizeof(double)*Len);
1658 dbs1=(double *)malloc(210*sizeof(double)*Len);
1659 ss=(double *)malloc(210*sizeof(double)*Len);
1660 jj=(double *)malloc(1050*sizeof(double));
1661
1662
1663
1664
1665 for(k=0;k<1050;k++)
1666 {
1667
1668 jj[k]=k/5.0;
1669
1670 }
1671
1672 sofacomplexMex(SoundIn,ss,inputLen,20,0.5,210,0.03,20,SampleRate);
1673 dbfunction(ss, Len, 210,dbs1);
1674
1675 for(i=0;i<Len;i++)
1676 {
1677 for(k=0;k<1045;k++)
1678 {
1679 TempInt=(int)jj[k];
1680 dbs[k+i*1050]=(jj[k]-TempInt)*dbs1[TempInt+1+i*210]+(TempInt+1-jj[k])*dbs1[TempInt+i*210];
1681
1682 }
1683
1684 for (k=1045;k<1050;k++)
1685 {
1686 dbs[k+i*1050]=dbs[1044+i*1050];
1687 }
1688
1689 }
1690
1691 OnsetDetection2(dbs,Len,In,3,1.2);
1692 for (i=0;i<Len;i++)
1693 {
1694 outArray2[i]=In[i];
1695
1696 }
1697
1698 OnsetN=0;
1699 count=0;
1700 for (i=0;i<Len;i++)
1701 {
1702 if(In[i]>0)
1703 {
1704 OnsetN=OnsetN+1;
1705 count=count+1;
1706 }
1707 }
1708 Len2=count;
1709 out2=(double *)malloc(112*Len2*sizeof(double));
1710 A6A=(double *)malloc(112*Len2*sizeof(double));
1711 OnsetToArray(In,Len,OutStart,OutEnd);
1712 DoMultiPitch(dbs,1050,Len, PitchOut1, PitchOut2);
1713
1714
1715 for (i=0;i<Len;i++)
1716 {
1717 for (j=0;j<112;j++)
1718 {
1719 PitchOut3[i*112+j]=PitchOut1[i*112+j];
1720 if(PitchOut3[i*112+j]>1)
1721 PitchOut3[i*112+j]=1;
1722 }
1723
1724 }
1725
1726
1727 for (i=0;i<OnsetN;i++)
1728 {
1729 for(j=0;j<112;j++)
1730 {
1731 A1[j]=0;A2[j]=0;A3[j]=0;A4[j]=0;A5[j]=0;A6[j]=0;
1732
1733 }
1734 maxV=0;
1735 start=(int)OutStart[i];
1736 endd=(int)OutEnd[i];
1737 if(i>0)
1738 {
1739 startb=(int)OutStart[i-1];
1740 }
1741
1742 for (j=0;j<112;j++)
1743 {
1744 sum=0;
1745 count=0;
1746 for (k=(start-1);k<endd;k++)
1747 {
1748 sum=sum+PitchOut3[k*112+j];
1749 count=count+1;
1750 }
1751
1752 A1[j]=sum;
1753 A6[j]=sum/count;
1754 A6A[i*112+j]=sum/count;
1755
1756 }
1757
1758 for (j=0;j<112;j++)
1759 {
1760 maxVal=PitchOut2[start*112+j];
1761 for (k=(start-1);k<endd;k++)
1762 {
1763 if(PitchOut2[k*112+j]>maxVal)
1764 {
1765 maxVal=PitchOut2[k*112+j];
1766 }
1767
1768 }
1769
1770 A3[j]=maxVal;
1771
1772 }
1773
1774 for (j=0;j<112;j++)
1775 {
1776 sum=0;
1777 count=0;
1778 for (k=(start-1);k<endd;k++)
1779 {
1780 if(PitchOut2[k*112+j]>0)
1781 {
1782 sum=sum+PitchOut2[k*112+j];
1783 count=count+1;
1784 }
1785 }
1786 if(count>0)
1787 A4[j]=sum/count;
1788 else
1789 A4[j]=0;
1790 }
1791
1792
1793 for (j=0;j<112;j++)
1794 {
1795 sum=0;
1796 count=0;
1797 for (k=(start-1);k<endd;k++)
1798 {
1799 if(PitchOut1[k*112+j]>0)
1800 {
1801 sum=sum+PitchOut1[k*112+j];
1802 count=count+1;
1803 }
1804 }
1805 if(count>0)
1806 A5[j]=sum/count;
1807 else
1808 A5[j]=0;
1809 }
1810
1811 maxV=A3[0];
1812 for (j=0;j<112;j++)
1813 {
1814 if(A3[j]>maxV)
1815 maxV=A3[j];
1816 }
1817
1818 for (j=0;j<112;j++)
1819 {
1820
1821 if(A1[j]>0)
1822 {
1823 D[j]=A1[j];D2[j]=A1[j];
1824 }
1825
1826 else
1827 {
1828 D[j]=A1[j];D2[j]=A1[j];
1829 }
1830 }
1831
1832 for (j=0;j<112;j++)
1833 {
1834 if(A1[j]<8)
1835 {
1836 D[j]=0;D2[j]=0;
1837 }
1838
1839 }
1840
1841 for(j=0;j<112;j++)
1842 {
1843
1844 if ((j>12)&&(D[j]>0)&&(D[j-12]>0))
1845 {
1846 D[j]=0; D2[j]=0;
1847 if((A3[j]>45)&&(A3[j]>(A3[j-12]+3)))
1848 {
1849 D[j]=1;
1850 }
1851 }
1852
1853
1854 if ((j>19)&&(D[j]>0)&&(D[j-19]>0))
1855 {
1856
1857 D[j]=0; D2[j]=0;
1858 if((A3[j]>50))
1859 {
1860 D[j]=1;
1861 }
1862 }
1863
1864 if ((j>24)&&(D[j]>0)&&(D[j-24]>0))
1865 {
1866
1867 D[j]=0; D2[j]=0;
1868 if((A3[j]>50))
1869 {
1870 D[j]=1;
1871 }
1872 }
1873
1874 if ((j>28)&&(D[j]>0)&&(D[j-28]>0))
1875 {
1876
1877 D[j]=0; D2[j]=0;
1878 if((A3[j]>50))
1879 {
1880 D[j]=1;
1881 }
1882 }
1883
1884 if ((j>34)&&(fabs(A5[j]-337.0-A5[j-34])<3.0)&&(D[j]>0)&&(D[j-34]>0))
1885 {
1886
1887 D[j]=0; D2[j]=0;
1888 if((A4[j]>25)&&(A3[j]>40)&&(A3[j]>(A3[j-34]-3))&&((A1[j]>8)||(A6[j]>0.8)))
1889 {
1890 D[j]=1;
1891 }
1892 }
1893
1894 if((j>48)&&(j<59)&&(A3[j]<20))
1895 {
1896 D[j]=0;
1897 }
1898
1899 if((j>58)&&(j<69)&&(A3[j]<28))
1900 {
1901 D[j]=0;
1902 }
1903
1904
1905 if((j>68)&&(j<79)&&(A3[j]<40))
1906 {
1907 D[j]=0;
1908 }
1909
1910 if((j>78)&&(A3[j]<50))
1911 {
1912 D[j]=0;
1913 }
1914
1915 if((j>85)&&(A3[j]<55))
1916 {
1917 D[j]=0;
1918 }
1919
1920 if((D2[j]>0)&&(A1[j]>15))
1921 {
1922 D[j]=1;
1923 }
1924 if(i>1)
1925 {
1926
1927 for (k=(startb-1);k<start;k++)
1928 {
1929 tempArray[k-startb+1]=PitchOut3[j+k*112];
1930
1931 }
1932 temp=Sum(tempArray,start-startb+1);
1933 if(((maxV-A3[j])>20)&&(temp>3))
1934 {
1935 D[j]=0;
1936 }
1937
1938 }
1939
1940 }
1941
1942 for(j=0;j<112;j++)
1943 {
1944 out[j+i*112]=D[j];
1945 out2[j+i*112]=D[j];
1946 }
1947 }
1948
1949 for (i=1;i<OnsetN;i++)
1950 {
1951 start2=(int)OutStart[i];
1952 endd2=(int)OutEnd[i];
1953
1954 for (j=0;j<112;j++)
1955 {
1956 sum=0;
1957 count=0;
1958 for (k=(start2-1);k<endd2;k++)
1959 {
1960 sum=sum+PitchOut3[k*112+j];
1961 count=count+1;
1962 }
1963
1964 A1[j]=sum;
1965 }
1966
1967 for (j=0;j<112;j++)
1968 {
1969 if((out2[(i-1)*112+j]>0)&&(out[j+i*112]>0))
1970 {
1971 out[j+i*112]=0;
1972 sum=0;
1973 for(k=(start2-1);k<endd2;k++)
1974 {
1975 sum=sum+PitchOut1[j+k*112];
1976
1977 }
1978 p=sum/A1[j];
1979
1980 index=(int)(p+0.5)-200;
1981
1982 if((index>0)&&(i<(OnsetN-1))&&(start2>5))
1983 {
1984
1985 M1=dbs[index+(start2-1)*1050];
1986 for (k=(start2-1);k<(start2+10);k++)
1987 {
1988 if(dbs[index+k*1050]>M1)
1989 M1=dbs[index+k*1050];
1990
1991 }
1992
1993 M2=dbs[index+(start2-5-1)*1050];
1994 for (k=(start2-5-1);k<start2;k++)
1995 {
1996 if(dbs[index+k*1050]<M2)
1997 M2=dbs[index+k*1050];
1998
1999 }
2000
2001 if((M1-M2)>10)
2002 {
2003 out[j+i*112]=1;
2004 }
2005 }
2006 }
2007 }
2008 }
2009
2010 count=0;
2011 for (i=0;i<OnsetN;i++)
2012 {
2013
2014 start=(int)OutStart[i];
2015 endd=(int)OutEnd[i];
2016
2017 for(j=0;j<112;j++)
2018 {
2019 if(out[j+i*112]>0)
2020 {
2021 outArray3[count*3+0]=j+1-21;//exp((log(2.0))*(j+1-69)/12)*440;
2022 outArray3[count*3+1]=start*0.01;
2023
2024 if(i==(OnsetN-1))
2025 {
2026 outArray3[count*3+2]=0.01*OutEnd[i];
2027 }
2028 else
2029 {
2030
2031 for(k=(i+1);k<OnsetN;k++)
2032 {
2033
2034 if(k==(OnsetN-1))
2035 {
2036 outArray3[count*3+2]=0.01*OutEnd[k];
2037 }
2038
2039 if(out[j+k*112]>0)
2040 {
2041 outArray3[count*3+2]=0.01*OutStart[k];
2042 break;
2043 }
2044
2045 if(A6A[k*112+j]<0.5)
2046 {
2047 outArray3[count*3+2]=0.01*OutStart[k];
2048 break;
2049
2050 }
2051
2052 }
2053
2054 }
2055
2056 count=count+1;
2057 }
2058
2059 }
2060
2061 }
2062 outArray3[count*3+0]=0;
2063 outArray3[count*3+1]=0;
2064 outArray3[count*3+2]=0;
2065
2066 free(tempArray);
2067 free(OutStart);
2068 free(OutEnd);
2069 free(A1);
2070 free(A2);
2071 free(A3);
2072 free(A4);
2073 free(A5);
2074 free(A6);
2075 free(A6A);
2076 free(D);
2077 free(D2);
2078 free(out2);
2079 free(PitchOut1);
2080 free(PitchOut2);
2081 free(PitchOut3);
2082 free(In);
2083 free(dbs);
2084 free(dbs1);
2085 free(ss);
2086 free(jj);
2087 }
2088
2089