distrpt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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_repeat(int current_buf,int incycles_start,int incycles_end,int *obufpos,dataptr dz);
  36. static int do_repeat_bufcros(int current_buf,int incycles_start,int incycles_end,int *obufpos,dataptr dz);
  37. static int do_interp(int current_buf,int incycle_start,int incycle_end,int *lastcycle_len,
  38. int lastcycle_start,int *previous_cycle_crossed_bufs,int *obufpos,dataptr dz);
  39. static int do_interp_bufcros(int current_buf,int incycle_end,int incycle_start,int *lastcycle_len,
  40. int lastcycle_start,int *previous_cycle_crossed_bufs,int *obufpos,dataptr dz);
  41. static int write_to_outbuf(int *obufpos,int collectbufpos,dataptr dz);
  42. static int no_fault_change_buff(float **b,int *current_buf,dataptr dz);
  43. /********************** SKIP_INITIAL_CYCLES ******************************/
  44. int skip_initial_cycles(int *current_pos,int *current_buf,int phase,int skip_paramno,dataptr dz)
  45. {
  46. int exit_status;
  47. register int i = 0;
  48. int n;
  49. float *b = dz->sampbuf[*current_buf];
  50. switch(phase) {
  51. case(1):
  52. for(n=0;n<dz->iparam[skip_paramno];n++) {
  53. while(b[i]>=0.0) {
  54. if(++i >= dz->ssampsread) {
  55. if((exit_status = no_fault_change_buff(&b,current_buf,dz))<0)
  56. return(exit_status);
  57. }
  58. }
  59. while(b[i]<=0.0) {
  60. if(++i >= dz->ssampsread) {
  61. if((exit_status = no_fault_change_buff(&b,current_buf,dz))<0)
  62. return(exit_status);
  63. }
  64. }
  65. }
  66. break;
  67. case(-1):
  68. for(n=0;n<dz->iparam[skip_paramno];n++) {
  69. while(b[i]<=0.0) {
  70. if(++i >= dz->ssampsread) {
  71. if((exit_status = no_fault_change_buff(&b,current_buf,dz))<0)
  72. return(exit_status);
  73. }
  74. }
  75. while(b[i]>=0.0) {
  76. if(++i >= dz->ssampsread) {
  77. if((exit_status = no_fault_change_buff(&b,current_buf,dz))<0)
  78. return(exit_status);
  79. }
  80. }
  81. }
  82. break;
  83. }
  84. *current_pos = i;
  85. return(FINISHED);
  86. }
  87. /**************************** DISTORT_RPT ******************************
  88. *
  89. * Distort file by repeating GROUPS of cycles.
  90. */
  91. int distort_rpt(int *current_buf,int initial_phase,int *obufpos,int *current_pos_in_buf,int *cnt,
  92. int cyclecnt,int *lastcycle_len,int *lastcycle_start,int *previous_cycle_crossed_bufs,dataptr dz)
  93. {
  94. int exit_status;
  95. register int i = *current_pos_in_buf;
  96. register int n;
  97. int cycleno_in_group_at_bufcros = -1;
  98. float *inbuf = dz->sampbuf[*current_buf];
  99. int incycles_end, incycles_start = i, jump_cyclecnt;
  100. for(n=0;n<cyclecnt;n++) {
  101. switch(initial_phase) {
  102. case(1):
  103. while(inbuf[i]>=0.0) {
  104. if(++i >= dz->ssampsread) {
  105. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  106. return(exit_status);
  107. cycleno_in_group_at_bufcros = n;
  108. i = 0;
  109. }
  110. }
  111. while(inbuf[i]<=0.0) {
  112. if(++i >= dz->ssampsread) {
  113. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  114. return(exit_status);
  115. cycleno_in_group_at_bufcros = n;
  116. i = 0;
  117. }
  118. }
  119. break;
  120. case(-1):
  121. while(inbuf[i]<=0.0) {
  122. if(++i >= dz->ssampsread) {
  123. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  124. return(exit_status);
  125. cycleno_in_group_at_bufcros = n;
  126. i = 0;
  127. }
  128. }
  129. while(inbuf[i]>=0.0) {
  130. if(++i >= dz->ssampsread) {
  131. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  132. return(exit_status);
  133. cycleno_in_group_at_bufcros = n;
  134. i = 0;
  135. }
  136. }
  137. break;
  138. }
  139. }
  140. incycles_end = i;
  141. switch(dz->process) {
  142. case(DISTORT_INTP):
  143. if(cycleno_in_group_at_bufcros >= 0)
  144. exit_status = do_interp_bufcros(*current_buf,incycles_start,incycles_end,lastcycle_len,
  145. *lastcycle_start,previous_cycle_crossed_bufs,obufpos,dz);
  146. else
  147. exit_status = do_interp(*current_buf,incycles_start,incycles_end,lastcycle_len,
  148. *lastcycle_start,previous_cycle_crossed_bufs,obufpos,dz);
  149. *lastcycle_start = *current_pos_in_buf;
  150. break;
  151. case(DISTORT_RPT):
  152. case(DISTORT_RPT2):
  153. if(cycleno_in_group_at_bufcros >= 0)
  154. exit_status = do_repeat_bufcros(*current_buf,incycles_start,incycles_end,obufpos,dz);
  155. else
  156. exit_status = do_repeat(*current_buf,incycles_start,incycles_end,obufpos,dz);
  157. break;
  158. default:
  159. sprintf(errstr,"Unknown case in distort_rpt()\n");
  160. return(PROGRAM_ERROR);
  161. }
  162. if(dz->process == DISTORT_RPT2) {
  163. jump_cyclecnt = (dz->iparam[DISTRPT_MULTIPLY] - 1) * cyclecnt;
  164. for(n=0;n<jump_cyclecnt;n++) {
  165. switch(initial_phase) {
  166. case(1):
  167. while(inbuf[i]>=0) {
  168. if(++i >= dz->ssampsread) {
  169. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  170. return(exit_status);
  171. cycleno_in_group_at_bufcros = n;
  172. i = 0;
  173. }
  174. }
  175. while(inbuf[i]<=0) {
  176. if(++i >= dz->ssampsread) {
  177. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  178. return(exit_status);
  179. cycleno_in_group_at_bufcros = n;
  180. i = 0;
  181. }
  182. }
  183. break;
  184. case(-1):
  185. while(inbuf[i]<=0) {
  186. if(++i >= dz->ssampsread) {
  187. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  188. return(exit_status);
  189. cycleno_in_group_at_bufcros = n;
  190. i = 0;
  191. }
  192. }
  193. while(inbuf[i]>=0) {
  194. if(++i >= dz->ssampsread) {
  195. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  196. return(exit_status);
  197. cycleno_in_group_at_bufcros = n;
  198. i = 0;
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. if(exit_status<0)
  206. return(exit_status);
  207. *current_pos_in_buf = i;
  208. (*cnt)++;
  209. return(CONTINUE);
  210. }
  211. /*********************** DO_REPEAT *************************/
  212. int do_repeat(int current_buf,int incycles_start,int incycles_end,int *obufpos,dataptr dz)
  213. {
  214. int exit_status;
  215. float *obuf, *ibuf = dz->sampbuf[current_buf] + incycles_start;
  216. int outcnt = 0;
  217. int incycle_len = incycles_end - incycles_start;
  218. int incycle_samps = incycle_len;
  219. int first_part_outbuf, second_part_outbuf;
  220. int next_obufpos;
  221. int finished = FALSE;
  222. do {
  223. while((next_obufpos = *obufpos + incycle_len) <= dz->buflen) {
  224. obuf = dz->sampbuf[2] + *obufpos;
  225. memmove((char *)obuf,(char *)ibuf,incycle_samps * sizeof(float));
  226. *obufpos = next_obufpos;
  227. if(++outcnt >= dz->iparam[DISTRPT_MULTIPLY]) {
  228. finished = TRUE;
  229. break;
  230. }
  231. }
  232. if(!finished) {
  233. first_part_outbuf = dz->buflen - *obufpos;
  234. second_part_outbuf = incycle_len - first_part_outbuf;
  235. obuf = dz->sampbuf[2] + *obufpos;
  236. memmove((char *)obuf,(char *)ibuf,first_part_outbuf * sizeof(float));
  237. obuf = dz->sampbuf[2];
  238. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  239. return(exit_status);
  240. memmove((char *)obuf,(char *)(ibuf + first_part_outbuf),second_part_outbuf * sizeof(float));
  241. *obufpos = second_part_outbuf;
  242. if(++outcnt >= dz->iparam[DISTRPT_MULTIPLY])
  243. finished = TRUE;
  244. }
  245. } while(!finished);
  246. return(FINISHED);
  247. }
  248. /*********************** DO_REPEAT_BUFCROS *************************/
  249. int do_repeat_bufcros(int current_buf,int incycles_start,int incycles_end,int *obufpos,dataptr dz)
  250. {
  251. int exit_status;
  252. float *obuf, *ibuf, *tempbuf;
  253. int outcnt = 0;
  254. int incycles_len = dz->buflen - incycles_start + incycles_end;
  255. int incycle_samps = incycles_len;
  256. int first_part_inbuf, second_part_inbuf;
  257. int first_part_outbuf, second_part_outbuf;
  258. int next_obufpos;
  259. int finished = FALSE;
  260. if(incycles_len >= dz->buflen) {
  261. sprintf(errstr,"cycleslen exceeds bufferlen %lf secs\n",
  262. (double)(dz->total_samps_read - dz->ssampsread + incycles_start)/(double)dz->infile->srate);
  263. return(GOAL_FAILED);
  264. }
  265. first_part_inbuf = dz->buflen - incycles_start;
  266. second_part_inbuf = incycles_len - first_part_inbuf;
  267. ibuf = dz->sampbuf[!current_buf] + incycles_start;
  268. tempbuf = dz->sampbuf[3];
  269. memmove((char *)tempbuf,(char *)ibuf,first_part_inbuf * sizeof(float));
  270. ibuf = dz->sampbuf[current_buf];
  271. tempbuf += first_part_inbuf;
  272. memmove((char *)tempbuf,(char *)ibuf,second_part_inbuf * sizeof(float));
  273. ibuf = dz->sampbuf[3];
  274. do {
  275. while((next_obufpos = *obufpos + incycles_len) <= dz->buflen) {
  276. obuf = dz->sampbuf[2] + *obufpos;
  277. memmove((char *)obuf,(char *)ibuf,incycle_samps * sizeof(float));
  278. *obufpos = next_obufpos;
  279. if(++outcnt >= dz->iparam[DISTRPT_MULTIPLY]) {
  280. finished = TRUE;
  281. break;
  282. }
  283. }
  284. if(!finished) {
  285. first_part_outbuf = dz->buflen - *obufpos;
  286. second_part_outbuf = incycles_len - first_part_outbuf;
  287. obuf = dz->sampbuf[2] + *obufpos;
  288. memmove((char *)obuf,(char *)ibuf,first_part_outbuf * sizeof(float));
  289. obuf = dz->sampbuf[2];
  290. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  291. return(exit_status);
  292. memmove((char *)obuf,(char *)(ibuf + first_part_outbuf),second_part_outbuf * sizeof(float));
  293. *obufpos = second_part_outbuf;
  294. if(++outcnt >= dz->iparam[DISTRPT_MULTIPLY])
  295. finished = TRUE;
  296. }
  297. } while(!finished);
  298. return(FINISHED);
  299. }
  300. /*************************** DO_INTERP ****************************/
  301. int do_interp(int current_buf,int incycle_start,int incycle_end,int *lastcycle_len,
  302. int lastcycle_start,int *previous_cycle_crossed_bufs,int *obufpos,dataptr dz)
  303. {
  304. register int k;
  305. int this_cyclelen, n;
  306. double part[2], d, ratio;
  307. int cycpoint[2];
  308. int incycle_len = incycle_end - incycle_start;
  309. int cycdiff = incycle_len - *lastcycle_len; /* difference in length of cycles */
  310. float *tempbuf = dz->sampbuf[3];
  311. float *inbuf = dz->sampbuf[current_buf];
  312. float *collectbuf = dz->sampbuf[4];
  313. int collectbufpos = 0;
  314. int maxcyclelen = max(incycle_len,*lastcycle_len);
  315. if(maxcyclelen * dz->iparam[DISTINTP_MULTIPLY] > dz->buflen) {
  316. sprintf(errstr,"Cycles too long for buffer\n");
  317. return(GOAL_FAILED);
  318. }
  319. if(*lastcycle_len > 0) { /* very first cycle (lastcycle_len=0)is merely copied: otherwise, interp */
  320. for(n=1;n<dz->iparam[DISTINTP_MULTIPLY];n++) {
  321. d = round((double)cycdiff * (double)n/(double)dz->iparam[DISTINTP_MULTIPLY]);
  322. d += (double)(*lastcycle_len);
  323. this_cyclelen = (int)d;
  324. if(*previous_cycle_crossed_bufs) {
  325. for(k=0;k<this_cyclelen;k++) {
  326. ratio = (double)k/(double)this_cyclelen;
  327. cycpoint[0] = round(ratio * (double)(*lastcycle_len));
  328. cycpoint[1] = round(ratio * (double)incycle_len);
  329. cycpoint[1] += incycle_start;
  330. part[0] = (double)tempbuf[cycpoint[0]] * (double)(dz->iparam[DISTINTP_MULTIPLY] - n);
  331. part[1] = (double)inbuf[cycpoint[1]] * (double)n;
  332. collectbuf[collectbufpos++] = (float)/*round*/((part[0] + part[1])/dz->iparam[DISTINTP_MULTIPLY]);
  333. }
  334. *previous_cycle_crossed_bufs = FALSE;
  335. } else {
  336. for(k=0;k<this_cyclelen;k++) {
  337. ratio = (double)k/(double)this_cyclelen;
  338. cycpoint[0] = round(ratio * (double)(*lastcycle_len));
  339. cycpoint[0] += lastcycle_start;
  340. cycpoint[1] = round(ratio * (double)incycle_len);
  341. cycpoint[1] += incycle_start;
  342. part[0] = (double)inbuf[cycpoint[0]] * (double)(dz->iparam[DISTINTP_MULTIPLY] - n);
  343. part[1] = (double)inbuf[cycpoint[1]] * (double)n;
  344. collectbuf[collectbufpos++] = (float)/*round*/((part[0] + part[1])/dz->iparam[DISTINTP_MULTIPLY]);
  345. }
  346. }
  347. }
  348. }
  349. /******* COPY GOAL CYCLE ******/
  350. memmove((char *)(&(collectbuf[collectbufpos])),(char *)(&(inbuf[incycle_start])),incycle_len * sizeof(float));
  351. collectbufpos += incycle_len;
  352. *lastcycle_len = incycle_len;
  353. return write_to_outbuf(obufpos,collectbufpos,dz);
  354. }
  355. /*************************** DO_INTERP_CROSBUF ****************************/
  356. int do_interp_bufcros(int current_buf,int incycle_start,int incycle_end,int *lastcycle_len,int lastcycle_start,
  357. int *previous_cycle_crossed_bufs,int *obufpos,dataptr dz)
  358. {
  359. register int k;
  360. int this_cyclelen, n;
  361. double part[2], d, ratio;
  362. int cycpoint[2];
  363. int first_incycle_part = dz->buflen - incycle_start;
  364. int second_incycle_part = incycle_end;
  365. int incycle_len = first_incycle_part + second_incycle_part;
  366. int cycdiff = incycle_len - *lastcycle_len; /* difference in length of cycles */
  367. float *inbuf;
  368. float *tempbuf = dz->sampbuf[3];
  369. float *collectbuf = dz->sampbuf[4];
  370. int collectbufpos = 0;
  371. int maxcyclelen = max(incycle_len,*lastcycle_len);
  372. if(maxcyclelen * dz->iparam[DISTINTP_MULTIPLY] > dz->buflen) {
  373. sprintf(errstr,"Cycles too long for buffer\n");
  374. return(GOAL_FAILED);
  375. }
  376. if(*previous_cycle_crossed_bufs) { /* IF THE CURRENT CYCLE CROSSES A BUFFER, AND THE LAST CYCLE CROSSES */
  377. sprintf(errstr,"Buffer space overrun\n"); /* A BUFFER, 2 BUFFER ENDS ARE CROSSED AND WE CAN'T DO THE READING */
  378. return(GOAL_FAILED);
  379. }
  380. *previous_cycle_crossed_bufs = TRUE;
  381. if(*lastcycle_len > 0) { /* very first cycle (lastcycle_len=0)is merely copied: otherwise, interp */
  382. /* PUT BUFFER_CROSSING CYCLE INTO A TEMPORARY STORE */
  383. inbuf = dz->sampbuf[!current_buf] + incycle_start;
  384. tempbuf = dz->sampbuf[3];
  385. memmove((char *)tempbuf,(char *)inbuf,first_incycle_part * sizeof(float));
  386. inbuf = dz->sampbuf[current_buf];
  387. tempbuf += first_incycle_part;
  388. memmove((char *)tempbuf,(char *)inbuf,second_incycle_part * sizeof(float));
  389. tempbuf = dz->sampbuf[3];
  390. for(n=1;n<dz->iparam[DISTINTP_MULTIPLY];n++) {
  391. d = round((double)cycdiff * (double)n/(double)dz->iparam[DISTINTP_MULTIPLY]);
  392. d += (double)(*lastcycle_len);
  393. this_cyclelen = (int)d;
  394. for(k=0;k<this_cyclelen;k++) {
  395. ratio = (double)k/(double)this_cyclelen;
  396. cycpoint[0] = round(ratio * (double)(*lastcycle_len));
  397. cycpoint[0] += lastcycle_start;
  398. inbuf = dz->sampbuf[!current_buf]; /* PREVIOUS CYC MUST BE COMPLETELY IN PREVIOUS BUF */
  399. part[0] = (double)inbuf[cycpoint[0]] * (double)(dz->iparam[DISTINTP_MULTIPLY]-n);
  400. cycpoint[1] = round(ratio * (double)this_cyclelen);
  401. part[1] = (double)tempbuf[cycpoint[1]] * (double)(dz->iparam[DISTINTP_MULTIPLY]-n);
  402. collectbuf[collectbufpos++] = (float)/*round*/((part[0] + part[1])/dz->iparam[DISTINTP_MULTIPLY]);
  403. }
  404. }
  405. }
  406. /******* COPY GOAL CYCLE ******/
  407. memmove((char *)(&(collectbuf[collectbufpos])),(char *)tempbuf,incycle_len * sizeof(float));
  408. collectbufpos += incycle_len;
  409. *lastcycle_len = incycle_len;
  410. return write_to_outbuf(obufpos,collectbufpos,dz);
  411. }
  412. /****************************** NO_FAULT_CHANGE_BUFF *****************************/
  413. int no_fault_change_buff(float **b,int *current_buf,dataptr dz)
  414. {
  415. if(dz->samps_left <= 0) {
  416. sprintf(errstr,"Not enough cycles to perform this process.\n");
  417. return(GOAL_FAILED);
  418. }
  419. *current_buf = !(*current_buf);
  420. *b = dz->sampbuf[*current_buf];
  421. return read_samps(*b,dz);
  422. }
  423. /****************************** WRITE_TO_OUTBUF *****************************/
  424. int write_to_outbuf(int *obufpos,int collectbufpos,dataptr dz)
  425. {
  426. int exit_status;
  427. int obuf_free = dz->buflen - *obufpos;
  428. int collectbufcnt = collectbufpos;
  429. float *collectbuf = dz->sampbuf[4];
  430. dz->sbufptr[2] = dz->sampbuf[2] + *obufpos;
  431. if(collectbufcnt <= obuf_free) {
  432. memmove((char *)dz->sbufptr[2],(char *)collectbuf,collectbufcnt * sizeof(float));
  433. *obufpos += collectbufcnt;
  434. } else {
  435. memmove((char *)dz->sbufptr[2],(char *)collectbuf,obuf_free * sizeof(float));
  436. if((exit_status = write_samps(dz->sampbuf[2],dz->buflen,dz))<0)
  437. return(exit_status);
  438. dz->sbufptr[2] = dz->sampbuf[2];
  439. collectbuf += obuf_free;
  440. collectbufcnt -= obuf_free;
  441. memmove((char *)dz->sbufptr[2],(char *)collectbuf, collectbufcnt * sizeof(float));
  442. *obufpos = collectbufcnt;
  443. }
  444. return(FINISHED);
  445. }
  446. /**************************** DISTORT_RPT_FRQLIM ******************************
  447. *
  448. * Distort file by repeating GROUPS of cycles, but don't count too-short cycles.
  449. */
  450. int distort_rpt_frqlim(int *current_buf,int initial_phase,int *obufpos,int *current_pos_in_buf,int *cnt,
  451. int cyclecnt,dataptr dz)
  452. {
  453. int exit_status;
  454. register int i = *current_pos_in_buf;
  455. register int n;
  456. int mincyclen = (int)round(dz->infile->srate/dz->param[DISTRPT_CYCLIM]), thiscyclen;
  457. int cycleno_in_group_at_bufcros = -1;
  458. float *inbuf = dz->sampbuf[*current_buf];
  459. int incycles_end, incycles_start = i;
  460. for(n=0;n<cyclecnt;n++) {
  461. switch(initial_phase) {
  462. case(1):
  463. thiscyclen = 0;
  464. for(;;) {
  465. while(inbuf[i]>=0) {
  466. if(++i >= dz->ssampsread) {
  467. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  468. return(exit_status);
  469. cycleno_in_group_at_bufcros = n;
  470. i = 0;
  471. }
  472. thiscyclen++;
  473. }
  474. while(inbuf[i]<=0) {
  475. if(++i >= dz->ssampsread) {
  476. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  477. return(exit_status);
  478. cycleno_in_group_at_bufcros = n;
  479. i = 0;
  480. }
  481. thiscyclen++;
  482. }
  483. if(thiscyclen >= mincyclen)
  484. break;
  485. }
  486. break;
  487. case(-1):
  488. thiscyclen = 0;
  489. for(;;) {
  490. while(inbuf[i]<=0) {
  491. if(++i >= dz->ssampsread) {
  492. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  493. return(exit_status);
  494. cycleno_in_group_at_bufcros = n;
  495. i = 0;
  496. }
  497. thiscyclen++;
  498. }
  499. while(inbuf[i]>=0) {
  500. if(++i >= dz->ssampsread) {
  501. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  502. return(exit_status);
  503. cycleno_in_group_at_bufcros = n;
  504. i = 0;
  505. }
  506. thiscyclen++;
  507. }
  508. if(thiscyclen >= mincyclen)
  509. break;
  510. }
  511. break;
  512. }
  513. }
  514. incycles_end = i;
  515. if(cycleno_in_group_at_bufcros >= 0)
  516. exit_status = do_repeat_bufcros(*current_buf,incycles_start,incycles_end,obufpos,dz);
  517. else
  518. exit_status = do_repeat(*current_buf,incycles_start,incycles_end,obufpos,dz);
  519. if(exit_status<0)
  520. return(exit_status);
  521. *current_pos_in_buf = i;
  522. (*cnt)++;
  523. return(CONTINUE);
  524. }