tesselate.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * Need to separate lparrays into INTEGER and UNSIGNED LONG
  23. * THEN try compiling
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <structures.h>
  28. #include <tkglobals.h>
  29. #include <pnames.h>
  30. #include <filetype.h>
  31. #include <processno.h>
  32. #include <modeno.h>
  33. #include <logic.h>
  34. #include <globcon.h>
  35. #include <cdpmain.h>
  36. #include <math.h>
  37. #include <mixxcon.h>
  38. #include <osbind.h>
  39. #include <standalone.h>
  40. #include <science.h>
  41. #include <ctype.h>
  42. #include <sfsys.h>
  43. #include <string.h>
  44. #include <srates.h>
  45. #ifdef unix
  46. #define round(x) lround((x))
  47. #endif
  48. char errstr[2400];
  49. int anal_infiles = 1;
  50. int sloom = 0;
  51. int sloombatch = 0;
  52. const char* cdp_version = "7.0.0";
  53. //CDP LIB REPLACEMENTS
  54. static int check_tesselate_param_validity_and_consistency(dataptr dz);
  55. static int setup_tesselate_application(dataptr dz);
  56. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  57. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  58. static int handle_the_special_data(char *str,dataptr dz);
  59. static int setup_tesselate_param_ranges_and_defaults(dataptr dz);
  60. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  61. static int open_the_outfile(dataptr dz);
  62. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  63. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  64. static int establish_application(dataptr dz);
  65. static int initialise_vflags(dataptr dz);
  66. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  67. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  68. static int mark_parameter_types(dataptr dz,aplptr ap);
  69. static int assign_file_data_storage(int infilecnt,dataptr dz);
  70. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  71. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  72. //static int get_the_mode_from_cmdline(char *str,dataptr dz);
  73. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  74. static int tesselate(dataptr dz);
  75. static int create_tesselate_sndbufs(dataptr dz);
  76. static int copy_from_src(int srcno, int to, int *maxwrite, int offset, unsigned int samps_processed, unsigned int endsamp, dataptr dz);
  77. /**************************************** MAIN *********************************************/
  78. int main(int argc,char *argv[])
  79. {
  80. int exit_status;
  81. dataptr dz = NULL;
  82. char **cmdline;
  83. int cmdlinecnt;
  84. aplptr ap;
  85. int is_launched = FALSE;
  86. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  87. fprintf(stdout,"%s\n",cdp_version);
  88. fflush(stdout);
  89. return 0;
  90. }
  91. /* CHECK FOR SOUNDLOOM */
  92. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  93. sloom = 0;
  94. sloombatch = 1;
  95. }
  96. if(sflinit("cdp")){
  97. sfperror("cdp: initialisation\n");
  98. return(FAILED);
  99. }
  100. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  101. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  102. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  103. return(FAILED);
  104. }
  105. if(!sloom) {
  106. if(argc == 1) {
  107. usage1();
  108. return(FAILED);
  109. } else if(argc == 2) {
  110. usage2(argv[1]);
  111. return(FAILED);
  112. }
  113. }
  114. if(!sloom) {
  115. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  116. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  117. return(FAILED);
  118. }
  119. cmdline = argv;
  120. cmdlinecnt = argc;
  121. if((get_the_process_no(argv[0],dz))<0)
  122. return(FAILED);
  123. cmdline++;
  124. cmdlinecnt--;
  125. dz->maxmode = 0;
  126. // setup_particular_application =
  127. if((exit_status = setup_tesselate_application(dz))<0) {
  128. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  129. return(FAILED);
  130. }
  131. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  132. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  133. return(FAILED);
  134. }
  135. } else {
  136. //parse_TK_data() =
  137. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  138. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  139. return(exit_status);
  140. }
  141. }
  142. ap = dz->application;
  143. // parse_infile_and_hone_type() =
  144. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  145. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  146. return(FAILED);
  147. }
  148. // setup_param_ranges_and_defaults() =
  149. if((exit_status = setup_tesselate_param_ranges_and_defaults(dz))<0) {
  150. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(FAILED);
  152. }
  153. // open_first_infile CDP LIB
  154. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  155. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  156. return(FAILED);
  157. }
  158. cmdlinecnt--;
  159. cmdline++;
  160. if(dz->infilecnt > 1) {
  161. if((exit_status = handle_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  162. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  163. return(FAILED);
  164. }
  165. }
  166. // handle_outfile() =
  167. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  168. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  169. return(FAILED);
  170. }
  171. // handle_formants() redundant
  172. // handle_formant_quiksearch() redundant
  173. if((exit_status = handle_the_special_data(cmdline[0],dz))<0) {
  174. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  175. return(FAILED);
  176. }
  177. cmdlinecnt--;
  178. cmdline++;
  179. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. if((exit_status = open_the_outfile(dz))<0) {
  184. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  185. return(FAILED);
  186. }
  187. if((exit_status = check_tesselate_param_validity_and_consistency(dz))<0) {
  188. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  189. return(FAILED);
  190. }
  191. is_launched = TRUE;
  192. if((exit_status = create_tesselate_sndbufs(dz))<0) {
  193. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  194. return(FAILED);
  195. }
  196. //spec_process_file =
  197. if((exit_status = tesselate(dz))<0) {
  198. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(FAILED);
  200. }
  201. if((exit_status = complete_output(dz))<0) { // CDP LIB
  202. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  203. return(FAILED);
  204. }
  205. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  206. free(dz);
  207. return(SUCCEEDED);
  208. }
  209. /**********************************************
  210. REPLACED CDP LIB FUNCTIONS
  211. **********************************************/
  212. /****************************** SET_PARAM_DATA *********************************/
  213. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  214. {
  215. ap->special_data = (char)special_data;
  216. ap->param_cnt = (char)paramcnt;
  217. ap->max_param_cnt = (char)maxparamcnt;
  218. if(ap->max_param_cnt>0) {
  219. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  220. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  221. return(MEMORY_ERROR);
  222. }
  223. strcpy(ap->param_list,paramlist);
  224. }
  225. return(FINISHED);
  226. }
  227. /****************************** SET_VFLGS *********************************/
  228. int set_vflgs
  229. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  230. {
  231. ap->option_cnt = (char) optcnt; /*RWD added cast */
  232. if(optcnt) {
  233. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  234. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  235. return(MEMORY_ERROR);
  236. }
  237. strcpy(ap->option_list,optlist);
  238. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  239. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  240. return(MEMORY_ERROR);
  241. }
  242. strcpy(ap->option_flags,optflags);
  243. }
  244. ap->vflag_cnt = (char) vflagcnt;
  245. ap->variant_param_cnt = (char) vparamcnt;
  246. if(vflagcnt) {
  247. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  248. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  249. return(MEMORY_ERROR);
  250. }
  251. strcpy(ap->variant_list,varlist);
  252. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  253. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  254. return(MEMORY_ERROR);
  255. }
  256. strcpy(ap->variant_flags,varflags);
  257. }
  258. return(FINISHED);
  259. }
  260. /***************************** APPLICATION_INIT **************************/
  261. int application_init(dataptr dz)
  262. {
  263. int exit_status;
  264. int storage_cnt;
  265. int tipc, brkcnt;
  266. aplptr ap = dz->application;
  267. if(ap->vflag_cnt>0)
  268. initialise_vflags(dz);
  269. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  270. ap->total_input_param_cnt = (char)tipc;
  271. if(tipc>0) {
  272. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  273. return(exit_status);
  274. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  275. return(exit_status);
  276. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  277. return(exit_status);
  278. }
  279. brkcnt = tipc;
  280. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  281. if(brkcnt>0) {
  282. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  283. return(exit_status);
  284. }
  285. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  286. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  287. return(exit_status);
  288. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  289. return(exit_status);
  290. }
  291. if((exit_status = mark_parameter_types(dz,ap))<0)
  292. return(exit_status);
  293. // establish_infile_constants() replaced by
  294. dz->infilecnt = 1;
  295. //establish_bufptrs_and_extra_buffers():
  296. return(FINISHED);
  297. }
  298. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  299. /* RWD mallo changed to calloc; helps debug verison run as release! */
  300. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  301. {
  302. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  303. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  304. return(MEMORY_ERROR);
  305. }
  306. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  307. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  308. return(MEMORY_ERROR);
  309. }
  310. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  311. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  312. return(MEMORY_ERROR);
  313. }
  314. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  315. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  316. return(MEMORY_ERROR);
  317. }
  318. return(FINISHED);
  319. }
  320. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  321. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  322. {
  323. int n;
  324. for(n=0;n<storage_cnt;n++) {
  325. dz->is_int[n] = (char)0;
  326. dz->no_brk[n] = (char)0;
  327. }
  328. return(FINISHED);
  329. }
  330. /***************************** MARK_PARAMETER_TYPES **************************/
  331. int mark_parameter_types(dataptr dz,aplptr ap)
  332. {
  333. int n, m; /* PARAMS */
  334. for(n=0;n<ap->max_param_cnt;n++) {
  335. switch(ap->param_list[n]) {
  336. case('0'): break; /* dz->is_active[n] = 0 is default */
  337. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  338. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  339. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  340. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  341. default:
  342. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  343. return(PROGRAM_ERROR);
  344. }
  345. } /* OPTIONS */
  346. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  347. switch(ap->option_list[n]) {
  348. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  349. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  350. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  351. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  352. default:
  353. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  354. return(PROGRAM_ERROR);
  355. }
  356. } /* VARIANTS */
  357. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  358. switch(ap->variant_list[n]) {
  359. case('0'): break;
  360. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  361. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  362. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  363. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  364. default:
  365. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  366. return(PROGRAM_ERROR);
  367. }
  368. } /* INTERNAL */
  369. for(n=0,
  370. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  371. switch(ap->internal_param_list[n]) {
  372. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  373. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  374. case('d'): dz->no_brk[m] = (char)1; break;
  375. default:
  376. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  377. return(PROGRAM_ERROR);
  378. }
  379. }
  380. return(FINISHED);
  381. }
  382. /************************ HANDLE_THE_OUTFILE *********************/
  383. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  384. {
  385. char *filename = (*cmdline)[0], *p;
  386. if(filename[0]=='-' && filename[1]=='f') {
  387. dz->floatsam_output = 1;
  388. dz->true_outfile_stype = SAMP_FLOAT;
  389. filename+= 2;
  390. }
  391. if(!sloom) {
  392. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  393. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  394. return(DATA_ERROR);
  395. }
  396. }
  397. p = filename; // Drop file extension
  398. while(*p != ENDOFSTR) {
  399. if(*p == '.') {
  400. *p = ENDOFSTR;
  401. break;
  402. }
  403. p++;
  404. }
  405. strcpy(dz->outfilename,filename);
  406. (*cmdline)++;
  407. (*cmdlinecnt)--;
  408. return(FINISHED);
  409. }
  410. /************************ OPEN_THE_OUTFILE *********************/
  411. int open_the_outfile(dataptr dz)
  412. {
  413. int exit_status;
  414. dz->infile->channels = dz->iparam[TESS_CHANS];
  415. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  416. return(exit_status);
  417. dz->infile->channels = 1;
  418. return(FINISHED);
  419. }
  420. /***************************** ESTABLISH_APPLICATION **************************/
  421. int establish_application(dataptr dz)
  422. {
  423. aplptr ap;
  424. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  425. sprintf(errstr,"establish_application()\n");
  426. return(MEMORY_ERROR);
  427. }
  428. ap = dz->application;
  429. memset((char *)ap,0,sizeof(struct applic));
  430. return(FINISHED);
  431. }
  432. /************************* INITIALISE_VFLAGS *************************/
  433. int initialise_vflags(dataptr dz)
  434. {
  435. int n;
  436. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  437. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  438. return(MEMORY_ERROR);
  439. }
  440. for(n=0;n<dz->application->vflag_cnt;n++)
  441. dz->vflag[n] = FALSE;
  442. return FINISHED;
  443. }
  444. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  445. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  446. {
  447. int n;
  448. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  449. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  450. return(MEMORY_ERROR);
  451. }
  452. for(n=0;n<tipc;n++)
  453. ap->default_val[n] = 0.0;
  454. return(FINISHED);
  455. }
  456. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  457. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  458. {
  459. int n;
  460. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  461. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  462. return(MEMORY_ERROR);
  463. }
  464. for(n=0;n<tipc;n++)
  465. dz->is_active[n] = (char)0;
  466. return(FINISHED);
  467. }
  468. /************************* SETUP_TESSELATE_APPLICATION *******************/
  469. int setup_tesselate_application(dataptr dz)
  470. {
  471. int exit_status;
  472. aplptr ap;
  473. if((exit_status = establish_application(dz))<0) // GLOBAL
  474. return(FAILED);
  475. ap = dz->application;
  476. // SEE parstruct FOR EXPLANATION of next 2 functions
  477. if((exit_status = set_param_data(ap,TESSELATION ,4,4,"iddi"))<0)
  478. return(FAILED);
  479. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  480. return(FAILED);
  481. // set_legal_infile_structure -->
  482. dz->has_otherfile = FALSE;
  483. // assign_process_logic -->
  484. dz->input_data_type = ONE_OR_MANY_SNDFILES;
  485. dz->process_type = UNEQUAL_SNDFILE;
  486. dz->outfiletype = SNDFILE_OUT;
  487. return application_init(dz); //GLOBAL
  488. }
  489. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  490. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  491. {
  492. int exit_status;
  493. infileptr infile_info;
  494. if(!sloom) {
  495. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  496. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  497. return(MEMORY_ERROR);
  498. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  499. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  500. return(PROGRAM_ERROR);
  501. } else if(infile_info->filetype != SNDFILE) {
  502. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  503. return(DATA_ERROR);
  504. } else if(infile_info->channels != 1) {
  505. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  506. return(DATA_ERROR);
  507. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  508. sprintf(errstr,"Failed to copy file parsing information\n");
  509. return(PROGRAM_ERROR);
  510. }
  511. free(infile_info);
  512. }
  513. return(FINISHED);
  514. }
  515. /************************* SETUP_MADRID_PARAM_RANGES_AND_DEFAULTS *******************/
  516. int setup_tesselate_param_ranges_and_defaults(dataptr dz)
  517. {
  518. int exit_status;
  519. aplptr ap = dz->application;
  520. // set_param_ranges()
  521. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  522. // NB total_input_param_cnt is > 0 !!!
  523. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  524. return(FAILED);
  525. // get_param_ranges()
  526. ap->lo[TESS_CHANS] = 2;
  527. ap->hi[TESS_CHANS] = 16;
  528. ap->default_val[TESS_CHANS] = 8;
  529. ap->lo[TESS_PHRAS] = FLTERR;
  530. ap->hi[TESS_PHRAS] = 60;
  531. ap->default_val[TESS_PHRAS] = 1.0;
  532. ap->lo[TESS_DUR] = 1;
  533. ap->hi[TESS_DUR] = 7200;
  534. ap->default_val[TESS_DUR] = 60;
  535. ap->lo[TESS_TYP] = 0;
  536. ap->hi[TESS_TYP] = 15;
  537. ap->default_val[TESS_TYP] = 0;
  538. dz->maxmode = 0;
  539. if(!sloom)
  540. put_default_vals_in_all_params(dz);
  541. return(FINISHED);
  542. }
  543. /********************************* PARSE_SLOOM_DATA *********************************/
  544. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  545. {
  546. int exit_status;
  547. int cnt = 1, infilecnt;
  548. int filesize, insams, inbrksize;
  549. double dummy;
  550. int true_cnt = 0;
  551. aplptr ap;
  552. while(cnt<=PRE_CMDLINE_DATACNT) {
  553. if(cnt > argc) {
  554. sprintf(errstr,"Insufficient data sent from TK\n");
  555. return(DATA_ERROR);
  556. }
  557. switch(cnt) {
  558. case(1):
  559. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  560. sprintf(errstr,"Cannot read process no. sent from TK\n");
  561. return(DATA_ERROR);
  562. }
  563. break;
  564. case(2):
  565. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  566. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  567. return(DATA_ERROR);
  568. }
  569. if(dz->mode > 0)
  570. dz->mode--;
  571. //setup_particular_application() =
  572. if((exit_status = setup_tesselate_application(dz))<0)
  573. return(exit_status);
  574. ap = dz->application;
  575. break;
  576. case(3):
  577. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  578. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  579. return(DATA_ERROR);
  580. }
  581. if(infilecnt < 1) {
  582. true_cnt = cnt + 1;
  583. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  584. }
  585. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  586. return(exit_status);
  587. break;
  588. case(INPUT_FILETYPE+4):
  589. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  590. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  591. return(DATA_ERROR);
  592. }
  593. break;
  594. case(INPUT_FILESIZE+4):
  595. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  596. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  597. return(DATA_ERROR);
  598. }
  599. dz->insams[0] = filesize;
  600. break;
  601. case(INPUT_INSAMS+4):
  602. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  603. sprintf(errstr,"Cannot read insams sent from TK\n");
  604. return(DATA_ERROR);
  605. }
  606. dz->insams[0] = insams;
  607. break;
  608. case(INPUT_SRATE+4):
  609. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  610. sprintf(errstr,"Cannot read srate sent from TK\n");
  611. return(DATA_ERROR);
  612. }
  613. break;
  614. case(INPUT_CHANNELS+4):
  615. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  616. sprintf(errstr,"Cannot read channels sent from TK\n");
  617. return(DATA_ERROR);
  618. }
  619. break;
  620. case(INPUT_STYPE+4):
  621. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  622. sprintf(errstr,"Cannot read stype sent from TK\n");
  623. return(DATA_ERROR);
  624. }
  625. break;
  626. case(INPUT_ORIGSTYPE+4):
  627. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  628. sprintf(errstr,"Cannot read origstype sent from TK\n");
  629. return(DATA_ERROR);
  630. }
  631. break;
  632. case(INPUT_ORIGRATE+4):
  633. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  634. sprintf(errstr,"Cannot read origrate sent from TK\n");
  635. return(DATA_ERROR);
  636. }
  637. break;
  638. case(INPUT_MLEN+4):
  639. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  640. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  641. return(DATA_ERROR);
  642. }
  643. break;
  644. case(INPUT_DFAC+4):
  645. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  646. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  647. return(DATA_ERROR);
  648. }
  649. break;
  650. case(INPUT_ORIGCHANS+4):
  651. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  652. sprintf(errstr,"Cannot read origchans sent from TK\n");
  653. return(DATA_ERROR);
  654. }
  655. break;
  656. case(INPUT_SPECENVCNT+4):
  657. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  658. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  659. return(DATA_ERROR);
  660. }
  661. dz->specenvcnt = dz->infile->specenvcnt;
  662. break;
  663. case(INPUT_WANTED+4):
  664. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  665. sprintf(errstr,"Cannot read wanted sent from TK\n");
  666. return(DATA_ERROR);
  667. }
  668. break;
  669. case(INPUT_WLENGTH+4):
  670. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  671. sprintf(errstr,"Cannot read wlength sent from TK\n");
  672. return(DATA_ERROR);
  673. }
  674. break;
  675. case(INPUT_OUT_CHANS+4):
  676. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  677. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  678. return(DATA_ERROR);
  679. }
  680. break;
  681. /* RWD these chanegs to samps - tk will have to deal with that! */
  682. case(INPUT_DESCRIPTOR_BYTES+4):
  683. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  684. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  685. return(DATA_ERROR);
  686. }
  687. break;
  688. case(INPUT_IS_TRANSPOS+4):
  689. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  690. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  691. return(DATA_ERROR);
  692. }
  693. break;
  694. case(INPUT_COULD_BE_TRANSPOS+4):
  695. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  696. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  697. return(DATA_ERROR);
  698. }
  699. break;
  700. case(INPUT_COULD_BE_PITCH+4):
  701. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  702. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  703. return(DATA_ERROR);
  704. }
  705. break;
  706. case(INPUT_DIFFERENT_SRATES+4):
  707. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  708. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  709. return(DATA_ERROR);
  710. }
  711. break;
  712. case(INPUT_DUPLICATE_SNDS+4):
  713. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  714. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  715. return(DATA_ERROR);
  716. }
  717. break;
  718. case(INPUT_BRKSIZE+4):
  719. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  720. sprintf(errstr,"Cannot read brksize sent from TK\n");
  721. return(DATA_ERROR);
  722. }
  723. if(inbrksize > 0) {
  724. switch(dz->input_data_type) {
  725. case(WORDLIST_ONLY):
  726. break;
  727. case(PITCH_AND_PITCH):
  728. case(PITCH_AND_TRANSPOS):
  729. case(TRANSPOS_AND_TRANSPOS):
  730. dz->tempsize = inbrksize;
  731. break;
  732. case(BRKFILES_ONLY):
  733. case(UNRANGED_BRKFILE_ONLY):
  734. case(DB_BRKFILES_ONLY):
  735. case(ALL_FILES):
  736. case(ANY_NUMBER_OF_ANY_FILES):
  737. if(dz->extrabrkno < 0) {
  738. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  739. return(DATA_ERROR);
  740. }
  741. if(dz->brksize == NULL) {
  742. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  743. return(PROGRAM_ERROR);
  744. }
  745. dz->brksize[dz->extrabrkno] = inbrksize;
  746. break;
  747. default:
  748. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  749. dz->input_data_type);
  750. return(PROGRAM_ERROR);
  751. }
  752. break;
  753. }
  754. break;
  755. case(INPUT_NUMSIZE+4):
  756. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  757. sprintf(errstr,"Cannot read numsize sent from TK\n");
  758. return(DATA_ERROR);
  759. }
  760. break;
  761. case(INPUT_LINECNT+4):
  762. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  763. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  764. return(DATA_ERROR);
  765. }
  766. break;
  767. case(INPUT_ALL_WORDS+4):
  768. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  769. sprintf(errstr,"Cannot read all_words sent from TK\n");
  770. return(DATA_ERROR);
  771. }
  772. break;
  773. case(INPUT_ARATE+4):
  774. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  775. sprintf(errstr,"Cannot read arate sent from TK\n");
  776. return(DATA_ERROR);
  777. }
  778. break;
  779. case(INPUT_FRAMETIME+4):
  780. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  781. sprintf(errstr,"Cannot read frametime sent from TK\n");
  782. return(DATA_ERROR);
  783. }
  784. dz->frametime = (float)dummy;
  785. break;
  786. case(INPUT_WINDOW_SIZE+4):
  787. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  788. sprintf(errstr,"Cannot read window_size sent from TK\n");
  789. return(DATA_ERROR);
  790. }
  791. break;
  792. case(INPUT_NYQUIST+4):
  793. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  794. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  795. return(DATA_ERROR);
  796. }
  797. break;
  798. case(INPUT_DURATION+4):
  799. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  800. sprintf(errstr,"Cannot read duration sent from TK\n");
  801. return(DATA_ERROR);
  802. }
  803. break;
  804. case(INPUT_MINBRK+4):
  805. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  806. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  807. return(DATA_ERROR);
  808. }
  809. break;
  810. case(INPUT_MAXBRK+4):
  811. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  812. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  813. return(DATA_ERROR);
  814. }
  815. break;
  816. case(INPUT_MINNUM+4):
  817. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  818. sprintf(errstr,"Cannot read minnum sent from TK\n");
  819. return(DATA_ERROR);
  820. }
  821. break;
  822. case(INPUT_MAXNUM+4):
  823. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  824. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  825. return(DATA_ERROR);
  826. }
  827. break;
  828. default:
  829. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  830. return(PROGRAM_ERROR);
  831. }
  832. cnt++;
  833. }
  834. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  835. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  836. return(DATA_ERROR);
  837. }
  838. if(true_cnt)
  839. cnt = true_cnt;
  840. *cmdlinecnt = 0;
  841. while(cnt < argc) {
  842. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  843. return(exit_status);
  844. cnt++;
  845. }
  846. return(FINISHED);
  847. }
  848. /********************************* GET_TK_CMDLINE_WORD *********************************/
  849. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  850. {
  851. if(*cmdlinecnt==0) {
  852. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  853. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  854. return(MEMORY_ERROR);
  855. }
  856. } else {
  857. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  858. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  859. return(MEMORY_ERROR);
  860. }
  861. }
  862. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  863. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  864. return(MEMORY_ERROR);
  865. }
  866. strcpy((*cmdline)[*cmdlinecnt],q);
  867. (*cmdlinecnt)++;
  868. return(FINISHED);
  869. }
  870. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  871. int assign_file_data_storage(int infilecnt,dataptr dz)
  872. {
  873. int exit_status;
  874. int no_sndfile_system_files = FALSE;
  875. dz->infilecnt = infilecnt;
  876. if((exit_status = allocate_filespace(dz))<0)
  877. return(exit_status);
  878. if(no_sndfile_system_files)
  879. dz->infilecnt = 0;
  880. return(FINISHED);
  881. }
  882. /************************* redundant functions: to ensure libs compile OK *******************/
  883. int assign_process_logic(dataptr dz)
  884. {
  885. return(FINISHED);
  886. }
  887. void set_legal_infile_structure(dataptr dz)
  888. {}
  889. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  890. {
  891. return(FINISHED);
  892. }
  893. int setup_internal_arrays_and_array_pointers(dataptr dz)
  894. {
  895. return(FINISHED);
  896. }
  897. int establish_bufptrs_and_extra_buffers(dataptr dz)
  898. {
  899. return(FINISHED);
  900. }
  901. int read_special_data(char *str,dataptr dz)
  902. {
  903. return(FINISHED);
  904. }
  905. int inner_loop
  906. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  907. {
  908. return(FINISHED);
  909. }
  910. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  911. {
  912. return(FINISHED);
  913. }
  914. /******************************** USAGE1 ********************************/
  915. int usage1(void)
  916. {
  917. usage2("tesselate");
  918. return(USAGE_ONLY);
  919. }
  920. /********************************************************************************************/
  921. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  922. {
  923. if(!strcmp(prog_identifier_from_cmdline,"tesselate")) dz->process = TESSELATE;
  924. else {
  925. fprintf(stdout,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  926. fflush(stdout);
  927. return(USAGE_ONLY);
  928. }
  929. return(FINISHED);
  930. }
  931. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  932. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  933. {
  934. int n;
  935. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  936. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  937. return(MEMORY_ERROR);
  938. }
  939. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  940. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  941. return(MEMORY_ERROR);
  942. }
  943. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  944. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  945. return(MEMORY_ERROR);
  946. }
  947. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  948. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  949. return(MEMORY_ERROR);
  950. }
  951. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  952. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  953. return(MEMORY_ERROR);
  954. }
  955. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  956. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  957. return(MEMORY_ERROR);
  958. }
  959. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  960. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  961. return(MEMORY_ERROR);
  962. }
  963. for(n=0;n<brkcnt;n++) {
  964. dz->brk[n] = NULL;
  965. dz->brkptr[n] = NULL;
  966. dz->brkinit[n] = 0;
  967. dz->brksize[n] = 0;
  968. }
  969. return(FINISHED);
  970. }
  971. /******************************** USAGE2 ********************************/
  972. int usage2(char *str)
  973. {
  974. if(!strcmp(str,"tesselate")) {
  975. fprintf(stdout,
  976. "USAGE:\n"
  977. "tesselate tesselate inf [inf2 ...] outf datafile chans cycledur outdur type [-ffrom]\n"
  978. "\n"
  979. "Create repeating-pattern-with-shift in space and time.\n"
  980. "Each (mono) source repeated on pair(s) of channels,\n"
  981. "But with slightly different delay in the paired channels.\n"
  982. "Shortest repeat-time (without the extra delay) = \"cycledur\".\n"
  983. "\n"
  984. "CHANS Number of output channels (>= 2) and an even number.\n"
  985. "CYCLEDUR The delay between repetitions (same for every source)\n"
  986. " (Sources can have staggered entry, so they form a rhythmic phrase)\n"
  987. " see \"datafile\", line 2.\n"
  988. " Increase in delay on paired-channel(s) determined by \"datafile\" - line 2.\n"
  989. "OUTDUR Duration of output sound.\n"
  990. "TYPE 0: Delay drift between odd and even channels (e.g. 1357-v-2468).\n"
  991. " 1: Delay drift between adjacent channels (1-v-2, 2-v-3, 3-v-4 etc).\n"
  992. " 2: Delay drift between alternate channels (1-v-3, 2-v-4, 3-v-5 etc).\n"
  993. " 3 :Delay drift between every 3rd channel (1-v-4, 2-v-5, 3-v-6 etc).\n"
  994. " and so on.\n"
  995. "FROM Pattern normally starts with all chans in sync at time zero.\n"
  996. " To start later in pattern, specify an integer number of cycles\n"
  997. " from which to start outputting sound.\n"
  998. "\n"
  999. "DATAFILE textfile containing two lines, with the same number of entries per line,\n"
  1000. " and the number of entries corresponds to the number of input files..\n"
  1001. " Line 1 - Lists number of repeats before time-delayed repeat-cycle\n"
  1002. " resychronises with the \"cycledur\"-delayed src.\n"
  1003. " e.g. with value 5 \n"
  1004. " \"cycledur\"-delayed src x x x x x x x\n"
  1005. " timedelayed src x x x x x x\n"
  1006. " Line 2 - Time delay of initial entry of each src.\n"
  1007. " Allows sources to be arranged in some initial rhythmic order.\n"
  1008. " e.g. 0.0 0.1 0.2 0.3 0.4 0.45 0.55 0.7\n"
  1009. " Maximum time-delay must be less than \"cycledur\".\n"
  1010. " All vals must be different.\n"
  1011. " (With same value, 2 sources would collapse into one double-src).\n"
  1012. "\n");
  1013. } else
  1014. fprintf(stdout,"Unknown option '%s'\n",str);
  1015. return(USAGE_ONLY);
  1016. }
  1017. int usage3(char *str1,char *str2)
  1018. {
  1019. fprintf(stderr,"Insufficient parameters on command line.\n");
  1020. return(USAGE_ONLY);
  1021. }
  1022. /******************************** TESSELATE ********************************/
  1023. int tesselate(dataptr dz)
  1024. {
  1025. int exit_status, srcno, chan, layercnt, layerno, passno;
  1026. int safety = 4, outchancnt = dz->iparam[TESS_CHANS], tesstyp = dz->iparam[TESS_TYP];
  1027. unsigned int samps_processed = 0, endsamp;
  1028. int maxwrite = 0, offset = 0, phrasdur_samps_mono, orig_buflen, n;
  1029. double maxsamp = 0.0, normaliser = 1.0, expand;
  1030. int *synccyle = dz->iparray[0];
  1031. int *staggersamps = dz->lparray[0];
  1032. float *obuf = dz->sampbuf[dz->infilecnt], *ovflw = dz->sampbuf[dz->infilecnt + 1], *ibuf, *origobuf;
  1033. int *local_sampcnt, *current_timestep;
  1034. phrasdur_samps_mono = (int)ceil(dz->param[TESS_PHRAS] * dz->infile->srate); // length of shortest repeat-cycle, in samples, in input (mono) frame
  1035. endsamp = (unsigned int)ceil(dz->param[TESS_DUR] * dz->infile->srate);
  1036. endsamp *= outchancnt; // final sample to generate, in output (multichannel) frame
  1037. layercnt = dz->infilecnt * outchancnt; // number of repeating layers
  1038. // if(dz->iparam[TESS_FROM] > 0) {
  1039. // offset = dz->iparam[TESS_FROM] * phrasdur_samps_mono * outchancnt; // If output starts after (syncd) start of the tesselation process
  1040. // endsamp += offset; // Offset the "endsamp" appropriately, to retain same output duration.
  1041. // }
  1042. orig_buflen = dz->buflen;
  1043. for(n=0;n<dz->infilecnt;n++) { // Read all input files into respective buffers.
  1044. dz->buflen = dz->insams[n] + safety;
  1045. ibuf = dz->sampbuf[n];
  1046. if((dz->ssampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[n],0)) < 0) {
  1047. sprintf(errstr,"Can't read samples from input soundfile %d.\n",n+1);
  1048. return(SYSTEM_ERROR);
  1049. }
  1050. }
  1051. dz->buflen = orig_buflen;
  1052. origobuf = obuf;
  1053. // There are "layercnt" repeating streams in the tesseleation, and all need accounting procedures
  1054. if((current_timestep = (int *)malloc(layercnt * sizeof(int)))==NULL) {
  1055. sprintf(errstr,"Insufficient memory for stream timesteps.\n");
  1056. return(MEMORY_ERROR);
  1057. }
  1058. if((local_sampcnt = (int *)malloc(layercnt * sizeof(int)))==NULL) {
  1059. sprintf(errstr,"Insufficient memory for stream sample-counters.\n");
  1060. return(MEMORY_ERROR);
  1061. }
  1062. // INTIALISATION
  1063. for(passno = 0;passno < 2; passno++) {
  1064. samps_processed = 0;
  1065. for(srcno = 0; srcno < dz->infilecnt; srcno++) { // For every source (srcno)
  1066. for(chan = 0; chan < outchancnt; chan++) { // For every channel (chan)
  1067. layerno = (srcno * outchancnt) + chan;
  1068. local_sampcnt[layerno] = staggersamps[srcno]; // Initial sample-offset of repeating layer (srcno-chan).
  1069. local_sampcnt[layerno] += chan; // offset by channel it occupies in output
  1070. expand = 1.0 + (1.0/(double)synccyle[srcno]); // Delay of associated cycle
  1071. switch(tesstyp) {
  1072. case(0): // 1234 v 5678
  1073. if(EVEN(chan))
  1074. current_timestep[layerno] = phrasdur_samps_mono;
  1075. else
  1076. current_timestep[layerno] = (int)round(phrasdur_samps_mono * expand);
  1077. break;
  1078. default:
  1079. if(srcno == chan) // standard delay channel
  1080. current_timestep[layerno] = phrasdur_samps_mono;
  1081. else if((srcno + tesstyp) % outchancnt == chan) // associated longer-delayed channel.
  1082. current_timestep[layerno] = (int)round(phrasdur_samps_mono * expand);
  1083. else
  1084. current_timestep[layerno] = 0; // In all other cases, the source is not copied
  1085. break;
  1086. }
  1087. current_timestep[layerno] *= outchancnt; // convert samp-step to output frame
  1088. }
  1089. }
  1090. memset((char *)obuf,0,dz->buflen * sizeof(float)); // (re)initialise output buffers.
  1091. memset((char *)ovflw,0,dz->buflen * sizeof(float));
  1092. maxsamp = 0; // Initialise maxsample-finder
  1093. while(samps_processed < endsamp) { // Until we reach the last required sample
  1094. // PROCESS EVERY SOURCE FOR EVERY CHANNEL UNTIL BUFFER END IS REACHED
  1095. maxwrite = 0; // Initialise position of last sample written into buffer
  1096. for(srcno = 0; srcno < dz->infilecnt; srcno++) { // For every source
  1097. for(chan = 0; chan < outchancnt; chan++) { // For every channel
  1098. layerno = (srcno * outchancnt) + chan; // Set index to all associated arrays
  1099. if(current_timestep[layerno] == 0) // Don't copy source into channels with no timestep
  1100. continue;
  1101. while(local_sampcnt[layerno] < dz->buflen) { // Write to output, until we reach buffer end
  1102. if((exit_status = copy_from_src(srcno,local_sampcnt[layerno],&maxwrite,offset,samps_processed,endsamp,dz))<0)
  1103. return exit_status;
  1104. local_sampcnt[layerno] += current_timestep[layerno];
  1105. }
  1106. }
  1107. }
  1108. // WRITE OUT COMPLETED BUFFER, THEN COPY OVERFLOW BACK TO BUFFER, AND CONTINUE
  1109. samps_processed += dz->buflen;
  1110. if(offset) {
  1111. // IF SKIPPING EARLY CYCLES, curtail buffer
  1112. if(samps_processed > (unsigned int)offset) { // Check if we've reached the offset
  1113. offset %= dz->buflen; // find offset position in buffer
  1114. obuf += offset; // Write from offset position only
  1115. dz->buflen -= offset;
  1116. offset = 0; // switch off "offset", now we've passed it
  1117. }
  1118. } else {
  1119. // IF WE'VE REACHED END extend buffer
  1120. if(samps_processed >= endsamp) {
  1121. maxwrite = maxwrite/dz->iparam[TESS_CHANS]; // deal with everything in the outbuf (including any overflow).
  1122. maxwrite++; // Adjusting to ensure that a complete group of N-samples is considered
  1123. maxwrite *= dz->iparam[TESS_CHANS];
  1124. dz->buflen = maxwrite;
  1125. }
  1126. // OTHERWISE (buflen does not change) WE WRITE A COMPLETE BUFFER
  1127. }
  1128. switch(passno) {
  1129. case(0): // On first pass, find maximum sample in buffer
  1130. for(n=0;n<dz->buflen;n++)
  1131. maxsamp = max(maxsamp,fabs(obuf[n]));
  1132. break;
  1133. case(1): // On second pass, normalise output, and write to file
  1134. for(n=0;n<dz->buflen;n++)
  1135. obuf[n] = (float)(obuf[n] * normaliser);
  1136. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1137. return(exit_status);
  1138. break;
  1139. }
  1140. dz->buflen = orig_buflen; // Reset buffer start and buflen for next pass
  1141. obuf = origobuf;
  1142. memcpy((char *)obuf,(char *)ovflw,dz->buflen * sizeof(float));
  1143. memset((char *)ovflw,0,dz->buflen * sizeof(float)); // Copy overflow back into outbuffer, and zero overflow.
  1144. for(layerno=0;layerno<layercnt;layerno++)
  1145. local_sampcnt[layerno] -= dz->buflen; // Adjust position of start of next write, in buffer, for every layer
  1146. maxwrite -= dz->buflen; // readjust location-in-buffer of maximum-sample-written
  1147. }
  1148. if(passno == 0) // Once all processing has concluded...
  1149. normaliser = 0.95/maxsamp; // On 1st pass, use "maxsamp" to calculate normaliser.
  1150. }
  1151. return FINISHED;
  1152. }
  1153. /**************************** CREATE_TESSELATE_SNDBUFS ****************************/
  1154. int create_tesselate_sndbufs(dataptr dz)
  1155. {
  1156. int n, safety = 4;
  1157. unsigned int lastbigbufsize = 0, bigbufsize = 0, maxinbufsize = 0, offset;
  1158. unsigned int obufsize, ovflwbufsize;
  1159. int *staggersamps = dz->lparray[0];
  1160. dz->bufcnt = dz->infilecnt+2;
  1161. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  1162. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  1163. return(MEMORY_ERROR);
  1164. }
  1165. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  1166. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  1167. return(MEMORY_ERROR);
  1168. }
  1169. for(n=0;n<dz->infilecnt;n++) {
  1170. offset = staggersamps[n];
  1171. maxinbufsize = max(maxinbufsize,(unsigned int)((dz->insams[n] + offset) + safety));
  1172. bigbufsize += (dz->insams[n] + safety) * sizeof(float);
  1173. if(bigbufsize < lastbigbufsize) {
  1174. sprintf(errstr,"Insufficient memory to store the input soundfiles in buffers.\n");
  1175. return(MEMORY_ERROR);
  1176. }
  1177. lastbigbufsize = bigbufsize;
  1178. }
  1179. dz->buflen = maxinbufsize * dz->iparam[TESS_CHANS]; // Output buffer is at least as big as longest input src + its delay
  1180. if(dz->buflen < 0) {
  1181. sprintf(errstr,"Insufficient memory to store output sound (a).\n");
  1182. return(MEMORY_ERROR);
  1183. }
  1184. obufsize = dz->buflen * sizeof(float);
  1185. if(obufsize < 0) {
  1186. sprintf(errstr,"Insufficient memory to store output sound (b).\n");
  1187. return(MEMORY_ERROR);
  1188. }
  1189. bigbufsize += obufsize;
  1190. if(bigbufsize < lastbigbufsize) {
  1191. sprintf(errstr,"Insufficient memory to store the input soundfiles and create output buffer.\n");
  1192. return(MEMORY_ERROR);
  1193. }
  1194. lastbigbufsize = bigbufsize;
  1195. ovflwbufsize = obufsize;
  1196. bigbufsize += ovflwbufsize;
  1197. if(bigbufsize < lastbigbufsize) {
  1198. sprintf(errstr,"Insufficient memory to store the input soundfiles and create overflow buffer.\n");
  1199. return(MEMORY_ERROR);
  1200. }
  1201. if((dz->bigbuf = (float *)malloc(bigbufsize)) == NULL) {
  1202. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1203. return(PROGRAM_ERROR);
  1204. }
  1205. dz->sbufptr[n] = dz->sampbuf[0] = dz->bigbuf; // inbuf 0
  1206. for(n=1;n<dz->infilecnt;n++) // inbufs 1 - N
  1207. dz->sbufptr[n] = dz->sampbuf[n] = dz->sampbuf[n-1] + dz->insams[n-1] + safety;
  1208. dz->sbufptr[n] = dz->sampbuf[n] = dz->sampbuf[n-1] + dz->insams[n-1] + safety; // obuf
  1209. n++;
  1210. dz->sbufptr[n] = dz->sampbuf[n] = dz->sampbuf[n-1] + dz->buflen; // ovflwbuf
  1211. n++;
  1212. dz->sampbuf[n] = dz->sampbuf[n-1] + dz->buflen; // bufs_end
  1213. return(FINISHED);
  1214. }
  1215. /**************************** COPY_FROM_SRC *****************************/
  1216. int copy_from_src(int srcno, int to, int *maxwrite, int offset, unsigned int samps_processed, unsigned int endsamp, dataptr dz)
  1217. {
  1218. float *obuf = dz->sampbuf[dz->infilecnt];
  1219. int n, k, thismaxwrite;
  1220. unsigned int total_output_sampcnt_at_writestart, index_of_last_input_sample_read, index_of_last_output_sample_writ;
  1221. total_output_sampcnt_at_writestart = samps_processed + to;
  1222. index_of_last_input_sample_read = dz->insams[srcno] - 1; // In INfile
  1223. index_of_last_output_sample_writ = index_of_last_input_sample_read * dz->iparam[TESS_CHANS]; // In OUTfile
  1224. // Only write to output if we're beyond any initial offset AND the write will NOT reach beyond the "endsamp" required
  1225. if((total_output_sampcnt_at_writestart > (unsigned int)offset)
  1226. && (total_output_sampcnt_at_writestart + index_of_last_output_sample_writ < endsamp)) {
  1227. for(n = 0, k = to; n < dz->insams[srcno]; n++, k += dz->iparam[TESS_CHANS]) {
  1228. obuf[to] = (float)(obuf[to] + dz->sampbuf[srcno][n]);
  1229. to += dz->iparam[TESS_CHANS];
  1230. }
  1231. thismaxwrite = to + index_of_last_output_sample_writ + 1; // maxwrite (IN BUFFER) is 1 beyond last sample written
  1232. *maxwrite = max(*maxwrite,thismaxwrite);
  1233. }
  1234. return FINISHED;
  1235. }
  1236. /**************************** CHECK_TESSELATE_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  1237. int check_tesselate_param_validity_and_consistency(dataptr dz)
  1238. {
  1239. int n, m;
  1240. double *stagger = dz->parray[0];
  1241. int *staggersamps = dz->lparray[0];
  1242. if(dz->param[TESS_PHRAS] > dz->param[TESS_DUR]) {
  1243. sprintf(errstr,"Output duration (%lf) too short for phrase duration (%lf) specified.\n",dz->param[TESS_DUR],dz->param[TESS_PHRAS]);
  1244. return(DATA_ERROR);
  1245. }
  1246. if(ODD(dz->iparam[TESS_CHANS])) {
  1247. sprintf(errstr,"Number of output channels (%d) must be even.\n",dz->iparam[TESS_CHANS]);
  1248. return(DATA_ERROR);
  1249. }
  1250. if(dz->iparam[TESS_TYP] >= dz->iparam[TESS_CHANS]) {
  1251. sprintf(errstr,"Tesselation type (%d) must be less than the number of output channels (%d).\n",dz->iparam[TESS_TYP],dz->iparam[TESS_CHANS]);
  1252. return(DATA_ERROR);
  1253. }
  1254. n = 0;
  1255. while(n < dz->itemcnt) {
  1256. if(stagger[n] >= dz->param[TESS_PHRAS]) {
  1257. sprintf(errstr,"Stagger time (%lf) in line 3 of datafile, out of range (must be less than phase duration %lf).\n",stagger[n],dz->param[TESS_PHRAS]);
  1258. return(DATA_ERROR);
  1259. } // Convert initial source delays to samples cnts in (multichan) outbuf.
  1260. staggersamps[n] = (int)round(stagger[n] * dz->infile->srate) * dz->iparam[TESS_CHANS];
  1261. n++;
  1262. }
  1263. n = 0;
  1264. while(n < dz->itemcnt - 1) {
  1265. m = n + 1;
  1266. while(m < dz->itemcnt) {
  1267. if(flteq(stagger[n],stagger[m])) {
  1268. sprintf(errstr,"Not all staggered-entry-times in line 3 of datafile are distinct (e.g. %lf).\n",stagger[n]);
  1269. return(DATA_ERROR);
  1270. }
  1271. m++;
  1272. }
  1273. n++;
  1274. }
  1275. return FINISHED;
  1276. }
  1277. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  1278. int handle_the_special_data(char *str,dataptr dz)
  1279. {
  1280. double dummy = 0.0;
  1281. FILE *fp;
  1282. int cnt, linecnt, idummy;
  1283. char temp[8000], *p;
  1284. if((fp = fopen(str,"r"))==NULL) {
  1285. sprintf(errstr,"Cannot open file %s to read data.\n",str);
  1286. return(DATA_ERROR);
  1287. }
  1288. linecnt = 0;
  1289. while(fgets(temp,8000,fp)!=NULL) {
  1290. p = temp;
  1291. while(isspace(*p))
  1292. p++;
  1293. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1294. continue;
  1295. cnt = 0;
  1296. while(get_float_from_within_string(&p,&dummy)) {
  1297. switch(linecnt) {
  1298. case(0):
  1299. idummy = (int)round(dummy);
  1300. if(idummy < 2 || idummy > 1024) {
  1301. sprintf(errstr,"Resync count in line 2 of data, out of range (2 to 1024), in file %s\n",str);
  1302. return(DATA_ERROR);
  1303. }
  1304. break;
  1305. case(1):
  1306. if(dummy < 0.0) {
  1307. sprintf(errstr,"Invalid src-onset delay in line 3 of file %s\n",str);
  1308. return(DATA_ERROR);
  1309. }
  1310. break;
  1311. }
  1312. cnt++;
  1313. }
  1314. if(linecnt == 0) {
  1315. dz->itemcnt = cnt;
  1316. if(dz->itemcnt != dz->infilecnt) {
  1317. sprintf(errstr,"No of data items (%d) in 1st line of file %s doesn't correspond to no of input files (%d)\n",dz->itemcnt,str,dz->infilecnt);
  1318. return(DATA_ERROR);
  1319. }
  1320. } else {
  1321. if(cnt != dz->itemcnt) {
  1322. sprintf(errstr,"Different number of data items (%d %d) in different lines in file %s\n",dz->itemcnt,cnt,str);
  1323. return(DATA_ERROR);
  1324. }
  1325. }
  1326. linecnt++;
  1327. }
  1328. if(linecnt == 0) {
  1329. sprintf(errstr,"No data found in file %s.\n",str);
  1330. return(DATA_ERROR);
  1331. }
  1332. else if(linecnt != 2) {
  1333. sprintf(errstr,"Should be 2 lines of data in file %s : found %d lines.\n",str,linecnt);
  1334. return(DATA_ERROR);
  1335. }
  1336. fseek(fp,0,0);
  1337. if((dz->parray = (double **)malloc(sizeof(double *)))==NULL) {
  1338. sprintf(errstr,"INSUFFICIENT MEMORY to store stagger values.\n");
  1339. return(MEMORY_ERROR);
  1340. }
  1341. if((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1342. sprintf(errstr,"INSUFFICIENT MEMORY to store stagger values.\n");
  1343. return(MEMORY_ERROR);
  1344. }
  1345. if((dz->lparray = (int **)malloc(sizeof(int *)))==NULL) {
  1346. sprintf(errstr,"INSUFFICIENT MEMORY to store stagger values.\n");
  1347. return(MEMORY_ERROR);
  1348. }
  1349. if((dz->lparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1350. sprintf(errstr,"INSUFFICIENT MEMORY to store stagger values.\n");
  1351. return(MEMORY_ERROR);
  1352. }
  1353. if((dz->iparray = (int **)malloc(sizeof(int)))==NULL) {
  1354. sprintf(errstr,"INSUFFICIENT MEMORY to store integer data.\n");
  1355. return(MEMORY_ERROR);
  1356. }
  1357. if((dz->iparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1358. sprintf(errstr,"INSUFFICIENT MEMORY to store initial-channel data.\n");
  1359. return(MEMORY_ERROR);
  1360. }
  1361. fseek(fp,0,0);
  1362. linecnt = 0;
  1363. while(fgets(temp,8000,fp)!=NULL) {
  1364. p = temp;
  1365. while(isspace(*p))
  1366. p++;
  1367. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1368. continue;
  1369. cnt = 0;
  1370. while(get_float_from_within_string(&p,&dummy)) {
  1371. switch(linecnt) {
  1372. case(0):
  1373. dz->iparray[0][cnt] = (int)round(dummy);
  1374. break;
  1375. case(1):
  1376. dz->parray[0][cnt] = dummy;
  1377. break;
  1378. }
  1379. cnt++;
  1380. }
  1381. linecnt++;
  1382. }
  1383. if(fclose(fp)<0) {
  1384. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",str);
  1385. fflush(stdout);
  1386. }
  1387. return(FINISHED);
  1388. }