distort.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 <modeno.h>
  29. #include <arrays.h>
  30. #include <distort.h>
  31. #include <cdpmain.h>
  32. #include <sfsys.h>
  33. #include <osbind.h>
  34. #if defined unix || defined __GNUC__
  35. #define round lround
  36. #endif
  37. //TW COMMENT : phase as used here, with vals >1 or <-1 (meaning above zero or below zero) in intrinsically 'int'
  38. // phase is also not 'float' elsewhere,
  39. // To avoid the construction "switch((int)phase)", I've made phase 'int' everywhere, casting in situ where ness.
  40. //static int distort_func(float *b,int tthis,int i,float phase,int lastzero,float cyclemax,dataptr dz);
  41. static int distort_func(float *b,int tthis,int i,int phase,int lastzero,float cyclemax,dataptr dz);
  42. //static int distort_func_crosbuf(int tthis,int i,float phase,int lastzero,float cyclemax,int oldbufcnt,dataptr dz);
  43. static int distort_func_crosbuf(int tthis,int i,int phase,int lastzero,float cyclemax,int oldbufcnt,dataptr dz);
  44. static int sinusoid(int tthis,int lastzero,float cyclemax,int endcycle,dataptr dz);
  45. static int end_sinusoid(int tthis,int lastzero,float cyclemax,int newbufcnt,int oldbufcnt,dataptr dz);
  46. static int genpulse(int step,float maxamp,float *bufptr,float *bufend);
  47. static int triangle(float *b,int lastzero,float cyclemax,int endcycle);
  48. static int end_triangle(int tthis,int lastzero,float cyclemax,int newbufcnt,int oldbufcnt,dataptr dz);
  49. //static int exaggerate(int lastzero,int thiszero,float phase,float *b,double powfac);
  50. static int exaggerate(int lastzero,int thiszero,int phase,float *b,double powfac);
  51. static int do_exagg
  52. (double lastmax,int lastpos,double thismax,int thispos,double powfac,float *b);
  53. //static int do_exagg2(float phase,int lastpos,int thispos,double powfac,float *b);
  54. static int do_exagg2(int phase,int lastpos,int thispos,double powfac,float *b);
  55. //static int end_exagg(int tthis,int lastzero,int newbufcnt,int oldbufcnt,double powfac,float phase,dataptr dz);
  56. static int end_exagg(int tthis,int lastzero,int newbufcnt,int oldbufcnt,double powfac,int phase,dataptr dz);
  57. static int get_powfac(int lastzero,int thiszero,dataptr dz);
  58. /************************* DO_DISTORT ***********************************
  59. *
  60. * Works on SINGLE HALF wavecycles.
  61. *
  62. * This looks for single half-wavecycles, and does not change there
  63. * number (or their phase) in the output. It therefore resets the
  64. * input & output phase on each entry.
  65. */
  66. int do_distort(int tthis,int is_last,int *lastzero,float *cyclemax,dataptr dz)
  67. {
  68. int exit_status;
  69. register int i = *lastzero;
  70. int oldbufcnt, lastmarker;
  71. int phase = 1;
  72. float *b = dz->sampbuf[tthis];
  73. int samples = dz->buflen;
  74. if(is_last)
  75. samples = dz->ssampsread;
  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. while(i < samples) {
  83. switch(phase) {
  84. case(1):
  85. if(b[i] > 0) {
  86. if(b[i]>*cyclemax)
  87. *cyclemax=b[i];
  88. i++;
  89. } else {
  90. if((exit_status = distort_func(b,tthis,i,phase,*lastzero,*cyclemax,dz))<0)
  91. return(exit_status);
  92. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  93. if(b[i]<0)
  94. phase=-1;
  95. }
  96. break;
  97. case(-1):
  98. if(b[i] < 0) {
  99. if(b[i]<*cyclemax)
  100. *cyclemax=b[i];
  101. i++;
  102. } else {
  103. if((exit_status = distort_func(b,tthis,i,phase,*lastzero,*cyclemax,dz))<0)
  104. return(exit_status);
  105. i = lastmarker = reset(i,samples,b,lastzero,cyclemax);
  106. if(b[i]>0)
  107. phase=1;
  108. }
  109. break;
  110. }
  111. }
  112. if(!is_last) {
  113. oldbufcnt = dz->buflen - *lastzero;
  114. b = dz->sampbuf[!tthis];
  115. samples = dz->ssampsread;
  116. i = 0;
  117. switch(phase) {
  118. case(1):
  119. while(i < samples) {
  120. if(b[i]>0) {
  121. if(b[i]>*cyclemax)
  122. *cyclemax = b[i];
  123. i++;
  124. } else
  125. break;
  126. }
  127. break;
  128. case(-1):
  129. while(i < samples) {
  130. if(b[i]<0) {
  131. if(b[i]<*cyclemax)
  132. *cyclemax = b[i];
  133. i++;
  134. } else
  135. break;
  136. }
  137. break;
  138. }
  139. if((exit_status = distort_func_crosbuf(tthis,i,phase,*lastzero,*cyclemax,oldbufcnt,dz))<0)
  140. return(exit_status);
  141. i = lastmarker = reset(i,samples,dz->sampbuf[!tthis],lastzero,cyclemax);
  142. }
  143. return(FINISHED);
  144. }
  145. /*************************** DISTORT_FUNC ******************************/
  146. int distort_func(float *b,int tthis,int i,int phase,int lastzero,float cyclemax,dataptr dz)
  147. {
  148. int exit_status;
  149. register int j;
  150. int halfcycle, step;
  151. float clipmax;
  152. switch(dz->mode) {
  153. case(DISTORT_SQUARE_FIXED):
  154. clipmax = FCLIPMAX * (float)phase;
  155. for(j=lastzero;j<i;j++)
  156. b[j]=clipmax; /* 4 */
  157. break;
  158. case(DISTORT_SQUARE):
  159. for(j=lastzero;j<i;j++)
  160. b[j]=cyclemax;
  161. break;
  162. case(DISTORT_TRIANGLE_FIXED):
  163. clipmax = FCLIPMAX * (float)phase;
  164. if(i - lastzero > 3) {
  165. if((exit_status = triangle(b,lastzero,clipmax,i))<0)
  166. return(exit_status);
  167. }
  168. break;
  169. case(DISTORT_TRIANGLE):
  170. if(i - lastzero > 3) {
  171. if((exit_status = triangle(b,lastzero,cyclemax,i))<0)
  172. return(exit_status);
  173. }
  174. break;
  175. case(DISTORT_INVERT_HALFCYCLE):
  176. for(j=lastzero;j<i;j++)
  177. b[j]=cyclemax - b[j];
  178. break;
  179. case(DISTORT_CLICK):
  180. halfcycle = i - lastzero;
  181. step = min((int)dz->iparam[DISTORT_PULSE],halfcycle);
  182. if((exit_status = genpulse(step,cyclemax,b+lastzero,b+i))<0)
  183. return(exit_status);
  184. break;
  185. case(DISTORT_SINE):
  186. if(i - lastzero > 5) {
  187. if((exit_status = sinusoid(tthis,lastzero,cyclemax,i,dz))<0)
  188. return(exit_status);
  189. }
  190. break;
  191. case(DISTORT_EXAGG):
  192. if(dz->brksize[DISTORT_POWFAC] && (exit_status = get_powfac(lastzero,i,dz))<0)
  193. return(exit_status);
  194. if((exit_status = exaggerate(lastzero,i,phase,dz->sampbuf[tthis],dz->param[DISTORT_POWFAC]))<0)
  195. return(exit_status);
  196. break;
  197. default:
  198. sprintf(errstr,"Unknown case in distort_func()\n");
  199. return(PROGRAM_ERROR);
  200. }
  201. return(FINISHED);
  202. }
  203. /************************ DISTORT_FUNC_CROSBUF **********************/
  204. int distort_func_crosbuf(int tthis,int i,int phase,int lastzero,float cyclemax,int oldbufcnt,dataptr dz)
  205. {
  206. int exit_status;
  207. register int j;
  208. int end_bit, halfcycle, step;
  209. float *buf = dz->sampbuf[tthis];
  210. /*int*/float clipmax;
  211. switch(dz->mode) {
  212. case(DISTORT_SQUARE_FIXED):
  213. clipmax = (float)(FCLIPMAX * (float)phase);
  214. for(j=lastzero;j<dz->buflen; j++)
  215. buf[j] = clipmax;
  216. buf = dz->sampbuf[!tthis];
  217. for(j=0;j<i;j++)
  218. buf[j] = clipmax;
  219. break;
  220. case(DISTORT_SQUARE):
  221. for(j=lastzero;j<dz->buflen; j++)
  222. buf[j] = cyclemax;
  223. buf = dz->sampbuf[!tthis];
  224. for(j=0;j<i;j++)
  225. buf[j] = cyclemax;
  226. break;
  227. case(DISTORT_TRIANGLE_FIXED):
  228. clipmax = (float)(FCLIPMAX * (float)phase);
  229. if(oldbufcnt + i > 3) {
  230. if((exit_status = end_triangle(tthis,lastzero,clipmax,i,oldbufcnt,dz))<0)
  231. return(exit_status);
  232. }
  233. break;
  234. case(DISTORT_TRIANGLE):
  235. if(oldbufcnt + i > 3) {
  236. if((exit_status = end_triangle(tthis,lastzero,cyclemax,i,oldbufcnt,dz))<0)
  237. return(exit_status);
  238. }
  239. break;
  240. case(DISTORT_INVERT_HALFCYCLE):
  241. for(j=lastzero;j<dz->buflen; j++)
  242. buf[j] = cyclemax - buf[j];
  243. buf = dz->sampbuf[!tthis];
  244. for(j=0;j<i;j++)
  245. buf[j] = cyclemax - buf[j];
  246. break;
  247. case(DISTORT_CLICK):
  248. end_bit = dz->buflen - lastzero;
  249. halfcycle = i + end_bit;
  250. step = min((int)dz->iparam[DISTORT_PULSE],halfcycle);
  251. if(step > end_bit) {
  252. if((exit_status = genpulse(end_bit,cyclemax,buf+lastzero,buf+dz->buflen))<0)
  253. return(exit_status);
  254. buf = dz->sampbuf[!tthis];
  255. if((exit_status = genpulse(step - end_bit,cyclemax,buf,buf+i))<0)
  256. return(exit_status);
  257. } else {
  258. if((exit_status = genpulse(end_bit,cyclemax,buf + lastzero,buf + dz->buflen))<0)
  259. return(exit_status);
  260. buf = dz->sampbuf[!tthis];
  261. if((exit_status = genpulse(0,cyclemax,buf,buf+i))<0)
  262. return(exit_status);
  263. }
  264. break;
  265. case(DISTORT_SINE):
  266. if(oldbufcnt + i > 5) {
  267. if((exit_status = end_sinusoid(tthis,lastzero,cyclemax,i,oldbufcnt,dz))<0)
  268. return(exit_status);
  269. }
  270. break;
  271. case(DISTORT_EXAGG):
  272. if(dz->brksize[DISTORT_POWFAC] && (exit_status = get_powfac(lastzero,dz->buflen+i,dz))<0)
  273. return(exit_status);
  274. if((exit_status = end_exagg(tthis,lastzero,i,oldbufcnt,dz->param[DISTORT_POWFAC],phase,dz))<0)
  275. return(exit_status);
  276. break;
  277. default:
  278. sprintf(errstr,"Unknown case in distort_func_crosbuf()\n");
  279. return(PROGRAM_ERROR);
  280. }
  281. return(FINISHED);
  282. }
  283. /***************************** SINUSOID ******************************
  284. *
  285. * Create sinusoidal half-wave, over half-cycle.
  286. */
  287. int sinusoid(int tthis,int lastzero,float cyclemax,int endcycle,dataptr dz)
  288. {
  289. float *bb = dz->sampbuf[tthis];
  290. int i, j, start = lastzero; /* 1 */
  291. int half_cyclecnt = endcycle - lastzero; /* 2 */
  292. double index_factor = (double)SINETABLEN/(double)half_cyclecnt;
  293. for(j=start, i=0; j<endcycle; j++, i++)
  294. bb[j] = /*round*/(float) (dz->parray[DISTORT_SIN][round(i * index_factor)] * cyclemax);
  295. return(FINISHED);
  296. }
  297. /*********************** END_SINUSOID ***************************/
  298. int end_sinusoid(int tthis,int lastzero,float cyclemax,int newbufcnt,int oldbufcnt,dataptr dz)
  299. {
  300. float *bb = dz->sampbuf[tthis];
  301. int i, j, start = lastzero;
  302. int half_cyclecnt = oldbufcnt + newbufcnt;
  303. double index_factor = (double)SINETABLEN/(double)half_cyclecnt;
  304. for(j=start, i=0; j<dz->buflen; j++,i++)
  305. bb[j] = /*round*/(float)(dz->parray[DISTORT_SIN][round(i * index_factor)] * cyclemax);
  306. bb = dz->sampbuf[!tthis];
  307. for(j=0; j<newbufcnt; j++, i++)
  308. bb[j] = /*round*/(float)(dz->parray[DISTORT_SIN][round(i * index_factor)] * cyclemax);
  309. return(FINISHED);
  310. }
  311. /***************************** GENPULSE ****************************/
  312. int genpulse(int step,float maxamp,float *bufptr,float *bufend)
  313. {
  314. int n, m, z, sum = 0;
  315. float ampval = maxamp;
  316. while(sum < step) {
  317. n = round(drand48() * MAXWIDTH); /* TRUNCATE */
  318. n++;
  319. sum += n;
  320. z = min(n,step-sum);
  321. for(m=0;m<z;m++)
  322. *bufptr++ = ampval;
  323. ampval = -ampval;
  324. }
  325. while(bufptr<bufend)
  326. *bufptr++ = 0;
  327. return(FINISHED);
  328. }
  329. /***************************** TRIANGLE ******************************
  330. *
  331. * Create triangular half-wave, over half-cycle.
  332. *
  333. * (0) Samples values calculated as reals, then approxd to integers.
  334. * (1) establish start of loop after initial sample (set to zero).
  335. * (2) 'half_cyclecnt' is number of samples in the half_cycle.
  336. * 'halfcnt' is count to mid-point of triangle form.
  337. * (3) establish midpoint of half_cycle.
  338. * (4) Set initial sample to zero.
  339. * (5) Calculate real increment(decrement) to create triangular form.
  340. * (6) Create triangle from start to middle.
  341. * (7) For even number of samples in half-cycle, add extra peak sample.
  342. * (8) Create middle sample of triangle.
  343. * (9) Create triangle from middle to end.
  344. */
  345. int triangle(float *b,int lastzero,float cyclemax,int endcycle)
  346. {
  347. double realval = 0.0, step; /* 0 */
  348. int start = lastzero+1; /* 1 */
  349. int half_cyclecnt = endcycle - lastzero; /* 2 */
  350. int halfcnt = (half_cyclecnt+1)/2;
  351. int middle = lastzero + halfcnt; /* 3 */
  352. int j;
  353. b[lastzero] = 0; /* 4 */
  354. step = (double)cyclemax/(double)halfcnt; /* 5 */
  355. for(j=start; j<middle; j++) {
  356. realval += step; /* 6 */
  357. b[j] = (float)realval;
  358. }
  359. if(EVEN(half_cyclecnt))
  360. realval += step; /* 7 */
  361. b[middle] = (float)/*round*/(realval); /* 8 */
  362. for(j=middle+1; j<endcycle; j++) {
  363. realval -= step; /* 9 */
  364. b[j] = (float)realval;
  365. }
  366. return(FINISHED);
  367. }
  368. /*********************** END_TRIANGLE ***************************
  369. *
  370. * (0) Sample values calculated as reals, then approximated to integers.
  371. * (1) half_cycle is now that counted in original buffer, plus the part
  372. * counted into the next buffer (newbufcnt).
  373. * (1a) The count as far as middle of half-cycle.
  374. * (1b) middle = actual sample index of midpoint of half-cycle.
  375. * (1c) Set inital sample of half-cycle to a zero.
  376. * (1d) calculate the increment (=decrement) to create triangle wave.
  377. * (2) If midpoint of triangle is in ORIGINAL buffer...
  378. * (3) ..create values from start to middle,
  379. * (A) ..if half_cycle has even number of samples, get extra peak value.
  380. * (5) ..put in middle value.
  381. * (6) ..create values from middle to end of buf.
  382. * (7) ..switch to other buf.
  383. * (8) ..create values from start of new buf, to end of triangle.
  384. * (9) Else, midpoint of triangle is in OTHER buffer...
  385. * (10) ..create values from start to end of buffer.
  386. * (11) ..decrement location of middle by size of original buffer.
  387. * (12) ..switch to other buf.
  388. * (13) ..create values from start of new buf, to middle of triangle.
  389. * (14) ..if half_cycle has even number of samples, get extra peak value.
  390. * (15) ..put in middle value.
  391. * (16) ..create values from middle to end of triangle.
  392. */
  393. int end_triangle(int tthis,int lastzero,float cyclemax,int newbufcnt,int oldbufcnt,dataptr dz)
  394. {
  395. double realval = 0.0, step; /* 0 */
  396. int start = lastzero+1;
  397. int half_cyclecnt = oldbufcnt + newbufcnt;/* 1 */
  398. int halfcnt = (half_cyclecnt+1)/2; /* 1a */
  399. int middle = lastzero + halfcnt; /* 1b */
  400. int j;
  401. float *b = dz->sampbuf[tthis];
  402. b[lastzero] = 0.0; /* 1c */
  403. step = (double)cyclemax/(double)(halfcnt);
  404. if(middle < dz->buflen) { /* 2 */
  405. for(j=start; j<middle; j++) {
  406. realval += step; /* 3 */
  407. b[j] = (float)realval;
  408. }
  409. if(EVEN(half_cyclecnt)) /* A */
  410. realval += step;
  411. b[middle] = (float)realval; /* 5 */
  412. for(j=middle+1; j<dz->buflen; j++) {
  413. realval -= step; /* 6 */
  414. b[j] = (float)realval;
  415. }
  416. b = dz->sampbuf[!tthis]; /* 7 */
  417. for(j=0; j<newbufcnt; j++) {
  418. realval -= step; /* 8 */
  419. b[j] = (float)realval;
  420. }
  421. } else { /* 9 */
  422. for(j=start; j<dz->buflen; j++) {
  423. realval += step; /* 10 */
  424. b[j] = (float)realval;
  425. }
  426. middle -= dz->buflen; /* 11 */
  427. b = dz->sampbuf[!tthis]; /* 12 */
  428. for(j=0; j<middle; j++) {
  429. realval += step; /* 13 */
  430. b[j] = (float)realval;
  431. }
  432. if(EVEN(half_cyclecnt)) /* 14 */
  433. realval += step;
  434. b[middle] = (float)realval; /* 15 */
  435. for(j=middle+1; j<newbufcnt; j++) {
  436. realval -= step; /* 16 */
  437. b[j] = (float)realval;
  438. }
  439. }
  440. return(FINISHED);
  441. }
  442. /************************** RESET ******************************
  443. *
  444. * (9) Reset lastzero, marking state of new (half)wavecycle.
  445. * (10) Skip any zero samples : drop out if end of samples in buffer reached.
  446. * (11) Reset maximum sample in halfcycle to mimimum (zero).
  447. * (12) Return first NON-ZERO sample.
  448. */
  449. int reset(int i,int samples,float *bbuf,int *lastzero,float *cyclemax)
  450. {
  451. *lastzero = i; /* 9 */
  452. while(bbuf[i]==0.0 && i < samples) /* 10 */
  453. i++;
  454. *cyclemax=0.0; /* 11 */
  455. return(i); /* 12 */
  456. }
  457. /****************************** EXAGGERATE *******************************/
  458. int exaggerate(int lastzero,int thiszero,int phase,float *b,double powfac)
  459. {
  460. int exit_status;
  461. int n = lastzero + 1;
  462. double lastmax, thismax; // thismin;
  463. int lastpos, thispos; //, minpos;
  464. int OK = 1;
  465. switch(phase) {
  466. case(1):
  467. while(b[n] >= b[n-1])
  468. n++;
  469. lastmax = (double)b[n-1];
  470. lastpos = n-1;
  471. if(powfac < 0.0)
  472. exit_status = do_exagg2(phase,lastzero+1,lastpos,powfac,b);
  473. else
  474. exit_status = do_exagg2(phase,lastzero,lastpos,powfac,b);
  475. if(exit_status < 0)
  476. return(exit_status);
  477. while(n < thiszero) {
  478. while(b[n] < b[n-1]) {
  479. if(++n >= thiszero) {
  480. OK = 0;
  481. break;
  482. }
  483. }
  484. if(OK) {
  485. //thismin = (double)b[n-1];
  486. //minpos = n-1;
  487. while(b[n] >= b[n-1])
  488. n++;
  489. thismax = (double)b[n-1];
  490. thispos = n-1;
  491. if((exit_status = do_exagg(lastmax,lastpos,thismax,thispos,powfac,b))<0)
  492. return(exit_status);
  493. lastmax = thismax;
  494. lastpos = thispos;
  495. } else
  496. break;
  497. }
  498. if(powfac < 0.0)
  499. exit_status = do_exagg2(phase,lastpos+1,n-1,powfac,b);
  500. else
  501. exit_status = do_exagg2(phase,lastpos,n-1,powfac,b);
  502. if(exit_status < 0)
  503. return(exit_status);
  504. break;
  505. case(-1):
  506. while(b[n] <= b[n-1])
  507. n++;
  508. lastmax = (double)b[n-1];
  509. lastpos = n-1;
  510. if(powfac < 0.0)
  511. exit_status = do_exagg2(phase,lastzero+1,lastpos,powfac,b);
  512. else
  513. exit_status = do_exagg2(phase,lastzero,lastpos,powfac,b);
  514. if(exit_status < 0)
  515. return(exit_status);
  516. while(n < thiszero) {
  517. while(b[n] > b[n-1]) {
  518. if(++n >= thiszero) {
  519. OK = 0;
  520. break;
  521. }
  522. }
  523. if(OK) {
  524. //thismin = (double)b[n-1];
  525. //minpos = n-1;
  526. while(b[n] <= b[n-1])
  527. n++;
  528. thismax = (double)b[n-1];
  529. thispos = n-1;
  530. if((exit_status = do_exagg(lastmax,lastpos,thismax,thispos,powfac,b))<0)
  531. return(exit_status);
  532. lastmax = thismax;
  533. lastpos = thispos;
  534. } else
  535. break;
  536. }
  537. if(powfac < 0.0)
  538. exit_status = do_exagg2(phase,lastpos+1,n-1,powfac,b);
  539. else
  540. exit_status = do_exagg2(phase,lastpos,n-1,powfac,b);
  541. if(exit_status < 0)
  542. return(exit_status);
  543. break;
  544. }
  545. return(FINISHED);
  546. }
  547. /****************************** DO_EXAGG ******************************/
  548. int do_exagg
  549. (double lastmax,int lastpos,double thismax,int thispos,double powfac,float *b)
  550. {
  551. double maxdif = thismax - lastmax;
  552. double posdif = thispos - lastpos;
  553. double local_max, rati_o;
  554. int n, m;
  555. for(n = lastpos+1, m = 1; n<thispos; n++, m++) {
  556. local_max = lastmax + (maxdif * (double)m/posdif);
  557. rati_o = min(((double)b[n]/local_max),1.0);
  558. rati_o = pow(rati_o,powfac);
  559. b[n] = (float)/*round*/((double)b[n] * rati_o);
  560. }
  561. return(FINISHED);
  562. }
  563. /*************************** DO_EXAGG2 *******************************/
  564. int do_exagg2(int phase,int lastpos,int thispos,double powfac, float *b)
  565. {
  566. int n;
  567. double val, valdiff = (double)max(fabs(b[lastpos]),fabs(b[thispos]));
  568. for(n = lastpos; n<=thispos; n++) {
  569. val = (double)b[n]/valdiff;
  570. val *= (float)phase;
  571. val = pow(val,powfac);
  572. val *= valdiff * (float)phase;
  573. b[n] = (float)/*round*/(val);
  574. }
  575. return(FINISHED);
  576. }
  577. /************************ END_EXAGG *******************************/
  578. int end_exagg(int tthis,int lastzero,int newbufcnt,int oldbufcnt,double powfac,int phase,dataptr dz)
  579. {
  580. float *bold = dz->sampbuf[tthis] + lastzero;
  581. float *bnew = dz->sampbuf[!tthis];
  582. float *btemp = dz->sampbuf[2];
  583. memmove((char *)btemp,(char *)bold,oldbufcnt * sizeof(float));
  584. btemp += oldbufcnt;
  585. memmove((char *)btemp,(char *)bnew,newbufcnt * sizeof(float));
  586. exaggerate(0,oldbufcnt+newbufcnt,phase,dz->sampbuf[2],powfac);
  587. btemp = dz->sampbuf[2]; /* copy the transformed material back to where it came from */
  588. memmove((char *)bold,(char *)btemp,oldbufcnt * sizeof(float));
  589. btemp += oldbufcnt;
  590. memmove((char *)bnew,(char *)btemp,newbufcnt * sizeof(float));
  591. return(FINISHED);
  592. }
  593. /************************ GET_POWFAC *******************************/
  594. int get_powfac(int lastzero,int thiszero,dataptr dz)
  595. {
  596. double thistime;
  597. thistime = (double)(dz->total_samps_read - dz->ssampsread - dz->buflen); /* START OF BUF IN USE */
  598. thistime += ((double)(thiszero + lastzero)/2.0); /* MEAN OFFSET OF CYCLE IN CURRENT BUF */
  599. thistime /= (double)dz->infile->srate;
  600. return read_value_from_brktable(thistime,DISTORT_POWFAC,dz);
  601. }
  602. //TW UPDATE : NEW FUNCTION
  603. /************************ DISTORT_OVERLOAD *******************************/
  604. // NOV 2002: NEED TO FIX buffers to float, MAXSAMP to F_MAXSAP, (short) to THINGY!!
  605. int distort_overload(dataptr dz)
  606. {
  607. int exit_status, n;
  608. // short *buf;
  609. float *buf;
  610. double *tab = NULL;
  611. double maxgate, mingate, inc_ratio, time = 0.0, timestep = 0.01, tabposd = 0.0, normaliser, k;
  612. int stepper, tabpos = 0, in_maxgate = 0, in_mingate = 0;
  613. buf = dz->sampbuf[0];
  614. if(dz->mode==OVER_SINE)
  615. tab = dz->parray[0];
  616. maxgate = dz->param[DISTORTER_MULT];
  617. mingate = -maxgate;
  618. inc_ratio = (double)DISTORTER_TABLEN/(double)dz->infile->srate;
  619. stepper = (int)round((double)dz->infile->srate * timestep);
  620. normaliser = ((double)F_MAXSAMP/(maxgate + 1.0)) /* TEST * 0.9 */;
  621. // while(dz->bytes_left > 0) {
  622. while(dz->samps_left > 0) {
  623. // if((exit_status = read_bytes((char *)dz->bigbuf,dz))<0)
  624. if((exit_status = read_samps(dz->bigbuf,dz))<0)
  625. return(exit_status);
  626. switch(dz->mode) {
  627. case(OVER_NOISE):
  628. for(n=0;n<dz->ssampsread;n++) {
  629. if(!(n%stepper)) {
  630. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  631. return(exit_status);
  632. time += timestep;
  633. }
  634. if((double)buf[n] >= maxgate) {
  635. k = 1.0 - (drand48() * dz->param[DISTORTER_DEPTH]);
  636. // buf[n] = (short)round(k * maxgate);
  637. buf[n] = (float)(k * maxgate);
  638. } else if((double)buf[n] <= mingate) {
  639. k = 1.0 - (drand48() * dz->param[DISTORTER_DEPTH]);
  640. // buf[n] = (short)round(k * mingate);
  641. buf[n] = (float)(k * mingate);
  642. }
  643. // buf[n] = (short)round(buf[n] * normaliser);
  644. buf[n] = (float)(buf[n] * normaliser);
  645. }
  646. break;
  647. case(OVER_SINE):
  648. for(n=0;n<dz->ssampsread;n++) {
  649. if(!(n%stepper)) {
  650. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  651. return(exit_status);
  652. time += timestep;
  653. }
  654. if((double)buf[n] >= maxgate) {
  655. if(!in_maxgate) {
  656. tabpos = 0;
  657. tabposd = 0.0;
  658. in_mingate = 0;
  659. in_maxgate = 1;
  660. }
  661. k = 1.0 + (tab[tabpos] * dz->param[DISTORTER_DEPTH]);
  662. // buf[n] = (short)round(k * maxgate);
  663. buf[n] = (float)(k * maxgate);
  664. } else if((double)buf[n] <= mingate) {
  665. if(!in_mingate) {
  666. tabpos = 0;
  667. tabposd = 0.0;
  668. in_maxgate = 0;
  669. in_mingate = 1;
  670. }
  671. k = 1.0 + (tab[tabpos] * dz->param[DISTORTER_DEPTH]);
  672. // buf[n] = (short)round(k * mingate);
  673. buf[n] = (float)(k * mingate);
  674. } else {
  675. in_maxgate = 0;
  676. in_mingate = 0;
  677. }
  678. // buf[n] = (short)round(buf[n] * normaliser);
  679. buf[n] = (float)(buf[n] * normaliser);
  680. tabposd = fmod(tabposd + (dz->param[DISTORTER_FRQ] * inc_ratio),(double)DISTORTER_TABLEN);
  681. tabpos = round(tabposd) % DISTORTER_TABLEN;
  682. }
  683. break;
  684. }
  685. if(dz->ssampsread > 0) {
  686. if((exit_status = write_samps(dz->bigbuf,dz->ssampsread,dz))<0)
  687. return(exit_status);
  688. }
  689. }
  690. return(FINISHED);
  691. }