newdelay.c 50 KB

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