pvoc2.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * Copyright (c) 1983-2020 Richard Dobson and Composers Desktop Project Ltd
  3. * http://www.rwdobson.com
  4. * http://www.composersdesktop.com
  5. * This file is part of the CDP System.
  6. * The CDP System is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * The CDP System is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU Lesser General Public License for more details.
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the CDP System; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /*pvoc.cpp*/
  21. /* simple class wrapper for (modified) CARL pvoc; supports realtime streaming. (c) Richard Dobson 2000,2014 */
  22. /* includes correction to sinc function code from Dan Timis, June 2000 */
  23. /* RWD 12:2000: define PVOC_NORM_PHASE to use slower normalized phase calcs */
  24. /* NB: optional FFTW code is for v 2.1.5 */
  25. #include <math.h>
  26. #include <stdlib.h>
  27. #include <memory.h>
  28. extern "C" {
  29. #ifdef USE_FFTW
  30. # include <rfftw.h>
  31. # else
  32. void fft_(float *, float *,int,int,int,int);
  33. void fftmx(float *,float *,int,int,int,int,int,int *,float *,float *,float *,float *,int *,int[]);
  34. void reals_(float *,float *,int,int);
  35. #endif
  36. }
  37. #include "pvpp.h"
  38. #ifndef PI
  39. #define PI (3.141592653589793)
  40. #endif
  41. #define TWOPI (2.0 * PI)
  42. #if defined _WIN32 && defined _MSC_VER
  43. #pragma message ("using assembler round()")
  44. __inline static int round(double fval)
  45. {
  46. int result;
  47. _asm{
  48. fld fval
  49. fistp result
  50. mov eax,result
  51. }
  52. return result;
  53. }
  54. #else
  55. #define round(x) lround((x))
  56. #endif
  57. phasevocoder::phasevocoder()
  58. {
  59. input = NULL;
  60. output = NULL;
  61. anal = NULL;
  62. syn = NULL; /* pointer to start of synthesis buffer */
  63. banal = NULL; /* pointer to anal[1] (for FFT calls) */
  64. bsyn = NULL; /* pointer to syn[1] (for FFT calls) */
  65. nextIn = NULL; /* pointer to next empty word in input */
  66. nextOut = NULL; /* pointer to next empty word in output */
  67. analWindow = NULL; /* pointer to center of analysis window */
  68. synWindow = NULL; /* pointer to center of synthesis window */
  69. maxAmp = NULL; /* pointer to start of max amp buffer */
  70. avgAmp = NULL; /* pointer to start of avg amp buffer */
  71. avgFrq = NULL; /* pointer to start of avg frq buffer */
  72. env = NULL; /* pointer to start of spectral envelope */
  73. i0 = NULL; /* pointer to amplitude channels */
  74. i1 = NULL; /* pointer to frequency channels */
  75. oi = NULL; /* pointer to old phase channels */
  76. oldInPhase = NULL; /* pointer to start of input phase buffer */
  77. oldOutPhase = NULL; /* pointer to start of output phase buffer */
  78. m = n = 0;
  79. N = 0; /* number of phase vocoder channels (bands) */
  80. M = 0; /* length of analWindow impulse response */
  81. L = 0; /* length of synWindow impulse response */
  82. D = 0; /* decimation factor (default will be M/8) */
  83. I = 0; /* interpolation factor (default will be I=D)*/
  84. W = -1; /* filter overlap factor (determines M, L) */
  85. //F = 0; /* fundamental frequency (determines N) */
  86. //F2 = 0; /* F/2 */
  87. /*RWD */
  88. Fexact = 0.0f;
  89. analWinLen = 0, /* half-length of analysis window */
  90. synWinLen = 0; /* half-length of synthesis window */
  91. sampsize = 0; /* sample size for output file */
  92. outCount = 0; /* number of samples written to output */
  93. ibuflen= 0; /* length of input buffer */
  94. obuflen= 0; /* length of output buffer */
  95. nI = 0; /* current input (analysis) sample */
  96. nO= 0; /* current output (synthesis) sample */
  97. nMaxOut= 0; /* last output (synthesis) sample */
  98. nMin = 0; /* first input (analysis) sample */
  99. nMax = 100000000; /* last input sample (unless EOF) */
  100. /***************************** 6:2:91 OLD CODE **************
  101. long origsize;
  102. *******************************NEW CODE **********************/
  103. origsize = 0; /* sample type of file analysed */
  104. ch = 0;
  105. ifd = ofd = -1;
  106. beta = 6.8f; /* parameter for Kaiser window */
  107. real = 0.0f; /* real part of analysis data */
  108. imag= 0.0f; /* imaginary part of analysis data */
  109. mag= 0.0f; /* magnitude of analysis data */
  110. phase= 0.0f; /* phase of analysis data */
  111. angleDif= 0.0f; /* angle difference */
  112. RoverTwoPi= 0.0f; /* R/D divided by 2*Pi */
  113. TwoPioverR= 0.0f; /* 2*Pi divided by R/I */
  114. sum= 0.0f; /* scale factor for renormalizing windows */
  115. ftot = 0.0f, /* scale factor for calculating statistics */
  116. rIn= 0.0f; /* decimated sampling rate */
  117. rOut= 0.0f; /* pre-interpolated sampling rate */
  118. invR= 0.0f; /* 1. / srate */
  119. time= 0.0f; /* nI / srate */
  120. tvx0 = 0.0f;
  121. tvx1 = 0.0f;
  122. tvdx = 0.0f;
  123. tvy0 = tvy1 = 0.0f;
  124. tvdy = 0.0f;
  125. frac = 0.0f;
  126. warp = 0.0f; /* spectral envelope warp factor */
  127. R = 0.0f; /* input sampling rate */
  128. P = 1.0f; /* pitch scale factor */
  129. Pinv= 0.0f; /* 1. / P */
  130. T = 1.0f; /* time scale factor ( >1 to expand)*/
  131. //Mlen,
  132. Mf = 0; /* flag for even M */
  133. Lf = 0; /* flag for even L */
  134. //Dfac,
  135. flag = 0; /* end-of-input flag */
  136. C = 0; /* flag for resynthesizing even or odd chans */
  137. Ci = 0; /* flag for resynthesizing chans i to j only */
  138. Cj = 0; /* flag for resynthesizing chans i to j only */
  139. CC = 0; /* flag for selected channel resynthesis */
  140. X = 0; /* flag for magnitude output */
  141. E = 0; /* flag for spectral envelope output */
  142. tvflg = 0; /* flag for time-varying time-scaling */
  143. tvnxt = 0;
  144. tvlen = 0;
  145. timecheckf= 0.0f;
  146. K = H = 0; /* default window is Hamming */
  147. Nchans = 0;
  148. NO2 = 0;
  149. vH = 0; /* RWD set to 1 to set von Hann window */
  150. bin_index = 0;
  151. m_mode = PVPP_NOT_SET;
  152. synWindow_base = NULL;
  153. analWindow_base = NULL;
  154. };
  155. /* use when decfac needs specifying directly: cuts out other options */
  156. bool phasevocoder::init(long outsrate,long fftlen,long winlen,long decfac,float scalefac,
  157. pvoc_scaletype stype,pvoc_windowtype wtype,pvocmode mode)
  158. {
  159. N = fftlen;
  160. M = N*2; /* RWD make double-window the default */
  161. D = decfac;
  162. if(scalefac <=0.0)
  163. return false;
  164. switch(stype){
  165. case PVOC_S_TIME:
  166. T = scalefac;
  167. P = 1.0f;
  168. break;
  169. case PVOC_S_PITCH:
  170. T = P = scalefac;
  171. break;
  172. default:
  173. T = P = 1.0f;
  174. break;
  175. }
  176. switch(wtype){
  177. case PVOC_HANN:
  178. H = 1;
  179. break;
  180. case PVOC_KAISER:
  181. K = 1;
  182. break;
  183. default:
  184. /* for now, anything else just sets Hamming window! */
  185. break;
  186. }
  187. if(N <=0)
  188. return false;
  189. if(D < 0)
  190. return false;
  191. if(M < 0)
  192. return false;
  193. /*for now */
  194. if(!(mode == PVPP_OFFLINE || mode == PVPP_STREAMING) )
  195. return false;
  196. m_mode = mode;
  197. isr = outsrate;
  198. R = srate = (float) outsrate;
  199. N = N + N%2; /* Make N even */
  200. N2 = N / 2;
  201. // if (N2 > 16384){
  202. // return false;
  203. // }
  204. // F = (int)((float) R / N);
  205. Fexact = (float)R / (float)N; /* RWD */
  206. // F2 = F / 2;
  207. if(winlen > 0)
  208. M = winlen;
  209. Mf = 1 - M%2;
  210. L = (L != 0 ? L : M);
  211. Lf = 1 - L%2;
  212. ibuflen = 4 * M;
  213. obuflen = 4 * L;
  214. if (W == -1)
  215. W = (int)(3.322 * log10((double)(4. * N) / M));/* cosmetic */
  216. if (Cj == 0)
  217. Cj = N2;
  218. if (Cj > N2)
  219. Cj = N2;
  220. if (Ci < 0)
  221. Ci = 0;
  222. D = (int)((D != 0 ? D : M/(8.0*(T > 1.0 ? T : 1.0))));
  223. if (D == 0){
  224. //fprintf(stderr,"pvoc: warning - T greater than M/8 \n");
  225. D = 1;
  226. }
  227. I = (int)(I != 0 ? I : (float) T*D );
  228. T = ((float) I / D);
  229. if (P != 1.)
  230. P = T;
  231. NO = (int)((float) N / P); /* synthesis transform will be NO points */
  232. NO = NO + NO%2; /* make NO even */
  233. NO2 = NO / 2;
  234. P = ((float) N / NO); /* ideally, N / NO = I / D = pitch change */
  235. Pinv = (float)(1.0/ P);
  236. if (warp == -1.)
  237. warp = P;
  238. if ((E == 1) && (warp == 0.))
  239. warp = 1.0f;
  240. //if ((P != 1.) && (P != T))
  241. // fprintf(stderr,"pvoc: warning P=%f not equal to T=%f\n",P,T);
  242. IO = (int)((float) I / P);
  243. nMax -= nMin;
  244. /*RWD need this to get sum setup for synth window! */
  245. /* NB vonHann window also available now */
  246. /* set up analysis window: The window is assumed to be symmetric
  247. with M total points. After the initial memory allocation,
  248. analWindow always points to the midpoint of the window
  249. (or one half sample to the right, if M is even); analWinLen
  250. is half the true window length (rounded down). Any low pass
  251. window will work; a Hamming window is generally fine,
  252. but a Kaiser is also available. If the window duration is
  253. longer than the transform (M > N), then the window is
  254. multiplied by a sin(x)/x function to meet the condition:
  255. analWindow[Ni] = 0 for i != 0. In either case, the
  256. window is renormalized so that the phase vocoder amplitude
  257. estimates are properly scaled. The maximum allowable
  258. window duration is ibuflen/2. */
  259. analWindow_base = new float[M+Mf];
  260. analWindow = analWindow_base + (analWinLen = M/2);
  261. #ifdef USE_FFTW
  262. in_fftw_size = N;
  263. out_fftw_size = NO;
  264. forward_plan = rfftwnd_create_plan_specific(1,&in_fftw_size,
  265. FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE | FFTW_IN_PLACE,
  266. (fftw_real*) analWindow_base,1,NULL,1);
  267. inverse_plan = rfftwnd_create_plan(1,&out_fftw_size, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE | FFTW_IN_PLACE);
  268. #endif
  269. if (K)
  270. kaiser(analWindow_base,M+Mf,beta); /* ??? or just M?*/
  271. else if (vH)
  272. vonhann(analWindow,analWinLen,Mf);
  273. else
  274. hamming(analWindow,analWinLen,Mf);
  275. for (i = 1; i <= analWinLen; i++)
  276. *(analWindow - i) = *(analWindow + i - Mf);
  277. if (M > N) {
  278. if (Mf)
  279. *analWindow *=(float)( (double)N * sin((double)PI*.5/N) /(double)( PI*.5));
  280. for (i = 1; i <= analWinLen; i++)
  281. *(analWindow + i) *=(float)
  282. ((double)N * sin((double) (PI*(i+.5*Mf)/N)) / (PI*(i+.5*Mf))); /* D.T. 2000*/
  283. for (i = 1; i <= analWinLen; i++)
  284. *(analWindow - i) = *(analWindow + i - Mf);
  285. }
  286. sum = 0.0f;
  287. for (i = -analWinLen; i <= analWinLen; i++)
  288. sum += *(analWindow + i);
  289. sum = (float)(2.0 / sum); /*factor of 2 comes in later in trig identity*/
  290. for (i = -analWinLen; i <= analWinLen; i++)
  291. *(analWindow + i) *= sum;
  292. /* set up synthesis window: For the minimal mean-square-error
  293. formulation (valid for N >= M), the synthesis window
  294. is identical to the analysis window (except for a
  295. scale factor), and both are even in length. If N < M,
  296. then an interpolating synthesis window is used. */
  297. synWindow_base = new float[L+Lf];
  298. synWindow = synWindow_base + (synWinLen = L/2);
  299. #ifdef USE_FFTW
  300. Ninv = (float) (1.0 / N);
  301. #endif
  302. if (M <= N){
  303. if(K)
  304. kaiser(synWindow_base,L+Lf,beta);
  305. else if(vH)
  306. vonhann(synWindow,synWinLen,Lf);
  307. else
  308. hamming(synWindow,synWinLen,Lf);
  309. for (i = 1; i <= synWinLen; i++)
  310. *(synWindow - i) = *(synWindow + i - Lf);
  311. for (i = -synWinLen; i <= synWinLen; i++)
  312. *(synWindow + i) *= sum;
  313. sum = 0.0f;
  314. for (i = -synWinLen; i <= synWinLen; i+=I)
  315. sum += *(synWindow + i) * *(synWindow + i);
  316. sum = (float)(1.0/ sum);
  317. #ifdef USE_FFTW
  318. sum *= Ninv;
  319. #endif
  320. for (i = -synWinLen; i <= synWinLen; i++)
  321. *(synWindow + i) *= sum;
  322. }
  323. else {
  324. if(K)
  325. kaiser(synWindow_base,L+Lf,beta);
  326. else if(vH)
  327. vonhann(synWindow,synWinLen,Lf);
  328. else
  329. hamming(synWindow,synWinLen,Lf);
  330. for (i = 1; i <= synWinLen; i++)
  331. *(synWindow - i) = *(synWindow + i - Lf);
  332. if (Lf)
  333. *synWindow *= (float)((double)IO * sin((double) (PI*.5/IO)) / (double)(PI*.5));
  334. for (i = 1; i <= synWinLen; i++)
  335. *(synWindow + i) *=(float)
  336. ((double)IO * sin((double) (PI*(i+.5*Lf)/IO)) /(double) (PI*(i+.5*Lf)));
  337. for (i = 1; i <= synWinLen; i++)
  338. *(synWindow - i) = *(synWindow + i - Lf);
  339. sum = (float)(1.0/sum);
  340. #ifdef USE_FFTW
  341. sum *= Ninv;
  342. #endif
  343. for (i = -synWinLen; i <= synWinLen; i++)
  344. *(synWindow + i) *= sum;
  345. }
  346. try{
  347. /* set up input buffer: nextIn always points to the next empty
  348. word in the input buffer (i.e., the sample following
  349. sample number (n + analWinLen)). If the buffer is full,
  350. then nextIn jumps back to the beginning, and the old
  351. values are written over. */
  352. input = new float[ibuflen];
  353. nextIn = input;
  354. /* set up output buffer: nextOut always points to the next word
  355. to be shifted out. The shift is simulated by writing the
  356. value to the standard output and then setting that word
  357. of the buffer to zero. When nextOut reaches the end of
  358. the buffer, it jumps back to the beginning. */
  359. output = new float [obuflen];
  360. nextOut = output;
  361. /* set up analysis buffer for (N/2 + 1) channels: The input is real,
  362. so the other channels are redundant. oldInPhase is used
  363. in the conversion to remember the previous phase when
  364. calculating phase difference between successive samples. */
  365. anal = new float[N+2];
  366. banal = anal + 1;
  367. oldInPhase = new float[N2+1];
  368. maxAmp = new float[N2+1];
  369. avgAmp = new float[N2+1];
  370. avgFrq = new float[N2+1];
  371. env = new float[N2+1];
  372. /* set up synthesis buffer for (N/2 + 1) channels: (This is included
  373. only for clarity.) oldOutPhase is used in the re-
  374. conversion to accumulate angle differences (actually angle
  375. difference per second). */
  376. syn = new float[NO+2];
  377. bsyn = syn + 1;
  378. oldOutPhase = new float[NO2+1];
  379. }
  380. catch(...){
  381. if(synWindow_base){
  382. delete [] synWindow_base;
  383. synWindow_base = 0;
  384. }
  385. if(analWindow_base){
  386. delete [] analWindow_base;
  387. analWindow_base = 0;
  388. }
  389. if(input) {
  390. delete [] input;
  391. input = 0;
  392. }
  393. if(output) {
  394. delete [] output;
  395. output = 0;
  396. }
  397. if(anal) {
  398. delete [] anal;
  399. anal = 0;
  400. }
  401. if(oldInPhase) {
  402. delete [] oldInPhase;
  403. oldInPhase = 0;
  404. }
  405. if(maxAmp){
  406. delete [] maxAmp;
  407. maxAmp = 0;
  408. }
  409. if(avgAmp) {
  410. delete [] avgAmp;
  411. avgAmp = 0;
  412. }
  413. if(avgFrq) {
  414. delete [] avgFrq;
  415. avgFrq = 0;
  416. }
  417. if(env){
  418. delete [] env;
  419. env= 0;
  420. }
  421. if(syn){
  422. delete [] syn;
  423. syn = 0;
  424. }
  425. if(oldOutPhase){
  426. delete [] oldOutPhase;
  427. oldOutPhase = 0;
  428. }
  429. return false;
  430. }
  431. outCount = 0;
  432. rIn = ((float) R / D);
  433. rOut = ((float) R / I);
  434. invR =((float) 1. / R);
  435. RoverTwoPi = (float)(rIn / TWOPI);
  436. TwoPioverR = (float)(TWOPI / rOut);
  437. nI = -(analWinLen / D) * D; /* input time (in samples) */
  438. nO = (long)((float) T/P * nI); /* output time (in samples) */
  439. Dd = analWinLen + nI + 1; /* number of new inputs to read */
  440. Ii = 0; /* number of new outputs to write */
  441. IOi = 0;
  442. flag = 1;
  443. for(i=0;i < ibuflen;i++) {
  444. input[i] = 0.0f;
  445. output[i] = 0.0f;
  446. }
  447. for(i=0;i < NO+2;i++)
  448. syn[i] = 0.0f;
  449. for(i=0;i < N+2;i++)
  450. anal[i] = 0.0f;
  451. for(i=0;i < NO2+1;i++)
  452. oldOutPhase[i] = 0.0f;
  453. for(i=0;i < N2+1;i++)
  454. oldInPhase[i] = maxAmp[i] = avgAmp[i] = avgFrq[i] = env[i] = 0.0f;
  455. return true;
  456. }
  457. /* closer to PVOC command-line format; we will need a full array of settings ere long */
  458. /* e.g to install a custom window...*/
  459. bool phasevocoder::init(long outsrate,long fftlen,pvoc_overlapfac ofac,float scalefac,
  460. pvoc_scaletype stype,pvoc_windowtype wtype,pvocmode mode)
  461. {
  462. N = fftlen;
  463. D = 0;
  464. //D = N/4; /* one problem - when M is larger, overlap is larger too */
  465. switch(ofac){
  466. case PVOC_O_W0:
  467. W = 0;
  468. break;
  469. case PVOC_O_W1:
  470. W = 1;
  471. break;
  472. case PVOC_O_W2:
  473. W = 2;
  474. break;
  475. case PVOC_O_W3:
  476. W = 3;
  477. break;
  478. default:
  479. W = 1; /* double-window option */
  480. break;
  481. }
  482. switch(wtype){
  483. case PVOC_HANN:
  484. H = 1;
  485. break;
  486. case PVOC_KAISER:
  487. K = 1;
  488. break;
  489. default:
  490. /* for now, anything else just sets Hamming window! */
  491. break;
  492. }
  493. if(scalefac <=0.0)
  494. return false;
  495. switch(stype){
  496. case PVOC_S_TIME:
  497. T = scalefac;
  498. P = 1.0f;
  499. break;
  500. case PVOC_S_PITCH:
  501. T = P = scalefac;
  502. break;
  503. default:
  504. T = P = 1.0f;
  505. break;
  506. }
  507. if(N <=0)
  508. return false;
  509. if(D < 0)
  510. return false;
  511. /*for now */
  512. if(!(mode == PVPP_OFFLINE || mode == PVPP_STREAMING) )
  513. return false;
  514. m_mode = mode;
  515. isr = outsrate;
  516. R = srate = (float) outsrate;
  517. N = N + N%2; /* Make N even */
  518. N2 = N / 2;
  519. // if (N2 > 16384){
  520. // return false;
  521. // }
  522. // F = (int)((float) R / N);
  523. Fexact = (float)R / (float)N; /* RWD */
  524. // F2 = F / 2;
  525. if (W != -1)
  526. /* cannot be used with M; but we don't touch M anyway here */
  527. switch(W){
  528. case 0: M = 4*N;
  529. break;
  530. case 1: M = 2*N; /* RWD: we want this one, most of the time */
  531. break;
  532. case 2: M = N;
  533. break;
  534. case 3: M = N2;
  535. break;
  536. default:
  537. break;
  538. }
  539. M = (M != 0 ? M : N*2); /* RWD double-window as default */
  540. Mf = 1 - M%2;
  541. L = M;
  542. Lf = 1 - L%2;
  543. ibuflen = 4 * M;
  544. obuflen = 4 * L;
  545. if (W == -1)
  546. W = (int)(3.322 * log10((double)(4. * N) / M));/* cosmetic */
  547. if (Cj == 0)
  548. Cj = N2;
  549. if (Cj > N2)
  550. Cj = N2;
  551. if (Ci < 0)
  552. Ci = 0;
  553. D = (int)((D != 0 ? D : M/(8.0*(T > 1.0 ? T : 1.0))));
  554. if (D == 0){
  555. //fprintf(stderr,"pvoc: warning - T greater than M/8 \n");
  556. D = 1;
  557. }
  558. I = (int)(I != 0 ? I : (float) T*D );
  559. T = ((float) I / D);
  560. if (P != 1.)
  561. P = T;
  562. NO = (int)((float) N / P); /* synthesis transform will be NO points */
  563. NO = NO + NO%2; /* make NO even */
  564. NO2 = NO / 2;
  565. P = ((float) N / NO); /* ideally, N / NO = I / D = pitch change */
  566. Pinv = (float)(1.0/ P);
  567. if (warp == -1.)
  568. warp = P;
  569. if ((E == 1) && (warp == 0.))
  570. warp = 1.0f;
  571. //if ((P != 1.) && (P != T))
  572. // fprintf(stderr,"pvoc: warning P=%f not equal to T=%f\n",P,T);
  573. IO = (int)((float) I / P);
  574. nMax -= nMin;
  575. /*RWD need this to get sum setup for synth window! */
  576. /* NB vonHann window also available now */
  577. /* set up analysis window: The window is assumed to be symmetric
  578. with M total points. After the initial memory allocation,
  579. analWindow always points to the midpoint of the window
  580. (or one half sample to the right, if M is even); analWinLen
  581. is half the true window length (rounded down). Any low pass
  582. window will work; a Hamming window is generally fine,
  583. but a Kaiser is also available. If the window duration is
  584. longer than the transform (M > N), then the window is
  585. multiplied by a sin(x)/x function to meet the condition:
  586. analWindow[Ni] = 0 for i != 0. In either case, the
  587. window is renormalized so that the phase vocoder amplitude
  588. estimates are properly scaled. The maximum allowable
  589. window duration is ibuflen/2. */
  590. analWindow_base = new float[M+Mf];
  591. analWindow = analWindow_base + (analWinLen = M/2);
  592. if (K)
  593. kaiser(analWindow_base,M+Mf,beta); /* ??? or just M?*/
  594. else if (H)
  595. vonhann(analWindow,analWinLen,Mf);
  596. else
  597. hamming(analWindow,analWinLen,Mf);
  598. for (i = 1; i <= analWinLen; i++)
  599. *(analWindow - i) = *(analWindow + i - Mf);
  600. if (M > N) {
  601. if (Mf)
  602. *analWindow *=(float)( (double)N * sin((double)PI*.5/N) /(double)( PI*.5));
  603. for (i = 1; i <= analWinLen; i++)
  604. *(analWindow + i) *=(float)
  605. ((double)N * sin((double) (PI*(i+.5*Mf)/N)) / (PI*(i+.5*Mf))); /* D.T 2000*/
  606. for (i = 1; i <= analWinLen; i++)
  607. *(analWindow - i) = *(analWindow + i - Mf);
  608. }
  609. sum = 0.0f;
  610. for (i = -analWinLen; i <= analWinLen; i++)
  611. sum += *(analWindow + i);
  612. sum = (float)(2.0 / sum); /*factor of 2 comes in later in trig identity*/
  613. for (i = -analWinLen; i <= analWinLen; i++)
  614. *(analWindow + i) *= sum;
  615. /* set up synthesis window: For the minimal mean-square-error
  616. formulation (valid for N >= M), the synthesis window
  617. is identical to the analysis window (except for a
  618. scale factor), and both are even in length. If N < M,
  619. then an interpolating synthesis window is used. */
  620. synWindow_base = new float[L+Lf];
  621. synWindow = synWindow_base + (synWinLen = L/2);
  622. #ifdef USE_FFTW
  623. Ninv = (float) (1.0 / N);
  624. #endif
  625. if (M <= N){
  626. if(K)
  627. kaiser(synWindow_base,L+Lf,beta);
  628. else if(H)
  629. vonhann(synWindow,synWinLen,Lf);
  630. else
  631. hamming(synWindow,synWinLen,Lf);
  632. for (i = 1; i <= synWinLen; i++)
  633. *(synWindow - i) = *(synWindow + i - Lf);
  634. for (i = -synWinLen; i <= synWinLen; i++)
  635. *(synWindow + i) *= sum;
  636. sum = 0.0f;
  637. /* RWD ??? this jumps by overlap samps thru table*/
  638. for (i = -synWinLen; i <= synWinLen; i+=I)
  639. sum += *(synWindow + i) * *(synWindow + i);
  640. sum = (float)(1.0/ sum);
  641. #ifdef USE_FFTW
  642. sum *= Ninv;
  643. #endif
  644. for (i = -synWinLen; i <= synWinLen; i++)
  645. *(synWindow + i) *= sum;
  646. }
  647. else {
  648. if(K)
  649. kaiser(synWindow_base,L+Lf,beta);
  650. else if(H)
  651. vonhann(synWindow,synWinLen,Lf);
  652. else
  653. hamming(synWindow,synWinLen,Lf);
  654. for (i = 1; i <= synWinLen; i++)
  655. *(synWindow - i) = *(synWindow + i - Lf);
  656. if (Lf)
  657. *synWindow *= (float)((double)IO * sin((double) (PI*.5/IO)) / (double)(PI*.5));
  658. for (i = 1; i <= synWinLen; i++)
  659. *(synWindow + i) *=(float)
  660. ((double)IO * sin((double) (PI*(i+.5*Lf)/IO)) /(double) (PI*(i+.5*Lf)));
  661. for (i = 1; i <= synWinLen; i++)
  662. *(synWindow - i) = *(synWindow + i - Lf);
  663. sum = (float)(1.0/sum);
  664. #ifdef USE_FFTW
  665. sum *= Ninv;
  666. #endif
  667. for (i = -synWinLen; i <= synWinLen; i++)
  668. *(synWindow + i) *= sum;
  669. }
  670. #ifdef USE_FFTW
  671. in_fftw_size = N;
  672. out_fftw_size = NO;
  673. forward_plan = rfftwnd_create_plan_specific(1,&in_fftw_size,
  674. FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE | FFTW_IN_PLACE,
  675. (fftw_real*) analWindow_base,1,NULL,1);
  676. inverse_plan = rfftwnd_create_plan_specific(1,&out_fftw_size,
  677. FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE | FFTW_IN_PLACE,
  678. (fftw_real*) synWindow_base,1,NULL,1);
  679. #endif
  680. try{
  681. /* set up input buffer: nextIn always points to the next empty
  682. word in the input buffer (i.e., the sample following
  683. sample number (n + analWinLen)). If the buffer is full,
  684. then nextIn jumps back to the beginning, and the old
  685. values are written over. */
  686. input = new float[ibuflen];
  687. nextIn = input;
  688. /* set up output buffer: nextOut always points to the next word
  689. to be shifted out. The shift is simulated by writing the
  690. value to the standard output and then setting that word
  691. of the buffer to zero. When nextOut reaches the end of
  692. the buffer, it jumps back to the beginning. */
  693. output = new float [obuflen];
  694. nextOut = output;
  695. /* set up analysis buffer for (N/2 + 1) channels: The input is real,
  696. so the other channels are redundant. oldInPhase is used
  697. in the conversion to remember the previous phase when
  698. calculating phase difference between successive samples. */
  699. anal = new float[N+2];
  700. banal = anal + 1;
  701. oldInPhase = new float[N2+1];
  702. maxAmp = new float[N2+1];
  703. avgAmp = new float[N2+1];
  704. avgFrq = new float[N2+1];
  705. env = new float[N2+1];
  706. /* set up synthesis buffer for (N/2 + 1) channels: (This is included
  707. only for clarity.) oldOutPhase is used in the re-
  708. conversion to accumulate angle differences (actually angle
  709. difference per second). */
  710. syn = new float[NO+2];
  711. bsyn = syn + 1;
  712. oldOutPhase = new float[NO2+1];
  713. }
  714. catch(...){
  715. if(synWindow_base){
  716. delete [] synWindow_base;
  717. synWindow_base = 0;
  718. }
  719. if(analWindow_base){
  720. delete [] analWindow_base;
  721. analWindow_base = 0;
  722. }
  723. if(input) {
  724. delete [] input;
  725. input = 0;
  726. }
  727. if(output) {
  728. delete [] output;
  729. output = 0;
  730. }
  731. if(anal) {
  732. delete [] anal;
  733. anal = 0;
  734. }
  735. if(oldInPhase) {
  736. delete [] oldInPhase;
  737. oldInPhase = 0;
  738. }
  739. if(maxAmp){
  740. delete [] maxAmp;
  741. maxAmp = 0;
  742. }
  743. if(avgAmp) {
  744. delete [] avgAmp;
  745. avgAmp = 0;
  746. }
  747. if(avgFrq) {
  748. delete [] avgFrq;
  749. avgFrq = 0;
  750. }
  751. if(env){
  752. delete [] env;
  753. env= 0;
  754. }
  755. if(syn){
  756. delete [] syn;
  757. syn = 0;
  758. }
  759. if(oldOutPhase){
  760. delete [] oldOutPhase;
  761. oldOutPhase = 0;
  762. }
  763. return false;
  764. }
  765. outCount = 0;
  766. rIn = ((float) R / D);
  767. rOut = ((float) R / I);
  768. invR =((float) 1. / R);
  769. RoverTwoPi = (float)(rIn / TWOPI);
  770. TwoPioverR = (float)(TWOPI / rOut);
  771. nI = -(analWinLen / D) * D; /* input time (in samples) */
  772. nO = (long)((float) T/P * nI); /* output time (in samples) */
  773. Dd = analWinLen + nI + 1; /* number of new inputs to read */
  774. Ii = 0; /* number of new outputs to write */
  775. IOi = 0;
  776. flag = 1;
  777. for(i=0;i < ibuflen;i++) {
  778. input[i] = 0.0f;
  779. output[i] = 0.0f;
  780. }
  781. for(i=0;i < NO+2;i++)
  782. syn[i] = 0.0f;
  783. for(i=0;i < N+2;i++)
  784. anal[i] = 0.0f;
  785. for(i=0;i < NO2+1;i++)
  786. oldOutPhase[i] = 0.0f;
  787. for(i=0;i < N2+1;i++)
  788. oldInPhase[i] = maxAmp[i] = avgAmp[i] = avgFrq[i] = env[i] = 0.0f;
  789. return true;
  790. }
  791. phasevocoder::~phasevocoder()
  792. {
  793. if(synWindow_base)
  794. delete [] synWindow_base;
  795. if(analWindow_base)
  796. delete [] analWindow_base;
  797. if(input)
  798. delete [] input;
  799. if(output)
  800. delete [] output;
  801. if(anal)
  802. delete [] anal;
  803. if(oldInPhase)
  804. delete [] oldInPhase;
  805. if(maxAmp)
  806. delete [] maxAmp;
  807. if(avgAmp)
  808. delete [] avgAmp;
  809. if(avgFrq)
  810. delete [] avgFrq;
  811. if(env)
  812. delete [] env;
  813. if(syn)
  814. delete [] syn;
  815. if(oldOutPhase)
  816. delete [] oldOutPhase;
  817. #ifdef USE_FFTW
  818. rfftwnd_destroy_plan(forward_plan);
  819. rfftwnd_destroy_plan(inverse_plan);
  820. #endif
  821. }
  822. void phasevocoder::hamming(float *win,int winLen,int even)
  823. {
  824. double Pi,ftmp;
  825. int i;
  826. /***********************************************************
  827. Pi = (double)((double)4.*atan((double)1.));
  828. ***********************************************************/
  829. Pi = (double)PI;
  830. ftmp = Pi/winLen;
  831. if (even) {
  832. for (i=0; i<winLen; i++)
  833. *(win+i) = (float)(.54 + .46*cos(ftmp*((double)i+.5)));
  834. *(win+winLen) = 0.0;}
  835. else{
  836. *(win) = 1.0;
  837. for (i=1; i<=winLen; i++)
  838. *(win+i) = (float)(.54 + .46*cos(ftmp*(double)i));
  839. }
  840. }
  841. double phasevocoder::besseli( double x)
  842. {
  843. double ax, ans;
  844. double y;
  845. if (( ax = fabs( x)) < 3.75) {
  846. y = x / 3.75;
  847. y *= y;
  848. ans = (1.0 + y * ( 3.5156229 +
  849. y * ( 3.0899424 +
  850. y * ( 1.2067492 +
  851. y * ( 0.2659732 +
  852. y * ( 0.360768e-1 +
  853. y * 0.45813e-2))))));
  854. }
  855. else {
  856. y = 3.75 / ax;
  857. ans = ((exp ( ax) / sqrt(ax))
  858. * (0.39894228 +
  859. y * (0.1328592e-1 +
  860. y * (0.225319e-2 +
  861. y * (-0.157565e-2 +
  862. y * (0.916281e-2 +
  863. y * (-0.2057706e-1 +
  864. y * (0.2635537e-1 +
  865. y * (-0.1647633e-1 +
  866. y * 0.392377e-2)))))))));
  867. }
  868. return ans;
  869. }
  870. //courtesy of Csound....
  871. void phasevocoder::kaiser(float *win,int len,double Beta)
  872. {
  873. float *ft = win;
  874. double i,xarg = 1.0; //xarg = amp scalefactor
  875. for (i = -len/2.0 + .1 ; i < len/2.0 ; i++)
  876. *ft++ = (float) (xarg *
  877. besseli((Beta * sqrt(1.0-pow((2.0*i/(len - 1)), 2.0)))) /
  878. besseli( Beta));
  879. // assymetrical hack: sort out first value!
  880. win[0] = win[len-1];
  881. }
  882. void phasevocoder::vonhann(float *win,int winLen,int even)
  883. {
  884. float Pi,ftmp;
  885. int i;
  886. Pi = (float)PI;
  887. ftmp = Pi/winLen;
  888. if (even) {
  889. for (i=0; i<winLen; i++)
  890. *(win+i) = (float)(.5 + .5 *cos(ftmp*((double)i+.5)));
  891. *(win+winLen) = 0.0f;
  892. }
  893. else{ *(win) = 1.0f;
  894. for (i=1; i<=winLen; i++)
  895. *(win+i) =(float)(.5 + .5 *cos(ftmp*(double)i));
  896. }
  897. }
  898. long phasevocoder::process_frame(float *anal,float *outbuf,pvoc_frametype frametype)
  899. {
  900. /*RWD vars */
  901. int n;
  902. long written;
  903. float *obufptr;
  904. /* reconversion: The magnitude and angle-difference-per-second in syn
  905. (assuming an intermediate sampling rate of rOut) are
  906. converted to real and imaginary values and are returned in syn.
  907. This automatically incorporates the proper phase scaling for
  908. time modifications. */
  909. if (NO <= N){
  910. for (i = 0; i < NO+2; i++)
  911. *(syn+i) = *(anal+i);
  912. }
  913. else {
  914. for (i = 0; i <= N+1; i++)
  915. *(syn+i) = *(anal+i);
  916. for (i = N+2; i < NO+2; i++)
  917. *(syn+i) = 0.0f;
  918. }
  919. if(frametype==PVOC_AMP_PHASE){
  920. for(i=0, i0=syn, i1=syn+1; i<= NO2; i++, i0+=2, i1+=2){
  921. mag = *i0;
  922. phase = *i1;
  923. *i0 = (float)((double)mag * cos((double)phase));
  924. *i1 = (float)((double)mag * sin((double)phase));
  925. }
  926. }
  927. else if(frametype == PVOC_AMP_FREQ){
  928. for(i=0, i0=syn, i1=syn+1; i<= NO2; i++, i0+=2, i1+=2){
  929. mag = *i0;
  930. /* keep phase wrapped within +- TWOPI */
  931. /* this could possibly be spread across several frame cycles, as the problem does not
  932. develop for a while */
  933. double angledif, the_phase;
  934. angledif = TwoPioverR * (*i1 - ((float) i * /*F*/Fexact));
  935. the_phase = *(oldOutPhase + i) +angledif;
  936. if(i== bin_index)
  937. the_phase = (float) fmod(the_phase,TWOPI);
  938. *(oldOutPhase + i) = (float) the_phase;
  939. phase = (float) the_phase;
  940. *i0 = (float)((double)mag * cos((double)phase));
  941. *i1 = (float)((double)mag * sin((double)phase));
  942. }
  943. }
  944. #ifdef PVOC_NORM_PHASE
  945. if(++bin_index == NO2+1)
  946. bin_index = 0;
  947. #endif
  948. /* else it must be PVOC_COMPLEX */
  949. if (P != 1.)
  950. for (i = 0; i < NO+2; i++)
  951. *(syn+i) *= Pinv;
  952. /* synthesis: The synthesis subroutine uses the Weighted Overlap-Add
  953. technique to reconstruct the time-domain signal. The (N/2 + 1)
  954. phase vocoder channel outputs at time n are inverse Fourier
  955. transformed, windowed, and added into the output array. The
  956. subroutine thinks of output as a shift register in which
  957. locations are referenced modulo obuflen. Therefore, the main
  958. program must take care to zero each location which it "shifts"
  959. out (to standard output). The subroutines reals and fft
  960. together perform an efficient inverse FFT. */
  961. # ifdef USE_FFTW
  962. rfftwnd_one_complex_to_real(inverse_plan,(fftw_complex * )syn,NULL);
  963. # else
  964. reals_(syn,bsyn,NO2,2);
  965. fft_(syn,bsyn,1,NO2,1,2);
  966. #endif
  967. j = nO - synWinLen - 1;
  968. while (j < 0)
  969. j += obuflen;
  970. j = j % obuflen;
  971. k = nO - synWinLen - 1;
  972. while (k < 0)
  973. k += NO;
  974. k = k % NO;
  975. for (i = -synWinLen; i <= synWinLen; i++) { /*overlap-add*/
  976. if (++j >= obuflen)
  977. j -= obuflen;
  978. if (++k >= NO)
  979. k -= NO;
  980. *(output + j) += *(syn + k) * *(synWindow + i);
  981. }
  982. obufptr = outbuf; /*RWD */
  983. written = 0;
  984. for (i = 0; i < IOi;){ /* shift out next IOi values */
  985. int j;
  986. int todo = MIN(IOi-i, output+obuflen-nextOut);
  987. /*outfloats(nextOut, todo, ofd);*/
  988. /*copy data to external buffer */
  989. for(n=0;n < todo;n++)
  990. *obufptr++ = nextOut[n];
  991. written += todo;
  992. i += todo;
  993. outCount += todo;
  994. for(j = 0; j < todo; j++)
  995. *nextOut++ = 0.0f;
  996. if (nextOut >= (output + obuflen))
  997. nextOut -= obuflen;
  998. }
  999. if (flag) /* flag means do this operation only once */
  1000. if ((nI > 0) && (Dd < D)){ /* EOF detected */
  1001. flag = 0;
  1002. nMax = nI + analWinLen - (D - Dd);
  1003. }
  1004. /* D = some_function(nI); for variable time-scaling */
  1005. /* rIn = ((float) R / D); for variable time-scaling */
  1006. /* RoverTwoPi = rIn / TwoPi; for variable time-scaling */
  1007. nI += D; /* increment time */
  1008. nO += IO;
  1009. /* Dd = D except when the end of the sample stream intervenes */
  1010. /* RWD handle offline and streaming separately -
  1011. can't count an infinite number of real-time samples! */
  1012. if(m_mode == PVPP_OFFLINE)
  1013. Dd = MIN(D, MAX(0L, D+nMax-nI-analWinLen)); /* CARL */
  1014. else
  1015. Dd = D; /* RWD */
  1016. if (nO > (synWinLen + I))
  1017. Ii = I;
  1018. else
  1019. if (nO > synWinLen)
  1020. Ii = nO - synWinLen;
  1021. else {
  1022. Ii = 0;
  1023. for (i=nO+synWinLen; i<obuflen; i++)
  1024. if (i > 0)
  1025. *(output+i) = 0.0f;
  1026. }
  1027. IOi = (int)((float) Ii / P);
  1028. return written;
  1029. }
  1030. /*RWD arrgh! */
  1031. long phasevocoder::process_frame(float *anal,short *outbuf,pvoc_frametype frametype)
  1032. {
  1033. /*RWD vars */
  1034. int n;
  1035. long written;
  1036. short *obufptr;
  1037. /* reconversion: The magnitude and angle-difference-per-second in syn
  1038. (assuming an intermediate sampling rate of rOut) are
  1039. converted to real and imaginary values and are returned in syn.
  1040. This automatically incorporates the proper phase scaling for
  1041. time modifications. */
  1042. if (NO <= N){
  1043. for (i = 0; i < NO+2; i++)
  1044. *(syn+i) = *(anal+i);
  1045. }
  1046. else {
  1047. for (i = 0; i <= N+1; i++)
  1048. *(syn+i) = *(anal+i);
  1049. for (i = N+2; i < NO+2; i++)
  1050. *(syn+i) = 0.0f;
  1051. }
  1052. if(frametype != PVOC_COMPLEX){ /* assume AMP_FREQ otherwise, for now */
  1053. for(i=0, i0=syn, i1=syn+1; i<= NO2; i++, i0+=2, i1+=2){
  1054. mag = *i0;
  1055. *(oldOutPhase + i) += *i1 - ((float) i * /*F*/ Fexact);
  1056. phase = *(oldOutPhase + i) * TwoPioverR;
  1057. *i0 = (float)((double)mag * cos((double)phase));
  1058. *i1 = (float)((double)mag * sin((double)phase));
  1059. }
  1060. }
  1061. if (P != 1.)
  1062. for (i = 0; i < NO+2; i++)
  1063. *(syn+i) *= Pinv;
  1064. /* synthesis: The synthesis subroutine uses the Weighted Overlap-Add
  1065. technique to reconstruct the time-domain signal. The (N/2 + 1)
  1066. phase vocoder channel outputs at time n are inverse Fourier
  1067. transformed, windowed, and added into the output array. The
  1068. subroutine thinks of output as a shift register in which
  1069. locations are referenced modulo obuflen. Therefore, the main
  1070. program must take care to zero each location which it "shifts"
  1071. out (to standard output). The subroutines reals and fft
  1072. together perform an efficient inverse FFT. */
  1073. #ifdef USE_FFTW
  1074. rfftwnd_one_complex_to_real(inverse_plan,(fftw_complex * )syn,NULL);
  1075. #else
  1076. reals_(syn,bsyn,NO2,2);
  1077. fft_(syn,bsyn,1,NO2,1,2);
  1078. #endif
  1079. j = nO - synWinLen - 1;
  1080. while (j < 0)
  1081. j += obuflen;
  1082. j = j % obuflen;
  1083. k = nO - synWinLen - 1;
  1084. while (k < 0)
  1085. k += NO;
  1086. k = k % NO;
  1087. for (i = -synWinLen; i <= synWinLen; i++) { /*overlap-add*/
  1088. if (++j >= obuflen)
  1089. j -= obuflen;
  1090. if (++k >= NO)
  1091. k -= NO;
  1092. *(output + j) += *(syn + k) * *(synWindow + i);
  1093. }
  1094. obufptr = outbuf; /*RWD */
  1095. written = 0;
  1096. for (i = 0; i < IOi;){ /* shift out next IOi values */
  1097. int j;
  1098. int todo = MIN(IOi-i, output+obuflen-nextOut);
  1099. /*copy data to external buffer */
  1100. for(n=0;n < todo;n++)
  1101. *obufptr++ = (short) round(32767.0 * nextOut[n]);
  1102. written += todo;
  1103. i += todo;
  1104. outCount += todo;
  1105. for(j = 0; j < todo; j++)
  1106. *nextOut++ = 0.0f;
  1107. if (nextOut >= (output + obuflen))
  1108. nextOut -= obuflen;
  1109. }
  1110. if (flag) /* flag means do this operation only once */
  1111. if ((nI > 0) && (Dd < D)){ /* EOF detected */
  1112. flag = 0;
  1113. nMax = nI + analWinLen - (D - Dd);
  1114. }
  1115. /* D = some_function(nI); for variable time-scaling */
  1116. /* rIn = ((float) R / D); for variable time-scaling */
  1117. /* RoverTwoPi = rIn / TwoPi; for variable time-scaling */
  1118. nI += D; /* increment time */
  1119. nO += IO;
  1120. /* Dd = D except when the end of the sample stream intervenes */
  1121. Dd = MIN(D, MAX(0L, D+nMax-nI-analWinLen));
  1122. if (nO > (synWinLen + I))
  1123. Ii = I;
  1124. else
  1125. if (nO > synWinLen)
  1126. Ii = nO - synWinLen;
  1127. else {
  1128. Ii = 0;
  1129. for (i=nO+synWinLen; i<obuflen; i++)
  1130. if (i > 0)
  1131. *(output+i) = 0.0f;
  1132. }
  1133. IOi = (int)((float) Ii / P);
  1134. return written;
  1135. }
  1136. /* trying to get clean playback when looping! */
  1137. void phasevocoder::reset_phases(void)
  1138. {
  1139. int i;
  1140. if(oldInPhase){
  1141. for(i=0;i < N2+1;i++)
  1142. oldInPhase[i] = 0.0f;
  1143. }
  1144. if(oldOutPhase){
  1145. for(i=0;i < NO2+1;i++)
  1146. oldOutPhase[i] = 0.0f;
  1147. }
  1148. if(syn){
  1149. for(i=0;i < NO+2;i++)
  1150. syn[i] = 0.0f;
  1151. }
  1152. #ifdef _DEBUG
  1153. //printf("\nnI = %d; nO = %d; j = %d; k = %d\n",nI,nO,j,k);
  1154. #endif
  1155. /* these seem definitely necessary, but don't solve everything... */
  1156. nI = -(analWinLen / D) * D; /* input time (in samples) */
  1157. nO = (long)((float) T/P * nI); /* output time (in samples) */
  1158. }
  1159. /* we don't read in a single sample, a la pvoc, but just output an empty first frame*/
  1160. /* we assume final block zero-padded if necessary */
  1161. long phasevocoder::generate_frame(float *fbuf,float *outanal,long samps,pvoc_frametype frametype)
  1162. {
  1163. /*sblen = decfac = D */
  1164. //static int sblen = 0;
  1165. int got, tocp;
  1166. float *fp,*ofp;
  1167. got = samps; /*always assume */
  1168. if(got < Dd)
  1169. Dd = got;
  1170. fp = fbuf;
  1171. tocp = MIN(got, input+ibuflen-nextIn);
  1172. got -= tocp;
  1173. while(tocp-- > 0)
  1174. *nextIn++ = *fp++;
  1175. if(got > 0) {
  1176. nextIn -= ibuflen;
  1177. while(got-- > 0)
  1178. *nextIn++ = *fp++;
  1179. }
  1180. if (nextIn >= (input + ibuflen))
  1181. nextIn -= ibuflen;
  1182. if (nI > 0)
  1183. for (i = Dd; i < D; i++){ /* zero fill at EOF */
  1184. *(nextIn++) = 0.0f;
  1185. if (nextIn >= (input + ibuflen))
  1186. nextIn -= ibuflen;
  1187. }
  1188. /* analysis: The analysis subroutine computes the complex output at
  1189. time n of (N/2 + 1) of the phase vocoder channels. It operates
  1190. on input samples (n - analWinLen) thru (n + analWinLen) and
  1191. expects to find these in input[(n +- analWinLen) mod ibuflen].
  1192. It expects analWindow to point to the center of a
  1193. symmetric window of length (2 * analWinLen +1). It is the
  1194. responsibility of the main program to ensure that these values
  1195. are correct! The results are returned in anal as succesive
  1196. pairs of real and imaginary values for the lowest (N/2 + 1)
  1197. channels. The subroutines fft and reals together implement
  1198. one efficient FFT call for a real input sequence. */
  1199. for (i = 0; i < N+2; i++)
  1200. *(anal + i) = 0.0f; /*initialize*/
  1201. j = (nI - analWinLen - 1 + ibuflen) % ibuflen; /*input pntr*/
  1202. k = nI - analWinLen - 1; /*time shift*/
  1203. while (k < 0)
  1204. k += N;
  1205. k = k % N;
  1206. for (i = -analWinLen; i <= analWinLen; i++) {
  1207. if (++j >= ibuflen)
  1208. j -= ibuflen;
  1209. if (++k >= N)
  1210. k -= N;
  1211. *(anal + k) += *(analWindow + i) * *(input + j);
  1212. }
  1213. #ifdef USE_FFTW
  1214. rfftwnd_one_real_to_complex(forward_plan,(fftw_real*) anal,NULL);
  1215. //reals_(anal,banal,N2,-2);
  1216. #else
  1217. fft_(anal,banal,1,N2,1,-2);
  1218. reals_(anal,banal,N2,-2);
  1219. #endif
  1220. /* conversion: The real and imaginary values in anal are converted to
  1221. magnitude and angle-difference-per-second (assuming an
  1222. intermediate sampling rate of rIn) and are returned in
  1223. anal. */
  1224. if(frametype == PVOC_AMP_PHASE){
  1225. /* PVOCEX uses plain (wrapped) phase format, ref Soundhack */
  1226. for(i=0,i0=anal,i1=anal+1,oi=oldInPhase; i <= N2; i++,i0+=2,i1+=2, oi++){
  1227. real = *i0;
  1228. imag = *i1;
  1229. *i0 =(float) sqrt((double)(real * real + imag * imag));
  1230. /* phase unwrapping */
  1231. /*if (*i0 == 0.)*/
  1232. if(*i0 < 1.0E-10f)
  1233. //angleDif = 0.0f;
  1234. phase = 0.0f;
  1235. else {
  1236. #ifdef OLDCALC
  1237. rratio = atan((double)imag/(double)real);
  1238. if(real<0.0) {
  1239. if(imag<0.0)
  1240. rratio -= PI;
  1241. else
  1242. rratio += PI;
  1243. }
  1244. #else
  1245. rratio = atan2((double)imag,(double)real);
  1246. #endif
  1247. /*angleDif = (phase = (float)rratio) - *oi;
  1248. *oi = phase;
  1249. */
  1250. phase = (float)rratio;
  1251. }
  1252. *i1 = phase;
  1253. }
  1254. }
  1255. if(frametype==PVOC_AMP_FREQ){
  1256. for(i=0,i0=anal,i1=anal+1,oi=oldInPhase; i <= N2; i++,i0+=2,i1+=2, oi++){
  1257. real = *i0;
  1258. imag = *i1;
  1259. *i0 =(float) sqrt((double)(real * real + imag * imag));
  1260. /* phase unwrapping */
  1261. /*if (*i0 == 0.)*/
  1262. if(*i0 < 1.0E-10f)
  1263. angleDif = 0.0f;
  1264. else {
  1265. #ifdef OLDCALC
  1266. /*RWD 12.99: slower with Pentium Pro build, but otherwise faster??!?? */
  1267. rratio = atan((double)imag/(double)real);
  1268. if(real<0.0) {
  1269. if(imag<0.0)
  1270. rratio -= PI;
  1271. else
  1272. rratio += PI;
  1273. }
  1274. #else
  1275. /*RWD98*/ rratio = atan2((double)imag,(double)real);
  1276. #endif
  1277. angleDif = (phase = (float)rratio) - *oi;
  1278. *oi = phase;
  1279. }
  1280. if (angleDif > PI)
  1281. angleDif = (float)(angleDif - TWOPI);
  1282. if (angleDif < -PI)
  1283. angleDif = (float)(angleDif + TWOPI);
  1284. /* add in filter center freq.*/
  1285. *i1 = angleDif * RoverTwoPi + ((float) i * /*F*/Fexact);
  1286. }
  1287. }
  1288. /* else must be PVOC_COMPLEX */
  1289. fp = anal;
  1290. ofp = outanal;
  1291. for(i=0;i < N+2;i++)
  1292. *ofp++ = *fp++;
  1293. nI += D; /* increment time */
  1294. nO += IO;
  1295. /* deal with offline and streaming differently */
  1296. if(m_mode== PVPP_OFFLINE)
  1297. Dd = MIN(D, MAX(0L, D+nMax-nI-analWinLen)); /* CARL */
  1298. else
  1299. Dd = D; /* RWD */
  1300. if (nO > (synWinLen + I))
  1301. Ii = I;
  1302. else
  1303. if (nO > synWinLen)
  1304. Ii = nO - synWinLen;
  1305. else {
  1306. Ii = 0;
  1307. for (i=nO+synWinLen; i<obuflen; i++)
  1308. if (i > 0)
  1309. *(output+i) = 0.0f;
  1310. }
  1311. IOi = (int)((float) Ii / P);
  1312. return D;
  1313. }