mchzig.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /*
  2. * Copyright (c) 1983-2023 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /* MCHZIG zigzag with mchan output
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <pnames.h>
  28. #include <filetype.h>
  29. #include <processno.h>
  30. #include <modeno.h>
  31. #include <logic.h>
  32. #include <globcon.h>
  33. #include <cdpmain.h>
  34. #include <math.h>
  35. #include <mixxcon.h>
  36. #include <osbind.h>
  37. #include <standalone.h>
  38. #include <ctype.h>
  39. #include <sfsys.h>
  40. #include <string.h>
  41. #include <srates.h>
  42. #include <extdcon.h>
  43. #include <arrays.h>
  44. #include <flags.h>
  45. //#include <memory.h>
  46. //#include <string.h>
  47. #if defined unix || defined __GNUC__
  48. #define round(x) lround((x))
  49. #endif
  50. #ifndef HUGE
  51. #define HUGE 3.40282347e+38F
  52. #endif
  53. char errstr[2400];
  54. int anal_infiles = 1;
  55. int sloom = 0;
  56. int sloombatch = 0;
  57. const char* cdp_version = "7.1.0";
  58. /* internal */
  59. #define MZIG_SPLICECNT (8)
  60. #define MZIG_SPLSAMPS (9)
  61. #define MZIG_RUNSTOEND (10)
  62. #define MZIG_PERMCH (0)
  63. #define ZIG (1)
  64. #define UNKNOWN (0)
  65. #define ZAG (-1)
  66. #define NORMAL (0)
  67. #define REVERSE (1)
  68. #define FORWARDS (1)
  69. #define BACKWARDS (-1)
  70. #define ROOT2 (1.4142136)
  71. #define SECMARGIN (256)
  72. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  73. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  74. static int setup_mchzig_application(dataptr dz);
  75. static int setup_mchzig_param_ranges_and_defaults(dataptr dz);
  76. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  77. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  78. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  79. static int open_the_outfile(dataptr dz);
  80. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  81. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  82. static int establish_application(dataptr dz);
  83. static int initialise_vflags(dataptr dz);
  84. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  85. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  86. static int mark_parameter_types(dataptr dz,aplptr ap);
  87. static int assign_file_data_storage(int infilecnt,dataptr dz);
  88. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  89. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  90. static int check_zigzag_consistency(dataptr dz);
  91. static int mzigzag_preprocess(int *maxzig,dataptr dz);
  92. static int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz);
  93. static int setup_the_special_data_ranges(int mode,int srate,double duration,double nyquist,int wlength,int channels,aplptr ap);
  94. static int read_ziginfo(char *filename,dataptr dz);
  95. static int setup_zigzag_splice(int paramno,dataptr dz);
  96. static int make_zigsplice(int paramno,dataptr dz);
  97. static int create_zigzag_splicebuffer(dataptr dz);
  98. static int generate_zigzag_table(dataptr dz);
  99. static int sort_zigs(int *maxzig,dataptr dz);
  100. static int eliminate_n_steps(int *this_zigtime,int *next_zigtime,int **ziglistend,int *cnt,dataptr dz);
  101. static int eliminate_step(int *next_zigtime,int **ziglistend,int *cnt,dataptr dz);
  102. static int zigzag(dataptr dz);
  103. static int zig_or_zag(int *thisstart,int *lastend,int *outbuf_space,int obufno,int obufendno,int startchan,int endchan,dataptr dz);
  104. static int find_zzchunk(int *thisstart,int *ziglistend, int *minsamp, dataptr dz);
  105. static int reverse_it(int incnt,dataptr dz);
  106. static int copy_with_spatial_scatter(float *outptr,float *tobufend,int startchan,int endchan,int sampcnt,int chancnt,dataptr dz);
  107. static void permute_chans(int outchans,dataptr dz);
  108. static void insertch(int n,int t,int outchans,dataptr dz);
  109. static void prefixch(int n,int outchans,dataptr dz);
  110. static void shuflupch(int k,int outchans,dataptr dz);
  111. static int mz_setup_internal_arrays_and_array_pointers(dataptr dz);
  112. static void pancalc(double position,double *leftgain,double *rightgain);
  113. static void get_bufsize_needed(int *maxzig,dataptr dz);
  114. static int create_mzig_sndbufs(int maxzig,dataptr dz);
  115. static void do_start_splice(float *buf,dataptr dz);
  116. static void do_end_splice(float *buf,int incnt,dataptr dz);
  117. static int adjacence(int endchan,dataptr dz);
  118. /**************************************** MAIN *********************************************/
  119. int main(int argc,char *argv[])
  120. {
  121. int exit_status;
  122. dataptr dz = NULL;
  123. char **cmdline;
  124. int cmdlinecnt;
  125. int n, maxzig;
  126. //aplptr ap;
  127. int is_launched = FALSE;
  128. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  129. fprintf(stdout,"%s\n",cdp_version);
  130. fflush(stdout);
  131. return 0;
  132. }
  133. /* CHECK FOR SOUNDLOOM */
  134. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  135. sloom = 0;
  136. sloombatch = 1;
  137. }
  138. if(sflinit("cdp")){
  139. sfperror("cdp: initialisation\n");
  140. return(FAILED);
  141. }
  142. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  143. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  144. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  145. return(FAILED);
  146. }
  147. if(!sloom) {
  148. if(argc == 1) {
  149. usage1();
  150. return(FAILED);
  151. } else if(argc == 2) {
  152. usage2(argv[1]);
  153. return(FAILED);
  154. }
  155. }
  156. if(!sloom) {
  157. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  158. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  159. return(FAILED);
  160. }
  161. cmdline = argv;
  162. cmdlinecnt = argc;
  163. if((get_the_process_no(argv[0],dz))<0)
  164. return(FAILED);
  165. cmdline++;
  166. cmdlinecnt--;
  167. dz->maxmode = 2;
  168. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  169. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  170. return(exit_status);
  171. }
  172. cmdline++;
  173. cmdlinecnt--;
  174. // setup_particular_application =
  175. if((exit_status = setup_mchzig_application(dz))<0) {
  176. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  177. return(FAILED);
  178. }
  179. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. } else {
  184. //parse_TK_data() =
  185. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  186. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  187. return(exit_status);
  188. }
  189. }
  190. //ap = dz->application;
  191. // parse_infile_and_hone_type() =
  192. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  193. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  194. return(FAILED);
  195. }
  196. // setup_param_ranges_and_defaults() =
  197. if((exit_status = setup_mchzig_param_ranges_and_defaults(dz))<0) {
  198. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(FAILED);
  200. }
  201. // open_first_infile CDP LIB
  202. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  203. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  204. return(FAILED);
  205. }
  206. cmdlinecnt--;
  207. cmdline++;
  208. // handle_extra_infiles() : redundant
  209. // handle_outfile() =
  210. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  211. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  212. return(FAILED);
  213. }
  214. // handle_formants() redundant
  215. // handle_formant_quiksearch() redundant
  216. // handle_special_data() =
  217. if(dz->mode == 1) {
  218. if((exit_status = handle_the_special_data(&cmdlinecnt,&cmdline,dz))<0) {
  219. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  220. return(FAILED);
  221. }
  222. }
  223. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  224. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  225. return(FAILED);
  226. }
  227. // check_param_validity_and_consistency....
  228. if((exit_status = check_zigzag_consistency(dz))<0) {
  229. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  230. return(FAILED);
  231. }
  232. is_launched = TRUE;
  233. dz->bufcnt = 2 + dz->iparam[MZIG_OCHANS];
  234. dz->extra_bufcnt = 1;
  235. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  236. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  237. return(MEMORY_ERROR);
  238. }
  239. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  240. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  241. return(MEMORY_ERROR);
  242. }
  243. for(n = 0;n <dz->bufcnt; n++)
  244. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  245. dz->sampbuf[n] = (float *)0;
  246. if((dz->extrabuf = (float **)malloc(sizeof(float *) * (dz->extra_bufcnt+1)))==NULL) {
  247. sprintf(errstr,"INSUFFICIENT MEMORY establishing extra buffers.\n");
  248. return(MEMORY_ERROR);
  249. }
  250. if((dz->extrabufptr = (float **)malloc(sizeof(float *) * (dz->extra_bufcnt+1)))==NULL) {
  251. sprintf(errstr,"INSUFFICIENT MEMORY establishing extrabuffer pointers.\n");
  252. return(MEMORY_ERROR);
  253. }
  254. for(n = 0;n <dz->extra_bufcnt; n++)
  255. dz->extrabuf[n] = dz->extrabufptr[n] = (float *)0;
  256. dz->extrabuf[n] = (float *)0;
  257. //param_preprocess() ...........
  258. if((exit_status = mzigzag_preprocess(&maxzig,dz))<0) {
  259. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  260. return(FAILED);
  261. }
  262. if((exit_status = create_mzig_sndbufs(maxzig,dz))<0) { // CDP LIB
  263. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  264. return(FAILED);
  265. }
  266. if((exit_status = open_the_outfile(dz))<0) {
  267. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  268. return(FAILED);
  269. }
  270. //groucho_process_file =
  271. if((exit_status = zigzag(dz))<0) {
  272. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  273. return(FAILED);
  274. }
  275. if((exit_status = complete_output(dz))<0) { // CDP LIB
  276. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  277. return(FAILED);
  278. }
  279. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  280. free(dz);
  281. return(SUCCEEDED);
  282. }
  283. /**********************************************
  284. REPLACED CDP LIB FUNCTIONS
  285. **********************************************/
  286. /****************************** SET_PARAM_DATA *********************************/
  287. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  288. {
  289. ap->special_data = (char)special_data;
  290. ap->param_cnt = (char)paramcnt;
  291. ap->max_param_cnt = (char)maxparamcnt;
  292. if(ap->max_param_cnt>0) {
  293. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  294. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  295. return(MEMORY_ERROR);
  296. }
  297. strcpy(ap->param_list,paramlist);
  298. }
  299. return(FINISHED);
  300. }
  301. /****************************** SET_VFLGS *********************************/
  302. int set_vflgs
  303. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  304. {
  305. ap->option_cnt = (char) optcnt; /*RWD added cast */
  306. if(optcnt) {
  307. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  308. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  309. return(MEMORY_ERROR);
  310. }
  311. strcpy(ap->option_list,optlist);
  312. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  313. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  314. return(MEMORY_ERROR);
  315. }
  316. strcpy(ap->option_flags,optflags);
  317. }
  318. ap->vflag_cnt = (char) vflagcnt;
  319. ap->variant_param_cnt = (char) vparamcnt;
  320. if(vflagcnt) {
  321. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  322. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  323. return(MEMORY_ERROR);
  324. }
  325. strcpy(ap->variant_list,varlist);
  326. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  327. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  328. return(MEMORY_ERROR);
  329. }
  330. strcpy(ap->variant_flags,varflags);
  331. }
  332. return(FINISHED);
  333. }
  334. /***************************** APPLICATION_INIT **************************/
  335. int application_init(dataptr dz)
  336. {
  337. int exit_status;
  338. int storage_cnt;
  339. int tipc, brkcnt;
  340. aplptr ap = dz->application;
  341. if(ap->vflag_cnt>0)
  342. initialise_vflags(dz);
  343. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  344. ap->total_input_param_cnt = (char)tipc;
  345. if(tipc>0) {
  346. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  347. return(exit_status);
  348. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  349. return(exit_status);
  350. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  351. return(exit_status);
  352. }
  353. brkcnt = tipc;
  354. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  355. if(brkcnt>0) {
  356. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  357. return(exit_status);
  358. }
  359. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  360. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  361. return(exit_status);
  362. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  363. return(exit_status);
  364. }
  365. if((exit_status = mark_parameter_types(dz,ap))<0)
  366. return(exit_status);
  367. // establish_infile_constants() replaced by
  368. dz->infilecnt = 1;
  369. //establish_bufptrs_and_extra_buffers():
  370. if((exit_status = mz_setup_internal_arrays_and_array_pointers(dz))<0)
  371. return(exit_status);
  372. return(FINISHED);
  373. }
  374. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  375. /* RWD mallo changed to calloc; helps debug verison run as release! */
  376. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  377. {
  378. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  379. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  380. return(MEMORY_ERROR);
  381. }
  382. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  383. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  384. return(MEMORY_ERROR);
  385. }
  386. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  387. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  388. return(MEMORY_ERROR);
  389. }
  390. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  391. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  392. return(MEMORY_ERROR);
  393. }
  394. return(FINISHED);
  395. }
  396. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  397. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  398. {
  399. int n;
  400. for(n=0;n<storage_cnt;n++) {
  401. dz->is_int[n] = (char)0;
  402. dz->no_brk[n] = (char)0;
  403. }
  404. return(FINISHED);
  405. }
  406. /***************************** MARK_PARAMETER_TYPES **************************/
  407. int mark_parameter_types(dataptr dz,aplptr ap)
  408. {
  409. int n, m; /* PARAMS */
  410. for(n=0;n<ap->max_param_cnt;n++) {
  411. switch(ap->param_list[n]) {
  412. case('0'): break; /* dz->is_active[n] = 0 is default */
  413. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  414. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  415. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  416. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  417. default:
  418. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  419. return(PROGRAM_ERROR);
  420. }
  421. } /* OPTIONS */
  422. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  423. switch(ap->option_list[n]) {
  424. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  425. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  426. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  427. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  428. default:
  429. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  430. return(PROGRAM_ERROR);
  431. }
  432. } /* VARIANTS */
  433. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  434. switch(ap->variant_list[n]) {
  435. case('0'): break;
  436. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  437. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  438. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  439. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  440. default:
  441. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  442. return(PROGRAM_ERROR);
  443. }
  444. } /* INTERNAL */
  445. for(n=0,
  446. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  447. switch(ap->internal_param_list[n]) {
  448. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  449. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  450. case('d'): dz->no_brk[m] = (char)1; break;
  451. default:
  452. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  453. return(PROGRAM_ERROR);
  454. }
  455. }
  456. return(FINISHED);
  457. }
  458. /************************ HANDLE_THE_OUTFILE *********************/
  459. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  460. {
  461. char *filename = (*cmdline)[0];
  462. if(filename[0]=='-' && filename[1]=='f') {
  463. dz->floatsam_output = 1;
  464. dz->true_outfile_stype = SAMP_FLOAT;
  465. filename+= 2;
  466. }
  467. if(!sloom) {
  468. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  469. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  470. return(DATA_ERROR);
  471. }
  472. }
  473. strcpy(dz->outfilename,filename);
  474. (*cmdline)++;
  475. (*cmdlinecnt)--;
  476. return(FINISHED);
  477. }
  478. /************************ OPEN_THE_OUTFILE *********************/
  479. int open_the_outfile(dataptr dz)
  480. {
  481. int exit_status;
  482. dz->infile->channels = dz->iparam[MZIG_OCHANS];
  483. dz->outfile->channels = dz->iparam[MZIG_OCHANS];
  484. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  485. return(exit_status);
  486. dz->infile->channels = 1;
  487. return(FINISHED);
  488. }
  489. /***************************** ESTABLISH_APPLICATION **************************/
  490. int establish_application(dataptr dz)
  491. {
  492. aplptr ap;
  493. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  494. sprintf(errstr,"establish_application()\n");
  495. return(MEMORY_ERROR);
  496. }
  497. ap = dz->application;
  498. memset((char *)ap,0,sizeof(struct applic));
  499. return(FINISHED);
  500. }
  501. /************************* INITIALISE_VFLAGS *************************/
  502. int initialise_vflags(dataptr dz)
  503. {
  504. int n;
  505. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  506. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  507. return(MEMORY_ERROR);
  508. }
  509. for(n=0;n<dz->application->vflag_cnt;n++)
  510. dz->vflag[n] = FALSE;
  511. return FINISHED;
  512. }
  513. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  514. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  515. {
  516. int n;
  517. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  518. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  519. return(MEMORY_ERROR);
  520. }
  521. for(n=0;n<tipc;n++)
  522. ap->default_val[n] = 0.0;
  523. return(FINISHED);
  524. }
  525. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  526. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  527. {
  528. int n;
  529. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  530. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  531. return(MEMORY_ERROR);
  532. }
  533. for(n=0;n<tipc;n++)
  534. dz->is_active[n] = (char)0;
  535. return(FINISHED);
  536. }
  537. /************************* SETUP_MCHZIG_APPLICATION *******************/
  538. int setup_mchzig_application(dataptr dz)
  539. {
  540. int exit_status;
  541. aplptr ap;
  542. if((exit_status = establish_application(dz))<0) // GLOBAL
  543. return(FAILED);
  544. ap = dz->application;
  545. // SEE parstruct FOR EXPLANATION of next 2 functions
  546. switch(dz->mode) {
  547. case(0): exit_status = set_param_data(ap,0 , 5,5,"ddddi"); break;
  548. case(1): exit_status = set_param_data(ap,MZIGDATA,5,1,"0000i"); break;
  549. }
  550. if(exit_status <0)
  551. return(FAILED);
  552. switch(dz->mode) {
  553. case(0): exit_status = set_vflgs(ap,"smr",3,"ddi","a",1,0,"0"); break;
  554. case(1): exit_status = set_vflgs(ap,"s" ,1,"d" ,"a",1,0,"0"); break;
  555. }
  556. if(exit_status <0)
  557. return(FAILED);
  558. // set_legal_infile_structure -->
  559. dz->has_otherfile = FALSE;
  560. // assign_process_logic -->
  561. dz->input_data_type = SNDFILES_ONLY;
  562. dz->process_type = UNEQUAL_SNDFILE;
  563. dz->outfiletype = SNDFILE_OUT;
  564. return application_init(dz); //GLOBAL
  565. }
  566. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  567. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  568. {
  569. int exit_status;
  570. infileptr infile_info;
  571. if(!sloom) {
  572. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  573. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  574. return(MEMORY_ERROR);
  575. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  576. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  577. return(PROGRAM_ERROR);
  578. } else if(infile_info->filetype != SNDFILE) {
  579. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  580. return(DATA_ERROR);
  581. } else if(infile_info->channels != 1) {
  582. sprintf(errstr,"Mono files only\n");
  583. return(DATA_ERROR);
  584. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  585. sprintf(errstr,"Failed to copy file parsing information\n");
  586. return(PROGRAM_ERROR);
  587. }
  588. free(infile_info);
  589. }
  590. return(FINISHED);
  591. }
  592. /************************* SETUP_MCHZIG_PARAM_RANGES_AND_DEFAULTS *******************/
  593. int setup_mchzig_param_ranges_and_defaults(dataptr dz)
  594. {
  595. int exit_status;
  596. aplptr ap = dz->application;
  597. // set_param_ranges()
  598. double duration = (double)dz->insams[0]/(double)dz->infile->srate;
  599. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  600. // NB total_input_param_cnt is > 0 !!!
  601. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  602. return(FAILED);
  603. // get_param_ranges()
  604. ap->lo[MZIG_START] = 0.0;
  605. ap->hi[MZIG_START] = duration - (MZIG_SPLICELEN * MS_TO_SECS);
  606. ap->default_val[MZIG_START] = 0.0;
  607. ap->lo[MZIG_END] = ((MZIG_SPLICELEN * 2) + MZIG_MIN_UNSPLICED) * MS_TO_SECS;
  608. ap->hi[MZIG_END] = duration;
  609. ap->default_val[MZIG_END] = duration;
  610. ap->lo[MZIG_DUR] = duration + FLTERR;
  611. ap->hi[MZIG_DUR] = BIG_TIME;
  612. ap->default_val[MZIG_DUR] = duration * 2.0;
  613. ap->lo[MZIG_MIN] = ((MZIG_SPLICELEN * 2) + MZIG_MIN_UNSPLICED) * MS_TO_SECS;
  614. ap->hi[MZIG_MIN] = duration - (2 * MZIG_SPLICELEN * MS_TO_SECS);
  615. ap->default_val[MZIG_MIN] = ((MZIG_SPLICELEN * 2) + MZIG_MIN_UNSPLICED) * MS_TO_SECS;
  616. ap->lo[MZIG_OCHANS] = 2;
  617. ap->hi[MZIG_OCHANS] = 16;
  618. ap->default_val[MZIG_OCHANS] = 8;
  619. ap->lo[MZIG_SPLEN] = MMIN_ZIGSPLICE;
  620. ap->hi[MZIG_SPLEN] = MMAX_ZIGSPLICE;
  621. ap->default_val[MZIG_SPLEN] = MZIG_SPLICELEN;
  622. if(dz->mode==ZIGZAG_SELF) {
  623. ap->lo[MZIG_MAX] = ((MZIG_SPLICELEN * 2) + MZIG_MIN_UNSPLICED) * MS_TO_SECS;
  624. ap->hi[MZIG_MAX] = duration - (2 * MZIG_SPLICELEN * MS_TO_SECS);
  625. ap->default_val[MZIG_MAX] = min(2.0,duration - (2 * MZIG_SPLICELEN * MS_TO_SECS));
  626. ap->lo[MZIG_RSEED] = 0.0;
  627. ap->hi[MZIG_RSEED] = MAXSHORT;
  628. ap->default_val[MZIG_RSEED] = 0.0;
  629. }
  630. if(!sloom)
  631. put_default_vals_in_all_params(dz);
  632. return(FINISHED);
  633. }
  634. /********************************* PARSE_SLOOM_DATA *********************************/
  635. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  636. {
  637. int exit_status;
  638. int cnt = 1, infilecnt;
  639. int filesize, insams, inbrksize;
  640. double dummy;
  641. int true_cnt = 0;
  642. //aplptr ap;
  643. while(cnt<=PRE_CMDLINE_DATACNT) {
  644. if(cnt > argc) {
  645. sprintf(errstr,"Insufficient data sent from TK\n");
  646. return(DATA_ERROR);
  647. }
  648. switch(cnt) {
  649. case(1):
  650. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  651. sprintf(errstr,"Cannot read process no. sent from TK\n");
  652. return(DATA_ERROR);
  653. }
  654. break;
  655. case(2):
  656. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  657. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  658. return(DATA_ERROR);
  659. }
  660. if(dz->mode > 0)
  661. dz->mode--;
  662. //setup_particular_application() =
  663. if((exit_status = setup_mchzig_application(dz))<0)
  664. return(exit_status);
  665. //ap = dz->application;
  666. break;
  667. case(3):
  668. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  669. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  670. return(DATA_ERROR);
  671. }
  672. if(infilecnt < 1) {
  673. true_cnt = cnt + 1;
  674. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  675. }
  676. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  677. return(exit_status);
  678. break;
  679. case(INPUT_FILETYPE+4):
  680. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  681. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  682. return(DATA_ERROR);
  683. }
  684. break;
  685. case(INPUT_FILESIZE+4):
  686. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  687. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  688. return(DATA_ERROR);
  689. }
  690. dz->insams[0] = filesize;
  691. break;
  692. case(INPUT_INSAMS+4):
  693. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  694. sprintf(errstr,"Cannot read insams sent from TK\n");
  695. return(DATA_ERROR);
  696. }
  697. dz->insams[0] = insams;
  698. break;
  699. case(INPUT_SRATE+4):
  700. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  701. sprintf(errstr,"Cannot read srate sent from TK\n");
  702. return(DATA_ERROR);
  703. }
  704. break;
  705. case(INPUT_CHANNELS+4):
  706. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  707. sprintf(errstr,"Cannot read channels sent from TK\n");
  708. return(DATA_ERROR);
  709. }
  710. break;
  711. case(INPUT_STYPE+4):
  712. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  713. sprintf(errstr,"Cannot read stype sent from TK\n");
  714. return(DATA_ERROR);
  715. }
  716. break;
  717. case(INPUT_ORIGSTYPE+4):
  718. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  719. sprintf(errstr,"Cannot read origstype sent from TK\n");
  720. return(DATA_ERROR);
  721. }
  722. break;
  723. case(INPUT_ORIGRATE+4):
  724. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  725. sprintf(errstr,"Cannot read origrate sent from TK\n");
  726. return(DATA_ERROR);
  727. }
  728. break;
  729. case(INPUT_MLEN+4):
  730. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  731. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  732. return(DATA_ERROR);
  733. }
  734. break;
  735. case(INPUT_DFAC+4):
  736. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  737. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  738. return(DATA_ERROR);
  739. }
  740. break;
  741. case(INPUT_ORIGCHANS+4):
  742. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  743. sprintf(errstr,"Cannot read origchans sent from TK\n");
  744. return(DATA_ERROR);
  745. }
  746. break;
  747. case(INPUT_SPECENVCNT+4):
  748. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  749. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  750. return(DATA_ERROR);
  751. }
  752. dz->specenvcnt = dz->infile->specenvcnt;
  753. break;
  754. case(INPUT_WANTED+4):
  755. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  756. sprintf(errstr,"Cannot read wanted sent from TK\n");
  757. return(DATA_ERROR);
  758. }
  759. break;
  760. case(INPUT_WLENGTH+4):
  761. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  762. sprintf(errstr,"Cannot read wlength sent from TK\n");
  763. return(DATA_ERROR);
  764. }
  765. break;
  766. case(INPUT_OUT_CHANS+4):
  767. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  768. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  769. return(DATA_ERROR);
  770. }
  771. break;
  772. /* RWD these chanegs to samps - tk will have to deal with that! */
  773. case(INPUT_DESCRIPTOR_BYTES+4):
  774. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  775. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  776. return(DATA_ERROR);
  777. }
  778. break;
  779. case(INPUT_IS_TRANSPOS+4):
  780. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  781. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  782. return(DATA_ERROR);
  783. }
  784. break;
  785. case(INPUT_COULD_BE_TRANSPOS+4):
  786. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  787. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  788. return(DATA_ERROR);
  789. }
  790. break;
  791. case(INPUT_COULD_BE_PITCH+4):
  792. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  793. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  794. return(DATA_ERROR);
  795. }
  796. break;
  797. case(INPUT_DIFFERENT_SRATES+4):
  798. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  799. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  800. return(DATA_ERROR);
  801. }
  802. break;
  803. case(INPUT_DUPLICATE_SNDS+4):
  804. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  805. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  806. return(DATA_ERROR);
  807. }
  808. break;
  809. case(INPUT_BRKSIZE+4):
  810. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  811. sprintf(errstr,"Cannot read brksize sent from TK\n");
  812. return(DATA_ERROR);
  813. }
  814. if(inbrksize > 0) {
  815. switch(dz->input_data_type) {
  816. case(WORDLIST_ONLY):
  817. break;
  818. case(PITCH_AND_PITCH):
  819. case(PITCH_AND_TRANSPOS):
  820. case(TRANSPOS_AND_TRANSPOS):
  821. dz->tempsize = inbrksize;
  822. break;
  823. case(BRKFILES_ONLY):
  824. case(UNRANGED_BRKFILE_ONLY):
  825. case(DB_BRKFILES_ONLY):
  826. case(ALL_FILES):
  827. case(ANY_NUMBER_OF_ANY_FILES):
  828. if(dz->extrabrkno < 0) {
  829. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  830. return(DATA_ERROR);
  831. }
  832. if(dz->brksize == NULL) {
  833. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  834. return(PROGRAM_ERROR);
  835. }
  836. dz->brksize[dz->extrabrkno] = inbrksize;
  837. break;
  838. default:
  839. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  840. dz->input_data_type);
  841. return(PROGRAM_ERROR);
  842. }
  843. break;
  844. }
  845. break;
  846. case(INPUT_NUMSIZE+4):
  847. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  848. sprintf(errstr,"Cannot read numsize sent from TK\n");
  849. return(DATA_ERROR);
  850. }
  851. break;
  852. case(INPUT_LINECNT+4):
  853. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  854. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  855. return(DATA_ERROR);
  856. }
  857. break;
  858. case(INPUT_ALL_WORDS+4):
  859. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  860. sprintf(errstr,"Cannot read all_words sent from TK\n");
  861. return(DATA_ERROR);
  862. }
  863. break;
  864. case(INPUT_ARATE+4):
  865. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  866. sprintf(errstr,"Cannot read arate sent from TK\n");
  867. return(DATA_ERROR);
  868. }
  869. break;
  870. case(INPUT_FRAMETIME+4):
  871. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  872. sprintf(errstr,"Cannot read frametime sent from TK\n");
  873. return(DATA_ERROR);
  874. }
  875. dz->frametime = (float)dummy;
  876. break;
  877. case(INPUT_WINDOW_SIZE+4):
  878. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  879. sprintf(errstr,"Cannot read window_size sent from TK\n");
  880. return(DATA_ERROR);
  881. }
  882. break;
  883. case(INPUT_NYQUIST+4):
  884. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  885. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  886. return(DATA_ERROR);
  887. }
  888. break;
  889. case(INPUT_DURATION+4):
  890. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  891. sprintf(errstr,"Cannot read duration sent from TK\n");
  892. return(DATA_ERROR);
  893. }
  894. break;
  895. case(INPUT_MINBRK+4):
  896. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  897. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  898. return(DATA_ERROR);
  899. }
  900. break;
  901. case(INPUT_MAXBRK+4):
  902. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  903. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  904. return(DATA_ERROR);
  905. }
  906. break;
  907. case(INPUT_MINNUM+4):
  908. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  909. sprintf(errstr,"Cannot read minnum sent from TK\n");
  910. return(DATA_ERROR);
  911. }
  912. break;
  913. case(INPUT_MAXNUM+4):
  914. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  915. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  916. return(DATA_ERROR);
  917. }
  918. break;
  919. default:
  920. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  921. return(PROGRAM_ERROR);
  922. }
  923. cnt++;
  924. }
  925. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  926. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  927. return(DATA_ERROR);
  928. }
  929. if(true_cnt)
  930. cnt = true_cnt;
  931. *cmdlinecnt = 0;
  932. while(cnt < argc) {
  933. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  934. return(exit_status);
  935. cnt++;
  936. }
  937. return(FINISHED);
  938. }
  939. /********************************* GET_TK_CMDLINE_WORD *********************************/
  940. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  941. {
  942. if(*cmdlinecnt==0) {
  943. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  944. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  945. return(MEMORY_ERROR);
  946. }
  947. } else {
  948. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  949. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  950. return(MEMORY_ERROR);
  951. }
  952. }
  953. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  954. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  955. return(MEMORY_ERROR);
  956. }
  957. strcpy((*cmdline)[*cmdlinecnt],q);
  958. (*cmdlinecnt)++;
  959. return(FINISHED);
  960. }
  961. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  962. int assign_file_data_storage(int infilecnt,dataptr dz)
  963. {
  964. int exit_status;
  965. int no_sndfile_system_files = FALSE;
  966. dz->infilecnt = infilecnt;
  967. if((exit_status = allocate_filespace(dz))<0)
  968. return(exit_status);
  969. if(no_sndfile_system_files)
  970. dz->infilecnt = 0;
  971. return(FINISHED);
  972. }
  973. /************************* redundant functions: to ensure libs compile OK *******************/
  974. int assign_process_logic(dataptr dz)
  975. {
  976. return(FINISHED);
  977. }
  978. void set_legal_infile_structure(dataptr dz)
  979. {}
  980. int establish_bufptrs_and_extra_buffers(dataptr dz)
  981. {
  982. return(FINISHED);
  983. }
  984. int read_special_data(char *str,dataptr dz)
  985. {
  986. return(FINISHED);
  987. }
  988. int inner_loop
  989. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  990. {
  991. return(FINISHED);
  992. }
  993. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  994. {
  995. return(FINISHED);
  996. }
  997. int setup_internal_arrays_and_array_pointers(dataptr dz)
  998. {
  999. return(FINISHED);
  1000. }
  1001. /******************************** USAGE1 ********************************/
  1002. int usage1(void)
  1003. {
  1004. usage2("zag");
  1005. return(USAGE_ONLY);
  1006. }
  1007. /******************************** DBTOLEVEL ***********************/
  1008. double dbtolevel(double val)
  1009. {
  1010. int isneg = 0;
  1011. if(flteq(val,0.0))
  1012. return(1.0);
  1013. if(val < 0.0) {
  1014. val = -val;
  1015. isneg = 1;
  1016. }
  1017. val /= 20.0;
  1018. val = pow(10.0,val);
  1019. if(isneg)
  1020. val = 1.0/val;
  1021. return(val);
  1022. }
  1023. /********************************************************************************************/
  1024. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1025. {
  1026. if(!strcmp(prog_identifier_from_cmdline,"zag")) dz->process = MCHZIG;
  1027. else {
  1028. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1029. return(USAGE_ONLY);
  1030. }
  1031. return(FINISHED);
  1032. }
  1033. /****************************** GET_MODE *********************************/
  1034. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1035. {
  1036. if(sscanf(str,"%d",&dz->mode)!=1) {
  1037. sprintf(errstr,"Cannot read mode of program.\n");
  1038. return(USAGE_ONLY);
  1039. }
  1040. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1041. sprintf(errstr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1042. return(USAGE_ONLY);
  1043. }
  1044. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1045. return(FINISHED);
  1046. }
  1047. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  1048. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  1049. {
  1050. int n;
  1051. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1052. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  1053. return(MEMORY_ERROR);
  1054. }
  1055. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1056. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  1057. return(MEMORY_ERROR);
  1058. }
  1059. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1060. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  1061. return(MEMORY_ERROR);
  1062. }
  1063. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1064. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  1065. return(MEMORY_ERROR);
  1066. }
  1067. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1068. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  1069. return(MEMORY_ERROR);
  1070. }
  1071. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1072. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  1073. return(MEMORY_ERROR);
  1074. }
  1075. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1076. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  1077. return(MEMORY_ERROR);
  1078. }
  1079. for(n=0;n<brkcnt;n++) {
  1080. dz->brk[n] = NULL;
  1081. dz->brkptr[n] = NULL;
  1082. dz->brkinit[n] = 0;
  1083. dz->brksize[n] = 0;
  1084. }
  1085. return(FINISHED);
  1086. }
  1087. /******************************** USAGE2 ********************************/
  1088. int usage2(char *str)
  1089. {
  1090. if(!strcmp(str,"zag")) {
  1091. fprintf(stderr,
  1092. "USAGE: mchzig zag 1 infile outfile start end dur minzig outchans\n"
  1093. " [-ssplicelen] [-mmaxzig] [-rseed] [-a]\n"
  1094. "OR: mchzig zag 2 infile outfile timefile outchans [-ssplicelen] [-a]\n\n"
  1095. "READ SOUNDFILE BACK AND FORTH, PANNING RANDOMLY BETWEEN OUTPUT CHANS\n\n"
  1096. "MODES\n"
  1097. "1: random zigzags: starts at file start, ends at file end.\n"
  1098. "2: zigzagging follows times supplied by user.\n"
  1099. "\n"
  1100. "PARAMETERS\n"
  1101. "start: together with...\n"
  1102. "end: define interval in which times zigzag.\n"
  1103. "dur: is total duration of output sound required.\n"
  1104. "minzig: is min acceptable time between successive zigzag timepoints.\n"
  1105. "outchans: number of channels in output file.\n"
  1106. "splicelen: in MILLIsecs (Default 25ms).\n"
  1107. "maxzig: is max acceptable time between successive zigzag timepoints\n"
  1108. "seed: number to generate a replicable random sequence. (>0)\n"
  1109. " entering same number on next program run, generates same sequence.\n"
  1110. " (Default: (0) random sequence is different every time).\n"
  1111. "timefile: text file containing sequence of times to zigzag between.\n"
  1112. " Each step-between-times must be > (3 * splicelen).\n"
  1113. " zigsteps moving in the same (time-)direction will be concatenated.\n"
  1114. "-a Avoid zigs between adjacent channels (5 or more outchans only).\n");
  1115. } else
  1116. fprintf(stdout,"Unknown option '%s'\n",str);
  1117. return(USAGE_ONLY);
  1118. }
  1119. int usage3(char *str1,char *str2)
  1120. {
  1121. fprintf(stderr,"Insufficient parameters on command line.\n");
  1122. return(USAGE_ONLY);
  1123. }
  1124. /********************************** CHECK_ZIGZAG_CONSISTENCY **********************************/
  1125. int check_zigzag_consistency(dataptr dz)
  1126. {
  1127. double diff;
  1128. if(dz->mode==ZIGZAG_SELF) {
  1129. if(dz->infile->channels != 1) {
  1130. sprintf(errstr,"mchzig only works with mono files.\n");
  1131. return(DATA_ERROR);
  1132. }
  1133. if(dz->param[MZIG_MAX] <= dz->param[MZIG_MIN]) {
  1134. sprintf(errstr,"maximum zig duration <= minimum zig duration\n");
  1135. return(DATA_ERROR);
  1136. }
  1137. if(dz->param[MZIG_MIN] < (((dz->param[MZIG_SPLEN] * 2) + ZIG_MIN_UNSPLICED) * MS_TO_SECS)) {
  1138. sprintf(errstr,"minimum ziglength must be > %.3lf: cannot proceed\n",
  1139. ((dz->param[MZIG_SPLEN] * 2) + ZIG_MIN_UNSPLICED) * MS_TO_SECS);
  1140. return(DATA_ERROR);
  1141. }
  1142. diff = dz->param[MZIG_END] - dz->param[MZIG_START];
  1143. if(diff<=0.0) {
  1144. sprintf(errstr,"Zig start and end times incompatible.\n");
  1145. return(DATA_ERROR);
  1146. }
  1147. if(round(diff/dz->param[MZIG_MIN])<1) {
  1148. sprintf(errstr,"Zigzagging sector too short for zig-zag minlength specified.\n");
  1149. return(DATA_ERROR);
  1150. }
  1151. }
  1152. if(dz->vflag[0] && (dz->iparam[MZIG_OCHANS] < 5)) {
  1153. sprintf(errstr,"Can't avoid adjacent chans + use all chans, with < 5 outchans.\n");
  1154. return(DATA_ERROR);
  1155. }
  1156. dz->outchans = dz->iparam[MZIG_OCHANS];
  1157. return FINISHED;
  1158. }
  1159. /********************************** ZIGZAG_PREPROCESS **********************************/
  1160. int mzigzag_preprocess(int *maxzig,dataptr dz)
  1161. {
  1162. int exit_status;
  1163. int n = 0;
  1164. if(dz->mode==ZIGZAG_SELF)
  1165. initialise_random_sequence(IS_ZIG_RSEED,MZIG_RSEED,dz);
  1166. if((exit_status = setup_zigzag_splice(MZIG_SPLEN,dz))<0)
  1167. return(exit_status);
  1168. if(dz->insams[0] <= dz->iparam[MZIG_SPLSAMPS] * 2) {
  1169. sprintf(errstr,"Infile too short for splices.\n");
  1170. return(DATA_ERROR);
  1171. }
  1172. if(dz->mode == ZIGZAG_SELF) {
  1173. if((exit_status = generate_zigzag_table(dz))<0)
  1174. return(exit_status);
  1175. }
  1176. if((exit_status = sort_zigs(maxzig,dz))<0)
  1177. return(exit_status);
  1178. if(sloom) {
  1179. dz->tempsize = 0L;
  1180. for(n=1;n<dz->itemcnt;n++)
  1181. /*RWD treat tempszie as in samps */
  1182. dz->tempsize += abs(dz->lparray[ZIGZAG_TIMES][n] - dz->lparray[ZIGZAG_TIMES][n-1]);
  1183. }
  1184. if((dz->iparray[MZIG_PERMCH] = (int *)malloc(dz->iparam[MZIG_OCHANS] * sizeof(int)))==NULL) {
  1185. sprintf(errstr,"INSUFFICIENT MEMORY to make channel perm array.\n");
  1186. return(MEMORY_ERROR);
  1187. }
  1188. return(FINISHED);
  1189. }
  1190. /*********************** SETUP_ZIGZAG_SPLICE ***************************/
  1191. int setup_zigzag_splice(int paramno,dataptr dz)
  1192. {
  1193. int exit_status;
  1194. if((exit_status = make_zigsplice(paramno,dz))<0)
  1195. return(exit_status);
  1196. return create_zigzag_splicebuffer(dz);
  1197. }
  1198. /*********************** MAKE_ZIGSPLICE ***************************/
  1199. int make_zigsplice(int paramno,dataptr dz)
  1200. {
  1201. int n;
  1202. dz->iparam[MZIG_SPLICECNT] = (int)round(dz->param[paramno] * MS_TO_SECS * dz->infile->srate);
  1203. dz->iparam[MZIG_SPLSAMPS] = dz->iparam[MZIG_SPLICECNT];
  1204. if((dz->parray[ZIGZAG_SPLICE] = (double *)malloc(dz->iparam[MZIG_SPLICECNT] * sizeof(double)))==NULL) {
  1205. sprintf(errstr,"INSUFFICIENT MEMORY to make splicer buffer.\n");
  1206. return(MEMORY_ERROR);
  1207. }
  1208. for(n=0;n<dz->iparam[MZIG_SPLICECNT];n++)
  1209. dz->parray[ZIGZAG_SPLICE][n] = (double)n/(double)dz->iparam[MZIG_SPLICECNT];
  1210. return(FINISHED);
  1211. }
  1212. /*********************** CREATE_ZIGZAG_SPLICEBUFFER ***************************/
  1213. int create_zigzag_splicebuffer(dataptr dz)
  1214. {
  1215. if(dz->extrabuf == (float **)0) {
  1216. sprintf(errstr,"extrabuf has not been created: create_zigzag_splicebuffer()\n");
  1217. return(PROGRAM_ERROR);
  1218. }
  1219. if((dz->extrabuf[ZIGZAG_SPLBUF] = (float *)malloc(sizeof(float) * dz->iparam[MZIG_SPLSAMPS]))==NULL) {
  1220. sprintf(errstr,"INSUFFICIENT MEMORY to make splicing buffer.\n");
  1221. return(MEMORY_ERROR);
  1222. }
  1223. memset((char *)dz->extrabuf[ZIGZAG_SPLBUF],0,sizeof(float) * dz->iparam[MZIG_SPLSAMPS]);
  1224. return(FINISHED);
  1225. }
  1226. /***************************** GENERATE_ZIGZAG_TABLE ***************************/
  1227. int generate_zigzag_table(dataptr dz)
  1228. {
  1229. int OK;
  1230. int arraysize = BIGARRAY;
  1231. double infiledur = (double)(dz->insams[0]/dz->infile->channels)/(double)(dz->infile->srate);
  1232. double totaltime = dz->param[MZIG_START];
  1233. double goaltime = dz->param[MZIG_DUR] - (infiledur - dz->param[MZIG_END]);
  1234. double diff, randlen = 0.0, here = dz->param[MZIG_START];
  1235. int direction = FORWARDS;
  1236. if((dz->lparray[ZIGZAG_TIMES] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  1237. sprintf(errstr,"INSUFFICIENT MEMORY to store times.\n");
  1238. return(MEMORY_ERROR);
  1239. }
  1240. dz->lparray[ZIGZAG_TIMES][0] = 0;
  1241. dz->itemcnt = 1;
  1242. do {
  1243. OK = TRUE;
  1244. switch(direction) {
  1245. case(FORWARDS):
  1246. diff = min(dz->param[MZIG_MAX],dz->param[MZIG_END] - here);
  1247. if(diff<=dz->param[MZIG_MIN])
  1248. OK = FALSE;
  1249. else {
  1250. randlen = drand48(); /* generate segment length at random */
  1251. randlen *= (diff - dz->param[MZIG_MIN]); /* scale it to range required */
  1252. randlen += dz->param[MZIG_MIN]; /* and add mindur */
  1253. here = (here + randlen);
  1254. }
  1255. break;
  1256. case(BACKWARDS):
  1257. diff = min(dz->param[MZIG_MAX],here - dz->param[MZIG_START]);
  1258. if(diff<=dz->param[MZIG_MIN])
  1259. OK = FALSE;
  1260. else {
  1261. randlen = drand48(); /* generate segment length at random */
  1262. randlen *= (diff - dz->param[MZIG_MIN]); /* scale it to range required */
  1263. randlen += dz->param[MZIG_MIN]; /* and add mindur */
  1264. here = (here - randlen);
  1265. }
  1266. break;
  1267. }
  1268. direction = -direction; /* invert time-direction */
  1269. if(!OK)
  1270. continue;
  1271. totaltime += randlen;
  1272. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = round(here * (double)dz->infile->srate) * dz->infile->channels;
  1273. if(++dz->itemcnt >= arraysize) {
  1274. arraysize += BIGARRAY;
  1275. if((dz->lparray[ZIGZAG_TIMES] =
  1276. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],arraysize*sizeof(int)))==NULL) {
  1277. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  1278. return(MEMORY_ERROR);
  1279. }
  1280. }
  1281. } while(totaltime<goaltime);
  1282. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = dz->insams[0];
  1283. dz->itemcnt++;
  1284. if(dz->itemcnt < arraysize) {
  1285. if((dz->lparray[ZIGZAG_TIMES] =
  1286. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt*sizeof(int)))==NULL) {
  1287. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  1288. return(MEMORY_ERROR);
  1289. }
  1290. }
  1291. return(FINISHED);
  1292. }
  1293. /****************************** SORT_ZIGS ************************************/
  1294. int sort_zigs(int *maxzig,dataptr dz)
  1295. {
  1296. int exit_status;
  1297. int zigsize;
  1298. int safety = round(ZIG_MIN_UNSPLICED * MS_TO_SECS * (double)dz->infile->srate) * dz->infile->channels;
  1299. int cnt = 0, firstime = TRUE, direction, previous_direction = FORWARDS;
  1300. int *this_zigtime = dz->lparray[ZIGZAG_TIMES];
  1301. int *next_zigtime = dz->lparray[ZIGZAG_TIMES] + 1;
  1302. int *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  1303. int minzig = (dz->iparam[MZIG_SPLSAMPS] * 2) + safety;
  1304. int file_samplen = dz->insams[0];
  1305. double convert_to_time = 1.0/(double)dz->infile->channels/(double)dz->infile->srate;
  1306. if(*this_zigtime < 0 || *this_zigtime > file_samplen) {
  1307. sprintf(errstr,"Invalid 1st zigtime %lf\n",(*this_zigtime) * convert_to_time);
  1308. return(DATA_ERROR);
  1309. }
  1310. if(*(ziglistend-1) >= file_samplen) {
  1311. *(ziglistend-1) = file_samplen;
  1312. dz->iparam[ZIG_RUNSTOEND] = 1;
  1313. } else
  1314. dz->iparam[ZIG_RUNSTOEND] = 0;
  1315. while(next_zigtime < ziglistend - dz->iparam[ZIG_RUNSTOEND]) {
  1316. if(*next_zigtime < 0 || *next_zigtime > file_samplen) {
  1317. sprintf(errstr,"Invalid zigtime %lf\n",(*next_zigtime) * convert_to_time);
  1318. return(DATA_ERROR);
  1319. }
  1320. while((zigsize = abs(*next_zigtime - *this_zigtime)) < minzig) {
  1321. if(++next_zigtime == ziglistend - dz->iparam[ZIG_RUNSTOEND])
  1322. break;
  1323. }
  1324. if(next_zigtime - this_zigtime > 1) {
  1325. if(dz->mode == ZIGZAG_USER) {
  1326. sprintf(errstr,"Some zigs too short to use with specified splicelen.\n");
  1327. return(DATA_ERROR);
  1328. }
  1329. eliminate_n_steps(this_zigtime,next_zigtime,&ziglistend,&cnt,dz);
  1330. next_zigtime = this_zigtime + 1;
  1331. }
  1332. if(*next_zigtime > *this_zigtime)
  1333. direction = FORWARDS;
  1334. else
  1335. direction = BACKWARDS;
  1336. if(!firstime && (direction == previous_direction)) {
  1337. if((exit_status = eliminate_step(next_zigtime,&ziglistend,&cnt,dz))<0)
  1338. return(exit_status);
  1339. continue;
  1340. }
  1341. previous_direction = direction;
  1342. firstime = FALSE;
  1343. this_zigtime++;
  1344. next_zigtime++;
  1345. }
  1346. if(cnt>0) {
  1347. fprintf(stdout,"WARNING: %d steps eliminated (too small relative to spliclength\n",cnt);
  1348. //TW : CAN'T SPLIT LINES SENT TO SLOOM - 'WARNING' is a flag to SLOOM - possibly my error, since updated
  1349. fprintf(stdout,"WARNING: or moving in same direction as previous step)\n");
  1350. fflush(stdout);
  1351. if((dz->lparray[ZIGZAG_TIMES] =
  1352. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  1353. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  1354. return(MEMORY_ERROR);
  1355. }
  1356. }
  1357. if(dz->iparam[ZIG_RUNSTOEND]) {
  1358. if(*(ziglistend-1) - *(ziglistend-2) < dz->iparam[MZIG_SPLSAMPS] + safety) {
  1359. sprintf(errstr,"Final zig too short for splicelen.\n");
  1360. return(GOAL_FAILED);
  1361. }
  1362. }
  1363. get_bufsize_needed(maxzig,dz);
  1364. return FINISHED;
  1365. }
  1366. /**************************** ELIMINATE_N_STEPS ***************************/
  1367. int eliminate_n_steps(int *this_zigtime,int *next_zigtime,int **ziglistend,int *cnt,dataptr dz)
  1368. {
  1369. int *here = this_zigtime + 1;
  1370. int *there = next_zigtime;
  1371. int elimination_cnt = next_zigtime - this_zigtime - 1;
  1372. while(there < *ziglistend) {
  1373. *here = *there;
  1374. here++;
  1375. there++;
  1376. }
  1377. if((dz->itemcnt -= elimination_cnt) < 2) {
  1378. sprintf(errstr,"All zigsteps either too small for splices: or moving in same direction.\n");
  1379. return(DATA_ERROR);
  1380. }
  1381. *ziglistend -= elimination_cnt;
  1382. (*cnt) += elimination_cnt;
  1383. return(FINISHED);
  1384. }
  1385. /***************************** ELIMINATE_STEP ***************************/
  1386. int eliminate_step(int *next_zigtime,int **ziglistend,int *cnt,dataptr dz)
  1387. {
  1388. int *here = next_zigtime;
  1389. while(here < *ziglistend) {
  1390. *(here-1) = *here;
  1391. here++;
  1392. }
  1393. if(--dz->itemcnt < 2) {
  1394. sprintf(errstr,"All zigsteps either too small for splices: or moving in same direction.\n");
  1395. return(DATA_ERROR);
  1396. }
  1397. (*ziglistend)--;
  1398. (*cnt)++;
  1399. return(FINISHED);
  1400. }
  1401. /***************************** INSERT_EXTRA_ZIG ***************************/
  1402. int insert_extra_zig(int direction,int **this_zigtime,int **next_zigtime,int **ziglistend,int len,dataptr dz)
  1403. {
  1404. int *here;
  1405. int zthis = *this_zigtime - dz->lparray[ZIGZAG_TIMES];
  1406. int znext = *next_zigtime - dz->lparray[ZIGZAG_TIMES];
  1407. dz->itemcnt++;
  1408. if((dz->lparray[ZIGZAG_TIMES] =
  1409. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  1410. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate times store.\n");
  1411. return(MEMORY_ERROR);
  1412. }
  1413. *this_zigtime = dz->lparray[ZIGZAG_TIMES] + zthis;
  1414. *next_zigtime = dz->lparray[ZIGZAG_TIMES] + znext;
  1415. *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  1416. here = *ziglistend - 1;
  1417. while(here > *next_zigtime) {
  1418. *here = *(here-1);
  1419. here--;
  1420. }
  1421. switch(direction) {
  1422. case(FORWARDS):
  1423. *here = **this_zigtime + len;
  1424. break;
  1425. case(BACKWARDS):
  1426. *here = **this_zigtime - len;
  1427. break;
  1428. }
  1429. if(*here < 0.0 || (int)*here > dz->insams[0]) {
  1430. sprintf(errstr,"Error in logic of sample arithmetic: insert_extra_zig()\n");
  1431. return(PROGRAM_ERROR);
  1432. }
  1433. return(FINISHED);
  1434. }
  1435. /**************************** GET_BUFSIZE_NEEDED ***************************/
  1436. void get_bufsize_needed(int *maxzig,dataptr dz)
  1437. {
  1438. int *zigtime = dz->lparray[ZIGZAG_TIMES], n, ziglen;
  1439. *maxzig = 0;
  1440. for(n = 1; n < dz->itemcnt;n++) {
  1441. ziglen = abs(zigtime[n] - zigtime[n-1]);
  1442. if(ziglen > *maxzig)
  1443. *maxzig = ziglen;
  1444. }
  1445. }
  1446. /***************************** ZIGZAG **********************************/
  1447. int zigzag(dataptr dz)
  1448. {
  1449. int exit_status;
  1450. int *perm = dz->iparray[MZIG_PERMCH];
  1451. int *thisstart, *lastend = dz->lparray[ZIGZAG_TIMES], minsamp;
  1452. int permno, chancnt = dz->iparam[MZIG_OCHANS],startchan,endchan;
  1453. int *ziglistend = dz->lparray[ZIGZAG_TIMES] + dz->itemcnt;
  1454. int outbuf_space = dz->buflen, samps_left;
  1455. int obufno, obufendno;
  1456. obufno = 2; // The last chancnt bufs are the multichan outbuf
  1457. obufendno = 2 + chancnt; // which starts at dz->sampbuf[2];
  1458. // dz->sampbuf[2+chancnt] marks the outbuf end
  1459. thisstart = lastend;
  1460. thisstart++;
  1461. if((exit_status = find_zzchunk(thisstart,ziglistend,&minsamp,dz))!=CONTINUE) {
  1462. if(exit_status == FINISHED)
  1463. exit_status = GOAL_FAILED;
  1464. sprintf(errstr,"WARNING: No valid zigzag found\n");
  1465. return(exit_status);
  1466. }
  1467. lastend = thisstart;
  1468. thisstart++;
  1469. if(sndseekEx(dz->ifd[0],minsamp,0)<0) {
  1470. sprintf(errstr,"Seek Anomaly 1()\n");
  1471. return(PROGRAM_ERROR);
  1472. }
  1473. if((exit_status = read_samps(dz->sampbuf[0],dz))<0)
  1474. return(exit_status);
  1475. endchan = -1;
  1476. do {
  1477. permute_chans(chancnt,dz);
  1478. } while(adjacence(endchan,dz));
  1479. startchan = perm[0];
  1480. endchan = perm[1];
  1481. permno = 2;
  1482. while(thisstart < ziglistend-1) {
  1483. if((exit_status = zig_or_zag(thisstart,lastend,&outbuf_space,obufno,obufendno,startchan,endchan,dz))<0)
  1484. return(exit_status);
  1485. startchan = endchan;
  1486. endchan = perm[permno];
  1487. permno++;
  1488. if(permno >= dz->iparam[MZIG_OCHANS]) {
  1489. do {
  1490. permute_chans(dz->iparam[MZIG_OCHANS],dz);
  1491. } while((perm[0] == endchan) || adjacence(endchan,dz));
  1492. permno = 0;
  1493. }
  1494. if((exit_status = find_zzchunk(thisstart,ziglistend,&minsamp,dz))!=CONTINUE) {
  1495. if(exit_status==FINISHED)
  1496. break;
  1497. else
  1498. return(exit_status);
  1499. }
  1500. if(sndseekEx(dz->ifd[0],minsamp,0)<0) {
  1501. sprintf(errstr,"Seek Anomaly 2()\n");
  1502. return(PROGRAM_ERROR);
  1503. }
  1504. if((exit_status = read_samps(dz->sampbuf[0],dz))<0)
  1505. return(exit_status);
  1506. lastend = thisstart;
  1507. thisstart++;
  1508. }
  1509. samps_left = dz->sbufptr[obufno] - dz->sampbuf[obufno];
  1510. if(samps_left > 0)
  1511. return write_samps(dz->sampbuf[obufno],samps_left,dz);
  1512. return FINISHED;
  1513. }
  1514. /************************* DO_ZIGZAGS ************************************/
  1515. int zig_or_zag(int *thisstart,int *lastend,int *outbuf_space,int obufno,int obufendno,int startchan,int endchan,dataptr dz)
  1516. {
  1517. int exit_status;
  1518. int *here = thisstart;
  1519. int *next = thisstart + 1;
  1520. int chancnt = dz->iparam[MZIG_OCHANS];
  1521. int incnt = abs(*next - *here);
  1522. float *write_limit;
  1523. int wraplen = dz->iparam[MZIG_SPLICECNT] * chancnt;
  1524. if(*here < *next) {
  1525. // direction = ZIG;
  1526. memcpy((char *)dz->sampbuf[1],(char *)dz->sampbuf[0],incnt * sizeof(float));
  1527. } else {
  1528. //direction = ZAG;
  1529. if((exit_status = reverse_it(incnt,dz))<0)
  1530. return(exit_status);
  1531. }
  1532. do_start_splice(dz->sampbuf[1],dz);
  1533. do_end_splice(dz->sampbuf[1],incnt,dz);
  1534. write_limit = dz->sampbuf[obufendno] + wraplen; // Allows for wrap-around-area at end of obuf
  1535. if((exit_status = copy_with_spatial_scatter(dz->sbufptr[obufno],write_limit,startchan,endchan,incnt,chancnt,dz))<0)
  1536. return(exit_status);
  1537. dz->sbufptr[obufno] -= wraplen;
  1538. return(FINISHED);
  1539. }
  1540. /*************************** FIND_ZZCHUNK ************************************/
  1541. int find_zzchunk(int *thisstart,int *ziglistend, int *minsamp, dataptr dz)
  1542. {
  1543. int *p, min_samp, max_samp;
  1544. p = thisstart;
  1545. /* WE'RE SEARCHING FOR THE earliest and latest times among successive zigtimes */
  1546. min_samp = *p; /* so preset both of these to the current first zigtime */
  1547. max_samp = *p;
  1548. p++;
  1549. if(p == ziglistend)
  1550. return FINISHED;
  1551. if(*p < min_samp) /* if this zigtime earlier than any encountered so far */
  1552. min_samp = *p;
  1553. else if(*p > max_samp) /* if this zigtime later than any encountered so far */
  1554. max_samp = *p;
  1555. if(max_samp - min_samp < 0) {
  1556. sprintf(errstr,"Anomaly in find_zzchunk()\n");
  1557. return(PROGRAM_ERROR);
  1558. }
  1559. *minsamp = min_samp;
  1560. return(CONTINUE);
  1561. }
  1562. /********************** REVERSE_IT ***************************/
  1563. int reverse_it(int incnt,dataptr dz)
  1564. {
  1565. int n;
  1566. float *s0ptr = dz->sampbuf[0] + incnt - 1;
  1567. float *s1ptr = dz->sampbuf[1];
  1568. for(n=0;n<incnt;n++)
  1569. *s1ptr++ = *s0ptr--;
  1570. return(FINISHED);
  1571. }
  1572. /********************** COPY_WITH_SPATIAL_SCATTER ***************************/
  1573. int copy_with_spatial_scatter(float *outptr,float *tobufend,int startchan,int endchan,int sampcnt,int chancnt,dataptr dz)
  1574. {
  1575. int exit_status;
  1576. int panpos, n;
  1577. double pos, leftgain, rightgain;
  1578. int outchans = dz->iparam[MZIG_OCHANS];
  1579. int obufsamps = dz->buflen * chancnt; // Samples in a full outbuffer
  1580. int wrapsamps = dz->iparam[MZIG_SPLICECNT] * outchans; // Samples in wraparound seg after obufend
  1581. float *toptr = outptr; // Location of next write in obuffer
  1582. float *fromptr = dz->sampbuf[1]; // Location of first read in input buffer
  1583. float *obuf_wrap = dz->sampbuf[2 + outchans]; // Start of wrap-around segment
  1584. if(sampcnt < 0) {
  1585. sprintf(errstr,"Attempted to copy -ve number of samps: copy_with_spatial_scatter()\n");
  1586. return(PROGRAM_ERROR);
  1587. }
  1588. panpos = 0;
  1589. for(n = 0;n<sampcnt;n++) {
  1590. if(toptr >= tobufend) {
  1591. if((exit_status = write_samps(dz->sampbuf[2],obufsamps,dz))<0)
  1592. return exit_status;
  1593. memset((char *)dz->sampbuf[2],0,obufsamps * sizeof(float)); // clear the outbuf,
  1594. memcpy((char *)dz->sampbuf[2],(char *)obuf_wrap,wrapsamps * sizeof(float)); // wrap_around any buffer overflow,
  1595. memset((char *)obuf_wrap,0,wrapsamps * sizeof(float)); // THEN clear the wrap-around segment.
  1596. toptr -= obufsamps; // Baktrak output pointer by a full buflen.
  1597. }
  1598. pos = (double)panpos/(double)sampcnt; // Fraction of time-distance into zig
  1599. pos = (pos * 2.0) - 1.0; // Change to -1 to + 1 frame
  1600. pancalc(pos,&leftgain,&rightgain);
  1601. toptr[startchan] = (float)(toptr[startchan] + (fromptr[n] * leftgain));
  1602. toptr[endchan] = (float)(toptr[endchan] + (fromptr[n] * rightgain));
  1603. toptr += outchans;
  1604. panpos++;
  1605. }
  1606. dz->sbufptr[2] = toptr;
  1607. return FINISHED;
  1608. }
  1609. /********************** DO_START_SPLICE ***************************/
  1610. void do_start_splice(float *buf,dataptr dz)
  1611. {
  1612. int n;
  1613. for(n=0;n < dz->iparam[MZIG_SPLICECNT];n++)
  1614. buf[n] = (float)(buf[n] * dz->parray[ZIGZAG_SPLICE][n]);
  1615. }
  1616. /********************** DO_END_SPLICE ***************************/
  1617. void do_end_splice(float *buf,int incnt,dataptr dz)
  1618. {
  1619. int n, m;
  1620. int splicecnt = dz->iparam[MZIG_SPLICECNT];
  1621. buf += incnt - splicecnt;
  1622. for(n=0,m = splicecnt - 1;n < splicecnt;n++,m--)
  1623. buf[n] = (float)(buf[n] * dz->parray[ZIGZAG_SPLICE][m]);
  1624. }
  1625. /*************************** PERMUTE_CHANS ***************************/
  1626. void permute_chans(int outchans,dataptr dz)
  1627. {
  1628. int n, t;
  1629. for(n=0;n<outchans;n++) {
  1630. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  1631. if(t==n)
  1632. prefixch(n,outchans,dz);
  1633. else
  1634. insertch(n,t,outchans,dz);
  1635. }
  1636. }
  1637. /****************************** INSERTCH ****************************/
  1638. void insertch(int n,int t,int outchans,dataptr dz)
  1639. {
  1640. shuflupch(t+1,outchans,dz);
  1641. dz->iparray[MZIG_PERMCH][t+1] = n;
  1642. }
  1643. /****************************** PREFIX ****************************/
  1644. void prefixch(int n,int outchans,dataptr dz)
  1645. {
  1646. shuflupch(0,outchans,dz);
  1647. dz->iparray[MZIG_PERMCH][0] = n;
  1648. }
  1649. /****************************** SHUFLUPCH ****************************/
  1650. void shuflupch(int k,int outchans,dataptr dz)
  1651. {
  1652. int n;
  1653. for(n = outchans - 1; n > k; n--)
  1654. dz->iparray[MZIG_PERMCH][n] = dz->iparray[MZIG_PERMCH][n-1];
  1655. }
  1656. /***************************** SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS **************************/
  1657. int mz_setup_internal_arrays_and_array_pointers(dataptr dz)
  1658. {
  1659. int n;
  1660. dz->array_cnt = 1;
  1661. dz->iarray_cnt = 1;
  1662. dz->larray_cnt = 1;
  1663. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  1664. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  1665. return(MEMORY_ERROR);
  1666. }
  1667. for(n=0;n<dz->array_cnt;n++)
  1668. dz->parray[n] = NULL;
  1669. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  1670. sprintf(errstr,"INSUFFICIENT MEMORY for internal int arrays.\n");
  1671. return(MEMORY_ERROR);
  1672. }
  1673. for(n=0;n<dz->iarray_cnt;n++)
  1674. dz->iparray[n] = NULL;
  1675. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  1676. sprintf(errstr,"INSUFFICIENT MEMORY for internal long arrays.\n");
  1677. return(MEMORY_ERROR);
  1678. }
  1679. for(n=0;n<dz->larray_cnt;n++)
  1680. dz->lparray[n] = NULL;
  1681. return(FINISHED);
  1682. }
  1683. /************************************ PANCALC *******************************/
  1684. void pancalc(double position,double *leftgain,double *rightgain)
  1685. {
  1686. int signal_to_left = 0;
  1687. double temp;
  1688. double relpos;
  1689. double reldist, invsquare;
  1690. if(position < 0.0)
  1691. signal_to_left = 1; /* signal on left */
  1692. if(position < 0)
  1693. relpos = -position;
  1694. else
  1695. relpos = position;
  1696. if(relpos <= 1.0){ /* between the speakers */
  1697. temp = 1.0 + (relpos * relpos);
  1698. reldist = ROOT2 / sqrt(temp);
  1699. temp = (position + 1.0) / 2.0;
  1700. *rightgain = temp * reldist;
  1701. *leftgain = (1.0 - temp ) * reldist;
  1702. } else { /* outside the speakers */
  1703. temp = (relpos * relpos) + 1.0;
  1704. reldist = sqrt(temp) / ROOT2; /* relative distance to source */
  1705. invsquare = 1.0 / (reldist * reldist);
  1706. if(signal_to_left){
  1707. *leftgain = invsquare;
  1708. *rightgain = 0.0;
  1709. } else { /* SIGNAL_TO_RIGHT */
  1710. *rightgain = invsquare;
  1711. *leftgain = 0;
  1712. }
  1713. }
  1714. }
  1715. /************************ HANDLE_THE_SPECIAL_DATA *********************/
  1716. int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz)
  1717. {
  1718. int exit_status;
  1719. aplptr ap = dz->application;
  1720. if(ap->special_data) {
  1721. if(!sloom) {
  1722. if(*cmdlinecnt <= 0) {
  1723. sprintf(errstr,"Insufficient parameters on command line.\n");
  1724. return(USAGE_ONLY);
  1725. }
  1726. }
  1727. if((exit_status = setup_the_special_data_ranges
  1728. (dz->mode,dz->infile->srate,dz->duration,dz->nyquist,dz->wlength,dz->infile->channels,ap))<0)
  1729. return(exit_status);
  1730. if((exit_status = read_ziginfo((*cmdline)[0],dz))<0)
  1731. return(exit_status);
  1732. (*cmdline)++;
  1733. (*cmdlinecnt)--;
  1734. }
  1735. return(FINISHED);
  1736. }
  1737. /************************ SETUP_SPECIAL_DATA_RANGES *********************/
  1738. int setup_the_special_data_ranges(int mode,int srate,double duration,double nyquist,int wlength,int channels,aplptr ap)
  1739. {
  1740. ap->data_in_file_only = TRUE;
  1741. ap->special_range = TRUE;
  1742. ap->min_special = MZIG_SPLICELEN * MS_TO_SECS * 3;
  1743. ap->max_special = duration;
  1744. return(FINISHED);
  1745. }
  1746. /***************************** READ_ZIGINFO ***************************/
  1747. int read_ziginfo(char *filename,dataptr dz)
  1748. {
  1749. FILE *fp;
  1750. double p;
  1751. char temp[200], *q;
  1752. int arraysize = BIGARRAY, sampcnt;
  1753. int maxlong = /*getmaxlong()*/0x7fffffff;
  1754. dz->itemcnt = 0;
  1755. if((fp = fopen(filename,"r"))==NULL) {
  1756. sprintf(errstr,"Can't open text file %s to read.\n",filename);
  1757. return(DATA_ERROR);
  1758. }
  1759. if((dz->lparray[ZIGZAG_TIMES] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  1760. sprintf(errstr,"INSUFFICIENT MEMORY to store zigzag times.\n");
  1761. return(MEMORY_ERROR);
  1762. }
  1763. while(fgets(temp,200,fp)!=NULL) {
  1764. q = temp;
  1765. while(get_float_from_within_string(&q,&p)){
  1766. if(p < 0.0) {
  1767. sprintf(errstr,"Invalid zigzag time, less than zero\n");
  1768. return(DATA_ERROR);
  1769. }
  1770. if((sampcnt = round(p * (double)dz->infile->srate) * dz->infile->channels) < 0) /* overflow */
  1771. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = (maxlong/dz->infile->channels) * dz->infile->channels;
  1772. else
  1773. dz->lparray[ZIGZAG_TIMES][dz->itemcnt] = sampcnt;
  1774. if(++dz->itemcnt >= arraysize) {
  1775. arraysize += BIGARRAY;
  1776. if((dz->lparray[ZIGZAG_TIMES] =
  1777. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],arraysize*sizeof(int)))==NULL) {
  1778. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate zigzag times.\n");
  1779. return(MEMORY_ERROR);
  1780. }
  1781. }
  1782. }
  1783. }
  1784. if(!dz->itemcnt) {
  1785. sprintf(errstr,"No data in file %s\n",filename);
  1786. return(DATA_ERROR);
  1787. }
  1788. if((dz->lparray[ZIGZAG_TIMES] =
  1789. (int *)realloc((char *)dz->lparray[ZIGZAG_TIMES],dz->itemcnt * sizeof(int)))==NULL) {
  1790. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate zigzag times.\n");
  1791. return(MEMORY_ERROR);
  1792. }
  1793. if(fclose(fp)<0) {
  1794. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  1795. fflush(stdout);
  1796. }
  1797. if(dz->itemcnt < 2) {
  1798. sprintf(errstr,"Not enough zig information found in file %s\n",filename);
  1799. return(DATA_ERROR);
  1800. }
  1801. return(FINISHED);
  1802. }
  1803. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1804. {
  1805. int exit_status;
  1806. switch(mode) {
  1807. case(0): exit_status = set_internalparam_data("iii" ,ap); break;
  1808. case(1): exit_status = set_internalparam_data("diiii",ap); break;
  1809. default:
  1810. sprintf(errstr,"Unknown mode for zigzag: set_legal_internalparam_structure()\n");
  1811. return(PROGRAM_ERROR);
  1812. }
  1813. return(exit_status);
  1814. }
  1815. /*************************** CREATE_MZIG_SNDBUFS **************************/
  1816. /* 2009 MULTICHANNEL */
  1817. int create_mzig_sndbufs(int maxzig,dataptr dz)
  1818. {
  1819. int n;
  1820. int bigbufsize, totalbufsize;
  1821. int safety = 20;
  1822. int outchans = dz->iparam[MZIG_OCHANS];
  1823. int wraplen = dz->iparam[MZIG_SPLICECNT] * outchans;
  1824. //framesize = F_SECSIZE * dz->infile->channels;
  1825. if(dz->sbufptr == 0 || dz->sampbuf==0) {
  1826. sprintf(errstr,"buffer pointers not allocated: create_sndbufs()\n");
  1827. return(PROGRAM_ERROR);
  1828. }
  1829. bigbufsize = ((maxzig + safety)* dz->bufcnt) * sizeof(float);
  1830. if(bigbufsize <= 0) {
  1831. sprintf(errstr,"maximum zig too large for available moeory\n");
  1832. return(DATA_ERROR);
  1833. }
  1834. bigbufsize /= dz->bufcnt;
  1835. dz->buflen = bigbufsize / sizeof(float);
  1836. totalbufsize = (bigbufsize * dz->bufcnt);
  1837. totalbufsize += (wraplen * sizeof(float)) + safety; // NB This creates a splicelen(+) segment of buffer beyond outbuf end
  1838. if((dz->bigbuf = (float *)malloc(totalbufsize)) == NULL) { // Which is used to wrap-around samples, into outbuf, at write_samps
  1839. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n"); // This is needed because there is a splicelen backtrack
  1840. return(PROGRAM_ERROR); // before every zig write to the outbuf.
  1841. }
  1842. for(n=0;n<dz->bufcnt;n++)
  1843. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1844. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1845. return(FINISHED);
  1846. }
  1847. /*************************** ADJACENCE **************************/
  1848. int adjacence(int endchan,dataptr dz)
  1849. {
  1850. int *perm = dz->iparray[MZIG_PERMCH];
  1851. int i, j, ochans = dz->iparam[MZIG_OCHANS];
  1852. int lastchan = ochans - 1;
  1853. if(dz->vflag[0] == 0)
  1854. return 0;
  1855. if(endchan >= 0) {
  1856. if ((perm[0] == 0) && (endchan == lastchan))
  1857. return 1;
  1858. else if((endchan == 0) && (perm[0] == lastchan))
  1859. return 1;
  1860. else if(perm[0] - endchan == 1)
  1861. return 1;
  1862. else if(endchan - perm[0] == 1)
  1863. return 1;
  1864. }
  1865. for(i=0;i < ochans;i++) {
  1866. j = (i + 1) % ochans;
  1867. if ((perm[i] == 0) && (perm[j] == lastchan))
  1868. return 1;
  1869. else if((perm[j] == 0) && (perm[i] == lastchan))
  1870. return 1;
  1871. else if(perm[j] - perm[i] == 1)
  1872. return 1;
  1873. else if(perm[i] - perm[j] == 1)
  1874. return 1;
  1875. }
  1876. return 0;
  1877. }