partition.c 42 KB

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