distshift.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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 = "7.0.0";
  28. //CDP LIB REPLACEMENTS
  29. static int check_distshift_param_validity_and_consistency(dataptr dz);
  30. static int setup_distshift_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_distshift_param_ranges_and_defaults(dataptr dz);
  34. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  35. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  36. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  37. static int establish_application(dataptr dz);
  38. static int initialise_vflags(dataptr dz);
  39. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  40. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  41. static int mark_parameter_types(dataptr dz,aplptr ap);
  42. static int assign_file_data_storage(int infilecnt,dataptr dz);
  43. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  44. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  45. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  46. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  47. static int count_wavesets(int *initial_phase,dataptr dz);
  48. static int store_wavesets(int initial_phase,dataptr dz);
  49. static int distort_shift(dataptr dz);
  50. static int distort_swap(dataptr dz);
  51. /**************************************** MAIN *********************************************/
  52. int main(int argc,char *argv[])
  53. {
  54. int exit_status;
  55. dataptr dz = NULL;
  56. char **cmdline;
  57. int cmdlinecnt;
  58. int n;
  59. aplptr ap;
  60. int is_launched = FALSE;
  61. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  62. fprintf(stdout,"%s\n",cdp_version);
  63. fflush(stdout);
  64. return 0;
  65. }
  66. /* CHECK FOR SOUNDLOOM */
  67. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  68. sloom = 0;
  69. sloombatch = 1;
  70. }
  71. if(sflinit("cdp")){
  72. sfperror("cdp: initialisation\n");
  73. return(FAILED);
  74. }
  75. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  76. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  77. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  78. return(FAILED);
  79. }
  80. if(!sloom) {
  81. if(argc == 1) {
  82. usage1();
  83. return(FAILED);
  84. } else if(argc == 2) {
  85. usage2(argv[1]);
  86. return(FAILED);
  87. }
  88. }
  89. if(!sloom) {
  90. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  91. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  92. return(FAILED);
  93. }
  94. cmdline = argv;
  95. cmdlinecnt = argc;
  96. if((get_the_process_no(argv[0],dz))<0)
  97. return(FAILED);
  98. cmdline++;
  99. cmdlinecnt--;
  100. dz->maxmode = 2;
  101. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  102. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  103. return(exit_status);
  104. }
  105. cmdline++;
  106. cmdlinecnt--;
  107. // setup_particular_application =
  108. if((exit_status = setup_distshift_application(dz))<0) {
  109. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  110. return(FAILED);
  111. }
  112. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  113. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  114. return(FAILED);
  115. }
  116. } else {
  117. //parse_TK_data() =
  118. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  119. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  120. return(exit_status);
  121. }
  122. }
  123. ap = dz->application;
  124. // parse_infile_and_hone_type() =
  125. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  126. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  127. return(FAILED);
  128. }
  129. // setup_param_ranges_and_defaults() =
  130. if((exit_status = setup_distshift_param_ranges_and_defaults(dz))<0) {
  131. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  132. return(FAILED);
  133. }
  134. // open_first_infile CDP LIB
  135. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  136. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  137. return(FAILED);
  138. }
  139. cmdlinecnt--;
  140. cmdline++;
  141. // handle_extra_infiles() : redundant
  142. // handle_outfile() =
  143. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  144. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  145. return(FAILED);
  146. }
  147. // handle_formants() redundant
  148. // handle_formant_quiksearch() redundant
  149. // handle_special_data() redundant
  150. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  151. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  152. return(FAILED);
  153. }
  154. is_launched = TRUE;
  155. dz->bufcnt = 1;
  156. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  157. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  158. return(MEMORY_ERROR);
  159. }
  160. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  161. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  162. return(MEMORY_ERROR);
  163. }
  164. for(n = 0;n <dz->bufcnt; n++)
  165. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  166. dz->sampbuf[n] = (float *)0;
  167. if((exit_status = create_sndbufs(dz))<0) { // CDP LIB
  168. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  169. return(FAILED);
  170. }
  171. // check_param_validity_and_consistency....
  172. if((exit_status = check_distshift_param_validity_and_consistency(dz))<0) {
  173. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. //param_preprocess() redundant
  177. //spec_process_file =
  178. switch(dz->mode) {
  179. case(0): exit_status = distort_shift(dz); break;
  180. case(1): exit_status = distort_swap(dz); break;
  181. }
  182. if(exit_status < 0) {
  183. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  184. return(FAILED);
  185. }
  186. if((exit_status = complete_output(dz))<0) { // CDP LIB
  187. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  188. return(FAILED);
  189. }
  190. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  191. free(dz);
  192. return(SUCCEEDED);
  193. }
  194. /**********************************************
  195. REPLACED CDP LIB FUNCTIONS
  196. **********************************************/
  197. /****************************** SET_PARAM_DATA *********************************/
  198. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  199. {
  200. ap->special_data = (char)special_data;
  201. ap->param_cnt = (char)paramcnt;
  202. ap->max_param_cnt = (char)maxparamcnt;
  203. if(ap->max_param_cnt>0) {
  204. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  205. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  206. return(MEMORY_ERROR);
  207. }
  208. strcpy(ap->param_list,paramlist);
  209. }
  210. return(FINISHED);
  211. }
  212. /****************************** SET_VFLGS *********************************/
  213. int set_vflgs
  214. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  215. {
  216. ap->option_cnt = (char) optcnt; /*RWD added cast */
  217. if(optcnt) {
  218. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  219. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  220. return(MEMORY_ERROR);
  221. }
  222. strcpy(ap->option_list,optlist);
  223. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  224. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  225. return(MEMORY_ERROR);
  226. }
  227. strcpy(ap->option_flags,optflags);
  228. }
  229. ap->vflag_cnt = (char) vflagcnt;
  230. ap->variant_param_cnt = (char) vparamcnt;
  231. if(vflagcnt) {
  232. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  233. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  234. return(MEMORY_ERROR);
  235. }
  236. strcpy(ap->variant_list,varlist);
  237. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  238. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  239. return(MEMORY_ERROR);
  240. }
  241. strcpy(ap->variant_flags,varflags);
  242. }
  243. return(FINISHED);
  244. }
  245. /***************************** APPLICATION_INIT **************************/
  246. int application_init(dataptr dz)
  247. {
  248. int exit_status;
  249. int storage_cnt;
  250. int tipc, brkcnt;
  251. aplptr ap = dz->application;
  252. if(ap->vflag_cnt>0)
  253. initialise_vflags(dz);
  254. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  255. ap->total_input_param_cnt = (char)tipc;
  256. if(tipc>0) {
  257. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  258. return(exit_status);
  259. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  260. return(exit_status);
  261. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  262. return(exit_status);
  263. }
  264. brkcnt = tipc;
  265. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  266. if(brkcnt>0) {
  267. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  268. return(exit_status);
  269. }
  270. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  271. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  272. return(exit_status);
  273. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  274. return(exit_status);
  275. }
  276. if((exit_status = mark_parameter_types(dz,ap))<0)
  277. return(exit_status);
  278. // establish_infile_constants() replaced by
  279. dz->infilecnt = 1;
  280. //establish_bufptrs_and_extra_buffers():
  281. return(FINISHED);
  282. }
  283. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  284. /* RWD mallo changed to calloc; helps debug verison run as release! */
  285. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  286. {
  287. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  288. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  289. return(MEMORY_ERROR);
  290. }
  291. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  292. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  293. return(MEMORY_ERROR);
  294. }
  295. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  296. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  297. return(MEMORY_ERROR);
  298. }
  299. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  300. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  301. return(MEMORY_ERROR);
  302. }
  303. return(FINISHED);
  304. }
  305. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  306. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  307. {
  308. int n;
  309. for(n=0;n<storage_cnt;n++) {
  310. dz->is_int[n] = (char)0;
  311. dz->no_brk[n] = (char)0;
  312. }
  313. return(FINISHED);
  314. }
  315. /***************************** MARK_PARAMETER_TYPES **************************/
  316. int mark_parameter_types(dataptr dz,aplptr ap)
  317. {
  318. int n, m; /* PARAMS */
  319. for(n=0;n<ap->max_param_cnt;n++) {
  320. switch(ap->param_list[n]) {
  321. case('0'): break; /* dz->is_active[n] = 0 is default */
  322. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  323. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  324. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  325. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  326. default:
  327. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  328. return(PROGRAM_ERROR);
  329. }
  330. } /* OPTIONS */
  331. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  332. switch(ap->option_list[n]) {
  333. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  334. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  335. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  336. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  337. default:
  338. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  339. return(PROGRAM_ERROR);
  340. }
  341. } /* VARIANTS */
  342. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  343. switch(ap->variant_list[n]) {
  344. case('0'): break;
  345. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  346. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  347. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  348. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  349. default:
  350. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  351. return(PROGRAM_ERROR);
  352. }
  353. } /* INTERNAL */
  354. for(n=0,
  355. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  356. switch(ap->internal_param_list[n]) {
  357. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  358. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  359. case('d'): dz->no_brk[m] = (char)1; break;
  360. default:
  361. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  362. return(PROGRAM_ERROR);
  363. }
  364. }
  365. return(FINISHED);
  366. }
  367. /************************ HANDLE_THE_OUTFILE *********************/
  368. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  369. {
  370. int exit_status;
  371. char *filename = (*cmdline)[0];
  372. if(filename[0]=='-' && filename[1]=='f') {
  373. dz->floatsam_output = 1;
  374. dz->true_outfile_stype = SAMP_FLOAT;
  375. filename+= 2;
  376. }
  377. if(!sloom) {
  378. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  379. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  380. return(DATA_ERROR);
  381. }
  382. }
  383. strcpy(dz->outfilename,filename);
  384. if((exit_status = create_sized_outfile(filename,dz))<0)
  385. return(exit_status);
  386. (*cmdline)++;
  387. (*cmdlinecnt)--;
  388. return(FINISHED);
  389. }
  390. /***************************** ESTABLISH_APPLICATION **************************/
  391. int establish_application(dataptr dz)
  392. {
  393. aplptr ap;
  394. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  395. sprintf(errstr,"establish_application()\n");
  396. return(MEMORY_ERROR);
  397. }
  398. ap = dz->application;
  399. memset((char *)ap,0,sizeof(struct applic));
  400. return(FINISHED);
  401. }
  402. /************************* INITIALISE_VFLAGS *************************/
  403. int initialise_vflags(dataptr dz)
  404. {
  405. int n;
  406. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  407. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  408. return(MEMORY_ERROR);
  409. }
  410. for(n=0;n<dz->application->vflag_cnt;n++)
  411. dz->vflag[n] = FALSE;
  412. return FINISHED;
  413. }
  414. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  415. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  416. {
  417. int n;
  418. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  419. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  420. return(MEMORY_ERROR);
  421. }
  422. for(n=0;n<tipc;n++)
  423. ap->default_val[n] = 0.0;
  424. return(FINISHED);
  425. }
  426. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  427. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  428. {
  429. int n;
  430. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  431. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  432. return(MEMORY_ERROR);
  433. }
  434. for(n=0;n<tipc;n++)
  435. dz->is_active[n] = (char)0;
  436. return(FINISHED);
  437. }
  438. /************************* SETUP_DISTSHIFT_APPLICATION *******************/
  439. int setup_distshift_application(dataptr dz)
  440. {
  441. int exit_status;
  442. aplptr ap;
  443. if((exit_status = establish_application(dz))<0) // GLOBAL
  444. return(FAILED);
  445. ap = dz->application;
  446. // SEE parstruct FOR EXPLANATION of next 2 functions
  447. if(dz->mode==0) exit_status = set_param_data(ap,0 ,2,2,"ii");
  448. else exit_status = set_param_data(ap,0 ,2,1,"i0");
  449. if(exit_status < 0)
  450. return(FAILED);
  451. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  452. return(FAILED);
  453. // set_legal_infile_structure -->
  454. dz->has_otherfile = FALSE;
  455. // assign_process_logic -->
  456. dz->input_data_type = SNDFILES_ONLY;
  457. dz->process_type = UNEQUAL_SNDFILE;
  458. dz->outfiletype = SNDFILE_OUT;
  459. return application_init(dz); //GLOBAL
  460. }
  461. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  462. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  463. {
  464. int exit_status;
  465. infileptr infile_info;
  466. if(!sloom) {
  467. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  468. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  469. return(MEMORY_ERROR);
  470. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  471. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  472. return(PROGRAM_ERROR);
  473. } else if(infile_info->filetype != SNDFILE) {
  474. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  475. return(DATA_ERROR);
  476. } else if(infile_info->channels != 1) {
  477. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  478. return(DATA_ERROR);
  479. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  480. sprintf(errstr,"Failed to copy file parsing information\n");
  481. return(PROGRAM_ERROR);
  482. }
  483. free(infile_info);
  484. }
  485. return(FINISHED);
  486. }
  487. /************************* SETUP_DISTSHIFT_PARAM_RANGES_AND_DEFAULTS *******************/
  488. int setup_distshift_param_ranges_and_defaults(dataptr dz)
  489. {
  490. int exit_status;
  491. aplptr ap = dz->application;
  492. // set_param_ranges()
  493. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  494. // NB total_input_param_cnt is > 0 !!!
  495. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  496. return(FAILED);
  497. // get_param_ranges()
  498. ap->lo[0] = 1;
  499. ap->hi[0] = 32767;
  500. ap->default_val[0] = 1;
  501. if(dz->mode==0) {
  502. ap->lo[1] = 1;
  503. ap->hi[1] = 32767;
  504. ap->default_val[1] = 1;
  505. }
  506. dz->maxmode = 2;
  507. if(!sloom)
  508. put_default_vals_in_all_params(dz);
  509. return(FINISHED);
  510. }
  511. /********************************* PARSE_SLOOM_DATA *********************************/
  512. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  513. {
  514. int exit_status;
  515. int cnt = 1, infilecnt;
  516. int filesize, insams, inbrksize;
  517. double dummy;
  518. int true_cnt = 0;
  519. aplptr ap;
  520. while(cnt<=PRE_CMDLINE_DATACNT) {
  521. if(cnt > argc) {
  522. sprintf(errstr,"Insufficient data sent from TK\n");
  523. return(DATA_ERROR);
  524. }
  525. switch(cnt) {
  526. case(1):
  527. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  528. sprintf(errstr,"Cannot read process no. sent from TK\n");
  529. return(DATA_ERROR);
  530. }
  531. break;
  532. case(2):
  533. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  534. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  535. return(DATA_ERROR);
  536. }
  537. if(dz->mode > 0)
  538. dz->mode--;
  539. //setup_particular_application() =
  540. if((exit_status = setup_distshift_application(dz))<0)
  541. return(exit_status);
  542. ap = dz->application;
  543. break;
  544. case(3):
  545. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  546. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  547. return(DATA_ERROR);
  548. }
  549. if(infilecnt < 1) {
  550. true_cnt = cnt + 1;
  551. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  552. }
  553. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  554. return(exit_status);
  555. break;
  556. case(INPUT_FILETYPE+4):
  557. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  558. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  559. return(DATA_ERROR);
  560. }
  561. break;
  562. case(INPUT_FILESIZE+4):
  563. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  564. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  565. return(DATA_ERROR);
  566. }
  567. dz->insams[0] = filesize;
  568. break;
  569. case(INPUT_INSAMS+4):
  570. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  571. sprintf(errstr,"Cannot read insams sent from TK\n");
  572. return(DATA_ERROR);
  573. }
  574. dz->insams[0] = insams;
  575. break;
  576. case(INPUT_SRATE+4):
  577. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  578. sprintf(errstr,"Cannot read srate sent from TK\n");
  579. return(DATA_ERROR);
  580. }
  581. break;
  582. case(INPUT_CHANNELS+4):
  583. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  584. sprintf(errstr,"Cannot read channels sent from TK\n");
  585. return(DATA_ERROR);
  586. }
  587. break;
  588. case(INPUT_STYPE+4):
  589. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  590. sprintf(errstr,"Cannot read stype sent from TK\n");
  591. return(DATA_ERROR);
  592. }
  593. break;
  594. case(INPUT_ORIGSTYPE+4):
  595. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  596. sprintf(errstr,"Cannot read origstype sent from TK\n");
  597. return(DATA_ERROR);
  598. }
  599. break;
  600. case(INPUT_ORIGRATE+4):
  601. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  602. sprintf(errstr,"Cannot read origrate sent from TK\n");
  603. return(DATA_ERROR);
  604. }
  605. break;
  606. case(INPUT_MLEN+4):
  607. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  608. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  609. return(DATA_ERROR);
  610. }
  611. break;
  612. case(INPUT_DFAC+4):
  613. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  614. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  615. return(DATA_ERROR);
  616. }
  617. break;
  618. case(INPUT_ORIGCHANS+4):
  619. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  620. sprintf(errstr,"Cannot read origchans sent from TK\n");
  621. return(DATA_ERROR);
  622. }
  623. break;
  624. case(INPUT_SPECENVCNT+4):
  625. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  626. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  627. return(DATA_ERROR);
  628. }
  629. dz->specenvcnt = dz->infile->specenvcnt;
  630. break;
  631. case(INPUT_WANTED+4):
  632. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  633. sprintf(errstr,"Cannot read wanted sent from TK\n");
  634. return(DATA_ERROR);
  635. }
  636. break;
  637. case(INPUT_WLENGTH+4):
  638. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  639. sprintf(errstr,"Cannot read wlength sent from TK\n");
  640. return(DATA_ERROR);
  641. }
  642. break;
  643. case(INPUT_OUT_CHANS+4):
  644. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  645. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  646. return(DATA_ERROR);
  647. }
  648. break;
  649. /* RWD these chanegs to samps - tk will have to deal with that! */
  650. case(INPUT_DESCRIPTOR_BYTES+4):
  651. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  652. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  653. return(DATA_ERROR);
  654. }
  655. break;
  656. case(INPUT_IS_TRANSPOS+4):
  657. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  658. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  659. return(DATA_ERROR);
  660. }
  661. break;
  662. case(INPUT_COULD_BE_TRANSPOS+4):
  663. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  664. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  665. return(DATA_ERROR);
  666. }
  667. break;
  668. case(INPUT_COULD_BE_PITCH+4):
  669. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  670. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  671. return(DATA_ERROR);
  672. }
  673. break;
  674. case(INPUT_DIFFERENT_SRATES+4):
  675. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  676. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  677. return(DATA_ERROR);
  678. }
  679. break;
  680. case(INPUT_DUPLICATE_SNDS+4):
  681. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  682. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  683. return(DATA_ERROR);
  684. }
  685. break;
  686. case(INPUT_BRKSIZE+4):
  687. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  688. sprintf(errstr,"Cannot read brksize sent from TK\n");
  689. return(DATA_ERROR);
  690. }
  691. if(inbrksize > 0) {
  692. switch(dz->input_data_type) {
  693. case(WORDLIST_ONLY):
  694. break;
  695. case(PITCH_AND_PITCH):
  696. case(PITCH_AND_TRANSPOS):
  697. case(TRANSPOS_AND_TRANSPOS):
  698. dz->tempsize = inbrksize;
  699. break;
  700. case(BRKFILES_ONLY):
  701. case(UNRANGED_BRKFILE_ONLY):
  702. case(DB_BRKFILES_ONLY):
  703. case(ALL_FILES):
  704. case(ANY_NUMBER_OF_ANY_FILES):
  705. if(dz->extrabrkno < 0) {
  706. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  707. return(DATA_ERROR);
  708. }
  709. if(dz->brksize == NULL) {
  710. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  711. return(PROGRAM_ERROR);
  712. }
  713. dz->brksize[dz->extrabrkno] = inbrksize;
  714. break;
  715. default:
  716. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  717. dz->input_data_type);
  718. return(PROGRAM_ERROR);
  719. }
  720. break;
  721. }
  722. break;
  723. case(INPUT_NUMSIZE+4):
  724. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  725. sprintf(errstr,"Cannot read numsize sent from TK\n");
  726. return(DATA_ERROR);
  727. }
  728. break;
  729. case(INPUT_LINECNT+4):
  730. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  731. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  732. return(DATA_ERROR);
  733. }
  734. break;
  735. case(INPUT_ALL_WORDS+4):
  736. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  737. sprintf(errstr,"Cannot read all_words sent from TK\n");
  738. return(DATA_ERROR);
  739. }
  740. break;
  741. case(INPUT_ARATE+4):
  742. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  743. sprintf(errstr,"Cannot read arate sent from TK\n");
  744. return(DATA_ERROR);
  745. }
  746. break;
  747. case(INPUT_FRAMETIME+4):
  748. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  749. sprintf(errstr,"Cannot read frametime sent from TK\n");
  750. return(DATA_ERROR);
  751. }
  752. dz->frametime = (float)dummy;
  753. break;
  754. case(INPUT_WINDOW_SIZE+4):
  755. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  756. sprintf(errstr,"Cannot read window_size sent from TK\n");
  757. return(DATA_ERROR);
  758. }
  759. break;
  760. case(INPUT_NYQUIST+4):
  761. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  762. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  763. return(DATA_ERROR);
  764. }
  765. break;
  766. case(INPUT_DURATION+4):
  767. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  768. sprintf(errstr,"Cannot read duration sent from TK\n");
  769. return(DATA_ERROR);
  770. }
  771. break;
  772. case(INPUT_MINBRK+4):
  773. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  774. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  775. return(DATA_ERROR);
  776. }
  777. break;
  778. case(INPUT_MAXBRK+4):
  779. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  780. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  781. return(DATA_ERROR);
  782. }
  783. break;
  784. case(INPUT_MINNUM+4):
  785. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  786. sprintf(errstr,"Cannot read minnum sent from TK\n");
  787. return(DATA_ERROR);
  788. }
  789. break;
  790. case(INPUT_MAXNUM+4):
  791. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  792. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  793. return(DATA_ERROR);
  794. }
  795. break;
  796. default:
  797. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  798. return(PROGRAM_ERROR);
  799. }
  800. cnt++;
  801. }
  802. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  803. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  804. return(DATA_ERROR);
  805. }
  806. if(true_cnt)
  807. cnt = true_cnt;
  808. *cmdlinecnt = 0;
  809. while(cnt < argc) {
  810. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  811. return(exit_status);
  812. cnt++;
  813. }
  814. return(FINISHED);
  815. }
  816. /********************************* GET_TK_CMDLINE_WORD *********************************/
  817. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  818. {
  819. if(*cmdlinecnt==0) {
  820. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  821. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  822. return(MEMORY_ERROR);
  823. }
  824. } else {
  825. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  826. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  827. return(MEMORY_ERROR);
  828. }
  829. }
  830. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  831. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  832. return(MEMORY_ERROR);
  833. }
  834. strcpy((*cmdline)[*cmdlinecnt],q);
  835. (*cmdlinecnt)++;
  836. return(FINISHED);
  837. }
  838. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  839. int assign_file_data_storage(int infilecnt,dataptr dz)
  840. {
  841. int exit_status;
  842. int no_sndfile_system_files = FALSE;
  843. dz->infilecnt = infilecnt;
  844. if((exit_status = allocate_filespace(dz))<0)
  845. return(exit_status);
  846. if(no_sndfile_system_files)
  847. dz->infilecnt = 0;
  848. return(FINISHED);
  849. }
  850. /************************* redundant functions: to ensure libs compile OK *******************/
  851. int assign_process_logic(dataptr dz)
  852. {
  853. return(FINISHED);
  854. }
  855. void set_legal_infile_structure(dataptr dz)
  856. {}
  857. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  858. {
  859. return(FINISHED);
  860. }
  861. int setup_internal_arrays_and_array_pointers(dataptr dz)
  862. {
  863. return(FINISHED);
  864. }
  865. int establish_bufptrs_and_extra_buffers(dataptr dz)
  866. {
  867. return(FINISHED);
  868. }
  869. int read_special_data(char *str,dataptr dz)
  870. {
  871. return(FINISHED);
  872. }
  873. int inner_loop
  874. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  875. {
  876. return(FINISHED);
  877. }
  878. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  879. {
  880. return(FINISHED);
  881. }
  882. /******************************** USAGE1 ********************************/
  883. int usage1(void)
  884. {
  885. usage2("distshift");
  886. return(USAGE_ONLY);
  887. }
  888. /**************************** CHECK_DISTSHIFT_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  889. int check_distshift_param_validity_and_consistency(dataptr dz)
  890. {
  891. int exit_status;
  892. int wscnt = 0;
  893. int initial_phase = 0;
  894. if((exit_status = count_wavesets(&initial_phase,dz))<0) // returns no. of half-wavesets as dz->itemcnt
  895. return exit_status ;
  896. if(dz->iparam[0] >= dz->itemcnt) {
  897. sprintf(errstr,"Insufficient wavesets in source (%d) to make group of %d\n",wscnt,dz->iparam[0]);
  898. return(DATA_ERROR);
  899. }
  900. if(dz->mode == 0) {
  901. if(dz->iparam[1] >= dz->itemcnt) {
  902. sprintf(errstr,"Insufficient wavesets in source (%d) to make shift of %d\n",wscnt,dz->iparam[0]);
  903. return(DATA_ERROR);
  904. }
  905. }
  906. if((exit_status = store_wavesets(initial_phase,dz))<0)
  907. return exit_status;
  908. return FINISHED;
  909. }
  910. /********************************************************************************************/
  911. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  912. {
  913. if(!strcmp(prog_identifier_from_cmdline,"distshift")) dz->process = DISTSHIFT;
  914. else {
  915. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  916. return(USAGE_ONLY);
  917. }
  918. return(FINISHED);
  919. }
  920. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  921. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  922. {
  923. int n;
  924. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  925. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  926. return(MEMORY_ERROR);
  927. }
  928. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  929. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  930. return(MEMORY_ERROR);
  931. }
  932. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  933. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  934. return(MEMORY_ERROR);
  935. }
  936. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  937. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  938. return(MEMORY_ERROR);
  939. }
  940. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  941. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  942. return(MEMORY_ERROR);
  943. }
  944. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  945. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  946. return(MEMORY_ERROR);
  947. }
  948. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  949. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  950. return(MEMORY_ERROR);
  951. }
  952. for(n=0;n<brkcnt;n++) {
  953. dz->brk[n] = NULL;
  954. dz->brkptr[n] = NULL;
  955. dz->brkinit[n] = 0;
  956. dz->brksize[n] = 0;
  957. }
  958. return(FINISHED);
  959. }
  960. /******************************** USAGE2 ********************************/
  961. int usage2(char *str)
  962. {
  963. if(!strcmp(str,"distshift")) {
  964. fprintf(stderr,
  965. "USAGE:\n"
  966. "distshift distshift 1 infile outfile grpcnt shift\n"
  967. "distshift distshift 2 infile outfile grpcnt\n"
  968. "\n"
  969. "Mode 1: Shift alternate (groups of) half-wavecycles forward in time\n"
  970. " (wrapping back around zero-time if pushed beyond sound end).\n"
  971. "Mode 2: Swap alternate half-wavecycle(group)s.\n"
  972. "\n"
  973. "GRPCNT Size of elements to operate on.\n"
  974. " 1 = single half-wavesets\n"
  975. " 2 = 1 waveset + single half-waveset.\n"
  976. " 3 = 2 wavesets + single half-waveset.\n"
  977. " etc.\n"
  978. "SHIFT Move alternate groups forward by \"shift\" waveset(group)s.\n"
  979. "\n");
  980. } else
  981. fprintf(stdout,"Unknown option '%s'\n",str);
  982. return(USAGE_ONLY);
  983. }
  984. int usage3(char *str1,char *str2)
  985. {
  986. fprintf(stderr,"Insufficient parameters on command line.\n");
  987. return(USAGE_ONLY);
  988. }
  989. /****************************** COUNT_WAVESETS *********************************/
  990. int count_wavesets(int *initial_phase,dataptr dz)
  991. {
  992. int exit_status;
  993. int n, cnt = 0;
  994. float *ibuf = dz->sampbuf[0];
  995. int halfwaveset_cnt = 0;
  996. int phase = 0;
  997. int *wavesetsampcnt;
  998. dz->samps_left = dz->insams[0];
  999. // DETERMINE INITIAL PHASE
  1000. while(dz->samps_left > 0) {
  1001. if((exit_status = read_samps(ibuf,dz))<0)
  1002. return(exit_status);
  1003. n = 0;
  1004. while(n < dz->ssampsread) {
  1005. if(ibuf[n] > 0.0) {
  1006. *initial_phase = 1;
  1007. break;
  1008. } else if(ibuf[n] < 0.0) {
  1009. *initial_phase = -1;
  1010. break;
  1011. }
  1012. n++;
  1013. }
  1014. if(*initial_phase != 0)
  1015. break;
  1016. }
  1017. if(*initial_phase == 0) {
  1018. fprintf(stderr,"No signal found in soundfile: Cannot proceed\n");
  1019. return DATA_ERROR;
  1020. }
  1021. phase = *initial_phase;
  1022. if((sndseekEx(dz->ifd[0],0,0) < 0)){
  1023. sprintf(errstr,"sndseek failed\n");
  1024. return SYSTEM_ERROR;
  1025. }
  1026. dz->samps_left = dz->insams[0];
  1027. dz->total_samps_read = 0;
  1028. while(dz->samps_left > 0) {
  1029. if((exit_status = read_samps(ibuf,dz))<0)
  1030. return(exit_status);
  1031. n = 0;
  1032. while(n < dz->ssampsread) {
  1033. switch(*initial_phase) {
  1034. case(1):
  1035. if(phase == *initial_phase) {
  1036. if(ibuf[n] < 0.0) { // i.e. phase has changed from 1 (initial_phase) to -1
  1037. halfwaveset_cnt++; // Count half-waveset
  1038. phase = -phase; // Switch phase
  1039. }
  1040. } else {
  1041. if(ibuf[n] >= 0.0) { // i.e. phase has changed from -1 to 1
  1042. halfwaveset_cnt++; // Count half-waveset
  1043. phase = -phase; // Switch phase
  1044. }
  1045. }
  1046. break;
  1047. case(-1):
  1048. if(phase == *initial_phase) {
  1049. if(ibuf[n] >= 0.0) { // i.e. phase has changed from -1 (initial_phase) to 1
  1050. halfwaveset_cnt++; // Count half-waveset
  1051. phase = -phase; // Switch phase
  1052. }
  1053. } else {
  1054. if(ibuf[n] < 0.0) { // i.e. phase has changed from 1 to -1
  1055. halfwaveset_cnt++; // Count half-waveset
  1056. phase = -phase; // Switch phase
  1057. }
  1058. }
  1059. break;
  1060. }
  1061. n++;
  1062. }
  1063. }
  1064. if(halfwaveset_cnt < 2) {
  1065. sprintf(errstr,"Failed to find any half-wavesets.\n");
  1066. return DATA_ERROR;
  1067. }
  1068. dz->itemcnt = halfwaveset_cnt;
  1069. if((sndseekEx(dz->ifd[0],0,0) < 0)){
  1070. sprintf(errstr,"sndseek failed\n");
  1071. return SYSTEM_ERROR;
  1072. }
  1073. dz->samps_left = dz->insams[0];
  1074. dz->total_samps_read = 0;
  1075. dz->total_samps_written = 0;
  1076. // establish array to store waveset sizes
  1077. if((dz->iparray = (int **)malloc(sizeof(int *)))==NULL) {
  1078. sprintf(errstr,"INSUFFICIENT MEMORY for waveset samplecnts storage.\n");
  1079. return(MEMORY_ERROR);
  1080. }
  1081. if((dz->iparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1082. sprintf(errstr,"INSUFFICIENT MEMORY for waveset samplecnts storage.\n");
  1083. return(MEMORY_ERROR);
  1084. }
  1085. wavesetsampcnt = dz->iparray[0];
  1086. halfwaveset_cnt = 0;
  1087. phase = *initial_phase;
  1088. while(dz->samps_left > 0) {
  1089. if((exit_status = read_samps(ibuf,dz))<0)
  1090. return(exit_status);
  1091. n = 0;
  1092. while(n < dz->ssampsread) {
  1093. switch(*initial_phase) {
  1094. case(1):
  1095. if(phase == *initial_phase) {
  1096. if(ibuf[n] >= 0.0)
  1097. cnt++;
  1098. else { // i.e. phase has changed from 1 (initial_phase) to -1
  1099. wavesetsampcnt[halfwaveset_cnt] = cnt;
  1100. cnt = 1;
  1101. halfwaveset_cnt++;
  1102. phase = -phase; // Switch phase
  1103. }
  1104. } else {
  1105. if(ibuf[n] < 0.0)
  1106. cnt++;
  1107. else { // i.e. phase has changed from -1 to 1
  1108. wavesetsampcnt[halfwaveset_cnt] = cnt;
  1109. cnt = 1;
  1110. halfwaveset_cnt++;
  1111. phase = -phase; // Switch phase
  1112. }
  1113. }
  1114. break;
  1115. case(-1):
  1116. if(phase == *initial_phase) {
  1117. if(ibuf[n] < 0.0)
  1118. cnt++;
  1119. else { // i.e. phase has changed from -1 (initial_phase) to 1
  1120. wavesetsampcnt[halfwaveset_cnt] = cnt;
  1121. cnt = 1;
  1122. halfwaveset_cnt++;
  1123. phase = -phase; // Switch phase
  1124. }
  1125. } else {
  1126. if(ibuf[n] >= 0.0)
  1127. cnt++;
  1128. else { // i.e. phase has changed from 1 to -1
  1129. wavesetsampcnt[halfwaveset_cnt] = cnt;
  1130. cnt = 1;
  1131. halfwaveset_cnt++;
  1132. phase = -phase; // Switch phase
  1133. }
  1134. }
  1135. break;
  1136. }
  1137. n++;
  1138. }
  1139. }
  1140. if((sndseekEx(dz->ifd[0],0,0) < 0)){
  1141. sprintf(errstr,"sndseek failed\n");
  1142. return SYSTEM_ERROR;
  1143. }
  1144. if((dz->fptr = (float **)malloc(dz->itemcnt * sizeof(float *)))==NULL) {
  1145. sprintf(errstr,"INSUFFICIENT MEMORY for waveset stores.\n");
  1146. return(MEMORY_ERROR);
  1147. }
  1148. for(n = 0;n < dz->itemcnt;n++) {
  1149. if((dz->fptr[n] = (float *)malloc(wavesetsampcnt[n] * sizeof(float)))==NULL) {
  1150. sprintf(errstr,"INSUFFICIENT MEMORY for waveset store %d.\n",n+1);
  1151. return(MEMORY_ERROR);
  1152. }
  1153. }
  1154. dz->samps_left = dz->insams[0];
  1155. dz->total_samps_read = 0;
  1156. dz->total_samps_written = 0;
  1157. return FINISHED;
  1158. }
  1159. /****************************** STORE_WAVESETS *********************************/
  1160. int store_wavesets(int initial_phase,dataptr dz)
  1161. {
  1162. int exit_status, phase, done = 0;
  1163. float *ibuf = dz->sampbuf[0];
  1164. int cnt = 0, n, thisarray = 0;
  1165. float **waveset = dz->fptr;
  1166. dz->samps_left = dz->insams[0];
  1167. phase = initial_phase;
  1168. while(dz->samps_left > 0) {
  1169. if((exit_status = read_samps(ibuf,dz))<0)
  1170. return(exit_status);
  1171. n = 0;
  1172. while(n < dz->ssampsread) {
  1173. switch(initial_phase) {
  1174. case(1):
  1175. if(phase == initial_phase) {
  1176. if(ibuf[n] >= 0.0)
  1177. waveset[thisarray][cnt++] = ibuf[n];
  1178. else {
  1179. if(++thisarray >= dz->itemcnt) // Step to next array store
  1180. done = 1; // Ignoring any incomplete half-cycle at end
  1181. cnt = 0; // Reset waveset-size counter to 0.
  1182. phase = -phase; // Switch phase
  1183. }
  1184. } else {
  1185. if(ibuf[n] < 0.0)
  1186. waveset[thisarray][cnt++] = ibuf[n];
  1187. else {
  1188. if(++thisarray >= dz->itemcnt) // Step to next array store
  1189. done = 1; // Ignoring any incomplete half-cycle at end
  1190. cnt = 0; // Reset waveset-size counter to 0.
  1191. phase = -phase; // Switch phase
  1192. }
  1193. }
  1194. break;
  1195. case(-1):
  1196. if(phase == initial_phase) {
  1197. if(ibuf[n] < 0.0)
  1198. waveset[thisarray][cnt++] = ibuf[n];
  1199. else {
  1200. if(++thisarray >= dz->itemcnt) // Step to next array store
  1201. done = 1; // Ignoring any incomplete half-cycle at end
  1202. cnt = 0; // Reset waveset-size counter to 0.
  1203. phase = -phase; // Switch phase
  1204. }
  1205. } else {
  1206. if(ibuf[n] >= 0)
  1207. waveset[thisarray][cnt++] = ibuf[n];
  1208. else {
  1209. if(++thisarray >= dz->itemcnt) // Step to next array store
  1210. done = 1; // Ignoring any incomplete half-cycle at end
  1211. cnt = 0; // Reset waveset-size counter to 0.
  1212. phase = -phase; // Switch phase
  1213. }
  1214. }
  1215. break;
  1216. }
  1217. if(done)
  1218. break;
  1219. n++;
  1220. }
  1221. if(done)
  1222. break;
  1223. }
  1224. if((sndseekEx(dz->ifd[0],0,0) < 0)){
  1225. sprintf(errstr,"sndseek failed\n");
  1226. return SYSTEM_ERROR;
  1227. }
  1228. dz->samps_left = dz->insams[0];
  1229. dz->total_samps_read = 0;
  1230. dz->total_samps_written = 0;
  1231. return FINISHED;
  1232. }
  1233. /****************************** DISTORT_SHIFT *********************************/
  1234. int distort_shift(dataptr dz)
  1235. {
  1236. int exit_status;
  1237. float *obuf = dz->sampbuf[0];
  1238. int bufpos = 0, k, kend, j, jend, n, gpsize, shift, thisarray, thatarray;
  1239. int *wavesetsampcnt = dz->iparray[0];
  1240. float **waveset = dz->fptr;
  1241. gpsize = (dz->iparam[0] * 2) - 1; // i.e. param 1 2 3 4
  1242. // whole cycles in group 0 1 2 3
  1243. // = halfcycles in group 1 2 4 6
  1244. // + required halfcycle 1 1 1 1
  1245. // gives gpsize 1 3 5 7
  1246. shift = 2 * dz->iparam[1]; // i.e. for single-halfcycle groups, moving an up-phase halfcycle
  1247. // move up-phase halfcycle back to before previous dn-phase halfcycle (2 half-cycle steps)
  1248. // and do this dz->param[1] times
  1249. shift *= gpsize; // but if the groups are size "gpsize", upscale this
  1250. for(k=0;k < dz->itemcnt; k+= gpsize * 2) { // For first of each PAIR of gps, just read it to output
  1251. kend = min(k + gpsize,dz->itemcnt); // End of group to copy (not beyond end of all wavesets).
  1252. j = (k+gpsize - shift); // For 2nd of each pair, search to the offset required then read
  1253. while(j < 0)
  1254. j += dz->itemcnt;
  1255. jend = j + gpsize; // Possible overflow checked later
  1256. for(thisarray = k; thisarray < kend; thisarray++) { // For all half-wavesets in (non-moved) group,
  1257. for(n=0;n<wavesetsampcnt[thisarray];n++) { // For every sample in waveset,
  1258. obuf[bufpos++] = waveset[thisarray][n]; // Copy to output
  1259. if(bufpos >= dz->buflen) {
  1260. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1261. return(exit_status);
  1262. bufpos = 0;
  1263. }
  1264. }
  1265. } // For second of each PAIR of gps (the moved group)
  1266. for(thisarray = j; thisarray < jend; thisarray++) { // For all half-wavesets in group
  1267. thatarray = thisarray % dz->itemcnt; // Check not gone past end of waveset store (cycle back to start if so)
  1268. for(n=0;n<wavesetsampcnt[thatarray];n++) { // For every sample in waveset,
  1269. obuf[bufpos++] = waveset[thatarray][n]; // Copy to output
  1270. if(bufpos >= dz->buflen) {
  1271. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1272. return(exit_status);
  1273. bufpos = 0;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. if(bufpos > 0) {
  1279. if((exit_status = write_samps(obuf,bufpos,dz))<0)
  1280. return(exit_status);
  1281. }
  1282. return FINISHED;
  1283. }
  1284. /****************************** DISTORT_SWAP *********************************
  1285. *
  1286. * A B C D E F (e.g. upper case are up-wavesets)
  1287. * a b c d e f (lower case, down-wavesets)
  1288. * 0 1 2 3 4 5 6 7 8 9 10 11
  1289. *
  1290. * A B C D E F
  1291. * b a d c f e (swapped pairwise)
  1292. * (-3) 0 3 2 1 4 7 6 5 8 11 10 9
  1293. * i.e.
  1294. * +3 +3 -1 -1 +3 +3 -1 -1 etc.
  1295. * ---------- ---------
  1296. *
  1297. */
  1298. int distort_swap(dataptr dz)
  1299. {
  1300. int exit_status;
  1301. float *obuf = dz->sampbuf[0];
  1302. int bufpos = 0, j, jend, n, gpsize, thisarray;
  1303. int *wavesetsampcnt = dz->iparray[0];
  1304. float **waveset = dz->fptr;
  1305. gpsize = (dz->iparam[0] * 2) - 1; // i.e. param 1 2 3 4
  1306. // whole cycles in group 0 1 2 3
  1307. // = halfcycles in group 1 2 4 6
  1308. // + required halfcycle 1 1 1 1
  1309. // gives gpsize 1 3 5 7
  1310. j = -(3 * gpsize); // "j" starts at -3, advancing initially to zero inside while-loop
  1311. while(j < dz->itemcnt) {
  1312. if((j += gpsize * 3) >= dz->itemcnt) // Advance by 3 positions
  1313. break;
  1314. if((jend = j + gpsize) >= dz->itemcnt) // End of group to copy (not beyond end of all wavesets).
  1315. break;
  1316. for(thisarray = j; thisarray < jend; thisarray++) { // For all half-wavesets in (non-moved) group,
  1317. for(n=0;n<wavesetsampcnt[thisarray];n++) { // For every sample in waveset,
  1318. obuf[bufpos++] = waveset[thisarray][n]; // Copy to output
  1319. if(bufpos >= dz->buflen) {
  1320. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1321. return(exit_status);
  1322. bufpos = 0;
  1323. }
  1324. }
  1325. }
  1326. if((j += gpsize * 3) >= dz->itemcnt) // Advance by 3 positions
  1327. break;
  1328. if((jend = j + gpsize) >= dz->itemcnt)
  1329. break;
  1330. for(thisarray = j; thisarray < jend; thisarray++) { // For all half-wavesets in group
  1331. for(n=0;n<wavesetsampcnt[thisarray];n++) { // For every sample in waveset,
  1332. obuf[bufpos++] = waveset[thisarray][n]; // Copy to output
  1333. if(bufpos >= dz->buflen) {
  1334. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1335. return(exit_status);
  1336. bufpos = 0;
  1337. }
  1338. }
  1339. }
  1340. j -= gpsize; // Regress by 1 position
  1341. jend = j + gpsize;
  1342. for(thisarray = j; thisarray < jend; thisarray++) { // For all half-wavesets in group
  1343. for(n=0;n<wavesetsampcnt[thisarray];n++) { // For every sample in waveset,
  1344. obuf[bufpos++] = waveset[thisarray][n]; // Copy to output
  1345. if(bufpos >= dz->buflen) {
  1346. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1347. return(exit_status);
  1348. bufpos = 0;
  1349. }
  1350. }
  1351. }
  1352. j -= gpsize; // Regress by 1 position
  1353. jend = j + gpsize;
  1354. for(thisarray = j; thisarray < jend; thisarray++) { // For all half-wavesets in group
  1355. for(n=0;n<wavesetsampcnt[thisarray];n++) { // For every sample in waveset,
  1356. obuf[bufpos++] = waveset[thisarray][n]; // Copy to output
  1357. if(bufpos >= dz->buflen) {
  1358. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1359. return(exit_status);
  1360. bufpos = 0;
  1361. }
  1362. }
  1363. }
  1364. }
  1365. if(bufpos > 0) {
  1366. if((exit_status = write_samps(obuf,bufpos,dz))<0)
  1367. return(exit_status);
  1368. }
  1369. return FINISHED;
  1370. }
  1371. /****************************** GET_MODE *********************************/
  1372. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1373. {
  1374. char temp[200], *p;
  1375. if(sscanf(str,"%s",temp)!=1) {
  1376. sprintf(errstr,"Cannot read mode of program.\n");
  1377. return(USAGE_ONLY);
  1378. }
  1379. p = temp + strlen(temp) - 1;
  1380. while(p >= temp) {
  1381. if(!isdigit(*p)) {
  1382. fprintf(stderr,"Invalid mode of program entered.\n");
  1383. return(USAGE_ONLY);
  1384. }
  1385. p--;
  1386. }
  1387. if(sscanf(str,"%d",&dz->mode)!=1) {
  1388. fprintf(stderr,"Cannot read mode of program.\n");
  1389. return(USAGE_ONLY);
  1390. }
  1391. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1392. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1393. return(USAGE_ONLY);
  1394. }
  1395. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1396. return(FINISHED);
  1397. }