mixshuf1.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Copyright (c) 1983-2023 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 <filetype.h>
  32. #include <mix.h>
  33. #include <cdpmain.h>
  34. #include <sfsys.h>
  35. #include <osbind.h>
  36. #include <float.h>
  37. static int get_maxpostdec(int *maxpostdec,dataptr dz);
  38. static int get_postdec(char *thisword);
  39. static int get_times_and_timediffs(double *timestor,double *timediff,dataptr dz);
  40. static int reverse_times(double *timestor,double *timediff,dataptr dz);
  41. static int freeze_times(double *timestor,double *timediff,dataptr dz);
  42. static int delete_times(double *timestor,double *timediff,dataptr dz);
  43. static int altdel_times(double *timestor,double *timediff,dataptr dz);
  44. static int regular_times(double *timestor,double *timediff,dataptr dz);
  45. static int arithme_times(double *timestor,double *timediff,dataptr dz);
  46. static int aritexp_times(double *timestor,double *timediff,dataptr dz);
  47. static int geometr_times(double *timestor,double *timediff,dataptr dz);
  48. static int geomexp_times(double *timestor,double *timediff,dataptr dz);
  49. static int exponen_times(double *timestor,double *timediff,dataptr dz);
  50. static int expoexp_times(double *timestor,double *timediff,dataptr dz);
  51. static int spacing_times(double *timestor,double *timediff,dataptr dz);
  52. static int scatter_times(double *timestor,double *timediff,dataptr dz);
  53. static int shuffle_times(double *timestor,double *timediff,dataptr dz);
  54. static int stretch_times(double *timestor,double *timediff,dataptr dz);
  55. static int put_new_time(int total_words,int postdec,double timeval,dataptr dz);
  56. static int convert_level(int lineno,int total_words,int *maxwordsize,
  57. double *llevelstor,double *rlevelstor,int lmaxlevelsize,int rmaxlevelsize,dataptr dz);
  58. static int get_levels(int lineno,int total_words,
  59. double *llevelstor,double *rlevelstor,int *lmaxlevelsize,int *rmaxlevelsize,dataptr dz);
  60. static void delete_wordstorage_space_of_all_deleted_lines(int oldwordcnt,char **wordcopy,dataptr dz);
  61. /************************* DO_TIME_MANIP ******************************/
  62. int do_time_manip(dataptr dz)
  63. {
  64. int exit_status;
  65. double *timestor, *timediff;
  66. int *maxwordsize = NULL, postdec = 0;
  67. if((dz->parray[MTW_TIMESTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  68. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  69. return(MEMORY_ERROR);
  70. }
  71. if((dz->parray[MTW_TIMEDIFF] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  72. sprintf(errstr,"INSUFFICIENT MEMORY for timesteps store.\n");
  73. return(MEMORY_ERROR);
  74. }
  75. timestor = dz->parray[MTW_TIMESTOR];
  76. timediff = dz->parray[MTW_TIMEDIFF];
  77. if((exit_status = get_maxpostdec(&postdec,dz))<0)
  78. return(exit_status);
  79. if((exit_status = get_times_and_timediffs(timestor,timediff,dz))<0)
  80. return(exit_status);
  81. switch(dz->process) {
  82. case(MIXTWARP):
  83. switch(dz->mode) {
  84. case(MTW_TIMESORT):
  85. break;
  86. case(MTW_REVERSE_T):
  87. if((exit_status = reverse_times(timestor,timediff,dz))<0)
  88. return(exit_status);
  89. break;
  90. case(MTW_FREEZE_T):
  91. if((exit_status = freeze_times(timestor,timediff,dz))<0)
  92. return(exit_status);
  93. break;
  94. case(MTW_SCATTER):
  95. if((exit_status = scatter_times(timestor,timediff,dz))<0)
  96. return(exit_status);
  97. break;
  98. case(MTW_DOMINO):
  99. if((exit_status = shuffle_times(timestor,timediff,dz))<0)
  100. return(exit_status);
  101. break;
  102. case(MTW_ADD_TO_TG):
  103. if((exit_status = spacing_times(timestor,timediff,dz))<0)
  104. return(exit_status);
  105. break;
  106. case(MTW_CREATE_TG_1):
  107. if((exit_status = regular_times(timestor,timediff,dz))<0)
  108. return(exit_status);
  109. break;
  110. case(MTW_CREATE_TG_2):
  111. if((exit_status = aritexp_times(timestor,timediff,dz))<0)
  112. return(exit_status);
  113. break;
  114. case(MTW_CREATE_TG_3):
  115. if((exit_status = geomexp_times(timestor,timediff,dz))<0)
  116. return(exit_status);
  117. break;
  118. case(MTW_CREATE_TG_4):
  119. if((exit_status = expoexp_times(timestor,timediff,dz))<0)
  120. return(exit_status);
  121. break;
  122. case(MTW_ENLARGE_TG_1):
  123. if((exit_status = stretch_times(timestor,timediff,dz))<0)
  124. return(exit_status);
  125. break;
  126. case(MTW_ENLARGE_TG_2):
  127. if((exit_status = arithme_times(timestor,timediff,dz))<0)
  128. return(exit_status);
  129. break;
  130. case(MTW_ENLARGE_TG_3):
  131. if((exit_status = geometr_times(timestor,timediff,dz))<0)
  132. return(exit_status);
  133. break;
  134. case(MTW_ENLARGE_TG_4):
  135. if((exit_status = exponen_times(timestor,timediff,dz))<0)
  136. return(exit_status);
  137. break;
  138. default:
  139. sprintf(errstr,"Unknown case in do_time_manip()\n");
  140. return(PROGRAM_ERROR);
  141. }
  142. break;
  143. case(MIXSHUFL):
  144. switch(dz->mode) {
  145. case(MSH_OMIT_ALT):
  146. if((exit_status = altdel_times(timestor,timediff,dz))<0)
  147. return(exit_status);
  148. break;
  149. case(MSH_OMIT):
  150. if((exit_status = delete_times(timestor,timediff,dz))<0)
  151. return(exit_status);
  152. break;
  153. default:
  154. sprintf(errstr,"Unknown case in do_time_manip()\n");
  155. return(PROGRAM_ERROR);
  156. }
  157. break;
  158. default:
  159. sprintf(errstr,"Unknown case in do_time_manip()\n");
  160. return(PROGRAM_ERROR);
  161. }
  162. if((exit_status = retime_the_lines(timestor,postdec,dz))<0)
  163. return(exit_status);
  164. if((exit_status = timesort_mixfile(timestor,dz))<0)
  165. return(exit_status);
  166. if((exit_status = get_maxwordsize(&maxwordsize,dz))<0)
  167. return(exit_status);
  168. if((exit_status = output_mixfile_lines(maxwordsize,dz))<0)
  169. return(exit_status);
  170. free(maxwordsize);
  171. return(FINISHED);
  172. }
  173. /************************** REVERSE_TIMES **************************/
  174. int reverse_times(double *timestor,double *timediff,dataptr dz)
  175. {
  176. int n, m, k;
  177. int this_linecnt;
  178. if(dz->iparam[MSH_ENDLINE] - dz->iparam[MSH_STARTLINE] < 2) {
  179. sprintf(errstr,"CAN'T REVERSE TIME PATTERN: TOO FEW LINES\n");
  180. return(DATA_ERROR);
  181. }
  182. this_linecnt = (dz->iparam[MSH_ENDLINE] - dz->iparam[MSH_STARTLINE]) - 2;
  183. for(n = 0, k=dz->iparam[MSH_STARTLINE]+1, m = dz->iparam[MSH_ENDLINE] - 2; n<this_linecnt; n++, k++, m--)
  184. timestor[k] = timestor[k-1] + timediff[m];
  185. return(FINISHED);
  186. }
  187. /************************** STRETCH_TIMES **************************/
  188. int stretch_times(double *timestor,double *timediff,dataptr dz)
  189. {
  190. int n;
  191. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++)
  192. timediff[n-1] *= dz->param[MTW_PARAM];
  193. for(n=1; n<dz->linecnt; n++) {
  194. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  195. sprintf(errstr,"Numbers got too large.\n");
  196. return(GOAL_FAILED);
  197. }
  198. timestor[n] = timestor[n-1] + timediff[n-1];
  199. }
  200. return(FINISHED);
  201. }
  202. /************************** FREEZE_TIMES **************************/
  203. int freeze_times(double *timestor,double *timediff,dataptr dz)
  204. {
  205. int n;
  206. for(n=dz->iparam[MSH_STARTLINE]+1;n<dz->iparam[MSH_ENDLINE]-1;n++)
  207. timediff[n] = timediff[dz->iparam[MSH_STARTLINE]];
  208. for(n=1; n<dz->linecnt; n++)
  209. timestor[n] = timestor[n-1] + timediff[n-1];
  210. return(FINISHED);
  211. }
  212. /************************** DELETE_TIMES **************************/
  213. int delete_times(double *timestor,double *timediff,dataptr dz)
  214. {
  215. int n, m, k;
  216. int del_lines = dz->iparam[MSH_ENDLINE] - dz->iparam[MSH_STARTLINE];
  217. int new_total_wordcnt = 0, current_word_to_copy, /*total_wordcnt,*/ oldwordcnt;
  218. char **wordcopy;
  219. if((wordcopy = (char **)malloc(dz->all_words * sizeof(char *)))==NULL) {
  220. sprintf(errstr,"INSUFFICIENT MEMORY for wordcopy pointers store.\n");
  221. return(MEMORY_ERROR);
  222. }
  223. for(n=0;n<dz->all_words;n++)
  224. wordcopy[n] = dz->wordstor[n];
  225. if(dz->iparam[MSH_STARTLINE]==0 && dz->iparam[MSH_ENDLINE] == dz->linecnt) {
  226. free(wordcopy);
  227. sprintf(errstr,"Whole file would be deleted!\n");
  228. return(USER_ERROR);
  229. }
  230. for(n=1; n < dz->linecnt; n++) {
  231. if(timestor[n] < timestor[n-1]) {
  232. free(wordcopy);
  233. sprintf(errstr,"mixfile lines are not in correct time-order\n");
  234. return(USER_ERROR);
  235. }
  236. }
  237. for(n=dz->iparam[MSH_STARTLINE],m = dz->iparam[MSH_ENDLINE]; m < dz->linecnt-1; n++,m++)
  238. timediff[n] = timediff[m]; /* adjust times: by shuffling down timediffs */
  239. for(n=0;n<dz->iparam[MSH_STARTLINE];n++)
  240. new_total_wordcnt += dz->wordcnt[n];
  241. // total_wordcnt = new_total_wordcnt;
  242. // for(n=dz->iparam[MSH_STARTLINE];n<dz->linecnt;n++)
  243. // total_wordcnt += dz->wordcnt[n];
  244. current_word_to_copy = new_total_wordcnt;
  245. for(n=dz->iparam[MSH_STARTLINE];n<dz->iparam[MSH_ENDLINE];n++)
  246. current_word_to_copy += dz->wordcnt[n];
  247. for(n=dz->iparam[MSH_STARTLINE],m = dz->iparam[MSH_ENDLINE]; m < dz->linecnt; n++,m++) {
  248. for(k=0;k<dz->wordcnt[m];k++)
  249. dz->wordstor[new_total_wordcnt++] = dz->wordstor[current_word_to_copy++];
  250. dz->wordcnt[n] = dz->wordcnt[m];
  251. }
  252. //oldlinecnt = dz->linecnt;
  253. oldwordcnt = dz->all_words;
  254. dz->linecnt -= del_lines;
  255. dz->all_words = new_total_wordcnt;
  256. delete_wordstorage_space_of_all_deleted_lines(oldwordcnt,wordcopy,dz);
  257. free(wordcopy);
  258. for(n=1; n<dz->linecnt; n++)
  259. timestor[n] = timestor[n-1] + timediff[n-1];
  260. return(FINISHED);
  261. }
  262. /************************** ALTDEL_TIMES **************************/
  263. int altdel_times(double *timestor,double *timediff,dataptr dz)
  264. {
  265. int n, copied_or_deleted_line, k;
  266. int new_total_wordcnt = 0, current_word_to_copy, oldwordcnt, new_linecnt;
  267. char **wordcopy;
  268. if((wordcopy = (char **)malloc(dz->all_words * sizeof(char *)))==NULL) {
  269. sprintf(errstr,"INSUFFICIENT MEMORY for wordcopy pointers store.\n");
  270. return(MEMORY_ERROR);
  271. }
  272. for(n=0;n<dz->all_words;n++)
  273. wordcopy[n] = dz->wordstor[n];
  274. copied_or_deleted_line = dz->iparam[MSH_STARTLINE];
  275. for(n=dz->iparam[MSH_STARTLINE]; n <= dz->iparam[MSH_ENDLINE]; n++) {
  276. if(++copied_or_deleted_line >= dz->linecnt) /* SKIP DELETED LINE */
  277. break;
  278. timediff[n] = timediff[copied_or_deleted_line]; /* SHUFFLE BACK TIME DIFFERENCES */
  279. if(++copied_or_deleted_line >= dz->linecnt)
  280. break;
  281. }
  282. while(copied_or_deleted_line < dz->linecnt) /* RESET TIMEDIFFS OF ANY REMAINING LINES */
  283. timediff[n++] = timediff[copied_or_deleted_line++];
  284. for(n=0;n<dz->iparam[MSH_STARTLINE];n++)
  285. new_total_wordcnt += dz->wordcnt[n];
  286. current_word_to_copy = new_total_wordcnt;
  287. new_linecnt = dz->iparam[MSH_STARTLINE];
  288. copied_or_deleted_line = dz->iparam[MSH_STARTLINE];
  289. for(n=dz->iparam[MSH_STARTLINE]; n < dz->iparam[MSH_ENDLINE];n++) {
  290. current_word_to_copy += dz->wordcnt[copied_or_deleted_line];/* SKIP DELETED LINE */
  291. if(++copied_or_deleted_line >= dz->linecnt)
  292. break;
  293. for(k=0;k<dz->wordcnt[copied_or_deleted_line];k++) /* COPY RETAINED LINE */
  294. dz->wordstor[new_total_wordcnt++] = dz->wordstor[current_word_to_copy++];
  295. dz->wordcnt[n] = dz->wordcnt[copied_or_deleted_line]; /* RESET NEW LINE-WORDCNT */
  296. new_linecnt++; /* COUNT NEW TOTAL LINES */
  297. if(++copied_or_deleted_line >= dz->linecnt)
  298. break;
  299. }
  300. while(copied_or_deleted_line<dz->linecnt) { /* RESET WORDCNTS OF ANY REMAINING LINES */
  301. dz->wordcnt[n] = dz->wordcnt[copied_or_deleted_line];
  302. new_linecnt++; /* KEEP COUNTING NEW TOTAL LINES */
  303. copied_or_deleted_line++;
  304. }
  305. while(current_word_to_copy<dz->all_words) /* COPY ANY REMAINING LINES */
  306. dz->wordstor[new_total_wordcnt++] = dz->wordstor[current_word_to_copy++];
  307. //oldlinecnt = dz->linecnt;
  308. oldwordcnt = dz->all_words;
  309. dz->linecnt = new_linecnt;
  310. dz->all_words = new_total_wordcnt;
  311. delete_wordstorage_space_of_all_deleted_lines(oldwordcnt,wordcopy,dz);
  312. free(wordcopy);
  313. for(n=1; n<dz->linecnt; n++)
  314. timestor[n] = timestor[n-1] + timediff[n-1];
  315. return(FINISHED);
  316. }
  317. /************************** REGULAR_TIMES **************************/
  318. int regular_times(double *timestor,double *timediff,dataptr dz)
  319. { int n;
  320. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++)
  321. timediff[n-1] = dz->param[MTW_PARAM];
  322. for(n=1; n<dz->linecnt; n++)
  323. timestor[n] = timestor[n-1] + timediff[n-1];
  324. return(FINISHED);
  325. }
  326. /************************** ARITHME_TIMES **************************/
  327. int arithme_times(double *timestor,double *timediff,dataptr dz)
  328. {
  329. int n;
  330. double base_factor = dz->param[MTW_PARAM];
  331. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++) {
  332. timediff[n-1] += dz->param[MTW_PARAM];
  333. if(DBL_MAX - dz->param[MTW_PARAM] <= base_factor) {
  334. sprintf(errstr,"Numbers got too large.\n");
  335. return(GOAL_FAILED);
  336. }
  337. dz->param[MTW_PARAM] += base_factor;
  338. }
  339. for(n=1; n<dz->linecnt; n++) {
  340. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  341. sprintf(errstr,"Numbers got too large.\n");
  342. return(GOAL_FAILED);
  343. }
  344. timestor[n] = timestor[n-1] + timediff[n-1];
  345. if(timestor[n]<0.0) {
  346. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  347. return(GOAL_FAILED);
  348. }
  349. }
  350. return(FINISHED);
  351. }
  352. /************************** ARITEXP_TIMES **************************/
  353. int aritexp_times(double *timestor,double *timediff,dataptr dz)
  354. {
  355. int n;
  356. double value;
  357. value = timediff[dz->iparam[MSH_STARTLINE]];
  358. for(n=dz->iparam[MSH_STARTLINE]+1;n<dz->iparam[MSH_ENDLINE]-1;n++) {
  359. if(DBL_MAX - value <= dz->param[MTW_PARAM]) {
  360. sprintf(errstr,"Numbers got too large.\n");
  361. return(GOAL_FAILED);
  362. }
  363. value += dz->param[MTW_PARAM];
  364. timediff[n] = value;
  365. }
  366. for(n=1; n<dz->linecnt; n++) {
  367. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  368. sprintf(errstr,"Numbers got too large.\n");
  369. return(GOAL_FAILED);
  370. }
  371. timestor[n] = timestor[n-1] + timediff[n-1];
  372. if(timestor[n]<0.0) {
  373. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  374. return(GOAL_FAILED);
  375. }
  376. }
  377. return(FINISHED);
  378. }
  379. /************************** GEOMETR_TIMES **************************/
  380. int geometr_times(double *timestor,double *timediff,dataptr dz)
  381. {
  382. int n;
  383. double base_factor = dz->param[MTW_PARAM];
  384. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++) {
  385. if(dz->param[MTW_PARAM] > 1.0) {
  386. if(DBL_MAX/dz->param[MTW_PARAM] <= timediff[n-1]) {
  387. sprintf(errstr,"Numbers got too large.\n");
  388. return(GOAL_FAILED);
  389. }
  390. }
  391. timediff[n-1] *= dz->param[MTW_PARAM];
  392. dz->param[MTW_PARAM] += base_factor;
  393. }
  394. for(n=1; n<dz->linecnt; n++) {
  395. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  396. sprintf(errstr,"Numbers got too large.\n");
  397. return(GOAL_FAILED);
  398. }
  399. timestor[n] = timestor[n-1] + timediff[n-1];
  400. if(timestor[n]<0.0) {
  401. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  402. return(GOAL_FAILED);
  403. }
  404. }
  405. return(FINISHED);
  406. }
  407. /************************** GEOMEXP_TIMES **************************/
  408. int geomexp_times(double *timestor,double *timediff,dataptr dz)
  409. {
  410. int n;
  411. double value, base_factor = dz->param[MTW_PARAM];
  412. value = timediff[dz->iparam[MSH_STARTLINE]];
  413. for(n=dz->iparam[MSH_STARTLINE]+1;n<dz->iparam[MSH_ENDLINE]-1;n++) {
  414. if(dz->param[MTW_PARAM] > 1.0) {
  415. if(DBL_MAX/(dz->param[MTW_PARAM]) <= value) {
  416. sprintf(errstr,"Numbers got too large.\n");
  417. return(GOAL_FAILED);
  418. }
  419. }
  420. timediff[n] = value * dz->param[MTW_PARAM];
  421. if(DBL_MAX - dz->param[MTW_PARAM] <= base_factor) {
  422. sprintf(errstr,"Numbers got too large.\n");
  423. return(GOAL_FAILED);
  424. }
  425. dz->param[MTW_PARAM] += base_factor;
  426. }
  427. for(n=1; n<dz->linecnt; n++) {
  428. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  429. sprintf(errstr,"Numbers got too large.\n");
  430. return(GOAL_FAILED);
  431. }
  432. timestor[n] = timestor[n-1] + timediff[n-1];
  433. if(timestor[n]<0.0) {
  434. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  435. return(GOAL_FAILED);
  436. }
  437. }
  438. return(FINISHED);
  439. }
  440. /************************** EXPONEN_TIMES **************************/
  441. int exponen_times(double *timestor,double *timediff,dataptr dz)
  442. {
  443. int n;
  444. double base_factor = dz->param[MTW_PARAM];
  445. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++) {
  446. if(DBL_MAX/dz->param[MTW_PARAM] <= timediff[n-1]) {
  447. sprintf(errstr,"Numbers got too large.\n");
  448. return(GOAL_FAILED);
  449. }
  450. timediff[n-1] *= dz->param[MTW_PARAM];
  451. dz->param[MTW_PARAM] *= base_factor;
  452. }
  453. for(n=1; n<dz->linecnt; n++) {
  454. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  455. sprintf(errstr,"Numbers got too large.\n");
  456. return(GOAL_FAILED);
  457. }
  458. timestor[n] = timestor[n-1] + timediff[n-1];
  459. if(timestor[n]<0.0) {
  460. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  461. return(GOAL_FAILED);
  462. }
  463. }
  464. return(FINISHED);
  465. }
  466. /************************** EXPOEXP_TIMES **************************/
  467. int expoexp_times(double *timestor,double *timediff,dataptr dz)
  468. {
  469. int n;
  470. double value, base_factor = dz->param[MTW_PARAM];
  471. value = timediff[dz->iparam[MSH_STARTLINE]];
  472. for(n=dz->iparam[MSH_STARTLINE]+1;n<dz->iparam[MSH_ENDLINE]-1;n++) {
  473. if(DBL_MAX/value <= dz->param[MTW_PARAM]) {
  474. sprintf(errstr,"Numbers got too large.\n");
  475. return(GOAL_FAILED);
  476. }
  477. timediff[n] = value * dz->param[MTW_PARAM];
  478. if(DBL_MAX/dz->param[MTW_PARAM] <= base_factor) {
  479. sprintf(errstr,"Numbers got too large.\n");
  480. return(GOAL_FAILED);
  481. }
  482. dz->param[MTW_PARAM] *= base_factor;
  483. }
  484. for(n=1; n<dz->linecnt; n++) {
  485. if(DBL_MAX - timestor[n-1] <= timediff[n-1]) {
  486. sprintf(errstr,"Numbers got too large.\n");
  487. return(GOAL_FAILED);
  488. }
  489. timestor[n] = timestor[n-1] + timediff[n-1];
  490. if(timestor[n]<0.0) {
  491. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  492. return(GOAL_FAILED);
  493. }
  494. }
  495. return(FINISHED);
  496. }
  497. /************************** SPACING_TIMES **************************/
  498. int spacing_times(double *timestor,double *timediff,dataptr dz)
  499. {
  500. int n;
  501. for(n = max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE];n++)
  502. timediff[n-1] += dz->param[MTW_PARAM];
  503. for(n=1; n<dz->linecnt; n++) {
  504. timestor[n] = timestor[n-1] + timediff[n-1];
  505. if(timestor[n]<0.0) {
  506. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  507. return(GOAL_FAILED);
  508. }
  509. }
  510. return(FINISHED);
  511. }
  512. /************************** SHUFFLE_TIMES **************************/
  513. int shuffle_times(double *timestor,double *timediff,dataptr dz)
  514. {
  515. int n;
  516. n = max(dz->iparam[MSH_STARTLINE],1);
  517. timediff[n-1] += dz->param[MTW_PARAM];
  518. for(; n<dz->iparam[MSH_ENDLINE]; n++) {
  519. timestor[n] = timestor[n-1] + timediff[n-1];
  520. if(timestor[n]<0.0) {
  521. sprintf(errstr,"NEGATIVE TIME GENERATED AT LINE %d\n",n+1);
  522. return(GOAL_FAILED);
  523. }
  524. }
  525. return(FINISHED);
  526. }
  527. /************************** SCATTER_TIMES **************************/
  528. int scatter_times(double *timestor,double *timediff,dataptr dz)
  529. {
  530. int n;
  531. double base_scatter = 0, scatdirection;
  532. if(dz->iparam[MSH_STARTLINE]==0) {
  533. base_scatter = (timediff[0]/2.0) * drand48() * dz->param[MTW_PARAM];
  534. timestor[0] += base_scatter;
  535. }
  536. for(n=max(dz->iparam[MSH_STARTLINE],1);n<dz->iparam[MSH_ENDLINE]-1;n++) {
  537. scatdirection = (drand48() * 2.0) - 1.0;
  538. if(scatdirection<0.0)
  539. timestor[n] += (timediff[n]/2.0) * drand48() * dz->param[MTW_PARAM];
  540. else
  541. timestor[n] -= (timediff[n-1]/2.0) * drand48() * dz->param[MTW_PARAM];
  542. }
  543. if(dz->iparam[MSH_ENDLINE]==dz->linecnt)
  544. timestor[dz->linecnt-1] -= (timediff[dz->linecnt-2]/2.0) * drand48() * dz->param[MTW_PARAM];
  545. else {
  546. scatdirection = (drand48() * 2.0) - 1.0;
  547. if(scatdirection<0.0)
  548. timestor[dz->iparam[MSH_ENDLINE]-1] +=
  549. (timediff[dz->iparam[MSH_ENDLINE]]/2.0) * drand48() * dz->param[MTW_PARAM];
  550. else
  551. timestor[dz->iparam[MSH_ENDLINE]-1] -=
  552. (timediff[dz->iparam[MSH_ENDLINE]-1]/2.0) * drand48() * dz->param[MTW_PARAM];
  553. }
  554. if(dz->iparam[MSH_STARTLINE]==0){
  555. for(n=0; n<dz->linecnt; n++)
  556. timestor[n] -= base_scatter;
  557. }
  558. return(FINISHED);
  559. }
  560. /*************************** GET_TIMES_AND_TIMEDIFFS ***************************/
  561. int get_times_and_timediffs(double *timestor,double *timediff,dataptr dz)
  562. {
  563. int n, timeloc;
  564. int total_wordcnt = 0;
  565. for(n=0;n<dz->linecnt;n++) {
  566. if(dz->wordcnt[n] < 2) {
  567. sprintf(errstr,"Problem getting line times: get_times_and_timediffs()\n");
  568. return(PROGRAM_ERROR);
  569. }
  570. timeloc = total_wordcnt + MIX_TIMEPOS;
  571. if(sscanf(dz->wordstor[timeloc],"%lf",&(timestor[n]))!=1) {
  572. sprintf(errstr,"Problem reading time: get_times_and_timediffs()\n");
  573. return(PROGRAM_ERROR);
  574. }
  575. total_wordcnt += dz->wordcnt[n];
  576. }
  577. for(n=1;n<dz->linecnt;n++)
  578. timediff[n-1] = timestor[n] - timestor[n-1];
  579. return(FINISHED);
  580. }
  581. /*************************** retime_the_lines ***************************/
  582. int retime_the_lines(double *timestor,int postdec,dataptr dz)
  583. {
  584. int exit_status;
  585. int lineno;
  586. int total_words = 0;
  587. for(lineno=0;lineno<dz->linecnt;lineno++) {
  588. if((exit_status = put_new_time(total_words,postdec,timestor[lineno],dz))<0)
  589. return(exit_status);
  590. total_words += dz->wordcnt[lineno];
  591. }
  592. return(FINISHED);
  593. }
  594. /************************** PUT_NEW_TIME ***************************/
  595. int put_new_time(int total_words,int postdec,double timeval,dataptr dz)
  596. {
  597. int wordno, newlen;
  598. wordno = total_words + MIX_TIMEPOS;
  599. switch(postdec) {
  600. case(0): sprintf(errstr,"%.0lf",timeval);
  601. case(1): sprintf(errstr,"%.1lf",timeval);
  602. case(2): sprintf(errstr,"%.2lf",timeval);
  603. case(3): sprintf(errstr,"%.3lf",timeval);
  604. case(4): sprintf(errstr,"%.4lf",timeval);
  605. default: sprintf(errstr,"%.5lf",timeval); /* DEFAULT_DECIMAL_REPRESENTATION */
  606. }
  607. if((newlen = (int)strlen(errstr)) > (int)strlen(dz->wordstor[wordno])) {
  608. if((dz->wordstor[wordno] = (char *)realloc(dz->wordstor[wordno],(newlen+1) * sizeof(char)))==NULL) {
  609. sprintf(errstr,"INSUFFICIENT MEMORY for wordstore %d\n",wordno+1);
  610. return(MEMORY_ERROR);
  611. }
  612. }
  613. strcpy(dz->wordstor[wordno],errstr);
  614. return(FINISHED);
  615. }
  616. /********************** GET_MAXWORDSIZE ****************************/
  617. int get_maxwordsize(int **maxwordsize,dataptr dz)
  618. {
  619. int total_words = 0, n;
  620. int m;
  621. if((*maxwordsize = (int *)malloc(MIX_MAXLINE * sizeof(int)))==NULL) {
  622. sprintf(errstr,"INSUFFICIENT MEMORY for max wordsize store.\n");
  623. return(MEMORY_ERROR);
  624. }
  625. for(m=0;m<MIX_MAXLINE;m++)
  626. (*maxwordsize)[m] = 0;
  627. for(n=0;n<dz->linecnt;n++) {
  628. for(m=0;m<dz->wordcnt[n];m++)
  629. (*maxwordsize)[m] = (int)max((int)(*maxwordsize)[m],(int)strlen(dz->wordstor[total_words+m]));
  630. total_words += dz->wordcnt[n];
  631. }
  632. return(FINISHED);
  633. }
  634. /********************** GET_MAXPOSTDEC ****************************/
  635. int get_maxpostdec(int *maxpostdec,dataptr dz)
  636. {
  637. int total_words = 0, n, timewordno;
  638. for(n=0;n<dz->linecnt;n++) {
  639. timewordno = total_words + MIX_TIMEPOS;
  640. *maxpostdec = max(*maxpostdec,get_postdec(dz->wordstor[timewordno]));
  641. total_words += dz->wordcnt[n];
  642. }
  643. return(FINISHED);
  644. }
  645. /********************** GET_POSTDEC ****************************/
  646. int get_postdec(char *thisword)
  647. {
  648. char *p = thisword;
  649. int finished = FALSE;
  650. int post_dec = 0;
  651. while(*p != '.') {
  652. if(*p==ENDOFSTR) {
  653. finished = TRUE;
  654. break;
  655. }
  656. p++;
  657. }
  658. if(!finished) {
  659. p++;
  660. while(*p != ENDOFSTR) {
  661. post_dec++;
  662. p++;
  663. }
  664. }
  665. return post_dec;
  666. }
  667. /************************* MIX_GAIN ******************************/
  668. int mix_gain(dataptr dz)
  669. {
  670. int exit_status;
  671. int lineno, total_words;
  672. double *llevelstor, *rlevelstor;
  673. int *maxwordsize = NULL, lmaxlevelsize = 0, rmaxlevelsize = 0;
  674. if((dz->parray[MIX_LLEVELSTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  675. sprintf(errstr,"INSUFFICIENT MEMORY for left levels store.\n");
  676. return(MEMORY_ERROR);
  677. }
  678. if((dz->parray[MIX_RLEVELSTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  679. sprintf(errstr,"INSUFFICIENT MEMORY for right levels store.\n");
  680. return(MEMORY_ERROR);
  681. }
  682. llevelstor = dz->parray[MIX_LLEVELSTOR];
  683. rlevelstor = dz->parray[MIX_RLEVELSTOR];
  684. if((exit_status = get_maxwordsize(&maxwordsize,dz))<0)
  685. return(exit_status);
  686. total_words = 0;
  687. for(lineno=0;lineno<dz->linecnt;lineno++) {
  688. if((exit_status = get_levels(lineno,total_words,llevelstor,rlevelstor,&lmaxlevelsize,&rmaxlevelsize,dz))<0) {
  689. free(maxwordsize);
  690. return(exit_status);
  691. }
  692. total_words += dz->wordcnt[lineno];
  693. }
  694. total_words = 0;
  695. for(lineno=0;lineno<dz->linecnt;lineno++) {
  696. if((exit_status = convert_level
  697. (lineno,total_words,maxwordsize,llevelstor,rlevelstor,lmaxlevelsize,rmaxlevelsize,dz))<0) {
  698. free(maxwordsize);
  699. return(exit_status);
  700. }
  701. total_words += dz->wordcnt[lineno];
  702. }
  703. free(maxwordsize);
  704. return(FINISHED);
  705. }
  706. /********************** GET_LEVELS ****************************/
  707. int get_levels(int lineno,int total_words,
  708. double *llevelstor,double *rlevelstor,int *lmaxlevelsize,int *rmaxlevelsize,dataptr dz)
  709. {
  710. int exit_status;
  711. int wordcnt = dz->wordcnt[lineno];
  712. char *thisword;
  713. double level;
  714. int wordno = total_words + MIX_LEVELPOS;
  715. thisword = dz->wordstor[wordno];
  716. if((exit_status = get_level(thisword,&level))<0)
  717. return(exit_status);
  718. if(lineno >= dz->iparam[MSH_STARTLINE] && lineno < dz->iparam[MSH_ENDLINE])
  719. llevelstor[lineno] = level * dz->param[MIX_GAIN];
  720. else
  721. llevelstor[lineno] = level;
  722. sprintf(errstr,"%.4lf",level);
  723. *lmaxlevelsize = max(*lmaxlevelsize,(int)strlen(errstr));
  724. if(wordcnt <= MIX_RLEVELPOS)
  725. return(FINISHED);
  726. wordno = total_words + MIX_RLEVELPOS;
  727. thisword = dz->wordstor[wordno];
  728. if((exit_status = get_level(thisword,&level))<0)
  729. return(exit_status);
  730. if(lineno >= dz->iparam[MSH_STARTLINE] && lineno <= dz->iparam[MSH_ENDLINE])
  731. rlevelstor[lineno] = level * dz->param[MIX_GAIN];
  732. else
  733. rlevelstor[lineno] = level;
  734. sprintf(errstr,"%.4lf",level);
  735. *rmaxlevelsize = max(*rmaxlevelsize,(int)strlen(errstr));
  736. return(FINISHED);
  737. }
  738. /************************** CONVERT_LEVEL ***************************/
  739. int convert_level(int lineno,int total_words,int *maxwordsize,
  740. double *llevelstor,double *rlevelstor,int lmaxlevelsize,int rmaxlevelsize,dataptr dz)
  741. {
  742. int n, m, spacecnt;
  743. int wordno;
  744. int wordcnt = dz->wordcnt[lineno];
  745. for(n=0;n<wordcnt;n++) {
  746. wordno = total_words + n;
  747. switch(n) {
  748. case(MIX_NAMEPOS):
  749. case(MIX_CHANPOS):
  750. case(MIX_PANPOS):
  751. case(MIX_RPANPOS):
  752. case(MIX_TIMEPOS):
  753. if((spacecnt = maxwordsize[n] - strlen(dz->wordstor[wordno]))<0) {
  754. sprintf(errstr,"word alignment error: 1: convert_level()\n");
  755. return(PROGRAM_ERROR);
  756. }
  757. spacecnt++;
  758. fprintf(dz->fp,"%s",dz->wordstor[wordno]);
  759. for(m=0;m<spacecnt;m++)
  760. fprintf(dz->fp," ");
  761. break;
  762. case(MIX_LEVELPOS):
  763. sprintf(errstr,"%.4lf",llevelstor[lineno]);
  764. if((spacecnt = lmaxlevelsize - strlen(errstr))<0) {
  765. sprintf(errstr,"word alignment error: 2: convert_level()\n");
  766. return(PROGRAM_ERROR);
  767. }
  768. spacecnt++;
  769. fprintf(dz->fp,"%s",errstr);
  770. for(m=0;m<spacecnt;m++)
  771. fprintf(dz->fp," ");
  772. break;
  773. case(MIX_RLEVELPOS):
  774. sprintf(errstr,"%.4lf",rlevelstor[lineno]);
  775. if((spacecnt = rmaxlevelsize - strlen(errstr))<0) {
  776. sprintf(errstr,"word alignment error: 3: convert_level()\n");
  777. return(PROGRAM_ERROR);
  778. }
  779. spacecnt++;
  780. fprintf(dz->fp,"%s",errstr);
  781. for(m=0;m<spacecnt;m++)
  782. fprintf(dz->fp," ");
  783. break;
  784. default:
  785. sprintf(errstr,"Impossible line wordcnt: convert_level()\n");
  786. return(PROGRAM_ERROR);
  787. }
  788. }
  789. fprintf(dz->fp,"\n");
  790. return(FINISHED);
  791. }
  792. /*********************** DO_NAME_AND_TIME_REVERSE **************************/
  793. int do_name_and_time_reverse(dataptr dz)
  794. {
  795. int exit_status;
  796. double *timestor, *timediff;
  797. int *maxwordsize = NULL, postdec = 0;
  798. int *name_index;
  799. char *temp;
  800. int lineno, newlineno, total_words = 0, half_linecnt;
  801. if((dz->parray[MTW_TIMESTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  802. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  803. return(MEMORY_ERROR);
  804. }
  805. if((dz->parray[MTW_TIMEDIFF] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  806. sprintf(errstr,"INSUFFICIENT MEMORY for timegaps store.\n");
  807. return(MEMORY_ERROR);
  808. }
  809. if((name_index = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  810. sprintf(errstr,"INSUFFICIENT MEMORY for name index store.\n");
  811. return(PROGRAM_ERROR);
  812. }
  813. timestor = dz->parray[MTW_TIMESTOR];
  814. timediff = dz->parray[MTW_TIMEDIFF];
  815. if((exit_status = get_maxpostdec(&postdec,dz))<0) {
  816. free(name_index);
  817. return(exit_status);
  818. }
  819. if((exit_status = get_times_and_timediffs(timestor,timediff,dz))<0) {
  820. free(name_index);
  821. return(exit_status);
  822. }
  823. if((exit_status = reverse_times(timestor,timediff,dz))<0) {
  824. free(name_index);
  825. return(exit_status);
  826. }
  827. for(lineno=0;lineno<dz->linecnt;lineno++) {
  828. name_index[lineno] = total_words;
  829. total_words += dz->wordcnt[lineno];
  830. }
  831. half_linecnt = dz->iparam[MSH_ENDLINE] - dz->iparam[MSH_STARTLINE];
  832. half_linecnt = half_linecnt/2; /* TRUNCATE */
  833. half_linecnt += dz->iparam[MSH_STARTLINE];
  834. for(lineno=dz->iparam[MSH_STARTLINE],newlineno=dz->iparam[MSH_ENDLINE]-1;lineno < half_linecnt;lineno++,newlineno--) {
  835. temp = dz->wordstor[name_index[lineno]];
  836. dz->wordstor[name_index[lineno]] = dz->wordstor[name_index[newlineno]];
  837. dz->wordstor[name_index[newlineno]] = temp;
  838. }
  839. if((exit_status = retime_the_lines(timestor,postdec,dz))<0) {
  840. free(name_index);
  841. return(exit_status);
  842. }
  843. if((exit_status = get_maxwordsize(&maxwordsize,dz))<0) {
  844. free(name_index);
  845. return(exit_status);
  846. }
  847. if((exit_status = output_mixfile_lines(maxwordsize,dz))<0) {
  848. free(maxwordsize);
  849. free(name_index);
  850. return(exit_status);
  851. }
  852. free(maxwordsize);
  853. free(name_index);
  854. return(FINISHED);
  855. }
  856. /*********************** DO_NAME_AND_TIME_FREEZE **************************/
  857. int do_name_and_time_freeze(dataptr dz)
  858. {
  859. int exit_status;
  860. double *timestor, *timediff;
  861. int *maxwordsize = NULL, postdec = 0;
  862. int *name_index;
  863. int lineno, total_words = 0;
  864. if((dz->parray[MTW_TIMESTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  865. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  866. return(MEMORY_ERROR);
  867. }
  868. if((dz->parray[MTW_TIMEDIFF] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  869. sprintf(errstr,"INSUFFICIENT MEMORY for timegaps store.\n");
  870. return(MEMORY_ERROR);
  871. }
  872. if((name_index = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  873. sprintf(errstr,"INSUFFICIENT MEMORY for name index store.\n");
  874. return(PROGRAM_ERROR);
  875. }
  876. timestor = dz->parray[MTW_TIMESTOR];
  877. timediff = dz->parray[MTW_TIMEDIFF];
  878. if((exit_status = get_maxpostdec(&postdec,dz))<0) {
  879. free(name_index);
  880. return(exit_status);
  881. }
  882. if((exit_status = get_times_and_timediffs(timestor,timediff,dz))<0) {
  883. free(name_index);
  884. return(exit_status);
  885. }
  886. if((exit_status = freeze_times(timestor,timediff,dz))<0) {
  887. free(name_index);
  888. return(exit_status);
  889. }
  890. for(lineno=0;lineno<dz->linecnt;lineno++) {
  891. name_index[lineno] = total_words;
  892. total_words += dz->wordcnt[lineno];
  893. }
  894. for(lineno=dz->iparam[MSH_STARTLINE]+1;lineno < dz->iparam[MSH_ENDLINE];lineno++) {
  895. free(dz->wordstor[name_index[lineno]]);
  896. dz->wordstor[name_index[lineno]] = dz->wordstor[name_index[dz->iparam[MSH_STARTLINE]]];
  897. }
  898. if((exit_status = retime_the_lines(timestor,postdec,dz))<0) {
  899. free(name_index);
  900. return(exit_status);
  901. }
  902. if((exit_status = get_maxwordsize(&maxwordsize,dz))<0) {
  903. free(name_index);
  904. return(exit_status);
  905. }
  906. if((exit_status = output_mixfile_lines(maxwordsize,dz))<0) {
  907. free(maxwordsize);
  908. free(name_index);
  909. return(exit_status);
  910. }
  911. free(maxwordsize);
  912. free(name_index);
  913. return(FINISHED);
  914. }
  915. /*********************** TIMESORT_MIXFILE **************************/
  916. int timesort_mixfile(double *timestor,dataptr dz)
  917. {
  918. int n, m;
  919. int lineno, startword;
  920. double temptime, *timecopy;
  921. int *line_wordindex, total_words, running_total_words;
  922. int *permm, tempperm, *wordcnt_copy;
  923. char **wordcopy;
  924. total_words = 0;
  925. for(n=0;n<dz->linecnt;n++)
  926. total_words += dz->wordcnt[n];
  927. if((permm = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  928. sprintf(errstr,"INSUFFICIENT MEMORY for permutation.");
  929. return(MEMORY_ERROR);
  930. }
  931. if((wordcnt_copy = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  932. sprintf(errstr,"INSUFFICIENT MEMORY for wordcnt copy.");
  933. return(MEMORY_ERROR);
  934. }
  935. if((line_wordindex = (int *)malloc((dz->linecnt + 1) * sizeof(int)))==NULL) {
  936. sprintf(errstr,"INSUFFICIENT MEMORY for word index.");
  937. return(MEMORY_ERROR);
  938. }
  939. if((timecopy = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  940. sprintf(errstr,"INSUFFICIENT MEMORY for time copy.");
  941. return(MEMORY_ERROR);
  942. }
  943. if((wordcopy = (char **)malloc(total_words * sizeof(char *)))==NULL) {
  944. sprintf(errstr,"INSUFFICIENT MEMORY for word copy.");
  945. return(MEMORY_ERROR);
  946. }
  947. for(n=0;n<dz->linecnt;n++)
  948. permm[n] = n;
  949. for(n=0;n<dz->linecnt;n++)
  950. timecopy[n] = timestor[n];
  951. total_words = 0;
  952. for(n=0;n<dz->linecnt;n++) {
  953. line_wordindex[n] = total_words;
  954. total_words += dz->wordcnt[n];
  955. }
  956. line_wordindex[dz->linecnt] = total_words;
  957. for(n=1;n<dz->linecnt;n++) {
  958. temptime = timecopy[n];
  959. tempperm = permm[n];
  960. m = n-1;
  961. while(m >= 0 && timecopy[m] > temptime) {
  962. timecopy[m+1] = timecopy[m];
  963. permm[m+1] = permm[m];
  964. m--;
  965. }
  966. timecopy[m+1] = temptime;
  967. permm[m+1] = tempperm;
  968. }
  969. running_total_words = 0;
  970. for(n=0;n<dz->linecnt;n++) {
  971. lineno = permm[n];
  972. wordcnt_copy[n] = dz->wordcnt[permm[n]];
  973. startword = line_wordindex[lineno];
  974. //endword = line_wordindex[lineno+1];
  975. for(m=0;m<wordcnt_copy[n];m++) {
  976. wordcopy[running_total_words] = dz->wordstor[startword+m];
  977. if(++running_total_words > total_words) {
  978. free(permm);
  979. free(wordcnt_copy);
  980. free(line_wordindex);
  981. free(timecopy);
  982. free(wordcopy);
  983. sprintf(errstr,"Accounting error in timesort_mixfile()\n");
  984. return(PROGRAM_ERROR);
  985. }
  986. }
  987. }
  988. for(n=0;n<total_words;n++)
  989. dz->wordstor[n] = wordcopy[n];
  990. for(n=0;n<dz->linecnt;n++)
  991. dz->wordcnt[n] = wordcnt_copy[n];
  992. free(permm);
  993. free(wordcnt_copy);
  994. free(line_wordindex);
  995. free(timecopy);
  996. free(wordcopy);
  997. return(FINISHED);
  998. }
  999. /*********************** DELETE_WORDSTORAGE_SPACE_OF_ALL_DELETED_LINES **************************/
  1000. void delete_wordstorage_space_of_all_deleted_lines(int oldwordcnt,char **wordcopy,dataptr dz)
  1001. {
  1002. int n, m;
  1003. int is_kept;
  1004. for(m=0;m<oldwordcnt;m++) {
  1005. is_kept = FALSE;
  1006. for(n=0;n<dz->all_words;n++) {
  1007. if(wordcopy[m] == dz->wordstor[n]) {
  1008. is_kept = TRUE;
  1009. break;
  1010. }
  1011. }
  1012. if(!is_kept)
  1013. free(wordcopy[m]);
  1014. }
  1015. }
  1016. /********************** MIX_SYNC ****************************/
  1017. int mix_sync(dataptr dz)
  1018. {
  1019. int exit_status;
  1020. int srate;
  1021. int this_namelen, max_namelen = 0, max_timeword = 0, *inchans;
  1022. int n;
  1023. double maxfiletime = 0.0, midtime, *timestor;
  1024. if(dz->infile->filetype==MIXFILE)
  1025. return mix_sync_a_mixfile(dz);
  1026. if(dz->all_words==0) {
  1027. sprintf(errstr,"No words in source file.\n");
  1028. return(PROGRAM_ERROR);
  1029. }
  1030. if((dz->parray[MSY_TIMESTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  1031. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  1032. return(MEMORY_ERROR);
  1033. }
  1034. timestor = dz->parray[MSY_TIMESTOR];
  1035. if((dz->iparray[MSY_CHANS] = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  1036. sprintf(errstr,"INSUFFICIENT MEMORY for channels store.\n");
  1037. return(MEMORY_ERROR);
  1038. }
  1039. inchans = dz->iparray[MSY_CHANS];
  1040. if((exit_status = establish_file_data_storage_for_mix((int)1,dz))<0)
  1041. return(exit_status);
  1042. for(n=0;n<dz->all_words;n++) { /* for each sndfile name */
  1043. if((this_namelen = strlen(dz->wordstor[n]))<=0) {
  1044. sprintf(errstr,"filename error: line %d: mix_sync()\n",n+1);
  1045. return(PROGRAM_ERROR);
  1046. }
  1047. max_namelen = max(max_namelen,this_namelen);
  1048. if((exit_status = open_file_and_retrieve_props(n,dz->wordstor[n],&srate,dz))<0)
  1049. return(exit_status);
  1050. inchans[n] = dz->infile->channels;
  1051. /*RWD NB clac from sampsize */
  1052. timestor[n] = (double)(dz->insams[0]/dz->infile->channels)/(double)dz->infile->srate;
  1053. maxfiletime = max(maxfiletime,timestor[n]);
  1054. }
  1055. midtime = maxfiletime/2.0;
  1056. for(n=0;n<dz->all_words;n++) {
  1057. switch(dz->mode) {
  1058. case(MIX_SYNCEND): timestor[n] = maxfiletime - timestor[n]; break;
  1059. case(MIX_SYNCMID): timestor[n] = midtime - timestor[n]/2.0; break;
  1060. case(MIX_SYNCSTT): timestor[n] = 0.0; break;
  1061. default:
  1062. sprintf(errstr,"Unknown mode in mix_sync()\n");
  1063. return(PROGRAM_ERROR);
  1064. }
  1065. sprintf(errstr,"%.5lf",timestor[n]);
  1066. max_timeword = max(max_timeword,(int)strlen(errstr));
  1067. }
  1068. for(n=0;n<dz->all_words;n++) {
  1069. if((exit_status = sync_and_output_mixfile_line
  1070. (n,dz->wordstor[n],max_namelen,max_timeword,timestor[n],1.0,dz))<0)
  1071. return(exit_status);
  1072. }
  1073. return(FINISHED);
  1074. }
  1075. /********************** MIX_SYNC_A_MIXFILE ****************************/
  1076. int mix_sync_a_mixfile(dataptr dz)
  1077. {
  1078. int exit_status;
  1079. int srate;
  1080. int n, total_words;
  1081. double maxfiletime = 0.0, midtime, *timestor;
  1082. int *maxwordsize = NULL, postdec;
  1083. if(dz->linecnt==0) {
  1084. sprintf(errstr,"No words in source file.\n");
  1085. return(PROGRAM_ERROR);
  1086. }
  1087. if((dz->parray[MSY_TIMESTOR] = (double *)malloc(dz->linecnt * sizeof(double)))==NULL) {
  1088. sprintf(errstr,"INSUFFICIENT MEMORY for times store.\n");
  1089. return(MEMORY_ERROR);
  1090. }
  1091. timestor = dz->parray[MSY_TIMESTOR];
  1092. if((exit_status = establish_file_data_storage_for_mix((int)1,dz))<0)
  1093. return(exit_status);
  1094. total_words = 0;
  1095. for(n=0;n<dz->linecnt;n++) { /* for each sndfile name */
  1096. if((exit_status = open_file_and_retrieve_props(n,dz->wordstor[total_words],&srate,dz))<0)
  1097. return(exit_status);
  1098. /*RWD NB calc from sampsize */
  1099. timestor[n] = (double)(dz->insams[0]/dz->infile->channels)/(double)dz->infile->srate;
  1100. maxfiletime = max(maxfiletime,timestor[n]);
  1101. total_words += dz->wordcnt[n];
  1102. }
  1103. midtime = maxfiletime/2.0;
  1104. for(n=0;n<dz->linecnt;n++) {
  1105. switch(dz->mode) {
  1106. case(MIX_SYNCEND): timestor[n] = maxfiletime - timestor[n]; break;
  1107. case(MIX_SYNCMID): timestor[n] = midtime - timestor[n]/2.0; break;
  1108. case(MIX_SYNCSTT): timestor[n] = 0.0; break;
  1109. default:
  1110. sprintf(errstr,"Unknown mode in mix_sync()\n");
  1111. return(PROGRAM_ERROR);
  1112. }
  1113. }
  1114. postdec = DEFAULT_DECIMAL_REPRESENTATION;
  1115. if((exit_status = retime_the_lines(timestor,postdec,dz))<0)
  1116. return(exit_status);
  1117. if((exit_status = timesort_mixfile(timestor,dz))<0)
  1118. return(exit_status);
  1119. if((exit_status = get_maxwordsize(&maxwordsize,dz))<0)
  1120. return(exit_status);
  1121. if((exit_status = output_mixfile_lines(maxwordsize,dz))<0)
  1122. return(exit_status);
  1123. free(maxwordsize);
  1124. return(FINISHED);
  1125. }
  1126. /************************* SYNC_AND_OUTPUT_MIXFILE_LINE *******************************/
  1127. int sync_and_output_mixfile_line
  1128. (int lineno,char *filename,int max_namelen,int max_timeword,double timestor,double gain,dataptr dz)
  1129. {
  1130. int space_count, n;
  1131. if((space_count = max_namelen - strlen(filename))<0) {
  1132. sprintf(errstr,"Anomaly 1 in space counting: sync_and_output_mixfile_line()\n");
  1133. return(PROGRAM_ERROR);
  1134. }
  1135. space_count++;
  1136. fprintf(dz->fp,"%s",filename);
  1137. for(n=0;n<space_count;n++)
  1138. fprintf(dz->fp," ");
  1139. sprintf(errstr,"%.5lf",timestor);
  1140. if((space_count = max_timeword - strlen(errstr))<0) {
  1141. sprintf(errstr,"Anomaly 2 in space counting: sync_and_output_mixfile_line()\n");
  1142. return(PROGRAM_ERROR);
  1143. }
  1144. space_count++;
  1145. fprintf(dz->fp,"%s",errstr);
  1146. for(n=0;n<space_count;n++)
  1147. fprintf(dz->fp," ");
  1148. switch(dz->iparray[MSY_CHANS][lineno]) {
  1149. case(MONO):
  1150. if(flteq(gain,1.0))
  1151. fprintf(dz->fp," 1 1.0 C\n");
  1152. else
  1153. fprintf(dz->fp," 1 %.5lf C\n",gain);
  1154. break;
  1155. case(STEREO):
  1156. if(flteq(gain,1.0))
  1157. fprintf(dz->fp," 2 1.0 L 1.0 R\n");
  1158. else
  1159. fprintf(dz->fp," 2 %.5lf L %.5lf R\n",gain,gain);
  1160. break;
  1161. default:
  1162. sprintf(errstr,"Unknown case in create_and_output_mixfile_line()\n");
  1163. return(PROGRAM_ERROR);
  1164. }
  1165. return(FINISHED);
  1166. }