distortm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  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. See the
  14. 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
  18. 02111-1307 USA
  19. *
  20. */
  21. /* floatsam version */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <globcon.h>
  28. #include <processno.h>
  29. #include <modeno.h>
  30. #include <arrays.h>
  31. #include <distort.h>
  32. #include <cdpmain.h>
  33. #include <sfsys.h>
  34. #include <osbind.h>
  35. static int do_multiply(int endend,int phaseswitch,int lastzero,int *output_phase,int current_buf,
  36. int *endsample,dataptr dz);
  37. static int do_cros_multiply(int endend,int phaseswitch,int lastzero,int *output_phase,int current_buf,
  38. int *endsample,dataptr dz);
  39. static int do_the_division(int i,int *output_phase,int *startindex,int *startmarker,int *endindex,int lastzero,
  40. int lastmarker,int *no_of_half_cycles,int current_buf,int *endsample,dataptr dz);
  41. static int do_divide(float *ob1,float *ob2,int input_phase,int endend,double ratio,int *output_phase,
  42. int startindex,int endindex,int current_buf,int *endsample,dataptr dz);
  43. static int do_idivide(float *ob1,float *ob2,int input_phase,int endend,double ratio,int *output_phase,
  44. int startindex,int endindex,int current_buf,int *endsample,dataptr dz);
  45. static int too_big(int startindex,int endindex,dataptr dz);
  46. static int get_iphase(int startmarker,int startindex,int current_buf,dataptr dz);
  47. static float interpval2(int m,int current_buf,double ratio,int startindex,dataptr dz);
  48. static float interpval(int m,int current_buf,double ratio,int startindex,dataptr dz);
  49. static float getval2(int m,int current_buf,double ratio,int startindex,dataptr dz);
  50. static float getval(int m,int current_buf,double ratio,int startindex,dataptr dz);
  51. static double set_ratio(int endend,int startindex,int endindex,dataptr dz);
  52. static int do_md(int crosbuf,int current_buf,int i,int lastzero,int lastmarker,int *output_phase,
  53. int *no_of_half_cycles,int *endsample,int *startindex,int *startmarker,
  54. int *endindex,dataptr dz);
  55. /************************* MDISTORT ***********************************
  56. *
  57. * Works on SINGLE HALF wavecycles.
  58. *
  59. * This looks for single half-wavecycles, and does not change there
  60. * number (or their phase) in the output. It therefore resets the
  61. * input & output phase on each entry.
  62. */
  63. int mdistort(int is_last,int *lastzero,int *endsample,int *output_phase,int current_buf,
  64. float *cyclemax,int *no_of_half_cycles,int *startindex,int *startmarker,int *endindex,dataptr dz)
  65. {
  66. int exit_status;
  67. int crosbuf = FALSE;
  68. register int i = *lastzero;
  69. int samples, lastmarker; //, oldbufcnt;
  70. int phase;
  71. float *b = dz->sampbuf[current_buf];
  72. if(is_last)
  73. samples = dz->ssampsread;
  74. else
  75. samples = dz->buflen;
  76. //TW CHANGED
  77. while(smpflteq(b[i],0.0) && i<samples)
  78. i++;
  79. lastmarker = i;
  80. if(b[i]<0)
  81. phase = -1;
  82. else
  83. phase = 1;
  84. while(i < samples) {
  85. switch(phase) {
  86. case(1):
  87. if(b[i] > 0) {
  88. if(b[i]>*cyclemax)
  89. *cyclemax=b[i];
  90. i++;
  91. } else {
  92. if((exit_status = do_md(crosbuf,current_buf,i,*lastzero,lastmarker,output_phase,
  93. no_of_half_cycles,endsample,startindex,startmarker,endindex,dz))<0)
  94. return(exit_status);
  95. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  96. if(b[i]<0)
  97. phase = -1;
  98. }
  99. break;
  100. case(-1):
  101. if(b[i] < 0.0) {
  102. if(b[i]<*cyclemax)
  103. *cyclemax=b[i];
  104. i++;
  105. } else {
  106. if((exit_status = do_md(crosbuf,current_buf,i,*lastzero,lastmarker,output_phase,
  107. no_of_half_cycles,endsample,startindex,startmarker,endindex,dz))<0)
  108. return(exit_status);
  109. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  110. if(b[i]>0.0)
  111. phase = 1;
  112. }
  113. break;
  114. }
  115. }
  116. if(!is_last) {
  117. crosbuf = TRUE;
  118. //oldbufcnt = dz->buflen - *lastzero;
  119. b = dz->sampbuf[!current_buf];
  120. samples = dz->ssampsread;
  121. i = 0;
  122. exit_status = CONTINUE;
  123. while(exit_status == CONTINUE && i < samples) {
  124. switch(phase) {
  125. case(1):
  126. if(b[i] > 0.0) {
  127. if(b[i] > *cyclemax)
  128. *cyclemax=b[i];
  129. i++;
  130. } else {
  131. if((exit_status = do_md(crosbuf,current_buf,i,*lastzero,lastmarker,output_phase,
  132. no_of_half_cycles,endsample,startindex,startmarker,endindex,dz))<0)
  133. return(exit_status);
  134. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  135. if(b[i]<0)
  136. phase = -1;
  137. }
  138. break;
  139. case(-1):
  140. if(b[i] < 0.0) {
  141. if(b[i]<*cyclemax)
  142. *cyclemax=b[i];
  143. i++;
  144. } else {
  145. if((exit_status = do_md(crosbuf,current_buf,i,*lastzero,lastmarker,output_phase,
  146. no_of_half_cycles,endsample,startindex,startmarker,endindex,dz))<0)
  147. return(exit_status);
  148. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  149. if(b[i]>0.0)
  150. phase = 1;
  151. }
  152. break;
  153. }
  154. }
  155. #ifdef NOTDEF
  156. /* removed TW 5:2002 */
  157. if(i >= samples) {
  158. sprintf(errstr,"wavecycle-group too large for buffer.\n");
  159. return(GOAL_FAILED);
  160. }
  161. #endif
  162. }
  163. return(FINISHED);
  164. }
  165. /*************************** DO_MULTIPLY ******************************
  166. *
  167. * (1) Set up pointers to input and output buffers.
  168. * (4) Length of the complete half_cycle established.
  169. * (5) "mid_cyclelen" is length of first half-cycle in transformed sound.
  170. * (6) "remnant" is the number of samples left over when the cycling factor
  171. * is divided into the true src half_cyclelen.
  172. * (7) "mid_cycle" is buffer index of first half-cycle in transformed sound.
  173. * (8) With no phase-inversion, 1st OUTPUT half_cycle established by indexing
  174. * complete input half_cycle.
  175. * (9) With phase-inversion, ditto + phase-inversion.
  176. * (10) If there are remnant samples, interpolating sample pads are added
  177. * between the output half-cycles, until no remant samples remain.
  178. * (1)) For all the remaining output halfcycles, establish start at end of
  179. * previous halfcycle, and end at mid_cycle_len beyond this.
  180. * (12) If it's on odd half-cycle, copy first half-cycle here, with
  181. * phase-inversion.
  182. * (13) But if it's an even half-cycle, copy ditto WITHOUT phase inversion.
  183. * (14) If factor is odd, startphase of transformed segements switch from
  184. * positive to negative and vice versa.
  185. */
  186. int do_multiply(int endend,int phaseswitch,int lastzero,int *output_phase,int current_buf,int *endsample,dataptr dz)
  187. {
  188. int index;
  189. register int n, m, k;
  190. float *b1 = dz->sampbuf[current_buf]; /* 1 */
  191. float *ob1 = dz->sampbuf[current_buf+2];
  192. int start, half_cyclelen, mid_cyclelen, remnant, mid_cycle;
  193. half_cyclelen = endend - lastzero; /* 4 */
  194. mid_cyclelen = half_cyclelen/dz->iparam[DISTORTM_FACTOR]; /* 5 */
  195. if(!mid_cyclelen) {
  196. switch(phaseswitch) {
  197. case(1):
  198. for(n = lastzero; n < endend; n++) /* i.e. just copy */
  199. ob1[n] = b1[n];
  200. break;
  201. case(-1):
  202. for(n = lastzero; n < endend; n++)
  203. ob1[n] = -b1[n];
  204. break;
  205. }
  206. return(FINISHED);
  207. }
  208. remnant = half_cyclelen - (mid_cyclelen * dz->iparam[DISTORTM_FACTOR]); /* 6 */
  209. mid_cycle = lastzero + mid_cyclelen; /* 7 */
  210. switch(phaseswitch) {
  211. case(1):
  212. for(n = lastzero,m=0; n < mid_cycle; n++,m++) {
  213. index = round((double)m * (double)dz->iparam[DISTORTM_FACTOR]); /* 8 */
  214. index += lastzero;
  215. ob1[n] = b1[index];
  216. }
  217. break;
  218. case(-1):
  219. for(n = lastzero,m=0; n < mid_cycle; n++,m++) {
  220. index = round((double)m * (double)dz->iparam[DISTORTM_FACTOR]); /* 9 */
  221. index += lastzero;
  222. ob1[n] = -b1[index];
  223. }
  224. break;
  225. }
  226. if(remnant) {
  227. ob1[mid_cycle] = ob1[mid_cycle-1]/2; /* 10 */
  228. mid_cycle++;
  229. remnant--;
  230. }
  231. for(k=1; k<dz->iparam[DISTORTM_FACTOR]; k++) {
  232. start = mid_cycle; /* 11 */
  233. mid_cycle += mid_cyclelen;
  234. if(ODD(k)) { /* 12 */
  235. for(n = start, m = lastzero; n < mid_cycle; n++, m++)
  236. ob1[n] = -ob1[m];
  237. } else { /* 13 */
  238. for(n = start, m = lastzero; n < mid_cycle; n++, m++)
  239. ob1[n] = ob1[m];
  240. }
  241. if(remnant) {
  242. ob1[mid_cycle] = ob1[n-1] /*/2 */ * 0.5f;
  243. mid_cycle++; /* 10 */
  244. remnant--;
  245. }
  246. }
  247. if(dz->vflag[DISTORTM_SMOOTH]) { /* TW MAR:1995 Smoothing at end of cycle */
  248. while(mid_cycle < endend) {
  249. ob1[mid_cycle] = ob1[mid_cycle-1] /*/2 */ * 0.5f;
  250. mid_cycle++;
  251. }
  252. }
  253. if(ODD(dz->iparam[DISTORTM_FACTOR])) /* 14 */
  254. *output_phase = -(*output_phase);
  255. *endsample = endend;
  256. return(FINISHED);
  257. }
  258. /*************************** DO_CROS_MULTIPLY ******************************/
  259. int do_cros_multiply
  260. (int endend,int phaseswitch,int lastzero,int *output_phase,int current_buf,int *endsample,dataptr dz)
  261. {
  262. /*short*/int index; /* RWD 4:2002 why a short ? */
  263. register int m, n, k;
  264. float *b1 = dz->sampbuf[current_buf];
  265. float *bb, *q;
  266. float *ob1 = dz->sampbuf[current_buf + 2];
  267. float *b2 = dz->sampbuf[!current_buf];
  268. float *ob2 = dz->sampbuf[(!current_buf) + 2];
  269. int start, half_cyclelen, mid_cyclelen, remnant;
  270. int mid_cycle, oldbufcnt;
  271. int is_2nd_buf = 0;
  272. half_cyclelen = endend + dz->buflen - lastzero;
  273. mid_cyclelen = half_cyclelen/dz->iparam[DISTORTM_FACTOR];
  274. remnant = half_cyclelen - (mid_cyclelen * dz->iparam[DISTORTM_FACTOR]);
  275. mid_cycle = lastzero + mid_cyclelen;
  276. if(!mid_cyclelen) { /* i.e. IF WERE TRYING TO CREATE MORE CYCLES THAN THERE ARE SAMPLES */
  277. switch(phaseswitch) {
  278. case(1):
  279. for(n = lastzero; n < dz->buflen; n++) /* i.e. just copy */
  280. ob1[n] = b1[n];
  281. for(n = 0; n < endend; n++)
  282. ob2[n] = b2[n];
  283. break;
  284. case(-1):
  285. for(n = lastzero; n < dz->buflen; n++) /* i.e. just copy */
  286. ob1[n] = -b1[n];
  287. for(n = 0; n < endend; n++)
  288. ob2[n] = -b2[n];
  289. break;
  290. }
  291. return(FINISHED);
  292. }
  293. /* OTHERWISE: USE dz->sampbuf[4] and [5] AS TEMPORARY STORAGE LOCATIONS */
  294. oldbufcnt = dz->buflen - lastzero;
  295. bb = dz->sampbuf[4]; /* copy start of complete halfcycle to dz->sampbuf[4] */
  296. q = b1 + lastzero;
  297. memmove(bb,q,oldbufcnt * sizeof(float));
  298. bb += oldbufcnt; /* copy rest of complete halfcycle to end of that */
  299. q = b2;
  300. memmove(bb,q,endend * sizeof(float));
  301. for(n=0; n < mid_cyclelen; n++) {
  302. index = round((double)n * (double)dz->iparam[DISTORTM_FACTOR]);
  303. dz->sampbuf[5][n] = dz->sampbuf[4][index];/* convert halfcycle to compressed form */
  304. }
  305. start = lastzero;
  306. if(mid_cycle > dz->buflen)
  307. is_2nd_buf = 1;
  308. if(!is_2nd_buf) { /* GOAL HALFCYCLE LIES IN FIRST BUFFER */
  309. switch(phaseswitch) {
  310. case(1):
  311. for(n=start,m=0; n < mid_cycle; n++,m++)
  312. ob1[n] = dz->sampbuf[5][m];
  313. break;
  314. case(-1):
  315. for(n=start,m=0; n < mid_cycle; n++,m++)
  316. ob1[n] = -dz->sampbuf[5][m];
  317. break;
  318. }
  319. if(remnant) {
  320. if(mid_cycle < dz->buflen) {
  321. ob1[mid_cycle] = ob1[n-1]/2;
  322. if(++mid_cycle >= dz->buflen) {
  323. mid_cycle = 0;
  324. is_2nd_buf = 1;
  325. }
  326. } else {
  327. ob2[0] = ob1[dz->buflen-1]/2;
  328. mid_cycle = 1;
  329. is_2nd_buf = 1;
  330. }
  331. remnant--;
  332. }
  333. } else { /* GOAL HALFCYCLE STRADDLES THE 2 BUFFERS */
  334. switch(phaseswitch) {
  335. case(1):
  336. for(n=start,m=0; n < dz->buflen; n++,m++)
  337. ob1[n] = dz->sampbuf[5][m];
  338. mid_cycle -= dz->buflen;
  339. for(n=0; n < mid_cycle; n++, m++)
  340. ob2[n] = dz->sampbuf[5][m];
  341. break;
  342. case(-1):
  343. for(n=start,m=0; n < dz->buflen; n++,m++)
  344. ob1[n] = -dz->sampbuf[5][m];
  345. mid_cycle -= dz->buflen;
  346. for(n=0; n < mid_cycle; n++, m++)
  347. ob2[n] = -dz->sampbuf[5][m];
  348. break;
  349. }
  350. }
  351. phaseswitch = -phaseswitch;
  352. for(k=1; k<dz->iparam[DISTORTM_FACTOR]; k++) { /* FOR ALL THE OTHER HALF-CYCLES */
  353. if(!is_2nd_buf) {
  354. start = mid_cycle;
  355. mid_cycle += mid_cyclelen;
  356. if(mid_cycle < dz->buflen) {
  357. if(phaseswitch==1) { /* GOAL HALFCYCLE LIES IN 1ST BUFFER */
  358. for(n = start, m = 0; n < mid_cycle; n++, m++)
  359. ob1[n] = dz->sampbuf[5][m];
  360. } else {
  361. for(n = start, m = 0; n < mid_cycle; n++, m++)
  362. ob1[n] = -dz->sampbuf[5][m];
  363. }
  364. if(remnant) {
  365. ob1[mid_cycle] = ob1[n-1] /*/2*/ * 0.5f;
  366. remnant--;
  367. if(++mid_cycle>=dz->buflen) {
  368. mid_cycle = 0;
  369. is_2nd_buf = 1;
  370. }
  371. }
  372. } else { /* GOAL HALFCYCLE STRADDLES 2 BUFFERS */
  373. mid_cycle -= dz->buflen;
  374. if(phaseswitch==1) {
  375. for(n = start, m = 0; n < dz->buflen; n++, m++)
  376. ob1[n] = dz->sampbuf[5][m];
  377. for(n = 0; n < mid_cycle; n++, m++)
  378. ob2[n] = dz->sampbuf[5][m];
  379. } else {
  380. for(n = start, m = 0; n < dz->buflen; n++, m++)
  381. ob1[n] = -dz->sampbuf[5][m];
  382. for(n = 0; n < mid_cycle; n++, m++)
  383. ob2[n] = -dz->sampbuf[5][m];
  384. }
  385. if(remnant) {
  386. if(n>0)
  387. ob2[mid_cycle] = ob2[n-1] /*/2 */ * 0.5f;
  388. else
  389. ob2[mid_cycle] = ob1[dz->buflen-1] /* /2*/ * 0.5f;
  390. remnant--;
  391. mid_cycle++;
  392. }
  393. is_2nd_buf = 1;
  394. }
  395. } else { /* GOAL HALFCYCLE IS ENTIRELY IN 2nd BUFFER */
  396. start = mid_cycle;
  397. mid_cycle += mid_cyclelen;
  398. if(phaseswitch==1) {
  399. for(n = start, m = 0; n < mid_cycle; n++, m++)
  400. ob2[n] = dz->sampbuf[5][m];
  401. } else {
  402. for(n = start, m = 0; n < mid_cycle; n++, m++)
  403. ob2[n] = -dz->sampbuf[5][m];
  404. }
  405. if(remnant) {
  406. ob2[mid_cycle] = ob2[n-1] /* /2 */ * 0.5f;
  407. remnant--;
  408. mid_cycle++;
  409. }
  410. }
  411. phaseswitch = -phaseswitch;
  412. }
  413. if(dz->vflag[DISTORTM_SMOOTH]) { /* TW MAR:1995 Smoothing at end of cycle */
  414. if(mid_cycle!=endend) {
  415. if(mid_cycle > endend) {
  416. while(mid_cycle < dz->buflen) {
  417. ob1[mid_cycle] = ob1[mid_cycle-1]/* /2 */ * 0.5f;
  418. mid_cycle++;
  419. }
  420. mid_cycle = 0;
  421. if(endend>0)
  422. ob2[mid_cycle++] = ob1[dz->buflen-1]/* /2 */ * 0.5f;
  423. } else {
  424. if(mid_cycle==0)
  425. ob2[mid_cycle++] = ob1[dz->buflen-1]/* /2 */ * 0.5f;
  426. }
  427. while(mid_cycle < endend) {
  428. ob2[mid_cycle] = ob2[mid_cycle-1]/* /2 */ * 0.5f;
  429. mid_cycle++;
  430. }
  431. }
  432. }
  433. if(ODD(dz->iparam[DISTORTM_FACTOR])) /* CHANGE OUTPUT PHASE FOR NEXT PASS, IF FACTOR IS ODD */
  434. *output_phase = -(*output_phase);
  435. *endsample = endend;
  436. return(FINISHED);
  437. }
  438. /************************* SET_PHASESWITCH ****************************
  439. *
  440. * (1) lastmarker is AFTER lastzero, So if it's bufptr val is LESS
  441. * it must be in the other buffer.
  442. * (2) If the factor is ODD, the start-phase of the waveform switches
  443. * every cycle. In this case, if input phase and output start-phase are
  444. * same, no phase-inversion necessary (phaseswitch = 1). Otherwise
  445. * phase of input signal must be inverted (phaseswitch = -1).
  446. * The logic of the code is equivalent to this (though it takes
  447. * a moment to deduce this!!
  448. * (3) If the factor is even, output start-phase is always positive.
  449. * Hence input phase must be inverted (phaseswitch = -1) only if it is
  450. * -ve.
  451. */
  452. int set_phaseswitch(int output_phase,int lastzero,int lastmarker,int current_buf,dataptr dz)
  453. {
  454. float *bbuf = dz->sampbuf[current_buf];
  455. if(lastmarker < lastzero) /* 1 */
  456. bbuf = dz->sampbuf[!current_buf];
  457. if(ODD(dz->iparam[DISTORTM_FACTOR])) { /* 2 */
  458. if(bbuf[lastmarker] > 0.0)
  459. return(output_phase);
  460. else
  461. return(-output_phase);
  462. } else { /* 3 */
  463. if(bbuf[lastmarker] > 0.0)
  464. return(1);
  465. } /* ELSE */
  466. return(-1);
  467. }
  468. /************************* DO_THE_DIVISION ****************************/
  469. int do_the_division(int i,int *output_phase,int *startindex,int *startmarker,int *endindex,int lastzero,
  470. int lastmarker,int *no_of_half_cycles,int current_buf,int *endsample,dataptr dz)
  471. {
  472. int exit_status;
  473. double ratio;
  474. float *ob1,*ob2;
  475. int input_phase;
  476. (*no_of_half_cycles)++;
  477. if(*no_of_half_cycles == 1) {
  478. *startindex = lastzero;
  479. *startmarker = lastmarker;
  480. *endindex = i;
  481. } else {
  482. if(*no_of_half_cycles >= dz->iparam[DISTORTM_FACTOR]) {
  483. if(too_big(*startindex,*endindex,dz)) {
  484. sprintf(errstr,"Wavecycle length exceeds buffer length at %lf\n",
  485. (double)(dz->total_samps_written + lastzero)/(double)dz->infile->srate);
  486. return(GOAL_FAILED);
  487. }
  488. ratio = set_ratio(i,*startindex,*endindex,dz);
  489. ob1 = dz->sampbuf[current_buf + 2];
  490. ob2 = dz->sampbuf[(!current_buf) + 2];
  491. input_phase = get_iphase(*startmarker,*startindex,current_buf,dz) * (*output_phase);
  492. if(dz->vflag[DISTORTD_INTERP])
  493. exit_status = do_idivide(ob1,ob2,input_phase,i,ratio,output_phase,*startindex,*endindex,current_buf,endsample,dz);
  494. else
  495. exit_status = do_divide(ob1,ob2,input_phase,i,ratio,output_phase,*startindex,*endindex,current_buf,endsample,dz);
  496. if(exit_status < 0)
  497. return(exit_status);
  498. *no_of_half_cycles = 0;
  499. return(FINISHED);
  500. }
  501. }
  502. return(CONTINUE);
  503. }
  504. /************************** SET_RATIO ***************************/
  505. double set_ratio(int endend,int startindex,int endindex,dataptr dz)
  506. {
  507. double ratio;
  508. if(startindex < endindex) {
  509. if(endindex < endend) /* SRC CYC & ALL GOAL CYCS IN SAME BUFFER */
  510. ratio =(double)(endindex - startindex)/(double)(endend - startindex);
  511. else /* SRC CYC IN 1 BUF, GOAL CYC CROSSES BUFS */
  512. ratio = (double)(endindex - startindex)/(double)(endend + dz->buflen - startindex);
  513. } else { /* SRC CYC & ALL GOAL CYCS, CROSS BUFFERS */
  514. ratio = (double)(endindex + dz->buflen - startindex)/(double)(endend + dz->buflen - startindex);
  515. }
  516. return(ratio);
  517. }
  518. /*************************** DO_DIVIDE ******************************/
  519. int do_divide(float *ob1,float *ob2,int input_phase,int endend,double ratio,int *output_phase,
  520. int startindex,int endindex,int current_buf,int *endsample,dataptr dz)
  521. {
  522. register int n, m;
  523. switch(input_phase) {
  524. case(1): /* SRC CYCLE AND ENTIRE SET OF GOAL CYCLES IN SAME BUFFER */
  525. if(startindex < endindex) {
  526. if(endindex < endend) {
  527. for(n=startindex,m=0; n <endend; n++,m++)
  528. ob1[n] = getval(m,current_buf,ratio,startindex,dz);
  529. } else { /* SRC CYCLE IN ONE BUFFER, GOAL CYCLE CROSSES BUFFER BOUNDARY */
  530. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  531. ob1[n] = getval(m,current_buf,ratio,startindex,dz);
  532. for(n=0; n <endend; n++,m++)
  533. ob2[n] = getval(m,current_buf,ratio,startindex,dz);
  534. }
  535. } else { /* SRC CYCLE AND SET OF GOAL CYCLES, BOTH CROSS BUFFER BOUNDARY */
  536. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  537. ob1[n] = getval(m,current_buf,ratio,startindex,dz);
  538. for(n=0; n <endend; n++,m++)
  539. ob2[n] = getval2(m,current_buf,ratio,startindex,dz);
  540. }
  541. break;
  542. case(-1):
  543. if(startindex < endindex) {
  544. if(endindex < endend) { /* SRC CYC & ALL GOAL CYCS IN 1 BUFF */
  545. for(n=startindex,m=0; n <endend; n++,m++)
  546. ob1[n] = -getval(m,current_buf,ratio,startindex,dz);
  547. } else { /* SRC CYC IN 1 BUF, GOAL SET CROSSES BUFFS */
  548. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  549. ob1[n] = -getval(m,current_buf,ratio,startindex,dz);
  550. for(n=0; n <endend; n++,m++)
  551. ob2[n] = -getval(m,current_buf,ratio,startindex,dz);
  552. }
  553. } else { /* SRC CYC & GOAL SET, CROSS BUFFS */
  554. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  555. ob1[n] = -getval(m,current_buf,ratio,startindex,dz);
  556. for(n=0; n <endend; n++,m++)
  557. ob2[n] = -getval2(m,current_buf,ratio,startindex,dz);
  558. }
  559. break;
  560. }
  561. *output_phase = -(*output_phase);
  562. *endsample = endend;
  563. return(FINISHED);
  564. }
  565. /*************************** DO_IDIVIDE ******************************/
  566. int do_idivide(float *ob1,float *ob2,int input_phase,int endend,double ratio,int *output_phase,
  567. int startindex,int endindex,int current_buf,int *endsample,dataptr dz)
  568. {
  569. register int n = 0, m;
  570. switch(input_phase) {
  571. case(1):
  572. if(startindex < endindex) {
  573. if(endindex < endend) { /* SRC CYC & ALL GOAL CYCS IN SAME BUFF */
  574. for(n=startindex,m=0; n <endend; n++,m++)
  575. ob1[n] = interpval(m,current_buf,ratio,startindex,dz);
  576. } else { /* SRC CYC IN 1 BUF, GOAL CYC CROSSES BUF */
  577. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  578. ob1[n] = interpval(m,current_buf,ratio,startindex,dz);
  579. for(n=0; n <endend; n++,m++)
  580. ob2[n] = interpval(m,current_buf,ratio,startindex,dz);
  581. }
  582. } else { /* SRC CYC & GOAL SET, CROSS BUFFERS */
  583. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  584. ob1[n] = interpval(m,current_buf,ratio,startindex,dz);
  585. for(n=0; n <endend; n++,m++)
  586. ob2[n] = interpval2(m,current_buf,ratio,startindex,dz);
  587. }
  588. break;
  589. case(-1):
  590. if(startindex < endindex) {
  591. if(endindex < endend) { /* SRC CYC & ALL GOAL CYCS IN SAME BUFF */
  592. for(n=startindex,m=0; n <endend; n++,m++)
  593. ob1[n] = -interpval(m,current_buf,ratio,startindex,dz);
  594. } else { /* SRC CYC IN 1 BUF, GOAL SET CROSSES BUF */
  595. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  596. ob1[n] = -interpval(m,current_buf,ratio,startindex,dz);
  597. for(n=0; n <endend; n++,m++)
  598. ob2[n] = -interpval(m,current_buf,ratio,startindex,dz);
  599. }
  600. } else { /* SRC CYC & GOAL SET, CROSS BUFFS */
  601. for(n=startindex,m=0; n <dz->buflen; n++,m++)
  602. ob1[n] = -interpval(m,current_buf,ratio,startindex,dz);
  603. for(n=0; n <endend; n++,m++)
  604. ob2[n] = -interpval2(m,current_buf,ratio,startindex,dz);
  605. }
  606. break;
  607. }
  608. *output_phase = -(*output_phase);
  609. *endsample = n;
  610. return(FINISHED);
  611. }
  612. /************************** GETVAL ******************************/
  613. float getval(int m,int current_buf,double ratio,int startindex,dataptr dz)
  614. {
  615. float *bbuf = dz->sampbuf[current_buf];
  616. int index = (int)round((double)m * ratio);
  617. if((index += startindex)>=dz->buflen)
  618. index = dz->buflen-1;
  619. return(bbuf[index]);
  620. }
  621. /************************ GETVAL2 ****************************/
  622. float getval2(int m,int current_buf,double ratio,int startindex,dataptr dz)
  623. {
  624. float *bbuf;
  625. int index = (int)round((double)m * ratio);
  626. index += startindex;
  627. if(index >= dz->buflen) { /* If beyond end of thisbuf, */
  628. if((index -= dz->buflen)>=dz->buflen) /* put index within range */
  629. index = dz->buflen-1;
  630. bbuf = dz->sampbuf[!current_buf]; /* BUT read from other buf. */
  631. } else
  632. bbuf = dz->sampbuf[current_buf];
  633. return(bbuf[index]);
  634. }
  635. /***************************** INTERPVAL **********************************/
  636. float interpval(int m,int current_buf,double ratio,int startindex,dataptr dz)
  637. {
  638. float *bbuf = dz->sampbuf[current_buf];
  639. double findex = (double)m * ratio;
  640. int index = (int)findex; /* truncate */
  641. double frac = findex - (double)index;
  642. float diff,bb,ba;
  643. index += startindex;
  644. ba = bbuf[index];
  645. if(++index>=dz->buflen) {
  646. bbuf = dz->sampbuf[!current_buf];
  647. index = 0;
  648. }
  649. bb = bbuf[index];
  650. diff = (float) /*round*/((double)(bb - ba) * frac);
  651. return(ba + diff);
  652. }
  653. /****************************** INTERPVAL2 ***************************/
  654. float interpval2(int m,int current_buf,double ratio,int startindex,dataptr dz)
  655. {
  656. float *bbuf;
  657. float diff, ba, bb;
  658. double findex = (double)m * ratio;
  659. int index = (int)findex; /* truncate */
  660. double frac = findex - (double)index;
  661. index += startindex;
  662. if(index >= dz->buflen) { /* If beyond end thisbuf */
  663. if((index -= dz->buflen)>=dz->buflen) /* put index within range */
  664. index = dz->buflen-1;
  665. bbuf = dz->sampbuf[!current_buf]; /* BUT read from other buf. */
  666. ba = bbuf[index];
  667. if(index < dz->buflen-1)
  668. index++;
  669. } else {
  670. bbuf = dz->sampbuf[current_buf];
  671. ba = bbuf[index];
  672. if(++index>=dz->buflen) { /* IF at very end of buffer */
  673. bbuf = dz->sampbuf[!current_buf];
  674. index = 0; /* goto firstsamp in nextbuf */
  675. }
  676. }
  677. bb = bbuf[index];
  678. diff = (float)/*round*/((double)(bb - ba) * frac);
  679. return(ba + diff);
  680. }
  681. /***************************** GET_IPHASE ************************/
  682. int get_iphase(int startmarker,int startindex,int current_buf,dataptr dz)
  683. {
  684. float *bbuf;
  685. if(startmarker < startindex)
  686. bbuf = dz->sampbuf[!current_buf];
  687. else
  688. bbuf = dz->sampbuf[current_buf];
  689. if(bbuf[startmarker] > 0)
  690. return(1);
  691. return(-1);
  692. }
  693. /*************************** TOO_BIG *******************************/
  694. int too_big(int startindex,int endindex,dataptr dz)
  695. {
  696. int wavelen;
  697. if((wavelen = endindex - startindex)<0)
  698. wavelen += dz->buflen;
  699. wavelen *= dz->iparam[DISTORTM_FACTOR];
  700. if(wavelen >= dz->buflen)
  701. return(1);
  702. return(0);
  703. }
  704. /*************************** DO_MD *******************************/
  705. int do_md(int crosbuf,int current_buf,int i,int lastzero,int lastmarker,int *output_phase,
  706. int *no_of_half_cycles,int *endsample,int *startindex,int *startmarker,int *endindex,dataptr dz)
  707. {
  708. int exit_status;
  709. double thistime;
  710. int phaseswitch;
  711. if(*no_of_half_cycles == 0) {
  712. thistime = (double)(dz->total_samps_read - dz->ssampsread + lastmarker)/(double)dz->infile->srate;
  713. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  714. return(exit_status);
  715. }
  716. switch(dz->process) {
  717. case(DISTORT_MLT):
  718. phaseswitch = set_phaseswitch(*output_phase,lastzero,lastmarker,current_buf,dz);
  719. if(crosbuf==TRUE)
  720. return do_cros_multiply(i,phaseswitch,lastzero,output_phase,current_buf,endsample,dz);
  721. else
  722. return do_multiply(i,phaseswitch,lastzero,output_phase,current_buf,endsample,dz);
  723. case(DISTORT_DIV):
  724. return do_the_division(i,output_phase,startindex,startmarker,endindex,lastzero,lastmarker,
  725. no_of_half_cycles,current_buf,endsample,dz);
  726. }
  727. sprintf(errstr,"Unknown case in do_md()\n");
  728. return(PROGRAM_ERROR);
  729. }