specsphinx.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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 <pnames.h>
  25. #include <speccon.h>
  26. #include <standalone.h>
  27. #include <tkglobals.h>
  28. #include <filetype.h>
  29. #include <modeno.h>
  30. #include <cdpmain.h>
  31. #include <logic.h>
  32. #include <globcon.h>
  33. #include <cdpmain.h>
  34. #include <sfsys.h>
  35. #include <osbind.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #ifdef unix
  39. #define round(x) lround((x))
  40. #endif
  41. #define EIGHT_OVER_SEVEN (1.142857143)
  42. #define LOCSPREAD (5)
  43. static int specsphinx(dataptr dz);
  44. static int specvolve(dataptr dz);
  45. static int spec_sphinx(float* inbuf1,float* inbuf2,float* ampval,int* amploc,float* frqval,int* frqloc,dataptr dz);
  46. char errstr[2400];
  47. const char* cdp_version = "7.1.0";
  48. /* extern */ int sloom = 0;
  49. /* extern */ int sloombatch = 0;
  50. /* extern */ int anal_infiles = 1;
  51. /* extern */ int is_converted_to_stereo = -1;
  52. /* CDP LIBRARY FUNCTIONS TRANSFERRED HERE */
  53. static int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist);
  54. static int set_vflgs(aplptr ap,char *optflags,int optcnt,char *optlist,
  55. char *varflags,int vflagcnt, int vparamcnt,char *varlist);
  56. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  57. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  58. static int mark_parameter_types(dataptr dz,aplptr ap);
  59. static int establish_application(dataptr dz);
  60. static int application_init(dataptr dz);
  61. static int initialise_vflags(dataptr dz);
  62. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  63. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  64. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  65. static int assign_file_data_storage(int infilecnt,dataptr dz);
  66. //static int store_wordlist(char *filename,dataptr dz);
  67. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  68. /* CDP LIB FUNCTION MODIFIED TO AVOID CALLING setup_particular_application() */
  69. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  70. /* SIMPLIFICATION OF LIB FUNC TO APPLY TO JUST THIS FUNCTION */
  71. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  72. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz);
  73. static int setup_specsphinx_application(dataptr dz);
  74. static int setup_specsphinx_param_ranges_and_defaults(dataptr dz);
  75. static int open_the_first_infile(char *filename,dataptr dz);
  76. static int handle_the_extra_infiles(char ***cmdline,int *cmdlinecnt,dataptr dz);
  77. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  78. /**************************************** MAIN *********************************************/
  79. int main(int argc,char *argv[])
  80. {
  81. int exit_status;
  82. /* FILE *fp = NULL; */
  83. dataptr dz = NULL;
  84. // char *special_data_string = NULL;
  85. char **cmdline;
  86. int cmdlinecnt;
  87. //aplptr ap;
  88. int is_launched = FALSE;
  89. /* CHECK FOR SOUNDLOOM */
  90. //TW UPDATE
  91. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  92. fprintf(stdout,"%s\n",cdp_version);
  93. fflush(stdout);
  94. return 0;
  95. }
  96. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  97. sloom = 0;
  98. sloombatch = 1;
  99. }
  100. if(sflinit("cdp")){
  101. sfperror("cdp: initialisation\n");
  102. return(FAILED);
  103. }
  104. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  105. if((exit_status = establish_datastructure(&dz))<0) {
  106. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  107. return(FAILED);
  108. }
  109. if(!sloom) {
  110. /* INITIAL CHECK OF CMDLINE DATA */
  111. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) {
  112. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  113. return(FAILED);
  114. }
  115. cmdline = argv; /* GET PRE_DATA, ALLOCATE THE APPLICATION, CHECK FOR EXTRA INFILES */
  116. cmdlinecnt = argc;
  117. // get_process_and_mode_from_cmdline -->
  118. if (!strcmp(argv[0],"specsphinx")) {
  119. dz->process = SPECSPHINX;
  120. } else
  121. usage1();
  122. cmdline++;
  123. cmdlinecnt--;
  124. dz->maxmode = 2;
  125. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  126. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  127. return(exit_status);
  128. }
  129. cmdline++;
  130. cmdlinecnt--;
  131. // setup_particular_application =
  132. if((exit_status = setup_specsphinx_application(dz))<0) {
  133. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  134. return(FAILED);
  135. }
  136. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) {
  137. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  138. return(FAILED);
  139. }
  140. } else {
  141. //parse_TK_data() =
  142. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) { /* includes setup_particular_application() */
  143. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);/* and cmdlinelength check = sees extra-infiles */
  144. return(exit_status);
  145. }
  146. }
  147. //ap = dz->application;
  148. // parse_infile_and_hone_type() =
  149. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  150. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(FAILED);
  152. }
  153. // setup_param_ranges_and_defaults = MOVED IN THIS CASE ONLY TO LATER
  154. /* OPEN FIRST INFILE AND STORE DATA, AND INFORMATION, APPROPRIATELY */
  155. if((exit_status = open_the_first_infile(cmdline[0],dz))<0) {
  156. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  157. return(FAILED);
  158. }
  159. cmdlinecnt--;
  160. cmdline++;
  161. if((exit_status = handle_the_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  162. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  163. return(FAILED);
  164. }
  165. if((exit_status = setup_specsphinx_param_ranges_and_defaults(dz))<0) {
  166. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  167. return(FAILED);
  168. }
  169. // FOR display_virtual_time
  170. dz->tempsize = min(dz->insams[0],dz->insams[1]);
  171. // handle_outfile
  172. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) {
  173. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. // handle_formants
  177. // handle_formant_quiksearch
  178. // handle_special_data
  179. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. // check_param_validity_and_consistency =
  184. is_launched = TRUE;
  185. if((exit_status = allocate_double_buffer(dz))<0){
  186. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  187. return(FAILED);
  188. }
  189. // param_preprocess =
  190. // spec_process_file
  191. if(dz->mode == 0) {
  192. if((exit_status = specsphinx(dz))<0) {
  193. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  194. return(FAILED);
  195. }
  196. } else {
  197. if((exit_status = specvolve(dz))<0) {
  198. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(FAILED);
  200. }
  201. }
  202. if((exit_status = complete_output(dz))<0) {
  203. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  204. return(FAILED);
  205. }
  206. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz);
  207. free(dz);
  208. return(SUCCEEDED);
  209. }
  210. /**********************************************
  211. REPLACED CDP LIB FUNCTIONS
  212. **********************************************/
  213. /****************************** SET_PARAM_DATA *********************************/
  214. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  215. {
  216. ap->special_data = (char)special_data;
  217. ap->param_cnt = (char)paramcnt;
  218. ap->max_param_cnt = (char)maxparamcnt;
  219. if(ap->max_param_cnt>0) {
  220. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  221. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  222. return(MEMORY_ERROR);
  223. }
  224. strcpy(ap->param_list,paramlist);
  225. }
  226. return(FINISHED);
  227. }
  228. /****************************** SET_VFLGS *********************************/
  229. int set_vflgs
  230. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  231. {
  232. ap->option_cnt = (char) optcnt; /*RWD added cast */
  233. if(optcnt) {
  234. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  235. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  236. return(MEMORY_ERROR);
  237. }
  238. strcpy(ap->option_list,optlist);
  239. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  240. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  241. return(MEMORY_ERROR);
  242. }
  243. strcpy(ap->option_flags,optflags);
  244. }
  245. ap->vflag_cnt = (char) vflagcnt;
  246. ap->variant_param_cnt = (char) vparamcnt;
  247. if(vflagcnt) {
  248. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  249. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  250. return(MEMORY_ERROR);
  251. }
  252. strcpy(ap->variant_list,varlist);
  253. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  254. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  255. return(MEMORY_ERROR);
  256. }
  257. strcpy(ap->variant_flags,varflags);
  258. }
  259. return(FINISHED);
  260. }
  261. /***************************** APPLICATION_INIT **************************/
  262. int application_init(dataptr dz)
  263. {
  264. int exit_status;
  265. int storage_cnt, n;
  266. int tipc, brkcnt;
  267. aplptr ap = dz->application;
  268. if(ap->vflag_cnt>0)
  269. initialise_vflags(dz);
  270. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  271. ap->total_input_param_cnt = (char)tipc;
  272. if(tipc>0) {
  273. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  274. return(exit_status);
  275. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  276. return(exit_status);
  277. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  278. return(exit_status);
  279. }
  280. brkcnt = tipc;
  281. if(brkcnt>0) {
  282. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  283. return(exit_status);
  284. }
  285. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  286. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  287. return(exit_status);
  288. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  289. return(exit_status);
  290. }
  291. if((exit_status = mark_parameter_types(dz,ap))<0)
  292. return(exit_status);
  293. // establish_infile_constants() replaced by
  294. dz->infilecnt = -2; /* flags 2 or more */
  295. // establish_bufptrs_and_extra_buffers
  296. dz->extra_bufcnt = 0;
  297. dz->bptrcnt = 5;
  298. // setup_internal_arrays_and_array_pointers()
  299. dz->fptr_cnt = 2;
  300. dz->array_cnt = 5;
  301. if((dz->fptr = (float **)malloc(dz->fptr_cnt * sizeof(float *)))==NULL) {
  302. sprintf(errstr,"INSUFFICIENT MEMORY for internal float-pointer arrays.\n");
  303. return(MEMORY_ERROR);
  304. }
  305. if(dz->array_cnt > 0) {
  306. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  307. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  308. return(MEMORY_ERROR);
  309. }
  310. for(n=0;n<dz->array_cnt;n++)
  311. dz->parray[n] = NULL;
  312. }
  313. for(n=0;n<dz->fptr_cnt;n++)
  314. dz->fptr[n] = NULL;
  315. return establish_spec_bufptrs_and_extra_buffers(dz);
  316. }
  317. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  318. /* RWD mallo changed to calloc; helps debug verison run as release! */
  319. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  320. {
  321. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  322. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  323. return(MEMORY_ERROR);
  324. }
  325. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  326. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  327. return(MEMORY_ERROR);
  328. }
  329. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  330. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  331. return(MEMORY_ERROR);
  332. }
  333. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  334. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  335. return(MEMORY_ERROR);
  336. }
  337. return(FINISHED);
  338. }
  339. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  340. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  341. {
  342. int n;
  343. for(n=0;n<storage_cnt;n++) {
  344. dz->is_int[n] = (char)0;
  345. dz->no_brk[n] = (char)0;
  346. }
  347. return(FINISHED);
  348. }
  349. /***************************** MARK_PARAMETER_TYPES **************************/
  350. int mark_parameter_types(dataptr dz,aplptr ap)
  351. {
  352. int n, m; /* PARAMS */
  353. for(n=0;n<ap->max_param_cnt;n++) {
  354. switch(ap->param_list[n]) {
  355. case('0'): break; /* dz->is_active[n] = 0 is default */
  356. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  357. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  358. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  359. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  360. default:
  361. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  362. return(PROGRAM_ERROR);
  363. }
  364. } /* OPTIONS */
  365. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  366. switch(ap->option_list[n]) {
  367. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  368. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  369. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  370. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  371. default:
  372. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  373. return(PROGRAM_ERROR);
  374. }
  375. } /* VARIANTS */
  376. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  377. switch(ap->variant_list[n]) {
  378. case('0'): break;
  379. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  380. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  381. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  382. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  383. default:
  384. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  385. return(PROGRAM_ERROR);
  386. }
  387. } /* INTERNAL */
  388. for(n=0,
  389. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  390. switch(ap->internal_param_list[n]) {
  391. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  392. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  393. case('d'): dz->no_brk[m] = (char)1; break;
  394. default:
  395. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  396. return(PROGRAM_ERROR);
  397. }
  398. }
  399. return(FINISHED);
  400. }
  401. /***************************** ESTABLISH_APPLICATION **************************/
  402. int establish_application(dataptr dz)
  403. {
  404. aplptr ap;
  405. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  406. sprintf(errstr,"establish_application()\n");
  407. return(MEMORY_ERROR);
  408. }
  409. ap = dz->application;
  410. memset((char *)ap,0,sizeof(struct applic));
  411. return(FINISHED);
  412. }
  413. /************************* INITIALISE_VFLAGS *************************/
  414. int initialise_vflags(dataptr dz)
  415. {
  416. int n;
  417. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  418. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  419. return(MEMORY_ERROR);
  420. }
  421. for(n=0;n<dz->application->vflag_cnt;n++)
  422. dz->vflag[n] = FALSE;
  423. return FINISHED;
  424. }
  425. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  426. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  427. {
  428. int n;
  429. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  430. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  431. return(MEMORY_ERROR);
  432. }
  433. for(n=0;n<tipc;n++)
  434. ap->default_val[n] = 0.0;
  435. return(FINISHED);
  436. }
  437. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  438. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  439. {
  440. int n;
  441. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  442. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  443. return(MEMORY_ERROR);
  444. }
  445. for(n=0;n<tipc;n++)
  446. dz->is_active[n] = (char)0;
  447. return(FINISHED);
  448. }
  449. /************************* SETUP_SPECSPHINX_APPLICATION *******************/
  450. int setup_specsphinx_application(dataptr dz)
  451. {
  452. int exit_status;
  453. aplptr ap;
  454. if((exit_status = establish_application(dz))<0) // GLOBAL
  455. return(FAILED);
  456. ap = dz->application;
  457. // SEE parstruct FOR EXPLANATION of next 2 functions
  458. if((exit_status = set_param_data(ap,0 ,0,0,"" ))<0)
  459. return(FAILED);
  460. if(dz->mode == 0) {
  461. if((exit_status = set_vflgs(ap, "af",2,"DD" ,"" ,0,0,"" ))<0)
  462. return(FAILED);
  463. } else {
  464. if((exit_status = set_vflgs(ap, "bg",2,"DD" ,"" ,0,0,"" ))<0)
  465. return(FAILED);
  466. }
  467. // THERE IS NO NEED TO set_formant_flags in this case....
  468. // Following only needed if internal params are linked to dz structure
  469. // set_legal_infile_structure -->
  470. dz->has_otherfile = FALSE;
  471. // assign_process_logic -->
  472. dz->input_data_type = TWO_ANALFILES;
  473. dz->process_type = BIG_ANALFILE;
  474. dz->outfiletype = ANALFILE_OUT;
  475. return application_init(dz); //GLOBAL
  476. }
  477. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  478. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  479. {
  480. int n;
  481. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  482. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  483. return(MEMORY_ERROR);
  484. }
  485. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  486. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  487. return(MEMORY_ERROR);
  488. }
  489. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  490. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  491. return(MEMORY_ERROR);
  492. }
  493. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  494. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  495. return(MEMORY_ERROR);
  496. }
  497. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  498. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  499. return(MEMORY_ERROR);
  500. }
  501. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  502. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  503. return(MEMORY_ERROR);
  504. }
  505. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  506. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  507. return(MEMORY_ERROR);
  508. }
  509. for(n=0;n<brkcnt;n++) {
  510. dz->brk[n] = NULL;
  511. dz->brkptr[n] = NULL;
  512. dz->brkinit[n] = 0;
  513. dz->brksize[n] = 0;
  514. }
  515. return(FINISHED);
  516. }
  517. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  518. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  519. {
  520. int exit_status;
  521. infileptr infile_info;
  522. if(!sloom) {
  523. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  524. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  525. return(MEMORY_ERROR);
  526. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  527. sprintf(errstr,"Failed tp parse input file %s\n",cmdline[0]);
  528. return(PROGRAM_ERROR);
  529. } else if(infile_info->filetype != ANALFILE) {
  530. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  531. return(DATA_ERROR);
  532. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  533. sprintf(errstr,"Failed to copy file parsing information\n");
  534. return(PROGRAM_ERROR);
  535. }
  536. free(infile_info);
  537. }
  538. if((exit_status = set_chunklens_and_establish_windowbufs(dz))<0)
  539. return(exit_status);
  540. return(FINISHED);
  541. }
  542. /************************* SETUP_SPECSPHINX_PARAM_RANGES_AND_DEFAULTS *******************/
  543. int setup_specsphinx_param_ranges_and_defaults(dataptr dz)
  544. {
  545. int exit_status;
  546. aplptr ap = dz->application;
  547. // set_param_ranges()
  548. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  549. // setup_input_param_range_stores()
  550. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  551. return(FAILED);
  552. // get_param_ranges()
  553. if(dz->mode == 0) {
  554. ap->lo[0] = 0.0;
  555. ap->hi[0] = 1.0;
  556. ap->default_val[0] = 0.0;
  557. ap->lo[1] = 0.0;
  558. ap->hi[1] = 1.0;
  559. ap->default_val[1] = 0.0;
  560. } else {
  561. ap->lo[0] = -1.0;
  562. ap->hi[0] = 1.0;
  563. ap->default_val[0] = 0.0;
  564. ap->lo[1] = 0.01;
  565. ap->hi[1] = 100.0;
  566. ap->default_val[1] = 1.0;
  567. }
  568. dz->maxmode = 2;
  569. if(!sloom)
  570. put_default_vals_in_all_params(dz);
  571. return(FINISHED);
  572. }
  573. /********************************* PARSE_SLOOM_DATA *********************************/
  574. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  575. {
  576. int exit_status;
  577. int cnt = 1, infilecnt;
  578. int filesize, insams, inbrksize;
  579. double dummy;
  580. int true_cnt = 0;
  581. //aplptr ap;
  582. while(cnt<=PRE_CMDLINE_DATACNT) {
  583. if(cnt > argc) {
  584. sprintf(errstr,"Insufficient data sent from TK\n");
  585. return(DATA_ERROR);
  586. }
  587. switch(cnt) {
  588. case(1):
  589. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  590. sprintf(errstr,"Cannot read process no. sent from TK\n");
  591. return(DATA_ERROR);
  592. }
  593. break;
  594. case(2):
  595. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  596. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  597. return(DATA_ERROR);
  598. }
  599. if(dz->mode > 0)
  600. dz->mode--;
  601. //setup_particular_application() =
  602. if((exit_status = setup_specsphinx_application(dz))<0)
  603. return(exit_status);
  604. //ap = dz->application;
  605. break;
  606. case(3):
  607. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  608. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  609. return(DATA_ERROR);
  610. }
  611. if(infilecnt < 1) {
  612. true_cnt = cnt + 1;
  613. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  614. }
  615. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  616. return(exit_status);
  617. break;
  618. case(INPUT_FILETYPE+4):
  619. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  620. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  621. return(DATA_ERROR);
  622. }
  623. break;
  624. case(INPUT_FILESIZE+4):
  625. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  626. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  627. return(DATA_ERROR);
  628. }
  629. dz->insams[0] = filesize;
  630. break;
  631. case(INPUT_INSAMS+4):
  632. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  633. sprintf(errstr,"Cannot read insams sent from TK\n");
  634. return(DATA_ERROR);
  635. }
  636. dz->insams[0] = insams;
  637. break;
  638. case(INPUT_SRATE+4):
  639. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  640. sprintf(errstr,"Cannot read srate sent from TK\n");
  641. return(DATA_ERROR);
  642. }
  643. break;
  644. case(INPUT_CHANNELS+4):
  645. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  646. sprintf(errstr,"Cannot read channels sent from TK\n");
  647. return(DATA_ERROR);
  648. }
  649. break;
  650. case(INPUT_STYPE+4):
  651. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  652. sprintf(errstr,"Cannot read stype sent from TK\n");
  653. return(DATA_ERROR);
  654. }
  655. break;
  656. case(INPUT_ORIGSTYPE+4):
  657. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  658. sprintf(errstr,"Cannot read origstype sent from TK\n");
  659. return(DATA_ERROR);
  660. }
  661. break;
  662. case(INPUT_ORIGRATE+4):
  663. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  664. sprintf(errstr,"Cannot read origrate sent from TK\n");
  665. return(DATA_ERROR);
  666. }
  667. break;
  668. case(INPUT_MLEN+4):
  669. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  670. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  671. return(DATA_ERROR);
  672. }
  673. break;
  674. case(INPUT_DFAC+4):
  675. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  676. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  677. return(DATA_ERROR);
  678. }
  679. break;
  680. case(INPUT_ORIGCHANS+4):
  681. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  682. sprintf(errstr,"Cannot read origchans sent from TK\n");
  683. return(DATA_ERROR);
  684. }
  685. break;
  686. case(INPUT_SPECENVCNT+4):
  687. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  688. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  689. return(DATA_ERROR);
  690. }
  691. dz->specenvcnt = dz->infile->specenvcnt;
  692. break;
  693. case(INPUT_WANTED+4):
  694. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  695. sprintf(errstr,"Cannot read wanted sent from TK\n");
  696. return(DATA_ERROR);
  697. }
  698. break;
  699. case(INPUT_WLENGTH+4):
  700. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  701. sprintf(errstr,"Cannot read wlength sent from TK\n");
  702. return(DATA_ERROR);
  703. }
  704. break;
  705. case(INPUT_OUT_CHANS+4):
  706. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  707. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  708. return(DATA_ERROR);
  709. }
  710. break;
  711. /* RWD these chanegs to samps - tk will have to deal with that! */
  712. case(INPUT_DESCRIPTOR_BYTES+4):
  713. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  714. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  715. return(DATA_ERROR);
  716. }
  717. break;
  718. case(INPUT_IS_TRANSPOS+4):
  719. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  720. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  721. return(DATA_ERROR);
  722. }
  723. break;
  724. case(INPUT_COULD_BE_TRANSPOS+4):
  725. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  726. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  727. return(DATA_ERROR);
  728. }
  729. break;
  730. case(INPUT_COULD_BE_PITCH+4):
  731. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  732. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  733. return(DATA_ERROR);
  734. }
  735. break;
  736. case(INPUT_DIFFERENT_SRATES+4):
  737. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  738. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  739. return(DATA_ERROR);
  740. }
  741. break;
  742. case(INPUT_DUPLICATE_SNDS+4):
  743. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  744. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  745. return(DATA_ERROR);
  746. }
  747. break;
  748. case(INPUT_BRKSIZE+4):
  749. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  750. sprintf(errstr,"Cannot read brksize sent from TK\n");
  751. return(DATA_ERROR);
  752. }
  753. if(inbrksize > 0) {
  754. switch(dz->input_data_type) {
  755. case(WORDLIST_ONLY):
  756. break;
  757. case(PITCH_AND_PITCH):
  758. case(PITCH_AND_TRANSPOS):
  759. case(TRANSPOS_AND_TRANSPOS):
  760. dz->tempsize = inbrksize;
  761. break;
  762. case(BRKFILES_ONLY):
  763. case(UNRANGED_BRKFILE_ONLY):
  764. case(DB_BRKFILES_ONLY):
  765. case(ALL_FILES):
  766. case(ANY_NUMBER_OF_ANY_FILES):
  767. if(dz->extrabrkno < 0) {
  768. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  769. return(DATA_ERROR);
  770. }
  771. if(dz->brksize == NULL) {
  772. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  773. return(PROGRAM_ERROR);
  774. }
  775. dz->brksize[dz->extrabrkno] = inbrksize;
  776. break;
  777. default:
  778. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  779. dz->input_data_type);
  780. return(PROGRAM_ERROR);
  781. }
  782. break;
  783. }
  784. break;
  785. case(INPUT_NUMSIZE+4):
  786. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  787. sprintf(errstr,"Cannot read numsize sent from TK\n");
  788. return(DATA_ERROR);
  789. }
  790. break;
  791. case(INPUT_LINECNT+4):
  792. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  793. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  794. return(DATA_ERROR);
  795. }
  796. break;
  797. case(INPUT_ALL_WORDS+4):
  798. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  799. sprintf(errstr,"Cannot read all_words sent from TK\n");
  800. return(DATA_ERROR);
  801. }
  802. break;
  803. case(INPUT_ARATE+4):
  804. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  805. sprintf(errstr,"Cannot read arate sent from TK\n");
  806. return(DATA_ERROR);
  807. }
  808. break;
  809. case(INPUT_FRAMETIME+4):
  810. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  811. sprintf(errstr,"Cannot read frametime sent from TK\n");
  812. return(DATA_ERROR);
  813. }
  814. dz->frametime = (float)dummy;
  815. break;
  816. case(INPUT_WINDOW_SIZE+4):
  817. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  818. sprintf(errstr,"Cannot read window_size sent from TK\n");
  819. return(DATA_ERROR);
  820. }
  821. break;
  822. case(INPUT_NYQUIST+4):
  823. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  824. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  825. return(DATA_ERROR);
  826. }
  827. break;
  828. case(INPUT_DURATION+4):
  829. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  830. sprintf(errstr,"Cannot read duration sent from TK\n");
  831. return(DATA_ERROR);
  832. }
  833. break;
  834. case(INPUT_MINBRK+4):
  835. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  836. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  837. return(DATA_ERROR);
  838. }
  839. break;
  840. case(INPUT_MAXBRK+4):
  841. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  842. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  843. return(DATA_ERROR);
  844. }
  845. break;
  846. case(INPUT_MINNUM+4):
  847. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  848. sprintf(errstr,"Cannot read minnum sent from TK\n");
  849. return(DATA_ERROR);
  850. }
  851. break;
  852. case(INPUT_MAXNUM+4):
  853. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  854. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  855. return(DATA_ERROR);
  856. }
  857. break;
  858. default:
  859. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  860. return(PROGRAM_ERROR);
  861. }
  862. cnt++;
  863. }
  864. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  865. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  866. return(DATA_ERROR);
  867. }
  868. if(true_cnt)
  869. cnt = true_cnt;
  870. *cmdlinecnt = 0;
  871. while(cnt < argc) {
  872. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  873. return(exit_status);
  874. cnt++;
  875. }
  876. return(FINISHED);
  877. }
  878. /********************************* GET_TK_CMDLINE_WORD *********************************/
  879. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  880. {
  881. if(*cmdlinecnt==0) {
  882. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  883. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  884. return(MEMORY_ERROR);
  885. }
  886. } else {
  887. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  888. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  889. return(MEMORY_ERROR);
  890. }
  891. }
  892. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  893. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  894. return(MEMORY_ERROR);
  895. }
  896. strcpy((*cmdline)[*cmdlinecnt],q);
  897. (*cmdlinecnt)++;
  898. return(FINISHED);
  899. }
  900. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  901. int assign_file_data_storage(int infilecnt,dataptr dz)
  902. {
  903. int exit_status;
  904. int no_sndfile_system_files = FALSE;
  905. dz->infilecnt = infilecnt;
  906. if((exit_status = allocate_filespace(dz))<0)
  907. return(exit_status);
  908. if(no_sndfile_system_files)
  909. dz->infilecnt = 0;
  910. return(FINISHED);
  911. }
  912. /************************** OPEN_THE_FIRST_INFILE *****************************/
  913. int open_the_first_infile(char *filename,dataptr dz)
  914. {
  915. if((dz->ifd[0] = sndopenEx(filename,0,CDP_OPEN_RDONLY)) < 0) {
  916. sprintf(errstr,"Failure to open file %s for input.\n",filename);
  917. return(SYSTEM_ERROR);
  918. }
  919. if(dz->infilecnt<=0 || dz->infile->filetype!=ANALFILE) {
  920. sprintf(errstr,"%s is wrong type of file for this process.\n",filename);
  921. return(DATA_ERROR);
  922. }
  923. dz->samps_left = dz->insams[0];
  924. return(FINISHED);
  925. }
  926. /************************ HANDLE_THE_EXTRA_INFILES *********************/
  927. int handle_the_extra_infiles(char ***cmdline,int *cmdlinecnt,dataptr dz)
  928. {
  929. /* OPEN ANY FURTHER INFILES, CHECK COMPATIBILITY, STORE DATA AND INFO */
  930. int exit_status, n;
  931. char *filename;
  932. if(dz->infilecnt > 1) {
  933. for(n=1;n<dz->infilecnt;n++) {
  934. filename = (*cmdline)[0];
  935. if((exit_status = handle_other_infile(n,filename,dz))<0)
  936. return(exit_status);
  937. (*cmdline)++;
  938. (*cmdlinecnt)--;
  939. }
  940. } else {
  941. sprintf(errstr,"Insufficient input files for this process\n");
  942. return(DATA_ERROR);
  943. }
  944. return(FINISHED);
  945. }
  946. /************************ HANDLE_THE_OUTFILE *********************/
  947. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz)
  948. {
  949. char *filename = NULL;
  950. int n;
  951. int stype = SAMP_FLOAT;
  952. if(sloom) {
  953. filename = (*cmdline)[0];
  954. } else {
  955. if(*cmdlinecnt<=0) {
  956. sprintf(errstr,"Insufficient cmdline parameters.\n");
  957. return(USAGE_ONLY);
  958. }
  959. filename = (*cmdline)[0];
  960. if(filename[0]=='-' && filename[1]=='f') {
  961. sprintf(errstr,"-f flag used incorrectly on command line (output is not a sound file).\n");
  962. return(USAGE_ONLY);
  963. }
  964. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  965. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  966. return(DATA_ERROR);
  967. }
  968. }
  969. dz->true_outfile_stype = stype;
  970. dz->outfilesize = min(dz->insams[1],dz->insams[0]);
  971. if((dz->ofd = sndcreat_formatted(filename,dz->outfilesize,stype,
  972. dz->infile->channels,dz->infile->srate,CDP_CREATE_NORMAL)) < 0) {
  973. sprintf(errstr,"Cannot open output file %s\n", filename);
  974. return(DATA_ERROR);
  975. }
  976. dz->outchans = dz->infile->channels;
  977. dz->needpeaks = 1;
  978. dz->outpeaks = (CHPEAK *) malloc(sizeof(CHPEAK) * dz->outchans);
  979. if(dz->outpeaks==NULL)
  980. return MEMORY_ERROR;
  981. dz->outpeakpos = (unsigned int *) malloc(sizeof(unsigned int) * dz->outchans);
  982. if(dz->outpeakpos==NULL)
  983. return MEMORY_ERROR;
  984. for(n=0;n < dz->outchans;n++){
  985. dz->outpeaks[n].value = 0.0f;
  986. dz->outpeaks[n].position = 0;
  987. dz->outpeakpos[n] = 0;
  988. }
  989. strcpy(dz->outfilename,filename);
  990. (*cmdline)++;
  991. (*cmdlinecnt)--;
  992. return(FINISHED);
  993. }
  994. /******************************** USAGE ********************************/
  995. int usage1(void)
  996. {
  997. return usage2("specsphinx");
  998. }
  999. int usage2(char *str)
  1000. {
  1001. if(!strcmp(str,"specsphinx")) {
  1002. fprintf(stdout,
  1003. "USAGE: specsphinx specsphinx 1 analfile1 analfile2 outanalfile\n"
  1004. " [-aampbalance] [-ffrqbalance]\n"
  1005. "\n"
  1006. "IMPOSE CHANNEL AMPLITUDES OF FILE2 ON CHANNEL FRQS OF FILE 1\n"
  1007. "\n"
  1008. "AMPBALANCE Proportion of File1 amps retained (Default 0)\n"
  1009. "FRQBALANCE Proportion of File2 frqs injected into output spectrum\n"
  1010. "(Default 0)\n"
  1011. "\n"
  1012. "USAGE: specsphinx specsphinx 2 analfile1 analfile2 outanalfile\n"
  1013. " [-bbias] [-ggain]\n"
  1014. "\n"
  1015. "MULTIPLY THE SPECTRA\n"
  1016. "\n"
  1017. "BIAS When non-zero, adds proportion of original files to output.\n"
  1018. "\n < 1 : add analfile1 to outanalfile in proportion (-bias)/(1+bias)"
  1019. "\n > 1 : add analfile2 to outanalfile in proportion (bias/(1-bias)"
  1020. "\n"
  1021. "GAIN Overall gain on output.\n"
  1022. "\n");
  1023. } else
  1024. fprintf(stdout,"Unknown option '%s'\n",str);
  1025. return(USAGE_ONLY);
  1026. }
  1027. int usage3(char *str1,char *str2)
  1028. {
  1029. sprintf(errstr,"Insufficient parameters on command line.\n");
  1030. return(USAGE_ONLY);
  1031. }
  1032. /************************* redundant functions: to ensure libs compile OK *******************/
  1033. int assign_process_logic(dataptr dz)
  1034. {
  1035. return(FINISHED);
  1036. }
  1037. void set_legal_infile_structure(dataptr dz)
  1038. {}
  1039. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1040. {
  1041. return(FINISHED);
  1042. }
  1043. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1044. {
  1045. return(FINISHED);
  1046. }
  1047. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1048. {
  1049. return(FINISHED);
  1050. }
  1051. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1052. {
  1053. return(FINISHED);
  1054. }
  1055. int read_special_data(char *str,dataptr dz)
  1056. {
  1057. return(FINISHED);
  1058. }
  1059. int inner_loop
  1060. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1061. {
  1062. return(FINISHED);
  1063. }
  1064. /***************************** SPECSPHINX *****************************/
  1065. int specsphinx(dataptr dz)
  1066. {
  1067. int exit_status;
  1068. int ssamps2read;
  1069. double time = 0.0;
  1070. float *inbuf1 = dz->bigfbuf;
  1071. float *inbuf2 = dz->flbufptr[2];
  1072. float *ampval = NULL, *frqval = NULL;
  1073. int *amploc = NULL, *frqloc = NULL;
  1074. if((sndseekEx(dz->ifd[1],0,0)<0)){
  1075. sprintf(errstr,"sndseek() failed in 2nd file\n");
  1076. return SYSTEM_ERROR;
  1077. }
  1078. if((sndseekEx(dz->ifd[0],0,0)<0)){
  1079. sprintf(errstr,"sndseek() failed in 1st file\n");
  1080. return SYSTEM_ERROR;
  1081. }
  1082. dz->total_samps_read = 0;
  1083. dz->total_windows = 0;
  1084. dz->samps_left = min(dz->insams[0],dz->insams[1]);
  1085. dz->flbufptr[0] = dz->bigfbuf;
  1086. if((dz->brksize[1] > 0.0) || (dz->param[1] > 0)) {
  1087. if((ampval = (float *)malloc(dz->clength * sizeof(float)))==NULL) {
  1088. sprintf(errstr,"INSUFFICIENT MEMORY for ampvals array.\n");
  1089. return(MEMORY_ERROR);
  1090. }
  1091. if((amploc = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  1092. sprintf(errstr,"INSUFFICIENT MEMORY for ampvals array.\n");
  1093. return(MEMORY_ERROR);
  1094. }
  1095. if((frqval = (float *)malloc(dz->clength * sizeof(float)))==NULL) {
  1096. sprintf(errstr,"INSUFFICIENT MEMORY for ampvals array.\n");
  1097. return(MEMORY_ERROR);
  1098. }
  1099. if((frqloc = (int *)malloc(dz->clength * LOCSPREAD * sizeof(int)))==NULL) {
  1100. sprintf(errstr,"INSUFFICIENT MEMORY for ampvals array.\n");
  1101. return(MEMORY_ERROR);
  1102. }
  1103. }
  1104. while(dz->samps_left > 0) {
  1105. if((dz->ssampsread = fgetfbufEx(inbuf1, dz->wanted,dz->ifd[0],0)) < 0) {
  1106. sprintf(errstr,"Can't reread samples to do spectral interpolation.\n");
  1107. return(SYSTEM_ERROR);
  1108. }
  1109. if((ssamps2read = fgetfbufEx(inbuf2, dz->wanted,dz->ifd[1],0)) < 0) {
  1110. sprintf(errstr,"Can't reread samples to do spectral interpolation.\n");
  1111. return(SYSTEM_ERROR);
  1112. }
  1113. dz->ssampsread = min(dz->ssampsread,ssamps2read);
  1114. dz->samps_left -= dz->ssampsread;
  1115. dz->total_samps_read += dz->ssampsread;
  1116. if(dz->brksize[0] || dz->brksize[1]) {
  1117. if(dz->brksize[0]) {
  1118. if((exit_status = read_value_from_brktable(time,0,dz))<0)
  1119. return(exit_status);
  1120. }
  1121. if(dz->brksize[1]) {
  1122. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  1123. return(exit_status);
  1124. }
  1125. time += dz->frametime;
  1126. }
  1127. if(dz->total_windows > 0) { /* Skip Window 0, no significant data */
  1128. spec_sphinx(inbuf1,inbuf2,ampval,amploc,frqval,frqloc,dz);
  1129. }
  1130. if((exit_status = write_samps(dz->flbufptr[0],dz->wanted,dz))<0)
  1131. return(exit_status);
  1132. dz->total_windows++;
  1133. }
  1134. return(FINISHED);
  1135. }
  1136. /***************************** SPECVOLVE *****************************/
  1137. int specvolve(dataptr dz)
  1138. {
  1139. int exit_status, cc, vc;
  1140. int ssamps2read;
  1141. double time = 0.0, val;
  1142. float *inbuf1 = dz->bigfbuf;
  1143. float *inbuf2 = dz->flbufptr[2];
  1144. if((sndseekEx(dz->ifd[1],0,0)<0)){
  1145. sprintf(errstr,"sndseek() failed in 2nd file\n");
  1146. return SYSTEM_ERROR;
  1147. }
  1148. if((sndseekEx(dz->ifd[0],0,0)<0)){
  1149. sprintf(errstr,"sndseek() failed in 1st file\n");
  1150. return SYSTEM_ERROR;
  1151. }
  1152. dz->flbufptr[0] = dz->bigfbuf;
  1153. dz->total_samps_read = 0;
  1154. dz->total_windows = 0;
  1155. dz->samps_left = min(dz->insams[0],dz->insams[1]);
  1156. while(dz->samps_left > 0) {
  1157. if((dz->ssampsread = fgetfbufEx(inbuf1, dz->wanted,dz->ifd[0],0)) < 0) {
  1158. sprintf(errstr,"Can't reread samples to do spectral interpolation.\n");
  1159. return(SYSTEM_ERROR);
  1160. }
  1161. if((ssamps2read = fgetfbufEx(inbuf2, dz->wanted,dz->ifd[1],0)) < 0) {
  1162. sprintf(errstr,"Can't reread samples to do spectral interpolation.\n");
  1163. return(SYSTEM_ERROR);
  1164. }
  1165. dz->ssampsread = min(dz->ssampsread,ssamps2read);
  1166. dz->samps_left -= dz->ssampsread;
  1167. dz->total_samps_read += dz->ssampsread;
  1168. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1169. return(exit_status);
  1170. // bias = dz->param[0]
  1171. // gain = dz->param[1]
  1172. if(dz->param[0] > 0.0) {
  1173. for(cc = 0, vc = 0; cc < dz->clength; cc++, vc += 2) {
  1174. val = inbuf1[AMPP] * inbuf2[AMPP] * dz->param[1];
  1175. val *= 1.0 - dz->param[0];
  1176. val += inbuf2[AMPP] * dz->param[0];
  1177. inbuf1[AMPP] = (float)val;
  1178. }
  1179. } else if(dz->param[0] < 0.0) {
  1180. for(cc = 0, vc = 0; cc < dz->clength; cc++, vc += 2) {
  1181. val = inbuf1[AMPP] * inbuf2[AMPP] * dz->param[1];
  1182. val *= 1.0 + dz->param[0];
  1183. val += inbuf1[AMPP] * (-dz->param[0]);
  1184. inbuf1[AMPP] = (float)val;
  1185. }
  1186. } else {
  1187. for(cc = 0, vc = 0; cc < dz->clength; cc++, vc += 2)
  1188. inbuf1[AMPP] = (float)(inbuf1[AMPP] * inbuf2[AMPP] * dz->param[1]);
  1189. }
  1190. if((exit_status = write_samps(dz->flbufptr[0],dz->wanted,dz))<0)
  1191. return(exit_status);
  1192. time += dz->frametime;
  1193. dz->total_windows++;
  1194. }
  1195. return(FINISHED);
  1196. }
  1197. /***************************** SPEC_SPHINX *****************************/
  1198. int spec_sphinx(float* inbuf1,float* inbuf2,float* ampval,int* amploc,float* frqval,int* frqloc,dataptr dz)
  1199. {
  1200. int cc, vc, kk, ii, jj, frqstorecnt, initvc, OK, srchcnt, frqsetcnt = 0;
  1201. float amp, frq, frqhere;
  1202. double frqratio;
  1203. if(dz->param[1] > 0) {
  1204. for(cc = 0, vc = 0; cc < dz->clength; cc++, vc += 2) {
  1205. ampval[cc] = inbuf2[AMPP]; // Store input buffer 2's data and location
  1206. frqval[cc] = inbuf2[FREQ];
  1207. amploc[cc] = AMPP;
  1208. }
  1209. memset((char *)ampval,0,dz->clength * sizeof(float));
  1210. memset((char *)frqval,0,dz->clength * sizeof(float));
  1211. memset((char *)amploc,0,dz->clength * sizeof(int));
  1212. memset((char *)frqloc,0,dz->clength * sizeof(int));
  1213. for(cc = 0; cc < dz->clength-1; cc++) {
  1214. for(ii = cc; ii < dz->clength; ii++) {
  1215. if(ampval[ii] > ampval[cc]) { // Sort data into decreasing amplitude order
  1216. amp = ampval[cc];
  1217. ampval[cc] = ampval[ii];
  1218. ampval[ii] = amp;
  1219. frq = frqval[cc];
  1220. frqval[cc] = frqval[ii];
  1221. frqval[ii] = frq;
  1222. kk = amploc[cc];
  1223. amploc[cc] = amploc[ii];
  1224. amploc[ii] = kk;
  1225. kk = amploc[cc];
  1226. }
  1227. }
  1228. }
  1229. frqstorecnt = 0;
  1230. for(kk = 0; kk < dz->clength; kk++) { // In descending loudness order ...
  1231. frqhere = frqval[kk]; // Frq at the (next) loudest channel
  1232. initvc = amploc[kk] + 1; // Location of that frq in input buffer
  1233. OK = 1;
  1234. for(jj = 0; jj < frqstorecnt;jj++) {
  1235. if(initvc == frqloc[jj]) { // If we've already stored this frq-location, skip and go to next
  1236. OK = 0;
  1237. break;
  1238. }
  1239. }
  1240. if(!OK)
  1241. continue;
  1242. frqstorecnt = kk * LOCSPREAD; // Start position in frq-locations store
  1243. frqloc[frqstorecnt++] = initvc; // Store this frq in the loudest-frq-locations store
  1244. srchcnt = 1; // We now have 1 value for the location of this frq in the input buffer
  1245. vc = initvc - 2; // Location of frq below it in input buffer
  1246. while(vc > 0) { // Don't search beyond bottom of buffer
  1247. if(inbuf2[vc] > frqhere) // Compare previous inbuf frq with initial inbuf frq
  1248. frqratio = inbuf2[vc]/frqhere;
  1249. else
  1250. frqratio = frqhere/inbuf2[vc];
  1251. if(frqratio < EIGHT_OVER_SEVEN) { // if "same" frq
  1252. frqloc[frqstorecnt++] = vc; // Store this is an equivalent location of start frq
  1253. srchcnt++; // increment count of stored locations
  1254. } else // But if NOT same frq, assume ensuing frqs
  1255. break; // will be even further out of tune, and break.
  1256. if(srchcnt >= LOCSPREAD) // Once we've filled all 4 other slots in the frq-locations store,
  1257. break; // quit search
  1258. vc -= 2; // descend to next frq in input buffer
  1259. }
  1260. if(srchcnt < LOCSPREAD) { // If not already filled all storage frq-locations array spaces...
  1261. vc = initvc + 2; // Location of frq above initial frq in input buffer
  1262. while(vc < dz->wanted) { // Don't search beyond end of buffer
  1263. if(inbuf2[vc] > frqhere) // Compare next inbuf frq with initial inbuf frq
  1264. frqratio = inbuf2[vc]/frqhere;
  1265. else
  1266. frqratio = frqhere/inbuf2[vc];
  1267. if(frqratio < EIGHT_OVER_SEVEN) { // if "same" frq
  1268. frqloc[frqstorecnt++] = vc; // Store this as an equivalent location of start frq
  1269. srchcnt++;
  1270. } else // if it's not "same" frq, assume we're out of similar frqs
  1271. break; // quit search
  1272. if(srchcnt >= LOCSPREAD) // Once we've filled all 4 similar-frq locations, quit search
  1273. break;
  1274. vc += 2; // ascend to next frq in input buffer
  1275. }
  1276. while(srchcnt < LOCSPREAD) { // If still not filled all available array spaces..
  1277. frqloc[frqstorecnt++] = -1; // fill rest of similar-frq locations with -1 flags
  1278. srchcnt++;
  1279. }
  1280. }
  1281. }
  1282. frqsetcnt = frqstorecnt/LOCSPREAD; // No of sets of prominent frq-locations
  1283. }
  1284. for(cc = 0, vc = 0; cc < dz->clength; cc++, vc += 2) {
  1285. frq = inbuf1[FREQ];
  1286. amp = (float)(((1.0 - dz->param[0]) * inbuf2[AMPP]) + (dz->param[0] * inbuf1[AMPP]));
  1287. inbuf1[AMPP] = amp;
  1288. inbuf1[FREQ] = frq;
  1289. }
  1290. if(dz->param[1] > 0) { // Proportion of 2nd spectrum's most prominent frqs to impose on output
  1291. dz->iparam[1] = (int)round(dz->param[1] * frqsetcnt); // Number of 2nd spectrum's most prominent frqs to impose on output
  1292. for(kk = 0; kk < dz->iparam[1]; kk++) { // For each 2ndspectrum's most prominent frq
  1293. frqstorecnt = kk * LOCSPREAD;
  1294. for(jj = 0; jj < LOCSPREAD; jj++) { // Find their locations in the 2nd spectrums input data
  1295. vc = frqloc[frqstorecnt++];
  1296. if(vc > 0) // (and if data location is not flagged as empty)
  1297. inbuf1[vc] = inbuf2[vc]; // Impose that 2nd-sectra frq on to output spectrum
  1298. else
  1299. break;
  1300. }
  1301. }
  1302. }
  1303. return FINISHED;
  1304. }
  1305. /****************************** GET_MODE *********************************/
  1306. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1307. {
  1308. char temp[200], *p;
  1309. if(sscanf(str,"%s",temp)!=1) {
  1310. sprintf(errstr,"Cannot read mode of program.\n");
  1311. return(USAGE_ONLY);
  1312. }
  1313. p = temp + strlen(temp) - 1;
  1314. while(p >= temp) {
  1315. if(!isdigit(*p)) {
  1316. fprintf(stderr,"Invalid mode of program entered.\n");
  1317. return(USAGE_ONLY);
  1318. }
  1319. p--;
  1320. }
  1321. if(sscanf(str,"%d",&dz->mode)!=1) {
  1322. fprintf(stderr,"Cannot read mode of program.\n");
  1323. return(USAGE_ONLY);
  1324. }
  1325. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1326. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1327. return(USAGE_ONLY);
  1328. }
  1329. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1330. return(FINISHED);
  1331. }