rejoin.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <structures.h>
  24. #include <tkglobals.h>
  25. #include <pnames.h>
  26. #include <filetype.h>
  27. #include <processno.h>
  28. #include <modeno.h>
  29. #include <logic.h>
  30. #include <globcon.h>
  31. #include <cdpmain.h>
  32. #include <math.h>
  33. #include <mixxcon.h>
  34. #include <osbind.h>
  35. #include <standalone.h>
  36. #include <ctype.h>
  37. #include <sfsys.h>
  38. #include <string.h>
  39. #include <srates.h>
  40. #ifdef unix
  41. #define round(x) lround((x))
  42. #endif
  43. char errstr[2400];
  44. int anal_infiles = 1;
  45. int sloom = 0;
  46. int sloombatch = 0;
  47. const char* cdp_version = "7.1.0";
  48. //CDP LIB REPLACEMENTS
  49. static int check_rejoin_param_validity_and_consistency(dataptr dz);
  50. static int setup_rejoin_application(dataptr dz);
  51. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  52. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  53. static int setup_rejoin_param_ranges_and_defaults(dataptr dz);
  54. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  55. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  56. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  57. static int establish_application(dataptr dz);
  58. static int initialise_vflags(dataptr dz);
  59. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  60. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  61. static int mark_parameter_types(dataptr dz,aplptr ap);
  62. static int assign_file_data_storage(int infilecnt,dataptr dz);
  63. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  64. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  65. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  66. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  67. static int rejoin(dataptr dz);
  68. static int rejoin_param_preprocess(dataptr dz);
  69. static int reverse_sndread(float *ibuf,int fileno,int start, int toread, dataptr dz);
  70. /**************************************** MAIN *********************************************/
  71. int main(int argc,char *argv[])
  72. {
  73. int exit_status;
  74. dataptr dz = NULL;
  75. char **cmdline;
  76. int cmdlinecnt;
  77. int n;
  78. //aplptr ap;
  79. int is_launched = FALSE;
  80. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  81. fprintf(stdout,"%s\n",cdp_version);
  82. fflush(stdout);
  83. return 0;
  84. }
  85. /* CHECK FOR SOUNDLOOM */
  86. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  87. sloom = 0;
  88. sloombatch = 1;
  89. }
  90. if(sflinit("cdp")){
  91. sfperror("cdp: initialisation\n");
  92. return(FAILED);
  93. }
  94. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  95. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  96. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  97. return(FAILED);
  98. }
  99. if(!sloom) {
  100. if(argc == 1) {
  101. usage1();
  102. return(FAILED);
  103. } else if(argc == 2) {
  104. usage2(argv[1]);
  105. return(FAILED);
  106. }
  107. }
  108. if(!sloom) {
  109. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  110. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  111. return(FAILED);
  112. }
  113. cmdline = argv;
  114. cmdlinecnt = argc;
  115. if((get_the_process_no(argv[0],dz))<0)
  116. return(FAILED);
  117. cmdline++;
  118. cmdlinecnt--;
  119. dz->maxmode = 2;
  120. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  121. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  122. return(exit_status);
  123. }
  124. cmdline++;
  125. cmdlinecnt--;
  126. // setup_particular_application =
  127. if((exit_status = setup_rejoin_application(dz))<0) {
  128. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  129. return(FAILED);
  130. }
  131. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  132. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  133. return(FAILED);
  134. }
  135. } else {
  136. //parse_TK_data() =
  137. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  138. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  139. return(exit_status);
  140. }
  141. }
  142. //ap = dz->application;
  143. // parse_infile_and_hone_type() =
  144. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  145. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  146. return(FAILED);
  147. }
  148. // setup_param_ranges_and_defaults() =
  149. if((exit_status = setup_rejoin_param_ranges_and_defaults(dz))<0) {
  150. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(FAILED);
  152. }
  153. // open_first_infile CDP LIB
  154. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  155. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  156. return(FAILED);
  157. }
  158. cmdlinecnt--;
  159. cmdline++;
  160. if((exit_status = handle_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  161. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  162. return(FAILED);
  163. }
  164. // handle_outfile() =
  165. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  166. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  167. return(FAILED);
  168. }
  169. // handle_formants() redundant
  170. // handle_formant_quiksearch() redundant
  171. // handle_special_data() redundant
  172. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  173. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. // check_param_validity_and_consistency....
  177. if((exit_status = check_rejoin_param_validity_and_consistency(dz))<0) {
  178. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  179. return(FAILED);
  180. }
  181. is_launched = TRUE;
  182. dz->bufcnt = 2;
  183. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  184. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  185. return(MEMORY_ERROR);
  186. }
  187. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  188. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  189. return(MEMORY_ERROR);
  190. }
  191. for(n = 0;n <dz->bufcnt; n++)
  192. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  193. dz->sampbuf[n] = (float *)0;
  194. if((exit_status = create_sndbufs(dz))<0) { // CDP LIB
  195. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  196. return(FAILED);
  197. }
  198. if((exit_status = rejoin_param_preprocess(dz))<0) {
  199. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  200. return(FAILED);
  201. }
  202. //spec_process_file =
  203. if((exit_status = rejoin(dz))<0) {
  204. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  205. return(FAILED);
  206. }
  207. if((exit_status = complete_output(dz))<0) { // CDP LIB
  208. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  209. return(FAILED);
  210. }
  211. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  212. free(dz);
  213. return(SUCCEEDED);
  214. }
  215. /**********************************************
  216. REPLACED CDP LIB FUNCTIONS
  217. **********************************************/
  218. /****************************** SET_PARAM_DATA *********************************/
  219. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  220. {
  221. ap->special_data = (char)special_data;
  222. ap->param_cnt = (char)paramcnt;
  223. ap->max_param_cnt = (char)maxparamcnt;
  224. if(ap->max_param_cnt>0) {
  225. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  226. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  227. return(MEMORY_ERROR);
  228. }
  229. strcpy(ap->param_list,paramlist);
  230. }
  231. return(FINISHED);
  232. }
  233. /****************************** SET_VFLGS *********************************/
  234. int set_vflgs
  235. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  236. {
  237. ap->option_cnt = (char) optcnt; /*RWD added cast */
  238. if(optcnt) {
  239. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  240. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  241. return(MEMORY_ERROR);
  242. }
  243. strcpy(ap->option_list,optlist);
  244. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  245. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  246. return(MEMORY_ERROR);
  247. }
  248. strcpy(ap->option_flags,optflags);
  249. }
  250. ap->vflag_cnt = (char) vflagcnt;
  251. ap->variant_param_cnt = (char) vparamcnt;
  252. if(vflagcnt) {
  253. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  254. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  255. return(MEMORY_ERROR);
  256. }
  257. strcpy(ap->variant_list,varlist);
  258. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  259. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  260. return(MEMORY_ERROR);
  261. }
  262. strcpy(ap->variant_flags,varflags);
  263. }
  264. return(FINISHED);
  265. }
  266. /***************************** APPLICATION_INIT **************************/
  267. int application_init(dataptr dz)
  268. {
  269. int exit_status;
  270. int storage_cnt;
  271. int tipc, brkcnt;
  272. aplptr ap = dz->application;
  273. if(ap->vflag_cnt>0)
  274. initialise_vflags(dz);
  275. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  276. ap->total_input_param_cnt = (char)tipc;
  277. if(tipc>0) {
  278. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  279. return(exit_status);
  280. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  281. return(exit_status);
  282. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  283. return(exit_status);
  284. }
  285. brkcnt = tipc;
  286. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  287. if(brkcnt>0) {
  288. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  289. return(exit_status);
  290. }
  291. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  292. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  293. return(exit_status);
  294. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  295. return(exit_status);
  296. }
  297. if((exit_status = mark_parameter_types(dz,ap))<0)
  298. return(exit_status);
  299. // establish_infile_constants() replaced by
  300. dz->infilecnt = 1;
  301. //establish_bufptrs_and_extra_buffers():
  302. return(FINISHED);
  303. }
  304. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  305. /* RWD mallo changed to calloc; helps debug verison run as release! */
  306. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  307. {
  308. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  309. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  310. return(MEMORY_ERROR);
  311. }
  312. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  313. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  314. return(MEMORY_ERROR);
  315. }
  316. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  317. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  318. return(MEMORY_ERROR);
  319. }
  320. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  321. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  322. return(MEMORY_ERROR);
  323. }
  324. return(FINISHED);
  325. }
  326. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  327. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  328. {
  329. int n;
  330. for(n=0;n<storage_cnt;n++) {
  331. dz->is_int[n] = (char)0;
  332. dz->no_brk[n] = (char)0;
  333. }
  334. return(FINISHED);
  335. }
  336. /***************************** MARK_PARAMETER_TYPES **************************/
  337. int mark_parameter_types(dataptr dz,aplptr ap)
  338. {
  339. int n, m; /* PARAMS */
  340. for(n=0;n<ap->max_param_cnt;n++) {
  341. switch(ap->param_list[n]) {
  342. case('0'): break; /* dz->is_active[n] = 0 is default */
  343. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  344. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  345. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  346. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  347. default:
  348. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  349. return(PROGRAM_ERROR);
  350. }
  351. } /* OPTIONS */
  352. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  353. switch(ap->option_list[n]) {
  354. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  355. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  356. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  357. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  358. default:
  359. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  360. return(PROGRAM_ERROR);
  361. }
  362. } /* VARIANTS */
  363. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  364. switch(ap->variant_list[n]) {
  365. case('0'): break;
  366. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  367. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  368. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  369. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  370. default:
  371. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  372. return(PROGRAM_ERROR);
  373. }
  374. } /* INTERNAL */
  375. for(n=0,
  376. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  377. switch(ap->internal_param_list[n]) {
  378. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  379. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  380. case('d'): dz->no_brk[m] = (char)1; break;
  381. default:
  382. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  383. return(PROGRAM_ERROR);
  384. }
  385. }
  386. return(FINISHED);
  387. }
  388. /************************ HANDLE_THE_OUTFILE *********************/
  389. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  390. {
  391. int exit_status;
  392. char *filename = (*cmdline)[0];
  393. if(filename[0]=='-' && filename[1]=='f') {
  394. dz->floatsam_output = 1;
  395. dz->true_outfile_stype = SAMP_FLOAT;
  396. filename+= 2;
  397. }
  398. if(!sloom) {
  399. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  400. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  401. return(DATA_ERROR);
  402. }
  403. }
  404. strcpy(dz->outfilename,filename);
  405. if((exit_status = create_sized_outfile(filename,dz))<0)
  406. return(exit_status);
  407. (*cmdline)++;
  408. (*cmdlinecnt)--;
  409. return(FINISHED);
  410. }
  411. /***************************** ESTABLISH_APPLICATION **************************/
  412. int establish_application(dataptr dz)
  413. {
  414. aplptr ap;
  415. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  416. sprintf(errstr,"establish_application()\n");
  417. return(MEMORY_ERROR);
  418. }
  419. ap = dz->application;
  420. memset((char *)ap,0,sizeof(struct applic));
  421. return(FINISHED);
  422. }
  423. /************************* INITIALISE_VFLAGS *************************/
  424. int initialise_vflags(dataptr dz)
  425. {
  426. int n;
  427. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  428. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  429. return(MEMORY_ERROR);
  430. }
  431. for(n=0;n<dz->application->vflag_cnt;n++)
  432. dz->vflag[n] = FALSE;
  433. return FINISHED;
  434. }
  435. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  436. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  437. {
  438. int n;
  439. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  440. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  441. return(MEMORY_ERROR);
  442. }
  443. for(n=0;n<tipc;n++)
  444. ap->default_val[n] = 0.0;
  445. return(FINISHED);
  446. }
  447. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  448. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  449. {
  450. int n;
  451. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  452. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  453. return(MEMORY_ERROR);
  454. }
  455. for(n=0;n<tipc;n++)
  456. dz->is_active[n] = (char)0;
  457. return(FINISHED);
  458. }
  459. /************************* SETUP_REJOIN_APPLICATION *******************/
  460. int setup_rejoin_application(dataptr dz)
  461. {
  462. int exit_status;
  463. aplptr ap;
  464. if((exit_status = establish_application(dz))<0) // GLOBAL
  465. return(FAILED);
  466. ap = dz->application;
  467. if((exit_status = set_param_data(ap,0 ,0,0,""))<0)
  468. return(FAILED);
  469. if((exit_status = set_vflgs(ap,"g",1,"d","r",1,0,"0"))<0)
  470. return(FAILED);
  471. // set_legal_infile_structure -->
  472. dz->has_otherfile = FALSE;
  473. // assign_process_logic -->
  474. dz->input_data_type = MANY_SNDFILES;
  475. dz->process_type = UNEQUAL_SNDFILE;
  476. dz->outfiletype = SNDFILE_OUT;
  477. return application_init(dz); //GLOBAL
  478. }
  479. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  480. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  481. {
  482. int exit_status;
  483. infileptr infile_info;
  484. if(!sloom) {
  485. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  486. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  487. return(MEMORY_ERROR);
  488. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  489. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  490. return(PROGRAM_ERROR);
  491. } else if(infile_info->filetype != SNDFILE) {
  492. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  493. return(DATA_ERROR);
  494. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  495. sprintf(errstr,"Failed to copy file parsing information\n");
  496. return(PROGRAM_ERROR);
  497. }
  498. free(infile_info);
  499. }
  500. return(FINISHED);
  501. }
  502. /************************* SETUP_REJOIN_PARAM_RANGES_AND_DEFAULTS *******************/
  503. int setup_rejoin_param_ranges_and_defaults(dataptr dz)
  504. {
  505. int exit_status;
  506. aplptr ap = dz->application;
  507. // set_param_ranges()
  508. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  509. // NB total_input_param_cnt is > 0 !!!
  510. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  511. return(FAILED);
  512. // get_param_ranges()
  513. ap->lo[0] = 0;
  514. ap->hi[0] = 1;
  515. ap->default_val[0] = 1;
  516. dz->maxmode = 2;
  517. if(!sloom)
  518. put_default_vals_in_all_params(dz);
  519. return(FINISHED);
  520. }
  521. /********************************* PARSE_SLOOM_DATA *********************************/
  522. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  523. {
  524. int exit_status;
  525. int cnt = 1, infilecnt;
  526. int filesize, insams, inbrksize;
  527. double dummy;
  528. int true_cnt = 0;
  529. //aplptr ap;
  530. while(cnt<=PRE_CMDLINE_DATACNT) {
  531. if(cnt > argc) {
  532. sprintf(errstr,"Insufficient data sent from TK\n");
  533. return(DATA_ERROR);
  534. }
  535. switch(cnt) {
  536. case(1):
  537. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  538. sprintf(errstr,"Cannot read process no. sent from TK\n");
  539. return(DATA_ERROR);
  540. }
  541. break;
  542. case(2):
  543. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  544. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  545. return(DATA_ERROR);
  546. }
  547. if(dz->mode > 0)
  548. dz->mode--;
  549. //setup_particular_application() =
  550. if((exit_status = setup_rejoin_application(dz))<0)
  551. return(exit_status);
  552. //ap = dz->application;
  553. break;
  554. case(3):
  555. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  556. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  557. return(DATA_ERROR);
  558. }
  559. if(infilecnt < 1) {
  560. true_cnt = cnt + 1;
  561. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  562. }
  563. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  564. return(exit_status);
  565. break;
  566. case(INPUT_FILETYPE+4):
  567. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  568. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  569. return(DATA_ERROR);
  570. }
  571. break;
  572. case(INPUT_FILESIZE+4):
  573. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  574. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  575. return(DATA_ERROR);
  576. }
  577. dz->insams[0] = filesize;
  578. break;
  579. case(INPUT_INSAMS+4):
  580. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  581. sprintf(errstr,"Cannot read insams sent from TK\n");
  582. return(DATA_ERROR);
  583. }
  584. dz->insams[0] = insams;
  585. break;
  586. case(INPUT_SRATE+4):
  587. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  588. sprintf(errstr,"Cannot read srate sent from TK\n");
  589. return(DATA_ERROR);
  590. }
  591. break;
  592. case(INPUT_CHANNELS+4):
  593. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  594. sprintf(errstr,"Cannot read channels sent from TK\n");
  595. return(DATA_ERROR);
  596. }
  597. break;
  598. case(INPUT_STYPE+4):
  599. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  600. sprintf(errstr,"Cannot read stype sent from TK\n");
  601. return(DATA_ERROR);
  602. }
  603. break;
  604. case(INPUT_ORIGSTYPE+4):
  605. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  606. sprintf(errstr,"Cannot read origstype sent from TK\n");
  607. return(DATA_ERROR);
  608. }
  609. break;
  610. case(INPUT_ORIGRATE+4):
  611. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  612. sprintf(errstr,"Cannot read origrate sent from TK\n");
  613. return(DATA_ERROR);
  614. }
  615. break;
  616. case(INPUT_MLEN+4):
  617. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  618. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  619. return(DATA_ERROR);
  620. }
  621. break;
  622. case(INPUT_DFAC+4):
  623. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  624. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  625. return(DATA_ERROR);
  626. }
  627. break;
  628. case(INPUT_ORIGCHANS+4):
  629. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  630. sprintf(errstr,"Cannot read origchans sent from TK\n");
  631. return(DATA_ERROR);
  632. }
  633. break;
  634. case(INPUT_SPECENVCNT+4):
  635. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  636. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  637. return(DATA_ERROR);
  638. }
  639. dz->specenvcnt = dz->infile->specenvcnt;
  640. break;
  641. case(INPUT_WANTED+4):
  642. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  643. sprintf(errstr,"Cannot read wanted sent from TK\n");
  644. return(DATA_ERROR);
  645. }
  646. break;
  647. case(INPUT_WLENGTH+4):
  648. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  649. sprintf(errstr,"Cannot read wlength sent from TK\n");
  650. return(DATA_ERROR);
  651. }
  652. break;
  653. case(INPUT_OUT_CHANS+4):
  654. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  655. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  656. return(DATA_ERROR);
  657. }
  658. break;
  659. /* RWD these chanegs to samps - tk will have to deal with that! */
  660. case(INPUT_DESCRIPTOR_BYTES+4):
  661. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  662. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  663. return(DATA_ERROR);
  664. }
  665. break;
  666. case(INPUT_IS_TRANSPOS+4):
  667. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  668. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  669. return(DATA_ERROR);
  670. }
  671. break;
  672. case(INPUT_COULD_BE_TRANSPOS+4):
  673. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  674. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  675. return(DATA_ERROR);
  676. }
  677. break;
  678. case(INPUT_COULD_BE_PITCH+4):
  679. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  680. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  681. return(DATA_ERROR);
  682. }
  683. break;
  684. case(INPUT_DIFFERENT_SRATES+4):
  685. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  686. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  687. return(DATA_ERROR);
  688. }
  689. break;
  690. case(INPUT_DUPLICATE_SNDS+4):
  691. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  692. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  693. return(DATA_ERROR);
  694. }
  695. break;
  696. case(INPUT_BRKSIZE+4):
  697. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  698. sprintf(errstr,"Cannot read brksize sent from TK\n");
  699. return(DATA_ERROR);
  700. }
  701. if(inbrksize > 0) {
  702. switch(dz->input_data_type) {
  703. case(WORDLIST_ONLY):
  704. break;
  705. case(PITCH_AND_PITCH):
  706. case(PITCH_AND_TRANSPOS):
  707. case(TRANSPOS_AND_TRANSPOS):
  708. dz->tempsize = inbrksize;
  709. break;
  710. case(BRKFILES_ONLY):
  711. case(UNRANGED_BRKFILE_ONLY):
  712. case(DB_BRKFILES_ONLY):
  713. case(ALL_FILES):
  714. case(ANY_NUMBER_OF_ANY_FILES):
  715. if(dz->extrabrkno < 0) {
  716. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  717. return(DATA_ERROR);
  718. }
  719. if(dz->brksize == NULL) {
  720. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  721. return(PROGRAM_ERROR);
  722. }
  723. dz->brksize[dz->extrabrkno] = inbrksize;
  724. break;
  725. default:
  726. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  727. dz->input_data_type);
  728. return(PROGRAM_ERROR);
  729. }
  730. break;
  731. }
  732. break;
  733. case(INPUT_NUMSIZE+4):
  734. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  735. sprintf(errstr,"Cannot read numsize sent from TK\n");
  736. return(DATA_ERROR);
  737. }
  738. break;
  739. case(INPUT_LINECNT+4):
  740. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  741. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  742. return(DATA_ERROR);
  743. }
  744. break;
  745. case(INPUT_ALL_WORDS+4):
  746. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  747. sprintf(errstr,"Cannot read all_words sent from TK\n");
  748. return(DATA_ERROR);
  749. }
  750. break;
  751. case(INPUT_ARATE+4):
  752. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  753. sprintf(errstr,"Cannot read arate sent from TK\n");
  754. return(DATA_ERROR);
  755. }
  756. break;
  757. case(INPUT_FRAMETIME+4):
  758. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  759. sprintf(errstr,"Cannot read frametime sent from TK\n");
  760. return(DATA_ERROR);
  761. }
  762. dz->frametime = (float)dummy;
  763. break;
  764. case(INPUT_WINDOW_SIZE+4):
  765. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  766. sprintf(errstr,"Cannot read window_size sent from TK\n");
  767. return(DATA_ERROR);
  768. }
  769. break;
  770. case(INPUT_NYQUIST+4):
  771. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  772. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  773. return(DATA_ERROR);
  774. }
  775. break;
  776. case(INPUT_DURATION+4):
  777. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  778. sprintf(errstr,"Cannot read duration sent from TK\n");
  779. return(DATA_ERROR);
  780. }
  781. break;
  782. case(INPUT_MINBRK+4):
  783. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  784. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  785. return(DATA_ERROR);
  786. }
  787. break;
  788. case(INPUT_MAXBRK+4):
  789. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  790. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  791. return(DATA_ERROR);
  792. }
  793. break;
  794. case(INPUT_MINNUM+4):
  795. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  796. sprintf(errstr,"Cannot read minnum sent from TK\n");
  797. return(DATA_ERROR);
  798. }
  799. break;
  800. case(INPUT_MAXNUM+4):
  801. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  802. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  803. return(DATA_ERROR);
  804. }
  805. break;
  806. default:
  807. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  808. return(PROGRAM_ERROR);
  809. }
  810. cnt++;
  811. }
  812. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  813. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  814. return(DATA_ERROR);
  815. }
  816. if(true_cnt)
  817. cnt = true_cnt;
  818. *cmdlinecnt = 0;
  819. while(cnt < argc) {
  820. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  821. return(exit_status);
  822. cnt++;
  823. }
  824. return(FINISHED);
  825. }
  826. /********************************* GET_TK_CMDLINE_WORD *********************************/
  827. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  828. {
  829. if(*cmdlinecnt==0) {
  830. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  831. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  832. return(MEMORY_ERROR);
  833. }
  834. } else {
  835. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  836. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  837. return(MEMORY_ERROR);
  838. }
  839. }
  840. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  841. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  842. return(MEMORY_ERROR);
  843. }
  844. strcpy((*cmdline)[*cmdlinecnt],q);
  845. (*cmdlinecnt)++;
  846. return(FINISHED);
  847. }
  848. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  849. int assign_file_data_storage(int infilecnt,dataptr dz)
  850. {
  851. int exit_status;
  852. int no_sndfile_system_files = FALSE;
  853. dz->infilecnt = infilecnt;
  854. if((exit_status = allocate_filespace(dz))<0)
  855. return(exit_status);
  856. if(no_sndfile_system_files)
  857. dz->infilecnt = 0;
  858. return(FINISHED);
  859. }
  860. /************************* redundant functions: to ensure libs compile OK *******************/
  861. int assign_process_logic(dataptr dz)
  862. {
  863. return(FINISHED);
  864. }
  865. void set_legal_infile_structure(dataptr dz)
  866. {}
  867. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  868. {
  869. return(FINISHED);
  870. }
  871. int setup_internal_arrays_and_array_pointers(dataptr dz)
  872. {
  873. return(FINISHED);
  874. }
  875. int establish_bufptrs_and_extra_buffers(dataptr dz)
  876. {
  877. return(FINISHED);
  878. }
  879. int read_special_data(char *str,dataptr dz)
  880. {
  881. return(FINISHED);
  882. }
  883. int inner_loop
  884. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  885. {
  886. return(FINISHED);
  887. }
  888. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  889. {
  890. return(FINISHED);
  891. }
  892. /******************************** USAGE1 ********************************/
  893. int usage1(void)
  894. {
  895. usage2("rejoin");
  896. return(USAGE_ONLY);
  897. }
  898. /**************************** CHECK_REJOIN_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  899. int check_rejoin_param_validity_and_consistency(dataptr dz)
  900. {
  901. int n, longest;
  902. int maxsamps;
  903. if(flteq(dz->param[0],0.0)) {
  904. sprintf(errstr,"Output level is effectively zero: output file will be silent.\n");
  905. return(DATA_ERROR);
  906. }
  907. longest = 0;
  908. maxsamps = dz->insams[0];
  909. for(n=1;n <dz->infilecnt;n++) {
  910. if(dz->insams[n] > maxsamps) {
  911. maxsamps = dz->insams[n];
  912. longest = n;
  913. }
  914. }
  915. dz->temp_sampsize = maxsamps;
  916. dz->ringsize = longest;
  917. return FINISHED;
  918. }
  919. /**************************** REJOIN_PARAM_PREPROCESS *****************************/
  920. int rejoin_param_preprocess(dataptr dz)
  921. {
  922. // Set up arrays to store read-start-sample, buffer-count and buffer-total for each infile
  923. int n, firstread_bufno ;
  924. int *startsamp, *to_read, *bufloc, *orig_to_read, longest = dz->ringsize, firstread_sampend, maxsamps;
  925. dz->larray_cnt = 4;
  926. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  927. sprintf(errstr,"INSUFFICIENT MEMORY for internal integer arrays.\n");
  928. return(MEMORY_ERROR);
  929. }
  930. for(n = 0;n < 4;n++) {
  931. if((dz->lparray[n] = (int *)malloc(dz->infilecnt * sizeof(int)))==NULL) {
  932. sprintf(errstr,"INSUFFICIENT MEMORY to store segment cuts.\n");
  933. return(MEMORY_ERROR);
  934. }
  935. }
  936. maxsamps = dz->insams[longest];
  937. startsamp = dz->lparray[0];
  938. to_read = dz->lparray[1];
  939. bufloc = dz->lparray[2];
  940. orig_to_read = dz->lparray[3];
  941. for(n=0;n<dz->infilecnt;n++) {
  942. startsamp[n] = maxsamps - dz->insams[n]; // total-sampcnt in longest file, where new file N starts to be read
  943. firstread_bufno = startsamp[n]/dz->buflen; // Buffer in which file N is first read
  944. firstread_sampend = min((firstread_bufno + 1) * dz->buflen, maxsamps); // sampcnt-location IN THE LONGEST FILE of last sample in 1st read of file N
  945. to_read[n] = firstread_sampend - startsamp[n]; // Number of samples read on first read of file N
  946. bufloc[n] = 0; // Initial location in its own buffer for file N
  947. orig_to_read[n] = to_read[n];
  948. }
  949. return FINISHED;
  950. }
  951. /********************************************************************************************/
  952. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  953. {
  954. if(!strcmp(prog_identifier_from_cmdline,"rejoin")) dz->process = REJOIN;
  955. else {
  956. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  957. return(USAGE_ONLY);
  958. }
  959. return(FINISHED);
  960. }
  961. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  962. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  963. {
  964. int n;
  965. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  966. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  967. return(MEMORY_ERROR);
  968. }
  969. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  970. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  971. return(MEMORY_ERROR);
  972. }
  973. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  974. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  975. return(MEMORY_ERROR);
  976. }
  977. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  978. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  979. return(MEMORY_ERROR);
  980. }
  981. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  982. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  983. return(MEMORY_ERROR);
  984. }
  985. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  986. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  987. return(MEMORY_ERROR);
  988. }
  989. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  990. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  991. return(MEMORY_ERROR);
  992. }
  993. for(n=0;n<brkcnt;n++) {
  994. dz->brk[n] = NULL;
  995. dz->brkptr[n] = NULL;
  996. dz->brkinit[n] = 0;
  997. dz->brksize[n] = 0;
  998. }
  999. return(FINISHED);
  1000. }
  1001. /******************************** USAGE2 ********************************/
  1002. int usage2(char *str)
  1003. {
  1004. if(!strcmp(str,"rejoin")) {
  1005. fprintf(stdout,
  1006. "USAGE:\n"
  1007. "rejoin rejoin 1-2 infile infile2 [infile3 ....] [-ggain] [-r]\n"
  1008. "\n"
  1009. "Remix segment-files originating in \"isolate\" process.\n"
  1010. "Mode 1: Remix segment files only.\n"
  1011. "Mode 2: Remix segment files and remnant file.\n"
  1012. " NB Remnant file must be LAST infile in list of input files.\n"
  1013. "\n"
  1014. "GAIN Change output level (Range 0 - 1).\n"
  1015. " Note that output level is automatically turned down if output clips.\n"
  1016. "\n"
  1017. "-r Reverse the segment-files before mixing with remnant file.\n"
  1018. "\n");
  1019. } else
  1020. fprintf(stdout,"Unknown option '%s'\n",str);
  1021. return(USAGE_ONLY);
  1022. }
  1023. int usage3(char *str1,char *str2)
  1024. {
  1025. fprintf(stderr,"Insufficient parameters on command line.\n");
  1026. return(USAGE_ONLY);
  1027. }
  1028. /******************************** REJOIN ********************************/
  1029. int rejoin(dataptr dz)
  1030. {
  1031. int exit_status, fileno;
  1032. int maxlen = dz->temp_sampsize, longest = dz->ringsize, sampsread = 0, n, writeloc;
  1033. float *ibuf = dz->sampbuf[0], *obuf = dz->sampbuf[1];
  1034. double gain = dz->param[0], maxsamp = 0.0, normaliser;
  1035. int *startsamp = dz->lparray[0], *to_read = dz->lparray[1], *bufloc = dz->lparray[2], *orig_to_read = dz->lparray[3];
  1036. dz->total_samps_read = 0;
  1037. fprintf(stdout,"INFO: Pass 1: Test output level\n");
  1038. fflush(stdout);
  1039. while(dz->total_samps_read < maxlen) {
  1040. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1041. memset((char *)obuf,0,dz->buflen * sizeof(short));
  1042. if(dz->vflag[0]) {
  1043. if((exit_status = reverse_sndread(ibuf,longest,bufloc[longest],to_read[longest],dz)) <0)
  1044. return exit_status;
  1045. for(n=0;n<dz->ssampsread;n++)
  1046. obuf[n] = (float)(ibuf[n] * gain);
  1047. bufloc[longest] += to_read[longest];
  1048. if(bufloc[longest] + dz->buflen > dz->insams[longest])
  1049. to_read[longest] = dz->insams[longest] - bufloc[longest];
  1050. else
  1051. to_read[longest] = dz->buflen;
  1052. for(fileno=0; fileno < dz->infilecnt;fileno++) {
  1053. if(fileno != longest) {
  1054. if(startsamp[fileno] < dz->total_samps_read) { // Only if we've reached start of file
  1055. if(bufloc[fileno] == 0) // If not yet started reading from file
  1056. writeloc = startsamp[fileno] % dz->buflen; // Write-location is somewhere in current buffer
  1057. else // but if already written from this file
  1058. writeloc = 0; // Start writing from buffer start
  1059. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1060. if(dz->mode == 1 && fileno == dz->infilecnt-1) { // Read remnant file forwards
  1061. if((sampsread = fgetfbufEx(ibuf, to_read[fileno],dz->ifd[fileno],0)) < 0) {
  1062. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1063. return(SYSTEM_ERROR);
  1064. }
  1065. } else {
  1066. if((exit_status = reverse_sndread(ibuf,fileno,bufloc[fileno],to_read[fileno],dz)) <0)
  1067. return exit_status;
  1068. }
  1069. for(n=writeloc;n<to_read[fileno];n++)
  1070. obuf[n] = (float)(obuf[n] + (ibuf[n] * gain));
  1071. bufloc[fileno] += to_read[fileno];
  1072. if(bufloc[fileno] + dz->buflen > dz->insams[fileno])
  1073. to_read[fileno] = dz->insams[fileno] - bufloc[fileno];
  1074. else
  1075. to_read[fileno] = dz->buflen;
  1076. }
  1077. }
  1078. }
  1079. } else {
  1080. if((dz->ssampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[longest],0)) < 0) {
  1081. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1082. return(SYSTEM_ERROR);
  1083. }
  1084. if(dz->ssampsread == 0) {
  1085. fprintf(stdout,"WARNING: Non-fatal error 1 in reading input samples.\n");
  1086. fflush(stdout);
  1087. break;
  1088. }
  1089. dz->total_samps_read += dz->ssampsread;
  1090. for(n=0;n<dz->ssampsread;n++)
  1091. obuf[n] = (float)(ibuf[n] * gain);
  1092. for(fileno=0; fileno < dz->infilecnt;fileno++) {
  1093. if(fileno != longest) {
  1094. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1095. if((sampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[fileno],0)) < 0) {
  1096. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1097. return(SYSTEM_ERROR);
  1098. }
  1099. if(sampsread > 0) {
  1100. for(n=0;n<sampsread;n++)
  1101. obuf[n] = (float)(obuf[n] + (ibuf[n] * gain));
  1102. }
  1103. }
  1104. }
  1105. }
  1106. for(n=0;n < dz->ssampsread;n++) {
  1107. if(fabs(obuf[n]) > maxsamp)
  1108. maxsamp = fabs(obuf[n]);
  1109. }
  1110. }
  1111. if(maxsamp <= 0.0) {
  1112. sprintf(errstr,"No output level after mixing.\n");
  1113. return(DATA_ERROR);
  1114. }
  1115. if(maxsamp > 0.95)
  1116. normaliser = 1.0/maxsamp;
  1117. else
  1118. normaliser = 1.0;
  1119. for(n=0;n <dz->infilecnt;n++)
  1120. sndseekEx(dz->ifd[n],0,0);
  1121. dz->total_samps_read = 0;
  1122. dz->total_samps_written = 0;
  1123. for(n = 0;n < dz->infilecnt;n++) {
  1124. to_read[n] = orig_to_read[n];
  1125. bufloc[n] = 0;
  1126. }
  1127. fprintf(stdout,"INFO: Pass 2: Rejoining sounds\n");
  1128. fflush(stdout);
  1129. while(dz->total_samps_read < maxlen) {
  1130. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1131. memset((char *)obuf,0,dz->buflen * sizeof(short));
  1132. if(dz->vflag[0]) {
  1133. if((exit_status = reverse_sndread(ibuf,longest,bufloc[longest],to_read[longest],dz)) <0)
  1134. return exit_status;
  1135. for(n=0;n<dz->ssampsread;n++)
  1136. obuf[n] = (float)(ibuf[n] * gain);
  1137. bufloc[longest] += to_read[longest];
  1138. if(bufloc[longest] + dz->buflen > dz->insams[longest])
  1139. to_read[longest] = dz->insams[longest] - bufloc[longest];
  1140. else
  1141. to_read[longest] = dz->buflen;
  1142. for(fileno=0; fileno < dz->infilecnt;fileno++) {
  1143. if(fileno != longest) {
  1144. if(startsamp[fileno] < dz->total_samps_read) { // Only if we've reached start of file
  1145. if(bufloc[fileno] == 0) // If not yet started reading from file
  1146. writeloc = startsamp[fileno] % dz->buflen; // Write-location is somewhere in current buffer
  1147. else // but if already written from this file
  1148. writeloc = 0; // Start writing from buffer start
  1149. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1150. if(dz->mode == 1 && fileno == dz->infilecnt-1) { // Read remnant file forwards
  1151. if((sampsread = fgetfbufEx(ibuf,to_read[fileno],dz->ifd[fileno],0)) < 0) {
  1152. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1153. return(SYSTEM_ERROR);
  1154. }
  1155. } else {
  1156. if((exit_status = reverse_sndread(ibuf,fileno,bufloc[fileno],to_read[fileno],dz)) <0)
  1157. return exit_status;
  1158. }
  1159. for(n=writeloc;n<to_read[fileno];n++)
  1160. obuf[n] = (float)(obuf[n] + (ibuf[n] * gain));
  1161. bufloc[fileno] += to_read[fileno];
  1162. if(bufloc[fileno] + dz->buflen > dz->insams[fileno])
  1163. to_read[fileno] = dz->insams[fileno] - bufloc[fileno];
  1164. else
  1165. to_read[fileno] = dz->buflen;
  1166. }
  1167. }
  1168. }
  1169. } else {
  1170. if((dz->ssampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[longest],0)) < 0) {
  1171. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1172. return(SYSTEM_ERROR);
  1173. }
  1174. if(dz->ssampsread == 0) {
  1175. fprintf(stdout,"WARNING: Non-fatal error 1 in reading input samples.\n");
  1176. fflush(stdout);
  1177. break;
  1178. }
  1179. dz->total_samps_read += dz->ssampsread;
  1180. for(n=0;n<dz->ssampsread;n++)
  1181. obuf[n] = (float)(ibuf[n] * gain);
  1182. for(fileno=0; fileno < dz->infilecnt;fileno++) {
  1183. if(fileno != longest) {
  1184. memset((char *)ibuf,0,dz->buflen * sizeof(short));
  1185. if((sampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[fileno],0)) < 0) {
  1186. sprintf(errstr,"Can't read samples from input soundfile.\n");
  1187. return(SYSTEM_ERROR);
  1188. }
  1189. if(sampsread > 0) {
  1190. for(n=0;n<sampsread;n++)
  1191. obuf[n] = (float)(obuf[n] + (ibuf[n] * gain));
  1192. }
  1193. }
  1194. }
  1195. }
  1196. if(normaliser < 1.0) {
  1197. for(n=0;n<dz->ssampsread;n++)
  1198. obuf[n] = (float)(obuf[n] * normaliser);
  1199. }
  1200. if((exit_status = write_samps(obuf,dz->ssampsread,dz))<0)
  1201. return(exit_status);
  1202. }
  1203. return FINISHED;
  1204. }
  1205. /****************************** GET_MODE *********************************/
  1206. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1207. {
  1208. char temp[200], *p;
  1209. if(sscanf(str,"%s",temp)!=1) {
  1210. sprintf(errstr,"Cannot read mode of program.\n");
  1211. return(USAGE_ONLY);
  1212. }
  1213. p = temp + strlen(temp) - 1;
  1214. while(p >= temp) {
  1215. if(!isdigit(*p)) {
  1216. fprintf(stderr,"Invalid mode of program entered.\n");
  1217. return(USAGE_ONLY);
  1218. }
  1219. p--;
  1220. }
  1221. if(sscanf(str,"%d",&dz->mode)!=1) {
  1222. fprintf(stderr,"Cannot read mode of program.\n");
  1223. return(USAGE_ONLY);
  1224. }
  1225. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1226. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1227. return(USAGE_ONLY);
  1228. }
  1229. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1230. return(FINISHED);
  1231. }
  1232. /******************************** REVERSE_SNDREAD *************************
  1233. *
  1234. * Reverse buffer and read it.
  1235. * Takes a normal buffer location and number-of-samples-to-read.
  1236. * The buffer-location is then REVERSED in the file (i.e. to total-insams - buffer-location)
  1237. * the samples are read from [there MINUS number-of-samples-to-read].
  1238. * Then the samples read are reversed in order.
  1239. */
  1240. int reverse_sndread(float *ibuf,int fileno,int start, int toread, dataptr dz)
  1241. {
  1242. int longest = dz->ringsize;
  1243. int n, m, j, up, dn, sampsread;
  1244. float temp;
  1245. sndseekEx(dz->ifd[fileno],dz->insams[fileno] - (start + toread),0);
  1246. if((sampsread = fgetfbufEx(ibuf,toread,dz->ifd[fileno],0)) < 0) {
  1247. sprintf(errstr,"Can't read samples from input soundfile %d.\n",fileno+1);
  1248. return(SYSTEM_ERROR);
  1249. }
  1250. if(toread != sampsread) {
  1251. sprintf(errstr,"Anomaly in file reading for file %d\n",fileno+1);
  1252. return(PROGRAM_ERROR);
  1253. }
  1254. if(fileno == longest) {
  1255. dz->ssampsread = sampsread;
  1256. dz->total_samps_read += dz->ssampsread;
  1257. }
  1258. n = sampsread/2;
  1259. if(n * 2 != sampsread) { // ODD count of samples
  1260. up = n+1;
  1261. dn = n-1;
  1262. } else {
  1263. up = n;
  1264. dn = n-1;
  1265. }
  1266. for(m=up,j = dn;j >= 0;m++,j--) {
  1267. temp = ibuf[j];
  1268. ibuf[j] = ibuf[m];
  1269. ibuf[m] = temp;
  1270. }
  1271. return FINISHED;
  1272. }