distortt.c 50 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 <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. char errstr[2400];
  44. int anal_infiles = 1;
  45. int sloom = 0;
  46. int sloombatch = 0;
  47. const char* cdp_version = "6.1.0";
  48. //CDP LIB REPLACEMENTS
  49. //static int check_gate_param_validity_and_consistency(dataptr dz);
  50. static int setup_distortt_application(dataptr dz);
  51. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  52. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  53. static int setup_distortt_param_ranges_and_defaults(dataptr dz);
  54. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  55. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  56. static int establish_application(dataptr dz);
  57. static int initialise_vflags(dataptr dz);
  58. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  59. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  60. static int mark_parameter_types(dataptr dz,aplptr ap);
  61. static int assign_file_data_storage(int infilecnt,dataptr dz);
  62. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  63. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  64. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  65. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  66. static int distortt_param_preprocess(dataptr dz);
  67. static int create_distortt_sndbufs(dataptr dz);
  68. static int do_distortt(dataptr dz);
  69. static int get_initial_phase(int *ipos, dataptr dz);
  70. /**************************************** MAIN *********************************************/
  71. int main(int argc,char *argv[])
  72. {
  73. int exit_status;
  74. dataptr dz = NULL;
  75. char **cmdline;
  76. int cmdlinecnt;
  77. int n;
  78. // aplptr ap;
  79. int is_launched = FALSE;
  80. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  81. fprintf(stdout,"%s\n",cdp_version);
  82. fflush(stdout);
  83. return 0;
  84. }
  85. /* CHECK FOR SOUNDLOOM */
  86. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  87. sloom = 0;
  88. sloombatch = 1;
  89. }
  90. if(sflinit("cdp")){
  91. sfperror("cdp: initialisation\n");
  92. return(FAILED);
  93. }
  94. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  95. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  96. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  97. return(FAILED);
  98. }
  99. if(!sloom) {
  100. if(argc == 1) {
  101. usage1();
  102. return(FAILED);
  103. } else if(argc == 2) {
  104. usage2(argv[1]);
  105. return(FAILED);
  106. }
  107. }
  108. if(!sloom) {
  109. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  110. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  111. return(FAILED);
  112. }
  113. cmdline = argv;
  114. cmdlinecnt = argc;
  115. if((get_the_process_no(argv[0],dz))<0)
  116. return(FAILED);
  117. cmdline++;
  118. cmdlinecnt--;
  119. // setup_particular_application =
  120. if((exit_status = setup_distortt_application(dz))<0) {
  121. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  122. return(FAILED);
  123. }
  124. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  125. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  126. return(FAILED);
  127. }
  128. } else {
  129. //parse_TK_data() =
  130. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  131. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  132. return(exit_status);
  133. }
  134. }
  135. // ap = dz->application;
  136. // parse_infile_and_hone_type() =
  137. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  138. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  139. return(FAILED);
  140. }
  141. // setup_param_ranges_and_defaults() =
  142. if((exit_status = setup_distortt_param_ranges_and_defaults(dz))<0) {
  143. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  144. return(FAILED);
  145. }
  146. // open_first_infile CDP LIB
  147. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  148. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  149. return(FAILED);
  150. }
  151. cmdlinecnt--;
  152. cmdline++;
  153. // handle_extra_infiles() : redundant
  154. // handle_outfile() =
  155. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  156. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  157. return(FAILED);
  158. }
  159. // handle_formants() redundant
  160. // handle_formant_quiksearch() redundant
  161. // handle_special_data() redundant
  162. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  163. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  164. return(FAILED);
  165. }
  166. is_launched = TRUE;
  167. dz->bufcnt = 3;
  168. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  169. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  170. return(MEMORY_ERROR);
  171. }
  172. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  173. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  174. return(MEMORY_ERROR);
  175. }
  176. for(n = 0;n <dz->bufcnt; n++)
  177. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  178. dz->sampbuf[n] = (float *)0;
  179. if((exit_status = create_distortt_sndbufs(dz))<0) { // CDP LIB
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. //param_preprocess() redundant
  184. if((exit_status = distortt_param_preprocess(dz))<0) {
  185. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  186. return(FAILED);
  187. }
  188. //spec_process_file =
  189. if((exit_status = do_distortt(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. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  273. if(brkcnt>0) {
  274. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  275. return(exit_status);
  276. }
  277. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  278. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  279. return(exit_status);
  280. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  281. return(exit_status);
  282. }
  283. if((exit_status = mark_parameter_types(dz,ap))<0)
  284. return(exit_status);
  285. // establish_infile_constants() replaced by
  286. dz->infilecnt = 1;
  287. //establish_bufptrs_and_extra_buffers():
  288. return(FINISHED);
  289. }
  290. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  291. /* RWD mallo changed to calloc; helps debug verison run as release! */
  292. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  293. {
  294. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  295. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  296. return(MEMORY_ERROR);
  297. }
  298. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  299. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  300. return(MEMORY_ERROR);
  301. }
  302. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  303. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  304. return(MEMORY_ERROR);
  305. }
  306. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  307. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  308. return(MEMORY_ERROR);
  309. }
  310. return(FINISHED);
  311. }
  312. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  313. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  314. {
  315. int n;
  316. for(n=0;n<storage_cnt;n++) {
  317. dz->is_int[n] = (char)0;
  318. dz->no_brk[n] = (char)0;
  319. }
  320. return(FINISHED);
  321. }
  322. /***************************** MARK_PARAMETER_TYPES **************************/
  323. int mark_parameter_types(dataptr dz,aplptr ap)
  324. {
  325. int n, m; /* PARAMS */
  326. for(n=0;n<ap->max_param_cnt;n++) {
  327. switch(ap->param_list[n]) {
  328. case('0'): break; /* dz->is_active[n] = 0 is default */
  329. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  330. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  331. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  332. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  333. default:
  334. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  335. return(PROGRAM_ERROR);
  336. }
  337. } /* OPTIONS */
  338. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  339. switch(ap->option_list[n]) {
  340. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  341. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  342. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  343. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  344. default:
  345. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  346. return(PROGRAM_ERROR);
  347. }
  348. } /* VARIANTS */
  349. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  350. switch(ap->variant_list[n]) {
  351. case('0'): break;
  352. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  353. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  354. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  355. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  356. default:
  357. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  358. return(PROGRAM_ERROR);
  359. }
  360. } /* INTERNAL */
  361. for(n=0,
  362. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  363. switch(ap->internal_param_list[n]) {
  364. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  365. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  366. case('d'): dz->no_brk[m] = (char)1; break;
  367. default:
  368. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  369. return(PROGRAM_ERROR);
  370. }
  371. }
  372. return(FINISHED);
  373. }
  374. /************************ HANDLE_THE_OUTFILE *********************/
  375. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  376. {
  377. int exit_status;
  378. char *filename = (*cmdline)[0];
  379. if(filename[0]=='-' && filename[1]=='f') {
  380. dz->floatsam_output = 1;
  381. dz->true_outfile_stype = SAMP_FLOAT;
  382. filename+= 2;
  383. }
  384. if(!sloom) {
  385. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  386. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  387. return(DATA_ERROR);
  388. }
  389. }
  390. strcpy(dz->outfilename,filename);
  391. if((exit_status = create_sized_outfile(filename,dz))<0)
  392. return(exit_status);
  393. (*cmdline)++;
  394. (*cmdlinecnt)--;
  395. return(FINISHED);
  396. }
  397. /***************************** ESTABLISH_APPLICATION **************************/
  398. int establish_application(dataptr dz)
  399. {
  400. aplptr ap;
  401. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  402. sprintf(errstr,"establish_application()\n");
  403. return(MEMORY_ERROR);
  404. }
  405. ap = dz->application;
  406. memset((char *)ap,0,sizeof(struct applic));
  407. return(FINISHED);
  408. }
  409. /************************* INITIALISE_VFLAGS *************************/
  410. int initialise_vflags(dataptr dz)
  411. {
  412. int n;
  413. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  414. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  415. return(MEMORY_ERROR);
  416. }
  417. for(n=0;n<dz->application->vflag_cnt;n++)
  418. dz->vflag[n] = FALSE;
  419. return FINISHED;
  420. }
  421. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  422. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  423. {
  424. int n;
  425. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  426. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  427. return(MEMORY_ERROR);
  428. }
  429. for(n=0;n<tipc;n++)
  430. ap->default_val[n] = 0.0;
  431. return(FINISHED);
  432. }
  433. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  434. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  435. {
  436. int n;
  437. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  438. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  439. return(MEMORY_ERROR);
  440. }
  441. for(n=0;n<tipc;n++)
  442. dz->is_active[n] = (char)0;
  443. return(FINISHED);
  444. }
  445. /************************* SETUP_DISTORTT_APPLICATION *******************/
  446. int setup_distortt_application(dataptr dz)
  447. {
  448. int exit_status;
  449. aplptr ap;
  450. if((exit_status = establish_application(dz))<0) // GLOBAL
  451. return(FAILED);
  452. ap = dz->application;
  453. // SEE parstruct FOR EXPLANATION of next 2 functions
  454. if((exit_status = set_param_data(ap,0 ,4,4,"iidd"))<0)
  455. return(FAILED);
  456. if((exit_status = set_vflgs(ap,"",0,"","t",1,0,"0"))<0)
  457. return(FAILED);
  458. // set_legal_infile_structure -->
  459. dz->has_otherfile = FALSE;
  460. // assign_process_logic -->
  461. dz->input_data_type = SNDFILES_ONLY;
  462. dz->process_type = UNEQUAL_SNDFILE;
  463. dz->outfiletype = SNDFILE_OUT;
  464. return application_init(dz); //GLOBAL
  465. }
  466. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  467. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  468. {
  469. int exit_status;
  470. infileptr infile_info;
  471. if(!sloom) {
  472. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  473. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  474. return(MEMORY_ERROR);
  475. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  476. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  477. return(PROGRAM_ERROR);
  478. } else if(infile_info->filetype != SNDFILE) {
  479. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  480. return(DATA_ERROR);
  481. } else if(infile_info->channels != 1) {
  482. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  483. return(DATA_ERROR);
  484. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  485. sprintf(errstr,"Failed to copy file parsing information\n");
  486. return(PROGRAM_ERROR);
  487. }
  488. free(infile_info);
  489. }
  490. return(FINISHED);
  491. }
  492. /************************* SETUP_DISTORTT_PARAM_RANGES_AND_DEFAULTS *******************/
  493. int setup_distortt_param_ranges_and_defaults(dataptr dz)
  494. {
  495. int exit_status;
  496. aplptr ap = dz->application;
  497. // set_param_ranges()
  498. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  499. // NB total_input_param_cnt is > 0 !!!
  500. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  501. return(FAILED);
  502. // get_param_ranges()
  503. ap->lo[0] = 1; // Count of wavesets in group
  504. ap->hi[0] = 256;
  505. ap->default_val[0] = 1;
  506. ap->lo[1] = 2; // Wavset-group repetitions
  507. ap->hi[1] = 16;
  508. ap->default_val[1] = 2;
  509. ap->lo[2] = 0.0;
  510. ap->hi[2] = dz->duration * SECS_TO_MS; // Sound to skip at file start (mS)
  511. ap->default_val[2] = MZIG_SPLICELEN; // 15 mS = segment(MSPLICE)
  512. ap->lo[3] = dz->duration;
  513. ap->hi[3] = dz->duration * 100.0; // Output duration required
  514. ap->default_val[3] = dz->duration;
  515. dz->maxmode = 0;
  516. if(!sloom)
  517. put_default_vals_in_all_params(dz);
  518. return(FINISHED);
  519. }
  520. /********************************* PARSE_SLOOM_DATA *********************************/
  521. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  522. {
  523. int exit_status;
  524. int cnt = 1, infilecnt;
  525. int filesize, insams, inbrksize;
  526. double dummy;
  527. int true_cnt = 0;
  528. // aplptr ap;
  529. while(cnt<=PRE_CMDLINE_DATACNT) {
  530. if(cnt > argc) {
  531. sprintf(errstr,"Insufficient data sent from TK\n");
  532. return(DATA_ERROR);
  533. }
  534. switch(cnt) {
  535. case(1):
  536. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  537. sprintf(errstr,"Cannot read process no. sent from TK\n");
  538. return(DATA_ERROR);
  539. }
  540. break;
  541. case(2):
  542. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  543. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  544. return(DATA_ERROR);
  545. }
  546. if(dz->mode > 0)
  547. dz->mode--;
  548. //setup_particular_application() =
  549. if((exit_status = setup_distortt_application(dz))<0)
  550. return(exit_status);
  551. // ap = dz->application;
  552. break;
  553. case(3):
  554. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  555. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  556. return(DATA_ERROR);
  557. }
  558. if(infilecnt < 1) {
  559. true_cnt = cnt + 1;
  560. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  561. }
  562. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  563. return(exit_status);
  564. break;
  565. case(INPUT_FILETYPE+4):
  566. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  567. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  568. return(DATA_ERROR);
  569. }
  570. break;
  571. case(INPUT_FILESIZE+4):
  572. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  573. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  574. return(DATA_ERROR);
  575. }
  576. dz->insams[0] = filesize;
  577. break;
  578. case(INPUT_INSAMS+4):
  579. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  580. sprintf(errstr,"Cannot read insams sent from TK\n");
  581. return(DATA_ERROR);
  582. }
  583. dz->insams[0] = insams;
  584. break;
  585. case(INPUT_SRATE+4):
  586. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  587. sprintf(errstr,"Cannot read srate sent from TK\n");
  588. return(DATA_ERROR);
  589. }
  590. break;
  591. case(INPUT_CHANNELS+4):
  592. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  593. sprintf(errstr,"Cannot read channels sent from TK\n");
  594. return(DATA_ERROR);
  595. }
  596. break;
  597. case(INPUT_STYPE+4):
  598. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  599. sprintf(errstr,"Cannot read stype sent from TK\n");
  600. return(DATA_ERROR);
  601. }
  602. break;
  603. case(INPUT_ORIGSTYPE+4):
  604. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  605. sprintf(errstr,"Cannot read origstype sent from TK\n");
  606. return(DATA_ERROR);
  607. }
  608. break;
  609. case(INPUT_ORIGRATE+4):
  610. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  611. sprintf(errstr,"Cannot read origrate sent from TK\n");
  612. return(DATA_ERROR);
  613. }
  614. break;
  615. case(INPUT_MLEN+4):
  616. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  617. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  618. return(DATA_ERROR);
  619. }
  620. break;
  621. case(INPUT_DFAC+4):
  622. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  623. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  624. return(DATA_ERROR);
  625. }
  626. break;
  627. case(INPUT_ORIGCHANS+4):
  628. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  629. sprintf(errstr,"Cannot read origchans sent from TK\n");
  630. return(DATA_ERROR);
  631. }
  632. break;
  633. case(INPUT_SPECENVCNT+4):
  634. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  635. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  636. return(DATA_ERROR);
  637. }
  638. dz->specenvcnt = dz->infile->specenvcnt;
  639. break;
  640. case(INPUT_WANTED+4):
  641. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  642. sprintf(errstr,"Cannot read wanted sent from TK\n");
  643. return(DATA_ERROR);
  644. }
  645. break;
  646. case(INPUT_WLENGTH+4):
  647. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  648. sprintf(errstr,"Cannot read wlength sent from TK\n");
  649. return(DATA_ERROR);
  650. }
  651. break;
  652. case(INPUT_OUT_CHANS+4):
  653. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  654. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  655. return(DATA_ERROR);
  656. }
  657. break;
  658. /* RWD these chanegs to samps - tk will have to deal with that! */
  659. case(INPUT_DESCRIPTOR_BYTES+4):
  660. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  661. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  662. return(DATA_ERROR);
  663. }
  664. break;
  665. case(INPUT_IS_TRANSPOS+4):
  666. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  667. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  668. return(DATA_ERROR);
  669. }
  670. break;
  671. case(INPUT_COULD_BE_TRANSPOS+4):
  672. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  673. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  674. return(DATA_ERROR);
  675. }
  676. break;
  677. case(INPUT_COULD_BE_PITCH+4):
  678. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  679. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  680. return(DATA_ERROR);
  681. }
  682. break;
  683. case(INPUT_DIFFERENT_SRATES+4):
  684. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  685. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  686. return(DATA_ERROR);
  687. }
  688. break;
  689. case(INPUT_DUPLICATE_SNDS+4):
  690. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  691. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  692. return(DATA_ERROR);
  693. }
  694. break;
  695. case(INPUT_BRKSIZE+4):
  696. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  697. sprintf(errstr,"Cannot read brksize sent from TK\n");
  698. return(DATA_ERROR);
  699. }
  700. if(inbrksize > 0) {
  701. switch(dz->input_data_type) {
  702. case(WORDLIST_ONLY):
  703. break;
  704. case(PITCH_AND_PITCH):
  705. case(PITCH_AND_TRANSPOS):
  706. case(TRANSPOS_AND_TRANSPOS):
  707. dz->tempsize = inbrksize;
  708. break;
  709. case(BRKFILES_ONLY):
  710. case(UNRANGED_BRKFILE_ONLY):
  711. case(DB_BRKFILES_ONLY):
  712. case(ALL_FILES):
  713. case(ANY_NUMBER_OF_ANY_FILES):
  714. if(dz->extrabrkno < 0) {
  715. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  716. return(DATA_ERROR);
  717. }
  718. if(dz->brksize == NULL) {
  719. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  720. return(PROGRAM_ERROR);
  721. }
  722. dz->brksize[dz->extrabrkno] = inbrksize;
  723. break;
  724. default:
  725. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  726. dz->input_data_type);
  727. return(PROGRAM_ERROR);
  728. }
  729. break;
  730. }
  731. break;
  732. case(INPUT_NUMSIZE+4):
  733. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  734. sprintf(errstr,"Cannot read numsize sent from TK\n");
  735. return(DATA_ERROR);
  736. }
  737. break;
  738. case(INPUT_LINECNT+4):
  739. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  740. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  741. return(DATA_ERROR);
  742. }
  743. break;
  744. case(INPUT_ALL_WORDS+4):
  745. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  746. sprintf(errstr,"Cannot read all_words sent from TK\n");
  747. return(DATA_ERROR);
  748. }
  749. break;
  750. case(INPUT_ARATE+4):
  751. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  752. sprintf(errstr,"Cannot read arate sent from TK\n");
  753. return(DATA_ERROR);
  754. }
  755. break;
  756. case(INPUT_FRAMETIME+4):
  757. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  758. sprintf(errstr,"Cannot read frametime sent from TK\n");
  759. return(DATA_ERROR);
  760. }
  761. dz->frametime = (float)dummy;
  762. break;
  763. case(INPUT_WINDOW_SIZE+4):
  764. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  765. sprintf(errstr,"Cannot read window_size sent from TK\n");
  766. return(DATA_ERROR);
  767. }
  768. break;
  769. case(INPUT_NYQUIST+4):
  770. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  771. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  772. return(DATA_ERROR);
  773. }
  774. break;
  775. case(INPUT_DURATION+4):
  776. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  777. sprintf(errstr,"Cannot read duration sent from TK\n");
  778. return(DATA_ERROR);
  779. }
  780. break;
  781. case(INPUT_MINBRK+4):
  782. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  783. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  784. return(DATA_ERROR);
  785. }
  786. break;
  787. case(INPUT_MAXBRK+4):
  788. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  789. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  790. return(DATA_ERROR);
  791. }
  792. break;
  793. case(INPUT_MINNUM+4):
  794. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  795. sprintf(errstr,"Cannot read minnum sent from TK\n");
  796. return(DATA_ERROR);
  797. }
  798. break;
  799. case(INPUT_MAXNUM+4):
  800. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  801. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  802. return(DATA_ERROR);
  803. }
  804. break;
  805. default:
  806. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  807. return(PROGRAM_ERROR);
  808. }
  809. cnt++;
  810. }
  811. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  812. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  813. return(DATA_ERROR);
  814. }
  815. if(true_cnt)
  816. cnt = true_cnt;
  817. *cmdlinecnt = 0;
  818. while(cnt < argc) {
  819. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  820. return(exit_status);
  821. cnt++;
  822. }
  823. return(FINISHED);
  824. }
  825. /********************************* GET_TK_CMDLINE_WORD *********************************/
  826. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  827. {
  828. if(*cmdlinecnt==0) {
  829. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  830. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  831. return(MEMORY_ERROR);
  832. }
  833. } else {
  834. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  835. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  836. return(MEMORY_ERROR);
  837. }
  838. }
  839. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  840. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  841. return(MEMORY_ERROR);
  842. }
  843. strcpy((*cmdline)[*cmdlinecnt],q);
  844. (*cmdlinecnt)++;
  845. return(FINISHED);
  846. }
  847. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  848. int assign_file_data_storage(int infilecnt,dataptr dz)
  849. {
  850. int exit_status;
  851. int no_sndfile_system_files = FALSE;
  852. dz->infilecnt = infilecnt;
  853. if((exit_status = allocate_filespace(dz))<0)
  854. return(exit_status);
  855. if(no_sndfile_system_files)
  856. dz->infilecnt = 0;
  857. return(FINISHED);
  858. }
  859. /************************* redundant functions: to ensure libs compile OK *******************/
  860. int assign_process_logic(dataptr dz)
  861. {
  862. return(FINISHED);
  863. }
  864. void set_legal_infile_structure(dataptr dz)
  865. {}
  866. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  867. {
  868. return(FINISHED);
  869. }
  870. int setup_internal_arrays_and_array_pointers(dataptr dz)
  871. {
  872. return(FINISHED);
  873. }
  874. int establish_bufptrs_and_extra_buffers(dataptr dz)
  875. {
  876. return(FINISHED);
  877. }
  878. int read_special_data(char *str,dataptr dz)
  879. {
  880. return(FINISHED);
  881. }
  882. int inner_loop
  883. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  884. {
  885. return(FINISHED);
  886. }
  887. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  888. {
  889. return(FINISHED);
  890. }
  891. /******************************** USAGE1 ********************************/
  892. int usage1(void)
  893. {
  894. usage2("repeat");
  895. return(USAGE_ONLY);
  896. }
  897. /********************************************************************************************/
  898. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  899. {
  900. if(!strcmp(prog_identifier_from_cmdline,"repeat")) dz->process = DISTORTT;
  901. else {
  902. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  903. return(USAGE_ONLY);
  904. }
  905. return(FINISHED);
  906. }
  907. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  908. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  909. {
  910. int n;
  911. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  912. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  913. return(MEMORY_ERROR);
  914. }
  915. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  916. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  917. return(MEMORY_ERROR);
  918. }
  919. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  920. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  921. return(MEMORY_ERROR);
  922. }
  923. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  924. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  925. return(MEMORY_ERROR);
  926. }
  927. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  928. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  929. return(MEMORY_ERROR);
  930. }
  931. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  932. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  933. return(MEMORY_ERROR);
  934. }
  935. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  936. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  937. return(MEMORY_ERROR);
  938. }
  939. for(n=0;n<brkcnt;n++) {
  940. dz->brk[n] = NULL;
  941. dz->brkptr[n] = NULL;
  942. dz->brkinit[n] = 0;
  943. dz->brksize[n] = 0;
  944. }
  945. return(FINISHED);
  946. }
  947. /******************************** USAGE2 ********************************/
  948. int usage2(char *str)
  949. {
  950. if(!strcmp(str,"repeat")) {
  951. fprintf(stderr,
  952. "USAGE:\n"
  953. "distortt repeat infile outfile gpcnt rpt offset dur [-t]\n"
  954. "\n"
  955. "Skip initial 'offset', then each group of 'gpcnt' wavesets repeats 'rpt' times,\n"
  956. "until duration 'dur' is exceeded (or waveset data is exhausted).\n"
  957. "\n"
  958. "GPCNT Number of wavesets in the group to be repeated.\n"
  959. "RPT Number of repetitions of each waveset group.\n"
  960. "OFFSET Time, in mS, to skip, before starting waveset process.\n"
  961. " (sound in offset is suffixed before the output snd).\n"
  962. "DUR Required duration of output.\n"
  963. "-t Telescope: skip wavesets so output is similar size to input.\n"
  964. "\n");
  965. } else
  966. fprintf(stdout,"Unknown option '%s'\n",str);
  967. return(USAGE_ONLY);
  968. }
  969. int usage3(char *str1,char *str2)
  970. {
  971. fprintf(stderr,"Insufficient parameters on command line.\n");
  972. return(USAGE_ONLY);
  973. }
  974. /***************************** GETLEN **************************/
  975. int do_distortt(dataptr dz)
  976. {
  977. int exit_status, done, do_output;
  978. double srate = (double)dz->infile->srate;
  979. int goalsamps = (int)ceil(dz->param[3] * srate);
  980. int sampsout, splicelen, splicestart, splicepos, gplen;
  981. float *ibuf = dz->sampbuf[0], *wbuf = dz->sampbuf[1], *obuf = dz->sampbuf[2];
  982. int initial_phase, wavesetcnt = 0, repeats = dz->iparam[1], gpsize = dz->iparam[0];
  983. int startsamp, ipos, opos, wpos, n, grpno;
  984. double spliceincr;
  985. sampsout = 0;
  986. startsamp = dz->iparam[2];
  987. splicelen = (int)round(MZIG_SPLICELEN * MS_TO_SECS * srate);
  988. splicestart = goalsamps - splicelen;
  989. while (dz->total_samps_read < startsamp) {
  990. if((exit_status = read_samps(ibuf,dz))<0)
  991. return(exit_status);
  992. if(dz->ssampsread <= 0) {
  993. sprintf(errstr,"Failed to find post-offset start of sound.\n");
  994. return GOAL_FAILED;
  995. }
  996. if(dz->total_samps_read < startsamp) { // Until offset reached, copy input to output --> file
  997. if((exit_status = write_samps(ibuf,dz->buflen,dz))<0)
  998. return(exit_status);
  999. sampsout += dz->buflen;
  1000. }
  1001. }
  1002. ipos = startsamp; // search for initial phase
  1003. initial_phase = get_initial_phase(&ipos,dz);
  1004. while(initial_phase == 0) { // if initial phase (& start of initial waveset) not in this buffer
  1005. if((exit_status = write_samps(ibuf,dz->buflen,dz))<0) // Until first waveset-start is reached, copy input to file
  1006. return(exit_status);
  1007. sampsout += dz->buflen;
  1008. if(sampsout >= goalsamps) {
  1009. sprintf(errstr,"Failed to find start of wavesets (1).\n");
  1010. return GOAL_FAILED;
  1011. }
  1012. if((exit_status = read_samps(ibuf,dz))<0) // Get more of input
  1013. return(exit_status);
  1014. if(dz->ssampsread <= 0) { // If we run out of input, no phase found
  1015. sprintf(errstr,"Failed to find start of wavesets (2).\n");
  1016. return GOAL_FAILED;
  1017. }
  1018. ipos = 0;
  1019. initial_phase = get_initial_phase(&ipos,dz);
  1020. }
  1021. sampsout += ipos;
  1022. if(sampsout >= goalsamps) {
  1023. sprintf(errstr,"Failed to find start of wavesets (3).\n");
  1024. return GOAL_FAILED;
  1025. }
  1026. memcpy((char *)obuf,(char *)ibuf,ipos * sizeof(float)); // Copy all sound before offset to output buffer
  1027. opos = ipos;
  1028. done = 0;
  1029. grpno = 0;
  1030. while(!done) {
  1031. wpos = 0;
  1032. do { // find gpsize wavesets, writing them to wbuf
  1033. switch(initial_phase) {
  1034. case(1):
  1035. while (ibuf[ipos] >= 0.0) {
  1036. wbuf[wpos] = ibuf[ipos];
  1037. if(++wpos >= dz->buflen) {
  1038. sprintf(errstr,"Waveset buffer overrun.\n");
  1039. return PROGRAM_ERROR;
  1040. }
  1041. ipos++;
  1042. if(ipos >= dz->ssampsread) {
  1043. if((exit_status = read_samps(ibuf,dz))<0) // Get more of input
  1044. return(exit_status);
  1045. if(dz->ssampsread <= 0) { // If we run out of input, not enough wavesets for next group, quit
  1046. done = 1; // break from while loop
  1047. break;
  1048. }
  1049. ipos = 0;
  1050. }
  1051. }
  1052. if(!done) {
  1053. while (ibuf[ipos] <= 0.0) {
  1054. wbuf[wpos] = ibuf[ipos];
  1055. if(++wpos >= dz->buflen) {
  1056. sprintf(errstr,"Waveset buffer overrun.\n");
  1057. return PROGRAM_ERROR;
  1058. }
  1059. ipos++;
  1060. if(ipos >= dz->ssampsread) {
  1061. if((exit_status = read_samps(ibuf,dz))<0) // Get more of input
  1062. return(exit_status);
  1063. if(dz->ssampsread <= 0) { // If we run out of input, not enough wavesets found
  1064. done = 1; // break from while loop
  1065. break;
  1066. }
  1067. ipos = 0;
  1068. }
  1069. }
  1070. if(!done)
  1071. wavesetcnt++;
  1072. }
  1073. break;
  1074. case(-1):
  1075. while (ibuf[ipos] <= 0.0) {
  1076. wbuf[wpos] = ibuf[ipos];
  1077. if(++wpos >= dz->buflen) {
  1078. sprintf(errstr,"Waveset buffer overrun.\n");
  1079. return PROGRAM_ERROR;
  1080. }
  1081. ipos++;
  1082. if(ipos >= dz->ssampsread) {
  1083. if((exit_status = read_samps(ibuf,dz))<0) // Get more of input
  1084. return(exit_status);
  1085. if(dz->ssampsread <= 0) { // If we run out of input, not enough wavesets for next group, quit
  1086. done = 1; // break from while loop
  1087. break;
  1088. }
  1089. ipos = 0;
  1090. }
  1091. }
  1092. if(!done) {
  1093. while (ibuf[ipos] >= 0.0) {
  1094. wbuf[wpos] = ibuf[ipos];
  1095. if(++wpos >= dz->buflen) {
  1096. sprintf(errstr,"Waveset buffer overrun.\n");
  1097. return PROGRAM_ERROR;
  1098. }
  1099. ipos++;
  1100. if(ipos >= dz->ssampsread) {
  1101. if((exit_status = read_samps(ibuf,dz))<0) // Get more of input
  1102. return(exit_status);
  1103. if(dz->ssampsread <= 0) { // If we run out of input, not enough wavesets found
  1104. done = 1; // break from while loop
  1105. break;
  1106. }
  1107. ipos = 0;
  1108. }
  1109. }
  1110. if(!done)
  1111. wavesetcnt++;
  1112. }
  1113. break;
  1114. }
  1115. } while(!done && (wavesetcnt < gpsize)); // Stop if "done" or when desired number of wavesets found
  1116. if(done) // Run out of wavesets to use to build group, break from outer "while" loop
  1117. break;
  1118. do_output = 1;
  1119. if(dz->vflag[0]) { // If telescoping flag set
  1120. if(grpno % repeats > 0) // Only 1 in "repeats" waveset-groups is output
  1121. do_output = 0;
  1122. grpno++;
  1123. }
  1124. if (do_output) {
  1125. gplen = wpos;
  1126. for(n = 0; n < repeats; n++) {
  1127. wpos = 0;
  1128. while(wpos < gplen) {
  1129. obuf[opos] = wbuf[wpos++];
  1130. if((splicepos = sampsout - splicestart) > 0) { // If we're in endsplice
  1131. spliceincr = (double)(splicelen - splicepos)/(double)splicelen;
  1132. obuf[opos] = (float)(obuf[opos] * spliceincr); // do end-splice
  1133. }
  1134. sampsout++;
  1135. if(sampsout > goalsamps) {
  1136. done = 0; // Reached required duration, break from "while" loop
  1137. break;
  1138. }
  1139. if(++opos >= dz->buflen) {
  1140. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1141. return(exit_status);
  1142. opos = 0;
  1143. }
  1144. }
  1145. if(done) // Reached required duration, break from "for" loop
  1146. break;
  1147. } // If "done", breaks from OUTER while loop
  1148. }
  1149. }
  1150. if(opos) {
  1151. if((exit_status = write_samps(obuf,opos,dz))<0)
  1152. return(exit_status);
  1153. }
  1154. return FINISHED;
  1155. }
  1156. /*************************** GET_INITIAL_PHASE **********************
  1157. *
  1158. * Finds initial phase of first waveset AFTER the offset.
  1159. *
  1160. */
  1161. int get_initial_phase(int *ipos, dataptr dz)
  1162. {
  1163. float *ibuf = dz->sampbuf[0];
  1164. int phase = 0;
  1165. if(ibuf[*ipos] == 0.0) {
  1166. phase = 0;
  1167. while(ibuf[*ipos]==0 && *ipos < dz->ssampsread)
  1168. (*ipos)++;
  1169. } else if(ibuf[*ipos] > 0.0) {
  1170. phase = 1;
  1171. while(ibuf[*ipos] > 0.0 && *ipos < dz->ssampsread)
  1172. (*ipos)++;
  1173. } else {
  1174. phase = -1;
  1175. while(ibuf[*ipos] < 0.0 && *ipos < dz->ssampsread)
  1176. (*ipos)++;
  1177. }
  1178. if(*ipos >= dz->ssampsread)
  1179. return 0;
  1180. switch(phase) {
  1181. case(0):
  1182. if(ibuf[*ipos] < 0)
  1183. phase = -1;
  1184. else
  1185. phase = 1;
  1186. default:
  1187. phase = -phase;
  1188. }
  1189. return phase;
  1190. }
  1191. /******************************* DISTORTT_PARAM_PREPROCESS *****************************/
  1192. int distortt_param_preprocess(dataptr dz)
  1193. {
  1194. dz->param[2] *= MS_TO_SECS;
  1195. if(dz->param[2] >= dz->duration) {
  1196. sprintf(errstr,"OFFSET (%.2lf secs) TOO LARGE FOR LENGTH OF INPUT SOUND (%.2lf secs).\n",dz->param[2],dz->duration);
  1197. return DATA_ERROR;
  1198. }
  1199. dz->iparam[2] = (int)ceil(dz->param[2] * (double)dz->infile->srate);
  1200. return FINISHED;
  1201. }
  1202. /******************************* CREATE_DISTORTT_SNDBUFS *****************************/
  1203. int create_distortt_sndbufs(dataptr dz)
  1204. {
  1205. int n;
  1206. int bigbufsize, secsize;
  1207. dz->buflen = min(dz->insams[0],4 * dz->infile->srate);
  1208. secsize = dz->buflen/F_SECSIZE;
  1209. if(secsize * F_SECSIZE != dz->buflen)
  1210. secsize++;
  1211. dz->buflen = secsize * SECSIZE;
  1212. bigbufsize = dz->buflen * dz->bufcnt;
  1213. if((dz->bigbuf = (float *)Malloc(bigbufsize * sizeof(float))) == NULL) {
  1214. sprintf(errstr, "INSUFFICIENT MEMORY to create sound buffers.\n");
  1215. return(MEMORY_ERROR);
  1216. }
  1217. dz->buflen = bigbufsize/dz->bufcnt; /* length of floats buffers */
  1218. for(n=0;n<dz->bufcnt;n++)
  1219. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1220. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1221. return(FINISHED);
  1222. }