shifter.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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. /*
  22. * 2 MODES : mode 0: 1 infile
  23. * mode 1: 1 infile per cycle
  24. * Program sorts cycles into ascending order, and assumes cycles (say 11,12,13) assocd with infiles 0,1,2
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <structures.h>
  29. #include <tkglobals.h>
  30. #include <pnames.h>
  31. #include <filetype.h>
  32. #include <processno.h>
  33. #include <modeno.h>
  34. #include <logic.h>
  35. #include <globcon.h>
  36. #include <cdpmain.h>
  37. #include <math.h>
  38. #include <mixxcon.h>
  39. #include <osbind.h>
  40. #include <standalone.h>
  41. #include <ctype.h>
  42. #include <sfsys.h>
  43. #include <string.h>
  44. #include <srates.h>
  45. #define SIGNAL_TO_LEFT (0)
  46. #define SIGNAL_TO_RIGHT (1)
  47. #define ROOT2 (1.4142136)
  48. #define dupl descriptor_samps
  49. #ifdef unix
  50. #define round(x) lround((x))
  51. #endif
  52. char errstr[2400];
  53. int anal_infiles = 1;
  54. int sloom = 0;
  55. int sloombatch = 0;
  56. const char* cdp_version = "7.1.0";
  57. //CDP LIB REPLACEMENTS
  58. static int check_shifter_param_validity_and_consistency(dataptr dz);
  59. static int setup_shifter_application(dataptr dz);
  60. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  61. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  62. static int setup_shifter_param_ranges_and_defaults(dataptr dz);
  63. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  64. static int open_the_outfile(dataptr dz);
  65. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  66. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  67. static int establish_application(dataptr dz);
  68. static int initialise_vflags(dataptr dz);
  69. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  70. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  71. static int mark_parameter_types(dataptr dz,aplptr ap);
  72. static int assign_file_data_storage(int infilecnt,dataptr dz);
  73. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  74. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  75. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  76. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  77. static int shifter(dataptr dz);
  78. static int create_shifter_sndbufs(dataptr dz);
  79. static int handle_the_special_data(char *str,dataptr dz);
  80. static void gen_times_array(int a, int b, int *itimes, int cycledur, dataptr dz);
  81. static void gen_regular_times_array(int a, int *itimes, double cycledur,dataptr dz);
  82. static int get_max_overlap(int maxcycle,int maxinfiledur,int * cycle,dataptr dz);
  83. static void pancalc(double position,double *leftgain,double *rightgain);
  84. static void rndintperm(int *perm,int cnt);
  85. static int shifter_param_preprocess(dataptr dz);
  86. /**************************************** MAIN *********************************************/
  87. int main(int argc,char *argv[])
  88. {
  89. int exit_status;
  90. dataptr dz = NULL;
  91. char **cmdline;
  92. int cmdlinecnt;
  93. //aplptr ap;
  94. int is_launched = FALSE;
  95. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  96. fprintf(stdout,"%s\n",cdp_version);
  97. fflush(stdout);
  98. return 0;
  99. }
  100. /* CHECK FOR SOUNDLOOM */
  101. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  102. sloom = 0;
  103. sloombatch = 1;
  104. }
  105. if(sflinit("cdp")){
  106. sfperror("cdp: initialisation\n");
  107. return(FAILED);
  108. }
  109. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  110. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  111. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  112. return(FAILED);
  113. }
  114. if(!sloom) {
  115. if(argc == 1) {
  116. usage1();
  117. return(FAILED);
  118. } else if(argc == 2) {
  119. usage2(argv[1]);
  120. return(FAILED);
  121. }
  122. }
  123. if(!sloom) {
  124. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  125. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  126. return(FAILED);
  127. }
  128. cmdline = argv;
  129. cmdlinecnt = argc;
  130. if((get_the_process_no(argv[0],dz))<0)
  131. return(FAILED);
  132. cmdline++;
  133. cmdlinecnt--;
  134. dz->maxmode = 2;
  135. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  136. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  137. return(exit_status);
  138. }
  139. cmdline++;
  140. cmdlinecnt--;
  141. // setup_particular_application =
  142. if((exit_status = setup_shifter_application(dz))<0) {
  143. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  144. return(FAILED);
  145. }
  146. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  147. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  148. return(FAILED);
  149. }
  150. } else {
  151. //parse_TK_data() =
  152. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  153. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  154. return(exit_status);
  155. }
  156. }
  157. //ap = dz->application;
  158. // parse_infile_and_hone_type() =
  159. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  160. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  161. return(FAILED);
  162. }
  163. // setup_param_ranges_and_defaults() =
  164. if((exit_status = setup_shifter_param_ranges_and_defaults(dz))<0) {
  165. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  166. return(FAILED);
  167. }
  168. // open_first_infile CDP LIB
  169. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  170. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  171. return(FAILED);
  172. }
  173. cmdlinecnt--;
  174. cmdline++;
  175. if(dz->infilecnt > 1) {
  176. if((exit_status = handle_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  177. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  178. return(FAILED);
  179. }
  180. }
  181. // handle_outfile() =
  182. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  183. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  184. return(FAILED);
  185. }
  186. if((exit_status = handle_the_special_data(cmdline[0],dz))<0) {
  187. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  188. return(FAILED);
  189. }
  190. cmdlinecnt--;
  191. cmdline++;
  192. // handle_formants() redundant
  193. // handle_formant_quiksearch() redundant
  194. // handle_special_data() redundant
  195. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  196. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  197. return(FAILED);
  198. }
  199. // check_param_validity_and_consistency....
  200. if((exit_status = check_shifter_param_validity_and_consistency(dz))<0) {
  201. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  202. return(FAILED);
  203. }
  204. if((exit_status = open_the_outfile(dz))<0) {
  205. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  206. return(FAILED);
  207. }
  208. is_launched = TRUE;
  209. if((exit_status = create_shifter_sndbufs(dz))<0) {
  210. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  211. return(FAILED);
  212. }
  213. if((exit_status = shifter_param_preprocess(dz))<0) {
  214. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  215. return(FAILED);
  216. }
  217. //spec_process_file =
  218. if((exit_status = shifter(dz))<0) {
  219. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  220. return(FAILED);
  221. }
  222. if((exit_status = complete_output(dz))<0) { // CDP LIB
  223. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  224. return(FAILED);
  225. }
  226. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  227. free(dz);
  228. return(SUCCEEDED);
  229. }
  230. /**********************************************
  231. REPLACED CDP LIB FUNCTIONS
  232. **********************************************/
  233. /****************************** SET_PARAM_DATA *********************************/
  234. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  235. {
  236. ap->special_data = (char)special_data;
  237. ap->param_cnt = (char)paramcnt;
  238. ap->max_param_cnt = (char)maxparamcnt;
  239. if(ap->max_param_cnt>0) {
  240. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  241. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  242. return(MEMORY_ERROR);
  243. }
  244. strcpy(ap->param_list,paramlist);
  245. }
  246. return(FINISHED);
  247. }
  248. /****************************** SET_VFLGS *********************************/
  249. int set_vflgs
  250. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  251. {
  252. ap->option_cnt = (char) optcnt; /*RWD added cast */
  253. if(optcnt) {
  254. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  255. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  256. return(MEMORY_ERROR);
  257. }
  258. strcpy(ap->option_list,optlist);
  259. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  260. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  261. return(MEMORY_ERROR);
  262. }
  263. strcpy(ap->option_flags,optflags);
  264. }
  265. ap->vflag_cnt = (char) vflagcnt;
  266. ap->variant_param_cnt = (char) vparamcnt;
  267. if(vflagcnt) {
  268. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  269. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  270. return(MEMORY_ERROR);
  271. }
  272. strcpy(ap->variant_list,varlist);
  273. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  274. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  275. return(MEMORY_ERROR);
  276. }
  277. strcpy(ap->variant_flags,varflags);
  278. }
  279. return(FINISHED);
  280. }
  281. /***************************** APPLICATION_INIT **************************/
  282. int application_init(dataptr dz)
  283. {
  284. int exit_status;
  285. int storage_cnt;
  286. int tipc, brkcnt;
  287. aplptr ap = dz->application;
  288. if(ap->vflag_cnt>0)
  289. initialise_vflags(dz);
  290. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  291. ap->total_input_param_cnt = (char)tipc;
  292. if(tipc>0) {
  293. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  294. return(exit_status);
  295. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  296. return(exit_status);
  297. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  298. return(exit_status);
  299. }
  300. brkcnt = tipc;
  301. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  302. if(brkcnt>0) {
  303. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  304. return(exit_status);
  305. }
  306. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  307. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  308. return(exit_status);
  309. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  310. return(exit_status);
  311. }
  312. if((exit_status = mark_parameter_types(dz,ap))<0)
  313. return(exit_status);
  314. // establish_infile_constants() replaced by
  315. dz->infilecnt = 1;
  316. //establish_bufptrs_and_extra_buffers():
  317. return(FINISHED);
  318. }
  319. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  320. /* RWD mallo changed to calloc; helps debug verison run as release! */
  321. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  322. {
  323. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  324. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  325. return(MEMORY_ERROR);
  326. }
  327. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  328. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  329. return(MEMORY_ERROR);
  330. }
  331. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  332. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  333. return(MEMORY_ERROR);
  334. }
  335. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  336. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  337. return(MEMORY_ERROR);
  338. }
  339. return(FINISHED);
  340. }
  341. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  342. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  343. {
  344. int n;
  345. for(n=0;n<storage_cnt;n++) {
  346. dz->is_int[n] = (char)0;
  347. dz->no_brk[n] = (char)0;
  348. }
  349. return(FINISHED);
  350. }
  351. /***************************** MARK_PARAMETER_TYPES **************************/
  352. int mark_parameter_types(dataptr dz,aplptr ap)
  353. {
  354. int n, m; /* PARAMS */
  355. for(n=0;n<ap->max_param_cnt;n++) {
  356. switch(ap->param_list[n]) {
  357. case('0'): break; /* dz->is_active[n] = 0 is default */
  358. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  359. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  360. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  361. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  362. default:
  363. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  364. return(PROGRAM_ERROR);
  365. }
  366. } /* OPTIONS */
  367. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  368. switch(ap->option_list[n]) {
  369. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  370. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  371. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  372. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  373. default:
  374. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  375. return(PROGRAM_ERROR);
  376. }
  377. } /* VARIANTS */
  378. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  379. switch(ap->variant_list[n]) {
  380. case('0'): break;
  381. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  382. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  383. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  384. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  385. default:
  386. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  387. return(PROGRAM_ERROR);
  388. }
  389. } /* INTERNAL */
  390. for(n=0,
  391. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  392. switch(ap->internal_param_list[n]) {
  393. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  394. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  395. case('d'): dz->no_brk[m] = (char)1; break;
  396. default:
  397. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  398. return(PROGRAM_ERROR);
  399. }
  400. }
  401. return(FINISHED);
  402. }
  403. /***************************** ESTABLISH_APPLICATION **************************/
  404. int establish_application(dataptr dz)
  405. {
  406. aplptr ap;
  407. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  408. sprintf(errstr,"establish_application()\n");
  409. return(MEMORY_ERROR);
  410. }
  411. ap = dz->application;
  412. memset((char *)ap,0,sizeof(struct applic));
  413. return(FINISHED);
  414. }
  415. /************************* INITIALISE_VFLAGS *************************/
  416. int initialise_vflags(dataptr dz)
  417. {
  418. int n;
  419. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  420. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  421. return(MEMORY_ERROR);
  422. }
  423. for(n=0;n<dz->application->vflag_cnt;n++)
  424. dz->vflag[n] = FALSE;
  425. return FINISHED;
  426. }
  427. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  428. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  429. {
  430. int n;
  431. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  432. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  433. return(MEMORY_ERROR);
  434. }
  435. for(n=0;n<tipc;n++)
  436. ap->default_val[n] = 0.0;
  437. return(FINISHED);
  438. }
  439. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  440. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  441. {
  442. int n;
  443. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  444. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  445. return(MEMORY_ERROR);
  446. }
  447. for(n=0;n<tipc;n++)
  448. dz->is_active[n] = (char)0;
  449. return(FINISHED);
  450. }
  451. /************************* SETUP_SHIFTER_APPLICATION *******************/
  452. int setup_shifter_application(dataptr dz)
  453. {
  454. int exit_status;
  455. aplptr ap;
  456. if((exit_status = establish_application(dz))<0) // GLOBAL
  457. return(FAILED);
  458. ap = dz->application;
  459. // SEE parstruct FOR EXPLANATION of next 2 functions
  460. if((exit_status = set_param_data(ap,SHFCYCLES,7,7,"ddiiiid"))<0)
  461. return(FAILED);
  462. if((exit_status = set_vflgs(ap,"",0,"","zrl",3,0,"000"))<0)
  463. return(FAILED);
  464. // set_legal_infile_structure -->
  465. dz->has_otherfile = FALSE;
  466. // assign_process_logic -->
  467. if(dz->mode == 0)
  468. dz->input_data_type = SNDFILES_ONLY;
  469. else
  470. dz->input_data_type = MANY_SNDFILES;
  471. dz->process_type = UNEQUAL_SNDFILE;
  472. dz->outfiletype = SNDFILE_OUT;
  473. return application_init(dz); //GLOBAL
  474. }
  475. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  476. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  477. {
  478. int exit_status;
  479. infileptr infile_info;
  480. if(!sloom) {
  481. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  482. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  483. return(MEMORY_ERROR);
  484. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  485. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  486. return(PROGRAM_ERROR);
  487. } else if(infile_info->filetype != SNDFILE) {
  488. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  489. return(DATA_ERROR);
  490. } else if(infile_info->channels != 1) {
  491. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  492. return(DATA_ERROR);
  493. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  494. sprintf(errstr,"Failed to copy file parsing information\n");
  495. return(PROGRAM_ERROR);
  496. }
  497. free(infile_info);
  498. }
  499. return(FINISHED);
  500. }
  501. /************************* SETUP_SHIFTER_PARAM_RANGES_AND_DEFAULTS *******************/
  502. int setup_shifter_param_ranges_and_defaults(dataptr dz)
  503. {
  504. int exit_status;
  505. aplptr ap = dz->application;
  506. // set_param_ranges()
  507. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  508. // NB total_input_param_cnt is > 0 !!!
  509. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  510. return(FAILED);
  511. // get_param_ranges()
  512. ap->lo[SHF_CYCDUR] = 0.01;
  513. ap->hi[SHF_CYCDUR] = 3600.0;
  514. ap->default_val[SHF_CYCDUR] = 2;
  515. ap->lo[SHF_OUTDUR] = 0.0;
  516. ap->hi[SHF_OUTDUR] = 32767.0;
  517. ap->default_val[SHF_OUTDUR] = 8;
  518. ap->lo[SHF_OCHANS] = 1;
  519. ap->hi[SHF_OCHANS] = 16;
  520. ap->default_val[SHF_OCHANS] = 2;
  521. ap->lo[SHF_SUBDIV] = 3;
  522. ap->hi[SHF_SUBDIV] = 64;
  523. ap->default_val[SHF_SUBDIV] = 16;
  524. ap->lo[SHF_LINGER] = 0.0;
  525. ap->hi[SHF_LINGER] = 32767;
  526. ap->default_val[SHF_LINGER] = 2;
  527. ap->lo[SHF_TRNSIT] = 0.0;
  528. ap->hi[SHF_TRNSIT] = 32767;
  529. ap->default_val[SHF_TRNSIT] = 2;
  530. ap->lo[SHF_LBOOST] = 0.0;
  531. ap->hi[SHF_LBOOST] = 10.0;
  532. ap->default_val[SHF_LBOOST] = 1.0;
  533. dz->maxmode = 2;
  534. if(!sloom)
  535. put_default_vals_in_all_params(dz);
  536. return(FINISHED);
  537. }
  538. /********************************* PARSE_SLOOM_DATA *********************************/
  539. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  540. {
  541. int exit_status;
  542. int cnt = 1, infilecnt;
  543. int filesize, insams, inbrksize;
  544. double dummy;
  545. int true_cnt = 0;
  546. //aplptr ap;
  547. while(cnt<=PRE_CMDLINE_DATACNT) {
  548. if(cnt > argc) {
  549. sprintf(errstr,"Insufficient data sent from TK\n");
  550. return(DATA_ERROR);
  551. }
  552. switch(cnt) {
  553. case(1):
  554. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  555. sprintf(errstr,"Cannot read process no. sent from TK\n");
  556. return(DATA_ERROR);
  557. }
  558. break;
  559. case(2):
  560. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  561. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  562. return(DATA_ERROR);
  563. }
  564. if(dz->mode > 0)
  565. dz->mode--;
  566. //setup_particular_application() =
  567. if((exit_status = setup_shifter_application(dz))<0)
  568. return(exit_status);
  569. //ap = dz->application;
  570. break;
  571. case(3):
  572. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  573. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  574. return(DATA_ERROR);
  575. }
  576. if(infilecnt < 1) {
  577. true_cnt = cnt + 1;
  578. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  579. }
  580. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  581. return(exit_status);
  582. break;
  583. case(INPUT_FILETYPE+4):
  584. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  585. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  586. return(DATA_ERROR);
  587. }
  588. break;
  589. case(INPUT_FILESIZE+4):
  590. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  591. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  592. return(DATA_ERROR);
  593. }
  594. dz->insams[0] = filesize;
  595. break;
  596. case(INPUT_INSAMS+4):
  597. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  598. sprintf(errstr,"Cannot read insams sent from TK\n");
  599. return(DATA_ERROR);
  600. }
  601. dz->insams[0] = insams;
  602. break;
  603. case(INPUT_SRATE+4):
  604. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  605. sprintf(errstr,"Cannot read srate sent from TK\n");
  606. return(DATA_ERROR);
  607. }
  608. break;
  609. case(INPUT_CHANNELS+4):
  610. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  611. sprintf(errstr,"Cannot read channels sent from TK\n");
  612. return(DATA_ERROR);
  613. }
  614. break;
  615. case(INPUT_STYPE+4):
  616. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  617. sprintf(errstr,"Cannot read stype sent from TK\n");
  618. return(DATA_ERROR);
  619. }
  620. break;
  621. case(INPUT_ORIGSTYPE+4):
  622. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  623. sprintf(errstr,"Cannot read origstype sent from TK\n");
  624. return(DATA_ERROR);
  625. }
  626. break;
  627. case(INPUT_ORIGRATE+4):
  628. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  629. sprintf(errstr,"Cannot read origrate sent from TK\n");
  630. return(DATA_ERROR);
  631. }
  632. break;
  633. case(INPUT_MLEN+4):
  634. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  635. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  636. return(DATA_ERROR);
  637. }
  638. break;
  639. case(INPUT_DFAC+4):
  640. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  641. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  642. return(DATA_ERROR);
  643. }
  644. break;
  645. case(INPUT_ORIGCHANS+4):
  646. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  647. sprintf(errstr,"Cannot read origchans sent from TK\n");
  648. return(DATA_ERROR);
  649. }
  650. break;
  651. case(INPUT_SPECENVCNT+4):
  652. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  653. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  654. return(DATA_ERROR);
  655. }
  656. dz->specenvcnt = dz->infile->specenvcnt;
  657. break;
  658. case(INPUT_WANTED+4):
  659. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  660. sprintf(errstr,"Cannot read wanted sent from TK\n");
  661. return(DATA_ERROR);
  662. }
  663. break;
  664. case(INPUT_WLENGTH+4):
  665. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  666. sprintf(errstr,"Cannot read wlength sent from TK\n");
  667. return(DATA_ERROR);
  668. }
  669. break;
  670. case(INPUT_OUT_CHANS+4):
  671. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  672. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  673. return(DATA_ERROR);
  674. }
  675. break;
  676. /* RWD these chanegs to samps - tk will have to deal with that! */
  677. case(INPUT_DESCRIPTOR_BYTES+4):
  678. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  679. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  680. return(DATA_ERROR);
  681. }
  682. break;
  683. case(INPUT_IS_TRANSPOS+4):
  684. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  685. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  686. return(DATA_ERROR);
  687. }
  688. break;
  689. case(INPUT_COULD_BE_TRANSPOS+4):
  690. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  691. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  692. return(DATA_ERROR);
  693. }
  694. break;
  695. case(INPUT_COULD_BE_PITCH+4):
  696. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  697. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  698. return(DATA_ERROR);
  699. }
  700. break;
  701. case(INPUT_DIFFERENT_SRATES+4):
  702. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  703. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  704. return(DATA_ERROR);
  705. }
  706. break;
  707. case(INPUT_DUPLICATE_SNDS+4):
  708. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  709. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  710. return(DATA_ERROR);
  711. }
  712. break;
  713. case(INPUT_BRKSIZE+4):
  714. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  715. sprintf(errstr,"Cannot read brksize sent from TK\n");
  716. return(DATA_ERROR);
  717. }
  718. if(inbrksize > 0) {
  719. switch(dz->input_data_type) {
  720. case(WORDLIST_ONLY):
  721. break;
  722. case(PITCH_AND_PITCH):
  723. case(PITCH_AND_TRANSPOS):
  724. case(TRANSPOS_AND_TRANSPOS):
  725. dz->tempsize = inbrksize;
  726. break;
  727. case(BRKFILES_ONLY):
  728. case(UNRANGED_BRKFILE_ONLY):
  729. case(DB_BRKFILES_ONLY):
  730. case(ALL_FILES):
  731. case(ANY_NUMBER_OF_ANY_FILES):
  732. if(dz->extrabrkno < 0) {
  733. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  734. return(DATA_ERROR);
  735. }
  736. if(dz->brksize == NULL) {
  737. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  738. return(PROGRAM_ERROR);
  739. }
  740. dz->brksize[dz->extrabrkno] = inbrksize;
  741. break;
  742. default:
  743. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  744. dz->input_data_type);
  745. return(PROGRAM_ERROR);
  746. }
  747. break;
  748. }
  749. break;
  750. case(INPUT_NUMSIZE+4):
  751. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  752. sprintf(errstr,"Cannot read numsize sent from TK\n");
  753. return(DATA_ERROR);
  754. }
  755. break;
  756. case(INPUT_LINECNT+4):
  757. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  758. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  759. return(DATA_ERROR);
  760. }
  761. break;
  762. case(INPUT_ALL_WORDS+4):
  763. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  764. sprintf(errstr,"Cannot read all_words sent from TK\n");
  765. return(DATA_ERROR);
  766. }
  767. break;
  768. case(INPUT_ARATE+4):
  769. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  770. sprintf(errstr,"Cannot read arate sent from TK\n");
  771. return(DATA_ERROR);
  772. }
  773. break;
  774. case(INPUT_FRAMETIME+4):
  775. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  776. sprintf(errstr,"Cannot read frametime sent from TK\n");
  777. return(DATA_ERROR);
  778. }
  779. dz->frametime = (float)dummy;
  780. break;
  781. case(INPUT_WINDOW_SIZE+4):
  782. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  783. sprintf(errstr,"Cannot read window_size sent from TK\n");
  784. return(DATA_ERROR);
  785. }
  786. break;
  787. case(INPUT_NYQUIST+4):
  788. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  789. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  790. return(DATA_ERROR);
  791. }
  792. break;
  793. case(INPUT_DURATION+4):
  794. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  795. sprintf(errstr,"Cannot read duration sent from TK\n");
  796. return(DATA_ERROR);
  797. }
  798. break;
  799. case(INPUT_MINBRK+4):
  800. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  801. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  802. return(DATA_ERROR);
  803. }
  804. break;
  805. case(INPUT_MAXBRK+4):
  806. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  807. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  808. return(DATA_ERROR);
  809. }
  810. break;
  811. case(INPUT_MINNUM+4):
  812. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  813. sprintf(errstr,"Cannot read minnum sent from TK\n");
  814. return(DATA_ERROR);
  815. }
  816. break;
  817. case(INPUT_MAXNUM+4):
  818. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  819. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  820. return(DATA_ERROR);
  821. }
  822. break;
  823. default:
  824. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  825. return(PROGRAM_ERROR);
  826. }
  827. cnt++;
  828. }
  829. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  830. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  831. return(DATA_ERROR);
  832. }
  833. if(true_cnt)
  834. cnt = true_cnt;
  835. *cmdlinecnt = 0;
  836. while(cnt < argc) {
  837. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  838. return(exit_status);
  839. cnt++;
  840. }
  841. return(FINISHED);
  842. }
  843. /********************************* GET_TK_CMDLINE_WORD *********************************/
  844. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  845. {
  846. if(*cmdlinecnt==0) {
  847. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  848. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  849. return(MEMORY_ERROR);
  850. }
  851. } else {
  852. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  853. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  854. return(MEMORY_ERROR);
  855. }
  856. }
  857. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  858. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  859. return(MEMORY_ERROR);
  860. }
  861. strcpy((*cmdline)[*cmdlinecnt],q);
  862. (*cmdlinecnt)++;
  863. return(FINISHED);
  864. }
  865. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  866. int assign_file_data_storage(int infilecnt,dataptr dz)
  867. {
  868. int exit_status;
  869. int no_sndfile_system_files = FALSE;
  870. dz->infilecnt = infilecnt;
  871. if((exit_status = allocate_filespace(dz))<0)
  872. return(exit_status);
  873. if(no_sndfile_system_files)
  874. dz->infilecnt = 0;
  875. return(FINISHED);
  876. }
  877. /************************* redundant functions: to ensure libs compile OK *******************/
  878. int assign_process_logic(dataptr dz)
  879. {
  880. return(FINISHED);
  881. }
  882. void set_legal_infile_structure(dataptr dz)
  883. {}
  884. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  885. {
  886. return(FINISHED);
  887. }
  888. int setup_internal_arrays_and_array_pointers(dataptr dz)
  889. {
  890. return(FINISHED);
  891. }
  892. int establish_bufptrs_and_extra_buffers(dataptr dz)
  893. {
  894. return(FINISHED);
  895. }
  896. int read_special_data(char *str,dataptr dz)
  897. {
  898. return(FINISHED);
  899. }
  900. int inner_loop
  901. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  902. {
  903. return(FINISHED);
  904. }
  905. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  906. {
  907. return(FINISHED);
  908. }
  909. /******************************** USAGE1 ********************************/
  910. int usage1(void)
  911. {
  912. usage2("shifter");
  913. return(USAGE_ONLY);
  914. }
  915. /**************************** CHECK_SHIFTER_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  916. int check_shifter_param_validity_and_consistency(dataptr dz)
  917. {
  918. double srate = (double)dz->infile->srate;
  919. int k, j;
  920. if(dz->param[SHF_CYCDUR] > dz->param[SHF_OUTDUR]) {
  921. sprintf(errstr,"Cycle duration is less than output duration.\n");
  922. return(DATA_ERROR);
  923. }
  924. if(dz->iparam[SHF_LINGER] + dz->iparam[SHF_TRNSIT] < 1.0) {
  925. sprintf(errstr,"Linger and Transit parameters must add to >= 1.\n");
  926. return(DATA_ERROR);
  927. }
  928. dz->iparam[SHF_OUTDUR] = (int)round(dz->param[SHF_OUTDUR] * srate);
  929. dz->iparam[SHF_CYCDUR] = (int)round(dz->param[SHF_CYCDUR] * srate);
  930. if(dz->vflag[SHF_ZIG] && dz->vflag[SHF_RND]) {
  931. sprintf(errstr,"Zigzag and Random flags cannot be used in combination.\n");
  932. return(DATA_ERROR);
  933. }
  934. k = dz->iparam[SHF_SUBDIV]/2;
  935. j = dz->iparam[SHF_SUBDIV]/3;
  936. if((2 * k != dz->iparam[SHF_SUBDIV]) && (3 * j != dz->iparam[SHF_SUBDIV])) {
  937. sprintf(errstr,"Neat subdivision is not a multiple of 2 or 3.\n");
  938. return(DATA_ERROR);
  939. }
  940. dz->param[SHF_SUBDIV] = (double)dz->iparam[SHF_SUBDIV];
  941. return FINISHED;
  942. }
  943. /******************************** DBTOLEVEL ***********************/
  944. double dbtolevel(double val)
  945. {
  946. int isneg = 0;
  947. if(flteq(val,0.0))
  948. return(1.0);
  949. if(val < 0.0) {
  950. val = -val;
  951. isneg = 1;
  952. }
  953. val /= 20.0;
  954. val = pow(10.0,val);
  955. if(isneg)
  956. val = 1.0/val;
  957. return(val);
  958. }
  959. /********************************************************************************************/
  960. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  961. {
  962. if(!strcmp(prog_identifier_from_cmdline,"shifter")) dz->process = SHIFTER;
  963. else {
  964. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  965. return(USAGE_ONLY);
  966. }
  967. return(FINISHED);
  968. }
  969. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  970. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  971. {
  972. int n;
  973. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  974. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  975. return(MEMORY_ERROR);
  976. }
  977. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  978. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  979. return(MEMORY_ERROR);
  980. }
  981. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  982. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  983. return(MEMORY_ERROR);
  984. }
  985. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  986. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  987. return(MEMORY_ERROR);
  988. }
  989. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  990. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  991. return(MEMORY_ERROR);
  992. }
  993. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  994. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  995. return(MEMORY_ERROR);
  996. }
  997. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  998. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  999. return(MEMORY_ERROR);
  1000. }
  1001. for(n=0;n<brkcnt;n++) {
  1002. dz->brk[n] = NULL;
  1003. dz->brkptr[n] = NULL;
  1004. dz->brkinit[n] = 0;
  1005. dz->brksize[n] = 0;
  1006. }
  1007. return(FINISHED);
  1008. }
  1009. /******************************** USAGE2 ********************************/
  1010. int usage2(char *str)
  1011. {
  1012. if(!strcmp(str,"shifter")) {
  1013. fprintf(stdout,
  1014. "USAGE: shifter shifter \n"
  1015. "1 inf out cycles cycdur dur ochans subdiv linger transit boost [-z|-r] [-l]\n"
  1016. "2 inf1 inf2 [inf3 ....] cycles cycdur dur ochans subdiv linger transit boost\n"
  1017. " [-z|-r] [-l]\n"
  1018. "\n"
  1019. "Generate simultaneous repetition cycles, shifting focus from one to another.\n"
  1020. "\n"
  1021. "Mode 1: Uses the same input sound for all cycles.\n"
  1022. "Mode 2: In this mode the number of input files must equal the number of cycles.\n"
  1023. " Assigns the input files, in order, to the cycles, in order.\n"
  1024. "CYCLES Textfile listing the number of beats in each cycle.\n"
  1025. "CYCDUR Duration of 1 complete cycle.\n"
  1026. "DUR Required duration of the output sound.\n"
  1027. "OCHANS Number of channels in output.\n"
  1028. "SUBDIV Minimum division of the beat: > 4 and a multiple of 2 and or 3\n"
  1029. "LINGER Number of cycles to remain in a fixed focus.\n"
  1030. "TRANSIT Number of cycles to make transition to next focus.\n"
  1031. " The sum of LINGER & TRANSIT must be >= 1.\n"
  1032. "BOOST With standard stream level \"L\", add \"boost * L\" to focus stream level.\n"
  1033. "\n"
  1034. " Normal procedure is to move focus through the list of cycles.\n"
  1035. " e.g. with cycles 11,12,13 focus moves 11,12,13,11,12,13,etc\n"
  1036. "\n"
  1037. "-z Flag causes focus to ZIGZAG through the cycles.\n"
  1038. " e.g. with cycles 11,12,13 focus moves 11,12,13,12,11,12,13,12,etc\n"
  1039. "\n"
  1040. "-r Flag causes focus to select a RANDOM order of the cycles\n"
  1041. " e.g. 12,11,13, move through those, then select another random order etc.\n"
  1042. "\n"
  1043. " If number of output channels > 2, lspkr array assumed sound-surround.\n"
  1044. "-l Flag changes to a linear array, with a leftmost and rightmost lspkr.\n"
  1045. "\n");
  1046. } else
  1047. fprintf(stdout,"Unknown option '%s'\n",str);
  1048. return(USAGE_ONLY);
  1049. }
  1050. int usage3(char *str1,char *str2)
  1051. {
  1052. fprintf(stderr,"Insufficient parameters on command line.\n");
  1053. return(USAGE_ONLY);
  1054. }
  1055. /******************************** SHIFTER ********************************
  1056. *
  1057. * INTEGER ARRAYS lmost LONG ARRAYS DOUBLE ARRAYS
  1058. * cycles | rmost | thisreltime
  1059. * | time-arrays | | perm sndptrs nextreltime llev
  1060. * | | | | | | foc | | | abstime | rlev
  1061. * | |-----------------------| | | | | | |---------|---------|---------| | | |
  1062. * | | | | | | | | | cycleno | cycleno | cycleno | | | |
  1063. * array | | | | | | | | | arrays | arrays | arrays | | | |
  1064. * no 0 1 to ringsize ringsize+1 0 1 | | | 0 1 |
  1065. * | | | ringsize+2 | | | | | | | |
  1066. * | | | | ringsize+3 | | | | | | | |
  1067. * | | | | | ringsize+4 | | | | | | | |
  1068. * | | | | | | | | | | | | | | |
  1069. * | | cycleno | | | | | | | | |
  1070. * cycleno | cycleno cycleno*dupl| | | cycleno
  1071. * size | maxcycle | | cycleno | maxcycle maxcycle maxcycle | | cycleno
  1072. * | | | | | maxcycle | | | | | | | |
  1073. * | | | | | | gpeventcnt | | | | | | | |
  1074. *
  1075. * A CYCLE is a compete set of repetitions.
  1076. * AN EVENT is (the start of) a single src-reading
  1077. */
  1078. #define ibuf dz->sampbuf
  1079. int shifter(dataptr dz)
  1080. {
  1081. int exit_status, zig, permcnt, focus = 0, next_focus, transcnt = 0, ptrcnt, finished, transbase, chans = dz->iparam[SHF_OCHANS];
  1082. int *cycle = dz->iparray[0], **reltime = dz->iparray+1, *perm = dz->iparray[dz->ringsize+3], *foc = dz->iparray[dz->ringsize+4];
  1083. int *lmost = dz->iparray[dz->ringsize+1], *rmost= dz->iparray[dz->ringsize+2], *fflag = dz->iparray[dz->ringsize+5];
  1084. int **thisreltime = dz->lparray + 1, **nextreltime = dz->lparray + 1 + dz->itemcnt, **abstime = dz->lparray + 1 + (dz->itemcnt*2);
  1085. int *ptr = dz->lparray[0];
  1086. int outcnt, opos, cycgrpcntr, cyccntr, maxcycle, cycgrpsize, n, m, k, mm, nn, gpcnt, timediff, samps_read, transbas = 0, transdur;
  1087. float *obuf = dz->sampbuf[dz->infilecnt];
  1088. double *llev = dz->parray[0], *rlev = dz->parray[1];
  1089. double normaliser, maxsamp = 0.0, trans_position, transfrac, boost = 1.0, srate = (double)dz->infile->srate;
  1090. for(n=0;n<dz->infilecnt;n++) {
  1091. if((samps_read = fgetfbufEx(dz->sampbuf[n], dz->insams[n],dz->ifd[n],0))<0) {
  1092. sprintf(errstr,"Sound read error with input soundfile %d: %s\n",n+1,sferrstr());
  1093. return(SYSTEM_ERROR);
  1094. }
  1095. }
  1096. zig = 1; // zigzag starts in zig direction
  1097. permcnt = 0; // counter for permutations of cycle focus.
  1098. ptrcnt = dz->itemcnt * dz->dupl; // All pointers available to read infile(s)
  1099. for(n = 0; n < ptrcnt; n++)
  1100. ptr[n] = -1; // Mark all sound pointers as off
  1101. maxcycle = 0;
  1102. for(n = 0;n < dz->itemcnt;n++)
  1103. maxcycle = max(maxcycle,cycle[n]);
  1104. for(n = 0;n < dz->infilecnt;n++)
  1105. ibuf[n] = dz->sampbuf[n];
  1106. // Cycle-group is a whole set of LINGER+TRANSIT cycles
  1107. cycgrpsize = (dz->iparam[SHF_LINGER] + dz->iparam[SHF_TRNSIT]) * dz->iparam[SHF_CYCDUR];
  1108. opos = 0;
  1109. cycgrpcntr = 0; // This counts samples through a cycle-group
  1110. gpcnt = 0; // This counts the cycle-groups
  1111. cyccntr = 0; // This counts samples through a cycle
  1112. outcnt = 0; // This counts the output group-samples
  1113. transdur = dz->iparam[SHF_TRNSIT] * dz->iparam[SHF_CYCDUR];
  1114. if(dz->vflag[SHF_RND]) {
  1115. rndintperm(perm,dz->itemcnt);
  1116. next_focus = perm[0];
  1117. } else
  1118. next_focus = 0;
  1119. fprintf(stdout,"INFO: First pass: assessing level.\n");
  1120. fflush(stdout);
  1121. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1122. while(outcnt < dz->iparam[SHF_OUTDUR]) {
  1123. // IF AT START OF NEW CYCLE-GROUP
  1124. if(cycgrpcntr % cycgrpsize == 0) {
  1125. cycgrpcntr = 0;
  1126. // SET UP NEW LINGER-TRANSIT COUNTER
  1127. transcnt = -dz->iparam[SHF_LINGER];
  1128. transbas = outcnt + (dz->iparam[SHF_LINGER] * dz->iparam[SHF_CYCDUR]);
  1129. // DECIDE WHICH CYCLE IS THE FOCUS (AND WHICH THE NEXT)
  1130. if(dz->vflag[SHF_ZIG]) {
  1131. focus = next_focus;
  1132. if(zig) {
  1133. next_focus = focus + 1;
  1134. if(next_focus >= dz->itemcnt) {
  1135. zig = !zig;
  1136. next_focus -= 2;
  1137. }
  1138. } else {
  1139. next_focus = focus - 1;
  1140. if(next_focus < 0) {
  1141. zig = !zig;
  1142. next_focus = 1;
  1143. }
  1144. }
  1145. } else if(dz->vflag[SHF_RND]) {
  1146. if(permcnt == dz->itemcnt-1) {
  1147. focus = perm[permcnt-1];
  1148. do {
  1149. rndintperm(perm,dz->itemcnt);
  1150. } while(perm[0] != focus);
  1151. next_focus = perm[0];
  1152. } else {
  1153. if(permcnt == dz->itemcnt)
  1154. permcnt = 0;
  1155. focus = perm[permcnt];
  1156. next_focus = perm[permcnt+1];
  1157. }
  1158. permcnt++;
  1159. } else {
  1160. focus = next_focus;
  1161. next_focus = (focus+1) % dz->itemcnt;
  1162. }
  1163. foc[gpcnt] = focus;
  1164. foc[gpcnt+1] = next_focus;
  1165. // GET THE ABSOLUTE TIMINGS OF THE EVENTS IN EACH CYCLE
  1166. // With 3 cycles 00, 01, 02, 10, 11, 12, 20, 21, 22, AND if the focus is the 2nd cycle (1)
  1167. for(n = 0,m = focus; n < dz->itemcnt;n++,m+=dz->itemcnt) { // we want x x x (as focus is 2nd of each pair)
  1168. for(k = 0; k < maxcycle;k++) // which is focus focus focus
  1169. thisreltime[n][k] = reltime[m][k]; // (m) +itemcnt +itemcnt*2
  1170. }
  1171. // IF THERE'S ANY TRANSIT
  1172. // GET WHAT ABSOLUTE TIMINGS WOULD BE IF NEXT EVENT WERE THE FOCUS
  1173. if(dz->iparam[SHF_TRNSIT] > 0) { // With 3 cycles 00, 01, 02, 10, 11, 12, 20, 21, 22, if next focus is 3rd cycle (2)
  1174. for(n = 0,m = next_focus; n < dz->itemcnt;n++,m+=dz->itemcnt) {// we want x x x (as focus is 2nd of each pair)
  1175. for(k = 0; k < maxcycle;k++) // which is focus focus focus
  1176. nextreltime[n][k] = reltime[m][k]; // (m) +itemcnt +itemcnt*2
  1177. }
  1178. }
  1179. // INITIALLY, USE THE focus EVENT TIMINGS AS THE TRUE TIMINGS (MAY BE MODIFIED LATER)
  1180. for(n = 0;n < dz->itemcnt; n++) { // THIS IS REDUNDANT , as done at cycle start
  1181. for(k = 0; k < maxcycle;k++)
  1182. abstime[n][k] = thisreltime[n][k] + outcnt;
  1183. }
  1184. cyccntr = 0;
  1185. gpcnt++;
  1186. }
  1187. // IF WE'RE STARTING A NEW CYCLE
  1188. if(cyccntr % dz->iparam[SHF_CYCDUR] == 0) {
  1189. if(dz->iparam[SHF_TRNSIT] > 0) {
  1190. // IF WE'RE IN A TRANSITION (rather than being in a linger, where transcnt IS -ve), GET TRUE TIMINGS WITHIN CYCLE
  1191. if(transcnt >= 0) {
  1192. transbase = transcnt; // Which cycle are we in, within the transition
  1193. for(n = 0;n < dz->itemcnt; n++) {
  1194. for(k = 0; k < cycle[n];k++) { // For each event within the cycle
  1195. trans_position = (double)k/(double)cycle[n]; // How far into the cycle are we
  1196. transfrac = (double)(trans_position + transbase)/(double)dz->iparam[SHF_TRNSIT]; // What fraction of this transition is this event at
  1197. timediff = nextreltime[n][k] - thisreltime[n][k]; // What is timediff between positions at start and end of transition
  1198. timediff = (int)round((double)timediff * transfrac); // Relative adjustment at this point in transition
  1199. abstime[n][k] = thisreltime[n][k] + timediff + outcnt; // True time value at this point in transition
  1200. }
  1201. }
  1202. } else {
  1203. for(n = 0; n < dz->itemcnt;n++) {
  1204. for(k = 0; k < maxcycle;k++)
  1205. abstime[n][k] = thisreltime[n][k] + outcnt;
  1206. }
  1207. }
  1208. transcnt++;
  1209. } else {
  1210. for(n = 0; n < dz->itemcnt;n++) {
  1211. for(k = 0; k < maxcycle;k++)
  1212. abstime[n][k] = thisreltime[n][k] + outcnt;
  1213. }
  1214. }
  1215. }
  1216. // CHECK EACH CYCLE TO SEE IF WE'RE AT TIME TO INITIATE NEW EVENT
  1217. for(n = 0;n<dz->itemcnt;n++) {
  1218. for(k=0;k < cycle[n];k++) {
  1219. if(abstime[n][k] == outcnt) {
  1220. // AND IF SO, GET APPROPRIATE POINTERS TO INPUT FILE(S) FOR THIS CYCLE
  1221. for(m=n; m < ptrcnt; m+= dz->itemcnt) { // Find an available buffer-pointer assocd with that cycle
  1222. if(dz->mode == 1)
  1223. mm = n; // Associate the ptr with the correct file
  1224. else
  1225. mm = 0;
  1226. if(ptr[m] < 0 || ptr[m] >= dz->insams[mm]) { // If pointer is available
  1227. ptr[m] = 0; // set it to start counting in an infile
  1228. if(n == focus)
  1229. fflag[m] = 1;
  1230. else if(n == next_focus)
  1231. fflag[m] = 2;
  1232. else
  1233. fflag[m] = 0;
  1234. break;
  1235. }
  1236. }
  1237. } else if(abstime[n][k] > outcnt) // Don't search beyond "now"
  1238. break;
  1239. }
  1240. }
  1241. // FINALLY, WRITE TO NEXT SAMPLE IN OUTBUFFER FROM ALL ACTIVE POINTERS
  1242. trans_position = outcnt - transbas;
  1243. transfrac = (double)trans_position/(double)transdur;// What fraction of this transition is this event at (only valud once we're IN the transition
  1244. for(n = 0;n<ptrcnt;n++) { // For every pointer...
  1245. nn = n % dz->itemcnt; // Associate the ptr with the correct cycle+spatial-pos
  1246. if(dz->mode == 1)
  1247. mm = nn; // Associate the ptr with the correct file
  1248. else
  1249. mm = 0;
  1250. if(ptr[n] >= 0 && ptr[n] < dz->insams[mm]) { // If it is active
  1251. boost = 1.0;
  1252. if(dz->param[SHF_LBOOST] > 0) { // If there's a level boost to the focus stream
  1253. if(trans_position < 0) { // Before transit starts, boost level of focus stream pointers (only)
  1254. if(fflag[n] == 1)
  1255. boost = 1.0 + dz->param[SHF_LBOOST];
  1256. } else { // Once we're in a transition
  1257. if(fflag[n] == 1) // If this is a focus stream, decrement level
  1258. boost = 1.0 + ((1.0 - transfrac) * dz->param[SHF_LBOOST]);
  1259. else if(fflag[n] == 2) // If this is a next-focus stream, increment level
  1260. boost = 1.0 + (transfrac * dz->param[SHF_LBOOST]);
  1261. }
  1262. }
  1263. obuf[opos + lmost[nn]] = (float)(obuf[opos + lmost[nn]] + (ibuf[mm][ptr[n]] * llev[nn] * boost));
  1264. obuf[opos + rmost[nn]] = (float)(obuf[opos + rmost[nn]] + (ibuf[mm][ptr[n]] * rlev[nn] * boost));
  1265. // position-nn pointer[n] levels[nn]
  1266. // assocd with output cycle is associated with are associated with
  1267. // and corresponds to chans sound mm spatial pos
  1268. // in output buffer which it is reading of output cycle
  1269. }
  1270. if(ptr[n] >= 0) // Advance all switched-on ptrs, whether are not they are currently reading a file.
  1271. ptr[n]++; // Once they are beyond file end, they become available to select for next read.
  1272. }
  1273. // ADVANCE IN OUTBUF, AND TEST LEVEL OUTPUT IF BUFFER FULL
  1274. opos += chans;
  1275. if(opos >= dz->buflen) {
  1276. fprintf(stdout,"INFO: Level Check at %lf secs\n",(double)outcnt/srate);
  1277. fflush(stdout);
  1278. for(n = 0;n < dz->buflen;n++)
  1279. maxsamp = max(maxsamp,fabs(obuf[n]));
  1280. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1281. opos = 0;
  1282. }
  1283. cycgrpcntr++; // Counter notes where we are in a cycle-group
  1284. cyccntr++; // Counter notes where we are in a cycle
  1285. outcnt++; // Counter notes where we are in the output
  1286. }
  1287. // COMPLETE ALL ACTIVE WRITES
  1288. finished = 0;
  1289. while(!finished) {
  1290. finished = 1;
  1291. for(n = 0;n<ptrcnt;n++) { // For every pointer...
  1292. if(dz->mode == 1)
  1293. mm = n % dz->infilecnt;
  1294. else
  1295. mm = 0;
  1296. if(ptr[n] > 0 && ptr[n] < dz->insams[mm]) { // If it is active
  1297. finished = 0; // Flag not yet finished
  1298. break;
  1299. }
  1300. }
  1301. if(finished)
  1302. break;
  1303. trans_position = outcnt - transbas;
  1304. transfrac = (double)trans_position/(double)transdur; // What fraction of this transition is this event at
  1305. for(n = 0;n<ptrcnt;n++) {
  1306. nn = n % dz->itemcnt;
  1307. if(dz->mode == 1)
  1308. mm = nn;
  1309. else
  1310. mm = 0;
  1311. if(ptr[n] > 0 && ptr[n] < dz->insams[mm]) {
  1312. boost = 1.0;
  1313. if(dz->param[SHF_LBOOST] > 0) { // If there's a level boost to the focus stream
  1314. if(trans_position < 0) { // Before transit starts, boost level of focus stream pointers (only)
  1315. if(fflag[n] == 1)
  1316. boost = 1.0 + dz->param[SHF_LBOOST];
  1317. } else {
  1318. if(fflag[n] == 1)
  1319. boost = 1.0 + ((1.0 - transfrac) * dz->param[SHF_LBOOST]);
  1320. else if(fflag[n] == 2)
  1321. boost = 1.0 + (transfrac * dz->param[SHF_LBOOST]);
  1322. }
  1323. }
  1324. obuf[opos + lmost[nn]] = (float)(obuf[opos + lmost[nn]] + (ibuf[mm][ptr[n]] * llev[nn] * boost));
  1325. obuf[opos + rmost[nn]] = (float)(obuf[opos + rmost[nn]] + (ibuf[mm][ptr[n]] * rlev[nn] * boost));
  1326. }
  1327. if(ptr[n] > 0)
  1328. ptr[n]++;
  1329. }
  1330. opos += chans;
  1331. if(opos >= dz->buflen) {
  1332. for(n = 0;n < dz->buflen;n++)
  1333. maxsamp = max(maxsamp,fabs(obuf[n]));
  1334. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1335. opos = 0;
  1336. }
  1337. outcnt++;
  1338. }
  1339. if(opos) {
  1340. for(n = 0;n < opos;n++)
  1341. maxsamp = max(maxsamp,fabs(obuf[n]));
  1342. }
  1343. dz->tempsize = outcnt * chans; /* For sloom progress bar */
  1344. normaliser = 1.0;
  1345. if(maxsamp > 0.95) {
  1346. normaliser = 0.95/maxsamp;
  1347. fprintf(stdout,"INFO: Normalising by %lf secs\n",(double)normaliser);
  1348. fflush(stdout);
  1349. }
  1350. opos = 0;
  1351. cycgrpcntr = 0;
  1352. cyccntr = 0;
  1353. gpcnt = 0;
  1354. outcnt = 0;
  1355. fprintf(stdout,"INFO: Second pass: creating output.\n");
  1356. fflush(stdout);
  1357. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1358. while(outcnt < dz->iparam[SHF_OUTDUR]) {
  1359. // IF AT START OF NEW CYCLE-GROUP
  1360. if(cycgrpcntr % cycgrpsize == 0) {
  1361. cycgrpcntr = 0;
  1362. // SET UP NEW LINGER-TRANSIT COUNTER
  1363. transcnt = -dz->iparam[SHF_LINGER];
  1364. transbas = outcnt + (dz->iparam[SHF_LINGER] * dz->iparam[SHF_CYCDUR]);
  1365. // DECIDE WHICH CYCLE IS THE FOCUS (AND WHICH THE NEXT)
  1366. focus = foc[gpcnt];
  1367. next_focus = foc[gpcnt+1];
  1368. // GET THE ABSOLUTE TIMINGS OF THE EVENTS IN EACH CYCLE
  1369. // With 3 cycles 00, 01, 02, 10, 11, 12, 20, 21, 22, AND if the focus is the 2nd cycle (1)
  1370. for(n = 0,m = focus; n < dz->itemcnt;n++,m+=dz->itemcnt) { // we want x x x (as focus is 2nd of each pair)
  1371. for(k = 0; k < maxcycle;k++) // which is focus focus focus
  1372. thisreltime[n][k] = reltime[m][k]; // (m) +itemcnt +itemcnt*2
  1373. }
  1374. // IF THERE'S ANY TRANSIT
  1375. // GET WHAT ABSOLUTE TIMINGS WOULD BE IF NEXT EVENT WERE THE FOCUS
  1376. if(dz->iparam[SHF_TRNSIT] > 0) { // With 3 cycles 00, 01, 02, 10, 11, 12, 20, 21, 22, if next focus is 3rd cycle (2)
  1377. for(n = 0,m = next_focus; n < dz->itemcnt;n++,m+=dz->itemcnt) {// we want x x x (as focus is 2nd of each pair)
  1378. for(k = 0; k < maxcycle;k++) // which is focus focus focus
  1379. nextreltime[n][k] = reltime[m][k]; // (m) +itemcnt +itemcnt*2
  1380. }
  1381. }
  1382. // INITIALLY, USE THE focus EVENT TIMINGS AS THE TRUE TIMINGS (MAY BE MODIFIED LATER)
  1383. for(n = 0;n < dz->itemcnt; n++) {
  1384. for(k = 0; k < maxcycle;k++)
  1385. abstime[n][k] = thisreltime[n][k] + outcnt;
  1386. }
  1387. cyccntr = 0;
  1388. gpcnt++;
  1389. }
  1390. // IF WE'RE STARTING A NEW CYCLE
  1391. if(cyccntr % dz->iparam[SHF_CYCDUR] == 0) {
  1392. if(dz->iparam[SHF_TRNSIT] > 0) {
  1393. // IF WE'RE IN A TRANSITION (rather than being in a linger, where transcnt IS -ve), ADJUST TIMINGS WITHIN FIRST CYCLE
  1394. if(transcnt >= 0) {
  1395. transbase = transcnt; // Which cycle are we in, within the transition
  1396. for(n = 0;n < dz->itemcnt; n++) {
  1397. for(k = 0; k < cycle[n];k++) { // For each event within the cycle
  1398. trans_position = ((double)k/(double)cycle[n]); // How far into the cycle are we
  1399. transfrac = (double)(trans_position + transbase)/(double)dz->iparam[SHF_TRNSIT]; // What fraction of this transition is this event at
  1400. timediff = nextreltime[n][k] - thisreltime[n][k]; // What is timediff between positions at start and end of transition
  1401. timediff = (int)round((double)timediff * transfrac); // Relative adjustment at this point in transition
  1402. abstime[n][k] = thisreltime[n][k] + timediff + outcnt; // True time value at this point in transition
  1403. }
  1404. }
  1405. } else {
  1406. for(n = 0; n < dz->itemcnt;n++) {
  1407. for(k = 0; k < maxcycle;k++)
  1408. abstime[n][k] = thisreltime[n][k] + outcnt;
  1409. }
  1410. }
  1411. transcnt++;
  1412. } else {
  1413. for(n = 0; n < dz->itemcnt;n++) {
  1414. for(k = 0; k < maxcycle;k++)
  1415. abstime[n][k] = thisreltime[n][k] + outcnt;
  1416. }
  1417. }
  1418. }
  1419. // CHECK EACH CYCLE TO SEE IF WE'RE AT TIME TO INITIATE NEW EVENT
  1420. for(n = 0;n<dz->itemcnt;n++) {
  1421. for(k=0;k < cycle[n];k++) {
  1422. if(abstime[n][k] == outcnt) {
  1423. // AND IF SO, GET APPROPRIATE POINTERS TO INPUT FILE(S) FOR THIS CYCLE
  1424. for(m=n; m < ptrcnt; m+= dz->itemcnt) { // Find an available buffer-pointer assocd with that cycle
  1425. if(dz->mode == 1)
  1426. mm = n; // Associate the ptr with the correct file
  1427. else
  1428. mm = 0;
  1429. if(ptr[m] < 0 || ptr[m] >= dz->insams[mm]) { // If pointer is available
  1430. ptr[m] = 0; // set it to start counting in an infile
  1431. if(n==focus)
  1432. fflag[m] = 1;
  1433. else if(n==next_focus)
  1434. fflag[m] = 2;
  1435. else
  1436. fflag[m] = 0;
  1437. break;
  1438. }
  1439. }
  1440. } else if(abstime[n][k] > outcnt)
  1441. break;
  1442. }
  1443. }
  1444. // FINALLY, WRITE TO NEXT SAMPLE IN OUTBUFFER FROM ALL ACTIVE POINTERS
  1445. trans_position = outcnt - transbas;
  1446. transfrac = (double)trans_position/(double)transdur; // What fraction of this transition is this event at
  1447. for(n = 0;n<ptrcnt;n++) { // For every pointer...
  1448. nn = n % dz->itemcnt; // Associate the ptr with the correct cycle/pos
  1449. if(dz->mode == 1)
  1450. mm = n % dz->itemcnt; // Associate the ptr with the correct file
  1451. else
  1452. mm = 0;
  1453. if(ptr[n] >= 0 && ptr[n] < dz->insams[mm]) { // If it is active
  1454. boost = 1.0;
  1455. if(dz->param[SHF_LBOOST] > 0) { // If there's a level boost to the focus stream
  1456. if(trans_position < 0) { // Before transit starts, boost level of focus stream pointers (only)
  1457. if(fflag[n] == 1)
  1458. boost = 1.0 + dz->param[SHF_LBOOST];
  1459. } else {
  1460. if(fflag[n] == 1)
  1461. boost = 1.0 + ((1.0 - transfrac) * dz->param[SHF_LBOOST]);
  1462. else if(fflag[n] == 2)
  1463. boost = 1.0 + (transfrac * dz->param[SHF_LBOOST]);
  1464. }
  1465. }
  1466. obuf[opos + lmost[nn]] = (float)(obuf[opos + lmost[nn]] + (ibuf[mm][ptr[n]] * llev[nn] * boost));
  1467. obuf[opos + rmost[nn]] = (float)(obuf[opos + rmost[nn]] + (ibuf[mm][ptr[n]] * rlev[nn] * boost));
  1468. // position-nn pointer[n] levels[nn]
  1469. // assocd with output cycle is associated with are associated with
  1470. // and corresponds to chans sound mm spatial pos
  1471. // in output buffer which it is reading of output cycle
  1472. }
  1473. if(ptr[n] >= 0) // Advance all switched-on ptrs, whether are not they are currently reading a file.
  1474. (ptr[n])++; // Once they are beyond file end, they become available to select for next read.
  1475. }
  1476. opos += chans;
  1477. if(opos >= dz->buflen) {
  1478. if(normaliser < 1.0) {
  1479. for(n = 0;n < dz->buflen;n++)
  1480. obuf[n] = (float)(obuf[n] * normaliser);
  1481. }
  1482. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1483. return(exit_status);
  1484. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1485. opos = 0;
  1486. }
  1487. cycgrpcntr++; // Counter notes where we are in a cycle-group
  1488. cyccntr++; // Counter notes where we are in a cycle
  1489. outcnt++; // Counter notes where we are in the output
  1490. }
  1491. // COMPLETE ALL ACTIVE WRITES
  1492. finished = 0;
  1493. while(!finished) {
  1494. finished = 1;
  1495. for(n = 0;n<ptrcnt;n++) { // For every pointer...
  1496. if(dz->mode == 1)
  1497. mm = n % dz->infilecnt;
  1498. else
  1499. mm = 0;
  1500. if(ptr[n] > 0 && ptr[n] < dz->insams[mm]) { // If it is active
  1501. finished = 0; // Flag not yet finished
  1502. break;
  1503. }
  1504. }
  1505. if(finished)
  1506. break;
  1507. trans_position = outcnt - transbas;
  1508. transfrac = (double)trans_position/(double)transdur; // What fraction of this transition is this event at
  1509. for(n = 0;n<ptrcnt;n++) {
  1510. nn = n % dz->itemcnt;
  1511. if(dz->mode == 1)
  1512. mm = nn;
  1513. else
  1514. mm = 0;
  1515. if(ptr[n] > 0 && ptr[n] < dz->insams[mm]) {
  1516. boost = 1.0;
  1517. if(dz->param[SHF_LBOOST] > 0) { // If there's a level boost to the focus stream
  1518. if(trans_position < 0) { // Before transit starts, boost level of focus stream pointers (only)
  1519. if(fflag[n] == 1)
  1520. boost = 1.0 + dz->param[SHF_LBOOST];
  1521. } else {
  1522. if(fflag[n] == 1)
  1523. boost = 1.0 + ((1.0 - transfrac) * dz->param[SHF_LBOOST]);
  1524. else if(fflag[n] == 2)
  1525. boost = 1.0 + (transfrac * dz->param[SHF_LBOOST]);
  1526. }
  1527. }
  1528. obuf[opos + lmost[nn]] = (float)(obuf[opos + lmost[nn]] + (ibuf[mm][ptr[n]] * llev[nn] * boost));
  1529. obuf[opos + rmost[nn]] = (float)(obuf[opos + rmost[nn]] + (ibuf[mm][ptr[n]] * rlev[nn] * boost));
  1530. }
  1531. if(ptr[n] > 0)
  1532. ptr[n]++;
  1533. }
  1534. opos += chans;
  1535. if(opos >= dz->buflen) {
  1536. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1537. return(exit_status);
  1538. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1539. opos = 0;
  1540. }
  1541. outcnt++;
  1542. }
  1543. if(opos) {
  1544. if(normaliser < 1.0) {
  1545. for(n = 0;n < opos;n++)
  1546. obuf[n] = (float)(obuf[n] * normaliser);
  1547. }
  1548. if((exit_status = write_samps(obuf,opos,dz))<0)
  1549. return(exit_status);
  1550. }
  1551. return FINISHED;
  1552. }
  1553. /****************************** GET_MODE *********************************/
  1554. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1555. {
  1556. char temp[200], *p;
  1557. if(sscanf(str,"%s",temp)!=1) {
  1558. sprintf(errstr,"Cannot read mode of program.\n");
  1559. return(USAGE_ONLY);
  1560. }
  1561. p = temp + strlen(temp) - 1;
  1562. while(p >= temp) {
  1563. if(!isdigit(*p)) {
  1564. fprintf(stderr,"Invalid mode of program entered.\n");
  1565. return(USAGE_ONLY);
  1566. }
  1567. p--;
  1568. }
  1569. if(sscanf(str,"%d",&dz->mode)!=1) {
  1570. fprintf(stderr,"Cannot read mode of program.\n");
  1571. return(USAGE_ONLY);
  1572. }
  1573. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1574. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1575. return(USAGE_ONLY);
  1576. }
  1577. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1578. return(FINISHED);
  1579. }
  1580. /******************************** GEN_TIMES_ARRAY **************************
  1581. *
  1582. * Time placement of a beat cycle in b beat cycle.
  1583. * e.g. a = 11 : b = 13 a beats are placed at
  1584. *
  1585. * 13/11 = 1+2/11 26/11 = 2+4/11 39/11 = 3+6/11 etc
  1586. *
  1587. * "cycledur" is the duration of the entire 13-beat (and 11-beat) cycle
  1588. *
  1589. * itimes stores the samplecnt-times of the 11-beats-in-13, approxed to nearest 1/12 beat
  1590. */
  1591. void gen_times_array(int a, int b, int *itimes, int cycledur, dataptr dz)
  1592. {
  1593. int n = 0, m;
  1594. double real_timestep = (double)a/(double)b, fraction, twelfths;
  1595. while(n < (double)a/2.0) {
  1596. fraction = (real_timestep * n)/(double)a; // fraction of cycle
  1597. twelfths = (int)round(fraction * dz->param[SHF_SUBDIV]); // fraction to nearest 1/SUBDIV beats in 1/SUBDIV-beats
  1598. fraction = twelfths/dz->param[SHF_SUBDIV]; // fraction to nearest 1/SUBDIV beats
  1599. itimes[n]= (int)round(cycledur * fraction);// time within cycle, to nearest 1/12 beat
  1600. n++;
  1601. } // | | | | | a4 (EVEN), array has value at centre
  1602. m = n-1; // | | | |
  1603. if(EVEN(a)) { //
  1604. itimes[n++] = cycledur/2; // | | | | a3 (ODD), array has NO value at centre
  1605. }
  1606. while(n < a) { // | | | | |
  1607. itimes[n] = cycledur - itimes[m];
  1608. n++; // Make times are symmetrical around centre-position
  1609. m--;
  1610. }
  1611. }
  1612. void gen_regular_times_array(int a, int *itimes, double cycledur, dataptr dz)
  1613. {
  1614. int n = 0;
  1615. double fraction, real_timestep = cycledur/(double)a, srate = (double)dz->infile->srate;
  1616. while(n < a) {
  1617. fraction = real_timestep * n; // fraction of cycle
  1618. itimes[n]= (int)round(fraction * srate);
  1619. n++;
  1620. }
  1621. }
  1622. /********************************** GET_MAX_OVERLAP *********************************************
  1623. *
  1624. * Find maximum self-overlay of input sounds, so we can have duplicate pointers.
  1625. */
  1626. int get_max_overlap(int maxcycle,int maxinfiledur,int *cycle,dataptr dz)
  1627. {
  1628. double srate = (double)dz->infile->srate;
  1629. double min_cycunit_dur;
  1630. int overlay, maxoverlay, n;
  1631. if(dz->mode == 0) {
  1632. min_cycunit_dur = ((double)dz->param[SHF_CYCDUR]/(double)maxcycle); // Division of cycleduration by largest cycle count gives fastest repeat time
  1633. overlay = (int)ceil(dz->duration/min_cycunit_dur); // Division of largest infile by this gives maxoverlay
  1634. } else {
  1635. maxoverlay = 0;
  1636. for(n=0;n<dz->infilecnt;n++) {
  1637. min_cycunit_dur = ((double)dz->param[SHF_CYCDUR]/(double)cycle[n]); // Division of cycleduration by largest cycle count gives fastest repeat time
  1638. overlay = (int)ceil(((double)dz->insams[n]/srate)/min_cycunit_dur); // Division of largest infile by this gives maxoverlay
  1639. maxoverlay = max(maxoverlay,overlay);
  1640. }
  1641. overlay = maxoverlay;
  1642. }
  1643. return overlay;
  1644. }
  1645. /**************************** SHIFT_PARAM_PREPROCESS ****************************
  1646. *
  1647. * cycleno = number of cycles
  1648. * ringsize = cycleno * : array for every pair of cycles
  1649. * These are to be arranged in ascending order, so for 11,12,13 ..
  1650. * 11/12,11/13,11/13, 12/11,12/12,12/13,13/11,13/12,13/13
  1651. * For 11,12,13 largest array has 13 points, so make all arrays this big
  1652. *
  1653. * INTEGER ARRAYS lmost LONG ARRAYS DOUBLE ARRAYS
  1654. * cycles | rmost | thisreltime
  1655. * | time-arrays | | perm sndptrs nextreltime llev
  1656. * | | | | | foc | | abstime | rlev
  1657. * | |-----------------------| | | | fflag | |---------|---------|---------| | | |
  1658. * | | | | | | | | | | cycleno | cycleno | cycleno | | | |
  1659. * array | | | | | | | | | | arrays | arrays | arrays | | | |
  1660. * no 0 1 to ringsize ringsize+1| 0 1 | | | 0 1 |
  1661. * | | | ringsize+2 | | | | | | | |
  1662. * | | | | ringsize+3 | | | | | | | |
  1663. * | | | | | ringsize+4 | | | | | | | |
  1664. * | | | | | | ringsize+5 | | | | | | | |
  1665. * | | cycleno | | | | | | | | | |
  1666. * cycleno | cycleno | cycleno*dupl| | | cycleno
  1667. * size | maxcycle | | cycleno | maxcycle maxcycle | | | cycleno
  1668. * | | | | | maxcycle | | | | | | | |
  1669. * | | | | | gpeventcnt
  1670. * | | | | | | cycleno*dupl
  1671. *
  1672. * time-arrays store timings of 11 beats in 13, etc for all pairs of cycles
  1673. * lmost, and rmost are leftmost,rightmost lspkrs associated with stream position in output
  1674. * perm allows cycle focus order to be permuted (-r flag)
  1675. * foc remembers the order of focusings for the 2nd pass
  1676. * fflag flags whether any pointer is in focus(1), in next-focus(2) or neither(0)
  1677. *
  1678. * sndptrs are the pointers into the source associated with each cycle
  1679. * thisreltime are the relative times of the current-event cycle with a specified focus
  1680. * nextreltime are the absolute times if the next-event cycle with a different focus
  1681. * abstime are the absolute times (possibly) adjusted for any transition between one focus and another
  1682. *
  1683. * llev and rlev are the levels on relevant left and right speakers for each cycle
  1684. * perm allows order of cycle-focus in output to be permuted
  1685. */
  1686. int shifter_param_preprocess(dataptr dz)
  1687. {
  1688. int *cycle = dz->iparray[0], *lmost = NULL, *rmost = NULL;
  1689. int n, m, k, chans = dz->iparam[SHF_OCHANS];
  1690. int *ptr = NULL;
  1691. int maxinfiledur, gpeventcnt;
  1692. double *pos = NULL, *llev = NULL, *rlev = NULL;
  1693. double leftgain, rightgain, srate = (double)dz->infile->srate;
  1694. int maxcycle = 0;
  1695. for(n = 0;n < dz->itemcnt;n++)
  1696. maxcycle = max(maxcycle,cycle[n]);
  1697. dz->ringsize = dz->itemcnt * dz->itemcnt; // Number of cycle-pairs
  1698. // integer arrays to store relative timings of events in all pairings of cycles
  1699. for(n = 1; n <= dz->ringsize;n++) {
  1700. if((dz->iparray[n] = (int *)malloc(maxcycle * sizeof(int)))==NULL) {
  1701. sprintf(errstr,"INSUFFICIENT MEMORY to create integer array %d.\n",n+1);
  1702. return(MEMORY_ERROR);
  1703. }
  1704. }
  1705. // integer arrays for leftmost and rightmost chan for pos of each cycle-output, and array for permuting infile order
  1706. for(n = 1; n <= 3;n++) {
  1707. if((dz->iparray[dz->ringsize+n] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1708. sprintf(errstr,"INSUFFICIENT MEMORY to create integer array %d.\n",dz->ringsize+n+1);
  1709. return(MEMORY_ERROR);
  1710. }
  1711. }
  1712. // Array to remember which cycle is in focus, if randperm is used
  1713. gpeventcnt = dz->iparam[SHF_CYCDUR] * (dz->iparam[SHF_LINGER] + dz->iparam[SHF_TRNSIT]); // Size of group-cycle
  1714. gpeventcnt = (int)ceil(dz->iparam[SHF_OUTDUR]/gpeventcnt) + 4; /* SAFETY */
  1715. if((dz->iparray[dz->ringsize+4] = (int *)malloc(gpeventcnt * sizeof(int)))==NULL) {
  1716. sprintf(errstr,"INSUFFICIENT MEMORY to create integer array %d.\n",dz->ringsize+5);
  1717. return(MEMORY_ERROR);
  1718. }
  1719. if((dz->lparray = (int **)malloc(((dz->itemcnt * 3)+1) * sizeof(int *)))==NULL) {
  1720. sprintf(errstr,"INSUFFICIENT MEMORY to create longs arrays.\n");
  1721. return(MEMORY_ERROR);
  1722. }
  1723. // Find maximum amount of self-overlap of input sounds, so we can have duplicate pointers into src-sound(s)
  1724. maxinfiledur = dz->insams[0];
  1725. for(n=1;n<dz->infilecnt;n++ )
  1726. maxinfiledur = max(maxinfiledur,dz->insams[n]);
  1727. dz->dupl = get_max_overlap(maxcycle,maxinfiledur,cycle,dz);
  1728. dz->dupl++; // SAFETY, as events are shifted slightly by the process, so could generate a further overlap
  1729. // long arrays for pointers to src(s) for each cycle + duplications where there could be source self-overlap
  1730. if((dz->lparray[0] = (int *)malloc((dz->itemcnt * dz->dupl) * sizeof(int)))==NULL) {
  1731. sprintf(errstr,"INSUFFICIENT MEMORY to create longs arrays for sound pointers.\n");
  1732. return(MEMORY_ERROR);
  1733. }
  1734. // Array to remember whether pointer is in focus, next-focus, or neither
  1735. if((dz->iparray[dz->ringsize+5] = (int *)malloc((dz->itemcnt*dz->dupl) * sizeof(int)))==NULL) {
  1736. sprintf(errstr,"INSUFFICIENT MEMORY to create integer array %d.\n",dz->ringsize+6);
  1737. return(MEMORY_ERROR);
  1738. }
  1739. // Integer arrays to store the absolute (param) timings of a particular focused cycle
  1740. for(n = 1; n <= dz->itemcnt * 3;n++) {
  1741. if((dz->lparray[n] = (int *)malloc(maxcycle * sizeof(int)))==NULL) {
  1742. sprintf(errstr,"INSUFFICIENT MEMORY to create long arrays for absolute sampletime of cycles.\n");
  1743. return(MEMORY_ERROR);
  1744. }
  1745. }
  1746. // 2 double array for llev, rlev
  1747. if((dz->parray = (double **)malloc(2 * sizeof(double *)))==NULL) {
  1748. sprintf(errstr,"INSUFFICIENT MEMORY to create doubles arrays.\n");
  1749. return(MEMORY_ERROR);
  1750. }
  1751. for(n=0;n<2;n++) {
  1752. if((dz->parray[n] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1753. sprintf(errstr,"INSUFFICIENT MEMORY to create doubles array %d.\n",n+1);
  1754. return(MEMORY_ERROR);
  1755. }
  1756. }
  1757. // Calculate the relative-timings of events (in samples) in cycle-combos, and store
  1758. dz->iparam[SHF_CYCDUR] = (int)round(dz->param[SHF_CYCDUR] * srate);
  1759. k = 1;
  1760. for(n = 0;n < dz->itemcnt;n++) {
  1761. for(m = 0;m < dz->itemcnt;m++) {
  1762. if(m == n)
  1763. gen_regular_times_array(cycle[n],dz->iparray[k],dz->param[SHF_CYCDUR],dz);
  1764. else
  1765. gen_times_array(cycle[n],cycle[m],dz->iparray[k],dz->iparam[SHF_CYCDUR],dz);
  1766. k++;
  1767. }
  1768. }
  1769. // Calculate spatial positions of streams, and associated LR-levels, and store
  1770. lmost = dz->iparray[dz->ringsize+1];
  1771. rmost = dz->iparray[dz->ringsize+2];
  1772. pos = dz->parray[0]; // Used initially for position, then overwritten by left level value
  1773. llev = dz->parray[0];
  1774. rlev = dz->parray[1];
  1775. if(dz->itemcnt == chans) {
  1776. for(n = 0;n<chans;n++) {
  1777. lmost[n] = n;
  1778. rmost[n] = (lmost[n] + 1) % chans;
  1779. llev[n] = 1.0;
  1780. rlev[n] = 0.0;
  1781. }
  1782. } else {
  1783. if (chans == 1) {
  1784. for(n = 0;n<dz->itemcnt;n++) {
  1785. lmost[n] = 0;
  1786. rmost[n] = 0;
  1787. llev[n] = 1.0; // Daft, but keeps same algo for all cases
  1788. rlev[n] = 0.0;
  1789. }
  1790. } else {
  1791. if(dz->vflag[SHF_LIN] || chans == 2) {
  1792. for(n = 0;n<dz->itemcnt;n++) {
  1793. pos[n] = ((chans - 1) * n)/(double)(dz->itemcnt - 1);
  1794. lmost[n] = (int)floor(pos[n]);
  1795. pos[n] -= lmost[n];
  1796. }
  1797. } else {
  1798. for(n = 0;n<dz->itemcnt;n++) {
  1799. pos[n] = (chans * n)/(double)dz->itemcnt;
  1800. lmost[n] = (int)floor(pos[n]);
  1801. pos[n] -= lmost[n];
  1802. }
  1803. }
  1804. }
  1805. for(n = 0;n<dz->itemcnt;n++) {
  1806. rmost[n] = (lmost[n] + 1) % chans;
  1807. if(flteq(pos[n],0.0)) {
  1808. rlev[n] = 0.0;
  1809. llev[n] = 1.0; // pos values overwritten by associated level values (ETC below)
  1810. } else if(flteq(pos[n],1.0)) {
  1811. rlev[n] = 1.0;
  1812. llev[n] = 0.0;
  1813. } else {
  1814. pos[n] *= 2.0;
  1815. pos[n] -= 1.0; // Change position to -1 to +1 range
  1816. pancalc(pos[n],&leftgain,&rightgain);
  1817. rlev[n] = rightgain;
  1818. llev[n] = leftgain;
  1819. }
  1820. }
  1821. }
  1822. ptr = dz->lparray[0];
  1823. for(n = 0;n<dz->itemcnt * dz->dupl;n++)
  1824. ptr[n] = 0;
  1825. return FINISHED;
  1826. }
  1827. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  1828. int handle_the_special_data(char *str,dataptr dz)
  1829. {
  1830. int cnt;
  1831. int *cycle;
  1832. FILE *fp;
  1833. double dummy;
  1834. int idummy, n, m, k;
  1835. char temp[200], *p;
  1836. if((fp = fopen(str,"r"))==NULL) {
  1837. sprintf(errstr,"Cannot open file %s to read cyclelength data.\n",str);
  1838. return(DATA_ERROR);
  1839. }
  1840. cnt = 0;
  1841. while(fgets(temp,200,fp)!=NULL) {
  1842. p = temp;
  1843. while(isspace(*p))
  1844. p++;
  1845. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1846. continue;
  1847. while(get_float_from_within_string(&p,&dummy)) {
  1848. idummy = (int)round(dummy);
  1849. if(idummy < 2) {
  1850. sprintf(errstr,"Found cyclelength value %d in file %s: Cyclelength values must be >= 2\n",idummy,str);
  1851. return(DATA_ERROR);
  1852. }
  1853. if(idummy > 32767) {
  1854. sprintf(errstr,"Found cyclelength value %d in file %s: Too large\n",idummy,str);
  1855. return(DATA_ERROR);
  1856. }
  1857. cnt++;
  1858. }
  1859. }
  1860. if(dz->infilecnt > 1 && cnt != dz->infilecnt) {
  1861. sprintf(errstr,"Number of cyclelengths (%d) found in file %s does not tally with number of src sounds (%d)\n",cnt,str,dz->infilecnt);
  1862. return(DATA_ERROR);
  1863. }
  1864. dz->itemcnt = cnt; // Number of cycles
  1865. dz->ringsize = dz->itemcnt * dz->itemcnt; // Number of cycle pairs
  1866. /******************************************************************************
  1867. *
  1868. * cycleno = number of cycles
  1869. * ringsize = cycleno * (cycleno-1) : array for every pair of cycles
  1870. * These are to be arranged in ascending order, so for 11,12,13 ..
  1871. * 11/12,11/13,12/11,12/13,13/11,13/12
  1872. * Array of 13 points in time of 12 needs 13 entries ..
  1873. * For 11,12,13 largest array has 13 points, so make all arrays this big
  1874. *
  1875. * INTEGER ARRAYS lmost
  1876. * | rmost
  1877. * cycles | | perm
  1878. * | time-arrays | | | foc
  1879. * | | | | | | fflag
  1880. * | |-----------------------| | | | | |
  1881. * array | | ringsize+1|
  1882. * no 0 1 to ringsize | ringsize+2
  1883. * | | | | ringsize+3
  1884. * | | | | | ringsize+4
  1885. * cycleno cycleno ringsize+5
  1886. * size | maxcycle | cycleno |
  1887. * | | | | cycleno
  1888. * | | | | | gpeventcnt
  1889. * | | | | | | cycleno*dupl
  1890. * | | | | | | | |
  1891. *
  1892. * time-arrays store timings of 11 beats in 13, etc for all pairs of cycles
  1893. * lmost, and rmost are leftmost,rightmost lspkrs associated with stream position in output
  1894. * perm allows permutation of focus order, for random-perm-focus option
  1895. * foc remembers which stream is focus for each group-event
  1896. * tflag marks whether pointer has focus(1) or next-focus(2) or neither(0)
  1897. */
  1898. if((dz->iparray = (int **)malloc((dz->ringsize + 6) * sizeof(int *)))==NULL) {
  1899. sprintf(errstr,"INSUFFICIENT MEMORY to create integer arrays.\n");
  1900. return(MEMORY_ERROR);
  1901. }
  1902. if((dz->iparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1903. sprintf(errstr,"INSUFFICIENT MEMORY to create cyclecnt storage array.\n");
  1904. return(MEMORY_ERROR);
  1905. }
  1906. cycle = dz->iparray[0];
  1907. cnt = 0;
  1908. fseek(fp,0,0);
  1909. while(fgets(temp,200,fp)!=NULL) {
  1910. p = temp;
  1911. while(isspace(*p))
  1912. p++;
  1913. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1914. continue;
  1915. while(get_float_from_within_string(&p,&dummy))
  1916. cycle[cnt++] = (int)round(dummy);
  1917. }
  1918. fclose(fp);
  1919. for(n=0;n < cnt-1;n++) { // Sort into ascending order
  1920. for(m = n+1;m<cnt;m++) {
  1921. if(cycle[m] == cycle[n]) {
  1922. sprintf(errstr,"Two identical cycless of length %d found.\n",cycle[n]);
  1923. return(DATA_ERROR);
  1924. } else if (cycle[m] < cycle[n]) {
  1925. k = cycle[n];
  1926. cycle[n] = cycle[m];
  1927. cycle[m] = k;
  1928. }
  1929. }
  1930. }
  1931. return FINISHED;
  1932. }
  1933. /************************************ PANCALC *******************************/
  1934. void pancalc(double position,double *leftgain,double *rightgain)
  1935. {
  1936. int dirflag;
  1937. double temp;
  1938. double relpos;
  1939. double reldist, invsquare;
  1940. if(position < 0.0)
  1941. dirflag = SIGNAL_TO_LEFT; /* signal on left */
  1942. else
  1943. dirflag = SIGNAL_TO_RIGHT;
  1944. if(position < 0)
  1945. relpos = -position;
  1946. else
  1947. relpos = position;
  1948. if(relpos <= 1.0){ /* between the speakers */
  1949. temp = 1.0 + (relpos * relpos);
  1950. reldist = ROOT2 / sqrt(temp);
  1951. temp = (position + 1.0) / 2.0;
  1952. *rightgain = temp * reldist;
  1953. *leftgain = (1.0 - temp ) * reldist;
  1954. } else { /* outside the speakers */
  1955. temp = (relpos * relpos) + 1.0;
  1956. reldist = sqrt(temp) / ROOT2; /* relative distance to source */
  1957. invsquare = 1.0 / (reldist * reldist);
  1958. if(dirflag == SIGNAL_TO_LEFT){
  1959. *leftgain = invsquare;
  1960. *rightgain = 0.0;
  1961. } else { /* SIGNAL_TO_RIGHT */
  1962. *rightgain = invsquare;
  1963. *leftgain = 0;
  1964. }
  1965. }
  1966. }
  1967. /**************************** CREATE_SHIFTER_SNDBUFS ****************************/
  1968. int create_shifter_sndbufs(dataptr dz)
  1969. {
  1970. int n, safety = 4;
  1971. unsigned int lastbigbufsize, bigbufsize = 0;
  1972. float *bottom;
  1973. dz->bufcnt = dz->infilecnt+1;
  1974. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  1975. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  1976. return(MEMORY_ERROR);
  1977. }
  1978. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  1979. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  1980. return(MEMORY_ERROR);
  1981. }
  1982. lastbigbufsize = 0;
  1983. bigbufsize = 0;
  1984. for(n=0;n<dz->infilecnt;n++) {
  1985. bigbufsize += (dz->insams[n] + safety) * sizeof(float);
  1986. if(bigbufsize < lastbigbufsize) {
  1987. sprintf(errstr,"Insufficient memory to store the input soundfiles in buffers.\n");
  1988. return(MEMORY_ERROR);
  1989. }
  1990. lastbigbufsize = bigbufsize;
  1991. }
  1992. dz->buflen = NTEX_OBUFSIZE * dz->iparam[SHF_OCHANS];
  1993. bigbufsize += (dz->buflen + (safety * dz->iparam[SHF_OCHANS])) * sizeof(float);
  1994. if(bigbufsize < lastbigbufsize) {
  1995. sprintf(errstr,"Insufficient memory to store the input soundfiles in buffers.\n");
  1996. return(MEMORY_ERROR);
  1997. }
  1998. if((dz->bigbuf = (float *)malloc(bigbufsize)) == NULL) {
  1999. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  2000. return(PROGRAM_ERROR);
  2001. }
  2002. bottom = dz->bigbuf;
  2003. for(n = 0;n<dz->infilecnt;n++) {
  2004. dz->sbufptr[n] = dz->sampbuf[n] = bottom;
  2005. bottom += dz->insams[n] + safety;
  2006. }
  2007. dz->sbufptr[n] = dz->sampbuf[n] = bottom;
  2008. return(FINISHED);
  2009. }
  2010. /*********************** RNDINTPERM ************************/
  2011. void rndintperm(int *perm,int cnt)
  2012. {
  2013. int n,t,k;
  2014. memset((char *)perm,0,cnt * sizeof(int));
  2015. for(n=0;n<cnt+1;n++) {
  2016. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  2017. if(t==n) {
  2018. for(k=n;k>0;k--)
  2019. perm[k] = perm[k-1];
  2020. perm[0] = n;
  2021. } else {
  2022. for(k=n;k>t;k--)
  2023. perm[k] = perm[k-1];
  2024. perm[t] = n;
  2025. }
  2026. }
  2027. for(n=0;n<cnt;n++)
  2028. perm[n]--;
  2029. }
  2030. /************************ HANDLE_THE_OUTFILE *********************/
  2031. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  2032. {
  2033. char *filename = (*cmdline)[0], *p;
  2034. if(filename[0]=='-' && filename[1]=='f') {
  2035. dz->floatsam_output = 1;
  2036. dz->true_outfile_stype = SAMP_FLOAT;
  2037. filename+= 2;
  2038. }
  2039. if(!sloom) {
  2040. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  2041. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  2042. return(DATA_ERROR);
  2043. }
  2044. }
  2045. p = filename; // Drop file extension
  2046. while(*p != ENDOFSTR) {
  2047. if(*p == '.') {
  2048. *p = ENDOFSTR;
  2049. break;
  2050. }
  2051. p++;
  2052. }
  2053. strcpy(dz->outfilename,filename);
  2054. (*cmdline)++;
  2055. (*cmdlinecnt)--;
  2056. return(FINISHED);
  2057. }
  2058. /************************ OPEN_THE_OUTFILE *********************/
  2059. int open_the_outfile(dataptr dz)
  2060. {
  2061. int exit_status;
  2062. dz->infile->channels = dz->iparam[SHF_OCHANS];
  2063. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  2064. return(exit_status);
  2065. dz->infile->channels = 1;
  2066. return(FINISHED);
  2067. }