topantail2.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <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. #ifndef HUGE
  44. #define HUGE 3.40282347e+38F
  45. #endif
  46. char errstr[2400];
  47. int sloom = 0;
  48. int sloombatch = 0;
  49. int anal_infiles = 0;
  50. const char* cdp_version = "7.1.0";
  51. //CDP LIB REPLACEMENTS
  52. static int setup_topntail_application(dataptr dz);
  53. //static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  54. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  55. static int setup_topntail_param_ranges_and_defaults(dataptr dz);
  56. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  57. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  58. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  59. static int establish_application(dataptr dz);
  60. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  61. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  62. static int mark_parameter_types(dataptr dz,aplptr ap);
  63. //static int assign_file_data_storage(int infilecnt,dataptr dz);
  64. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  65. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  66. static int check_topntail_param_validity_and_consistency(dataptr dz);
  67. //static double dbtolevel(double val);
  68. //static int topntail(dataptr dz);
  69. static int create_topntail_buffer(dataptr dz);
  70. static int top_and_tail(dataptr dz);
  71. static int get_startsamp(float *ibuf,int chans,int nbuff,int *startsamp,double gate,double ngate,dataptr dz);
  72. static int get_endsamp(float *ibuf,int chans,int nbuff,int *endsamp,double gate,double ngate,dataptr dz);
  73. static int do_startsplice(int splicecnt,int startsamp,int *sampseek,int input_report,dataptr dz);
  74. static int advance_to_endsamp(int endsamp,int *sampseek,int input_report,dataptr dz);
  75. static int do_end_splice(int *k,int splicecnt,int input_report,dataptr dz);
  76. #define TOPNTAIL -1
  77. /**************************************** MAIN *********************************************/
  78. int main(int argc,char *argv[])
  79. {
  80. int exit_status;
  81. dataptr dz = NULL;
  82. char **cmdline;
  83. int cmdlinecnt;
  84. int n;
  85. //aplptr ap;
  86. int is_launched = FALSE;
  87. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  88. fprintf(stdout,"%s\n",cdp_version);
  89. fflush(stdout);
  90. return 0;
  91. }
  92. /* CHECK FOR SOUNDLOOM */
  93. if(sflinit("cdp")){
  94. sfperror("cdp: initialisation\n");
  95. return(FAILED);
  96. }
  97. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  98. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  99. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  100. return(FAILED);
  101. }
  102. if(argc == 1) {
  103. usage1();
  104. return(FAILED);
  105. } else if(argc == 2) {
  106. usage2(argv[1]);
  107. return(FAILED);
  108. }
  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. // setup_particular_application =
  120. if((exit_status = setup_topntail_application(dz))<0) {
  121. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  122. return(FAILED);
  123. }
  124. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  125. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  126. return(FAILED);
  127. }
  128. //ap = dz->application;
  129. // parse_infile_and_hone_type() =
  130. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  131. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  132. return(FAILED);
  133. }
  134. // setup_param_ranges_and_defaults() =
  135. if((exit_status = setup_topntail_param_ranges_and_defaults(dz))<0) {
  136. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  137. return(FAILED);
  138. }
  139. // open_first_infile CDP LIB
  140. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  141. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  142. return(FAILED);
  143. }
  144. cmdlinecnt--;
  145. cmdline++;
  146. // handle_extra_infiles() : redundant
  147. // handle_outfile() =
  148. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  149. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  150. return(FAILED);
  151. }
  152. // handle_formants() redundant
  153. // handle_formant_quiksearch() redundant
  154. // handle_special_data() redundant
  155. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  156. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  157. return(FAILED);
  158. }
  159. // check_param_validity_and_consistency() redundant
  160. if((exit_status = check_topntail_param_validity_and_consistency(dz))<0) {
  161. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  162. return(FAILED);
  163. }
  164. is_launched = TRUE;
  165. dz->bufcnt = 1;
  166. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  167. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  168. return(MEMORY_ERROR);
  169. }
  170. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  171. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  172. return(MEMORY_ERROR);
  173. }
  174. for(n = 0;n <dz->bufcnt; n++)
  175. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  176. dz->sampbuf[n] = (float *)0;
  177. if((exit_status = create_topntail_buffer(dz))<0) { // CDP LIB
  178. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  179. return(FAILED);
  180. }
  181. //param_preprocess() redundant
  182. //spec_process_file =
  183. if((exit_status = top_and_tail(dz))<0) {
  184. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  185. return(FAILED);
  186. }
  187. if((exit_status = complete_output(dz))<0) { // CDP LIB
  188. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  189. return(FAILED);
  190. }
  191. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  192. free(dz);
  193. return(SUCCEEDED);
  194. }
  195. /**********************************************
  196. REPLACED CDP LIB FUNCTIONS
  197. **********************************************/
  198. /****************************** SET_PARAM_DATA *********************************/
  199. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  200. {
  201. ap->special_data = (char)special_data;
  202. ap->param_cnt = (char)paramcnt;
  203. ap->max_param_cnt = (char)maxparamcnt;
  204. if(ap->max_param_cnt>0) {
  205. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  206. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  207. return(MEMORY_ERROR);
  208. }
  209. strcpy(ap->param_list,paramlist);
  210. }
  211. return(FINISHED);
  212. }
  213. /****************************** SET_VFLGS *********************************/
  214. int set_vflgs
  215. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  216. {
  217. ap->option_cnt = (char) optcnt; /*RWD added cast */
  218. if(optcnt) {
  219. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  220. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  221. return(MEMORY_ERROR);
  222. }
  223. strcpy(ap->option_list,optlist);
  224. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  225. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  226. return(MEMORY_ERROR);
  227. }
  228. strcpy(ap->option_flags,optflags);
  229. }
  230. ap->vflag_cnt = (char) vflagcnt;
  231. ap->variant_param_cnt = (char) vparamcnt;
  232. if(vflagcnt) {
  233. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  234. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  235. return(MEMORY_ERROR);
  236. }
  237. strcpy(ap->variant_list,varlist);
  238. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  239. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  240. return(MEMORY_ERROR);
  241. }
  242. strcpy(ap->variant_flags,varflags);
  243. }
  244. return(FINISHED);
  245. }
  246. /***************************** APPLICATION_INIT **************************/
  247. int application_init(dataptr dz)
  248. {
  249. int exit_status;
  250. int storage_cnt;
  251. int tipc, brkcnt;
  252. aplptr ap = dz->application;
  253. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  254. ap->total_input_param_cnt = (char)tipc;
  255. if(tipc>0) {
  256. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  257. return(exit_status);
  258. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  259. return(exit_status);
  260. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  261. return(exit_status);
  262. }
  263. brkcnt = tipc;
  264. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  265. if(brkcnt>0) {
  266. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  267. return(exit_status);
  268. }
  269. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  270. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  271. return(exit_status);
  272. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  273. return(exit_status);
  274. }
  275. if((exit_status = mark_parameter_types(dz,ap))<0)
  276. return(exit_status);
  277. // establish_infile_constants() replaced by
  278. dz->infilecnt = 1;
  279. //establish_bufptrs_and_extra_buffers():
  280. return(FINISHED);
  281. }
  282. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  283. /* RWD mallo changed to calloc; helps debug verison run as release! */
  284. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  285. {
  286. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  287. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  288. return(MEMORY_ERROR);
  289. }
  290. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  291. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  292. return(MEMORY_ERROR);
  293. }
  294. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  295. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  296. return(MEMORY_ERROR);
  297. }
  298. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  299. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  300. return(MEMORY_ERROR);
  301. }
  302. return(FINISHED);
  303. }
  304. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  305. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  306. {
  307. int n;
  308. for(n=0;n<storage_cnt;n++) {
  309. dz->is_int[n] = (char)0;
  310. dz->no_brk[n] = (char)0;
  311. }
  312. return(FINISHED);
  313. }
  314. /***************************** MARK_PARAMETER_TYPES **************************/
  315. int mark_parameter_types(dataptr dz,aplptr ap)
  316. {
  317. int n, m; /* PARAMS */
  318. for(n=0;n<ap->max_param_cnt;n++) {
  319. switch(ap->param_list[n]) {
  320. case('0'): break; /* dz->is_active[n] = 0 is default */
  321. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  322. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  323. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  324. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  325. default:
  326. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  327. return(PROGRAM_ERROR);
  328. }
  329. } /* OPTIONS */
  330. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  331. switch(ap->option_list[n]) {
  332. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  333. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  334. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  335. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  336. default:
  337. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  338. return(PROGRAM_ERROR);
  339. }
  340. } /* VARIANTS */
  341. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  342. switch(ap->variant_list[n]) {
  343. case('0'): break;
  344. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  345. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  346. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  347. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  348. default:
  349. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  350. return(PROGRAM_ERROR);
  351. }
  352. } /* INTERNAL */
  353. for(n=0,
  354. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  355. switch(ap->internal_param_list[n]) {
  356. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  357. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  358. case('d'): dz->no_brk[m] = (char)1; break;
  359. default:
  360. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  361. return(PROGRAM_ERROR);
  362. }
  363. }
  364. return(FINISHED);
  365. }
  366. /************************ HANDLE_THE_OUTFILE *********************/
  367. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  368. {
  369. int exit_status;
  370. char *filename = (*cmdline)[0];
  371. if(filename[0]=='-' && filename[1]=='f') {
  372. dz->floatsam_output = 1;
  373. dz->true_outfile_stype = SAMP_FLOAT;
  374. filename+= 2;
  375. }
  376. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  377. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  378. return(DATA_ERROR);
  379. }
  380. strcpy(dz->outfilename,filename);
  381. if((exit_status = create_sized_outfile(filename,dz))<0)
  382. return(exit_status);
  383. (*cmdline)++;
  384. (*cmdlinecnt)--;
  385. return(FINISHED);
  386. }
  387. /***************************** ESTABLISH_APPLICATION **************************/
  388. int establish_application(dataptr dz)
  389. {
  390. aplptr ap;
  391. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  392. sprintf(errstr,"establish_application()\n");
  393. return(MEMORY_ERROR);
  394. }
  395. ap = dz->application;
  396. memset((char *)ap,0,sizeof(struct applic));
  397. return(FINISHED);
  398. }
  399. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  400. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  401. {
  402. int n;
  403. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  404. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  405. return(MEMORY_ERROR);
  406. }
  407. for(n=0;n<tipc;n++)
  408. ap->default_val[n] = 0.0;
  409. return(FINISHED);
  410. }
  411. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  412. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  413. {
  414. int n;
  415. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  416. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  417. return(MEMORY_ERROR);
  418. }
  419. for(n=0;n<tipc;n++)
  420. dz->is_active[n] = (char)0;
  421. return(FINISHED);
  422. }
  423. /************************* SETUP_TOPNTAIL_APPLICATION *******************/
  424. int setup_topntail_application(dataptr dz)
  425. {
  426. int exit_status;
  427. aplptr ap;
  428. if((exit_status = establish_application(dz))<0) // GLOBAL
  429. return(FAILED);
  430. ap = dz->application;
  431. // SEE parstruct FOR EXPLANATION of next 2 functions
  432. if((exit_status = set_param_data(ap,0,2,2,"dd"))<0)
  433. return(FAILED);
  434. if((exit_status = set_vflgs(ap,"sb",2,"dd","",0,0,""))<0)
  435. return(FAILED);
  436. // set_legal_infile_structure -->
  437. dz->has_otherfile = FALSE;
  438. // assign_process_logic -->
  439. dz->input_data_type = SNDFILES_ONLY;
  440. dz->process_type = UNEQUAL_SNDFILE;
  441. dz->outfiletype = SNDFILE_OUT;
  442. return application_init(dz); //GLOBAL
  443. }
  444. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  445. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  446. {
  447. int exit_status;
  448. infileptr infile_info;
  449. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  450. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  451. return(MEMORY_ERROR);
  452. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  453. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  454. return(PROGRAM_ERROR);
  455. } else if(infile_info->filetype != SNDFILE) {
  456. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  457. return(DATA_ERROR);
  458. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  459. sprintf(errstr,"Failed to copy file parsing information\n");
  460. return(PROGRAM_ERROR);
  461. }
  462. free(infile_info);
  463. return(FINISHED);
  464. }
  465. /************************* SETUP_TOPNTAIL_PARAM_RANGES_AND_DEFAULTS *******************/
  466. int setup_topntail_param_ranges_and_defaults(dataptr dz)
  467. {
  468. int exit_status;
  469. aplptr ap = dz->application;
  470. // set_param_ranges()
  471. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  472. // NB total_input_param_cnt is > 0 !!!
  473. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  474. return(FAILED);
  475. // get_param_ranges()
  476. ap->lo[0] = 0;
  477. ap->hi[0] = 1;
  478. ap->default_val[0] = 0;
  479. ap->lo[1] = 0;
  480. ap->hi[1] = 1;
  481. ap->default_val[1] = 0;
  482. ap->lo[2] = 2;
  483. ap->hi[2] = 200;
  484. ap->default_val[2] = 15;
  485. ap->lo[3] = 0;
  486. ap->hi[3] = 1000;
  487. ap->default_val[3] = 0;
  488. dz->maxmode = 0;
  489. put_default_vals_in_all_params(dz);
  490. return(FINISHED);
  491. }
  492. /************************* redundant functions: to ensure libs compile OK *******************/
  493. int assign_process_logic(dataptr dz)
  494. {
  495. return(FINISHED);
  496. }
  497. void set_legal_infile_structure(dataptr dz)
  498. {}
  499. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  500. {
  501. return(FINISHED);
  502. }
  503. int setup_internal_arrays_and_array_pointers(dataptr dz)
  504. {
  505. return(FINISHED);
  506. }
  507. int establish_bufptrs_and_extra_buffers(dataptr dz)
  508. {
  509. return(FINISHED);
  510. }
  511. int read_special_data(char *str,dataptr dz)
  512. {
  513. return(FINISHED);
  514. }
  515. int inner_loop
  516. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  517. {
  518. return(FINISHED);
  519. }
  520. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  521. {
  522. return(FINISHED);
  523. }
  524. /******************************** USAGE1 ********************************/
  525. int usage1(void)
  526. {
  527. usage2("topantail");
  528. return(USAGE_ONLY);
  529. }
  530. /********************************************************************************************/
  531. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  532. {
  533. if(!strcmp(prog_identifier_from_cmdline,"topantail"))
  534. dz->process = TOPNTAIL;
  535. else {
  536. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  537. return(USAGE_ONLY);
  538. }
  539. return(FINISHED);
  540. }
  541. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  542. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  543. {
  544. int n;
  545. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  546. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  547. return(MEMORY_ERROR);
  548. }
  549. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  550. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  551. return(MEMORY_ERROR);
  552. }
  553. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  554. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  555. return(MEMORY_ERROR);
  556. }
  557. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  558. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  559. return(MEMORY_ERROR);
  560. }
  561. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  562. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  563. return(MEMORY_ERROR);
  564. }
  565. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  566. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  567. return(MEMORY_ERROR);
  568. }
  569. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  570. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  571. return(MEMORY_ERROR);
  572. }
  573. for(n=0;n<brkcnt;n++) {
  574. dz->brk[n] = NULL;
  575. dz->brkptr[n] = NULL;
  576. dz->brkinit[n] = 0;
  577. dz->brksize[n] = 0;
  578. }
  579. return(FINISHED);
  580. }
  581. /******************************** USAGE2 ********************************/
  582. int usage2(char *str)
  583. {
  584. if(!strcmp(str,"topantail")) {
  585. fprintf(stderr,
  586. "USAGE:\n"
  587. "topantail2 topantail infile outfile startgate endgate [-ssplicelen] [-bbacktrack]\n"
  588. "\n"
  589. "STARTGATE level at start before which sound is to be removed (0-1).\n"
  590. "ENDGATE level at end after which sound is to be removed (0-1).\n"
  591. "SPLICELEN splicelen in mS.\n"
  592. "BACKTRACK backtrack from initial gate point to an earlier splice point, in mS.\n"
  593. "\n");
  594. } else
  595. fprintf(stdout,"Unknown option '%s'\n",str);
  596. return(USAGE_ONLY);
  597. }
  598. int usage3(char *str1,char *str2)
  599. {
  600. fprintf(stderr,"Insufficient parameters on command line.\n");
  601. return(USAGE_ONLY);
  602. }
  603. /**************************** CHECK_TOPNTAIL_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  604. int check_topntail_param_validity_and_consistency(dataptr dz)
  605. {
  606. dz->iparam[3] = (int)round(dz->param[3] * MS_TO_SECS * dz->infile->srate) * dz->infile->channels;
  607. return FINISHED;
  608. }
  609. /*************************** CREATE_TOPNTAIL_BUFFER ************************/
  610. int create_topntail_buffer(dataptr dz)
  611. {
  612. size_t bigbufsize;
  613. bigbufsize = (long)Malloc(-1);
  614. dz->buflen = (int)(bigbufsize / sizeof(float));
  615. dz->buflen = (dz->buflen / dz->infile->channels) * dz->infile->channels;
  616. if((dz->bigbuf = (float*) Malloc(dz->buflen * sizeof(float)))== NULL){
  617. sprintf(errstr, "Can't allocate memory for sound.\n");
  618. return(MEMORY_ERROR);
  619. }
  620. return(FINISHED);
  621. }
  622. /******************************* TOP_AND_TAIL *******************************/
  623. int top_and_tail(dataptr dz)
  624. {
  625. int exit_status;
  626. float *ibuf = dz->bigbuf;
  627. float *obuf = ibuf;
  628. int nbuff;
  629. int chans = dz->infile->channels;
  630. int startsamp = 0;
  631. int endsamp = dz->insams[0];
  632. int splicecnt = round(dz->param[2] * MS_TO_SECS * (double)dz->infile->srate);
  633. int splicesamps = splicecnt * chans;
  634. int sampseek = 0;
  635. int startsplice, endsplice = 0, samps_to_write;
  636. double gate1, gate2;
  637. dz->ssampsread = 0;
  638. nbuff = dz->insams[0]/dz->buflen;
  639. if(nbuff * dz->buflen < dz->insams[0])
  640. nbuff++; /* number of buffers contaning entire file */
  641. gate1 = dz->param[0] * (double)F_MAXSAMP;
  642. gate2 = dz->param[1] * (double)F_MAXSAMP;
  643. if((exit_status = get_startsamp(ibuf,chans,nbuff,&startsamp,gate1,-gate1,dz))<0)
  644. return(exit_status);
  645. if((exit_status = get_endsamp(ibuf,chans,nbuff,&endsamp,gate2,-gate2,dz))<0)
  646. return(exit_status);
  647. if(endsamp == startsamp) {
  648. sprintf(errstr,"At this gate level, entire file will be removed.\n");
  649. return(GOAL_FAILED);
  650. }
  651. if((startsplice = startsamp - splicesamps) < 0)
  652. startsplice = 0;
  653. endsplice = endsamp;
  654. if((endsamp -= splicesamps) <= 0) {
  655. sprintf(errstr,"At this gate level, entire file will be removed.\n");
  656. return(GOAL_FAILED);
  657. }
  658. dz->ssampsread = 0;
  659. if(sndseekEx(dz->ifd[0],startsamp,0)<0) {
  660. sprintf(errstr,"sndseek() failed: 3\n");
  661. return(SYSTEM_ERROR);
  662. }
  663. if(startsplice != 0) {
  664. if((exit_status = do_startsplice(splicecnt,startsamp,&sampseek,0,dz))<0)
  665. return(exit_status);
  666. } else {
  667. if((exit_status = read_samps(dz->bigbuf,dz))<0) /* read buffer with additional sector */
  668. return(exit_status);
  669. }
  670. // endsamp = endsplice = dz->insams[0];
  671. if((exit_status = advance_to_endsamp(endsamp,&sampseek,0,dz))<0)
  672. return(exit_status);
  673. samps_to_write = endsamp - sampseek;
  674. if((exit_status = do_end_splice(&samps_to_write,splicecnt,0,dz))<0)
  675. return(exit_status);
  676. if(samps_to_write > 0) {
  677. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  678. return(exit_status);
  679. }
  680. dz->total_samps_written = endsplice - startsplice;
  681. return(FINISHED);
  682. }
  683. /******************************* GET_STARTSAMP *******************************/
  684. int get_startsamp(float *ibuf,int chans,int nbuff,int *startsamp,double gate,double ngate,dataptr dz)
  685. {
  686. int exit_status;
  687. int gotstart = FALSE;
  688. int thisbuff = 0;
  689. int n;
  690. while(!gotstart && thisbuff < nbuff) {
  691. if((exit_status = read_samps(ibuf,dz))<0)
  692. return(exit_status);
  693. for(n = 0;n < dz->ssampsread - 1;n++) {
  694. if(ibuf[n] > gate || ibuf[n] < ngate) {
  695. gotstart = TRUE;
  696. break;
  697. }
  698. }
  699. if(gotstart) {
  700. *startsamp = n + (thisbuff * dz->buflen);
  701. *startsamp = (*startsamp/chans) * chans; /* align to channel group boundary */
  702. break;
  703. }
  704. thisbuff++;
  705. }
  706. if(dz->iparam[3] > 0) {
  707. *startsamp -= dz->iparam[3];
  708. if(*startsamp <0)
  709. *startsamp = 0;
  710. }
  711. if(!gotstart) {
  712. sprintf(errstr,"Entire file is below gate level AAA\n");
  713. return(GOAL_FAILED);
  714. }
  715. return(FINISHED);
  716. }
  717. /******************************* GET_ENDSAMP *******************************/
  718. int get_endsamp(float *ibuf,int chans,int nbuff,int *endsamp,double gate,double ngate,dataptr dz)
  719. {
  720. int exit_status;
  721. int n;
  722. int gotend = FALSE;
  723. int thisbuff = nbuff - 1; /* buffer that contains last sample */
  724. while(!gotend && thisbuff >= 0) {
  725. if(sndseekEx(dz->ifd[0],thisbuff * dz->buflen,0)<0) {
  726. sprintf(errstr,"sndseek() failed: 1\n");
  727. return(SYSTEM_ERROR);
  728. }
  729. if((exit_status = read_samps(ibuf,dz))<0)
  730. return(exit_status);
  731. for(n = dz->ssampsread - 1;n>=0;n--) {
  732. if(ibuf[n] > gate || ibuf[n] < ngate) {
  733. gotend = TRUE;
  734. break;
  735. }
  736. }
  737. if(gotend) {
  738. *endsamp = n + (thisbuff * dz->buflen);
  739. *endsamp = (*endsamp/chans) * chans; /* align to channel group boundary */
  740. break;
  741. }
  742. thisbuff--;
  743. }
  744. if(!gotend) {
  745. sprintf(errstr,"Entire file is below gate level.\n");
  746. return(GOAL_FAILED);
  747. }
  748. return(FINISHED);
  749. }
  750. /******************************* DO_STARTSPLICE *******************************/
  751. int do_startsplice(int splicecnt,int startsamp,int *sampseek,int input_report,dataptr dz)
  752. {
  753. int exit_status;
  754. int chans = dz->infile->channels;
  755. int k, samps_written;
  756. double aincr, a1;
  757. int n;
  758. int m;
  759. // NEW TW June 2004
  760. *sampseek = startsamp;
  761. if((exit_status = read_samps(dz->bigbuf,dz))<0) /* read buffer with additional sector */
  762. return(exit_status);
  763. k = 0;
  764. aincr = 1.0/(double)splicecnt;
  765. a1 = 0.0;
  766. for(n = 0;n < splicecnt;n++) {
  767. for(m = 0; m < chans; m++) {
  768. dz->bigbuf[k] = (float) ((double)dz->bigbuf[k] * a1);
  769. k++;
  770. }
  771. if(k >= dz->ssampsread) {
  772. if(input_report) {
  773. if((exit_status = write_samps_no_report(dz->bigbuf,dz->buflen,&samps_written,dz))<0)
  774. return(exit_status);
  775. } else {
  776. if((exit_status = write_samps(dz->bigbuf,dz->buflen,dz))<0)
  777. return(exit_status);
  778. }
  779. *sampseek += dz->ssampsread;
  780. if((exit_status = read_samps(dz->bigbuf,dz))<0) /*RWD added * */
  781. return(exit_status);
  782. if(dz->ssampsread <=0)
  783. return(FINISHED);
  784. k = 0;
  785. }
  786. a1 += aincr;
  787. }
  788. return(FINISHED);
  789. }
  790. /******************************* ADVANCE_TO_ENDSAMP *******************************/
  791. int advance_to_endsamp(int endsamp,int *sampseek,int input_report,dataptr dz)
  792. {
  793. int exit_status;
  794. int samps_written;
  795. while(endsamp > *sampseek + dz->ssampsread) {
  796. if(dz->ssampsread > 0) {
  797. if(input_report) {
  798. if((exit_status = write_samps_no_report(dz->bigbuf,dz->ssampsread,&samps_written,dz))<0)
  799. return(exit_status);
  800. } else {
  801. if((exit_status = write_samps(dz->bigbuf,dz->ssampsread,dz))<0)
  802. return(exit_status);
  803. }
  804. }
  805. *sampseek += dz->ssampsread;
  806. if((exit_status = read_samps(dz->bigbuf,dz))<0)
  807. return(exit_status);
  808. if(dz->ssampsread ==0) {
  809. return FINISHED;
  810. }
  811. }
  812. return(FINISHED);
  813. }
  814. /******************************* DO_END_SPLICE *******************************/
  815. int do_end_splice(int *k,int splicecnt,int input_report,dataptr dz)
  816. {
  817. int exit_status;
  818. int chans = dz->infile->channels;
  819. int n, samps_written;
  820. int m;
  821. double aincr = -(1.0/splicecnt);
  822. double a1 = 1.0 + aincr;
  823. for(n = 0;n < splicecnt;n++) {
  824. for(m = 0; m < chans; m++) {
  825. dz->bigbuf[*k] = (float) ((double)dz->bigbuf[*k] * a1);
  826. (*k)++;
  827. }
  828. if(*k >= dz->ssampsread) {
  829. if(dz->ssampsread > 0) {
  830. if(input_report) {
  831. if((exit_status = write_samps_no_report(dz->bigbuf,dz->ssampsread,&samps_written,dz))<0)
  832. return(exit_status);
  833. } else {
  834. if((exit_status = write_samps(dz->bigbuf,dz->ssampsread,dz))<0)
  835. return(exit_status);
  836. }
  837. }
  838. if((exit_status = read_samps(dz->bigbuf,dz))<0)
  839. return(exit_status);
  840. if(dz->ssampsread <=0) {
  841. return(FINISHED);
  842. }
  843. *k = 0;
  844. }
  845. a1 += aincr;
  846. }
  847. return(FINISHED);
  848. }