distortt.c 40 KB

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