newmix.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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. *
  23. * THIS IS A MODEL for a program that USES THE CDP LIBRARIES
  24. * but where those LIBS DO NOT NEED TO BE ALTERED.
  25. *
  26. * CMDLINE must have STANDARD CDP FORM
  27. *
  28. * ALL FUNCTIONS that would have needed CDP libraries to be updated
  29. * are replaced by local functions in the code, to avoid recompiling CDP libs.
  30. *
  31. * All lib functions that need to be satisfied, but are not actively used, are here as DUMMIES
  32. *
  33. * cdparams_other() AND tkusage_other() NEED TO BE UPDATED
  34. * standalone.h NEEDS TO BE UPDATED
  35. * gobo AND gobosee NEED TO BE UPDATED
  36. *
  37. *********************************************
  38. *
  39. * THIS PARTICULAR FUNCTION USES A NEW FILETYPE (multichannel mixfile)
  40. * AND HENCE REQUIRED MODS TO cdparse() (IMPLEMENTED)
  41. *
  42. *********************************************
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <structures.h>
  47. #include <tkglobals.h>
  48. #include <pnames.h>
  49. #include <filetype.h>
  50. #include <processno.h>
  51. #include <modeno.h>
  52. #include <logic.h>
  53. #include <globcon.h>
  54. #include <cdpmain.h>
  55. #include <math.h>
  56. #include <mixxcon.h>
  57. #include <osbind.h>
  58. #include <standalone.h>
  59. #include <ctype.h>
  60. #include <sfsys.h>
  61. #include <string.h>
  62. #include <srates.h>
  63. #if defined unix || defined __GNUC__
  64. #define round(x) lround((x))
  65. #endif
  66. #ifndef HUGE
  67. #define HUGE 3.40282347e+38F
  68. #endif
  69. char errstr[2400];
  70. int anal_infiles = 0;
  71. int sloom = 0;
  72. int sloombatch = 0;
  73. const char* cdp_version = "7.1.0";
  74. /* STRUCTURES SPECIFIC TO THIS APPLICATION */
  75. struct actvalm {
  76. int ifd; /* file from which to read samples */
  77. int bufno; /* these must be assigned before we start using this struct */
  78. int samplen; /* number of samples produced from whole file */
  79. int inchans; /* channels in infile */
  80. int route[513]; /* This is the routing pairs from MAX_OUTCHANS inchans to MAX_OUTCHANS outchans */
  81. double level[256]; /* this is weighting of each possible routing */
  82. } ;
  83. typedef struct actvalm *actvalmptr;
  84. struct actionm {
  85. int position; /* time of action, in samples */
  86. actvalmptr val; /* all associated values, stored in an actval */
  87. int role; /* ON , REFILL, or OFF */
  88. } ;
  89. typedef struct actionm *actmptr;
  90. /* CDP LIBRARY FUNCTIONS TRANSFERRED HERE */
  91. static int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist);
  92. static int set_vflgs(aplptr ap,char *optflags,int optcnt,char *optlist,
  93. char *varflags,int vflagcnt, int vparamcnt,char *varlist);
  94. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  95. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  96. static int mark_parameter_types(dataptr dz,aplptr ap);
  97. static int establish_application(dataptr dz);
  98. static int application_init(dataptr dz);
  99. static int initialise_vflags(dataptr dz);
  100. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  101. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  102. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  103. static int assign_file_data_storage(int infilecnt,dataptr dz);
  104. static int store_wordlist(char *filename,dataptr dz);
  105. /* CDP LIB FUNCTION MODIFIED TO AVOID CALLING setup_particular_application() */
  106. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  107. /* SIMPLIFICATION OF LIB FUNC TO APPLY TO JUST THIS FUNCTION */
  108. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  109. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz);
  110. static int setup_newmix_application(dataptr dz);
  111. static int setup_newmix_param_ranges_and_defaults(dataptr dz);
  112. /* BYPASS LIBRARY GLOBAL FUNCTION TO GO DIRECTLY TO SPECIFIC APPLIC FUNCTIONS */
  113. static int create_mix_buffers(dataptr dz);
  114. static int new_mix_preprocess(actmptr **act,dataptr dz);
  115. static int new_mmix(actmptr *act,dataptr dz);
  116. static int set_up_newmix(actmptr **act,dataptr dz);
  117. /* FUNCTIONS SPECIFIC TO THIS APLIC */
  118. static int do_refill_acts(actmptr **act,dataptr dz);
  119. static void sort_actions(actmptr **act,dataptr dz);
  120. static int init_inbufs(dataptr dz);
  121. static int establish_action_value_storage_for_mix(dataptr dz);
  122. static int open_file_and_get_props(int filecnt,char *filename,int *srate,int chans,dataptr dz);
  123. static int allocate_actions(actmptr **act,int filecnt,dataptr dz);
  124. static int unmark_freeable_bufs(actmptr **f,int **bufsflag,int *bufsflagcnt,int startpos,int this,int longbitsize,dataptr dz);
  125. static int get_free_buf(int **bufsflag,int *bufsflagcnt,int longbitsize,int *thisbuf,dataptr dz);
  126. static int unmark_buf(int **bufsflag,int *bufsflagcnt,int thisbuf,int longbitsize);
  127. static int mark_buf(int **bufsflag,int *bufsflagcnt,int thisbuf,int longbitsize,dataptr dz);
  128. static int reallocate_bufsflag(int z,int **bufsflag,int *bufsflagcnt);
  129. static int adjust_check_mixdata_in_line(int chans,double *level,int *route,int outchans,int entrycnt,int lineno,dataptr dz);
  130. static int get_newmixdata_in_line(int total_words,double *time,int *chans,
  131. double *level,int *route,int filecnt,int *entrycnt,dataptr dz);
  132. static int check_level(char *str,double *level,int lineno,int entrycnt);
  133. static int adjust_buffer_status(actmptr *act,int n,int *thispos,int *active_bufcnt,dataptr dz);
  134. static int read_samps_to_an_inbuf(actmptr *act,int n,dataptr dz);
  135. static int adjust_activebufs_list(int active_bufcnt,dataptr dz);
  136. static int do_mix(int samps_to_mix,int *position_in_outbuf,int active_bufcnt,int *outbuf_space,dataptr dz);
  137. static int do_silence(int samps_to_mix,int *position_in_outbuf,int *outbuf_space,dataptr dz);
  138. static int mix_read_samps(actmptr *act,float *inbuf,int samps_to_read,int n,dataptr dz);
  139. static int IsNumeric(char *str);
  140. static actvalmptr *valstor;
  141. //static actmptr *act;
  142. /**************************************** MAIN *********************************************/
  143. int main(int argc,char *argv[])
  144. {
  145. int exit_status;
  146. dataptr dz = NULL;
  147. char **cmdline;
  148. int cmdlinecnt;
  149. //aplptr ap;
  150. int is_launched = FALSE;
  151. actmptr *act;
  152. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  153. fprintf(stdout,"%s\n",cdp_version);
  154. fflush(stdout);
  155. return 0;
  156. }
  157. /* CHECK FOR SOUNDLOOM */
  158. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  159. sloom = 0;
  160. sloombatch = 1;
  161. }
  162. if(sflinit("cdp")){
  163. sfperror("cdp: initialisation\n");
  164. return(FAILED);
  165. }
  166. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  167. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  168. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  169. return(FAILED);
  170. }
  171. if(!sloom) {
  172. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  173. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. cmdline = argv;
  177. cmdlinecnt = argc;
  178. // THERE IS NO MODE WITH THIS PROGRAM
  179. if (!strcmp(argv[0],"multichan")) {
  180. dz->process = MIXMULTI;
  181. dz->mode = 0;
  182. } else
  183. usage1();
  184. cmdline++;
  185. cmdlinecnt--;
  186. // setup_particular_application =
  187. if((exit_status = setup_newmix_application(dz))<0) {
  188. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  189. return(FAILED);
  190. }
  191. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  192. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  193. return(FAILED);
  194. }
  195. } else {
  196. //parse_TK_data() =
  197. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  198. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(exit_status);
  200. }
  201. }
  202. //ap = dz->application;
  203. // parse_infile_and_hone_type() =
  204. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  205. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  206. return(FAILED);
  207. }
  208. dz->itemcnt = dz->out_chans;
  209. // setup_param_ranges_and_defaults() =
  210. if((exit_status = setup_newmix_param_ranges_and_defaults(dz))<0) {
  211. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  212. return(FAILED);
  213. }
  214. //open_first_infile =
  215. if((exit_status = store_wordlist(cmdline[0],dz))<0) {
  216. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  217. return(FAILED);
  218. }
  219. cmdlinecnt--;
  220. cmdline++;
  221. // handle_extra_infiles() : redundant
  222. // handle_outfile() =
  223. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) {
  224. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  225. return(FAILED);
  226. }
  227. // handle_formants() redundant
  228. // handle_formant_quiksearch() redundant
  229. // handle_special_data() redundant
  230. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  231. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  232. return(FAILED);
  233. }
  234. //check_param_validity_and_consistency() =
  235. if((exit_status = set_up_newmix(&act,dz))<0) {
  236. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  237. return(FAILED);
  238. }
  239. is_launched = TRUE;
  240. //allocate_large_buffers() =
  241. if((exit_status = create_mix_buffers(dz))<0){
  242. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  243. return(FAILED);
  244. }
  245. //param_preprocess() =
  246. if((exit_status = new_mix_preprocess(&act,dz))<0) {
  247. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  248. return(FAILED);
  249. }
  250. //groucho_process_file =
  251. if((exit_status = new_mmix(act,dz))<0) {
  252. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  253. return(FAILED);
  254. }
  255. if((exit_status = complete_output(dz))<0) { // CDP LIB
  256. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  257. return(FAILED);
  258. }
  259. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  260. free(dz);
  261. return(SUCCEEDED);
  262. }
  263. /**********************************************
  264. REPLACED CDP LIB FUNCTIONS
  265. **********************************************/
  266. /****************************** SET_PARAM_DATA *********************************/
  267. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  268. {
  269. ap->special_data = (char)special_data;
  270. ap->param_cnt = (char)paramcnt;
  271. ap->max_param_cnt = (char)maxparamcnt;
  272. if(ap->max_param_cnt>0) {
  273. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  274. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  275. return(MEMORY_ERROR);
  276. }
  277. strcpy(ap->param_list,paramlist);
  278. }
  279. return(FINISHED);
  280. }
  281. /****************************** SET_VFLGS *********************************/
  282. int set_vflgs
  283. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  284. {
  285. ap->option_cnt = (char) optcnt; /*RWD added cast */
  286. if(optcnt) {
  287. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  288. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  289. return(MEMORY_ERROR);
  290. }
  291. strcpy(ap->option_list,optlist);
  292. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  293. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  294. return(MEMORY_ERROR);
  295. }
  296. strcpy(ap->option_flags,optflags);
  297. }
  298. ap->vflag_cnt = (char) vflagcnt;
  299. ap->variant_param_cnt = (char) vparamcnt;
  300. if(vflagcnt) {
  301. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  302. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  303. return(MEMORY_ERROR);
  304. }
  305. strcpy(ap->variant_list,varlist);
  306. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  307. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  308. return(MEMORY_ERROR);
  309. }
  310. strcpy(ap->variant_flags,varflags);
  311. }
  312. return(FINISHED);
  313. }
  314. /***************************** APPLICATION_INIT **************************/
  315. int application_init(dataptr dz)
  316. {
  317. int exit_status;
  318. int storage_cnt;
  319. int tipc;
  320. aplptr ap = dz->application;
  321. if(ap->vflag_cnt>0)
  322. initialise_vflags(dz);
  323. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  324. ap->total_input_param_cnt = (char)tipc;
  325. if(tipc>0) {
  326. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  327. return(exit_status);
  328. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  329. return(exit_status);
  330. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  331. return(exit_status);
  332. }
  333. //THERE ARE NO brktables USED IN THIS PROCESS
  334. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  335. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  336. return(exit_status);
  337. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  338. return(exit_status);
  339. }
  340. if((exit_status = mark_parameter_types(dz,ap))<0)
  341. return(exit_status);
  342. // establish_infile_constants() replaced by
  343. dz->infilecnt = ONE_NONSND_FILE;
  344. //establish_bufptrs_and_extra_buffers(): not required, handled by set_up_newmix()
  345. // setup_internal_arrays_and_array_pointers(): not required
  346. return(FINISHED);
  347. }
  348. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  349. /* RWD mallo changed to calloc; helps debug verison run as release! */
  350. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  351. {
  352. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  353. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  354. return(MEMORY_ERROR);
  355. }
  356. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  357. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  358. return(MEMORY_ERROR);
  359. }
  360. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  361. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  362. return(MEMORY_ERROR);
  363. }
  364. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  365. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  366. return(MEMORY_ERROR);
  367. }
  368. return(FINISHED);
  369. }
  370. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  371. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  372. {
  373. int n;
  374. for(n=0;n<storage_cnt;n++) {
  375. dz->is_int[n] = (char)0;
  376. dz->no_brk[n] = (char)0;
  377. }
  378. return(FINISHED);
  379. }
  380. /***************************** MARK_PARAMETER_TYPES **************************/
  381. int mark_parameter_types(dataptr dz,aplptr ap)
  382. {
  383. int n, m; /* PARAMS */
  384. for(n=0;n<ap->max_param_cnt;n++) {
  385. switch(ap->param_list[n]) {
  386. case('0'): break; /* dz->is_active[n] = 0 is default */
  387. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  388. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  389. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  390. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  391. default:
  392. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  393. return(PROGRAM_ERROR);
  394. }
  395. } /* OPTIONS */
  396. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  397. switch(ap->option_list[n]) {
  398. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  399. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  400. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  401. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  402. default:
  403. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  404. return(PROGRAM_ERROR);
  405. }
  406. } /* VARIANTS */
  407. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  408. switch(ap->variant_list[n]) {
  409. case('0'): break;
  410. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  411. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  412. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  413. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  414. default:
  415. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  416. return(PROGRAM_ERROR);
  417. }
  418. } /* INTERNAL */
  419. for(n=0,
  420. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  421. switch(ap->internal_param_list[n]) {
  422. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  423. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  424. case('d'): dz->no_brk[m] = (char)1; break;
  425. default:
  426. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  427. return(PROGRAM_ERROR);
  428. }
  429. }
  430. return(FINISHED);
  431. }
  432. /***************************** HANDLE_THE_OUTFILE **************************/
  433. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz)
  434. {
  435. char *filename = NULL;
  436. if(!sloom && (*cmdlinecnt<=0)) {
  437. sprintf(errstr,"Insufficient cmdline parameters.\n");
  438. return(USAGE_ONLY);
  439. }
  440. filename = (*cmdline)[0];
  441. if(filename[0]=='-' && filename[1]=='f') {
  442. dz->floatsam_output = 1;
  443. dz->true_outfile_stype = SAMP_FLOAT;
  444. filename+= 2;
  445. }
  446. strcpy(dz->outfilename,filename);
  447. (*cmdline)++;
  448. (*cmdlinecnt)--;
  449. return(FINISHED);
  450. }
  451. /***************************** ESTABLISH_APPLICATION **************************/
  452. int establish_application(dataptr dz)
  453. {
  454. aplptr ap;
  455. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  456. sprintf(errstr,"establish_application()\n");
  457. return(MEMORY_ERROR);
  458. }
  459. ap = dz->application;
  460. memset((char *)ap,0,sizeof(struct applic));
  461. return(FINISHED);
  462. }
  463. /************************* INITIALISE_VFLAGS *************************/
  464. int initialise_vflags(dataptr dz)
  465. {
  466. int n;
  467. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  468. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  469. return(MEMORY_ERROR);
  470. }
  471. for(n=0;n<dz->application->vflag_cnt;n++)
  472. dz->vflag[n] = FALSE;
  473. return FINISHED;
  474. }
  475. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  476. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  477. {
  478. int n;
  479. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  480. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  481. return(MEMORY_ERROR);
  482. }
  483. for(n=0;n<tipc;n++)
  484. ap->default_val[n] = 0.0;
  485. return(FINISHED);
  486. }
  487. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  488. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  489. {
  490. int n;
  491. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  492. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  493. return(MEMORY_ERROR);
  494. }
  495. for(n=0;n<tipc;n++)
  496. dz->is_active[n] = (char)0;
  497. return(FINISHED);
  498. }
  499. /************************* SETUP_NEWMIX_APPLICATION *******************/
  500. int setup_newmix_application(dataptr dz)
  501. {
  502. int exit_status;
  503. aplptr ap;
  504. if((exit_status = establish_application(dz))<0) // GLOBAL
  505. return(FAILED);
  506. ap = dz->application;
  507. // SEE parstruct FOR EXPLANATION of next 2 functions
  508. if((exit_status = set_param_data(ap,0 ,0,0,"" ))<0)
  509. return(FAILED);
  510. if((exit_status = set_vflgs(ap, "seg",3,"ddd" ,"" ,0,0,"" ))<0)
  511. return(FAILED);
  512. // THERE IS NO NEED TO set_formant_flags in this case....
  513. // Following only needed if internal params are linked to dz structure
  514. if((exit_status = set_internalparam_data("iii",ap))<0)
  515. return(FAILED);
  516. // set_legal_infile_structure -->
  517. dz->has_otherfile = FALSE;
  518. // assign_process_logic -->
  519. dz->input_data_type = MIXFILES_ONLY;
  520. dz->process_type = UNEQUAL_SNDFILE;
  521. dz->outfiletype = SNDFILE_OUT;
  522. return application_init(dz); //GLOBAL
  523. }
  524. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  525. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  526. {
  527. int exit_status;
  528. infileptr infile_info;
  529. if(!sloom) {
  530. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  531. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  532. return(MEMORY_ERROR);
  533. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  534. sprintf(errstr,"Failed tp parse input file %s\n",cmdline[0]);
  535. return(PROGRAM_ERROR);
  536. } else if(infile_info->filetype != MIX_MULTI) {
  537. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  538. return(DATA_ERROR);
  539. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  540. sprintf(errstr,"Failed to copy file parsing information\n");
  541. return(PROGRAM_ERROR);
  542. }
  543. free(infile_info);
  544. }
  545. return(FINISHED);
  546. }
  547. /************************* SETUP_NEWMIX_PARAM_RANGES_AND_DEFAULTS *******************/
  548. int setup_newmix_param_ranges_and_defaults(dataptr dz)
  549. {
  550. int exit_status;
  551. aplptr ap = dz->application;
  552. // set_param_ranges()
  553. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  554. // NB total_input_param_cnt is > 0 !!!s
  555. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  556. return(FAILED);
  557. // get_param_ranges()
  558. ap->lo[MIX_START] = 0.0;
  559. ap->hi[MIX_START] = dz->duration;
  560. ap->lo[MIX_END] = 0.0;
  561. ap->hi[MIX_END] = dz->duration;
  562. ap->lo[MIX_ATTEN] = 0.0;
  563. ap->hi[MIX_ATTEN] = 1.0;
  564. // initialise_param_values()
  565. ap->default_val[MIX_START] = 0.0;
  566. ap->default_val[MIX_END] = dz->duration;
  567. ap->default_val[MIX_ATTEN] = 1.0;
  568. if(!sloom)
  569. put_default_vals_in_all_params(dz);
  570. return(FINISHED);
  571. }
  572. /************************* STORE_WORDLIST *****************************/
  573. int store_wordlist(char *filename,dataptr dz)
  574. {
  575. char temp[1000],*p,*q;
  576. int n;
  577. int total_wordcnt = 0;
  578. int wordcnt_in_line;
  579. int line_cnt = 0;
  580. FILE *fp;
  581. if((dz->wordstor = (char **)malloc(dz->all_words * sizeof(char *)))==NULL) {
  582. sprintf(errstr,"INSUFFICIENT MEMORY for wordstores.\n");
  583. return(MEMORY_ERROR);
  584. }
  585. for(n=0;n<dz->all_words;n++) /* initialise, for testing and safe freeing */
  586. dz->wordstor[n] = NULL;
  587. if((dz->wordcnt = (int *)malloc(dz->linecnt * sizeof(int)))==NULL) {
  588. sprintf(errstr,"INSUFFICIENT MEMORY for line wordcounts.\n");
  589. return(MEMORY_ERROR);
  590. }
  591. if((fp = fopen(filename,"r"))==NULL) {
  592. sprintf(errstr,"Failed to open file %s for input.\n",filename);
  593. return(DATA_ERROR);
  594. }
  595. while(fgets(temp,1000,fp)!=NULL) {
  596. p = temp;
  597. if(is_an_empty_line_or_a_comment(p))
  598. continue;
  599. wordcnt_in_line = 0;
  600. while(get_word_from_string(&p,&q)) {
  601. if((dz->wordstor[total_wordcnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  602. sprintf(errstr,"INSUFFICIENT MEMORY for wordstor %d\n",total_wordcnt+1);
  603. return(MEMORY_ERROR);
  604. }
  605. strcpy(dz->wordstor[total_wordcnt],q);
  606. total_wordcnt++;
  607. wordcnt_in_line++;
  608. }
  609. dz->wordcnt[line_cnt] = wordcnt_in_line;
  610. line_cnt++;
  611. }
  612. if(dz->infile->filetype==SNDLIST) { /* reorganise snds in sndlist onto separate lines */
  613. if(dz->linecnt != dz->all_words) {
  614. if((dz->wordcnt = (int *)realloc(dz->wordcnt,dz->all_words * sizeof(int)))==NULL) {
  615. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate word counts.\n");
  616. return(MEMORY_ERROR);
  617. }
  618. dz->linecnt = dz->all_words;
  619. for(n=0;n<dz->linecnt;n++)
  620. dz->wordcnt[n] = 1;
  621. }
  622. }
  623. fclose(fp);
  624. return(FINISHED);
  625. }
  626. /********************************* PARSE_SLOOM_DATA *********************************/
  627. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  628. {
  629. int exit_status;
  630. int cnt = 1, infilecnt;
  631. int filesize, insams, inbrksize;
  632. double dummy;
  633. int true_cnt = 0;
  634. //aplptr ap;
  635. while(cnt<=PRE_CMDLINE_DATACNT) {
  636. if(cnt > argc) {
  637. sprintf(errstr,"Insufficient data sent from TK\n");
  638. return(DATA_ERROR);
  639. }
  640. switch(cnt) {
  641. case(1):
  642. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  643. sprintf(errstr,"Cannot read process no. sent from TK\n");
  644. return(DATA_ERROR);
  645. }
  646. break;
  647. case(2):
  648. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  649. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  650. return(DATA_ERROR);
  651. }
  652. if(dz->mode > 0)
  653. dz->mode--;
  654. //setup_particular_application() =
  655. if((exit_status = setup_newmix_application(dz))<0)
  656. return(exit_status);
  657. //ap = dz->application;
  658. break;
  659. case(3):
  660. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  661. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  662. return(DATA_ERROR);
  663. }
  664. if(infilecnt < 1) {
  665. true_cnt = cnt + 1;
  666. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  667. }
  668. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  669. return(exit_status);
  670. break;
  671. case(INPUT_FILETYPE+4):
  672. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  673. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  674. return(DATA_ERROR);
  675. }
  676. break;
  677. case(INPUT_FILESIZE+4):
  678. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  679. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  680. return(DATA_ERROR);
  681. }
  682. dz->insams[0] = filesize;
  683. break;
  684. case(INPUT_INSAMS+4):
  685. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  686. sprintf(errstr,"Cannot read insams sent from TK\n");
  687. return(DATA_ERROR);
  688. }
  689. dz->insams[0] = insams;
  690. break;
  691. case(INPUT_SRATE+4):
  692. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  693. sprintf(errstr,"Cannot read srate sent from TK\n");
  694. return(DATA_ERROR);
  695. }
  696. break;
  697. case(INPUT_CHANNELS+4):
  698. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  699. sprintf(errstr,"Cannot read channels sent from TK\n");
  700. return(DATA_ERROR);
  701. }
  702. break;
  703. case(INPUT_STYPE+4):
  704. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  705. sprintf(errstr,"Cannot read stype sent from TK\n");
  706. return(DATA_ERROR);
  707. }
  708. break;
  709. case(INPUT_ORIGSTYPE+4):
  710. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  711. sprintf(errstr,"Cannot read origstype sent from TK\n");
  712. return(DATA_ERROR);
  713. }
  714. break;
  715. case(INPUT_ORIGRATE+4):
  716. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  717. sprintf(errstr,"Cannot read origrate sent from TK\n");
  718. return(DATA_ERROR);
  719. }
  720. break;
  721. case(INPUT_MLEN+4):
  722. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  723. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  724. return(DATA_ERROR);
  725. }
  726. break;
  727. case(INPUT_DFAC+4):
  728. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  729. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  730. return(DATA_ERROR);
  731. }
  732. break;
  733. case(INPUT_ORIGCHANS+4):
  734. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  735. sprintf(errstr,"Cannot read origchans sent from TK\n");
  736. return(DATA_ERROR);
  737. }
  738. break;
  739. case(INPUT_SPECENVCNT+4):
  740. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  741. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  742. return(DATA_ERROR);
  743. }
  744. dz->specenvcnt = dz->infile->specenvcnt;
  745. break;
  746. case(INPUT_WANTED+4):
  747. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  748. sprintf(errstr,"Cannot read wanted sent from TK\n");
  749. return(DATA_ERROR);
  750. }
  751. break;
  752. case(INPUT_WLENGTH+4):
  753. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  754. sprintf(errstr,"Cannot read wlength sent from TK\n");
  755. return(DATA_ERROR);
  756. }
  757. break;
  758. case(INPUT_OUT_CHANS+4):
  759. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  760. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  761. return(DATA_ERROR);
  762. }
  763. break;
  764. /* RWD these chanegs to samps - tk will have to deal with that! */
  765. case(INPUT_DESCRIPTOR_BYTES+4):
  766. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  767. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  768. return(DATA_ERROR);
  769. }
  770. break;
  771. case(INPUT_IS_TRANSPOS+4):
  772. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  773. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  774. return(DATA_ERROR);
  775. }
  776. break;
  777. case(INPUT_COULD_BE_TRANSPOS+4):
  778. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  779. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  780. return(DATA_ERROR);
  781. }
  782. break;
  783. case(INPUT_COULD_BE_PITCH+4):
  784. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  785. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  786. return(DATA_ERROR);
  787. }
  788. break;
  789. case(INPUT_DIFFERENT_SRATES+4):
  790. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  791. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  792. return(DATA_ERROR);
  793. }
  794. break;
  795. case(INPUT_DUPLICATE_SNDS+4):
  796. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  797. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  798. return(DATA_ERROR);
  799. }
  800. break;
  801. case(INPUT_BRKSIZE+4):
  802. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  803. sprintf(errstr,"Cannot read brksize sent from TK\n");
  804. return(DATA_ERROR);
  805. }
  806. if(inbrksize > 0) {
  807. switch(dz->input_data_type) {
  808. case(WORDLIST_ONLY):
  809. break;
  810. case(PITCH_AND_PITCH):
  811. case(PITCH_AND_TRANSPOS):
  812. case(TRANSPOS_AND_TRANSPOS):
  813. dz->tempsize = inbrksize;
  814. break;
  815. case(BRKFILES_ONLY):
  816. case(UNRANGED_BRKFILE_ONLY):
  817. case(DB_BRKFILES_ONLY):
  818. case(ALL_FILES):
  819. case(ANY_NUMBER_OF_ANY_FILES):
  820. if(dz->extrabrkno < 0) {
  821. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  822. return(DATA_ERROR);
  823. }
  824. if(dz->brksize == NULL) {
  825. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  826. return(PROGRAM_ERROR);
  827. }
  828. dz->brksize[dz->extrabrkno] = inbrksize;
  829. break;
  830. default:
  831. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  832. dz->input_data_type);
  833. return(PROGRAM_ERROR);
  834. }
  835. break;
  836. }
  837. break;
  838. case(INPUT_NUMSIZE+4):
  839. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  840. sprintf(errstr,"Cannot read numsize sent from TK\n");
  841. return(DATA_ERROR);
  842. }
  843. break;
  844. case(INPUT_LINECNT+4):
  845. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  846. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  847. return(DATA_ERROR);
  848. }
  849. break;
  850. case(INPUT_ALL_WORDS+4):
  851. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  852. sprintf(errstr,"Cannot read all_words sent from TK\n");
  853. return(DATA_ERROR);
  854. }
  855. break;
  856. case(INPUT_ARATE+4):
  857. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  858. sprintf(errstr,"Cannot read arate sent from TK\n");
  859. return(DATA_ERROR);
  860. }
  861. break;
  862. case(INPUT_FRAMETIME+4):
  863. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  864. sprintf(errstr,"Cannot read frametime sent from TK\n");
  865. return(DATA_ERROR);
  866. }
  867. dz->frametime = (float)dummy;
  868. break;
  869. case(INPUT_WINDOW_SIZE+4):
  870. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  871. sprintf(errstr,"Cannot read window_size sent from TK\n");
  872. return(DATA_ERROR);
  873. }
  874. break;
  875. case(INPUT_NYQUIST+4):
  876. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  877. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  878. return(DATA_ERROR);
  879. }
  880. break;
  881. case(INPUT_DURATION+4):
  882. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  883. sprintf(errstr,"Cannot read duration sent from TK\n");
  884. return(DATA_ERROR);
  885. }
  886. break;
  887. case(INPUT_MINBRK+4):
  888. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  889. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  890. return(DATA_ERROR);
  891. }
  892. break;
  893. case(INPUT_MAXBRK+4):
  894. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  895. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  896. return(DATA_ERROR);
  897. }
  898. break;
  899. case(INPUT_MINNUM+4):
  900. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  901. sprintf(errstr,"Cannot read minnum sent from TK\n");
  902. return(DATA_ERROR);
  903. }
  904. break;
  905. case(INPUT_MAXNUM+4):
  906. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  907. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  908. return(DATA_ERROR);
  909. }
  910. break;
  911. default:
  912. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  913. return(PROGRAM_ERROR);
  914. }
  915. cnt++;
  916. }
  917. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  918. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  919. return(DATA_ERROR);
  920. }
  921. if(true_cnt)
  922. cnt = true_cnt;
  923. *cmdlinecnt = 0;
  924. while(cnt < argc) {
  925. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  926. return(exit_status);
  927. cnt++;
  928. }
  929. return(FINISHED);
  930. }
  931. /********************************* GET_TK_CMDLINE_WORD *********************************/
  932. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  933. {
  934. if(*cmdlinecnt==0) {
  935. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  936. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  937. return(MEMORY_ERROR);
  938. }
  939. } else {
  940. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  941. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  942. return(MEMORY_ERROR);
  943. }
  944. }
  945. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  946. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  947. return(MEMORY_ERROR);
  948. }
  949. strcpy((*cmdline)[*cmdlinecnt],q);
  950. (*cmdlinecnt)++;
  951. return(FINISHED);
  952. }
  953. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  954. int assign_file_data_storage(int infilecnt,dataptr dz)
  955. {
  956. int exit_status;
  957. int no_sndfile_system_files = FALSE;
  958. dz->infilecnt = infilecnt;
  959. if((exit_status = allocate_filespace(dz))<0)
  960. return(exit_status);
  961. if(no_sndfile_system_files)
  962. dz->infilecnt = 0;
  963. return(FINISHED);
  964. }
  965. /************************* redundant functions: to ensure libs compile OK *******************/
  966. int assign_process_logic(dataptr dz)
  967. {
  968. return(FINISHED);
  969. }
  970. void set_legal_infile_structure(dataptr dz)
  971. {}
  972. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  973. {
  974. return(FINISHED);
  975. }
  976. int setup_internal_arrays_and_array_pointers(dataptr dz)
  977. {
  978. return(FINISHED);
  979. }
  980. int establish_bufptrs_and_extra_buffers(dataptr dz)
  981. {
  982. return(FINISHED);
  983. }
  984. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  985. {
  986. return(FINISHED);
  987. }
  988. int read_special_data(char *str,dataptr dz)
  989. {
  990. return(FINISHED);
  991. }
  992. int inner_loop
  993. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  994. {
  995. return(FINISHED);
  996. }
  997. /*************************** CREATE_MIX_BUFFERS **************************
  998. *
  999. * Input files are read into a special buffer, then converted to the format
  1000. * of the outfile. This may involve an increase or decrease in the number of
  1001. * channels.
  1002. * Basic buffersize therefore needs an extra factor relating to the max number of channels
  1003. * found in both input files and output files.
  1004. * dz->itemcnt stores this value.
  1005. * There are ....
  1006. * 1) One buffer to store any infile data before converting it to the outfile format.
  1007. * 2) a buffer for each infile AFTER conversion to outfile format...
  1008. * During the course of the mix, buffers no longer in use by any current infile, may be reused ....
  1009. * (the max number of buffers needed is counted before we get to this function,
  1010. * and the count stored in dz->bufcnt: (buffer management is described in act[]).
  1011. * 3) an output buffer,
  1012. */
  1013. int create_mix_buffers(dataptr dz)
  1014. {
  1015. int exit_status, ratio;
  1016. size_t bigbufsize;
  1017. size_t bloksize = SECSIZE * dz->out_chans; /* 1 */
  1018. /* buffers for infiles have been counted while reading mixfile */
  1019. dz->bufcnt++; /* output buffer */
  1020. /* if any infile has MORE chans than outfile, allow for this */
  1021. ratio = (int)ceil((double)dz->itemcnt/(double)dz->out_chans);
  1022. dz->bufcnt += ratio; /* This defines size for a SINGLE (input-conversion) buffer */
  1023. /* used for converting infile format to outfile format */
  1024. bloksize = dz->bufcnt * bloksize;
  1025. bigbufsize = (size_t)Malloc(-1);
  1026. if((bigbufsize = (bigbufsize/bloksize) * bloksize) <= 0)
  1027. bigbufsize = bloksize;
  1028. if((dz->bigbuf = (float *)Malloc(bigbufsize)) == NULL) {
  1029. sprintf(errstr, "INSUFFICIENT MEMORY to create sound buffers.\n");
  1030. return(MEMORY_ERROR);
  1031. }
  1032. dz->buflen = (int)(bigbufsize/sizeof(float)/dz->bufcnt); /* length of floats buffers */
  1033. if((exit_status = establish_groucho_bufptrs_and_extra_buffers(dz))<0)
  1034. return(exit_status);
  1035. dz->sampbuf[OBUFMIX] = dz->bigbuf;
  1036. dz->sampbuf[STEREOBUF] = dz->bigbuf + dz->buflen;
  1037. /* this defines size of input-conversion buffer */
  1038. dz->sampbuf[IBUFMIX] = dz->sampbuf[STEREOBUF] + (dz->buflen * ratio);
  1039. /* post-conversion input buffers start at IBUFMAX */
  1040. dz->bufcnt -= (1 + ratio); /* bufcnt becomes a count of post-conversion input-bufs only */
  1041. return(FINISHED);
  1042. }
  1043. /******************************** USAGE1 ********************************/
  1044. int usage1(void)
  1045. {
  1046. sprintf(errstr,
  1047. "USAGE: newmix multichan mixfile outsndfile [-sSTART] [-eEND] [-gATTENUATION]\n\n"
  1048. "START gives starttime START (to start mixing later than zero).\n"
  1049. "END gives output endtime END (to stop mix before its true end).\n"
  1050. "Note that the START and END params are intended for mix TESTING purposes only.\n"
  1051. "If you want to keep output from such a testmix, you should TOPNTAIL it.\n"
  1052. "\n"
  1053. "Mixfile for multichannel work is same as standard CDP mixfiles BUT...\n"
  1054. "1) There is an extra initial line that states no. of output chans.\n"
  1055. "2) On ensuing lines, input and output channels are numbered 1,2,3 etc\n"
  1056. "3) Routing of input to output is indicated by inchan, colon, outchan\n"
  1057. " e.g. 1:4 sends input channel 1 to ouptut channel 4\n"
  1058. "5) The levels on these channels are in the range -1 to 1\n"
  1059. "6) An input channel must be routed to an output channel, with a level\n"
  1060. " e.g. 1:1 1.0 2:4 .5 (input 1 to output 1, input 2 to output 4)\n"
  1061. "7) You can route an input to many outs e,g 1:1 .5 1:2 .3 1:4 .7 etc.\n"
  1062. "8) You must take care with levels, where more than 1 input goes to same output.\n");
  1063. return(USAGE_ONLY);
  1064. }
  1065. /******************************** USAGE2 ********************************/
  1066. int usage2(char *str)
  1067. {
  1068. return usage1();
  1069. }
  1070. /******************************** USAGE3 ********************************/
  1071. int usage3(char *str1,char *str2)
  1072. {
  1073. sprintf(errstr,"Insufficient parameters on command line.\n");
  1074. return(USAGE_ONLY);
  1075. }
  1076. /************************** MIX_PREPROCESS ******************/
  1077. int new_mix_preprocess(actmptr **act,dataptr dz)
  1078. {
  1079. int exit_status;
  1080. fprintf(stdout,"INFO: Establishing mixing structures: This may take a few moments!!\n");
  1081. fflush(stdout);
  1082. if((exit_status = do_refill_acts(act,dz))<0)
  1083. return(exit_status);
  1084. sort_actions(act,dz);
  1085. if(dz->iparam[MIX_STRTPOS] > 0) /* Allow for mix not starting at zero time */
  1086. dz->iparam[MIX_STRTPOS_IN_ACTION] = (int)max(dz->iparam[MIX_STRTPOS] - (*act)[0]->position,0L);
  1087. dz->tempsize = ((*act)[dz->iparam[MIX_TOTAL_ACTCNT]-1]->position - dz->iparam[MIX_STRTPOS_IN_ACTION]);
  1088. return init_inbufs(dz);
  1089. }
  1090. /*************************** DO_REFILL_ACTS **************************/
  1091. int do_refill_acts(actmptr **act,dataptr dz)
  1092. {
  1093. int n;
  1094. int samps_used, actcnt = dz->iparam[MIX_TOTAL_ACTCNT];
  1095. int arraysize = actcnt + BIGARRAY; /* Generate more space for further actions. */
  1096. if((*act=(actmptr *)realloc(*act,arraysize * sizeof(actmptr)))==NULL) {
  1097. sprintf(errstr,"INSUFFICIENT MEMORY to construct buffer-fill action pointers.\n");
  1098. return(MEMORY_ERROR);
  1099. }
  1100. for(n=0;n<dz->iparam[MIX_TOTAL_ACTCNT];n+=2) { /* actions paired as ON/OFF, so we look only at ON bufs */
  1101. if((*act)[n]->val->samplen > dz->buflen) { /* If more data in (action's) file than fits in 1 buffer */
  1102. samps_used = 0;
  1103. while((samps_used += dz->buflen) < (*act)[n]->val->samplen) {
  1104. if(((*act)[actcnt] = (actmptr)malloc(sizeof(struct actionm)))==NULL) {
  1105. sprintf(errstr,"INSUFFICIENT MEMORY to construct buffer-fill actions.\n");
  1106. return(MEMORY_ERROR);
  1107. }
  1108. (*act)[actcnt]->val = (*act)[n]->val; /* Create a new action, using same vals as the original */
  1109. (*act)[actcnt]->position = (*act)[n]->position + samps_used;/* Positioned 1 further buflen later, and */
  1110. (*act)[actcnt]->role = MIX_ACTION_REFILL; /* with role REFILL */
  1111. if(++actcnt >= arraysize) {
  1112. arraysize += BIGARRAY;
  1113. if((*act=(actmptr *)realloc(*act,arraysize * sizeof(actmptr)))==NULL) {
  1114. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate buffer-fill action pointers.\n");
  1115. return(MEMORY_ERROR);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. if((*act=(actmptr *)realloc(*act,actcnt * sizeof(actmptr)))==NULL) {
  1122. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate buffer-fill actions.\n");
  1123. return(MEMORY_ERROR);
  1124. }
  1125. dz->iparam[MIX_TOTAL_ACTCNT] = (int)actcnt;
  1126. return(FINISHED);
  1127. }
  1128. /************************* SORT_ACTIONS ****************************/
  1129. void sort_actions(actmptr **act,dataptr dz)
  1130. {
  1131. actmptr temp;
  1132. int n, m;
  1133. for(n=0;n<dz->iparam[MIX_TOTAL_ACTCNT]-1;n++) {
  1134. for(m=n+1;m<dz->iparam[MIX_TOTAL_ACTCNT];m++) {
  1135. if((*act)[m]->position < (*act)[n]->position) {
  1136. temp = (*act)[n];
  1137. (*act)[n] = (*act)[m];
  1138. (*act)[m] = temp;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. /****************************** INIT_INBUFS *********************************
  1144. *
  1145. * (1) Create the space for buffers within each bufitem. NB enough space
  1146. * for ALL these buffers must be allocated in the initial creation
  1147. * of 'inbuf' in create_buffers.
  1148. * (2) Create the pointers to point to start and current_position in ACTIVE bufs.
  1149. */
  1150. int init_inbufs(dataptr dz)
  1151. {
  1152. int n;
  1153. float *thisbuf = dz->sampbuf[IBUFMIX];
  1154. dz->buflist = (mixbufptr *)malloc(dz->bufcnt * sizeof(mixbufptr));
  1155. for(n=0;n<dz->bufcnt;n++) {
  1156. dz->buflist[n] = NULL;
  1157. if((dz->buflist[n] = (mixbufptr)malloc(sizeof(struct bufitem)))==NULL) {
  1158. sprintf(errstr,"INSUFFICIENT MEMORY for buffer list.\n");
  1159. return(MEMORY_ERROR);
  1160. }
  1161. dz->buflist[n]->status = MIX_ACTION_OFF;
  1162. dz->buflist[n]->buf = thisbuf;
  1163. dz->buflist[n]->here = dz->buflist[n]->buf;
  1164. thisbuf += dz->buflen;
  1165. }
  1166. if((dz->activebuf = (int *)malloc(dz->bufcnt * sizeof(int)))==NULL) {
  1167. sprintf(errstr,"INSUFFICIENT MEMORY for active buffer list.\n");
  1168. return(MEMORY_ERROR);
  1169. }
  1170. if((dz->activebuf_ptr = (float **)malloc(dz->bufcnt * sizeof(float *)))==NULL) {
  1171. sprintf(errstr,"INSUFFICIENT MEMORY for active buffer pointer list.\n");
  1172. return(MEMORY_ERROR);
  1173. }
  1174. return(FINISHED);
  1175. }
  1176. /********************** SET_UP_MIX ****************************/
  1177. int set_up_newmix(actmptr **act,dataptr dz)
  1178. {
  1179. int exit_status;
  1180. double time, level[256];
  1181. int chans;
  1182. int route[513], k;
  1183. int startpos, endpos, lineno, j;
  1184. int *bufsflag = NULL;
  1185. int bufsflagcnt = 0;
  1186. double filetime, eventend;
  1187. int longbitsize = sizeof(int) * CHARBITSIZE;
  1188. int srate=0, end_of_mix = -1;
  1189. int mix_end_set = FALSE;
  1190. int mix_end_specified = FALSE;
  1191. int mix_start_set = FALSE;
  1192. char *filename;
  1193. int filecnt = 0, entrycnt = 0;
  1194. int total_words = 0;
  1195. int outchans = dz->out_chans;
  1196. if(dz->param[MIX_START] >= dz->param[MIX_END]) {
  1197. sprintf(errstr,"Mix starts after it ends.\n");
  1198. return(USER_ERROR);
  1199. }
  1200. dz->iparam[MIX_STRTPOS] = 0;
  1201. dz->iparam[MIX_STRTPOS_IN_ACTION] = 0;
  1202. dz->iparam[MIX_TOTAL_ACTCNT] = 0;
  1203. dz->bufcnt = 0; /* buffers are allocated in "mark_buf" */
  1204. if((exit_status = establish_file_data_storage_for_mix(dz->linecnt,dz))<0)
  1205. return(exit_status);
  1206. if((exit_status = establish_action_value_storage_for_mix(dz))<0)
  1207. return(exit_status);
  1208. total_words++; /* 1st word = first line = outchan count */
  1209. for(lineno=1;lineno<dz->linecnt;lineno++) { /* for each mixfile line */
  1210. memset((char *)route,0,513 * sizeof(int));
  1211. filename = dz->wordstor[total_words]; /* get the filename */
  1212. if(strlen(filename)<=0) {
  1213. sprintf(errstr,"filename error: line %d: set_up_newmix()\n",lineno+1);
  1214. return(PROGRAM_ERROR);
  1215. }
  1216. if((exit_status = get_newmixdata_in_line(total_words,&time,&chans,&(level[0]),&(route[0]),lineno,&entrycnt,dz))<0)
  1217. return(exit_status); /* read the mixfile line data */
  1218. if(chans > dz->itemcnt)
  1219. dz->itemcnt = chans;
  1220. total_words += dz->wordcnt[lineno]; /* tally of all words in file, acts as base to measure start of next line */
  1221. if(time >= dz->param[MIX_END]) /* If line starts after specified mix end, ignore */
  1222. continue;
  1223. if((exit_status =
  1224. adjust_check_mixdata_in_line(chans,&(level[0]),&(route[0]),dz->out_chans,entrycnt,lineno,dz))<0)
  1225. return(exit_status); /* do any attenuation required by input para, */
  1226. /* & check routing compatible with infile and outfile channel counts */
  1227. if((exit_status = open_file_and_get_props(filecnt,filename,&srate,chans,dz))<0)
  1228. return(exit_status); /* check infile specified exists, is a sndfile, contains data, */
  1229. /* has channel count specified in mix line */
  1230. /* and has srate same as all other soundfiles in mix */
  1231. filetime = (double)(dz->insams[filecnt]/chans)/(double)srate;
  1232. if((eventend = time + filetime) < dz->param[MIX_START]) /* If line ends before specified mix start, ignore */
  1233. continue;
  1234. if(!mix_start_set) { /* if mix start param not set yet, set it */
  1235. dz->iparam[MIX_STRTPOS] = (int)round(dz->param[MIX_START] * (double)(srate * dz->out_chans));
  1236. dz->iparam[MIX_STRTPOS_IN_ACTION] = dz->iparam[MIX_STRTPOS];
  1237. mix_start_set = TRUE;
  1238. }
  1239. if(!mix_end_set) { /* if mix end param not set yet, set it */
  1240. if(!flteq(dz->param[MIX_END],dz->application->default_val[MIX_END])) { /* if mixend param given by user */
  1241. end_of_mix = round(dz->param[MIX_END] * (double)srate) * dz->out_chans;
  1242. if(end_of_mix < 0) {
  1243. sprintf(errstr,"Error in end_of_mix logic: set_up_newmix()\n");
  1244. return(PROGRAM_ERROR);
  1245. }
  1246. mix_end_specified = TRUE;
  1247. }
  1248. mix_end_set = TRUE;
  1249. }
  1250. if((exit_status = allocate_actions(act,filecnt,dz))<0) /* assign values to mix ON and OFF actions */
  1251. return(exit_status);
  1252. startpos = round(time * (double)srate); /* startposition (in grouped samples), of this file */
  1253. startpos *= outchans; /* startposition, in output samples, of this file */
  1254. dz->insams[filecnt] /= chans; /* length of input file, in grouped-samples */
  1255. dz->insams[filecnt] *= outchans; /* EFFECTIVE sample-dur of file in output */
  1256. if((exit_status = unmark_freeable_bufs /* finds buffers which are not (any longer) in use */
  1257. (act,&bufsflag,&bufsflagcnt,startpos,dz->iparam[MIX_TOTAL_ACTCNT],longbitsize,dz))<0)
  1258. return(exit_status);
  1259. if((exit_status = get_free_buf(&bufsflag,&bufsflagcnt,longbitsize,&(valstor[filecnt]->bufno),dz))<0)
  1260. return(exit_status); /* Allocate a buffer (for htis file) which is not currently in use */
  1261. endpos = startpos + dz->insams[filecnt]; /* Find end-of-current-file in output stream */
  1262. if(mix_end_specified && (end_of_mix < endpos)) { /* If file ends aftert mix ends */
  1263. endpos = end_of_mix; /* curtail (effective) length */
  1264. dz->insams[filecnt] = end_of_mix - startpos;
  1265. }
  1266. valstor[filecnt]->samplen = dz->insams[filecnt]; /* store (effective) length */
  1267. valstor[filecnt]->inchans = chans; /* store infile channel-cnt */
  1268. for(j=0;j<entrycnt;j++) {
  1269. k = j * 2;
  1270. (valstor[filecnt])->route[k] = route[k]; /* store the in->out channel routing for this file */
  1271. k++;
  1272. (valstor[filecnt])->route[k] = route[k]; /* store the in->out channel routing for this file */
  1273. (valstor[filecnt]->level)[j] = level[j]; /* store the levels for each channel routing, for this file */
  1274. }
  1275. valstor[filecnt]->ifd = dz->ifd[filecnt]; /* store file-pointer to input file */
  1276. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]++]->position = startpos; /* store outputstream-position of mix start */
  1277. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]++]->position = endpos; /* store outputstream-position of mix end */
  1278. filecnt++; /* count the ACTUALLY USED lines, for which files have been opened */
  1279. }
  1280. if(!mix_end_set || filecnt==0) {
  1281. sprintf(errstr,"No mixfile line is active within the time limits specified.\n");
  1282. return(DATA_ERROR);
  1283. }
  1284. if(bufsflagcnt)
  1285. free(bufsflag);
  1286. dz->bufcnt++; /* bufcnt is number assigned to highest assigned buf, COUNTING FROM ZERO */
  1287. /* Hence, actual bufcnt is 1 more than this */
  1288. dz->infile->channels = dz->out_chans; /* output channels(evenutally derived from dz->infile->channels) */
  1289. //TW SET UP AFTER OUTCHANS KNOWN
  1290. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  1291. return(exit_status);
  1292. return(FINISHED);
  1293. }
  1294. /**************************** ESTABLISH_FILE_DATA_STORAGE_FOR_MIX ********************************/
  1295. int establish_file_data_storage_for_mix(int filecnt,dataptr dz)
  1296. {
  1297. int n;
  1298. if(dz->insams!=NULL)
  1299. free(dz->insams); /* in TK insams[0] also used in parse accounting */
  1300. if(dz->ifd!=NULL)
  1301. free(dz->ifd);
  1302. if((dz->insams = (int *)malloc(filecnt * sizeof(int)))==NULL) {
  1303. sprintf(errstr,"INSUFFICIENT MEMORY to allocate infile samplesize array.\n");
  1304. return(MEMORY_ERROR);
  1305. }
  1306. if((dz->ifd = (int *)malloc(filecnt * sizeof(int)))==NULL) {
  1307. sprintf(errstr,"INSUFFICIENT MEMORY to allocate infile pointers array.\n");
  1308. return(MEMORY_ERROR);
  1309. }
  1310. for(n=0;n<filecnt;n++)
  1311. dz->ifd[n] = -1;
  1312. return(FINISHED);
  1313. }
  1314. /**************************** ESTABLISH_ACTION_VALUE_STORAGE_FOR_MIX ********************************/
  1315. int establish_action_value_storage_for_mix(dataptr dz)
  1316. {
  1317. int n;
  1318. // Comment: valstor is declarted as an "actvalptr", but this is just an address
  1319. // so we're using it to point to an "actvalmptr"
  1320. if((valstor = (actvalmptr *)malloc(dz->linecnt * sizeof(actvalmptr)))==NULL) {
  1321. sprintf(errstr,"INSUFFICIENT MEMORY for mix action values store.\n");
  1322. return(MEMORY_ERROR);
  1323. }
  1324. for(n=0;n<dz->linecnt;n++) {
  1325. valstor[n] = NULL;
  1326. /* RWD 4 Dec 2010 the usual uninitialised data again.... */
  1327. if((valstor[n]=(actvalmptr) calloc(1,sizeof(struct actvalm)))==NULL) {
  1328. sprintf(errstr,"INSUFFICIENT MEMORY for mix action value store %d\n",n+1);
  1329. return(MEMORY_ERROR);
  1330. }
  1331. }
  1332. return(FINISHED);
  1333. }
  1334. /**************************** GET_MIXDATA_IN_LINE ********************************/
  1335. int get_newmixdata_in_line(int total_words,double *time,int *chans,
  1336. double *level,int *route,int lineno,int *entrycnt,dataptr dz)
  1337. {
  1338. int exit_status, here, is_level, n, test;
  1339. int wordcnt = dz->wordcnt[lineno], got_colon, in_chan, out_chan;
  1340. char temp[2000];
  1341. char *p;
  1342. char *q, *z;
  1343. double thislevel;
  1344. z = 0; // non-valid inital value: z is address of 1st char of out_chan in an in-chan_out-chan pair e.g. address of '6' in '3:6'
  1345. in_chan = 0; // non-valid inital value: in_chan is the input channel in an in-chan_out-chan pair e.g. 3 in '3:6'
  1346. test = wordcnt - 3;
  1347. if((test <= 0) || ODD(test)) {
  1348. here = total_words;
  1349. strcpy(temp,dz->wordstor[here]);
  1350. sprintf(errstr,"Wrong number of entries on line %d : %s\n",lineno+1,temp);
  1351. return(PROGRAM_ERROR);
  1352. }
  1353. if(sscanf(dz->wordstor[total_words+1],"%lf",time)!=1
  1354. || sscanf(dz->wordstor[total_words+2],"%d",chans)!=1) {
  1355. sprintf(errstr,"Error scanning data: line %d %s %s\n",lineno+1,dz->wordstor[total_words+1],dz->wordstor[total_words+2]);
  1356. return(PROGRAM_ERROR);
  1357. }
  1358. here = total_words+3;
  1359. is_level = 0;
  1360. *entrycnt = 0;
  1361. while(here < total_words + wordcnt) {
  1362. if(*entrycnt >= 256) {
  1363. sprintf(errstr,"Too many channel routing columns in line %d\n",lineno+1);
  1364. return(DATA_ERROR);
  1365. }
  1366. if(is_level) {
  1367. if((exit_status = check_level(dz->wordstor[here],&thislevel,lineno,*entrycnt))<0)
  1368. return(exit_status);
  1369. *(level + *entrycnt) = thislevel;
  1370. } else {
  1371. p = dz->wordstor[here];
  1372. q = p + strlen(dz->wordstor[here]);
  1373. got_colon = 0;
  1374. while(p < q) {
  1375. if(!isdigit(*p)) {
  1376. if(p == dz->wordstor[here])
  1377. return(DATA_ERROR);
  1378. else if(got_colon)
  1379. return(DATA_ERROR);
  1380. else if(*p != ':')
  1381. return(DATA_ERROR);
  1382. else {
  1383. *p = ENDOFSTR;
  1384. in_chan = atoi(dz->wordstor[here]);
  1385. *p = ':';
  1386. z = p+1;
  1387. got_colon = 1;
  1388. }
  1389. }
  1390. p++;
  1391. }
  1392. if(!got_colon || z == p)
  1393. return(DATA_ERROR);
  1394. out_chan = atoi(z);
  1395. if(in_chan > *chans || in_chan < 1)
  1396. return(DATA_ERROR);
  1397. if(out_chan > dz->out_chans)
  1398. return(DATA_ERROR);
  1399. n = (*entrycnt) * 2;
  1400. route[n++] = in_chan;
  1401. route[n] = out_chan;
  1402. }
  1403. if(is_level)
  1404. (*entrycnt)++;
  1405. is_level = !is_level;
  1406. here++;
  1407. }
  1408. return(FINISHED);
  1409. }
  1410. /**************************** ADJUST_CHECK_MIXDATA_IN_LINE ********************************/
  1411. int adjust_check_mixdata_in_line(int chans,double *level,int *route,int outchans,int entrycnt,int lineno,dataptr dz)
  1412. {
  1413. int len = entrycnt * 2, n, inchanroute, outchanroute;
  1414. if(chans > MAX_OUTCHAN) {
  1415. sprintf(errstr,"channel count %d greater than maximum (%d): line %d\n",chans,MAX_OUTCHAN,lineno+1);
  1416. return(DATA_ERROR);
  1417. }
  1418. if(!(flteq(dz->param[MIX_ATTEN],1.0))) { /* do any pre-attenuation of the entire mix, specified by atten parameter */
  1419. for(n=0;n<entrycnt;n++)
  1420. level[n] *= dz->param[MIX_ATTEN];
  1421. }
  1422. for(n = 0;n < len; n+=2) { /* check compatibility routing mnemonics with channel cnt of infile & outfile */
  1423. inchanroute = route[n];
  1424. outchanroute = route[n+1];
  1425. if(inchanroute > chans) {
  1426. sprintf(errstr,
  1427. "channel routing %d:%d uses input channel %d: there are only %d input channels: line %d\n",
  1428. inchanroute,outchanroute,inchanroute,chans,lineno+1);
  1429. return(DATA_ERROR);
  1430. }
  1431. if(outchanroute > outchans) {
  1432. sprintf(errstr,
  1433. "output channel count %d (implied by route %d:%d) greater than actual output channels (%d): line %d\n",
  1434. outchanroute,inchanroute,outchanroute,dz->out_chans,lineno+1);
  1435. return(DATA_ERROR);
  1436. }
  1437. }
  1438. return(FINISHED);
  1439. }
  1440. /************************* OPEN_FILE_AND_GET_PROPS *******************************/
  1441. int open_file_and_get_props(int filecnt,char *filename,int *srate,int chans,dataptr dz)
  1442. {
  1443. int exit_status;
  1444. double maxamp, maxloc;
  1445. int maxrep;
  1446. int getmax = 0, getmaxinfo = 0;
  1447. infileptr ifp;
  1448. if((ifp = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  1449. sprintf(errstr,"INSUFFICIENT MEMORY to store data on files.\n");
  1450. return(MEMORY_ERROR);
  1451. }
  1452. if((dz->ifd[filecnt] = sndopenEx(filename,0,CDP_OPEN_RDONLY)) < 0) {
  1453. sprintf(errstr,"Failed to open sndfile %s\n",filename);
  1454. return(SYSTEM_ERROR);
  1455. }
  1456. if((exit_status = readhead(ifp,dz->ifd[filecnt],filename,&maxamp,&maxloc,&maxrep,getmax,getmaxinfo))<0)
  1457. return(exit_status);
  1458. copy_to_fileptr(ifp,dz->infile);
  1459. if(dz->infile->filetype!=SNDFILE) {
  1460. sprintf(errstr,"Non soundfile encountered [%s]: open_file_and_get_props()\n",filename);
  1461. return(PROGRAM_ERROR);
  1462. }
  1463. if(dz->infile->channels!=chans) {
  1464. sprintf(errstr,"Incorrect channels found [%s]: open_file_and_get_props()\n",filename);
  1465. return(PROGRAM_ERROR);
  1466. }
  1467. if(filecnt==0)
  1468. *srate = dz->infile->srate;
  1469. else if(dz->infile->srate != *srate) {
  1470. sprintf(errstr,"incompatible srates: [file %s] open_file_and_get_props()\n",filename);
  1471. return(PROGRAM_ERROR);
  1472. }
  1473. if((dz->insams[filecnt] = sndsizeEx(dz->ifd[filecnt]))<0) {
  1474. sprintf(errstr, "Can't read size of input file %s: open_file_and_get_props()\n",filename);
  1475. return(PROGRAM_ERROR);
  1476. }
  1477. if(dz->insams[filecnt] <=0) {
  1478. sprintf(errstr, "Zero size for input file %s: open_file_and_get_props()\n",filename);
  1479. return(PROGRAM_ERROR);
  1480. }
  1481. return(FINISHED);
  1482. }
  1483. /*********************** ALLOCATE_ACTIONS *************************/
  1484. int allocate_actions(actmptr **act,int filecnt,dataptr dz)
  1485. {
  1486. int new_total = dz->iparam[MIX_TOTAL_ACTCNT] + 2;
  1487. if(dz->iparam[MIX_TOTAL_ACTCNT]==0) {
  1488. if((*act = (actmptr *)malloc(new_total * sizeof(actmptr)))==NULL) {
  1489. sprintf(errstr,"INSUFFICIENT MEMORY for mix actions store.\n");
  1490. return(MEMORY_ERROR);
  1491. }
  1492. } else {
  1493. if((*act=(actmptr *)realloc(*act,new_total * sizeof(actmptr)))==NULL) {
  1494. sprintf(errstr,"INSUFFICIENT MEMORY ro reallocate mix actions store.\n");
  1495. return(MEMORY_ERROR);
  1496. }
  1497. }
  1498. if(((*act)[dz->iparam[MIX_TOTAL_ACTCNT]] = (actmptr)malloc(sizeof(struct actionm)))==NULL) {
  1499. sprintf(errstr,"INSUFFICIENT MEMORY for final mix action store.\n");
  1500. return(MEMORY_ERROR);
  1501. }
  1502. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]]->val = valstor[filecnt];
  1503. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]]->role = MIX_ACTION_ON;
  1504. if(((*act)[dz->iparam[MIX_TOTAL_ACTCNT]+1] = (actmptr)malloc(sizeof(struct actionm)))==NULL) {
  1505. sprintf(errstr,"INSUFFICIENT MEMORY for further mix action store.\n");
  1506. return(MEMORY_ERROR);
  1507. }
  1508. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]+1]->val = valstor[filecnt];
  1509. (*act)[dz->iparam[MIX_TOTAL_ACTCNT]+1]->role = MIX_ACTION_OFF;
  1510. return(FINISHED);
  1511. }
  1512. /************************* UNMARK_FREEABLE_BUFS ***********************
  1513. *
  1514. * (1) If a buffer has been switched off BEFORE now, then it is
  1515. * available for use!! unmark it!!
  1516. * (2) If a buffer is subsequently turned back on, this catches it!!
  1517. * A buffer can ONLY be turned back on LATER (!), and is hence
  1518. * LATER in this list EVEN though it is not yet fully time-sorted!! (1998 ???)
  1519. */
  1520. int unmark_freeable_bufs(actmptr **act,int **bufsflag,int *bufsflagcnt,int startpos,int this,int longbitsize,dataptr dz)
  1521. {
  1522. int exit_status;
  1523. int n;
  1524. for(n=0;n<this;n++) {
  1525. switch((*act)[n]->role) {
  1526. case(MIX_ACTION_ON): /* 2 */
  1527. if((exit_status = mark_buf(bufsflag,bufsflagcnt,(*act)[n]->val->bufno,longbitsize,dz))<0)
  1528. return(exit_status);
  1529. break;
  1530. case(MIX_ACTION_OFF): /* 1 */
  1531. if((*act)[n]->position < startpos) {
  1532. if((exit_status = unmark_buf(bufsflag,bufsflagcnt,(*act)[n]->val->bufno,longbitsize))<0)
  1533. return(exit_status);
  1534. }
  1535. break;
  1536. }
  1537. }
  1538. return(FINISHED);
  1539. }
  1540. /************************** GET_FREE_BUF ***************************
  1541. *
  1542. * Get the FIRST available free buffer.
  1543. * (1) Going through each long.
  1544. * (2) Set the mask to start of long.
  1545. * (3) For each position in the long..
  1546. * (4) If that byte is NOT set, break, thisbuf counts which byte
  1547. * and therefore which buffer it is.
  1548. * (5) Advance the mask.
  1549. * (6) Advance the buffer counter.
  1550. * (7) Set the appropriate bit for this buffer, return buffer no.
  1551. */
  1552. int get_free_buf(int **bufsflag,int *bufsflagcnt,int longbitsize,int *thisbuf,dataptr dz)
  1553. {
  1554. int exit_status;
  1555. int y, z, got_it = 0;
  1556. int mask;
  1557. *thisbuf = 0;
  1558. for(z=0;z<*bufsflagcnt;z++) { /* 1 */
  1559. mask = 1; /* 2 */
  1560. for(y=0;y<longbitsize;y++) { /* 3 */
  1561. if(!(mask & (*bufsflag)[z])) { /* 4 */
  1562. got_it = 1;
  1563. break;
  1564. }
  1565. mask <<= 1; /* 5 */
  1566. (*thisbuf)++; /* 6 */
  1567. }
  1568. if(got_it)
  1569. break;
  1570. }
  1571. if((exit_status = mark_buf(bufsflag,bufsflagcnt,*thisbuf,longbitsize,dz))<0) /* 7 */
  1572. return(exit_status);
  1573. return(FINISHED);
  1574. }
  1575. /************************** CHECK_LEVEL ************************/
  1576. int check_level(char *str,double *level,int lineno,int entrycnt)
  1577. {
  1578. if(is_dB(str)) {
  1579. if(!get_leveldb(str,level)) {
  1580. sprintf(errstr,"Failed to find (dB) level %d: line %d\n",entrycnt+1,lineno+1);
  1581. return(PROGRAM_ERROR);
  1582. }
  1583. } else if(!(IsNumeric(str))) {
  1584. sprintf(errstr,"Failed to find level %d: line %d\n",entrycnt+1,lineno+1);
  1585. return(DATA_ERROR);
  1586. } else {
  1587. if(sscanf(str,"%lf",level)!=1) {
  1588. sprintf(errstr,"Failed to find level %d: line %d\n",entrycnt+1,lineno+1);
  1589. return(PROGRAM_ERROR);
  1590. }
  1591. }
  1592. // FEB 2007
  1593. // if(*level < 0.0) {
  1594. // sprintf(errstr,"Level %d less than 0.0: line %d\n",entrycnt+1,lineno+1);
  1595. // return(PROGRAM_ERROR);
  1596. // }
  1597. return(FINISHED);
  1598. }
  1599. /************************** UNMARK_BUF ***************************/
  1600. int unmark_buf(int **bufsflag,int *bufsflagcnt,int thisbuf,int longbitsize)
  1601. {
  1602. int exit_status;
  1603. int mask = 1;
  1604. int z = thisbuf/longbitsize;
  1605. /* 1998 --> */
  1606. if(z >= *bufsflagcnt) {
  1607. if((exit_status = reallocate_bufsflag(z,bufsflag,bufsflagcnt))<0)
  1608. return(exit_status);
  1609. }
  1610. /* <-- 1998 */
  1611. thisbuf -= (z * longbitsize);
  1612. mask <<= thisbuf;
  1613. mask = ~mask;
  1614. (*bufsflag)[z] &= mask;
  1615. return(FINISHED);
  1616. }
  1617. /************************** MARK_BUF ***************************
  1618. *
  1619. * (1) Which int is the appropriate byte in.
  1620. * (2) What is it's index into this int.
  1621. * (3) Move mask aint the int.
  1622. * (4) Set bit.
  1623. * (5) Keep note of max number of bufs in use.
  1624. */
  1625. int mark_buf(int **bufsflag,int *bufsflagcnt,int thisbuf,int longbitsize,dataptr dz)
  1626. {
  1627. int exit_status;
  1628. int mask = 1;
  1629. int z = thisbuf/longbitsize; /* 1 */
  1630. /* 1998 --> */
  1631. if(z >= *bufsflagcnt) {
  1632. if((exit_status = reallocate_bufsflag(z,bufsflag,bufsflagcnt))<0)
  1633. return(exit_status);
  1634. }
  1635. /* <-- 1998 */
  1636. if(thisbuf > dz->bufcnt)
  1637. dz->bufcnt = thisbuf;
  1638. thisbuf -= (z * longbitsize); /* 2 */
  1639. mask <<= thisbuf; /* 3 */
  1640. (*bufsflag)[z] |= mask; /* 4 */
  1641. return(FINISHED);
  1642. }
  1643. /**************************** REALLOCATE_BUFSFLAG ********************************/
  1644. int reallocate_bufsflag(int z,int **bufsflag,int *bufsflagcnt)
  1645. {
  1646. int n;
  1647. if(*bufsflagcnt==0)
  1648. *bufsflag = (int *)malloc((z+1) * sizeof(int));
  1649. else if((*bufsflag = (int *)realloc(*bufsflag,(z+1) * sizeof(int)))==NULL) {
  1650. sprintf(errstr,"INSUFFICIENT MEMORY for buffer flags store.\n");
  1651. return(MEMORY_ERROR);
  1652. }
  1653. for(n=*bufsflagcnt;n<z;n++)
  1654. *bufsflag[n] = 0;
  1655. *bufsflagcnt = z+1;
  1656. return(FINISHED);
  1657. }
  1658. /*************************** NEW_MMIX *******************************/
  1659. int new_mmix(actmptr *act,dataptr dz)
  1660. {
  1661. int exit_status;
  1662. int n;
  1663. int thispos, nextpos, samps_to_mix;
  1664. int position_in_outbuf = 0;
  1665. int active_bufcnt = 0;
  1666. int outbuf_space = dz->buflen; /* position in the mix is measured in output samples. */
  1667. fprintf(stdout,"INFO: Mixing the sounds.\n");
  1668. fflush(stdout);
  1669. display_virtual_time(0L,dz);
  1670. if(sloom) {
  1671. if(dz->iparam[MIX_STRTPOS_IN_ACTION] > 0) {
  1672. fprintf(stdout,"INFO: Skipping initial part of mix.\n");
  1673. fflush(stdout);
  1674. }
  1675. }
  1676. for(n=0;n<dz->iparam[MIX_TOTAL_ACTCNT]-1;n++) { /* Switch bufs ON or OFF as indicated in act[], & get position */
  1677. if((exit_status = adjust_buffer_status(act,n,&thispos,&active_bufcnt,dz))<0)
  1678. return(exit_status); /* and while doing so, count active buffers. */
  1679. nextpos = (act[n+1])->position; /* Get next position, (from which to calc dur of this mix-chunk) */
  1680. if((exit_status = adjust_activebufs_list(active_bufcnt,dz))<0)
  1681. return(exit_status); /* Ensure only pointers in ACTIVE bufs are in buf-pointer list */
  1682. if(dz->iparam[MIX_STRTPOS] > 0) { /* If mix does not start at zero */
  1683. if(nextpos <= dz->iparam[MIX_STRTPOS]){ /* update MIX_STRTPOS_IN_ACTION */
  1684. dz->iparam[MIX_STRTPOS_IN_ACTION] = (int)(dz->iparam[MIX_STRTPOS_IN_ACTION] - (nextpos - thispos));
  1685. continue; /* and skip action until we reach a valid mix-action */
  1686. }
  1687. } /* If we're in a valid mix action */
  1688. /* i.e. there is >zero time between this action and next */
  1689. /* AND time is later than time where mix-action starts */
  1690. if((samps_to_mix = nextpos - thispos - dz->iparam[MIX_STRTPOS_IN_ACTION])>0) {
  1691. if(active_bufcnt==0) { /* If no buffers are active, fill time with silence. */
  1692. if((exit_status = do_silence(samps_to_mix,&position_in_outbuf,&outbuf_space,dz))<0)
  1693. return(exit_status);
  1694. } else { /* Else, do mix */
  1695. if((exit_status = do_mix(samps_to_mix,&position_in_outbuf,active_bufcnt,&outbuf_space,dz))<0)
  1696. return(exit_status);
  1697. /* Having got to start of actual mixing, */
  1698. } /* MIX_STRTPOS_IN_ACTION is set to zero */
  1699. /* and NO LONGER affects calculations. */
  1700. dz->iparam[MIX_STRTPOS_IN_ACTION] = dz->iparam[MIX_STRTPOS] = 0;
  1701. }
  1702. } /* Write any data remaining in output buffer. */
  1703. if(position_in_outbuf > 0)
  1704. return write_samps(dz->sampbuf[OBUFMIX],position_in_outbuf,dz);
  1705. return FINISHED;
  1706. }
  1707. /************************** ADJUST_BUFFER_STATUS **************************
  1708. *
  1709. * And keep track of number of active buffers (active_bufcnt).
  1710. */
  1711. int adjust_buffer_status(actmptr *act,int n,int *thispos,int *active_bufcnt,dataptr dz)
  1712. {
  1713. int exit_status;
  1714. switch(act[n]->role) {
  1715. case(MIX_ACTION_ON): /* buffer is being used for 1st time in this mix-action */
  1716. (*active_bufcnt)++; /* Increment the count of ACTIVE buffers */
  1717. dz->buflist[act[n]->val->bufno]->status = MIX_ACTION_ON; /* Change buffer status to ON */
  1718. /* fall thro */
  1719. case(MIX_ACTION_REFILL): /* buffer is being reused OR used for 1st time */
  1720. if((exit_status = read_samps_to_an_inbuf(act,n,dz))<0) /* Read_samples into buffer */
  1721. return(exit_status);
  1722. dz->buflist[act[n]->val->bufno]->here = dz->buflist[act[n]->val->bufno]->buf;
  1723. break; /* Reset location_in_buffer_pointer (here) to buffer start */
  1724. case(MIX_ACTION_OFF): /* buffer is finished with */
  1725. dz->buflist[act[n]->val->bufno]->status = MIX_ACTION_OFF; /* Change buffer status to OFF */
  1726. (*active_bufcnt)--; /* Decrement the number of ACTIVE buffers */
  1727. break;
  1728. default:
  1729. sprintf(errstr,"Unknown case in adjust_buffer_status()\n");
  1730. //TEST ONLY
  1731. // RWD for 64bit arch, need to use size_t to convert pointer!
  1732. sprintf(errstr,"adjust_buffer_status()\nact[%d] = %d act = %d\n",n,(int)(size_t)act[n],(int)(size_t)act);
  1733. return(PROGRAM_ERROR);
  1734. }
  1735. *thispos = act[n]->position; /* Return position (starttime-in-samps) of the action */
  1736. return(FINISHED);
  1737. }
  1738. /************************* READ_SAMPS_TO_AN_INBUF ***************************/
  1739. int read_samps_to_an_inbuf(actmptr *act,int n,dataptr dz)
  1740. {
  1741. int exit_status;
  1742. int j, m, inpos, outpos, inchan, outchan;
  1743. double level;
  1744. float *inbuf = dz->buflist[act[n]->val->bufno]->buf;
  1745. int *p;
  1746. int samps_to_read = (dz->buflen/dz->out_chans) * act[n]->val->inchans;
  1747. /* dz->buflen = size of output buffer */
  1748. /* STEREOBUF size is calculated to allow for max channel-cnt of infiles, */
  1749. /* even if some input files have MORE chans than output. */
  1750. /* If the input file has LESS chans than output, */
  1751. /* less than 'buflen' gets read, and 'STEREOBUF' gets expanded into inbuf */
  1752. /* if infile has MORE chans than output, */
  1753. /* more than 'buflen' gets read, and 'STEREOBUF' gets contracted into inbuf */
  1754. if((exit_status = mix_read_samps(act,dz->sampbuf[STEREOBUF],samps_to_read,n,dz))<0)
  1755. return(exit_status);
  1756. memset((char *)inbuf,0,dz->buflen * sizeof(float));
  1757. for(j = 0; j < 256; j++) {
  1758. m = j * 2;
  1759. p = act[n]->val->route + m;
  1760. if(*p == 0)
  1761. break;
  1762. inchan = (*p - 1);
  1763. p++;
  1764. outchan = (*p - 1);
  1765. level = *(act[n]->val->level + j);
  1766. /* Unconverted infile is stored in STEREOBUF : we convert it to outfile format .....*/
  1767. /* Starting at infile-channel 'inpos' step through infile by infile-chancnt 'act[n]->val->inchans' */
  1768. /* scaling the value by 'level', and adding it into the outbuf-channel-format 'inbuf' */
  1769. /* Write to outbuf at outfile-channel 'outchan' and stepping by outfile chancnt 'dz->out_chans' */
  1770. for(inpos=inchan,outpos=outchan;inpos<dz->ssampsread;inpos+=act[n]->val->inchans,outpos+=dz->out_chans)
  1771. inbuf[outpos] = (float)(inbuf[outpos] + (dz->sampbuf[STEREOBUF][inpos] * level));
  1772. }
  1773. return(FINISHED);
  1774. }
  1775. /*********************** ADJUST_ACTIVEBUFS_LIST **************************
  1776. *
  1777. * Set the activebuf pointers to point only to the active buffers!!
  1778. * NB active_bufcnt has been reset by adjust_buffer_status()
  1779. */
  1780. int adjust_activebufs_list(int active_bufcnt,dataptr dz)
  1781. {
  1782. int n, k = 0;
  1783. for(n=0;n<dz->bufcnt;n++) {
  1784. if(dz->buflist[n]->status == MIX_ACTION_ON) {
  1785. dz->activebuf[k] = n;
  1786. dz->activebuf_ptr[k++] = dz->buflist[n]->here;
  1787. }
  1788. }
  1789. if(k>active_bufcnt) {
  1790. sprintf(errstr,"Accounting error: adjust_activebufs_list()\n");
  1791. return(PROGRAM_ERROR);
  1792. }
  1793. return(FINISHED);
  1794. }
  1795. /************************* DO_MIX **********************************
  1796. *
  1797. * This algorithm puts all the samples from buffer 1 into output, then
  1798. * sums in all the samples form buffer 2, and so on.
  1799. */
  1800. int do_mix(int samps_to_mix,int *position_in_outbuf,int active_bufcnt,int *outbuf_space,dataptr dz)
  1801. {
  1802. int exit_status;
  1803. int opos = *position_in_outbuf;
  1804. int m, start = opos;
  1805. int n, overflow;
  1806. float *obuf = dz->sampbuf[OBUFMIX];
  1807. float *outptr = obuf+opos;
  1808. if(dz->iparam[MIX_STRTPOS] > 0) { /* If mix doesn't start at mixlist start, */
  1809. for(n=0;n<active_bufcnt;n++) /* and hence most likely not at buffer start, */
  1810. dz->activebuf_ptr[n] += dz->iparam[MIX_STRTPOS_IN_ACTION];
  1811. } /* increment pointers in all active_bufs to actual mix start */
  1812. if((overflow = samps_to_mix - (*outbuf_space)) <= 0) { /* If samps_to_write DOESN'T overflow output buffer */
  1813. memmove((char *)outptr,(char *)(dz->activebuf_ptr[0]),samps_to_mix * sizeof(float));
  1814. dz->activebuf_ptr[0] += samps_to_mix; /* Increment pointer in this active buf */
  1815. dz->buflist[dz->activebuf[0]]->here += samps_to_mix;/* Increment current_position_in_buffer_pointer also */
  1816. opos += samps_to_mix; /* Increment position_in_outbuf */
  1817. for(m=1;m<active_bufcnt;m++) { /* For each of remaining ACTIVE buffers */
  1818. opos = start; /* Reset output-buf pointer to start-of-write point */
  1819. for(n=0;n<samps_to_mix;n++) /* Add in samples from the other buffers */
  1820. obuf[opos++] += *(dz->activebuf_ptr[m]++);
  1821. dz->buflist[dz->activebuf[m]]->here += samps_to_mix;
  1822. /* And update current_position_in_buffer_pointers */
  1823. }
  1824. if((*outbuf_space = dz->buflen - opos)<=0) { /* if output buffer is full */
  1825. if((exit_status = write_samps(obuf,dz->buflen ,dz))<0)
  1826. return(exit_status); /* write a full buffer */
  1827. *outbuf_space = dz->buflen; /* and reset available space and buffer position */
  1828. opos = 0;
  1829. }
  1830. *position_in_outbuf = opos;
  1831. return(FINISHED);
  1832. /* IF samps_to_write DOES overflow output buffer */
  1833. } else { /* which can only happen ONCE, */
  1834. /* as in- & out- bufs are same size */
  1835. if(*outbuf_space>0) {
  1836. memmove((char *)(outptr),(char *)(dz->activebuf_ptr[0]),(*outbuf_space) * sizeof(float));
  1837. dz->activebuf_ptr[0] += *outbuf_space;
  1838. dz->buflist[dz->activebuf[0]]->here += *outbuf_space;
  1839. opos += *outbuf_space;
  1840. for(m=1;m<active_bufcnt;m++) {
  1841. opos = start;
  1842. for(n=0;n<*outbuf_space;n++)
  1843. obuf[opos++] += *(dz->activebuf_ptr[m]++);
  1844. dz->buflist[dz->activebuf[m]]->here += *outbuf_space;
  1845. }
  1846. }
  1847. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1848. return(exit_status);
  1849. if(overflow) {
  1850. start = opos = 0;
  1851. memmove((char *)obuf,(char *)(dz->activebuf_ptr[0]),overflow*sizeof(float));
  1852. dz->activebuf_ptr[0] += overflow;
  1853. dz->buflist[dz->activebuf[0]]->here += overflow;
  1854. opos = overflow;
  1855. for(m=1;m<active_bufcnt;m++) {
  1856. opos = start;
  1857. for(n=0;n<overflow;n++)
  1858. obuf[opos++] += *(dz->activebuf_ptr[m]++);
  1859. dz->buflist[dz->activebuf[m]]->here += overflow;
  1860. }
  1861. }
  1862. }
  1863. *outbuf_space = dz->buflen - opos; /* Reset the space-left-in-outbuf */
  1864. *position_in_outbuf = opos;
  1865. return(FINISHED);
  1866. }
  1867. /************************* DO_SILENCE ***********************************/
  1868. int do_silence(int samps_to_mix,int *position_in_outbuf,int *outbuf_space,dataptr dz)
  1869. {
  1870. int exit_status;
  1871. int opos = *position_in_outbuf;
  1872. int overflow;
  1873. float *obuf = dz->sampbuf[OBUFMIX];
  1874. while((overflow = samps_to_mix - *outbuf_space) > 0) {
  1875. memset((char *)(obuf+opos),0,(*outbuf_space) * sizeof(float));
  1876. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1877. return(exit_status);
  1878. opos = 0;
  1879. *outbuf_space = dz->buflen;
  1880. samps_to_mix = overflow;
  1881. }
  1882. if(samps_to_mix) {
  1883. memset((char *)(obuf+opos),0,samps_to_mix*sizeof(float));
  1884. opos += samps_to_mix;
  1885. *outbuf_space = dz->buflen - opos;
  1886. }
  1887. *position_in_outbuf = opos;
  1888. return(FINISHED);
  1889. }
  1890. /*************************** MIX_READ_SAMPS **************************/
  1891. int mix_read_samps(actmptr *act,float *inbuf,int samps_to_read,int n,dataptr dz)
  1892. {
  1893. int thisfile = act[n]->val->ifd;
  1894. if((dz->ssampsread = fgetfbufEx(inbuf, samps_to_read,thisfile,0)) < 0) {
  1895. sprintf(errstr, "Can't read samps from input soundfile %d\n",n+1);
  1896. return(SYSTEM_ERROR);
  1897. }
  1898. return(FINISHED);
  1899. }
  1900. /*************************** ISNUMERIC **************************/
  1901. int IsNumeric(char *str)
  1902. {
  1903. char *p;
  1904. int got_point = 0;
  1905. p = str;
  1906. while(*p != ENDOFSTR) {
  1907. if(isdigit(*p))
  1908. p++;
  1909. else if(*p == '.') {
  1910. if(got_point)
  1911. return 0;
  1912. got_point = 1;
  1913. p++;
  1914. } else
  1915. return 0;
  1916. }
  1917. if(p == str)
  1918. return 0;
  1919. return 1;
  1920. }