distortion.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 <globcon.h>
  29. #include <processno.h>
  30. #include <modeno.h>
  31. #include <arrays.h>
  32. #include <distort.h>
  33. #include <cdpmain.h>
  34. #include <sfsys.h>
  35. #include <osbind.h>
  36. static int cyccnt(int *current_pos_in_buf,int *current_buf,int initial_phase,dataptr dz);
  37. /*************************** GET_INITIAL_PHASE **********************/
  38. int get_initial_phase(int *initial_phase,dataptr dz)
  39. {
  40. float *b = dz->sampbuf[0];
  41. int current_pos_in_buf = 0;
  42. while(b[current_pos_in_buf]==0 && current_pos_in_buf < dz->ssampsread)
  43. current_pos_in_buf++;
  44. if(current_pos_in_buf >= dz->ssampsread) {
  45. sprintf(errstr,"1st buffer is ALL ZEROES: Cannot proceed.\n");
  46. return(GOAL_FAILED);
  47. }
  48. if(b[current_pos_in_buf] > 0)
  49. *initial_phase = 1;
  50. else
  51. *initial_phase = -1;
  52. return(FINISHED);
  53. }
  54. /*************************** PROCESS_WITH_SWAPPED_BUFS_ON_SINGLE_HALF_CYCLES ************************/
  55. int process_with_swapped_bufs_on_single_half_cycles(dataptr dz) /* [distortion0] */
  56. {
  57. int exit_status;
  58. int current_buf = 0;
  59. int lastzero = 0;
  60. /*int*/float cyclemax = 0.0;
  61. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  62. return(exit_status);
  63. current_buf = !current_buf;
  64. while(dz->samps_left > 0) {
  65. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  66. return(exit_status);
  67. current_buf = !current_buf;
  68. switch(dz->process) {
  69. case(DISTORT): exit_status = do_distort(current_buf,0,&lastzero,&cyclemax,dz); break;
  70. default:
  71. sprintf(errstr,"Unknown case in process_with_swapped_bufs_on_single_half_cycles()\n");
  72. return(PROGRAM_ERROR);
  73. }
  74. if(exit_status<0)
  75. return(exit_status);
  76. if((exit_status = write_samps(dz->sampbuf[current_buf],dz->buflen,dz))<0)
  77. return(exit_status);
  78. }
  79. current_buf = !current_buf;
  80. switch(dz->process) {
  81. case(DISTORT): exit_status = do_distort(current_buf,1,&lastzero,&cyclemax,dz); break;
  82. default:
  83. sprintf(errstr,"Unknown case in process_with_swapped_bufs_on_single_half_cycles()\n");
  84. return(PROGRAM_ERROR);
  85. }
  86. if(exit_status<0)
  87. return(exit_status);
  88. if(dz->ssampsread > 0)
  89. return write_samps(dz->sampbuf[current_buf],dz->ssampsread,dz);
  90. return FINISHED;
  91. }
  92. /*************************** PROCESS_WITH_SWAPPED_BUFS_ON_FULL_CYCLES ************************/
  93. int process_with_swapped_bufs_on_full_cycles(dataptr dz) /* [distortion2] */
  94. {
  95. int exit_status;
  96. int current_pos_in_buf, cnt = 0;
  97. int buffer_overrun, initial_phase, current_buf = 0;
  98. double thistime;
  99. display_virtual_time(0,dz);
  100. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  101. return(exit_status);
  102. if((exit_status = get_initial_phase(&initial_phase,dz))<0)
  103. return(exit_status);
  104. current_pos_in_buf = 0;
  105. do {
  106. thistime = (double)(dz->total_samps_read - dz->ssampsread + current_pos_in_buf)/(double)dz->infile->srate;
  107. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  108. return(exit_status);
  109. buffer_overrun = FALSE;
  110. switch(dz->process) {
  111. case(DISTORT_ENV):
  112. exit_status = distort_env(&current_buf,initial_phase,&current_pos_in_buf,&buffer_overrun,&cnt,dz);
  113. break;
  114. case(DISTORT_REV):
  115. exit_status = distort_rev(&current_buf,initial_phase,&current_pos_in_buf,&buffer_overrun,&cnt,dz);
  116. break;
  117. default:
  118. sprintf(errstr,"Unknown case in process_with_swapped_bufs_on_full_cycles()\n");
  119. return(PROGRAM_ERROR);
  120. }
  121. } while(exit_status == CONTINUE);
  122. if(exit_status<0)
  123. return(exit_status);
  124. if(cnt==0) {
  125. sprintf(errstr,"source sound too short to attempt this process.\n");
  126. return(GOAL_FAILED);
  127. }
  128. //TW DELETED IN UPDATED CODE
  129. if(current_pos_in_buf > 0)
  130. return write_samps(dz->sampbuf[current_buf],current_pos_in_buf,dz);
  131. return(FINISHED);
  132. }
  133. /***************************** PROCESS_ON_SINGLE_BUF_WITH_PHASE_DEPENDENCE *****************************/
  134. int process_on_single_buf_with_phase_dependence(dataptr dz)
  135. {
  136. int exit_status;
  137. int initial_phase;
  138. int current_pos_in_buf = 0;
  139. double thistime;
  140. display_virtual_time(0,dz);
  141. if((exit_status = read_samps(dz->sampbuf[0],dz))<0)
  142. return(exit_status);
  143. if((exit_status = get_initial_phase(&initial_phase,dz))<0)
  144. return(exit_status);
  145. do {
  146. thistime = (double)(dz->total_samps_read - dz->ssampsread + current_pos_in_buf)/(double)dz->infile->srate;
  147. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  148. return(exit_status);
  149. exit_status = distort_omt(&current_pos_in_buf,initial_phase,dz);
  150. } while(exit_status==CONTINUE);
  151. ;
  152. return(exit_status);
  153. }
  154. /**************** PROCESS_WITH_SWAPPED_BUFS_ON_FULL_CYCLES_WITH_NEWSIZE_OUTPUT_AND_SKIPCYCLES *************************/
  155. int process_with_swapped_bufs_on_full_cycles_with_newsize_output_and_skipcycles(float *outbuf,int skip_param,dataptr dz)
  156. {
  157. int exit_status;
  158. int current_buf = 0, initial_phase, previous_cycle_crossed_bufs = FALSE;
  159. int obufpos = 0, current_pos_in_buf = 0;
  160. int cpinbf, iobufpos;
  161. int cnt = 0;
  162. double thistime;
  163. int lastcycle_len = 0, lastcycle_start;
  164. display_virtual_time(0,dz);
  165. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  166. return(exit_status);
  167. if((exit_status = get_initial_phase(&initial_phase,dz))<0)
  168. return(exit_status);
  169. if(dz->vflag[IS_DISTORT_SKIP] && (exit_status = skip_initial_cycles(&current_pos_in_buf,&current_buf,initial_phase,skip_param,dz))<0)
  170. return(exit_status);
  171. do {
  172. thistime = (double)(dz->total_samps_read - dz->ssampsread + current_pos_in_buf)/(double)dz->infile->srate;
  173. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  174. return(exit_status);
  175. switch(dz->process) {
  176. case(DISTORT_AVG):
  177. cpinbf = (int)current_pos_in_buf;
  178. exit_status = distort_avg(&current_buf,initial_phase,&obufpos,&cpinbf,&cnt,dz);
  179. current_pos_in_buf = (int)cpinbf;
  180. break;
  181. case(DISTORT_SHUF):
  182. exit_status = distort_shuf(&current_buf,initial_phase,&obufpos,&current_pos_in_buf,&cnt,dz);
  183. break;
  184. case(DISTORT_RPT):
  185. case(DISTORT_RPT2):
  186. exit_status = distort_rpt(&current_buf,initial_phase,&obufpos,&current_pos_in_buf,&cnt,
  187. dz->iparam[DISTRPT_CYCLECNT],&lastcycle_len,&lastcycle_start,&previous_cycle_crossed_bufs,dz);
  188. break;
  189. case(DISTORT_RPTFL):
  190. exit_status = distort_rpt_frqlim(&current_buf,initial_phase,&obufpos,&current_pos_in_buf,&cnt,
  191. dz->iparam[DISTRPT_CYCLECNT],dz);
  192. break;
  193. case(DISTORT_INTP):
  194. exit_status = distort_rpt(&current_buf,initial_phase,&obufpos,&current_pos_in_buf,&cnt,
  195. 1,&lastcycle_len,&lastcycle_start,&previous_cycle_crossed_bufs,dz);
  196. break;
  197. case(DISTORT_DEL):
  198. switch(dz->mode) {
  199. case(DELETE_IN_STRICT_ORDER):
  200. exit_status = distort_del(&current_buf,&current_pos_in_buf,initial_phase,&obufpos,&cnt,dz);
  201. break;
  202. case(KEEP_STRONGEST):
  203. case(DELETE_WEAKEST):
  204. exit_status = distort_del_with_loudness(&current_buf,initial_phase,&obufpos,&current_pos_in_buf,&cnt,dz);
  205. break;
  206. }
  207. break;
  208. case(DISTORT_RPL):
  209. iobufpos = (int)obufpos;
  210. exit_status = distort_rpl(&current_buf,initial_phase,&iobufpos,&current_pos_in_buf,&cnt,dz);
  211. obufpos = (int)iobufpos;
  212. break;
  213. case(DISTORT_TEL):
  214. iobufpos = (int)obufpos;
  215. cpinbf = (int)current_pos_in_buf;
  216. exit_status = distort_tel(&current_buf,initial_phase,&iobufpos,&cpinbf,&cnt,dz);
  217. obufpos = (int)iobufpos;
  218. current_pos_in_buf = (int)cpinbf;
  219. break;
  220. case(DISTORT_FLT):
  221. cnt = 1;
  222. iobufpos = (int)obufpos;
  223. exit_status = distort_flt(&current_buf,initial_phase,&iobufpos,&current_pos_in_buf,dz);
  224. obufpos = (int)iobufpos;
  225. break;
  226. default:
  227. sprintf(errstr,"Unknown case in process_with_swapped_bufs_on_full_cycles_with_newsize_output_and_skipcycles()\n");
  228. return(PROGRAM_ERROR);
  229. }
  230. } while(exit_status == CONTINUE);
  231. if(exit_status <0)
  232. return(exit_status);
  233. if(cnt==0) {
  234. sprintf(errstr,"source sound too short to attempt this process.\n");
  235. return(GOAL_FAILED);
  236. }
  237. if(obufpos > 0)
  238. return write_samps(outbuf,obufpos,dz);
  239. return(FINISHED);
  240. }
  241. /***************************** PROCESS_WITH_SWAPPED_BUF_TO_SWAPPED_OUTBUFS *****************************/
  242. int process_with_swapped_buf_to_swapped_outbufs(dataptr dz)
  243. {
  244. int exit_status;
  245. int current_buf = 0, output_phase = 1 /*, cyclemax = 0*/;
  246. float cyclemax = 0.0;
  247. int lastzero = 0, endsample;
  248. int no_of_half_cycles = 0;
  249. int startindex, startmarker, endindex;
  250. display_virtual_time(0L,dz);
  251. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  252. return(exit_status);
  253. if(dz->ssampsread <=0) {
  254. sprintf(errstr,"No data found in input soundfile.\n");
  255. return(DATA_ERROR);
  256. }
  257. current_buf = !current_buf;
  258. while(dz->samps_left > 0) {
  259. read_samps(dz->sampbuf[current_buf],dz);
  260. current_buf = !current_buf;
  261. switch(dz->process) {
  262. case(DISTORT_MLT):
  263. case(DISTORT_DIV):
  264. exit_status = mdistort(!LAST,&lastzero,&endsample,&output_phase,current_buf,&cyclemax,
  265. &no_of_half_cycles,&startindex,&startmarker,&endindex,dz);
  266. break;
  267. default:
  268. sprintf(errstr,"Unknown case in process_with_swapped_buf_to_swapped_outbufs()\n");
  269. return(PROGRAM_ERROR);
  270. }
  271. if(exit_status<0)
  272. return(exit_status);
  273. if((exit_status = write_samps(dz->sampbuf[current_buf+2],dz->buflen,dz))<0)
  274. return(exit_status);
  275. memset((char *)dz->sampbuf[current_buf+2],0,dz->buflen * sizeof(float));
  276. }
  277. current_buf = !current_buf;
  278. switch(dz->process) {
  279. case(DISTORT_MLT):
  280. case(DISTORT_DIV):
  281. exit_status = mdistort(LAST,&lastzero,&endsample,&output_phase,current_buf,&cyclemax,
  282. &no_of_half_cycles,&startindex,&startmarker,&endindex,dz);
  283. break;
  284. default:
  285. sprintf(errstr,"Unknown case in process_with_swapped_buf_to_swapped_outbufs()\n");
  286. return(PROGRAM_ERROR);
  287. }
  288. if(exit_status<0)
  289. return(exit_status);
  290. if(endsample > 0)
  291. return write_samps(dz->sampbuf[current_buf+2],endsample,dz);
  292. return(FINISHED);
  293. }
  294. /******** PROCESS_WITH_SWAPPED_BUFS_ON_FULL_CYCLES_WITH_OPTIONAL_PRESCALE *******/
  295. int process_with_swapped_bufs_on_full_cycles_with_optional_prescale(dataptr dz)
  296. {
  297. int exit_status;
  298. int initial_phase;
  299. int cnt = 0;
  300. //TW FIXED -> SAMPS
  301. int bufpos = 0, last_total_samps_read = 0;
  302. int prescale_param, current_buf = 0;
  303. display_virtual_time(0,dz);
  304. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  305. return(exit_status);
  306. switch(dz->process) {
  307. case(DISTORT_HRM): prescale_param = DISTORTH_PRESCALE; break;
  308. case(DISTORT_FRC): prescale_param = DISTORTF_PRESCALE; break;
  309. default:
  310. sprintf(errstr,"Unknown case: process_with_swapped_bufs_on_full_cycles_with_optional_prescale()\n");
  311. return(PROGRAM_ERROR);
  312. }
  313. if(dz->vflag[IS_PRESCALED])
  314. prescale(current_buf,prescale_param,dz);
  315. if((exit_status = get_initial_phase(&initial_phase,dz))<0)
  316. return(exit_status);
  317. exit_status = CONTINUE;
  318. switch(dz->process) {
  319. case(DISTORT_HRM):
  320. while(exit_status==CONTINUE) {
  321. //TW FIXED-> SAMPS
  322. exit_status = distorth(&bufpos,initial_phase,&last_total_samps_read,&current_buf,dz);
  323. cnt++;
  324. }
  325. break;
  326. case(DISTORT_FRC):
  327. while(exit_status==CONTINUE) {
  328. //TW FIXED-> SAMPS
  329. exit_status = distortf(&bufpos,initial_phase,&last_total_samps_read,&current_buf,dz);
  330. cnt++;
  331. }
  332. break;
  333. }
  334. if(exit_status<0)
  335. return(exit_status);
  336. if(cnt <= 1) {
  337. sprintf(errstr,"source sound too short to attempt this process.\n");
  338. return(GOAL_FAILED);
  339. }
  340. if(bufpos > 0)
  341. return write_samps(dz->sampbuf[current_buf],bufpos,dz);
  342. return(FINISHED);
  343. }
  344. /**************************** PRESCALE *****************************/
  345. /* prescaling added by rwd */
  346. void prescale(int current_buf,int prescale_param,dataptr dz)
  347. {
  348. int i;
  349. double temp;
  350. for(i=0;i<dz->ssampsread;i++) {
  351. temp = (double)dz->sampbuf[current_buf][i];
  352. dz->sampbuf[current_buf][i] = (float)/* round*/(temp * dz->param[prescale_param]); /*RWD added cast */
  353. }
  354. }
  355. /**************************** COP_OUT **************************/
  356. int cop_out(int i, int j, int last_total_samps_read, dataptr dz)
  357. {
  358. j = last_total_samps_read + i - j + 1;
  359. fprintf(stdout,"WARNING: Program assumes maximum wavelength is %lf secs.\n",MAXWAVELEN);
  360. fprintf(stdout,"WARNING: Wavelength too long at infile time %lf secs.\n",(double)j/(double)dz->infile->srate);
  361. return(GOAL_FAILED);
  362. }
  363. /***************************** PROCESS_FILE **************************/
  364. int process_cyclecnt(dataptr dz)
  365. {
  366. int exit_status;
  367. /*long i = 0;*/
  368. int current_buf = 0;
  369. int current_pos_in_buf = 0;
  370. int initial_phase;
  371. dz->itemcnt = 0;
  372. if((exit_status = read_samps(dz->sampbuf[current_buf],dz))<0)
  373. return(exit_status);
  374. get_initial_phase(&initial_phase,dz);
  375. while(cyccnt(&current_pos_in_buf,&current_buf,initial_phase,dz))
  376. dz->itemcnt++;
  377. sprintf(errstr,"%d cycles found.\n",dz->itemcnt);
  378. return(FINISHED);
  379. }
  380. /************************** CYCCNT *************************/
  381. int cyccnt(int *current_pos_in_buf,int *current_buf,int initial_phase,dataptr dz)
  382. {
  383. int exit_status;
  384. register int i = *current_pos_in_buf;
  385. float *b = dz->sampbuf[*current_buf];
  386. int buffer_overrun = FALSE;
  387. switch(initial_phase) {
  388. case(1):
  389. while(b[i]>=0) {
  390. if(++i >= dz->ssampsread) {
  391. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE)
  392. return(FINISHED);
  393. i = 0;
  394. }
  395. }
  396. while(b[i]<=0) {
  397. if(++i >= dz->ssampsread) {
  398. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE)
  399. return(FINISHED);
  400. i = 0;
  401. }
  402. }
  403. break;
  404. case(-1):
  405. while(b[i]<=0) {
  406. if(++i >= dz->ssampsread) {
  407. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE)
  408. return(FINISHED);
  409. i = 0;
  410. }
  411. }
  412. while(b[i]>=0) {
  413. if(++i >= dz->ssampsread) {
  414. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE)
  415. return(FINISHED);
  416. i = 0;
  417. }
  418. }
  419. break;
  420. }
  421. *current_pos_in_buf = i;
  422. return(CONTINUE);
  423. }