extprepro.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <structures.h>
  25. #include <tkglobals.h>
  26. #include <globcon.h>
  27. #include <processno.h>
  28. #include <modeno.h>
  29. #include <arrays.h>
  30. #include <extend.h>
  31. #include <sfsys.h>
  32. #include <osbind.h>
  33. //#if defined unix
  34. #define round(x) lround((x))
  35. //#endif
  36. #define RANDSET (32) /* reduce no.of segs to small finite no, so start and end seg have chance to be chosen */
  37. #define FORWARDS (1)
  38. #define BACKWARDS (-1)
  39. static int setup_zigzag_splice(int paramno,dataptr dz);
  40. static int make_zigsplice(int paramno,dataptr dz);
  41. static int create_zigzag_splicebuffer(dataptr dz);
  42. static int generate_zigzag_table(dataptr dz);
  43. static int sort_zigs(dataptr dz);
  44. static int eliminate_n_steps(int *this_zigtime,int *next_zigtime,int **ziglistend,int *cnt,dataptr dz);
  45. static int eliminate_step(int *next_zigtime,int **ziglistend,int *cnt,dataptr dz);
  46. static int insert_extra_zig
  47. (int direction,int **this_zigtime,int **next_zigtime,int **ziglistend,int len,dataptr dz);
  48. static int split_up_steps_too_large_for_buffer(dataptr dz);
  49. static int update_and_check_ziglist_params(int **next_zigtime,int **this_zigtime,int *ziglistend,int direction);
  50. static int generate_loop_table(dataptr dz);
  51. static int sort_loops(dataptr dz);
  52. static int insert_extra_loop_element
  53. (int direction,int **this_zigtime,int **next_zigtime,int **ziglistend,int len,dataptr dz);
  54. static int generate_scramble_table(dataptr dz);
  55. static int scramble_rand(int arraysize,dataptr dz);
  56. static int preprocess_scramble_shred(dataptr dz);
  57. static int scramble_shred(int arraysize,dataptr dz);
  58. static int get_basis_lengths(int chunkcnt,int worklen,int *unit_len, int *endlen,
  59. int *scatgrpcnt, int *endscat, int *range, int *endrange, dataptr dz);
  60. static int normal_scat(int unit_len,int endlen,int chunkcnt_less_one,dataptr dz);
  61. static int heavy_scat(int scatgrpcnt,int range,int endscat,int endrange,dataptr dz);
  62. static void permute_chunks(int chunkcnt,dataptr dz);
  63. static void insert(int n,int t,int chunkcnt,dataptr dz);
  64. static void prefix(int n,int chunkcnt,dataptr dz);
  65. static void shuflup(int k,int chunkcnt,dataptr dz);
  66. static int ptr_sort(int end,dataptr dz);
  67. static int get_maxvalue_of_rand(double *maxrand,dataptr dz);
  68. static int get_maxvalue_of_pscat(double *maxpscat,dataptr dz);
  69. static int get_minvalue_of_delay(double *mindelay,dataptr dz);
  70. static void reverse_fadevals(dataptr dz);
  71. static void set_default_gain(int mindelay_samps,dataptr dz);
  72. static void set_default_delays(dataptr dz);
  73. static void setup_iter_process_type(int is_unity_gain,dataptr dz);
  74. static int make_drnk_splicetab(dataptr dz);
  75. static int convert_sec_steps_to_grain_steps(dataptr dz);
  76. /***************************** ZIGZAG_PREPROCESS ******************************/
  77. int zigzag_preprocess(dataptr dz)
  78. {
  79. int exit_status;
  80. int n = 0;
  81. if(dz->mode == ZIGZAG_SELF)
  82. initialise_random_sequence(IS_ZIG_RSEED,ZIGZAG_RSEED,dz);
  83. if((exit_status = setup_zigzag_splice(ZIGZAG_SPLEN,dz))<0)
  84. return(exit_status);
  85. if(dz->insams[0] <= dz->iparam[ZIG_SPLSAMPS] * dz->infile->channels * 2) {
  86. sprintf(errstr,"Infile too short for splices.\n");
  87. return(DATA_ERROR);
  88. }
  89. if(dz->mode == ZIGZAG_SELF) {
  90. if((exit_status = generate_zigzag_table(dz))<0)
  91. return(exit_status);
  92. }
  93. if((exit_status = sort_zigs(dz))<0)
  94. return(exit_status);
  95. if(sloom) {
  96. dz->tempsize = 0L;
  97. for(n=1;n<dz->itemcnt;n++)
  98. /*RWD treat tempszie as in samps */
  99. dz->tempsize += abs(dz->lparray[ZIGZAG_TIMES][n] - dz->lparray[ZIGZAG_TIMES][n-1]);
  100. }
  101. return(FINISHED);
  102. }
  103. /*********************** SETUP_ZIGZAG_SPLICE ***************************/
  104. int setup_zigzag_splice(int paramno,dataptr dz)
  105. {
  106. int exit_status;
  107. if((exit_status = make_zigsplice(paramno,dz))<0)
  108. return(exit_status);
  109. return create_zigzag_splicebuffer(dz);
  110. }
  111. /*********************** MAKE_ZIGSPLICE ***************************/
  112. int make_zigsplice(int paramno,dataptr dz)
  113. {
  114. int n;
  115. int framesize = dz->infile->channels;
  116. dz->iparam[ZIG_SPLICECNT] = (int)round(dz->param[paramno] * MS_TO_SECS * dz->infile->srate);
  117. dz->iparam[ZIG_SPLSAMPS] = dz->iparam[ZIG_SPLICECNT] * framesize;
  118. if(dz->iparam[ZIG_SPLSAMPS] >= dz->buflen) {
  119. sprintf(errstr,"Splicelength too long for available memory.\n");
  120. return(GOAL_FAILED);
  121. }
  122. if((dz->parray[ZIGZAG_SPLICE] = (double *)malloc(dz->iparam[ZIG_SPLICECNT] * sizeof(double)))==NULL) {
  123. sprintf(errstr,"INSUFFICIENT MEMORY to make splicer buffer.\n");
  124. return(MEMORY_ERROR);
  125. }
  126. for(n=0;n<dz->iparam[ZIG_SPLICECNT];n++)
  127. dz->parray[ZIGZAG_SPLICE][n] = (double)n/(double)dz->iparam[ZIG_SPLICECNT];
  128. return(FINISHED);
  129. }
  130. /*********************** CREATE_ZIGZAG_SPLICEBUFFER ***************************/
  131. int create_zigzag_splicebuffer(dataptr dz)
  132. {
  133. if(dz->extrabuf == (float **)0) {
  134. sprintf(errstr,"extrabuf has not been created: create_zigzag_splicebuffer()\n");
  135. return(PROGRAM_ERROR);
  136. }
  137. if((dz->extrabuf[ZIGZAG_SPLBUF] = (float *)malloc(sizeof(float) * dz->iparam[ZIG_SPLSAMPS]))==NULL) {
  138. sprintf(errstr,"INSUFFICIENT MEMORY to make splicing buffer.\n");
  139. return(MEMORY_ERROR);
  140. }
  141. memset((char *)dz->extrabuf[ZIGZAG_SPLBUF],0,sizeof(float) * dz->iparam[ZIG_SPLSAMPS]);
  142. return(FINISHED);
  143. }
  144. /***************************** GENERATE_ZIGZAG_TABLE ***************************/
  145. int generate_zigzag_table(dataptr dz)
  146. {
  147. int OK;
  148. int arraysize = BIGARRAY;
  149. double infiledur = (double)(dz->insams[0]/dz->infile->channels)/(double)(dz->infile->srate);
  150. double totaltime = dz->param[ZIGZAG_START];
  151. double goaltime = dz->param[ZIGZAG_DUR] - (infiledur - dz->param[ZIGZAG_END]);
  152. double diff, randlen = 0.0, here = dz->param[ZIGZAG_START];
  153. int direction = FORWARDS;
  154. if((dz->lparray[ZIGZAG_TIMES] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  155. sprintf(errstr,"INSUFFICIENT MEMORY to store times.\n");
  156. return(MEMORY_ERROR);
  157. }
  158. dz->lparray[ZIGZAG_TIMES][0] = 0;
  159. dz->itemcnt = 1;
  160. do {
  161. OK = TRUE;
  162. switch(direction) {
  163. case(FORWARDS):
  164. diff = min(dz->param[ZIGZAG_MAX],dz->param[ZIGZAG_END] - here);
  165. if(diff<=dz->param[ZIGZAG_MIN])
  166. OK = FALSE;
  167. else {
  168. randlen = drand48(); /* generate segment length at random */
  169. randlen *= (diff - dz->param[ZIGZAG_MIN]); /* scale it to range required */
  170. randlen += dz->param[ZIGZAG_MIN]; /* and add mindur */
  171. here = (here + randlen);
  172. }
  173. break;
  174. case(BACKWARDS):
  175. diff = min(dz->param[ZIGZAG_MAX],here - dz->param[ZIGZAG_START]);
  176. if(diff<=dz->param[ZIGZAG_MIN])
  177. OK = FALSE;
  178. else {
  179. randlen = drand48(); /* generate segment length at random */
  180. randlen *= (diff - dz->param[ZIGZAG_MIN]); /* scale it to range required */
  181. randlen += dz->param[ZIGZAG_MIN]; /* and add mindur */
  182. here = (here - randlen);
  183. }
  184. break;
  185. }
  186. direction = -direction; /* invert time-direction */
  187. if(!OK)
  188. continue;
  189. totaltime += randlen;
  190. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = round(here * (double)dz->infile->srate) * dz->infile->channels;
  191. if(++dz->itemcnt >= arraysize) {
  192. arraysize += BIGARRAY;
  193. if((dz->lparray[ZIGZAG_TIMES] =
  194. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],arraysize*sizeof(int)))==NULL) {
  195. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  196. return(MEMORY_ERROR);
  197. }
  198. }
  199. } while(totaltime<goaltime);
  200. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = dz->insams[0];
  201. dz->itemcnt++;
  202. if(dz->itemcnt < arraysize) {
  203. if((dz->lparray[ZIGZAG_TIMES] =
  204. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt*sizeof(int)))==NULL) {
  205. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  206. return(MEMORY_ERROR);
  207. }
  208. }
  209. return(FINISHED);
  210. }
  211. /****************************** SORT_ZIGS ************************************/
  212. int sort_zigs(dataptr dz)
  213. {
  214. int exit_status;
  215. int zigsize;
  216. int safety = round(ZIG_MIN_UNSPLICED * MS_TO_SECS * (double)dz->infile->srate) * dz->infile->channels;
  217. int cnt = 0, firstime = TRUE, direction, previous_direction = FORWARDS;
  218. int *this_zigtime = dz->lparray[ZIGZAG_TIMES];
  219. int *next_zigtime = dz->lparray[ZIGZAG_TIMES] + 1;
  220. int *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  221. int minzig = (dz->iparam[ZIG_SPLSAMPS] * 2) + safety;
  222. int file_samplen = dz->insams[0];
  223. double convert_to_time = 1.0/(double)dz->infile->channels/(double)dz->infile->srate;
  224. if(minzig > (dz->buflen/2)-1) {
  225. sprintf(errstr,"splicelen too long to work with available memory\n"
  226. "Longest splicelen available %ld msecs.\n",
  227. round((double)(((((dz->buflen/2)-1) - safety)/2)/(double)dz->infile->channels/(double)dz->infile->srate) * SECS_TO_MS)-1);
  228. return(GOAL_FAILED);
  229. }
  230. if(*this_zigtime < 0 || *this_zigtime > file_samplen) {
  231. sprintf(errstr,"Invalid 1st zigtime %lf\n",(*this_zigtime) * convert_to_time);
  232. return(DATA_ERROR);
  233. }
  234. if(*(ziglistend-1) >= file_samplen) {
  235. *(ziglistend-1) = file_samplen;
  236. dz->iparam[ZIG_RUNSTOEND] = 1;
  237. } else
  238. dz->iparam[ZIG_RUNSTOEND] = 0;
  239. while(next_zigtime < ziglistend - dz->iparam[ZIG_RUNSTOEND]) {
  240. if(*next_zigtime < 0 || *next_zigtime > file_samplen) {
  241. sprintf(errstr,"Invalid zigtime %lf\n",(*next_zigtime) * convert_to_time);
  242. return(DATA_ERROR);
  243. }
  244. while((zigsize = abs(*next_zigtime - *this_zigtime)) < minzig) {
  245. if(++next_zigtime == ziglistend - dz->iparam[ZIG_RUNSTOEND])
  246. break;
  247. }
  248. if(next_zigtime - this_zigtime > 1) {
  249. if(dz->mode == ZIGZAG_USER) {
  250. sprintf(errstr,"Some zigs too short to use with specified splicelen.\n");
  251. return(DATA_ERROR);
  252. }
  253. eliminate_n_steps(this_zigtime,next_zigtime,&ziglistend,&cnt,dz);
  254. next_zigtime = this_zigtime + 1;
  255. }
  256. if(*next_zigtime > *this_zigtime)
  257. direction = FORWARDS;
  258. else
  259. direction = BACKWARDS;
  260. if(!firstime && (direction == previous_direction)) {
  261. if((exit_status = eliminate_step(next_zigtime,&ziglistend,&cnt,dz))<0)
  262. return(exit_status);
  263. continue;
  264. }
  265. previous_direction = direction;
  266. firstime = FALSE;
  267. this_zigtime++;
  268. next_zigtime++;
  269. }
  270. if(cnt>0) {
  271. fprintf(stdout,"WARNING: %d steps eliminated (too small relative to spliclength\n",cnt);
  272. //TW : CAN'T SPLIT LINES SENT TO SLOOM - 'WARNING' is a flag to SLOOM - possibly my error, since updated
  273. fprintf(stdout,"WARNING: or moving in same direction as previous step)\n");
  274. fflush(stdout);
  275. if((dz->lparray[ZIGZAG_TIMES] =
  276. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  277. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  278. return(MEMORY_ERROR);
  279. }
  280. }
  281. if(dz->iparam[ZIG_RUNSTOEND]) {
  282. if(*(ziglistend-1) - *(ziglistend-2) < dz->iparam[ZIG_SPLSAMPS] + safety) {
  283. sprintf(errstr,"Final zig too short for splicelen.\n");
  284. return(GOAL_FAILED);
  285. }
  286. }
  287. return split_up_steps_too_large_for_buffer(dz);
  288. }
  289. /**************************** ELIMINATE_N_STEPS ***************************/
  290. int eliminate_n_steps(int *this_zigtime,int *next_zigtime,int **ziglistend,int *cnt,dataptr dz)
  291. {
  292. int *here = this_zigtime + 1;
  293. int *there = next_zigtime;
  294. int elimination_cnt = (int)( next_zigtime - this_zigtime - 1);
  295. while(there < *ziglistend) {
  296. *here = *there;
  297. here++;
  298. there++;
  299. }
  300. if((dz->itemcnt -= elimination_cnt) < 2) {
  301. sprintf(errstr,"All zigsteps either too small for splices: or moving in same direction.\n");
  302. return(DATA_ERROR);
  303. }
  304. *ziglistend -= elimination_cnt;
  305. (*cnt) += elimination_cnt;
  306. return(FINISHED);
  307. }
  308. /***************************** ELIMINATE_STEP ***************************/
  309. int eliminate_step(int *next_zigtime,int **ziglistend,int *cnt,dataptr dz)
  310. {
  311. int *here = next_zigtime;
  312. while(here < *ziglistend) {
  313. *(here-1) = *here;
  314. here++;
  315. }
  316. if(--dz->itemcnt < 2) {
  317. sprintf(errstr,"All zigsteps either too small for splices: or moving in same direction.\n");
  318. return(DATA_ERROR);
  319. }
  320. (*ziglistend)--;
  321. (*cnt)++;
  322. return(FINISHED);
  323. }
  324. /***************************** INSERT_EXTRA_ZIG ***************************/
  325. int insert_extra_zig(int direction,int **this_zigtime,int **next_zigtime,int **ziglistend,int len,dataptr dz)
  326. {
  327. int *here;
  328. int zthis = *this_zigtime - dz->lparray[ZIGZAG_TIMES];
  329. int znext = *next_zigtime - dz->lparray[ZIGZAG_TIMES];
  330. dz->itemcnt++;
  331. if((dz->lparray[ZIGZAG_TIMES] =
  332. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  333. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  334. return(MEMORY_ERROR);
  335. }
  336. *this_zigtime = dz->lparray[ZIGZAG_TIMES] + zthis;
  337. *next_zigtime = dz->lparray[ZIGZAG_TIMES] + znext;
  338. *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  339. here = *ziglistend - 1;
  340. while(here > *next_zigtime) {
  341. *here = *(here-1);
  342. here--;
  343. }
  344. switch(direction) {
  345. case(FORWARDS):
  346. *here = **this_zigtime + len;
  347. break;
  348. case(BACKWARDS):
  349. *here = **this_zigtime - len;
  350. break;
  351. }
  352. if(*here < 0.0 || (int)*here > dz->insams[0]) {
  353. sprintf(errstr,"Error in logic of sample arithmetic: insert_extra_zig()\n");
  354. return(PROGRAM_ERROR);
  355. }
  356. return(FINISHED);
  357. }
  358. /**************************** SPLIT_UP_STEPS_TOO_LARGE_FOR_BUFFER ***************************/
  359. int split_up_steps_too_large_for_buffer(dataptr dz)
  360. {
  361. int zigsize;
  362. int direction, n, longbufs, shortbufs;
  363. int safety = round(ZIG_MIN_UNSPLICED * MS_TO_SECS * (double)dz->infile->srate) * dz->infile->channels;
  364. int minzig = (dz->iparam[ZIG_SPLSAMPS] * 2) + safety;
  365. int *this_zigtime = dz->lparray[ZIGZAG_TIMES];
  366. int *next_zigtime = this_zigtime + 1;
  367. int *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  368. int max_effective_length = dz->buflen - F_SECSIZE;
  369. int pre_endbuflen, endbuflen;
  370. while(next_zigtime < ziglistend) {
  371. zigsize = *next_zigtime - *this_zigtime;
  372. if(zigsize > 0.0)
  373. direction = FORWARDS;
  374. else
  375. direction = BACKWARDS;
  376. zigsize = abs(zigsize);
  377. if((longbufs = zigsize/max_effective_length) > 0) { /* TRUNCATE */
  378. shortbufs = -1;
  379. pre_endbuflen = 0;
  380. while((endbuflen = zigsize%max_effective_length) > 0 && endbuflen < minzig) {
  381. longbufs--;
  382. if(longbufs<0) {
  383. sprintf(errstr,"splicelen is too long for available memory.\n");
  384. return(GOAL_FAILED);
  385. }
  386. shortbufs++;
  387. pre_endbuflen = max_effective_length + endbuflen - minzig;
  388. if(pre_endbuflen > minzig)
  389. break;
  390. zigsize -= minzig;
  391. }
  392. for(n = 0; n < longbufs;n++) {
  393. insert_extra_zig(direction,&this_zigtime,&next_zigtime,&ziglistend,max_effective_length,dz);
  394. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  395. }
  396. if(pre_endbuflen > 0) {
  397. insert_extra_zig(direction,&this_zigtime,&next_zigtime,&ziglistend,pre_endbuflen,dz);
  398. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  399. }
  400. if(shortbufs > 0) {
  401. for(n = 0; n < shortbufs; n++) {
  402. insert_extra_zig(direction,&this_zigtime,&next_zigtime,&ziglistend,minzig,dz);
  403. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  404. }
  405. }
  406. }
  407. next_zigtime++;
  408. this_zigtime++;
  409. }
  410. return(FINISHED);
  411. }
  412. /***************************** UPDATE_AND_CHECK_ZIGLIST_PARAMS ***************************/
  413. int update_and_check_ziglist_params(int **next_zigtime,int **this_zigtime,int *ziglistend,int direction)
  414. {
  415. int zigsize;
  416. (*next_zigtime)++;
  417. (*this_zigtime)++;
  418. if(*this_zigtime >= ziglistend - 1) {
  419. sprintf(errstr,"Error in counting logic: update_and_check_ziglist_params()\n");
  420. return(PROGRAM_ERROR);
  421. }
  422. zigsize = **next_zigtime - **this_zigtime;
  423. if((zigsize > 0.0 && direction != FORWARDS)
  424. || (zigsize < 0.0 && direction != BACKWARDS)) {
  425. sprintf(errstr,"Error in counting logic(2): update_and_check_ziglist_params()\n");
  426. return(PROGRAM_ERROR);
  427. }
  428. return(FINISHED);
  429. }
  430. /*********************** LOOP_PREPROCESS ***************************/
  431. int loop_preprocess(dataptr dz)
  432. {
  433. int exit_status;
  434. initrand48();
  435. if(dz->param[LOOP_LEN] <= dz->param[LOOP_SPLEN] * 2) {
  436. sprintf(errstr,"Loop length too short for splices.\n");
  437. return(DATA_ERROR);
  438. }
  439. if((exit_status = setup_zigzag_splice(LOOP_SPLEN,dz))<0)
  440. return(exit_status);
  441. if(dz->insams[0] <= dz->iparam[ZIG_SPLSAMPS] * 2) {
  442. sprintf(errstr,"Infile too short for splices.\n");
  443. return(DATA_ERROR);
  444. }
  445. if((exit_status = generate_loop_table(dz))<0)
  446. return(exit_status);
  447. return sort_loops(dz);
  448. }
  449. /***************************** GENERATE_LOOP_TABLE ***************************/
  450. int generate_loop_table(dataptr dz)
  451. {
  452. int searchsamps, tmpstart;
  453. unsigned int file_endsamp = dz->insams[0];
  454. int i, arraysize = BIGARRAY;
  455. int startsamp = dz->infile->channels * (int) (dz->infile->srate * dz->param[LOOP_START]);
  456. int looplen = dz->infile->channels * (int) (dz->infile->srate * dz->param[LOOP_LEN] * MS_TO_SECS);
  457. int stepsamps = dz->infile->channels * (int) (dz->infile->srate * dz->param[LOOP_STEP] * MS_TO_SECS);
  458. int lsfield = dz->infile->channels * (int) (dz->infile->srate * dz->param[LOOP_SRCHF] * MS_TO_SECS);
  459. int samplen, excess, totallen, endsamp, n;
  460. /* NEW APR 16 --> */
  461. looplen += dz->iparam[ZIG_SPLSAMPS]; /* allow for splice overlaps in calcs */
  462. dz->itemcnt = 0;
  463. if((dz->lparray[ZIGZAG_TIMES] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  464. sprintf(errstr,"INSUFFICIENT MEMORY to store times.\n");
  465. return(MEMORY_ERROR);
  466. }
  467. if((dz->iparray[ZIGZAG_PLAY] = (int *)malloc(arraysize * sizeof(int )))==NULL) {
  468. sprintf(errstr,"INSUFFICIENT MEMORY to store section markers.\n");
  469. return(MEMORY_ERROR);
  470. }
  471. searchsamps = 0;
  472. tmpstart = startsamp;
  473. for(i = 0; i < dz->iparam[LOOP_REPETS] ;i++) {
  474. if(dz->itemcnt + 2 > arraysize) {
  475. arraysize += BIGARRAY;
  476. if((dz->lparray[ZIGZAG_TIMES] = (int *)realloc(dz->lparray[ZIGZAG_TIMES],arraysize * sizeof(int)))==NULL) {
  477. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times.\n");
  478. return(MEMORY_ERROR);
  479. }
  480. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY],arraysize * sizeof(int)))==NULL) {
  481. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers.\n");
  482. return(MEMORY_ERROR);
  483. }
  484. }
  485. dz->iparray[ZIGZAG_PLAY][dz->itemcnt] = TRUE;
  486. if(i==0 && dz->vflag[IS_KEEP_START])
  487. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = 0;
  488. else
  489. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = tmpstart;
  490. dz->itemcnt++;
  491. dz->iparray[ZIGZAG_PLAY][dz->itemcnt] = FALSE;
  492. if((unsigned int)(dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = tmpstart + looplen) >= file_endsamp) {
  493. dz->iparam[ZIG_RUNSTOEND] = TRUE;
  494. dz->itemcnt++;
  495. break;
  496. }
  497. dz->itemcnt++;
  498. startsamp += stepsamps;
  499. if(lsfield)
  500. searchsamps = round((double) lsfield * drand48());
  501. tmpstart = (startsamp + searchsamps);
  502. }
  503. if(dz->iparam[ZIG_RUNSTOEND])
  504. dz->lparray[ZIGZAG_TIMES][dz->itemcnt-1] = file_endsamp; /* Force read to end */
  505. else if(dz->mode==LOOP_OUTLEN) {
  506. samplen = dz->infile->channels * round(dz->infile->srate * dz->param[LOOP_OUTDUR]);
  507. totallen = 0; /* Force to correct outlen */
  508. for(n=1;n<dz->itemcnt;n+=2)
  509. totallen += dz->lparray[ZIGZAG_TIMES][n] - dz->lparray[ZIGZAG_TIMES][n-1] - dz->iparam[ZIG_SPLSAMPS];
  510. if((excess = samplen - totallen) > 0) {
  511. endsamp = min(dz->lparray[ZIGZAG_TIMES][dz->itemcnt-1] + excess,(int)file_endsamp);
  512. dz->lparray[ZIGZAG_TIMES][dz->itemcnt-1] = endsamp;
  513. if(endsamp == (int)file_endsamp)
  514. dz->iparam[ZIG_RUNSTOEND] = TRUE;
  515. }
  516. }
  517. if((dz->lparray[ZIGZAG_TIMES] = (int *)realloc(dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  518. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times.\n");
  519. return(MEMORY_ERROR);
  520. }
  521. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY], dz->itemcnt * sizeof(int )))==NULL) {
  522. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers.\n");
  523. return(MEMORY_ERROR);
  524. }
  525. return(FINISHED);
  526. }
  527. /****************************** SORT_LOOPS ************************************/
  528. int sort_loops(dataptr dz)
  529. {
  530. int zigsize;
  531. int direction, n, longbufs, shortbufs;
  532. int safety = round(ZIG_MIN_UNSPLICED * MS_TO_SECS * (double)dz->infile->srate) * dz->infile->channels;
  533. int minzig = (dz->iparam[ZIG_SPLSAMPS] * 2) + safety;
  534. int *this_zigtime = dz->lparray[ZIGZAG_TIMES];
  535. int *next_zigtime = this_zigtime + 1;
  536. int *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  537. int max_effective_length = dz->buflen - F_SECSIZE;
  538. int pre_endbuflen, endbuflen;
  539. int m = 0;
  540. while(next_zigtime < ziglistend) {
  541. zigsize = *next_zigtime - *this_zigtime;
  542. if(zigsize > 0.0)
  543. direction = FORWARDS;
  544. else
  545. direction = BACKWARDS;
  546. zigsize = abs(zigsize);
  547. if((longbufs = zigsize/max_effective_length) > 0) { /* TRUNCATE */
  548. shortbufs = -1;
  549. pre_endbuflen = 0;
  550. while((endbuflen = zigsize%max_effective_length) > 0 && endbuflen < minzig) {
  551. longbufs--;
  552. if(longbufs<0) {
  553. sprintf(errstr,"splicelen is too long for available memory.\n");
  554. return(GOAL_FAILED);
  555. }
  556. shortbufs++;
  557. pre_endbuflen = max_effective_length + endbuflen - minzig;
  558. if(pre_endbuflen > minzig)
  559. break;
  560. zigsize -= minzig;
  561. }
  562. for(n = 0; n < longbufs;n++) {
  563. insert_extra_loop_element(direction,&this_zigtime,&next_zigtime,&ziglistend,max_effective_length,dz);
  564. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  565. }
  566. if(pre_endbuflen > 0) {
  567. insert_extra_loop_element(direction,&this_zigtime,&next_zigtime,&ziglistend,pre_endbuflen,dz);
  568. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  569. }
  570. if(shortbufs > 0) {
  571. for(n = 0; n < shortbufs; n++) {
  572. insert_extra_loop_element(direction,&this_zigtime,&next_zigtime,&ziglistend,minzig,dz);
  573. update_and_check_ziglist_params(&next_zigtime,&this_zigtime,ziglistend,direction);
  574. }
  575. }
  576. }
  577. next_zigtime++;
  578. this_zigtime++;
  579. }
  580. if(sloom) {
  581. dz->tempsize = 0L;
  582. for(n=0,m=1;m<dz->itemcnt;n++,m++) {
  583. if(dz->iparray[ZIGZAG_PLAY][n])
  584. dz->tempsize += abs(dz->lparray[ZIGZAG_TIMES][m] - dz->lparray[ZIGZAG_TIMES][n]);
  585. } /* abs for SAFETY: should be unnecessary as all plays should be forwards!! */
  586. }
  587. return(FINISHED);
  588. }
  589. /***************************** INSERT_EXTRA_LOOP_ELEMENT ***************************/
  590. int insert_extra_loop_element(int direction,int **this_zigtime,int **next_zigtime,int **ziglistend,int len,dataptr dz)
  591. {
  592. int *here;
  593. int *there, *playlistend, *nextplaytime;
  594. int zthis = *this_zigtime - dz->lparray[ZIGZAG_TIMES];
  595. int znext = *next_zigtime - dz->lparray[ZIGZAG_TIMES];
  596. dz->itemcnt++;
  597. if((dz->lparray[ZIGZAG_TIMES] =
  598. (int *)realloc(dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  599. sprintf(errstr,"INSUFFICIENT MEMORY to insert extra loop time.\n");
  600. return(MEMORY_ERROR);
  601. }
  602. if((dz->iparray[ZIGZAG_PLAY] =
  603. (int *)realloc(dz->iparray[ZIGZAG_PLAY], dz->itemcnt * sizeof(int )))==NULL) {
  604. sprintf(errstr,"INSUFFICIENT MEMORY to insert extra loopmarker.\n");
  605. return(MEMORY_ERROR);
  606. }
  607. *this_zigtime = dz->lparray[ZIGZAG_TIMES] + zthis;
  608. *next_zigtime = dz->lparray[ZIGZAG_TIMES] + znext;
  609. *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  610. here = *ziglistend - 1;
  611. while(here > *next_zigtime) {
  612. *here = *(here-1);
  613. here--;
  614. }
  615. nextplaytime = dz->iparray[ZIGZAG_PLAY] + (*next_zigtime - dz->lparray[ZIGZAG_TIMES]);
  616. playlistend = dz->iparray[ZIGZAG_PLAY] + dz->itemcnt;
  617. there = playlistend - 1;
  618. while(there > nextplaytime) {
  619. *there = *(there-1);
  620. there--;
  621. }
  622. switch(dz->process) {
  623. case(LOOP): /* COMMENT: this old LOOPcode may be unnecessary, as SCRAMBLE version may be OK for both */
  624. switch(direction) {
  625. case(FORWARDS): *here = **this_zigtime + len; *there = TRUE; break;
  626. case(BACKWARDS): *here = **this_zigtime - len; *there = FALSE; break;
  627. }
  628. break;
  629. case(SCRAMBLE):
  630. switch(direction) {
  631. case(FORWARDS): *here = **this_zigtime + len; break;
  632. case(BACKWARDS): *here = **this_zigtime - len; break;
  633. }
  634. *there = *(there-1);
  635. break;
  636. }
  637. // if(*here < 0.0 || (unsigned int)*here > dz->insams[0]) {
  638. if(*here < 0.0 || (int)*here > dz->insams[0]) {
  639. sprintf(errstr,"Error in logic of sample arithmetic: insert_extra_loop_element()\n");
  640. return(PROGRAM_ERROR);
  641. }
  642. return(FINISHED);
  643. }
  644. /***************************** SCRAMBLE_PREPROCESS ***************************/
  645. int scramble_preprocess(dataptr dz)
  646. {
  647. int exit_status;
  648. initialise_random_sequence(IS_SCRAMBLE_RSEED,SCRAMBLE_SEED,dz);
  649. if((exit_status = setup_zigzag_splice(SCRAMBLE_SPLEN,dz))<0)
  650. return(exit_status);
  651. if(dz->insams[0] <= dz->iparam[ZIG_SPLSAMPS] * dz->infile->channels * 2) {
  652. sprintf(errstr,"Infile too short for splices.\n");
  653. return(DATA_ERROR);
  654. }
  655. if((exit_status = generate_scramble_table(dz))<0)
  656. return(exit_status);
  657. return sort_loops(dz);
  658. }
  659. /***************************** GENERATE_SCRAMBLE_TABLE **************************/
  660. int generate_scramble_table(dataptr dz)
  661. {
  662. int exit_status;
  663. int arraysize = BIGARRAY;
  664. if((dz->lparray[ZIGZAG_TIMES] = (int *)malloc(arraysize*sizeof(int)))==NULL) {
  665. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  666. return(MEMORY_ERROR);
  667. }
  668. if((dz->iparray[ZIGZAG_PLAY] = (int *)malloc(arraysize * sizeof(int )))==NULL) {
  669. sprintf(errstr,"INSUFFICIENT MEMORY for markers store.\n");
  670. return(MEMORY_ERROR);
  671. }
  672. switch(dz->mode) {
  673. case(SCRAMBLE_RAND):
  674. exit_status = scramble_rand(arraysize,dz);
  675. break;
  676. case(SCRAMBLE_SHRED):
  677. if((exit_status = preprocess_scramble_shred(dz))<0)
  678. return(exit_status);
  679. exit_status = scramble_shred(arraysize,dz);
  680. break;
  681. default:
  682. sprintf(errstr,"Unknown mode in generate_scramble_table()\n");
  683. return(PROGRAM_ERROR);
  684. }
  685. if(dz->vflag[IS_SCR_KEEP_START])
  686. dz->lparray[ZIGZAG_TIMES][0] = 0;
  687. if(dz->vflag[IS_SCR_KEEP_END])
  688. dz->lparray[ZIGZAG_TIMES][dz->itemcnt-1] = dz->insams[0];
  689. return(exit_status);
  690. }
  691. /***************************** SCRAMBLE_RAND **************************/
  692. int scramble_rand(int arraysize,dataptr dz)
  693. {
  694. int lenrange = round((dz->param[SCRAMBLE_MAX] - dz->param[SCRAMBLE_MIN]) * dz->infile->srate);
  695. int minlen = round(dz->param[SCRAMBLE_MIN] * dz->infile->srate);
  696. int infilelen = dz->insams[0];
  697. int thislen, outlen = 0, effective_range, thispos;
  698. int finished = 0, zz;
  699. double thisfrac;
  700. dz->iparam[SCRAMBLE_OUTLEN] = round(dz->param[SCRAMBLE_DUR] * dz->infile->srate) * dz->infile->channels;
  701. dz->itemcnt = 0;
  702. while(!finished) {
  703. if(dz->itemcnt + 2 >= arraysize) {
  704. arraysize += BIGARRAY;
  705. if((dz->lparray[ZIGZAG_TIMES] =(int *)realloc(dz->lparray[ZIGZAG_TIMES],arraysize*sizeof(int)))==NULL) {
  706. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  707. return(MEMORY_ERROR);
  708. }
  709. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY], arraysize*sizeof(int )))==NULL) {
  710. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers store.\n");
  711. return(MEMORY_ERROR);
  712. }
  713. }
  714. thislen = (round(drand48() * lenrange) + minlen) * dz->infile->channels;
  715. effective_range = infilelen - dz->iparam[ZIG_SPLSAMPS] - thislen;
  716. zz = round(drand48() * RANDSET);
  717. thisfrac = (double)zz/(double)RANDSET;
  718. thispos = round(thisfrac * effective_range);
  719. if(ODD(thispos))
  720. thispos--;
  721. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = thispos;
  722. dz->iparray[ZIGZAG_PLAY][dz->itemcnt++] = TRUE;
  723. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = thispos + thislen + dz->iparam[ZIG_SPLSAMPS];
  724. dz->iparray[ZIGZAG_PLAY][dz->itemcnt++] = FALSE;
  725. if((outlen += thislen) >= dz->iparam[SCRAMBLE_OUTLEN])
  726. finished = 1;
  727. }
  728. if((dz->lparray[ZIGZAG_TIMES] = (int *)realloc(dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  729. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  730. return(MEMORY_ERROR);
  731. }
  732. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY], dz->itemcnt * sizeof(int )))==NULL) {
  733. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers store.\n");
  734. return(MEMORY_ERROR);
  735. }
  736. return(FINISHED);
  737. }
  738. /***************************** PREPROCESS_SCRAMBLE_SHRED **************************/
  739. int preprocess_scramble_shred(dataptr dz)
  740. {
  741. // double infiledur;
  742. int chunksamps;
  743. //infiledur = (double)(dz->insams[0]/dz->infile->channels)/(double)(dz->infile->srate);
  744. chunksamps = round(dz->param[SCRAMBLE_LEN] * dz->infile->srate) * dz->infile->channels;
  745. dz->iparam[SCRAMBLE_CHCNT] = dz->insams[0]/chunksamps; /* truncate */
  746. if(dz->iparam[SCRAMBLE_CHCNT]<2) {
  747. sprintf(errstr,"Too long chunklen not trapped earlier.\n");
  748. return(PROGRAM_ERROR);
  749. }
  750. dz->iparam[SCRAMBLE_OUTLEN] = round(dz->param[SCRAMBLE_DUR] * dz->infile->srate) * dz->infile->channels;
  751. if((dz->lparray[SCRAMBLE_CHUNKPTR] = (int *)malloc(dz->iparam[SCRAMBLE_CHCNT] * sizeof(int)))==NULL) {
  752. sprintf(errstr,"INSUFFICIENT MEMORY to store scramble chunk pointers.\n");
  753. return(MEMORY_ERROR);
  754. }
  755. if((dz->lparray[SCRAMBLE_CHUNKLEN] = (int *)malloc(dz->iparam[SCRAMBLE_CHCNT] * sizeof(int)))==NULL) {
  756. sprintf(errstr,"INSUFFICIENT MEMORY to store scramble chunk lengths.\n");
  757. return(MEMORY_ERROR);
  758. }
  759. if((dz->iparray[SCRAMBLE_PERM] = (int *)malloc(dz->iparam[SCRAMBLE_CHCNT] * sizeof(int )))==NULL) {
  760. sprintf(errstr,"INSUFFICIENT MEMORY to store scramble permutation.\n");
  761. return(MEMORY_ERROR);
  762. }
  763. return(FINISHED);
  764. }
  765. /******************************* SCRAMBLE_SHRED ***************************/
  766. int scramble_shred(int arraysize,dataptr dz)
  767. {
  768. int n, exit_status;
  769. int finished = 0;
  770. int chunkcnt = dz->iparam[SCRAMBLE_CHCNT];
  771. int worklen = dz->insams[0];
  772. int total_len = 0;
  773. int chunkcnt_less_one = chunkcnt - 1;
  774. int unit_len, endlen, scatgrpcnt=0, endscat=0, range=0, endrange=0;
  775. int *perm = dz->iparray[SCRAMBLE_PERM];
  776. dz->itemcnt = 0;
  777. if((exit_status = get_basis_lengths(chunkcnt,worklen,&unit_len,&endlen,&scatgrpcnt,&endscat,&range,&endrange,dz))<0)
  778. return(exit_status);
  779. while(!finished) {
  780. if(dz->iparam[SCRAMBLE_SCAT]) {
  781. if((exit_status = heavy_scat(scatgrpcnt,range,endscat,endrange,dz))<0)
  782. return(exit_status);
  783. } else {
  784. if((exit_status = normal_scat(unit_len,endlen,chunkcnt_less_one,dz))<0)
  785. return(exit_status);
  786. }
  787. for(n=0;n<chunkcnt_less_one;n++) /* 2 */
  788. dz->lparray[SCRAMBLE_CHUNKLEN][n] = dz->lparray[SCRAMBLE_CHUNKPTR][n+1] - dz->lparray[SCRAMBLE_CHUNKPTR][n];
  789. dz->lparray[SCRAMBLE_CHUNKLEN][n] = worklen - dz->lparray[SCRAMBLE_CHUNKPTR][n]; /* 2A */
  790. permute_chunks(chunkcnt,dz); /* 3 */
  791. for(n=0;n < chunkcnt; n++) {
  792. if(dz->itemcnt + 2 >= arraysize) {
  793. arraysize += BIGARRAY;
  794. if((dz->lparray[ZIGZAG_TIMES] =(int *)realloc(dz->lparray[ZIGZAG_TIMES],arraysize*sizeof(int)))==NULL) {
  795. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  796. return(MEMORY_ERROR);
  797. }
  798. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY], arraysize*sizeof(int )))==NULL) {
  799. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers store.\n");
  800. return(MEMORY_ERROR);
  801. }
  802. }
  803. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = dz->lparray[SCRAMBLE_CHUNKPTR][perm[n]];
  804. dz->iparray[ZIGZAG_PLAY][dz->itemcnt] = TRUE;
  805. dz->lparray[ZIGZAG_TIMES][dz->itemcnt+1] = dz->lparray[ZIGZAG_TIMES][dz->itemcnt] + dz->lparray[SCRAMBLE_CHUNKLEN][perm[n]];
  806. /* APR 1998 */
  807. dz->lparray[ZIGZAG_TIMES][dz->itemcnt+1] += dz->iparam[ZIG_SPLSAMPS];
  808. if(dz->lparray[ZIGZAG_TIMES][dz->itemcnt+1] > worklen) {
  809. dz->lparray[ZIGZAG_TIMES][dz->itemcnt+1] = worklen;
  810. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = worklen - dz->lparray[SCRAMBLE_CHUNKLEN][perm[n]];
  811. }
  812. /* APR 1998 */
  813. dz->iparray[ZIGZAG_PLAY][dz->itemcnt+1] = FALSE;
  814. if((total_len += dz->lparray[SCRAMBLE_CHUNKLEN][perm[n]]) > dz->iparam[SCRAMBLE_OUTLEN]) {
  815. finished = 1;
  816. break;
  817. }
  818. dz->itemcnt += 2;
  819. }
  820. }
  821. if((dz->lparray[ZIGZAG_TIMES] = (int *)realloc(dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  822. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  823. return(MEMORY_ERROR);
  824. }
  825. if((dz->iparray[ZIGZAG_PLAY] = (int *)realloc(dz->iparray[ZIGZAG_PLAY], dz->itemcnt * sizeof(int )))==NULL) {
  826. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate markers store.\n");
  827. return(MEMORY_ERROR);
  828. }
  829. return(FINISHED);
  830. }
  831. /*********************** GET_BASIS_LENGTHS ***********************/
  832. int get_basis_lengths(int chunkcnt,int worklen,int *unit_len, int *endlen,
  833. int *scatgrpcnt, int *endscat, int *range, int *endrange, dataptr dz)
  834. {
  835. int excess;
  836. *unit_len = (int)round(worklen/chunkcnt);
  837. if(ODD(*unit_len))
  838. (*unit_len)--; /* Allow for stereo case */
  839. excess = worklen - ((*unit_len) * chunkcnt);
  840. if(ODD(excess) && dz->infile->channels==2) {
  841. sprintf(errstr,"Problem in buffer accounting: get_basis_lengths()\n");
  842. return(PROGRAM_ERROR);
  843. }
  844. *endlen = *unit_len + excess;
  845. if(dz->param[SCRAMBLE_SCAT] >= 1.0) {
  846. dz->iparam[SCRAMBLE_SCAT] = round(dz->param[SCRAMBLE_SCAT]);
  847. *scatgrpcnt = (int)(chunkcnt/dz->iparam[SCRAMBLE_SCAT]);
  848. *endscat = (int)(chunkcnt - ((*scatgrpcnt) * dz->iparam[SCRAMBLE_SCAT]));
  849. *range = (*unit_len) * dz->iparam[SCRAMBLE_SCAT];
  850. *endrange = ((*endscat - 1) * (*unit_len)) + *endlen;
  851. } else {
  852. dz->iparam[SCRAMBLE_SCAT] = 0;
  853. }
  854. return(FINISHED);
  855. }
  856. /************************** NORMAL_SCAT *******************************/
  857. int normal_scat(int unit_len,int endlen,int chunkcnt_less_one,dataptr dz)
  858. {
  859. double this_scatter;
  860. int n;
  861. int new_chunklen, total_len = unit_len;
  862. for(n=1;n<chunkcnt_less_one;n++) {
  863. this_scatter = (drand48() - 0.5) * dz->param[SCRAMBLE_SCAT]; /* range +- .5 reduced by *scatter */
  864. new_chunklen = (int)(this_scatter * (double)unit_len);
  865. if(ODD(new_chunklen))
  866. new_chunklen--; /* in case stereo?? */
  867. dz->lparray[SCRAMBLE_CHUNKPTR][n] = total_len + new_chunklen; /* position chunk-pointer */
  868. total_len += unit_len; /* step to next BASE position */
  869. }
  870. this_scatter = (drand48() - 0.5) * dz->param[SCRAMBLE_SCAT]; /* as final chunk may be unique length */
  871. if(this_scatter<0.0) { /* if scatter is -ve, scatter is frac of penultimate length */
  872. new_chunklen = (int)(this_scatter * (double)unit_len);
  873. if(ODD(new_chunklen))
  874. new_chunklen--;
  875. dz->lparray[SCRAMBLE_CHUNKPTR][n] = total_len - new_chunklen;
  876. } else { /* else, scatter is frac of final length */
  877. new_chunklen = (int)(this_scatter * (double)endlen);
  878. if(ODD(new_chunklen))
  879. new_chunklen--;
  880. dz->lparray[SCRAMBLE_CHUNKPTR][n] = total_len + new_chunklen;
  881. }
  882. return(FINISHED);
  883. }
  884. /*********************** HEAVY_SCAT ***************************
  885. *
  886. * (1) Start at the chunk (this=1) AFTER the first (which can't be moved).
  887. * (2) STARTPTR marks the start of the chunk GROUP (and will be advanced
  888. * by RANGE, which is length of chunk-group).
  889. * (3) The loop will generate a set of positions for the chunks in
  890. * a chunk-group. In the first chunkgroup the position of the
  891. * first chunk (start of file) can't be moved, so loop starts at
  892. * (first=) 1. Subsequemt loop passes start at 0.
  893. * (4) For eveery chunk-group.
  894. * (5) Set the index of the first chunk in this group (start) to the
  895. * current index (this).
  896. * (6) For every member of this chunk-group.
  897. * (7) Generate a random-position within the chunk-grp's range
  898. * and check it is not too close ( < SPLICELEN) to the others.
  899. * Set a checking flag (OK).
  900. * (8) Generate a position within the range, and after the startptr.
  901. * (9) Compare it with all previously generated positions in this
  902. * chunk-grp AND with last position of previous chunk-group!!
  903. * If it's closer than SPLICELEN, set OK = 0, drop out of
  904. checking loop and generate another position instead.
  905. * (10) If the position is OK, drop out of position generating loop..
  906. * (11) Advance to next chunk in this group.
  907. * (12) Once all this group is done, advance the group startpoint by RANGE.
  908. * (13) After FIRST grp, all positions can by varied, so set the initial
  909. * loop counter to (first=)0.
  910. * (14) If there are chunks left over (endscat!=0)..
  911. * Follow the same procedure for chunks in end group, using the
  912. * alternative variables, endscat and endrange.
  913. */
  914. int heavy_scat(int scatgrpcnt,int range,int endscat,int endrange,dataptr dz)
  915. {
  916. int thiss = 0, that, start, n, m, OK; /* 1 */
  917. int startptr = 0; /* 2 */
  918. int minlen = (dz->iparam[ZIG_SPLSAMPS] * 2);
  919. int filend = dz->insams[0];
  920. int first = 1; /* 3 */
  921. /* TW OCT 1997 */
  922. int latest_ptr = 0;
  923. dz->lparray[SCRAMBLE_CHUNKPTR][thiss++] = 0;
  924. /* TW OCT 1997 */
  925. for(n=0;n<scatgrpcnt;n++) { /* 4 */
  926. start = thiss; /* 5 */
  927. for(m=first;m<dz->iparam[SCRAMBLE_SCAT];m++) { /* 6 */
  928. do { /* 7 */
  929. OK = 1;
  930. dz->lparray[SCRAMBLE_CHUNKPTR][thiss] = (int)(drand48()*range); /* TRUNCATE */
  931. dz->lparray[SCRAMBLE_CHUNKPTR][thiss] += startptr; /* 8 */
  932. if(ODD(dz->lparray[SCRAMBLE_CHUNKPTR][thiss]))
  933. dz->lparray[SCRAMBLE_CHUNKPTR][thiss]--;
  934. /* TW OCT 1997 -->*/
  935. if(abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss] - latest_ptr)< minlen) {
  936. OK = 0;
  937. continue;
  938. }
  939. for(that=start; that<thiss; that++) {
  940. if(abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss] - dz->lparray[SCRAMBLE_CHUNKPTR][that])<minlen
  941. || abs(filend - abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss]))< minlen) {
  942. /* TW OCT 1997 */
  943. OK = 0; /* 9 */
  944. break;
  945. }
  946. }
  947. } while(!OK); /* 10 */
  948. thiss++; /* 11 */
  949. }
  950. /* TW OCT 1997 --> */
  951. for(m=0;m<thiss;m++) {
  952. if(dz->lparray[SCRAMBLE_CHUNKPTR][m] > latest_ptr)
  953. latest_ptr = dz->lparray[SCRAMBLE_CHUNKPTR][m];
  954. }
  955. /* TW OCT 1997 */
  956. startptr += range; /* 12 */
  957. first = 0; /* 13 */
  958. }
  959. if(endscat) { /* 14 */
  960. start = thiss;
  961. for(m=0;m<endscat;m++) {
  962. do {
  963. OK = 1;
  964. dz->lparray[SCRAMBLE_CHUNKPTR][thiss] = (int)(drand48() * endrange); /* TRUNCATE */
  965. dz->lparray[SCRAMBLE_CHUNKPTR][thiss] += startptr;
  966. if(ODD(dz->lparray[SCRAMBLE_CHUNKPTR][thiss]))
  967. dz->lparray[SCRAMBLE_CHUNKPTR][thiss]--;
  968. /* TW OCT 1997 */
  969. if(abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss] - latest_ptr)< minlen) {
  970. OK = 0;
  971. continue;
  972. }
  973. /* TW OCT 1997 */
  974. for(that=start; that<thiss; that++) {
  975. if(abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss] - dz->lparray[SCRAMBLE_CHUNKPTR][that])< minlen
  976. /* TW OCT 1997 */
  977. || abs(filend - abs(dz->lparray[SCRAMBLE_CHUNKPTR][thiss]))< minlen) {
  978. /* TW OCT 1997 */
  979. OK = 0;
  980. break;
  981. }
  982. }
  983. } while(!OK);
  984. thiss++;
  985. }
  986. }
  987. return ptr_sort(thiss,dz);
  988. }
  989. /*************************** PERMUTE_CHUNKS ***************************/
  990. void permute_chunks(int chunkcnt,dataptr dz)
  991. {
  992. int n, t;
  993. for(n=0;n<chunkcnt;n++) {
  994. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  995. if(t==n)
  996. prefix(n,chunkcnt,dz);
  997. else
  998. insert(n,t,chunkcnt,dz);
  999. }
  1000. }
  1001. /****************************** INSERT ****************************/
  1002. void insert(int n,int t,int chunkcnt,dataptr dz)
  1003. {
  1004. shuflup(t+1,chunkcnt,dz);
  1005. dz->iparray[SCRAMBLE_PERM][t+1] = n;
  1006. }
  1007. /****************************** PREFIX ****************************/
  1008. void prefix(int n,int chunkcnt,dataptr dz)
  1009. {
  1010. shuflup(0,chunkcnt,dz);
  1011. dz->iparray[SCRAMBLE_PERM][0] = n;
  1012. }
  1013. /****************************** SHUFLUP ****************************/
  1014. void shuflup(int k,int chunkcnt,dataptr dz)
  1015. {
  1016. int n;
  1017. int *i;
  1018. int z = chunkcnt-1;
  1019. i = &(dz->iparray[SCRAMBLE_PERM][z]);
  1020. for(n = z; n > k; n--) {
  1021. *i = *(i-1);
  1022. i--;
  1023. }
  1024. }
  1025. /************************** PTR_SORT ***************************/
  1026. int ptr_sort(int end,dataptr dz)
  1027. {
  1028. int i,j;
  1029. int a;
  1030. for(j=1;j<end;j++) {
  1031. a = dz->lparray[SCRAMBLE_CHUNKPTR][j];
  1032. i = j-1;
  1033. while(i >= 0 && dz->lparray[SCRAMBLE_CHUNKPTR][i] > a) {
  1034. dz->lparray[SCRAMBLE_CHUNKPTR][i+1]=dz->lparray[SCRAMBLE_CHUNKPTR][i];
  1035. i--;
  1036. }
  1037. dz->lparray[SCRAMBLE_CHUNKPTR][i+1] = a;
  1038. }
  1039. return(FINISHED);
  1040. }
  1041. /**************************** ITERATE_PREPROCESS ******************************/
  1042. int iterate_preprocess(dataptr dz)
  1043. {
  1044. int exit_status;
  1045. double maxrand, maxpscat, mindelay, temp;
  1046. int is_unity_gain = FALSE;
  1047. int mindelay_samps;
  1048. if(dz->process == ITERATE_EXTEND) {
  1049. if(dz->iparam[ITER_RRSEED] > 0)
  1050. srand((int)dz->iparam[ITER_RRSEED]);
  1051. else
  1052. initrand48();
  1053. } else {
  1054. if(dz->iparam[ITER_RSEED] > 0)
  1055. srand((int)dz->iparam[ITER_RSEED]);
  1056. else
  1057. initrand48();
  1058. }
  1059. if((exit_status = get_maxvalue_of_rand(&maxrand,dz))<0)
  1060. return(exit_status);
  1061. if((exit_status = get_maxvalue_of_pscat(&maxpscat,dz))<0)
  1062. return(exit_status);
  1063. if((exit_status = get_minvalue_of_delay(&mindelay,dz))<0)
  1064. return(exit_status);
  1065. mindelay_samps = round(mindelay * (double)dz->infile->srate);
  1066. if(dz->process == ITERATE_EXTEND) {
  1067. dz->iparam[CHUNKSTART] = (int)round(dz->param[CHUNKSTART] * dz->infile->srate) * dz->infile->channels;
  1068. dz->iparam[CHUNKEND] = (int)round(dz->param[CHUNKEND] * dz->infile->srate) * dz->infile->channels;
  1069. if(dz->param[CHUNKSTART] > dz->param[CHUNKEND]) {
  1070. temp = dz->param[CHUNKSTART];
  1071. dz->param[CHUNKSTART] = dz->param[CHUNKEND];
  1072. dz->param[CHUNKEND] = temp;
  1073. }
  1074. if(dz->iparam[CHUNKEND] - dz->iparam[CHUNKSTART] <= ITX_SPLICELEN * dz->infile->channels * 2) {
  1075. sprintf(errstr,"FROZEN SEGMENT (%d samples) TOO SHORT FOR SPLICING (needs %d samples)\n",
  1076. dz->iparam[CHUNKEND] - dz->iparam[CHUNKSTART],ITX_SPLICELEN * dz->infile->channels * 2);
  1077. return(DATA_ERROR);
  1078. }
  1079. if(dz->iparam[CHUNKEND] - dz->iparam[CHUNKSTART] < mindelay_samps * dz->infile->channels) {
  1080. sprintf(errstr,"FROZEN SEGMENT (%d samples) TOO SHORT FOR (MINIMUM) DELAY TIME SPECIFIED (%d samples)\n",
  1081. dz->iparam[CHUNKEND] - dz->iparam[CHUNKSTART],mindelay_samps * dz->infile->channels);
  1082. return(DATA_ERROR);
  1083. }
  1084. }
  1085. if((dz->process != ITERATE_EXTEND) && (dz->param[ITER_GAIN]==DEFAULT_ITER_GAIN))
  1086. set_default_gain(mindelay_samps,dz);
  1087. set_default_delays(dz);
  1088. if (dz->process == ITERATE_EXTEND) {
  1089. dz->param[ITER_SSTEP] = 1.0; /* 1st sound is exact copy of orig */
  1090. } else {
  1091. reverse_fadevals(dz);
  1092. dz->param[ITER_STEP] = 1.0; /* 1st sound is exact copy of orig */
  1093. }
  1094. if((dz->process != ITERATE_EXTEND) && flteq(dz->param[ITER_GAIN],1.0))
  1095. is_unity_gain = TRUE;
  1096. setup_iter_process_type(is_unity_gain,dz);
  1097. return create_iterbufs(maxpscat,dz);
  1098. }
  1099. /*************************** GET_MAXVALUE_OF_RAND ****************************/
  1100. int get_maxvalue_of_rand(double *maxrand,dataptr dz)
  1101. {
  1102. int exit_status;
  1103. if(dz->brksize[ITER_RANDOM]) {
  1104. if((exit_status = get_maxvalue_in_brktable(maxrand,ITER_RANDOM,dz))<0)
  1105. return(exit_status);
  1106. } else
  1107. *maxrand = dz->param[ITER_RANDOM];
  1108. return(FINISHED);
  1109. }
  1110. /************************* GET_MAXVALUE_OF_PSCAT ****************************/
  1111. int get_maxvalue_of_pscat(double *maxpscat,dataptr dz)
  1112. {
  1113. int exit_status;
  1114. if(dz->brksize[ITER_PSCAT]) {
  1115. if((exit_status = get_maxvalue_in_brktable(maxpscat,ITER_PSCAT,dz))<0)
  1116. return(exit_status);
  1117. } else
  1118. *maxpscat = dz->param[ITER_PSCAT];
  1119. return(FINISHED);
  1120. }
  1121. /************************* GET_MINVALUE_OF_DELAY ****************************/
  1122. int get_minvalue_of_delay(double *mindelay,dataptr dz)
  1123. {
  1124. int exit_status;
  1125. if(dz->brksize[ITER_DELAY]) {
  1126. if((exit_status = get_minvalue_in_brktable(mindelay,ITER_DELAY,dz))<0)
  1127. return(exit_status);
  1128. } else
  1129. *mindelay = dz->param[ITER_DELAY];
  1130. return(FINISHED);
  1131. }
  1132. /************************* REVERSE_FADEVALS ****************************/
  1133. void reverse_fadevals(dataptr dz)
  1134. {
  1135. double *p, *pend;
  1136. if(dz->brksize[ITER_FADE]==0)
  1137. dz->param[ITER_FADE] = 1.0 - dz->param[ITER_FADE];
  1138. else {
  1139. p = dz->brk[ITER_FADE] + 1;
  1140. pend = dz->brk[ITER_FADE] + (dz->brksize[ITER_FADE] * 2);
  1141. while(p < pend) {
  1142. *p = 1.0 - *p;
  1143. p += 2;
  1144. }
  1145. }
  1146. }
  1147. /************************** SET_DEFAULT_GAIN ****************************/
  1148. void set_default_gain(int mindelay_samps,dataptr dz)
  1149. {
  1150. int inmsampsize;
  1151. int maxoverlay_cnt;
  1152. inmsampsize = dz->insams[0]/dz->infile->channels;
  1153. maxoverlay_cnt = round(((double)inmsampsize/(double)mindelay_samps)+1.0);
  1154. if(dz->vflag[IS_ITER_RAND])
  1155. maxoverlay_cnt++;
  1156. dz->param[ITER_GAIN] = 1.0/(double)maxoverlay_cnt;
  1157. }
  1158. /*********************** SET_DEFAULT_DELAYS ****************************/
  1159. void set_default_delays(dataptr dz)
  1160. {
  1161. if(dz->process==ITERATE_EXTEND) {
  1162. if(!dz->brksize[ITER_DELAY])
  1163. dz->iparam[ITER_MSAMPDEL] = round(dz->param[ITER_DELAY] * (double)dz->infile->srate);
  1164. return;
  1165. }
  1166. if(dz->vflag[IS_ITER_DELAY]) {
  1167. if(!dz->brksize[ITER_DELAY])
  1168. dz->iparam[ITER_MSAMPDEL] = round(dz->param[ITER_DELAY] * (double)dz->infile->srate);
  1169. } else
  1170. dz->iparam[ITER_MSAMPDEL] = dz->insams[0]/dz->infile->channels; /* default */
  1171. }
  1172. /********************* SETUP_ITER_PROCESS_TYPE ********************************/
  1173. void setup_iter_process_type(int is_unity_gain,dataptr dz)
  1174. {
  1175. if(dz->process==ITERATE_EXTEND) {
  1176. if(dz->param[ITER_PSCAT] > 0.0) {
  1177. if(dz->infile->channels>1)
  1178. dz->iparam[ITER_PROCESS] = ST_INTP_SHIFT;
  1179. else
  1180. dz->iparam[ITER_PROCESS] = MN_INTP_SHIFT;
  1181. } else {
  1182. if(dz->infile->channels>1)
  1183. dz->iparam[ITER_PROCESS] = STEREO;
  1184. else
  1185. dz->iparam[ITER_PROCESS] = MONO;
  1186. }
  1187. if(flteq(dz->param[ITER_ASCAT],0.0)) {
  1188. dz->iparam[ITER_PROCESS] += FIXED_AMP;
  1189. }
  1190. return;
  1191. }
  1192. dz->iparam[ITER_DO_SCALE] = TRUE;
  1193. if(dz->vflag[IS_ITER_PSCAT]) {
  1194. if(dz->infile->channels>1)
  1195. dz->iparam[ITER_PROCESS] = ST_INTP_SHIFT;
  1196. else
  1197. dz->iparam[ITER_PROCESS] = MN_INTP_SHIFT;
  1198. } else {
  1199. if(dz->infile->channels>1)
  1200. dz->iparam[ITER_PROCESS] = STEREO;
  1201. else
  1202. dz->iparam[ITER_PROCESS] = MONO;
  1203. }
  1204. if(!dz->vflag[IS_ITER_ASCAT]) {
  1205. if(is_unity_gain)
  1206. dz->iparam[ITER_DO_SCALE] = FALSE;
  1207. dz->iparam[ITER_PROCESS] += FIXED_AMP;
  1208. }
  1209. }
  1210. /***************************** DRUNK_PREPROCESS *************************
  1211. *
  1212. * Splicelen & cloktiks have been converted to SAMPLES in pconsistency.c
  1213. */
  1214. int drunk_preprocess(dataptr dz)
  1215. {
  1216. int exit_status;
  1217. dz->iparam[DRNK_LAST_LOCUS] = -1;
  1218. initialise_random_sequence(IS_DRNK_RSEED,DRNK_RSEED,dz);
  1219. if((exit_status = create_drunk_buffers(dz))<0) /* 1 */
  1220. return(exit_status);
  1221. if((exit_status = make_drnk_splicetab(dz))<0)
  1222. return(exit_status);
  1223. if(dz->insams[0] <= dz->iparam[DRNK_SPLICELEN] * dz->infile->channels * 2) {
  1224. sprintf(errstr,"Infile too short for splices.\n");
  1225. return(DATA_ERROR);
  1226. }
  1227. dz->iparam[DRNK_LGRAIN] = round((double)dz->infile->srate * DRNK_GRAIN);
  1228. dz->iparam[DRNK_TOTALDUR] = round(dz->param[DRNK_TOTALDUR] * (double)(dz->infile->srate * dz->infile->channels));
  1229. if((exit_status = convert_time_and_vals_to_samplecnts(DRNK_LOCUS,dz))<0)
  1230. return(exit_status);
  1231. if((exit_status = convert_time_and_vals_to_samplecnts(DRNK_AMBITUS,dz))<0)
  1232. return(exit_status);
  1233. if((exit_status = convert_time_to_samplecnts(DRNK_CLOKRND,dz))<0)
  1234. return(exit_status);
  1235. if((exit_status = convert_time_to_samplecnts(DRNK_OVERLAP,dz))<0)
  1236. return(exit_status);
  1237. if((exit_status = get_maxvalue(DRNK_OVERLAP,&dz->param[DRNK_MAX_OVLAP],dz))<0)
  1238. return(exit_status);
  1239. if((exit_status = convert_time_to_samplecnts(DRNK_GSTEP,dz))<0)
  1240. return(exit_status);
  1241. if((exit_status = convert_sec_steps_to_grain_steps(dz))<0)
  1242. return(exit_status);
  1243. if(dz->mode==HAS_SOBER_MOMENTS) {
  1244. if((exit_status = convert_time_and_vals_to_samplecnts(DRNK_MIN_PAUS,dz))<0)
  1245. return(exit_status);
  1246. if((exit_status = convert_time_and_vals_to_samplecnts(DRNK_MAX_PAUS,dz))<0)
  1247. return(exit_status);
  1248. if((exit_status = convert_time_to_samplecnts(DRNK_MIN_DRNKTIK,dz))<0)
  1249. return(exit_status);
  1250. if((exit_status = convert_time_to_samplecnts(DRNK_MAX_DRNKTIK,dz))<0)
  1251. return(exit_status);
  1252. }
  1253. return(FINISHED);
  1254. }
  1255. /********************** MAKE_DRNK_SPLICETAB *********************/
  1256. int make_drnk_splicetab(dataptr dz)
  1257. {
  1258. int n;
  1259. if((dz->parray[DRNK_SPLICETAB] = (double *)malloc(dz->iparam[DRNK_SPLICELEN] * sizeof(double)))==NULL) {
  1260. sprintf(errstr,"INSUFFICIENT MEMORY to make splicetable.\n");
  1261. return(MEMORY_ERROR);
  1262. }
  1263. for(n=0;n<dz->iparam[DRNK_SPLICELEN];n++)
  1264. dz->parray[DRNK_SPLICETAB][n] = (double)n/(double)dz->iparam[DRNK_SPLICELEN];
  1265. return(FINISHED);
  1266. }
  1267. /************************* CONVERT_SEC_STEPS_TO_GRAIN_STEPS *********************/
  1268. int convert_sec_steps_to_grain_steps(dataptr dz)
  1269. {
  1270. double *p, *pend;
  1271. int lval;
  1272. if(dz->iparam[DRNK_LGRAIN]<=0) {
  1273. sprintf(errstr,"Invalid DRNK_LGRAIN val: convert_sec_steps_to_grain_steps()\n");
  1274. return(PROGRAM_ERROR);
  1275. }
  1276. if(dz->brksize[DRNK_GSTEP]) {
  1277. p = dz->brk[DRNK_GSTEP] + 1;
  1278. pend = dz->brk[DRNK_GSTEP] + (dz->brksize[DRNK_GSTEP] * 2);
  1279. while(p < pend) {
  1280. lval = round(*p * (double)dz->infile->srate);
  1281. lval = (lval+(dz->iparam[DRNK_LGRAIN]/2))/dz->iparam[DRNK_LGRAIN];
  1282. *p = (double)lval;
  1283. p += 2;
  1284. }
  1285. } else {
  1286. dz->iparam[DRNK_GSTEP] = round(dz->param[DRNK_GSTEP] * (double)dz->infile->srate);
  1287. dz->iparam[DRNK_GSTEP] = (dz->iparam[DRNK_GSTEP]+(dz->iparam[DRNK_LGRAIN]/2))/dz->iparam[DRNK_LGRAIN];
  1288. dz->param[DRNK_GSTEP] = (double)dz->iparam[DRNK_GSTEP];
  1289. }
  1290. return(FINISHED);
  1291. }