xue@9
|
1 /*
|
xue@9
|
2 Sample program: analysis of harmonic sinusoid in low-noise monophonic context.
|
xue@9
|
3
|
xue@9
|
4 Syntax:
|
xue@9
|
5 (executable path) filename <setting=value> <setting=value> ... <setting=value>
|
xue@9
|
6
|
xue@9
|
7 filename: path input wave form audio file, 16-bit PCM
|
xue@9
|
8 setting: optional algorithmic parameters; search "stricmp" in the source code for a complete list
|
xue@9
|
9 value: non-default values given to the algorithmic parameters
|
xue@9
|
10
|
xue@9
|
11 Output:
|
xue@9
|
12 a *.evt file containing RIFF chunks, each starting with id "EVT\0" and hosting a harmonic sinusoid.
|
xue@9
|
13
|
xue@9
|
14 The "EVT\0" chunk body contains a header ("HDR\0") chunk followed by atom ("ATM\0") chunks.
|
xue@9
|
15
|
xue@9
|
16 The "HDR\0" chunk body contains 4 __int32 values: channel index, number of partials, number of
|
xue@9
|
17 measurement points (frames), and a tag.
|
xue@9
|
18
|
xue@9
|
19 The "ATM\0" chunk body contains an atom structure (see hs.h).
|
xue@9
|
20
|
xue@9
|
21 */
|
xue@9
|
22
|
xue@9
|
23 #include <QtCore/QCoreApplication>
|
xue@9
|
24 #include "arrayalloc.h"
|
xue@9
|
25 #include "hs.h"
|
xue@9
|
26 #include "matrix.h"
|
xue@9
|
27 #include "quickspec.h"
|
xue@9
|
28 #include "procedures.h"
|
xue@9
|
29 #include "vibrato.h"
|
xue@9
|
30 #include <string.h>
|
xue@9
|
31 #include <stdio.h>
|
xue@9
|
32 #include <conio.h>
|
xue@9
|
33
|
xue@9
|
34 //structure hosting wave header data
|
xue@9
|
35 struct wavehdr
|
xue@9
|
36 {
|
xue@9
|
37 __int16 fmttag;
|
xue@9
|
38 __int16 channels;
|
xue@9
|
39 __int32 samplespersec;
|
xue@9
|
40 __int32 bytespersec;
|
xue@9
|
41 __int16 blockalign;
|
xue@9
|
42 __int16 bitspersample;
|
xue@9
|
43 };
|
xue@9
|
44
|
xue@9
|
45 //tells if a wave header contains consistent data
|
xue@9
|
46 bool isvalidwavehdr(wavehdr* hdr)
|
xue@9
|
47 {
|
xue@9
|
48 if (hdr->fmttag!=1) return false;
|
xue@9
|
49 if (hdr->bitspersample!=8 && hdr->bitspersample!=16) return false;
|
xue@9
|
50 if (hdr->channels*hdr->bitspersample!=hdr->blockalign*8) return false;
|
xue@9
|
51 if (hdr->samplespersec*hdr->blockalign!=hdr->bytespersec) return false;
|
xue@9
|
52 return true;
|
xue@9
|
53 }
|
xue@9
|
54
|
xue@9
|
55 //structure hosting a waveform audio
|
xue@9
|
56 struct waveaudio
|
xue@9
|
57 {
|
xue@9
|
58 union
|
xue@9
|
59 {
|
xue@9
|
60 wavehdr hdr;
|
xue@9
|
61 struct
|
xue@9
|
62 {
|
xue@9
|
63 __int16 fmttag;
|
xue@9
|
64 __int16 channels;
|
xue@9
|
65 __int32 samplespersec;
|
xue@9
|
66 __int32 bytespersec;
|
xue@9
|
67 __int16 blockalign;
|
xue@9
|
68 __int16 bitspersample;
|
xue@9
|
69 };
|
xue@9
|
70 };
|
xue@9
|
71 int length;
|
xue@9
|
72 union
|
xue@9
|
73 {
|
xue@9
|
74 unsigned char* data;
|
xue@9
|
75 unsigned char* data8;
|
xue@9
|
76 __int16* data16;
|
xue@9
|
77 };
|
xue@9
|
78 waveaudio(){memset(this, 0, sizeof(waveaudio));}
|
xue@9
|
79 ~waveaudio(){delete[] data;}
|
xue@9
|
80 };
|
xue@9
|
81
|
xue@9
|
82 void freewaveaudio(waveaudio* wa)
|
xue@9
|
83 {
|
xue@9
|
84 delete[] wa->data;
|
xue@9
|
85 memset(wa, 0, sizeof(waveaudio));
|
xue@9
|
86 }
|
xue@9
|
87
|
xue@9
|
88 //error messages for reading waveform audio file
|
xue@9
|
89 enum err_wavefile
|
xue@9
|
90 {
|
xue@9
|
91 err_success,
|
xue@9
|
92 err_RIFF_hdr,
|
xue@9
|
93 err_wave_hdr,
|
xue@9
|
94 err_data_hdr,
|
xue@9
|
95 err_data_chunk,
|
xue@9
|
96 err_io
|
xue@9
|
97 };
|
xue@9
|
98
|
xue@9
|
99 //reads wave header from file stream
|
xue@9
|
100 int loadwavehdr(wavehdr* hdr, FILE* fp)
|
xue@9
|
101 {
|
xue@9
|
102 int rc, hdrlen;
|
xue@9
|
103 char s[5];
|
xue@9
|
104
|
xue@9
|
105 rc=fread(s, 1, 4, fp); s[4]=0;
|
xue@9
|
106 if (rc<4) return err_wave_hdr;
|
xue@9
|
107 if (strcmp(s, "WAVE")) return err_wave_hdr;
|
xue@9
|
108
|
xue@9
|
109 rc=fread(s, 1, 4, fp); s[4]=0;
|
xue@9
|
110 if (rc<4) return err_wave_hdr;
|
xue@9
|
111 if (strcmp(s, "fmt ")) return err_wave_hdr;
|
xue@9
|
112
|
xue@9
|
113 rc=fread(&hdrlen, 1, 4, fp);
|
xue@9
|
114 if (rc<4) return err_wave_hdr;
|
xue@9
|
115 if (hdrlen<16) return err_wave_hdr;
|
xue@9
|
116
|
xue@9
|
117 rc=fread(hdr, 1, sizeof(wavehdr), fp);
|
xue@9
|
118 if (rc<sizeof(wavehdr)) return err_wave_hdr;
|
xue@9
|
119 if (!isvalidwavehdr(hdr)) return err_wave_hdr;
|
xue@9
|
120
|
xue@9
|
121 if (hdrlen>16)
|
xue@9
|
122 {
|
xue@9
|
123 rc=fseek(fp, hdrlen-16, SEEK_CUR);
|
xue@9
|
124 if (rc!=0) return err_wave_hdr;
|
xue@9
|
125 }
|
xue@9
|
126
|
xue@9
|
127 return err_success;
|
xue@9
|
128 }
|
xue@9
|
129
|
xue@9
|
130 //reads waveform audio, including header, from a file stream
|
xue@9
|
131 int loadwaveaudio(waveaudio* wa, FILE* fp)
|
xue@9
|
132 {
|
xue@9
|
133 int rc, mainlen;
|
xue@9
|
134 char s[5];
|
xue@9
|
135 wavehdr hdr;
|
xue@9
|
136
|
xue@9
|
137 rc=fread(s, 1, 4, fp); s[4]=0;
|
xue@9
|
138 if (rc<4) return err_RIFF_hdr;
|
xue@9
|
139 if (strcmp(s, "RIFF")) return err_RIFF_hdr;
|
xue@9
|
140
|
xue@9
|
141 rc=fread(&mainlen, 1, 4, fp);
|
xue@9
|
142 if (rc<4) return err_RIFF_hdr;
|
xue@9
|
143
|
xue@9
|
144 rc=loadwavehdr(&hdr, fp);
|
xue@9
|
145 if (rc!=err_success) return rc;
|
xue@9
|
146
|
xue@9
|
147 rc=fread(s, 1, 4, fp); s[4]=0;
|
xue@9
|
148 if (rc<4) return err_data_hdr;
|
xue@9
|
149 if (strcmp(s, "data")) return err_data_hdr;
|
xue@9
|
150
|
xue@9
|
151 rc=fread(&mainlen, 1, 4, fp);
|
xue@9
|
152 if (rc<4) return err_data_hdr;
|
xue@9
|
153
|
xue@9
|
154 if (mainlen<=0) return err_data_hdr;
|
xue@9
|
155
|
xue@9
|
156 unsigned char* data=new unsigned char[mainlen];
|
xue@9
|
157 rc=fread(data, 1, mainlen, fp);
|
xue@9
|
158 if (rc<mainlen)
|
xue@9
|
159 {
|
xue@9
|
160 delete[] data;
|
xue@9
|
161 return err_data_chunk;
|
xue@9
|
162 }
|
xue@9
|
163
|
xue@9
|
164 wa->hdr=hdr;
|
xue@9
|
165 wa->length=rc/hdr.blockalign;
|
xue@9
|
166 delete[] wa->data;
|
xue@9
|
167 wa->data=data;
|
xue@9
|
168
|
xue@9
|
169 return err_success;
|
xue@9
|
170 }
|
xue@9
|
171
|
xue@9
|
172 //reads waveform audio, including header, from a file
|
xue@9
|
173 int loadwavefile(waveaudio* wa, char* filename)
|
xue@9
|
174 {
|
xue@9
|
175 FILE* fp;
|
xue@9
|
176 if ((fp=fopen(filename, "rb"))==NULL) return err_io;
|
xue@9
|
177 int result=loadwaveaudio(wa, fp);
|
xue@9
|
178 fclose(fp);
|
xue@9
|
179 return result;
|
xue@9
|
180 }
|
xue@9
|
181
|
xue@9
|
182 //returns new file path with the specific extension replacing the original one
|
xue@9
|
183 char* ChangeFileExt(char* FileName, char* Ext)
|
xue@9
|
184 {
|
xue@9
|
185 char* oldext=strrchr(FileName, '.');
|
xue@9
|
186 int namelen=strlen(FileName)-strlen(oldext);
|
xue@9
|
187 char* dest=new char[namelen+strlen(Ext)+1];
|
xue@9
|
188 memcpy(dest, FileName, namelen); dest[namelen]=0;
|
xue@9
|
189 strcat(dest, Ext);
|
xue@9
|
190 return dest;
|
xue@9
|
191 }
|
xue@9
|
192
|
xue@9
|
193 //ACPower for __int16 data input
|
xue@9
|
194 double ACPower(__int16* data, int Count)
|
xue@9
|
195 {
|
xue@9
|
196 if (Count<=0) return 0;
|
xue@9
|
197 double power=0, avg=0, tmp;
|
xue@9
|
198 for (int i=0; i<Count; i++)
|
xue@9
|
199 {
|
xue@9
|
200 tmp=*(data++);
|
xue@9
|
201 power+=tmp*tmp;
|
xue@9
|
202 avg+=tmp;
|
xue@9
|
203 }
|
xue@9
|
204 power=(power-avg*avg/Count)/Count;
|
xue@9
|
205 return power;
|
xue@9
|
206 }//ACPower
|
xue@9
|
207
|
xue@9
|
208 //double QIE(double* y, double& x){double a=0.5*(y[1]+y[-1])-y[0], b=0.5*(y[1]-y[-1]); x=-0.5*b/a; return y[0]-0.25*b*b/a;}
|
xue@9
|
209
|
xue@9
|
210 //correlation coefficient
|
xue@9
|
211 double InnerC(int N, __int16* x, __int16* y)
|
xue@9
|
212 {
|
xue@9
|
213 double inner=0, inn1=0, inn2=0;
|
xue@9
|
214 for (int n=0; n<N; n++) inner+=1.0*x[n]*y[n], inn1+=1.0*x[n]*x[n], inn2+=1.0*y[n]*y[n];
|
xue@9
|
215 double inn=sqrt(inn1*inn2);
|
xue@9
|
216 return (inn<=0)?0:inner/inn;
|
xue@9
|
217 }
|
xue@9
|
218
|
xue@9
|
219 //monophonic pitch estimation by autocorrelation
|
xue@9
|
220 double pitchautocor(__int16* data, int wid, double minf0bin, double maxf0bin, double& hsr)
|
xue@9
|
221 {
|
xue@9
|
222 int hwid=wid/2, minT=wid/maxf0bin, maxT=wid/minf0bin;
|
xue@9
|
223 double ene=InnerC(wid, data, data);
|
xue@9
|
224 if (ene<=0){hsr=0; return 0;}
|
xue@9
|
225 double* autocor=new double[hwid];
|
xue@9
|
226 for (int i=1; i<maxT+2; i++)
|
xue@9
|
227 {
|
xue@9
|
228 autocor[i]=InnerC(wid-i, data, &data[i]);
|
xue@9
|
229 }
|
xue@9
|
230
|
xue@9
|
231 int prd=0, m=minT;
|
xue@9
|
232 while(m<=maxT)
|
xue@9
|
233 {
|
xue@9
|
234 while (m<=maxT && (autocor[m]<0 || autocor[m]<autocor[m-1] || autocor[m]<autocor[m+1])) m++;
|
xue@9
|
235 if (m<=maxT)
|
xue@9
|
236 {
|
xue@9
|
237 int mm=ceil(m*0.6667), mp=floor(m*1.3333), cont=0;
|
xue@9
|
238 for (int im=mm; im<=mp; im++) if (m!=im && autocor[im]>=autocor[m]) {cont=1; break;}
|
xue@9
|
239 if (cont==0)
|
xue@9
|
240 {
|
xue@9
|
241 if (prd==0 || autocor[m]>autocor[prd]*1.05) prd=m;
|
xue@9
|
242 }
|
xue@9
|
243 m++;
|
xue@9
|
244 }
|
xue@9
|
245 }
|
xue@9
|
246
|
xue@9
|
247 double pitchbin=0;
|
xue@9
|
248 if (prd>=minT && prd<=maxT)
|
xue@9
|
249 {
|
xue@9
|
250 double mshift; hsr=QIE(&autocor[prd], mshift); pitchbin=wid/(prd+mshift);
|
xue@9
|
251 }
|
xue@9
|
252 else{hsr=0;}
|
xue@9
|
253 delete[] autocor;
|
xue@9
|
254 return pitchbin;
|
xue@9
|
255 }
|
xue@9
|
256
|
xue@9
|
257 //main function
|
xue@9
|
258 int main(int argc, char* argv[])
|
xue@9
|
259 {
|
xue@9
|
260 //read audio file
|
xue@9
|
261 if (argc<2){printf("Please specify input wave file."); getch(); return 0;}
|
xue@9
|
262 printf("Loading wave file %s... ", argv[1]);
|
xue@9
|
263 waveaudio* wa=new waveaudio;
|
xue@9
|
264 int waverr=loadwavefile(wa, argv[1]); printf("[%d]\n", waverr);
|
xue@9
|
265 if (waverr!=err_success){printf("Aborted: error loading wave file."); getch(); return 0;}
|
xue@9
|
266 if (wa->bitspersample!=16){printf("Aborted: this program accepts 16-bit pcm only."); getch(); return 0;}
|
xue@9
|
267
|
xue@9
|
268 int length=wa->length, sps=wa->samplespersec;
|
xue@9
|
269 __int16* data16=wa->data16;
|
xue@9
|
270 if (wa->channels>1)
|
xue@9
|
271 {
|
xue@9
|
272 printf("Extracting channel 0... ");
|
xue@9
|
273 for (int i=1; i<length; i++) data16[i]=data16[i*wa->channels];
|
xue@9
|
274 printf("[0]\n");
|
xue@9
|
275 }
|
xue@9
|
276 wa->data=0;
|
xue@9
|
277 delete wa;
|
xue@9
|
278
|
xue@9
|
279 //default settings
|
xue@9
|
280 int maxhscount=100;
|
xue@9
|
281 float wid_s=0.02, offstwidratio=0.5;
|
xue@9
|
282 float dur_s=0.5;
|
xue@9
|
283 float intv_s=0.05; //duration, in seconds, to scan for peaks
|
xue@9
|
284 float minf0=55, maxf0=3520;
|
xue@9
|
285 WindowType wintype=wtHann;
|
xue@9
|
286
|
xue@9
|
287 NMSettings settings; memset(&settings, 0, sizeof(NMSettings));
|
xue@9
|
288 settings.hB=3; //spectral truncation half width
|
xue@9
|
289 settings.maxp=50; //maximal number of partials
|
xue@9
|
290 settings.maxB=0.001; //stiffness coefficient upper bound
|
xue@9
|
291 settings.epf=1e-4; //frequency estimation error tolerance for LSE estimation
|
xue@9
|
292 settings.epf0=1; //input frequency error bound for harmonic grouping
|
xue@9
|
293 settings.delm=1.1; //frequency error bound for harmonic grouping
|
xue@9
|
294 settings.delp=1.1; //pitch jump upper bound
|
xue@9
|
295
|
xue@9
|
296 double mina=0.5;
|
xue@9
|
297
|
xue@9
|
298 //user settings through commandline arguments
|
xue@9
|
299 for (int i=2; i<argc; i++)
|
xue@9
|
300 {
|
xue@9
|
301 char* s1=strchr(argv[i], '=');
|
xue@9
|
302 if (s1)
|
xue@9
|
303 {
|
xue@9
|
304 s1[0]=0; s1++;
|
xue@9
|
305 if (!stricmp(argv[i], "hB")){settings.hB=atof(s1); continue;}
|
xue@9
|
306 if (!stricmp(argv[i], "maxp")){settings.maxp=atoi(s1); continue;}
|
xue@9
|
307 if (!stricmp(argv[i], "maxB")){settings.maxB=atof(s1); continue;}
|
xue@9
|
308 if (!stricmp(argv[i], "epf")){settings.epf=atof(s1); continue;}
|
xue@9
|
309 if (!stricmp(argv[i], "epf0")){settings.epf0=atof(s1); continue;}
|
xue@9
|
310 if (!stricmp(argv[i], "delm")){settings.delm=atof(s1); continue;}
|
xue@9
|
311 if (!stricmp(argv[i], "delp")){settings.delp=atof(s1); continue;}
|
xue@9
|
312 if (!stricmp(argv[i], "minf0")){minf0=atof(s1); continue;}
|
xue@9
|
313 if (!stricmp(argv[i], "maxf0")){maxf0=atof(s1); continue;}
|
xue@9
|
314 if (!stricmp(argv[i], "wid_s")){wid_s=atof(s1); continue;}
|
xue@9
|
315 if (!stricmp(argv[i], "offstwidratio")){offstwidratio=atof(s1); continue;}
|
xue@9
|
316 if (!stricmp(argv[i], "dur_s")){dur_s=atof(s1); continue;}
|
xue@9
|
317 if (!stricmp(argv[i], "intv_s")){intv_s=atof(s1); continue;}
|
xue@9
|
318 if (!stricmp(argv[i], "maxhscount")){maxhscount=atoi(s1); continue;}
|
xue@9
|
319 if (!stricmp(argv[i], "wintype"))
|
xue@9
|
320 {
|
xue@9
|
321 if (!stricmp(s1, "hann")){wintype=wtHann; continue;}
|
xue@9
|
322 if (!stricmp(s1, "hamming")){wintype=wtHamming; continue;}
|
xue@9
|
323 if (!stricmp(s1, "blackman")){wintype=wtBlackman; continue;}
|
xue@9
|
324 if (!stricmp(s1, "rectangle")){wintype=wtRectangle; continue;}
|
xue@9
|
325 if (!stricmp(s1, "hannsqr")){wintype=wtHannSqr; continue;}
|
xue@9
|
326 }
|
xue@9
|
327 }
|
xue@9
|
328 }
|
xue@9
|
329
|
xue@9
|
330 //
|
xue@9
|
331 MList* List=new MList; List->Add(data16, 1);
|
xue@9
|
332
|
xue@9
|
333 //analysis window size and hop size
|
xue@9
|
334 int wid=1<<((int)floor(log2(wid_s*sps)+1)), offst=wid*offstwidratio;
|
xue@9
|
335
|
xue@9
|
336 //set up the spectrogram
|
xue@9
|
337 TQuickSpectrogram* Sp=new TQuickSpectrogram(0, 0, true, false, 0);
|
xue@9
|
338 Sp->Data=data16; Sp->DataLength=length; Sp->BytesPerSample=2;
|
xue@9
|
339 Sp->Wid=wid; Sp->Offst=offst; Sp->WinType=wintype;
|
xue@9
|
340 Sp->Spec(-1); //this sets Sp->Capacity to the number of accessible frames and does nothing else
|
xue@9
|
341
|
xue@9
|
342 //get number of frames
|
xue@9
|
343 int Fr=Sp->Capacity;
|
xue@9
|
344
|
xue@9
|
345 //the program searches every intv_fr frames over a duration of dur_fr frames for a spectral peak to start tracking.
|
xue@9
|
346 int dur_fr=sps*dur_s/offst, intv_fr=sps*intv_s/offst;
|
xue@9
|
347 if (dur_fr<10) dur_fr=10;
|
xue@9
|
348 if (intv_fr<1) intv_fr=1;
|
xue@9
|
349
|
xue@9
|
350 //minimal and maximal fundamental frequency, in bins
|
xue@9
|
351 settings.minf0=minf0/sps*wid;
|
xue@9
|
352 settings.maxf0=maxf0/sps*wid;
|
xue@9
|
353 if (settings.minf0<settings.hB) settings.minf0=settings.hB;
|
xue@9
|
354
|
xue@9
|
355 windowspec(wintype, wid, &settings.M, settings.c, &settings.iH2);
|
xue@9
|
356 double *fps=new double[wid], *vps=&fps[wid/2]; List->Add(fps, 1);
|
xue@9
|
357 cdouble *x=new cdouble[wid/2+1]; List->Add(x, 1);
|
xue@9
|
358
|
xue@9
|
359 //output file
|
xue@9
|
360 char* filename=ChangeFileExt(argv[1], ".evt"); List->Add(filename, 1);
|
xue@9
|
361 TFileStream* File=new TFileStream(filename, fmWrite);
|
xue@9
|
362
|
xue@9
|
363 double* frames=new double[Fr]; memset(frames, 0, sizeof(double)*Fr); List->Add(frames, 1);
|
xue@9
|
364 double* framesa=new double[Fr]; memset(framesa, 0, sizeof(double)*Fr); List->Add(framesa, 1);
|
xue@9
|
365 double* rsr=new double[Fr]; List->Add(rsr, 1); memset(rsr, 0, sizeof(double)*Fr);
|
xue@9
|
366 double* f0s=new double[Fr]; List->Add(f0s, 1); memset(f0s, 0, sizeof(double)*Fr);
|
xue@9
|
367 for (int fr=0; fr<Fr-1; fr++)
|
xue@9
|
368 {
|
xue@9
|
369 __int16* ldata16=&data16[offst*fr];
|
xue@9
|
370 frames[fr]=ACPower(ldata16, wid);
|
xue@9
|
371 framesa[fr]=1;
|
xue@9
|
372 // f0s[fr]=pitchautocor(ldata16, wid, settings.minf0, settings.maxf0, rsr[fr]); rsr[fr]=1-rsr[fr];
|
xue@9
|
373 }
|
xue@9
|
374 frames[Fr-1]=ACPower(&data16[(Fr-1)*offst], length-(Fr-1)*offst);
|
xue@9
|
375 framesa[Fr-1]=1;
|
xue@9
|
376
|
xue@9
|
377 int start=0, writecount=0;
|
xue@9
|
378
|
xue@9
|
379 while (writecount<maxhscount)
|
xue@9
|
380 {
|
xue@9
|
381 int fr_m=start+intv_fr;
|
xue@9
|
382
|
xue@9
|
383 //search for the highest spectral peak to start tracking
|
xue@9
|
384 double fp_m=0, vp_m=0;
|
xue@9
|
385 for (int fr=start+intv_fr; fr<start+dur_fr && fr<Fr; fr+=intv_fr)
|
xue@9
|
386 {
|
xue@9
|
387 cmplx<QSPEC_FORMAT>* speci=Sp->Spec(fr);
|
xue@9
|
388 for (int k=0; k<=wid/2; k++) x[k]=speci[k];
|
xue@9
|
389 int pc=QuickPeaks(fps, vps, wid, x, settings.M, settings.c, settings.iH2, mina, 0, wid/4);
|
xue@9
|
390
|
xue@9
|
391 for(int p=0; p<pc; p++)
|
xue@9
|
392 if (vps[p]>vp_m)
|
xue@9
|
393 {
|
xue@9
|
394 if (f0s[fr]==0 && rsr[fr]==0) f0s[fr]=pitchautocor(&data16[offst*fr], wid, settings.minf0, settings.maxf0, rsr[fr]); rsr[fr]=1-rsr[fr];
|
xue@9
|
395 if (f0s[fr]>0)
|
xue@9
|
396 {
|
xue@9
|
397 double dpind=fps[p]/f0s[fr], pind=floor(dpind+0.5);
|
xue@9
|
398 if (fabs(dpind-pind)<0.1) vp_m=vps[p], fr_m=fr, fp_m=fps[p];
|
xue@9
|
399 }
|
xue@9
|
400 }
|
xue@9
|
401 }
|
xue@9
|
402
|
xue@9
|
403 if (fp_m<=0){start=start+dur_fr; if (start>Fr) break; continue;}
|
xue@9
|
404
|
xue@9
|
405 f0s[fr_m]=pitchautocor(&data16[offst*fr_m], wid, settings.minf0, settings.maxf0, rsr[fr_m]); rsr[fr_m]=1-rsr[fr_m];
|
xue@9
|
406
|
xue@9
|
407 int _t=fr_m*offst+wid/2;
|
xue@9
|
408 double _f=fp_m/wid;
|
xue@9
|
409 int hsM, hsFr, frst=start, fren=Fr;
|
xue@9
|
410 atom** hsPartials=0;
|
xue@9
|
411
|
xue@9
|
412 settings.pin0=floor(fp_m/f0s[fr_m]+0.5);
|
xue@9
|
413 int tag=FindNote(_t, _f, hsM, hsFr, hsPartials, frst, fren, wid, offst, Sp, settings);
|
xue@9
|
414
|
xue@9
|
415 int fr1=(hsPartials[0][0].t-wid/2)/offst;
|
xue@9
|
416 double consi=1;
|
xue@9
|
417 for (int hsfr=0; hsfr<hsFr; hsfr++)
|
xue@9
|
418 {
|
xue@9
|
419 double ene=0;
|
xue@9
|
420 for (int m=0; m<hsM; m++)
|
xue@9
|
421 {
|
xue@9
|
422 double a=(hsPartials[m][hsfr].f>0)?hsPartials[m][hsfr].a:0;
|
xue@9
|
423
|
xue@9
|
424 if (hsfr==0) ene+=a*a;
|
xue@9
|
425 else
|
xue@9
|
426 {
|
xue@9
|
427 double b=(hsPartials[m][hsfr-1].f>0)?hsPartials[m][hsfr-1].a:0;
|
xue@9
|
428 ene+=(a*a+b*b+a*b)/3;
|
xue@9
|
429 }
|
xue@9
|
430
|
xue@9
|
431 if (hsfr==hsFr-1) ene+=a*a;
|
xue@9
|
432 else
|
xue@9
|
433 {
|
xue@9
|
434 double b=(hsPartials[m][hsfr+1].f>0)?hsPartials[m][hsfr+1].a:0;
|
xue@9
|
435 ene+=(a*a+b*b+a*b)/3;
|
xue@9
|
436 }
|
xue@9
|
437 }
|
xue@9
|
438 framesa[fr1+hsfr]=ene;//*2;
|
xue@9
|
439 if (framesa[fr1+hsfr]<frames[fr1+hsfr]) consi*=frames[fr1+hsfr]/framesa[fr1+hsfr];
|
xue@9
|
440 else consi*=framesa[fr1+hsfr]/frames[fr1+hsfr];
|
xue@9
|
441 if (framesa[fr1+hsfr]>frames[fr1+hsfr]) framesa[fr1+hsfr]=0;
|
xue@9
|
442 else framesa[fr1+hsfr]=1-framesa[fr1+hsfr]/frames[fr1+hsfr];
|
xue@9
|
443 }
|
xue@9
|
444 consi=pow(consi, 1.0/hsFr);
|
xue@9
|
445
|
xue@9
|
446 THS* HS=new THS;
|
xue@9
|
447 HS->M=hsM; HS->Fr=hsFr; HS->Partials=hsPartials;
|
xue@9
|
448 printf("%d (%.2fs), %d (%.2fs), inconsistency %f\n", start, start*offst*1.0/sps, hsFr, hsFr*offst*1.0/sps, consi-1);
|
xue@9
|
449 if (consi<1.5)
|
xue@9
|
450 {
|
xue@9
|
451 HS->WriteToStream(File);
|
xue@9
|
452 printf("Write note %d\n", writecount++);
|
xue@9
|
453 }
|
xue@9
|
454 delete HS;
|
xue@9
|
455
|
xue@9
|
456 start=fr1+hsFr;
|
xue@9
|
457
|
xue@9
|
458 }
|
xue@9
|
459 delete File; delete Sp;
|
xue@9
|
460 delete List;
|
xue@9
|
461 printf("Completed\n"); getch(); return 0;
|
xue@9
|
462 }
|