selfsim.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 <speccon.h>
  37. #include <ctype.h>
  38. #include <sfsys.h>
  39. #include <string.h>
  40. #include <srates.h>
  41. #if defined unix || defined __GNUC__
  42. #define round(x) lround((x))
  43. #endif
  44. #ifndef HUGE
  45. #define HUGE 3.40282347e+38F
  46. #endif
  47. char errstr[2400];
  48. int anal_infiles = 1;
  49. int sloom = 0;
  50. int sloombatch = 0;
  51. const char* cdp_version = "7.1.0";
  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. /* CDP LIB FUNCTION MODIFIED TO AVOID CALLING setup_particular_application() */
  67. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  68. /* SIMPLIFICATION OF LIB FUNC TO APPLY TO JUST THIS FUNCTION */
  69. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  70. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz);
  71. static int setup_the_application(dataptr dz);
  72. static int setup_the_param_ranges_and_defaults(dataptr dz);
  73. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  74. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  75. /* BYPASS LIBRARY GLOBAL FUNCTION TO GO DIRECTLY TO SPECIFIC APPLIC FUNCTIONS */
  76. static int allocate_selfsim_buffer(dataptr dz);
  77. static int do_selfsim(dataptr dz);
  78. static int create_order_and_scaling_arrays(dataptr dz);
  79. static int create_dissimilarity_array(dataptr dz);
  80. /**************************************** MAIN *********************************************/
  81. int main(int argc,char *argv[])
  82. {
  83. int exit_status;
  84. dataptr dz = NULL;
  85. char **cmdline;
  86. int cmdlinecnt;
  87. //aplptr ap;
  88. int is_launched = FALSE;
  89. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  90. fprintf(stdout,"%s\n",cdp_version);
  91. fflush(stdout);
  92. return 0;
  93. }
  94. /* CHECK FOR SOUNDLOOM */
  95. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  96. sloom = 0;
  97. sloombatch = 1;
  98. }
  99. if(sflinit("cdp")){
  100. sfperror("cdp: initialisation\n");
  101. return(FAILED);
  102. }
  103. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  104. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  105. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  106. return(FAILED);
  107. }
  108. if(!sloom) {
  109. if(argc == 1) {
  110. usage1();
  111. return(FAILED);
  112. } else if(argc == 2) {
  113. usage2(argv[1]);
  114. return(FAILED);
  115. }
  116. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  117. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  118. return(FAILED);
  119. }
  120. cmdline = argv;
  121. cmdlinecnt = argc;
  122. if((get_the_process_no(argv[0],dz))<0)
  123. return(FAILED);
  124. cmdline++;
  125. cmdlinecnt--;
  126. dz->maxmode = 0;
  127. if((exit_status = setup_the_application(dz))<0) {
  128. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  129. return(FAILED);
  130. }
  131. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  132. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  133. return(FAILED);
  134. }
  135. } else {
  136. //parse_TK_data() =
  137. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  138. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  139. return(exit_status);
  140. }
  141. }
  142. //ap = dz->application;
  143. // parse_infile_and_hone_type() =
  144. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  145. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  146. return(FAILED);
  147. }
  148. // setup_param_ranges_and_defaults() =
  149. if((exit_status = setup_the_param_ranges_and_defaults(dz))<0) {
  150. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(FAILED);
  152. }
  153. // open_first_infile CDP LIB
  154. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  155. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  156. return(FAILED);
  157. }
  158. cmdlinecnt--;
  159. cmdline++;
  160. // handle_outfile() =
  161. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) {
  162. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  163. return(FAILED);
  164. }
  165. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  166. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  167. return(FAILED);
  168. }
  169. //check_param_validity_and_consistency .....
  170. is_launched = TRUE;
  171. dz->extra_bufcnt = 0;
  172. dz->bptrcnt = 4;
  173. if((exit_status = establish_spec_bufptrs_and_extra_buffers(dz))<0) {
  174. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  175. return(FAILED);
  176. }
  177. if((exit_status = allocate_selfsim_buffer(dz)) < 0) {
  178. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  179. return(FAILED);
  180. }
  181. if((exit_status = create_order_and_scaling_arrays(dz)) < 0) {
  182. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  183. return(FAILED);
  184. }
  185. if((exit_status = create_dissimilarity_array(dz)) < 0) {
  186. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  187. return(FAILED);
  188. }
  189. if((exit_status = do_selfsim(dz)) < 0) {
  190. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  191. return(FAILED);
  192. }
  193. if((exit_status = complete_output(dz))<0) { // CDP LIB
  194. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  195. return(FAILED);
  196. }
  197. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  198. free(dz);
  199. return(SUCCEEDED);
  200. }
  201. /**********************************************
  202. REPLACED CDP LIB FUNCTIONS
  203. **********************************************/
  204. /****************************** SET_PARAM_DATA *********************************/
  205. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  206. {
  207. ap->special_data = (char)special_data;
  208. ap->param_cnt = (char)paramcnt;
  209. ap->max_param_cnt = (char)maxparamcnt;
  210. if(ap->max_param_cnt>0) {
  211. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  212. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  213. return(MEMORY_ERROR);
  214. }
  215. strcpy(ap->param_list,paramlist);
  216. }
  217. return(FINISHED);
  218. }
  219. /****************************** SET_VFLGS *********************************/
  220. int set_vflgs
  221. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  222. {
  223. ap->option_cnt = (char) optcnt; /*RWD added cast */
  224. if(optcnt) {
  225. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  226. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  227. return(MEMORY_ERROR);
  228. }
  229. strcpy(ap->option_list,optlist);
  230. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  231. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  232. return(MEMORY_ERROR);
  233. }
  234. strcpy(ap->option_flags,optflags);
  235. }
  236. ap->vflag_cnt = (char) vflagcnt;
  237. ap->variant_param_cnt = (char) vparamcnt;
  238. if(vflagcnt) {
  239. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  240. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  241. return(MEMORY_ERROR);
  242. }
  243. strcpy(ap->variant_list,varlist);
  244. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  245. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  246. return(MEMORY_ERROR);
  247. }
  248. strcpy(ap->variant_flags,varflags);
  249. }
  250. return(FINISHED);
  251. }
  252. /***************************** APPLICATION_INIT **************************/
  253. int application_init(dataptr dz)
  254. {
  255. int exit_status;
  256. int storage_cnt;
  257. int tipc, brkcnt;
  258. aplptr ap = dz->application;
  259. if(ap->vflag_cnt>0)
  260. initialise_vflags(dz);
  261. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  262. ap->total_input_param_cnt = (char)tipc;
  263. if(tipc>0) {
  264. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  265. return(exit_status);
  266. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  267. return(exit_status);
  268. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  269. return(exit_status);
  270. }
  271. brkcnt = tipc;
  272. if(brkcnt>0) {
  273. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  274. return(exit_status);
  275. }
  276. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  277. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  278. return(exit_status);
  279. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  280. return(exit_status);
  281. }
  282. if((exit_status = mark_parameter_types(dz,ap))<0)
  283. return(exit_status);
  284. // establish_infile_constants() replaced by
  285. dz->infilecnt = ONE_NONSND_FILE;
  286. //establish_bufptrs_and_extra_buffers():
  287. dz->array_cnt=3;
  288. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  289. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  290. return(MEMORY_ERROR);
  291. }
  292. dz->parray[0] = NULL;
  293. dz->parray[1] = NULL;
  294. dz->parray[2] = NULL;
  295. dz->larray_cnt=3;
  296. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  297. sprintf(errstr,"INSUFFICIENT MEMORY for internal long arrays.\n");
  298. return(MEMORY_ERROR);
  299. }
  300. dz->lparray[0] = NULL;
  301. dz->lparray[1] = NULL;
  302. dz->lparray[2] = NULL;
  303. return(FINISHED);
  304. }
  305. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  306. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  307. {
  308. int n;
  309. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  310. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  311. return(MEMORY_ERROR);
  312. }
  313. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  314. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  315. return(MEMORY_ERROR);
  316. }
  317. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  318. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  319. return(MEMORY_ERROR);
  320. }
  321. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  322. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  323. return(MEMORY_ERROR);
  324. }
  325. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  326. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  327. return(MEMORY_ERROR);
  328. }
  329. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  330. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  331. return(MEMORY_ERROR);
  332. }
  333. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  334. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  335. return(MEMORY_ERROR);
  336. }
  337. for(n=0;n<brkcnt;n++) {
  338. dz->brk[n] = NULL;
  339. dz->brkptr[n] = NULL;
  340. dz->brkinit[n] = 0;
  341. dz->brksize[n] = 0;
  342. }
  343. return(FINISHED);
  344. }
  345. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  346. /* RWD mallo changed to calloc; helps debug verison run as release! */
  347. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  348. {
  349. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  350. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  351. return(MEMORY_ERROR);
  352. }
  353. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  354. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  355. return(MEMORY_ERROR);
  356. }
  357. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  358. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  359. return(MEMORY_ERROR);
  360. }
  361. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  362. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  363. return(MEMORY_ERROR);
  364. }
  365. return(FINISHED);
  366. }
  367. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  368. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  369. {
  370. int n;
  371. for(n=0;n<storage_cnt;n++) {
  372. dz->is_int[n] = (char)0;
  373. dz->no_brk[n] = (char)0;
  374. }
  375. return(FINISHED);
  376. }
  377. /***************************** MARK_PARAMETER_TYPES **************************/
  378. int mark_parameter_types(dataptr dz,aplptr ap)
  379. {
  380. int n, m; /* PARAMS */
  381. for(n=0;n<ap->max_param_cnt;n++) {
  382. switch(ap->param_list[n]) {
  383. case('0'): break; /* dz->is_active[n] = 0 is default */
  384. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  385. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  386. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  387. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  388. default:
  389. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  390. return(PROGRAM_ERROR);
  391. }
  392. } /* OPTIONS */
  393. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  394. switch(ap->option_list[n]) {
  395. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  396. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  397. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  398. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  399. default:
  400. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  401. return(PROGRAM_ERROR);
  402. }
  403. } /* VARIANTS */
  404. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  405. switch(ap->variant_list[n]) {
  406. case('0'): break;
  407. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  408. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  409. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  410. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  411. default:
  412. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  413. return(PROGRAM_ERROR);
  414. }
  415. } /* INTERNAL */
  416. for(n=0,
  417. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  418. switch(ap->internal_param_list[n]) {
  419. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  420. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  421. case('d'): dz->no_brk[m] = (char)1; break;
  422. default:
  423. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  424. return(PROGRAM_ERROR);
  425. }
  426. }
  427. return(FINISHED);
  428. }
  429. /***************************** HANDLE_THE_OUTFILE **************************/
  430. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz)
  431. {
  432. int exit_status;
  433. char *filename = NULL;
  434. filename = (*cmdline)[0];
  435. strcpy(dz->outfilename,filename);
  436. if((exit_status = create_sized_outfile(filename,dz))<0)
  437. return(exit_status);
  438. (*cmdline)++;
  439. (*cmdlinecnt)--;
  440. return(FINISHED);
  441. }
  442. /***************************** ESTABLISH_APPLICATION **************************/
  443. int establish_application(dataptr dz)
  444. {
  445. aplptr ap;
  446. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  447. sprintf(errstr,"establish_application()\n");
  448. return(MEMORY_ERROR);
  449. }
  450. ap = dz->application;
  451. memset((char *)ap,0,sizeof(struct applic));
  452. return(FINISHED);
  453. }
  454. /************************* INITIALISE_VFLAGS *************************/
  455. int initialise_vflags(dataptr dz)
  456. {
  457. int n;
  458. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  459. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  460. return(MEMORY_ERROR);
  461. }
  462. for(n=0;n<dz->application->vflag_cnt;n++)
  463. dz->vflag[n] = FALSE;
  464. return FINISHED;
  465. }
  466. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  467. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  468. {
  469. int n;
  470. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  471. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  472. return(MEMORY_ERROR);
  473. }
  474. for(n=0;n<tipc;n++)
  475. ap->default_val[n] = 0.0;
  476. return(FINISHED);
  477. }
  478. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  479. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  480. {
  481. int n;
  482. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  483. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  484. return(MEMORY_ERROR);
  485. }
  486. for(n=0;n<tipc;n++)
  487. dz->is_active[n] = (char)0;
  488. return(FINISHED);
  489. }
  490. /************************* SETUP_THE_APPLICATION *******************/
  491. int setup_the_application(dataptr dz)
  492. {
  493. int exit_status;
  494. aplptr ap;
  495. if((exit_status = establish_application(dz))<0) // GLOBAL
  496. return(FAILED);
  497. ap = dz->application;
  498. // SEE parstruct FOR EXPLANATION of next 2 functions
  499. if((exit_status = set_param_data(ap,0 ,1,1,"i"))<0)
  500. return(FAILED);
  501. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  502. return(FAILED);
  503. dz->has_otherfile = FALSE;
  504. dz->input_data_type = ANALFILE_ONLY;
  505. dz->process_type = EQUAL_ANALFILE;
  506. dz->outfiletype = ANALFILE_OUT;
  507. return application_init(dz); //GLOBAL
  508. }
  509. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  510. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  511. {
  512. int exit_status;
  513. infileptr infile_info;
  514. if(!sloom) {
  515. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  516. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  517. return(MEMORY_ERROR);
  518. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  519. sprintf(errstr,"Failed tp parse input file %s\n",cmdline[0]);
  520. return(PROGRAM_ERROR);
  521. } else if(infile_info->filetype != ANALFILE) {
  522. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  523. return(DATA_ERROR);
  524. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  525. sprintf(errstr,"Failed to copy file parsing information\n");
  526. return(PROGRAM_ERROR);
  527. }
  528. free(infile_info);
  529. }
  530. dz->clength = dz->wanted / 2;
  531. dz->chwidth = dz->nyquist/(double)(dz->clength-1);
  532. dz->halfchwidth = dz->chwidth/2.0;
  533. dz->wlength = dz->insams[0]/dz->wanted;
  534. return(FINISHED);
  535. }
  536. /************************* SETUP_THE_PARAM_RANGES_AND_DEFAULTS *******************/
  537. int setup_the_param_ranges_and_defaults(dataptr dz)
  538. {
  539. int exit_status;
  540. aplptr ap = dz->application;
  541. // set_param_ranges()
  542. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  543. // NB total_input_param_cnt is > 0 !!!s
  544. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  545. return(FAILED);
  546. // get_param_ranges()
  547. ap->lo[0] = 1;
  548. ap->hi[0] = dz->wlength - 1;
  549. ap->default_val[0] = 1;
  550. dz->maxmode = 0;
  551. if(!sloom)
  552. put_default_vals_in_all_params(dz);
  553. return(FINISHED);
  554. }
  555. /********************************* PARSE_SLOOM_DATA *********************************/
  556. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  557. {
  558. int exit_status;
  559. int cnt = 1, infilecnt;
  560. int filesize, insams, inbrksize;
  561. double dummy;
  562. int true_cnt = 0;
  563. //aplptr ap;
  564. while(cnt<=PRE_CMDLINE_DATACNT) {
  565. if(cnt > argc) {
  566. sprintf(errstr,"Insufficient data sent from TK\n");
  567. return(DATA_ERROR);
  568. }
  569. switch(cnt) {
  570. case(1):
  571. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  572. sprintf(errstr,"Cannot read process no. sent from TK\n");
  573. return(DATA_ERROR);
  574. }
  575. break;
  576. case(2):
  577. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  578. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  579. return(DATA_ERROR);
  580. }
  581. if(dz->mode > 0)
  582. dz->mode--;
  583. //setup_particular_application() =
  584. if((exit_status = setup_the_application(dz))<0)
  585. return(exit_status);
  586. //ap = dz->application;
  587. break;
  588. case(3):
  589. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  590. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  591. return(DATA_ERROR);
  592. }
  593. if(infilecnt < 1) {
  594. true_cnt = cnt + 1;
  595. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  596. }
  597. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  598. return(exit_status);
  599. break;
  600. case(INPUT_FILETYPE+4):
  601. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  602. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  603. return(DATA_ERROR);
  604. }
  605. break;
  606. case(INPUT_FILESIZE+4):
  607. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  608. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  609. return(DATA_ERROR);
  610. }
  611. dz->insams[0] = filesize;
  612. break;
  613. case(INPUT_INSAMS+4):
  614. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  615. sprintf(errstr,"Cannot read insams sent from TK\n");
  616. return(DATA_ERROR);
  617. }
  618. dz->insams[0] = insams;
  619. break;
  620. case(INPUT_SRATE+4):
  621. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  622. sprintf(errstr,"Cannot read srate sent from TK\n");
  623. return(DATA_ERROR);
  624. }
  625. break;
  626. case(INPUT_CHANNELS+4):
  627. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  628. sprintf(errstr,"Cannot read channels sent from TK\n");
  629. return(DATA_ERROR);
  630. }
  631. break;
  632. case(INPUT_STYPE+4):
  633. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  634. sprintf(errstr,"Cannot read stype sent from TK\n");
  635. return(DATA_ERROR);
  636. }
  637. break;
  638. case(INPUT_ORIGSTYPE+4):
  639. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  640. sprintf(errstr,"Cannot read origstype sent from TK\n");
  641. return(DATA_ERROR);
  642. }
  643. break;
  644. case(INPUT_ORIGRATE+4):
  645. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  646. sprintf(errstr,"Cannot read origrate sent from TK\n");
  647. return(DATA_ERROR);
  648. }
  649. break;
  650. case(INPUT_MLEN+4):
  651. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  652. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  653. return(DATA_ERROR);
  654. }
  655. break;
  656. case(INPUT_DFAC+4):
  657. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  658. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  659. return(DATA_ERROR);
  660. }
  661. break;
  662. case(INPUT_ORIGCHANS+4):
  663. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  664. sprintf(errstr,"Cannot read origchans sent from TK\n");
  665. return(DATA_ERROR);
  666. }
  667. break;
  668. case(INPUT_SPECENVCNT+4):
  669. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  670. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  671. return(DATA_ERROR);
  672. }
  673. dz->specenvcnt = dz->infile->specenvcnt;
  674. break;
  675. case(INPUT_WANTED+4):
  676. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  677. sprintf(errstr,"Cannot read wanted sent from TK\n");
  678. return(DATA_ERROR);
  679. }
  680. break;
  681. case(INPUT_WLENGTH+4):
  682. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  683. sprintf(errstr,"Cannot read wlength sent from TK\n");
  684. return(DATA_ERROR);
  685. }
  686. break;
  687. case(INPUT_OUT_CHANS+4):
  688. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  689. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  690. return(DATA_ERROR);
  691. }
  692. break;
  693. /* RWD these chanegs to samps - tk will have to deal with that! */
  694. case(INPUT_DESCRIPTOR_BYTES+4):
  695. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  696. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  697. return(DATA_ERROR);
  698. }
  699. break;
  700. case(INPUT_IS_TRANSPOS+4):
  701. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  702. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  703. return(DATA_ERROR);
  704. }
  705. break;
  706. case(INPUT_COULD_BE_TRANSPOS+4):
  707. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  708. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  709. return(DATA_ERROR);
  710. }
  711. break;
  712. case(INPUT_COULD_BE_PITCH+4):
  713. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  714. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  715. return(DATA_ERROR);
  716. }
  717. break;
  718. case(INPUT_DIFFERENT_SRATES+4):
  719. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  720. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  721. return(DATA_ERROR);
  722. }
  723. break;
  724. case(INPUT_DUPLICATE_SNDS+4):
  725. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  726. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  727. return(DATA_ERROR);
  728. }
  729. break;
  730. case(INPUT_BRKSIZE+4):
  731. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  732. sprintf(errstr,"Cannot read brksize sent from TK\n");
  733. return(DATA_ERROR);
  734. }
  735. if(inbrksize > 0) {
  736. switch(dz->input_data_type) {
  737. case(WORDLIST_ONLY):
  738. break;
  739. case(PITCH_AND_PITCH):
  740. case(PITCH_AND_TRANSPOS):
  741. case(TRANSPOS_AND_TRANSPOS):
  742. dz->tempsize = inbrksize;
  743. break;
  744. case(BRKFILES_ONLY):
  745. case(UNRANGED_BRKFILE_ONLY):
  746. case(DB_BRKFILES_ONLY):
  747. case(ALL_FILES):
  748. case(ANY_NUMBER_OF_ANY_FILES):
  749. if(dz->extrabrkno < 0) {
  750. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  751. return(DATA_ERROR);
  752. }
  753. if(dz->brksize == NULL) {
  754. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  755. return(PROGRAM_ERROR);
  756. }
  757. dz->brksize[dz->extrabrkno] = inbrksize;
  758. break;
  759. default:
  760. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  761. dz->input_data_type);
  762. return(PROGRAM_ERROR);
  763. }
  764. break;
  765. }
  766. break;
  767. case(INPUT_NUMSIZE+4):
  768. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  769. sprintf(errstr,"Cannot read numsize sent from TK\n");
  770. return(DATA_ERROR);
  771. }
  772. break;
  773. case(INPUT_LINECNT+4):
  774. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  775. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  776. return(DATA_ERROR);
  777. }
  778. break;
  779. case(INPUT_ALL_WORDS+4):
  780. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  781. sprintf(errstr,"Cannot read all_words sent from TK\n");
  782. return(DATA_ERROR);
  783. }
  784. break;
  785. case(INPUT_ARATE+4):
  786. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  787. sprintf(errstr,"Cannot read arate sent from TK\n");
  788. return(DATA_ERROR);
  789. }
  790. break;
  791. case(INPUT_FRAMETIME+4):
  792. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  793. sprintf(errstr,"Cannot read frametime sent from TK\n");
  794. return(DATA_ERROR);
  795. }
  796. dz->frametime = (float)dummy;
  797. break;
  798. case(INPUT_WINDOW_SIZE+4):
  799. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  800. sprintf(errstr,"Cannot read window_size sent from TK\n");
  801. return(DATA_ERROR);
  802. }
  803. break;
  804. case(INPUT_NYQUIST+4):
  805. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  806. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  807. return(DATA_ERROR);
  808. }
  809. break;
  810. case(INPUT_DURATION+4):
  811. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  812. sprintf(errstr,"Cannot read duration sent from TK\n");
  813. return(DATA_ERROR);
  814. }
  815. break;
  816. case(INPUT_MINBRK+4):
  817. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  818. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  819. return(DATA_ERROR);
  820. }
  821. break;
  822. case(INPUT_MAXBRK+4):
  823. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  824. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  825. return(DATA_ERROR);
  826. }
  827. break;
  828. case(INPUT_MINNUM+4):
  829. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  830. sprintf(errstr,"Cannot read minnum sent from TK\n");
  831. return(DATA_ERROR);
  832. }
  833. break;
  834. case(INPUT_MAXNUM+4):
  835. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  836. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  837. return(DATA_ERROR);
  838. }
  839. break;
  840. default:
  841. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  842. return(PROGRAM_ERROR);
  843. }
  844. cnt++;
  845. }
  846. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  847. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  848. return(DATA_ERROR);
  849. }
  850. if(true_cnt)
  851. cnt = true_cnt;
  852. *cmdlinecnt = 0;
  853. while(cnt < argc) {
  854. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  855. return(exit_status);
  856. cnt++;
  857. }
  858. return(FINISHED);
  859. }
  860. /********************************* GET_TK_CMDLINE_WORD *********************************/
  861. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  862. {
  863. if(*cmdlinecnt==0) {
  864. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  865. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  866. return(MEMORY_ERROR);
  867. }
  868. } else {
  869. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  870. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  871. return(MEMORY_ERROR);
  872. }
  873. }
  874. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  875. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  876. return(MEMORY_ERROR);
  877. }
  878. strcpy((*cmdline)[*cmdlinecnt],q);
  879. (*cmdlinecnt)++;
  880. return(FINISHED);
  881. }
  882. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  883. int assign_file_data_storage(int infilecnt,dataptr dz)
  884. {
  885. int exit_status;
  886. int no_sndfile_system_files = FALSE;
  887. dz->infilecnt = infilecnt;
  888. if((exit_status = allocate_filespace(dz))<0)
  889. return(exit_status);
  890. if(no_sndfile_system_files)
  891. dz->infilecnt = 0;
  892. return(FINISHED);
  893. }
  894. /************************* redundant functions: to ensure libs compile OK *******************/
  895. int assign_process_logic(dataptr dz)
  896. {
  897. return(FINISHED);
  898. }
  899. void set_legal_infile_structure(dataptr dz)
  900. {}
  901. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  902. {
  903. return(FINISHED);
  904. }
  905. int setup_internal_arrays_and_array_pointers(dataptr dz)
  906. {
  907. return(FINISHED);
  908. }
  909. int establish_bufptrs_and_extra_buffers(dataptr dz)
  910. {
  911. return(FINISHED);
  912. }
  913. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  914. {
  915. return(FINISHED);
  916. }
  917. int inner_loop
  918. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  919. {
  920. return(FINISHED);
  921. }
  922. int read_special_data(char *str,dataptr dz)
  923. {
  924. return(FINISHED);
  925. }
  926. /********************************************************************************************/
  927. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  928. {
  929. if (!strcmp(prog_identifier_from_cmdline,"selfsim")) dz->process = SELFSIM;
  930. else {
  931. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  932. return(USAGE_ONLY);
  933. }
  934. return(FINISHED);
  935. }
  936. /**************************** ALLOCATE_SPECLEAN_BUFFER ******************************/
  937. int allocate_selfsim_buffer(dataptr dz)
  938. {
  939. unsigned int buffersize;
  940. buffersize = dz->wanted * 2;
  941. dz->buflen = dz->wanted;
  942. if((dz->bigfbuf = (float*) malloc(buffersize * sizeof(float)))==NULL) {
  943. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  944. return(MEMORY_ERROR);
  945. }
  946. dz->big_fsize = dz->buflen;
  947. dz->flbufptr[0] = dz->bigfbuf;
  948. dz->flbufptr[1] = dz->bigfbuf + dz->big_fsize;
  949. dz->flbufptr[2] = dz->flbufptr[1] + dz->big_fsize;
  950. return(FINISHED);
  951. }
  952. /******************************** USAGE1 ********************************/
  953. int usage1(void)
  954. {
  955. fprintf(stderr,
  956. "\nINCREASE SPECTRAL SELF-SIMILARITY\n\n"
  957. "USAGE: selfsim selfsim inanalfile outanalfile param\n"
  958. "\n"
  959. "Type 'selfsim selfsim' for more info.\n");
  960. return(USAGE_ONLY);
  961. }
  962. /******************************** USAGE2 ********************************/
  963. int usage2(char *str)
  964. {
  965. if(!strcmp(str,"selfsim")) {
  966. fprintf(stdout,
  967. "USAGE:\n"
  968. "selfsim selfsim inanalfile outanalfile self-similarity-index\n"
  969. "\n"
  970. "Replaces spectral windows with most similar, louder window(s).\n"
  971. "\n"
  972. "SELF-SIMILARITY-INDEX Number of similar windows to replace.\n"
  973. " Value 1 uses loudest window to replace the most similar window.\n"
  974. " then the next loudest window to replace window most similar to it\n"
  975. " and so on, with appropriate overall-loudness scaling.\n"
  976. " With value 2, loudest windows replaces the TWO most simil windows.\n"
  977. " and so on.\n"
  978. " If window A replaces B, and C is most simil to B,\n"
  979. " then A also replaces C, etc\n"
  980. "\n");
  981. } else
  982. fprintf(stdout,"Unknown option '%s'\n",str);
  983. return(USAGE_ONLY);
  984. }
  985. int usage3(char *str1,char *str2)
  986. {
  987. fprintf(stderr,"Insufficient parameters on command line.\n");
  988. return(USAGE_ONLY);
  989. }
  990. /*
  991. * IDEA: make spectrum more selfsimilar.
  992. *
  993. * (0) List all window numbers 0 - wlength-1 in int array W, and total-window-loudnesses in array L
  994. * (1) Scale all values in array L so 1 is maximum ....
  995. * (2) Create a scaling value array S, and set all these initially to 1.0
  996. * (3) List windows in loudness order in int-array Z
  997. * (4) Scaling each window to max loudness, compare it with every other window, and assign a similarity value
  998. * in a 1/2 matrix of values. (Lack of) Similarity = sum of all absolute differences between levels in all channels
  999. * (Deal with 0 level windows!!)
  1000. * (5) For each z in Z, find the K most similar windows k1,k2 etc, and replace those window numbers in W by a "z",
  1001. together with a scaling value in S to take amps of z into orig amp of the k1,k2 etc items
  1002. NB replace
  1003. * (6) Remove all other (z,n) (n,z) from the matrix, by setting similarity val to -1
  1004. * (7) for every k replaced, remove all (n,k) and (k,n) from the matrix, by setting similarity val to -1
  1005. * (7) Continue until Z exhausted or all similarity values set to -1 (keep a count of similarity vals)
  1006. *
  1007. * (4) For step 4, if z replaces g, compare similarity value of next most similar to z, and next most similar to g
  1008. * replacing whichever is the closer ..... or is this too complicated (once we have a chain of N comparisons!!!)
  1009. *
  1010. *
  1011. * Once we have a new order of windows
  1012. * (1) Get the window
  1013. * (2) Apply the scaling value
  1014. * (3) Output the window
  1015. */
  1016. //
  1017. // ibuf1, ibuf2, dissarray size (wlength * wlength)/2
  1018. /*************************************** CREATE_ORDER_AND_SCALING_ARRAYS ***************************************/
  1019. int create_order_and_scaling_arrays(dataptr dz) {
  1020. int n, m, itemp, samps_read;
  1021. int cc, vc;
  1022. float *ibuf = dz->flbufptr[0];
  1023. double *loudness; // (relative) loudness array, in original window order: size wlength
  1024. double *scaling; // Loudness scaling applied to windows in final output: size wlength
  1025. int *winorder; // Initial (and, later, Final) order of windows: size wlength
  1026. int *loudorder; // List of windows when arranged in decreasing loudness order: size wlength
  1027. int *winused; // List of windows already self-sim set
  1028. double loud, maxloud = 0.0;
  1029. if((dz->parray[0] = (double *)malloc(dz->wlength * dz->wlength * sizeof(double)))==NULL) {
  1030. sprintf(errstr,"Setting up array for similairty measure.\n");
  1031. return(MEMORY_ERROR);
  1032. }
  1033. if((dz->parray[1] = (double *)malloc(dz->wlength * sizeof(double)))==NULL) {
  1034. sprintf(errstr,"Setting up array for loudness values.\n");
  1035. return(MEMORY_ERROR);
  1036. }
  1037. if((dz->parray[2] = (double *)malloc(dz->wlength * sizeof(double)))==NULL) {
  1038. sprintf(errstr,"Setting up array for loudness scaling.\n");
  1039. return(MEMORY_ERROR);
  1040. }
  1041. if((dz->lparray[0] = (int *)malloc(dz->wlength * sizeof(int)))==NULL) {
  1042. sprintf(errstr,"Setting up array for final window order.\n");
  1043. return(MEMORY_ERROR);
  1044. }
  1045. if((dz->lparray[1] = (int *)malloc(dz->wlength * sizeof(int)))==NULL) {
  1046. sprintf(errstr,"Setting up array for window decreasing loudness order.\n");
  1047. return(MEMORY_ERROR);
  1048. }
  1049. if((dz->lparray[2] = (int *)malloc(dz->wlength * sizeof(int)))==NULL) {
  1050. sprintf(errstr,"Setting up array for marking windows used.\n");
  1051. return(MEMORY_ERROR);
  1052. }
  1053. loudness = dz->parray[1];
  1054. scaling = dz->parray[2];
  1055. winorder = dz->lparray[0];
  1056. loudorder = dz->lparray[1];
  1057. winused = dz->lparray[2];
  1058. for(n = 0;n < dz->wlength; n++) {
  1059. winorder[n] = n; // Store window numbers in their initial order
  1060. loudorder[n] = n; // Store window numbers in their order of decreasing LOUDNESS
  1061. winused[n] = 0; // Flag windows which have been processed (initially none)
  1062. if((samps_read = fgetfbufEx(ibuf,dz->wanted,dz->ifd[0],0))<0) {
  1063. sprintf(errstr,"Sound read error 1: %s\n",sferrstr());
  1064. return(SYSTEM_ERROR);
  1065. }
  1066. loud = 0.0; // Calculate and store total amplitude of window
  1067. for(cc=0,vc = 0;cc<dz->clength;cc++,vc+=2)
  1068. loud += ibuf[AMPP];
  1069. loudness[n] = loud;
  1070. maxloud = max(loud,maxloud); // Keep track of maximum window-amp
  1071. }
  1072. if(maxloud <= 0.0) {
  1073. sprintf(errstr,"No significant level found in spectrum.\n");
  1074. return DATA_ERROR;
  1075. }
  1076. for(n = 0;n < dz->wlength; n++) {
  1077. loudness[n] /= (double)maxloud; // Normalise window-loudness measures
  1078. scaling[n] = 1.0; // Preset output-scaling values to 1.0
  1079. }
  1080. // Create array of window-numbers in loudness order
  1081. for(n = 0;n < dz->wlength-1; n++) {
  1082. for(m = n+1;m < dz->wlength; m++) {
  1083. if(loudness[loudorder[m]] > loudness[loudorder[n]]) {
  1084. itemp = loudorder[m];
  1085. loudorder[m] = loudorder[n];
  1086. loudorder[n] = itemp;
  1087. }
  1088. }
  1089. }
  1090. return FINISHED;
  1091. }
  1092. /*************************************** CREATE_DISSIMILARITY_ARRAY ***************************************/
  1093. int create_dissimilarity_array(dataptr dz) {
  1094. int n, m, k1, k2, nbase, mbase, samps_read, zz = 0, measure = 256 * dz->wanted;
  1095. int cc, vc;
  1096. double *dissarray = dz->parray[0]; // Dissimilarity-measure array: size (wlength * wlength);
  1097. double *loudness = dz->parray[1];
  1098. float *ibuf1 = dz->flbufptr[0], *ibuf2 = dz->flbufptr[1];
  1099. double dissim;
  1100. fprintf(stdout,"INFO: Assessing self-similarity of %d windows.\n",dz->wlength);
  1101. fflush(stdout);
  1102. for(n = 0;n < dz->wlength-1; n++) {
  1103. nbase = n * dz->wlength;
  1104. sndseekEx(dz->ifd[0],n * dz->wanted,0); // Seek to next window, and read it
  1105. if((samps_read = fgetfbufEx(ibuf1,dz->wanted,dz->ifd[0],0))<0) {
  1106. sprintf(errstr,"Sound read error 1: %s\n",sferrstr());
  1107. return(SYSTEM_ERROR);
  1108. }
  1109. for(m = n+1;m < dz->wlength; m++) {
  1110. mbase = m * dz->wlength;
  1111. if((samps_read = fgetfbufEx(ibuf2,dz->wanted,dz->ifd[0],0))<0) {
  1112. sprintf(errstr,"Sound read error 2: %s\n",sferrstr());
  1113. return(SYSTEM_ERROR);
  1114. }
  1115. dissim = 0.0;
  1116. for(cc=0,vc = 0;cc<dz->clength;cc++,vc+=2)
  1117. // (1) Scale the amplitudes of the windows to match maxamp
  1118. // (2) Frq component only significant in lowest channels
  1119. dissim += fabs((ibuf1[AMPP]/loudness[n]) - (ibuf2[AMPP]/loudness[m])) + fabs(ibuf1[FREQ] - ibuf2[FREQ]);
  1120. k1 = nbase + m;
  1121. dissarray[k1] = dissim/(double)dz->clength;
  1122. k2 = mbase + n;
  1123. dissarray[k2] = dissarray[k1]; // Make a symmetrical array: easier later!!!
  1124. if(++zz % measure == 0) {
  1125. fprintf(stdout,"INFO: Assessed %d windows.\n",n+1);
  1126. fflush(stdout);
  1127. }
  1128. }
  1129. }
  1130. fprintf(stdout,"INFO: Assessed %d windows.\n",dz->wlength);
  1131. fflush(stdout);
  1132. for(n = 0;n < dz->wlength; n++) {
  1133. k1 = (n * dz->wlength) + n;
  1134. dissarray[k1] = 0.0; // Neutralise the diagonal ... don't compare window with itself
  1135. }
  1136. return FINISHED;
  1137. }
  1138. /*************************************** DO_SELFSIM ***************************************/
  1139. int do_selfsim(dataptr dz)
  1140. {
  1141. int exit_status, selfsim_cnt = dz->iparam[0], cc, vc;
  1142. float *ibuf = dz->flbufptr[0];
  1143. double minval;
  1144. int n, m, k,jj, windowsset = 0, thiswindow, thisclose, closest, arrayclosest, lastwindow, samps_read;
  1145. double *dissarray = dz->parray[0]; // Dissimilarity-measure array: size (wlength * wlength)/2;
  1146. double *loudness = dz->parray[1]; // (relative) loudness array, in window order: size wlength
  1147. double *scaling = dz->parray[2]; // Loudness scaling applied to windows in final output order
  1148. int *winorder = dz->lparray[0]; // Final order of windows: size wlength
  1149. int *loudorder = dz->lparray[1]; // List of windows when arranged in decreasing loudnewss order: size wlength
  1150. int *winused = dz->lparray[2]; // List of windows already self-sim set
  1151. fprintf(stdout,"INFO: Reconstructing self-similarity data.\n");
  1152. fflush(stdout);
  1153. for(n = 0; n < dz->wlength;n++) {
  1154. thiswindow = loudorder[n]; // Get next loudest window
  1155. if(winused[thiswindow]) // If this window has already been self-similared,
  1156. jj = winorder[thiswindow]; // use the self-similar window again,
  1157. else // else use this window
  1158. jj = thiswindow;
  1159. for(k = 0; k < selfsim_cnt;k++) {
  1160. thisclose = thiswindow * dz->wlength; // Find matrix row entry gives first dissimilarity value in array
  1161. minval = HUGE; // Set this as LEAST dissimilar = most similar
  1162. closest = 0; // SAFETY
  1163. arrayclosest = thisclose; // SAFETY
  1164. for(m = 0;m < dz->wlength;m++) { // Find the most similar
  1165. if(m==n)
  1166. continue; // No self-comparison
  1167. if(dissarray[thisclose] < minval) {
  1168. closest = m;
  1169. minval = dissarray[thisclose];
  1170. arrayclosest = thisclose;
  1171. }
  1172. thisclose++;
  1173. }
  1174. winorder[closest] = jj; // In the output window order, replace window "closest" by window "jj"
  1175. scaling[closest] = loudness[closest]/loudness[jj]; // Scale to an appropriate amplitude
  1176. winused[closest] = 1; // Mark window as already having being assigned
  1177. dissarray[arrayclosest] = HUGE; // Avoid it being used again when looking for next most selfsim window
  1178. windowsset++; // Count number of windows set
  1179. if(windowsset >= dz->wlength)
  1180. break;
  1181. }
  1182. winused[n] = 1;
  1183. windowsset++;
  1184. if(windowsset >= dz->wlength)
  1185. break;
  1186. }
  1187. lastwindow = -1;
  1188. fprintf(stdout,"INFO: Creating self-similar output.\n");
  1189. fflush(stdout);
  1190. for(n = 0; n < dz->wlength;n++) {
  1191. thiswindow = winorder[n];
  1192. if(thiswindow != lastwindow) {
  1193. sndseekEx(dz->ifd[0],thiswindow * dz->wanted,0); // Seek to next window, and read it
  1194. if((samps_read = fgetfbufEx(ibuf,dz->wanted,dz->ifd[0],0))<0) {
  1195. sprintf(errstr,"Sound read error 3: %s\n",sferrstr());
  1196. return(SYSTEM_ERROR);
  1197. }
  1198. }
  1199. for(cc=0,vc = 0;cc < dz->clength;cc++,vc+=2)
  1200. ibuf[AMPP] = (float)(ibuf[AMPP] * scaling[n]);
  1201. if((exit_status = write_samps(ibuf,dz->wanted,dz))<0)
  1202. return(exit_status);
  1203. lastwindow = thiswindow;
  1204. }
  1205. return FINISHED;
  1206. }