waveform.c 51 KB

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