disprepro.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /*RWD*/
  25. #include <memory.h>
  26. #include <structures.h>
  27. #include <tkglobals.h>
  28. #include <cdpmain.h>
  29. #include <globcon.h>
  30. #include <distort.h>
  31. //TW UPDATE
  32. #include <distort1.h>
  33. #include <modeno.h>
  34. #include <arrays.h>
  35. #include <sfsys.h>
  36. static int gen_distort_sin_table(dataptr dz);
  37. static int convert_frq_to_cyclelen(int paramno,dataptr dz);
  38. static int reduce_brkvals_by_one(int paramno,dataptr dz);
  39. static int frequencies_cross(dataptr dz);
  40. //TW UPDATE
  41. static int make_distorter_tab(dataptr dz);
  42. /****************************** DISTORT_PREPROCESS *********************************/
  43. int distort_preprocess(dataptr dz)
  44. {
  45. int exit_status;
  46. dz->iparam[DISTORT_PULSE] = (int)round(PULSEWIDTH * (double)dz->infile->srate);
  47. if(dz->mode==DISTORT_SINE && (exit_status = gen_distort_sin_table(dz))<0)
  48. return(exit_status);
  49. return(FINISHED);
  50. }
  51. /****************************** GEN_DISTORT_SIN_TABLE *********************************/
  52. int gen_distort_sin_table(dataptr dz)
  53. {
  54. int n;
  55. if((dz->parray[DISTORT_SIN] = (double *)malloc(SINETABLEN * sizeof(double)))==NULL) {
  56. sprintf(errstr,"INSUFFICIENT MEMORY for sinewave table.\n");
  57. return(MEMORY_ERROR);
  58. }
  59. for(n=0;n<SINETABLEN;n++)
  60. dz->parray[DISTORT_SIN][n] = sin(PI * (double)n/(double)SINETABLEN);
  61. return(FINISHED);
  62. }
  63. /****************************** DISTORTENV_PREPROCESS *********************************/
  64. int distortenv_preprocess(dataptr dz)
  65. {
  66. int exit_status;
  67. if((exit_status = reset_distorte_modes(dz))<0)
  68. return(exit_status);
  69. dz->param[ONE_LESS_TROF] = 1.0 - dz->param[DISTORTE_TROF];
  70. return(FINISHED);
  71. }
  72. /**************************** RESET_DISTORTE_MODES ************************/
  73. int reset_distorte_modes(dataptr dz)
  74. {
  75. if(dz->mode == DISTORTE_USERDEF)
  76. return(FINISHED);
  77. if(dz->mode == DISTORTE_TROFFED) {
  78. if(!dz->vflag[0]) /* Only 1 flag exists in this mode, it is for EXPON, not TROF as in other cases!! */
  79. dz->mode = DISTORTE_LINTROF;
  80. } else {
  81. if(!dz->vflag[DISTORTE_IS_EXPON]) { /* flag 1 */
  82. switch(dz->mode) {
  83. case(DISTORTE_RISING): dz->mode = DISTORTE_LINRISE; break;
  84. case(DISTORTE_FALLING): dz->mode = DISTORTE_LINFALL; break;
  85. default:
  86. sprintf(errstr,"Bad case in reset_distorte_modes()\n");
  87. return(PROGRAM_ERROR);
  88. }
  89. }
  90. if(dz->vflag[DISTORTE_IS_TROF]) { /* flag 0 */
  91. switch(dz->mode) {
  92. case(DISTORTE_RISING): dz->mode = DISTORTE_RISING_TR; break;
  93. case(DISTORTE_LINRISE): dz->mode = DISTORTE_LINRISE_TR; break;
  94. case(DISTORTE_FALLING): dz->mode = DISTORTE_FALLING_TR; break;
  95. case(DISTORTE_LINFALL): dz->mode = DISTORTE_LINFALL_TR; break;
  96. }
  97. }
  98. }
  99. return(FINISHED);
  100. }
  101. /****************************** DISTORTMLT_PREPROCESS *********************************/
  102. int distortmlt_preprocess(dataptr dz)
  103. {
  104. memset((char *)dz->sampbuf[2],0,dz->buflen * sizeof(float));
  105. memset((char *)dz->sampbuf[3],0,dz->buflen * sizeof(float));
  106. memset((char *)dz->sampbuf[4],0,dz->buflen * sizeof(float));
  107. memset((char *)dz->sampbuf[5],0,dz->buflen * sizeof(float));
  108. return(FINISHED);
  109. }
  110. /****************************** DISTORTDIV_PREPROCESS *********************************/
  111. int distortdiv_preprocess(dataptr dz)
  112. {
  113. memset((char *)dz->sampbuf[2],0,dz->buflen * sizeof(float));
  114. memset((char *)dz->sampbuf[3],0,dz->buflen * sizeof(float));
  115. return(FINISHED);
  116. }
  117. /****************************** DISTORTER_PREPROCESS *********************************/
  118. int distorter_preprocess(int param1,int param2,int param3,dataptr dz)
  119. {
  120. int exit_status;
  121. int brklen;
  122. double brkmax;
  123. if(dz->brksize[param1]) {
  124. if((exit_status = get_maxvalue_in_brktable(&brkmax,DISTORTA_CYCLECNT,dz))<0)
  125. return(exit_status);
  126. brklen = round(brkmax);
  127. } else
  128. brklen = dz->iparam[param1];
  129. if((dz->lparray[param2] = (int *)malloc((brklen+1) * sizeof(int)))==NULL
  130. || (dz->lparray[param3] = (int *)malloc(brklen * sizeof(int)))==NULL) {
  131. sprintf(errstr,"INSUFFICIENT MEMORY for cycle information arrays.\n");
  132. return(MEMORY_ERROR);
  133. }
  134. /*RWD 4:2002 we shadow this with lfarray */
  135. if((dz->lfarray[param2] = (float *)malloc((brklen+1) * sizeof(float)))==NULL
  136. || (dz->lfarray[param3] = (float *)malloc(brklen * sizeof(float)))==NULL) {
  137. sprintf(errstr,"INSUFFICIENT MEMORY for cycle information arrays.\n");
  138. return(MEMORY_ERROR);
  139. }
  140. return(FINISHED);
  141. }
  142. /****************************** DISTORTSHUF_PREPROCESS *********************************/
  143. int distortshuf_preprocess(dataptr dz)
  144. {
  145. int exit_status;
  146. int maxval;
  147. double brkmax;
  148. if(dz->brksize[DISTORTS_CYCLECNT]) {
  149. if((exit_status = get_maxvalue_in_brktable(&brkmax,DISTORTS_CYCLECNT,dz))<0)
  150. return(exit_status);
  151. maxval = round(brkmax);
  152. } else
  153. maxval = dz->iparam[DISTORTS_CYCLECNT];
  154. if((dz->lparray[DISTORTS_STARTCYC] = (int *)malloc((maxval+1)*dz->iparam[DISTORTS_DMNCNT]*sizeof(int)))==NULL) {
  155. sprintf(errstr,"INSUFFICIENT MEMORY for cycle information array.\n");
  156. return(MEMORY_ERROR);
  157. }
  158. return(FINISHED);
  159. }
  160. /****************************** DISTORTDEL_PREPROCESS *********************************/
  161. int distortdel_preprocess(dataptr dz)
  162. {
  163. switch(dz->mode) {
  164. case(DELETE_IN_STRICT_ORDER):
  165. if(dz->brksize[DISTDEL_CYCLECNT])
  166. reduce_brkvals_by_one(DISTDEL_CYCLECNT,dz); /* becomes number of cycles to delete */
  167. else
  168. dz->iparam[DISTDEL_CYCLECNT]--;
  169. break;
  170. case(KEEP_STRONGEST):
  171. case(DELETE_WEAKEST):
  172. return distorter_preprocess(DISTDEL_CYCLECNT,DISTDEL_STARTCYC,DISTDEL_CYCLEVAL,dz);
  173. }
  174. return(FINISHED);
  175. }
  176. /****************************** DISTORTFLT_PREPROCESS *********************************/
  177. int distortflt_preprocess(dataptr dz)
  178. {
  179. int exit_status;
  180. switch(dz->mode) {
  181. case(DISTFLT_HIPASS):
  182. convert_frq_to_cyclelen(DISTFLT_LOFRQ_CYCLELEN,dz);
  183. break;
  184. case(DISTFLT_LOPASS):
  185. convert_frq_to_cyclelen(DISTFLT_HIFRQ_CYCLELEN,dz);
  186. break;
  187. case(DISTFLT_BANDPASS):
  188. if(dz->brksize[DISTFLT_LOFRQ_CYCLELEN] && dz->brksize[DISTFLT_HIFRQ_CYCLELEN]) {
  189. /* Force both brktables to begin at time zero */
  190. if((exit_status = force_value_at_zero_time(DISTFLT_LOFRQ_CYCLELEN,dz))<0)
  191. return(exit_status);
  192. if((exit_status = force_value_at_zero_time(DISTFLT_HIFRQ_CYCLELEN,dz))<0)
  193. return(exit_status);
  194. }
  195. /* Check brktables against each other: If hifrq < lofrq: reject */
  196. if(frequencies_cross(dz)) {
  197. sprintf(errstr,"Filter frqs cross: cannot proceed\n");
  198. return(DATA_ERROR);
  199. }
  200. convert_frq_to_cyclelen(DISTFLT_LOFRQ_CYCLELEN,dz);
  201. convert_frq_to_cyclelen(DISTFLT_HIFRQ_CYCLELEN,dz);
  202. break;
  203. default:
  204. sprintf(errstr,"Unknown mode for DISTORT_FLT in param_preprocess()\n");
  205. return(PROGRAM_ERROR);
  206. }
  207. return(FINISHED);
  208. }
  209. /***************************** CONVERT_FRQ_TO_CYCLELEN ******************************/
  210. int convert_frq_to_cyclelen(int paramno,dataptr dz)
  211. {
  212. double *p, *pend;
  213. if(dz->brksize[paramno]) {
  214. p = dz->brk[paramno] + 1;
  215. pend = p + (dz->brksize[paramno] * 2);
  216. while(p < pend) {
  217. *p = dz->infile->srate/(*p);
  218. p += 2;
  219. }
  220. dz->is_int[paramno] = TRUE; /* Force rounding in brktable reads */
  221. } else
  222. dz->iparam[paramno] = (int)round(dz->infile->srate/dz->param[paramno]);
  223. return(FINISHED);
  224. }
  225. /**************************** REDUCE_BRKVALS_BY_ONE ************************/
  226. int reduce_brkvals_by_one(int paramno,dataptr dz)
  227. {
  228. double *p = dz->brk[paramno];
  229. int n;
  230. if(dz->brksize[paramno] <= 0) {
  231. sprintf(errstr,"Brktable is empty.\n");
  232. return(DATA_ERROR);
  233. }
  234. p++;
  235. for(n=0;n<dz->brksize[paramno];n++) {
  236. *p -= 1.0;
  237. p += 2;
  238. }
  239. return(FINISHED);
  240. }
  241. /**************************** FREQUENCIES_CROSS ************************/
  242. int frequencies_cross(dataptr dz)
  243. {
  244. int lo_finished = FALSE;
  245. int hi_finished = FALSE;
  246. double *flo, *fhi, *tlo, *thi, *loend, *hiend;
  247. double lasttime,thistime,timediff,timeratio,lastval,thisval,valdiff,val;
  248. if(dz->brksize[DISTFLT_LOFRQ_CYCLELEN]) {
  249. flo = dz->brk[DISTFLT_LOFRQ_CYCLELEN] + 1;
  250. loend = dz->brk[DISTFLT_LOFRQ_CYCLELEN] + (dz->brksize[DISTFLT_LOFRQ_CYCLELEN] * 2);
  251. if(dz->brksize[DISTFLT_HIFRQ_CYCLELEN]) {
  252. fhi = dz->brk[DISTFLT_HIFRQ_CYCLELEN] + 1;
  253. hiend = dz->brk[DISTFLT_HIFRQ_CYCLELEN] + (dz->brksize[DISTFLT_HIFRQ_CYCLELEN] * 2);
  254. tlo = flo - 1;
  255. thi = fhi - 1;
  256. while(lo_finished==FALSE && hi_finished==FALSE) {
  257. if(*tlo < *thi) {
  258. lasttime = *(thi - 2);
  259. thistime = *thi;
  260. timediff = thistime - lasttime;
  261. timeratio = (*tlo - lasttime)/timediff;
  262. lastval = *(fhi - 2);
  263. thisval = *fhi;
  264. valdiff = thisval - lastval;
  265. val = lastval + (valdiff * timeratio);
  266. if(*flo >= val)
  267. return(TRUE);
  268. if((flo += 2) < loend)
  269. tlo += 2;
  270. else {
  271. flo -= 2;
  272. lo_finished = TRUE;
  273. }
  274. } else if (*tlo > *thi) {
  275. lasttime = *(tlo - 2);
  276. thistime = *tlo;
  277. timediff = thistime - lasttime;
  278. timeratio = (*thi - lasttime)/timediff;
  279. lastval = *(flo - 2);
  280. thisval = *flo;
  281. valdiff = thisval - lastval;
  282. val = lastval + (valdiff * timeratio);
  283. if(val >= *fhi)
  284. return(TRUE);
  285. if((fhi += 2) < hiend)
  286. thi += 2;
  287. else {
  288. fhi -= 2;
  289. hi_finished = TRUE;
  290. }
  291. } else /* (*tlo == *thi) */ {
  292. if(*flo >= *fhi)
  293. return(TRUE);
  294. if((flo += 2) < loend)
  295. tlo += 2;
  296. else {
  297. flo -= 2;
  298. lo_finished = TRUE;
  299. }
  300. if((fhi += 2) < hiend)
  301. thi += 2;
  302. else {
  303. fhi -= 2;
  304. hi_finished = TRUE;
  305. }
  306. }
  307. }
  308. } else {
  309. while(flo < loend) {
  310. if(*flo >= dz->param[DISTFLT_HIFRQ_CYCLELEN])
  311. return(TRUE);
  312. flo += 2;
  313. }
  314. }
  315. } else if(dz->brksize[DISTFLT_HIFRQ_CYCLELEN]) {
  316. fhi = dz->brk[DISTFLT_HIFRQ_CYCLELEN] + 1;
  317. hiend = dz->brk[DISTFLT_HIFRQ_CYCLELEN] + (dz->brksize[DISTFLT_HIFRQ_CYCLELEN] * 2);
  318. while(fhi < hiend) {
  319. if(*fhi <= dz->param[DISTFLT_LOFRQ_CYCLELEN])
  320. return(TRUE);
  321. fhi += 2;
  322. }
  323. }
  324. return(FALSE);
  325. }
  326. //TW UPDATE : NEW FUNCTIONS
  327. /**************************** OVERLOAD_PREPROCESS ************************/
  328. int overload_preprocess(dataptr dz)
  329. {
  330. int exit_status;
  331. if((dz->mode==OVER_SINE) && ((exit_status = make_distorter_tab(dz))<0))
  332. return(exit_status);
  333. //TW NEW CODE UPDATE TO FLOATS
  334. // dz->param[DISTORTER_MULT] *= (double)MAXSAMP;
  335. return(FINISHED);
  336. }
  337. /******************************** MAKE_DISTORTER_TAB ********************************
  338. *
  339. * cosin table shifted to lie between 0.0 and -1.0
  340. */
  341. int make_distorter_tab(dataptr dz)
  342. {
  343. int n;
  344. double convertor = (double)TWOPI/(double)DISTORTER_TABLEN;
  345. double *tab;
  346. if((dz->parray[0] = (double *)malloc(DISTORTER_TABLEN * sizeof(double)))==NULL) {
  347. sprintf(errstr,"Out of memory for cosine table.");
  348. return(MEMORY_ERROR);
  349. }
  350. tab = dz->parray[0];
  351. for(n = 0; n <DISTORTER_TABLEN; n++)
  352. tab[n] = (cos((double)n * convertor) - 1.0)/2.0;
  353. return(FINISHED);
  354. }