iterfof.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  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. // BUG dz->brksize[ITF_DEL] IS 14 .... should be much bigger ... this is
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <pnames.h>
  28. #include <filetype.h>
  29. #include <processno.h>
  30. #include <modeno.h>
  31. #include <logic.h>
  32. #include <globcon.h>
  33. #include <cdpmain.h>
  34. #include <math.h>
  35. #include <mixxcon.h>
  36. #include <osbind.h>
  37. #include <standalone.h>
  38. #include <science.h>
  39. #include <ctype.h>
  40. #include <sfsys.h>
  41. #include <string.h>
  42. #include <srates.h>
  43. #include <pnames.h>
  44. #include <extdcon.h>
  45. #include <limits.h>
  46. #ifdef unix
  47. #include <aaio.h>
  48. #endif
  49. #ifdef unix
  50. #define round(x) lround((x))
  51. #endif
  52. char errstr[2400];
  53. #define infilespace rampbrksize
  54. #define ampvaried is_rectified
  55. #define overflowsize temp_sampsize
  56. int anal_infiles = 1;
  57. int sloom = 0;
  58. int sloombatch = 0;
  59. #define MINFADE 2 // minimum fade of element in MS
  60. const char* cdp_version = "7.1.0";
  61. //CDP LIB REPLACEMENTS
  62. static int setup_iterfof_application(dataptr dz);
  63. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  64. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  65. static int setup_iterfof_param_ranges_and_defaults(dataptr dz);
  66. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  67. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  68. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  69. static int establish_application(dataptr dz);
  70. static int initialise_vflags(dataptr dz);
  71. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  72. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  73. static int mark_parameter_types(dataptr dz,aplptr ap);
  74. static int assign_file_data_storage(int infilecnt,dataptr dz);
  75. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  76. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  77. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  78. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  79. static int iterfof(dataptr dz);
  80. static int create_the_iterbufs(dataptr dz);
  81. static int do_iterfof_preprocess(dataptr dz);
  82. static int read_stepd_delay_value(double thistime,double *notestarttime,double *noteendtime,double *portint,dataptr dz);
  83. static int transpos_read_element(int *transposcnt,double incr,dataptr dz);
  84. static int dovibrato(int *delays,dataptr dz);
  85. static int setup_lineportion_params(double *linegains,double *linefades, double *upfades, double *linegaps,dataptr dz);
  86. static int read_lineportion_gain(double thistime,double *lasttime,double *linegain,double *linegains,dataptr dz);
  87. static int read_lineportion_envelope(double thistime,double *envlasttime,int *linedur,int *linefade,double *linefadeincr,int *upfade,double *upfadeincr, double *lineenv,
  88. double *linefades,double *upfades,double *linegaps, int minfade, double *pshift, dataptr dz);
  89. static double portion_env(int *linedur,int *linefade,double *lineenv,double linefadeincr,int *upfade,double upfadeincr);
  90. /**************************************** MAIN *********************************************/
  91. int main(int argc,char *argv[])
  92. {
  93. int exit_status;
  94. dataptr dz = NULL;
  95. char **cmdline;
  96. int cmdlinecnt;
  97. int n;
  98. // aplptr ap;
  99. int is_launched = FALSE;
  100. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  101. fprintf(stdout,"%s\n",cdp_version);
  102. fflush(stdout);
  103. return 0;
  104. }
  105. /* CHECK FOR SOUNDLOOM */
  106. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  107. sloom = 0;
  108. sloombatch = 1;
  109. }
  110. if(sflinit("cdp")){
  111. sfperror("cdp: initialisation\n");
  112. return(FAILED);
  113. }
  114. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  115. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  116. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  117. return(FAILED);
  118. }
  119. if(!sloom) {
  120. if(argc == 1) {
  121. usage1();
  122. return(FAILED);
  123. } else if(argc == 2) {
  124. usage2(argv[1]);
  125. return(FAILED);
  126. }
  127. }
  128. if(!sloom) {
  129. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  130. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  131. return(FAILED);
  132. }
  133. cmdline = argv;
  134. cmdlinecnt = argc;
  135. if((get_the_process_no(argv[0],dz))<0)
  136. return(FAILED);
  137. cmdline++;
  138. cmdlinecnt--;
  139. dz->maxmode = 4;
  140. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  141. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  142. return(exit_status);
  143. }
  144. cmdline++;
  145. cmdlinecnt--;
  146. // setup_particular_application =
  147. if((exit_status = setup_iterfof_application(dz))<0) {
  148. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  149. return(FAILED);
  150. }
  151. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  152. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  153. return(FAILED);
  154. }
  155. } else {
  156. //parse_TK_data() =
  157. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  158. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  159. return(exit_status);
  160. }
  161. }
  162. // ap = dz->application;
  163. // parse_infile_and_hone_type() =
  164. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  165. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  166. return(FAILED);
  167. }
  168. // setup_param_ranges_and_defaults() =
  169. if((exit_status = setup_iterfof_param_ranges_and_defaults(dz))<0) {
  170. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  171. return(FAILED);
  172. }
  173. // open_first_infile CDP LIB
  174. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  175. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  176. return(FAILED);
  177. }
  178. cmdlinecnt--;
  179. cmdline++;
  180. // handle_extra_infiles() : redundant
  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. // handle_formants() redundant
  187. // handle_formant_quiksearch() redundant
  188. // handle_formant_quiksearch() redundant
  189. // handle_the_special_data() redundant
  190. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  191. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  192. return(FAILED);
  193. }
  194. // check_param_validity_and_consistency() redundant
  195. is_launched = TRUE;
  196. dz->bufcnt = 4;
  197. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  198. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  199. return(MEMORY_ERROR);
  200. }
  201. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt+1))==NULL) {
  202. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  203. return(MEMORY_ERROR);
  204. }
  205. for(n = 0;n <dz->bufcnt; n++)
  206. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  207. dz->sampbuf[n] = (float *)0;
  208. if((exit_status = do_iterfof_preprocess(dz))<0) {
  209. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  210. return(FAILED);
  211. }
  212. //spec_process_file =
  213. if((exit_status = iterfof(dz))<0) {
  214. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  215. return(FAILED);
  216. }
  217. if((exit_status = complete_output(dz))<0) { // CDP LIB
  218. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  219. return(FAILED);
  220. }
  221. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  222. free(dz);
  223. return(SUCCEEDED);
  224. }
  225. /**********************************************
  226. REPLACED CDP LIB FUNCTIONS
  227. **********************************************/
  228. /****************************** SET_PARAM_DATA *********************************/
  229. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  230. {
  231. ap->special_data = (char)special_data;
  232. ap->param_cnt = (char)paramcnt;
  233. ap->max_param_cnt = (char)maxparamcnt;
  234. if(ap->max_param_cnt>0) {
  235. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  236. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  237. return(MEMORY_ERROR);
  238. }
  239. strcpy(ap->param_list,paramlist);
  240. }
  241. return(FINISHED);
  242. }
  243. /****************************** SET_VFLGS *********************************/
  244. int set_vflgs
  245. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  246. {
  247. ap->option_cnt = (char) optcnt; /*RWD added cast */
  248. if(optcnt) {
  249. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  250. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  251. return(MEMORY_ERROR);
  252. }
  253. strcpy(ap->option_list,optlist);
  254. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  255. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  256. return(MEMORY_ERROR);
  257. }
  258. strcpy(ap->option_flags,optflags);
  259. }
  260. ap->vflag_cnt = (char) vflagcnt;
  261. ap->variant_param_cnt = (char) vparamcnt;
  262. if(vflagcnt) {
  263. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  264. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  265. return(MEMORY_ERROR);
  266. }
  267. strcpy(ap->variant_list,varlist);
  268. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  269. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  270. return(MEMORY_ERROR);
  271. }
  272. strcpy(ap->variant_flags,varflags);
  273. }
  274. return(FINISHED);
  275. }
  276. /***************************** APPLICATION_INIT **************************/
  277. int application_init(dataptr dz)
  278. {
  279. int exit_status;
  280. int storage_cnt;
  281. int tipc, brkcnt;
  282. aplptr ap = dz->application;
  283. if(ap->vflag_cnt>0)
  284. initialise_vflags(dz);
  285. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  286. ap->total_input_param_cnt = (char)tipc;
  287. if(tipc>0) {
  288. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  289. return(exit_status);
  290. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  291. return(exit_status);
  292. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  293. return(exit_status);
  294. }
  295. brkcnt = tipc;
  296. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  297. if(brkcnt>0) {
  298. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  299. return(exit_status);
  300. }
  301. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  302. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  303. return(exit_status);
  304. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  305. return(exit_status);
  306. }
  307. if((exit_status = mark_parameter_types(dz,ap))<0)
  308. return(exit_status);
  309. // establish_infile_constants() replaced by
  310. dz->infilecnt = 1;
  311. //establish_bufptrs_and_extra_buffers():
  312. return(FINISHED);
  313. }
  314. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  315. /* RWD mallo changed to calloc; helps debug verison run as release! */
  316. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  317. {
  318. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  319. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  320. return(MEMORY_ERROR);
  321. }
  322. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  323. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  324. return(MEMORY_ERROR);
  325. }
  326. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  327. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  328. return(MEMORY_ERROR);
  329. }
  330. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  331. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  332. return(MEMORY_ERROR);
  333. }
  334. return(FINISHED);
  335. }
  336. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  337. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  338. {
  339. int n;
  340. for(n=0;n<storage_cnt;n++) {
  341. dz->is_int[n] = (char)0;
  342. dz->no_brk[n] = (char)0;
  343. }
  344. return(FINISHED);
  345. }
  346. /***************************** MARK_PARAMETER_TYPES **************************/
  347. int mark_parameter_types(dataptr dz,aplptr ap)
  348. {
  349. int n, m; /* PARAMS */
  350. for(n=0;n<ap->max_param_cnt;n++) {
  351. switch(ap->param_list[n]) {
  352. case('0'): break; /* dz->is_active[n] = 0 is default */
  353. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  354. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  355. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  356. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  357. default:
  358. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  359. return(PROGRAM_ERROR);
  360. }
  361. } /* OPTIONS */
  362. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  363. switch(ap->option_list[n]) {
  364. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  365. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  366. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  367. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  368. default:
  369. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  370. return(PROGRAM_ERROR);
  371. }
  372. } /* VARIANTS */
  373. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  374. switch(ap->variant_list[n]) {
  375. case('0'): break;
  376. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  377. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  378. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  379. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  380. default:
  381. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  382. return(PROGRAM_ERROR);
  383. }
  384. } /* INTERNAL */
  385. for(n=0,
  386. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  387. switch(ap->internal_param_list[n]) {
  388. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  389. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  390. case('d'): dz->no_brk[m] = (char)1; break;
  391. default:
  392. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  393. return(PROGRAM_ERROR);
  394. }
  395. }
  396. return(FINISHED);
  397. }
  398. /************************ HANDLE_THE_OUTFILE *********************/
  399. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  400. {
  401. int exit_status;
  402. char *filename = (*cmdline)[0];
  403. if(filename[0]=='-' && filename[1]=='f') {
  404. dz->floatsam_output = 1;
  405. dz->true_outfile_stype = SAMP_FLOAT;
  406. filename+= 2;
  407. }
  408. if(!sloom) {
  409. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  410. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  411. return(DATA_ERROR);
  412. }
  413. }
  414. strcpy(dz->outfilename,filename);
  415. if((exit_status = create_sized_outfile(filename,dz))<0)
  416. return(exit_status);
  417. (*cmdline)++;
  418. (*cmdlinecnt)--;
  419. return(FINISHED);
  420. }
  421. /***************************** ESTABLISH_APPLICATION **************************/
  422. int establish_application(dataptr dz)
  423. {
  424. aplptr ap;
  425. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  426. sprintf(errstr,"establish_application()\n");
  427. return(MEMORY_ERROR);
  428. }
  429. ap = dz->application;
  430. memset((char *)ap,0,sizeof(struct applic));
  431. return(FINISHED);
  432. }
  433. /************************* INITIALISE_VFLAGS *************************/
  434. int initialise_vflags(dataptr dz)
  435. {
  436. int n;
  437. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  438. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  439. return(MEMORY_ERROR);
  440. }
  441. for(n=0;n<dz->application->vflag_cnt;n++)
  442. dz->vflag[n] = FALSE;
  443. return FINISHED;
  444. }
  445. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  446. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  447. {
  448. int n;
  449. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  450. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  451. return(MEMORY_ERROR);
  452. }
  453. for(n=0;n<tipc;n++)
  454. ap->default_val[n] = 0.0;
  455. return(FINISHED);
  456. }
  457. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  458. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  459. {
  460. int n;
  461. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  462. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  463. return(MEMORY_ERROR);
  464. }
  465. for(n=0;n<tipc;n++)
  466. dz->is_active[n] = (char)0;
  467. return(FINISHED);
  468. }
  469. /************************* SETUP_ITERLINE_APPLICATION *******************/
  470. int setup_iterfof_application(dataptr dz)
  471. {
  472. int exit_status;
  473. aplptr ap;
  474. if((exit_status = establish_application(dz))<0) // GLOBAL
  475. return(FAILED);
  476. ap = dz->application;
  477. if((exit_status = set_param_data(ap,0,2,2,"Dd"))<0)
  478. return(FAILED);
  479. if(EVEN(dz->mode))
  480. exit_status = set_vflgs(ap,"patTErvVdD",10,"DDDDdDDDDD","s",1,1,"i");
  481. else
  482. exit_status = set_vflgs(ap,"patTErvVdDgGFfSPi",17,"DDDDdDDDDDDDDDDiD","s",1,1,"i");
  483. if(exit_status < 0)
  484. return FAILED;
  485. // set_legal_infile_structure -->
  486. dz->has_otherfile = FALSE;
  487. // assign_process_logic -->
  488. dz->input_data_type = SNDFILES_ONLY;
  489. dz->process_type = UNEQUAL_SNDFILE;
  490. dz->outfiletype = SNDFILE_OUT;
  491. dz->maxmode = 4;
  492. return application_init(dz); //GLOBAL
  493. }
  494. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  495. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  496. {
  497. int exit_status;
  498. infileptr infile_info;
  499. if(!sloom) {
  500. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  501. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  502. return(MEMORY_ERROR);
  503. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  504. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  505. return(PROGRAM_ERROR);
  506. } else if(infile_info->filetype != SNDFILE) {
  507. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  508. return(DATA_ERROR);
  509. } else if(infile_info->channels != MONO) {
  510. sprintf(errstr,"File %s is not of correct type: must be MONO\n",cmdline[0]);
  511. return(DATA_ERROR);
  512. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  513. sprintf(errstr,"Failed to copy file parsing information\n");
  514. return(PROGRAM_ERROR);
  515. }
  516. free(infile_info);
  517. }
  518. return(FINISHED);
  519. }
  520. /************************* SETUP_ITERLINE_PARAM_RANGES_AND_DEFAULTS *******************/
  521. int setup_iterfof_param_ranges_and_defaults(dataptr dz)
  522. {
  523. int exit_status;
  524. aplptr ap = dz->application;
  525. // set_param_ranges()
  526. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  527. // NB total_input_param_cnt is > 0 !!!
  528. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  529. return(FAILED);
  530. // get_param_ranges()
  531. if(dz->mode <2) {
  532. ap->lo[ITF_DEL] = -24;
  533. ap->hi[ITF_DEL] = 12;
  534. ap->default_val[ITF_DEL] = 0;
  535. } else {
  536. ap->lo[ITF_DEL] = 24;
  537. ap->hi[ITF_DEL] = 96;
  538. ap->default_val[ITF_DEL] = 60;
  539. }
  540. ap->lo[ITF_DUR] = dz->duration;
  541. ap->hi[ITF_DUR] = BIG_TIME;
  542. ap->default_val[ITF_DUR] = 20;
  543. ap->lo[ITF_RAND] = 0.0;
  544. ap->hi[ITF_RAND] = 1.0;
  545. ap->default_val[ITF_RAND] = 0.0;
  546. ap->lo[ITF_VMIN] = 0.0;
  547. ap->hi[ITF_VMIN] = 20.0;
  548. ap->default_val[ITF_VMIN] = 0.0;
  549. ap->lo[ITF_VMAX] = 0.0;
  550. ap->hi[ITF_VMAX] = 20.0;
  551. ap->default_val[ITF_VMAX] = 0.0;
  552. ap->lo[ITF_DMIN] = 0.0;
  553. ap->hi[ITF_DMIN] = 2.0;
  554. ap->default_val[ITF_DMIN] = 0.0;
  555. ap->lo[ITF_DMAX] = 0.0;
  556. ap->hi[ITF_DMAX] = 2.0;
  557. ap->default_val[ITF_DMAX] = 0.0;
  558. ap->lo[ITF_PRND] = 0.0;
  559. ap->hi[ITF_PRND] = 2.0;
  560. ap->default_val[ITF_PRND] = 0.0;
  561. ap->lo[ITF_AMPC] = 0.0;
  562. ap->hi[ITF_AMPC] = 1.0;
  563. ap->default_val[ITF_AMPC] = 0.0;
  564. ap->lo[ITF_TRIM] = 0.0;
  565. ap->hi[ITF_TRIM] = dz->duration;
  566. ap->default_val[ITF_TRIM] = 0.0;
  567. ap->lo[ITF_TRBY] = 0.0;
  568. ap->hi[ITF_TRBY] = dz->duration;
  569. ap->default_val[ITF_TRBY] = 0.0;
  570. ap->lo[ITF_SLOP] = 1.0;
  571. ap->hi[ITF_SLOP] = 4.0;
  572. ap->default_val[ITF_SLOP] = 1.0;
  573. if(EVEN(dz->mode)) {
  574. ap->lo[ITF_SEED1] = 0.0;
  575. ap->hi[ITF_SEED1] = MAXSHORT;
  576. ap->default_val[ITF_SEED1] = 0.0;
  577. } else {
  578. ap->lo[ITF_GMIN] = 0.0;
  579. ap->hi[ITF_GMIN] = 1.0;
  580. ap->default_val[ITF_GMIN] = 1.0;
  581. ap->lo[ITF_GMAX] = 0.0;
  582. ap->hi[ITF_GMAX] = 1.0;
  583. ap->default_val[ITF_GMAX] = 1.0;
  584. ap->lo[ITF_UFAD] = 0.0;
  585. ap->hi[ITF_UFAD] = 10;
  586. ap->default_val[ITF_UFAD] = 0.0;
  587. ap->lo[ITF_FADE] = 0.0;
  588. ap->hi[ITF_FADE] = 10;
  589. ap->default_val[ITF_FADE] = 0.0;
  590. ap->lo[ITF_GAPP] = 0.0;
  591. ap->hi[ITF_GAPP] = 1.0;
  592. ap->default_val[ITF_GAPP] = 0.0;
  593. ap->lo[ITF_PORT] = -1;
  594. ap->hi[ITF_PORT] = 2.0;
  595. ap->default_val[ITF_PORT] = 0.0;
  596. ap->lo[ITF_PINT] = 0.0;
  597. ap->hi[ITF_PINT] = 2.0;
  598. ap->default_val[ITF_PINT] = 0.0;
  599. ap->lo[ITF_SEED2] = 0.0;
  600. ap->hi[ITF_SEED2] = MAXSHORT;
  601. ap->default_val[ITF_SEED2] = 0.0;
  602. }
  603. dz->maxmode = 4;
  604. if(!sloom)
  605. put_default_vals_in_all_params(dz);
  606. return(FINISHED);
  607. }
  608. /********************************* PARSE_SLOOM_DATA *********************************/
  609. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  610. {
  611. int exit_status;
  612. int cnt = 1, infilecnt;
  613. int filesize, insams, inbrksize;
  614. double dummy;
  615. int true_cnt = 0;
  616. // aplptr ap;
  617. while(cnt<=PRE_CMDLINE_DATACNT) {
  618. if(cnt > argc) {
  619. sprintf(errstr,"Insufficient data sent from TK\n");
  620. return(DATA_ERROR);
  621. }
  622. switch(cnt) {
  623. case(1):
  624. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  625. sprintf(errstr,"Cannot read process no. sent from TK\n");
  626. return(DATA_ERROR);
  627. }
  628. break;
  629. case(2):
  630. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  631. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  632. return(DATA_ERROR);
  633. }
  634. if(dz->mode > 0)
  635. dz->mode--;
  636. //setup_particular_application() =
  637. if((exit_status = setup_iterfof_application(dz))<0)
  638. return(exit_status);
  639. // ap = dz->application;
  640. break;
  641. case(3):
  642. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  643. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  644. return(DATA_ERROR);
  645. }
  646. if(infilecnt < 1) {
  647. true_cnt = cnt + 1;
  648. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  649. }
  650. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  651. return(exit_status);
  652. break;
  653. case(INPUT_FILETYPE+4):
  654. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  655. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  656. return(DATA_ERROR);
  657. }
  658. break;
  659. case(INPUT_FILESIZE+4):
  660. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  661. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  662. return(DATA_ERROR);
  663. }
  664. dz->insams[0] = filesize;
  665. break;
  666. case(INPUT_INSAMS+4):
  667. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  668. sprintf(errstr,"Cannot read insams sent from TK\n");
  669. return(DATA_ERROR);
  670. }
  671. dz->insams[0] = insams;
  672. break;
  673. case(INPUT_SRATE+4):
  674. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  675. sprintf(errstr,"Cannot read srate sent from TK\n");
  676. return(DATA_ERROR);
  677. }
  678. break;
  679. case(INPUT_CHANNELS+4):
  680. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  681. sprintf(errstr,"Cannot read channels sent from TK\n");
  682. return(DATA_ERROR);
  683. }
  684. break;
  685. case(INPUT_STYPE+4):
  686. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  687. sprintf(errstr,"Cannot read stype sent from TK\n");
  688. return(DATA_ERROR);
  689. }
  690. break;
  691. case(INPUT_ORIGSTYPE+4):
  692. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  693. sprintf(errstr,"Cannot read origstype sent from TK\n");
  694. return(DATA_ERROR);
  695. }
  696. break;
  697. case(INPUT_ORIGRATE+4):
  698. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  699. sprintf(errstr,"Cannot read origrate sent from TK\n");
  700. return(DATA_ERROR);
  701. }
  702. break;
  703. case(INPUT_MLEN+4):
  704. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  705. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  706. return(DATA_ERROR);
  707. }
  708. break;
  709. case(INPUT_DFAC+4):
  710. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  711. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  712. return(DATA_ERROR);
  713. }
  714. break;
  715. case(INPUT_ORIGCHANS+4):
  716. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  717. sprintf(errstr,"Cannot read origchans sent from TK\n");
  718. return(DATA_ERROR);
  719. }
  720. break;
  721. case(INPUT_SPECENVCNT+4):
  722. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  723. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  724. return(DATA_ERROR);
  725. }
  726. dz->specenvcnt = dz->infile->specenvcnt;
  727. break;
  728. case(INPUT_WANTED+4):
  729. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  730. sprintf(errstr,"Cannot read wanted sent from TK\n");
  731. return(DATA_ERROR);
  732. }
  733. break;
  734. case(INPUT_WLENGTH+4):
  735. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  736. sprintf(errstr,"Cannot read wlength sent from TK\n");
  737. return(DATA_ERROR);
  738. }
  739. break;
  740. case(INPUT_OUT_CHANS+4):
  741. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  742. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  743. return(DATA_ERROR);
  744. }
  745. break;
  746. /* RWD these chanegs to samps - tk will have to deal with that! */
  747. case(INPUT_DESCRIPTOR_BYTES+4):
  748. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  749. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  750. return(DATA_ERROR);
  751. }
  752. break;
  753. case(INPUT_IS_TRANSPOS+4):
  754. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  755. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  756. return(DATA_ERROR);
  757. }
  758. break;
  759. case(INPUT_COULD_BE_TRANSPOS+4):
  760. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  761. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  762. return(DATA_ERROR);
  763. }
  764. break;
  765. case(INPUT_COULD_BE_PITCH+4):
  766. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  767. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  768. return(DATA_ERROR);
  769. }
  770. break;
  771. case(INPUT_DIFFERENT_SRATES+4):
  772. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  773. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  774. return(DATA_ERROR);
  775. }
  776. break;
  777. case(INPUT_DUPLICATE_SNDS+4):
  778. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  779. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  780. return(DATA_ERROR);
  781. }
  782. break;
  783. case(INPUT_BRKSIZE+4):
  784. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  785. sprintf(errstr,"Cannot read brksize sent from TK\n");
  786. return(DATA_ERROR);
  787. }
  788. if(inbrksize > 0) {
  789. switch(dz->input_data_type) {
  790. case(WORDLIST_ONLY):
  791. break;
  792. case(PITCH_AND_PITCH):
  793. case(PITCH_AND_TRANSPOS):
  794. case(TRANSPOS_AND_TRANSPOS):
  795. dz->tempsize = inbrksize;
  796. break;
  797. case(BRKFILES_ONLY):
  798. case(UNRANGED_BRKFILE_ONLY):
  799. case(DB_BRKFILES_ONLY):
  800. case(ALL_FILES):
  801. case(ANY_NUMBER_OF_ANY_FILES):
  802. if(dz->extrabrkno < 0) {
  803. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  804. return(DATA_ERROR);
  805. }
  806. if(dz->brksize == NULL) {
  807. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  808. return(PROGRAM_ERROR);
  809. }
  810. dz->brksize[dz->extrabrkno] = inbrksize;
  811. break;
  812. default:
  813. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  814. dz->input_data_type);
  815. return(PROGRAM_ERROR);
  816. }
  817. break;
  818. }
  819. break;
  820. case(INPUT_NUMSIZE+4):
  821. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  822. sprintf(errstr,"Cannot read numsize sent from TK\n");
  823. return(DATA_ERROR);
  824. }
  825. break;
  826. case(INPUT_LINECNT+4):
  827. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  828. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  829. return(DATA_ERROR);
  830. }
  831. break;
  832. case(INPUT_ALL_WORDS+4):
  833. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  834. sprintf(errstr,"Cannot read all_words sent from TK\n");
  835. return(DATA_ERROR);
  836. }
  837. break;
  838. case(INPUT_ARATE+4):
  839. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  840. sprintf(errstr,"Cannot read arate sent from TK\n");
  841. return(DATA_ERROR);
  842. }
  843. break;
  844. case(INPUT_FRAMETIME+4):
  845. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  846. sprintf(errstr,"Cannot read frametime sent from TK\n");
  847. return(DATA_ERROR);
  848. }
  849. dz->frametime = (float)dummy;
  850. break;
  851. case(INPUT_WINDOW_SIZE+4):
  852. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  853. sprintf(errstr,"Cannot read window_size sent from TK\n");
  854. return(DATA_ERROR);
  855. }
  856. break;
  857. case(INPUT_NYQUIST+4):
  858. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  859. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  860. return(DATA_ERROR);
  861. }
  862. break;
  863. case(INPUT_DURATION+4):
  864. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  865. sprintf(errstr,"Cannot read duration sent from TK\n");
  866. return(DATA_ERROR);
  867. }
  868. break;
  869. case(INPUT_MINBRK+4):
  870. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  871. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  872. return(DATA_ERROR);
  873. }
  874. break;
  875. case(INPUT_MAXBRK+4):
  876. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  877. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  878. return(DATA_ERROR);
  879. }
  880. break;
  881. case(INPUT_MINNUM+4):
  882. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  883. sprintf(errstr,"Cannot read minnum sent from TK\n");
  884. return(DATA_ERROR);
  885. }
  886. break;
  887. case(INPUT_MAXNUM+4):
  888. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  889. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  890. return(DATA_ERROR);
  891. }
  892. break;
  893. default:
  894. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  895. return(PROGRAM_ERROR);
  896. }
  897. cnt++;
  898. }
  899. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  900. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  901. return(DATA_ERROR);
  902. }
  903. if(true_cnt)
  904. cnt = true_cnt;
  905. *cmdlinecnt = 0;
  906. while(cnt < argc) {
  907. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  908. return(exit_status);
  909. cnt++;
  910. }
  911. return(FINISHED);
  912. }
  913. /********************************* GET_TK_CMDLINE_WORD *********************************/
  914. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  915. {
  916. if(*cmdlinecnt==0) {
  917. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  918. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  919. return(MEMORY_ERROR);
  920. }
  921. } else {
  922. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  923. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  924. return(MEMORY_ERROR);
  925. }
  926. }
  927. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  928. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  929. return(MEMORY_ERROR);
  930. }
  931. strcpy((*cmdline)[*cmdlinecnt],q);
  932. (*cmdlinecnt)++;
  933. return(FINISHED);
  934. }
  935. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  936. int assign_file_data_storage(int infilecnt,dataptr dz)
  937. {
  938. int exit_status;
  939. int no_sndfile_system_files = FALSE;
  940. dz->infilecnt = infilecnt;
  941. if((exit_status = allocate_filespace(dz))<0)
  942. return(exit_status);
  943. if(no_sndfile_system_files)
  944. dz->infilecnt = 0;
  945. return(FINISHED);
  946. }
  947. /************************* redundant functions: to ensure libs compile OK *******************/
  948. int assign_process_logic(dataptr dz)
  949. {
  950. return(FINISHED);
  951. }
  952. void set_legal_infile_structure(dataptr dz)
  953. {}
  954. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  955. {
  956. int exit_status;
  957. exit_status = set_internalparam_data("diiii", ap);
  958. return(exit_status);
  959. }
  960. int setup_internal_arrays_and_array_pointers(dataptr dz)
  961. {
  962. return(FINISHED);
  963. }
  964. int establish_bufptrs_and_extra_buffers(dataptr dz)
  965. {
  966. return(FINISHED);
  967. }
  968. int read_special_data(char *str,dataptr dz)
  969. {
  970. return(FINISHED);
  971. }
  972. int inner_loop
  973. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  974. {
  975. return(FINISHED);
  976. }
  977. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  978. {
  979. return(FINISHED);
  980. }
  981. /******************************** USAGE1 ********************************/
  982. int usage1(void)
  983. {
  984. usage2("iterfof");
  985. return(USAGE_ONLY);
  986. }
  987. /********************************************************************************************/
  988. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  989. {
  990. if(!strcmp(prog_identifier_from_cmdline,"iterfof")) dz->process = ITERFOF;
  991. else {
  992. fprintf(stderr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  993. return(USAGE_ONLY);
  994. }
  995. return(FINISHED);
  996. }
  997. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  998. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  999. {
  1000. int n;
  1001. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1002. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  1003. return(MEMORY_ERROR);
  1004. }
  1005. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1006. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  1007. return(MEMORY_ERROR);
  1008. }
  1009. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1010. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  1011. return(MEMORY_ERROR);
  1012. }
  1013. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1014. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  1015. return(MEMORY_ERROR);
  1016. }
  1017. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1018. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  1019. return(MEMORY_ERROR);
  1020. }
  1021. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1022. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  1023. return(MEMORY_ERROR);
  1024. }
  1025. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1026. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  1027. return(MEMORY_ERROR);
  1028. }
  1029. for(n=0;n<brkcnt;n++) {
  1030. dz->brk[n] = NULL;
  1031. dz->brkptr[n] = NULL;
  1032. dz->brkinit[n] = 0;
  1033. dz->brksize[n] = 0;
  1034. }
  1035. return(FINISHED);
  1036. }
  1037. /******************************** USAGE2 ********************************/
  1038. int usage2(char *str)
  1039. {
  1040. if(!strcmp(str,"iterfof")) {
  1041. fprintf(stderr,
  1042. "USAGE:\n"
  1043. "iterfof iterfof 1-4 infile outfile linedata outduration\n"
  1044. " [-pprand] [-aampcut] [-ttrimto] [-Ttrimby] [-Etrimslope]\n"
  1045. " [-rrand] [-vvibmin] [-Vvibmax] [-depmin] [-Ddepmax]\n"
  1046. " [-ggainmin] [-Ggainmax] [-Fupfade] [-ffade] [-Sseparation]\n"
  1047. " [-Pportamento] [-iinterval]\n"
  1048. " [-sseed]\n"
  1049. "\n"
  1050. "ITERATE AN INPUT FOF OR SOUND-PACKET, FOLLOWING A DEFINED PITCHLINE.\n"
  1051. "\n"
  1052. "MODES 1 & 2: linedata is transposition in semitones.\n"
  1053. "(Input sound assumed to represent 1 wavelength, at zero transposition.)\n"
  1054. "MODES 3 & 4: linedata is MIDI pitch.\n"
  1055. "(Input sound can be anything.)\n"
  1056. "\n"
  1057. "LINEDATA Modes 1 & 2: semitone-transposition (can vary through time).\n"
  1058. " (varies delay between segments in output)\n"
  1059. " Modes 3 & 4: MIDI-pitch (can vary through time).\n"
  1060. "IF \"linedata\" is a breakpoint file\n"
  1061. "MODES 1 & 3: interpolate between timed values in the file.\n"
  1062. "MODES 2 & 4: step between timed values in the file.\n"
  1063. "\n"
  1064. "SEED the same seed-number will produce similar output on rerun.\n"
  1065. "\n"
  1066. "(FOR MORE INFORMATION ----- hit any key)\n"
  1067. "\n");
  1068. }
  1069. while(!kbhit())
  1070. ;
  1071. if(kbhit()) {
  1072. fprintf(stderr,
  1073. "Parameters of the line-elements\n"
  1074. "\n"
  1075. "PRAND randomises pitch of segments (semitones in range +- 2)\n"
  1076. "AMPCUT max of random amp-reduction on each iter: Range 0-1:\n"
  1077. " default 0 (no amp-reduction)\n"
  1078. "TRIMTO Shorten elements to specified duration\n"
  1079. "TRIMBY Fade elements over specified duration\n"
  1080. "TRIMSLOPE Slope of any fades\n"
  1081. "\n"
  1082. "Parameters of the line\n"
  1083. "\n"
  1084. "RAND transposition/pitch randomisation: Range 0 - 1: Default 0\n"
  1085. " (randomises delay between segments in output)\n"
  1086. "VIBMIN/MAX Min & max vib frequency (frq set rand between these).\n"
  1087. "DEPMIN/MAX Min & max vib depth (semitones) (depth set rand between these).\n"
  1088. "(Modes 2 and 4 only)\n"
  1089. "GAINMIN/MAX min and max level of stable-pitched portions of line.\n"
  1090. "UPFADE duration of fade into stable-pitched portions.\n"
  1091. "FADE duration of fade out of stable-pitched portions.\n"
  1092. "SEPARATION end-fraction of any stable-pitched portion which is silent.\n"
  1093. "PORTAMENTO type of line-portamento (if any)\n"
  1094. " 0 = none : 1 = up : -1 = down : 2 = randomly up/down\n"
  1095. "INTERVAL Portamento interval (semitones : range 0 - 2)\n"
  1096. " Interval attained only by start of next note.\n"
  1097. " (If note fades before next note enters, interval not reached).\n");
  1098. } else
  1099. fprintf(stderr,"Unknown option '%s'\n",str);
  1100. return(USAGE_ONLY);
  1101. }
  1102. int usage3(char *str1,char *str2)
  1103. {
  1104. fprintf(stderr,"Insufficient parameters on command line.\n");
  1105. return(USAGE_ONLY);
  1106. }
  1107. /****************************** GET_MODE *********************************/
  1108. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1109. {
  1110. char temp[200], *p;
  1111. if(sscanf(str,"%s",temp)!=1) {
  1112. sprintf(errstr,"Cannot read mode of program.\n");
  1113. return(USAGE_ONLY);
  1114. }
  1115. p = temp + strlen(temp) - 1;
  1116. while(p >= temp) {
  1117. if(!isdigit(*p)) {
  1118. fprintf(stderr,"Invalid mode of program entered.\n");
  1119. return(USAGE_ONLY);
  1120. }
  1121. p--;
  1122. }
  1123. if(sscanf(str,"%d",&dz->mode)!=1) {
  1124. fprintf(stderr,"Cannot read mode of program.\n");
  1125. return(USAGE_ONLY);
  1126. }
  1127. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1128. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1129. return(USAGE_ONLY);
  1130. }
  1131. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1132. return(FINISHED);
  1133. }
  1134. /**************************** DO_ITERATE_PREPROCESS ******************************/
  1135. int do_iterfof_preprocess(dataptr dz)
  1136. {
  1137. int n, m;
  1138. double wavelen, val, srate;
  1139. if(dz->brksize[ITF_DEL] == 1) {
  1140. dz->param[ITF_DEL] = dz->brk[ITF_DEL][1];
  1141. dz->brksize[ITF_DEL] = 0; // Shrink 1-line brkfile to single value parameter
  1142. }
  1143. if(dz->mode < 2) { // Data is transpositions
  1144. wavelen = (double)dz->insams[0];
  1145. if(dz->brksize[ITF_DEL]) {
  1146. for(n=0,m=1;n<dz->brksize[ITF_DEL];n++,m+=2) {
  1147. val = pow(2.0,dz->brk[ITF_DEL][m]/SEMITONES_PER_OCTAVE); // Semitones to frq ratio
  1148. dz->brk[ITF_DEL][m] = round(wavelen/val); // Delay = wavelen/frqratio
  1149. }
  1150. } else {
  1151. val = pow(2.0,dz->param[ITF_DEL]/SEMITONES_PER_OCTAVE); // Semitones to frq ratio
  1152. dz->param[ITF_DEL] = round(wavelen/val); // Delay = wavelen/frqratio
  1153. }
  1154. } else {
  1155. srate = (double)dz->infile->srate;
  1156. if(dz->brksize[ITF_DEL]) {
  1157. for(n=0,m=1;n<dz->brksize[ITF_DEL];n++,m+=2) {
  1158. val = miditohz(dz->brk[ITF_DEL][m]); // MIDI to frq
  1159. dz->brk[ITF_DEL][m] = round(srate/val); // Delay = srate/frq
  1160. }
  1161. } else {
  1162. val = miditohz(dz->param[ITF_DEL]); // MIDI to frq
  1163. dz->param[ITF_DEL] = round(srate/val); // Delay = srate/frq
  1164. }
  1165. }
  1166. if(dz->brksize[ITF_AMPC] == 0 && dz->param[ITF_AMPC] == 0.0)
  1167. dz->ampvaried = 0;
  1168. else
  1169. dz->ampvaried = 1;
  1170. if(ODD(dz->mode)) {
  1171. if(dz->brksize[ITF_DEL] == 0) { // If only a single transpos/pitch specified
  1172. if(dz->brksize[ITF_GMIN] || dz->brksize[ITF_GMAX] || dz->brksize[ITF_FADE] || dz->brksize[ITF_GAPP]) {
  1173. sprintf(errstr,"Line portion parameters set as time-varying, but only one note is being generated.\n");
  1174. return DATA_ERROR;
  1175. }
  1176. }
  1177. }
  1178. return create_the_iterbufs(dz);
  1179. }
  1180. /*************************** CREATE_THE_ITERBUFS **************************
  1181. *
  1182. * (1) Create extra spaces for interpolation guard points at end of infile.
  1183. *
  1184. * (2) Allow for transposition of source
  1185. *
  1186. * (3) Output buffer must be at least as big as the overflow buffer.
  1187. * Output buffer must be big enough for the whole of any possible
  1188. * data overflow (in overflow_size buff) to be copied back into it.
  1189. * This is because the overflow buffer is ZEROED after such a copy
  1190. * and if a 2nd copy of the overflow back into the main buffer
  1191. * were necessary , we would be copying zeroes rather than true data.
  1192. *
  1193. *
  1194. * true buffer (insams[0]) (insams[0]) + SAFETY
  1195. * |-----------------------------|------------------------------------------|
  1196. * ^ worst ^
  1197. * ^ possible case ^
  1198. * |<-------- BUFFER_SIZE------->
  1199. *
  1200. */
  1201. int create_the_iterbufs(dataptr dz)
  1202. {
  1203. int seccnt;
  1204. int extra_space, infile_space, big_buffer_size;
  1205. int overflow_size;
  1206. infile_space = dz->insams[0] + 1; /* Allows for wraparound point for tansposition interps */
  1207. overflow_size = dz->insams[0];
  1208. overflow_size *= 2; /* Allows for transposition of source up to 1 octave lower */
  1209. if((seccnt = overflow_size/F_SECSIZE) * F_SECSIZE < overflow_size)
  1210. seccnt++;
  1211. overflow_size = F_SECSIZE * overflow_size;
  1212. if((seccnt = infile_space/F_SECSIZE) * F_SECSIZE < infile_space)
  1213. seccnt++;
  1214. infile_space = F_SECSIZE * seccnt;
  1215. extra_space = infile_space + (overflow_size * 2); /* overflow_size for overflow, and also for transposition buffer */
  1216. dz->buflen = infile_space;
  1217. big_buffer_size = dz->buflen + extra_space;
  1218. if((dz->bigbuf = (float *)Malloc(big_buffer_size * sizeof(float)))==NULL) {
  1219. sprintf(errstr, "INSUFFICIENT MEMORY to create sound buffers.\n");
  1220. return(MEMORY_ERROR);
  1221. }
  1222. dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf; // INPUT BUFFER
  1223. dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + infile_space; // OUTPUT BUFFER
  1224. dz->sbufptr[2] = dz->sampbuf[2] = dz->sampbuf[1] + dz->buflen; // OUTPUT BUFFER OVERFLOW
  1225. dz->sbufptr[3] = dz->sampbuf[3] = dz->sampbuf[2] + overflow_size; // TRANSPOSITION BUFFER
  1226. dz->sbufptr[4] = dz->sampbuf[4] = dz->sampbuf[3] + overflow_size;
  1227. memset((char *)dz->sampbuf[0],0,(size_t)(infile_space * sizeof(float)));
  1228. memset((char *)dz->sampbuf[1],0,(size_t)(dz->buflen * sizeof(float)));
  1229. memset((char *)dz->sampbuf[2],0,(size_t)(overflow_size * sizeof(float)));
  1230. dz->infilespace = infile_space;
  1231. dz->overflowsize = overflow_size;
  1232. return(FINISHED);
  1233. }
  1234. /****************************** ITERFOF *************************/
  1235. #define ACCEPTABLE_MAXLEVEL 0.9
  1236. int iterfof(dataptr dz)
  1237. {
  1238. int exit_status, dovib;
  1239. int n, m, k, obufpos, absobufpos, *delays, ldelay, transposcnt, last_write = 0, this_last_write, local_last_write, minfade;
  1240. int bufs_written, bufs_before;
  1241. double time = 0.0, normaliser = 1.0, gain, delay, srate = (double)dz->infile->srate, *gains, *pshifts;
  1242. double *linegains = NULL, *linefades = NULL, *upfades = NULL, *linegaps = NULL;
  1243. double maxoutsamp = 0.0, pshift;
  1244. int passno;
  1245. float *ibuf = dz->sampbuf[0], *obuf = dz->sampbuf[1], *ovflw = dz->sampbuf[2], *tbuf = dz->sampbuf[3], *getbuf;
  1246. int dozeros, trimdur, trimfade, fade_to, write_to;
  1247. double spliceval, spliceincr, splicer, lasttime, envlasttime, lineoutgain, outtime;
  1248. double linegain = 0.0, linefadeincr = 0.0, lineenv = 1.0, portint = 0.0;
  1249. int linedur = 0, linefade = 0, upfade = 0;
  1250. double upfadeincr = 0.0, notestarttime = 0.0, noteendtime = 0.0;
  1251. double thisdelay, time_in_note, note_dur, timefrac, current_port_int, current_port_trans;
  1252. int itfseed;
  1253. if(EVEN(dz->mode))
  1254. itfseed = dz->iparam[ITF_SEED1];
  1255. else
  1256. itfseed = dz->iparam[ITF_SEED2];
  1257. if(sloom)
  1258. dz->tempsize = (int)round(dz->param[ITF_DUR] * srate) * dz->infile->channels;
  1259. srand((int)itfseed);
  1260. initrand48();
  1261. minfade = (int)round(MINFADE * MS_TO_SECS * srate);
  1262. print_outmessage_flush("Initial pass, checking segment count.\n");
  1263. k = 0;
  1264. notestarttime = -1.0;
  1265. noteendtime = 0.0;
  1266. while(time < dz->param[ITF_DUR]) {
  1267. if(dz->brksize[ITF_DEL]) {
  1268. if(ODD(dz->mode)) {
  1269. if((exit_status = read_stepd_delay_value(time,&notestarttime,&noteendtime,&portint,dz))<0)
  1270. return exit_status;
  1271. thisdelay = dz->param[ITF_DEL];
  1272. if(!flteq(portint,0.0)) { // If portamento interval > 0
  1273. time_in_note = time - notestarttime; // Find fraction of time into current note
  1274. note_dur = noteendtime - notestarttime;
  1275. timefrac = time_in_note/note_dur;
  1276. current_port_int = portint * timefrac;
  1277. current_port_trans = pow(2.0,(current_port_int/SEMITONES_PER_OCTAVE));
  1278. thisdelay /= current_port_trans;
  1279. }
  1280. } else {
  1281. if((exit_status = read_value_from_brktable(time,ITF_DEL,dz))<0)
  1282. return exit_status;
  1283. thisdelay = dz->param[ITF_DEL];
  1284. }
  1285. } else
  1286. thisdelay = dz->param[ITF_DEL];
  1287. if(dz->brksize[ITF_RAND]) {
  1288. if((exit_status = read_value_from_brktable(time,ITF_RAND,dz))<0)
  1289. return exit_status;
  1290. }
  1291. if(dz->param[ITF_RAND] > 0.0) {
  1292. delay = (drand48() * 2.0) - 1.0; // Range -1 to +1
  1293. delay /= 2.0; // Range -1/2 to +1/2
  1294. delay *= dz->param[ITF_RAND]; // Range scaled to +- rand max * 1/2
  1295. delay += 1.0; // Range scaled to 1 +- rand max * 1/2
  1296. delay *= thisdelay;
  1297. ldelay = (int)round(delay);
  1298. } else
  1299. ldelay = (int)round(thisdelay);
  1300. time += (double)ldelay/srate; // update time for next table-read
  1301. k++;
  1302. }
  1303. dz->itemcnt = k;
  1304. if((delays = (int *)malloc(sizeof(int) * (k + 20)))==NULL) { // 20 is safety margin
  1305. sprintf(errstr,"INSUFFICIENT MEMORY establishing delays store.\n");
  1306. return(MEMORY_ERROR);
  1307. }
  1308. if((gains = (double *)malloc(sizeof(double) * (k + 20)))==NULL) {
  1309. sprintf(errstr,"INSUFFICIENT MEMORY establishing gains store.\n");
  1310. return(MEMORY_ERROR);
  1311. }
  1312. if((pshifts = (double *)malloc(sizeof(double) * (k + 20)))==NULL) {
  1313. sprintf(errstr,"INSUFFICIENT MEMORY establishing gains store.\n");
  1314. return(MEMORY_ERROR);
  1315. }
  1316. if(ODD(dz->mode)) {
  1317. if((linegains = (double *)malloc(sizeof(double) * (dz->brksize[ITF_DEL] + 1)))==NULL) {
  1318. sprintf(errstr,"INSUFFICIENT MEMORY establishing linegains store.\n");
  1319. return(MEMORY_ERROR);
  1320. }
  1321. if((linefades = (double *)malloc(sizeof(double) * (dz->brksize[ITF_DEL] + 1)))==NULL) {
  1322. sprintf(errstr,"INSUFFICIENT MEMORY establishing linefades store.\n");
  1323. return(MEMORY_ERROR);
  1324. }
  1325. if((upfades = (double *)malloc(sizeof(double) * (dz->brksize[ITF_DEL] + 1)))==NULL) {
  1326. sprintf(errstr,"INSUFFICIENT MEMORY establishing linefades store.\n");
  1327. return(MEMORY_ERROR);
  1328. }
  1329. if((linegaps = (double *)malloc(sizeof(double) * (dz->brksize[ITF_DEL] + 1)))==NULL) {
  1330. sprintf(errstr,"INSUFFICIENT MEMORY establishing linegaps store.\n");
  1331. return(MEMORY_ERROR);
  1332. }
  1333. if((exit_status = setup_lineportion_params(linegains,linefades,upfades,linegaps,dz))<0)
  1334. return exit_status;
  1335. }
  1336. srand((int)itfseed); // Restart SAME random sequence
  1337. initrand48();
  1338. notestarttime = -1.0; // Initialise reading of note starttimes
  1339. noteendtime = 0.0;
  1340. portint = 0.0;
  1341. sndseekEx(dz->ifd[0],0L,0);
  1342. memset((char *)dz->sampbuf[1],0,(dz->sampbuf[3] - dz->sampbuf[1]) * sizeof(float));
  1343. time = 0.0;
  1344. k = 0;
  1345. while(time < dz->param[ITF_DUR]) {
  1346. if(dz->brksize[ITF_DEL]) {
  1347. if(ODD(dz->mode)) {
  1348. if((exit_status = read_stepd_delay_value(time,&notestarttime,&noteendtime,&portint,dz))<0)
  1349. return exit_status;
  1350. thisdelay = dz->param[ITF_DEL];
  1351. if(!flteq(portint,0.0)) { // If portamento interval > 0
  1352. time_in_note = time - notestarttime; // Find fraction of time into current note
  1353. note_dur = noteendtime - notestarttime;
  1354. timefrac = time_in_note/note_dur;
  1355. current_port_int = portint * timefrac;
  1356. current_port_trans = pow(2.0,(current_port_int/SEMITONES_PER_OCTAVE));
  1357. thisdelay /= current_port_trans; // Adjust current delay for any portamento
  1358. }
  1359. } else {
  1360. if((exit_status = read_value_from_brktable(time,ITF_DEL,dz))<0)
  1361. return exit_status;
  1362. thisdelay = dz->param[ITF_DEL];
  1363. }
  1364. } else
  1365. thisdelay = dz->param[ITF_DEL];
  1366. if(dz->brksize[ITF_RAND]) {
  1367. if((exit_status = read_value_from_brktable(time,ITF_RAND,dz))<0)
  1368. return exit_status;
  1369. }
  1370. if(dz->param[ITF_RAND] > 0.0) {
  1371. delay = (drand48() * 2.0) - 1.0; // Range -1 to +1
  1372. delay /= 2.0; // Range -1/2 to +1/2
  1373. delay *= dz->param[ITF_RAND]; // Range scaled to +- rand max * 1/2
  1374. delay += 1.0; // Range scaled to 1 +- rand max * 1/2
  1375. delay *= thisdelay;
  1376. ldelay = (int)round(delay);
  1377. } else
  1378. ldelay = (int)round(thisdelay);
  1379. delays[k] = ldelay;
  1380. time += (double)ldelay/srate; // update time for next table-read
  1381. k++;
  1382. }
  1383. dovib = 1;
  1384. if(dz->brksize[ITF_VMIN]==0 && flteq(dz->param[ITF_VMIN],0.0)
  1385. && dz->brksize[ITF_VMAX]==0 && flteq(dz->param[ITF_VMAX],0.0)
  1386. && dz->brksize[ITF_DMIN]==0 && flteq(dz->param[ITF_DMIN],0.0)
  1387. && dz->brksize[ITF_DMAX]==0 && flteq(dz->param[ITF_DMAX],0.0))
  1388. dovib = 0;
  1389. if(dovib) {
  1390. if((exit_status = dovibrato(delays,dz))<0)
  1391. return exit_status;
  1392. }
  1393. memset((char *)dz->sampbuf[0],0,(dz->sampbuf[3] - dz->sampbuf[0]) * sizeof(float));
  1394. if((exit_status = read_samps(ibuf,dz))<0)
  1395. return(exit_status);
  1396. for(passno=2;passno<4;passno++) {
  1397. sndseekEx(dz->ifd[0],0L,0);
  1398. memset((char *)dz->sampbuf[1],0,(dz->sampbuf[3] - dz->sampbuf[1]) * sizeof(float));
  1399. time = 0.0;
  1400. lasttime = -1.0;
  1401. envlasttime = -1.0;
  1402. obufpos = 0;
  1403. absobufpos = 0;
  1404. bufs_written = 0;
  1405. k = 0;
  1406. if(passno == 2)
  1407. print_outmessage_flush("Second pass, assessing level.\n");
  1408. else {
  1409. print_outmessage_flush("Third pass, writing sound.\n");
  1410. dz->tempsize = dz->total_samps_written;
  1411. dz->total_samps_written = 0;
  1412. }
  1413. display_virtual_time(0L,dz);
  1414. fflush(stdout);
  1415. for(k = 0;k < dz->itemcnt;k++) {
  1416. if(passno == 2) {
  1417. if(dz->brksize[ITF_PRND]) {
  1418. if((exit_status = read_value_from_brktable(time,ITF_PRND,dz))<0)
  1419. return exit_status;
  1420. }
  1421. if(flteq(dz->param[ITF_PRND],0.0))
  1422. pshifts[k] = 1.0;
  1423. else {
  1424. pshift = (drand48() * 2.0) - 1.0;
  1425. pshift *= dz->param[ITF_PRND];
  1426. pshift = pow(2.0,pshift/SEMITONES_PER_OCTAVE); // semitones to frq ratio
  1427. pshifts[k] = 1.0/pshift; // frq ratio to read-increment
  1428. }
  1429. if(dz->ampvaried) {
  1430. gain = drand48() * dz->param[ITF_AMPC]; // Random amp reduction up to max at ITF_MAXC
  1431. gain = 1.0 - gain; // Actual gain
  1432. } else
  1433. gain = 1.0;
  1434. if(ODD(dz->mode)) {
  1435. if((exit_status = read_lineportion_gain(time,&lasttime,&linegain,linegains,dz))<0)
  1436. return exit_status;
  1437. gain *= linegain;
  1438. }
  1439. gains[k] = gain; // On pass 2, store gain
  1440. }
  1441. ldelay = delays[k]; // Get stored delay
  1442. gain = gains[k] * normaliser; // On pass 2, normaliser = 1: On pass 3, gain gets adjusted by normaliser (if ness)
  1443. if(dz->brksize[ITF_TRIM]) {
  1444. if((exit_status = read_value_from_brktable(time,ITF_TRIM,dz))<0)
  1445. return exit_status;
  1446. }
  1447. trimdur = 0; // Trim ZERO indicates no trim
  1448. if(dz->param[ITF_TRIM] > 0.0)
  1449. trimdur = (int)round(dz->param[ITF_TRIM] * srate);
  1450. if(dz->brksize[ITF_TRBY]) {
  1451. if((exit_status = read_value_from_brktable(time,ITF_TRBY,dz))<0)
  1452. return exit_status;
  1453. }
  1454. trimfade = 0; // Trimfade ZERO indicates no fade
  1455. if(dz->param[ITF_TRBY] > 0.0)
  1456. trimfade = (int)round(dz->param[ITF_TRBY] * srate);
  1457. if(!flteq(pshifts[k],1.0)) {
  1458. if((exit_status = transpos_read_element(&transposcnt,pshifts[k],dz)) < 0)
  1459. return exit_status; // Transpos source into intermediate tbuf
  1460. getbuf = tbuf; // Set to read from tbuf
  1461. } else {
  1462. transposcnt = dz->insams[0]; // Simply a 1-1 read from input buffer
  1463. getbuf = ibuf; // Set to read from input buffer
  1464. }
  1465. dozeros = 0;
  1466. if(trimdur > 0) { // If trim is to be done
  1467. dozeros = max(transposcnt - trimdur,0); // Can the trim be done on the length of sound available (some smaples will be zeroed) ??
  1468. if(dozeros > 0) { // If trim can be done
  1469. if(trimfade == 0) // If no fade is specified, use minimum fade (if poss)
  1470. trimfade = min(transposcnt - dozeros,minfade);
  1471. else // Else use specified fade (if poss)
  1472. trimfade = min(transposcnt - dozeros,trimfade);
  1473. } else if(trimfade > 0) // If trim can't be done, but fade is specified
  1474. trimfade = min(transposcnt,trimfade); // Set up fade
  1475. } else if(trimfade > 0) // If trim is not specified, but fade is set
  1476. trimfade = min(transposcnt,trimfade); // Check fade length
  1477. fade_to = transposcnt - dozeros; // Number of non-zero samples to write
  1478. write_to = fade_to - trimfade; // Number of original level samples to write
  1479. for(n = 0; n < write_to;n++) { // Write all samples before any element_fade
  1480. obuf[obufpos] = (float)(obuf[obufpos] + (getbuf[n] * gain));
  1481. obufpos++;
  1482. }
  1483. if(trimfade) { // Where necessary, do fade of element
  1484. spliceval = 1.0;
  1485. spliceincr = 1.0/(double)trimfade;
  1486. for(; n < fade_to;n++) {
  1487. spliceval = max(0.0,spliceval - spliceincr);
  1488. splicer = pow(spliceval,dz->param[ITF_SLOP]);
  1489. obuf[obufpos] = (float)(obuf[obufpos] + (getbuf[n] * gain * splicer));
  1490. obufpos++;
  1491. }
  1492. }
  1493. this_last_write = (bufs_written * dz->buflen) + obufpos;
  1494. if(this_last_write > last_write)
  1495. last_write = this_last_write;
  1496. absobufpos += delays[k]; // Absolute position of next write in output
  1497. bufs_before = absobufpos/dz->buflen; // Number of full buffers preceding this
  1498. while(bufs_written < bufs_before) { // If not yet written this many bufs
  1499. if(passno == 2) { // output the missing bufs
  1500. if(ODD(dz->mode)) { // Where line is in pitched portions, adjust for envelope of these pitched-portions
  1501. for(m = 0; m < dz->buflen;m++) {
  1502. outtime = (double)((bufs_written * dz->buflen) + m)/srate;
  1503. if((exit_status = read_lineportion_envelope(outtime,&envlasttime,&linedur,&linefade,&linefadeincr,&upfade,&upfadeincr,
  1504. &lineenv,linefades,upfades,linegaps,minfade,pshifts,dz))<0)
  1505. return exit_status;
  1506. lineoutgain = portion_env(&linedur,&linefade,&lineenv,linefadeincr,&upfade,upfadeincr);
  1507. maxoutsamp = max(maxoutsamp,fabs(obuf[m] * lineoutgain));
  1508. }
  1509. } else {
  1510. for(m = 0; m < dz->buflen;m++)
  1511. maxoutsamp = max(maxoutsamp,fabs(obuf[m]));
  1512. }
  1513. } else {
  1514. if(ODD(dz->mode)) { // Where line is in pitched portions, adjust for envelope of these pitched-portions
  1515. for(m = 0; m < dz->buflen;m++) {
  1516. outtime = (double)((bufs_written * dz->buflen) + m)/srate;
  1517. if((exit_status = read_lineportion_envelope(outtime,&envlasttime,&linedur,&linefade,&linefadeincr,&upfade,&upfadeincr,
  1518. &lineenv,linefades,upfades,linegaps,minfade,pshifts,dz))<0)
  1519. return exit_status;
  1520. lineoutgain = portion_env(&linedur,&linefade,&lineenv,linefadeincr,&upfade,upfadeincr);
  1521. obuf[m] = (float)(obuf[m] * lineoutgain);
  1522. }
  1523. }
  1524. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1525. return(exit_status);
  1526. }
  1527. memcpy((char *)obuf,(char *)ovflw,dz->buflen * sizeof(float));
  1528. memset((char *)ovflw,0,dz->buflen * sizeof(float));
  1529. bufs_written++;
  1530. }
  1531. obufpos = absobufpos % dz->buflen; // Find position in output buffer
  1532. time += (double)delays[k]/srate; // update time for next table-read
  1533. }
  1534. local_last_write = last_write - (bufs_written * dz->buflen);
  1535. if(local_last_write > 0) { // Flush output
  1536. if(passno == 2) {
  1537. if(ODD(dz->mode)) { // Where line is in pitched portions, adjust for envelope of these pitched-portions
  1538. for(m = 0; m < local_last_write;m++) {
  1539. outtime = (double)((bufs_written * dz->buflen) + m)/srate;
  1540. if((exit_status = read_lineportion_envelope(outtime,&envlasttime,&linedur,&linefade,&linefadeincr,&upfade,&upfadeincr,
  1541. &lineenv,linefades,upfades,linegaps,minfade,pshifts,dz))<0)
  1542. return exit_status;
  1543. lineoutgain = portion_env(&linedur,&linefade,&lineenv,linefadeincr,&upfade,upfadeincr);
  1544. maxoutsamp = max(maxoutsamp,fabs(obuf[m] * lineoutgain));
  1545. }
  1546. } else {
  1547. for(m = 0; m < local_last_write;m++)
  1548. maxoutsamp = max(maxoutsamp,fabs(obuf[m]));
  1549. }
  1550. } else {
  1551. if(ODD(dz->mode)) { // Where line is in pitched portions, adjust for envelope of these pitched-portions
  1552. for(m = 0; m < local_last_write;m++) {
  1553. outtime = (double)((bufs_written * dz->buflen) + m)/srate;
  1554. if((exit_status = read_lineportion_envelope(outtime,&envlasttime,&linedur,&linefade,&linefadeincr,&upfade,&upfadeincr,
  1555. &lineenv,linefades,upfades,linegaps,minfade,pshifts,dz))<0)
  1556. return exit_status;
  1557. lineoutgain = portion_env(&linedur,&linefade,&lineenv,linefadeincr,&upfade,upfadeincr);
  1558. obuf[m] = (float)(obuf[m] * lineoutgain);
  1559. }
  1560. }
  1561. if((exit_status = write_samps(obuf,local_last_write,dz))<0)
  1562. return(exit_status);
  1563. }
  1564. }
  1565. if(passno == 2) { // If overload, set normaliser < 1.0
  1566. if(maxoutsamp > ACCEPTABLE_MAXLEVEL)
  1567. normaliser = ACCEPTABLE_MAXLEVEL/maxoutsamp;
  1568. }
  1569. }
  1570. return FINISHED;
  1571. }
  1572. /**************************** READ_STEPD_DELAY_VALUE *****************************/
  1573. int read_stepd_delay_value(double thistime,double *notestarttime,double *noteendtime,double *portint,dataptr dz)
  1574. {
  1575. int exit_status;
  1576. double *p, *del = dz->brk[ITF_DEL], *delend = dz->brk[ITF_DEL] + (dz->brksize[ITF_DEL] * 2);
  1577. double currentnotestart = 0.0, val, thisport;
  1578. int lastval = (dz->brksize[ITF_DEL] * 2) - 1;
  1579. p = del;
  1580. while(thistime >= *p) {
  1581. p += 2;
  1582. if(p >= delend) {
  1583. dz->param[ITF_DEL] = del[lastval];
  1584. currentnotestart = *(delend-2);
  1585. *noteendtime = dz->param[ITF_DUR];
  1586. break;
  1587. }
  1588. }
  1589. if(p < delend) {
  1590. dz->param[ITF_DEL] = *(p-1);
  1591. currentnotestart = *(p-2);
  1592. *noteendtime = *p;
  1593. }
  1594. if(currentnotestart > *notestarttime) { // IF in new note
  1595. if(dz->iparam[ITF_PORT] == 0)
  1596. *portint = 0.0;
  1597. else { // IF portamento is set
  1598. if(dz->brksize[ITF_PINT]) { // Get portamento interval
  1599. if((exit_status = read_value_from_brktable(currentnotestart,ITF_PINT,dz))<0)
  1600. return exit_status;
  1601. }
  1602. thisport = dz->param[ITF_PINT];
  1603. switch(dz->iparam[ITF_PORT]) { // Portamento up, down or randomly up/dn
  1604. case(1):
  1605. break;
  1606. case(-1):
  1607. thisport = -thisport;
  1608. break;
  1609. default:
  1610. val = drand48();
  1611. if(val < 0.5)
  1612. thisport = -thisport;
  1613. break;
  1614. }
  1615. *portint = thisport; // Set (end of) portamento interval
  1616. }
  1617. }
  1618. *notestarttime = currentnotestart; // (Re)set start of current note
  1619. return FINISHED;
  1620. }
  1621. /********************** TRANSPOS_READ_ELEMENT ********************************/
  1622. int transpos_read_element(int *transposcnt,double incr,dataptr dz)
  1623. {
  1624. float *ibuf = dz->sampbuf[0], *tbuf = dz->sampbuf[3];
  1625. int k, lo, hi;
  1626. double dpos, loval, hival, diff, frac;
  1627. dpos = 0.0;
  1628. k = 0;
  1629. while(dpos < (double)dz->insams[0]) {
  1630. lo = (int)floor(dpos);
  1631. hi = (int)ceil(dpos);
  1632. frac = dpos - (double)lo;
  1633. loval = ibuf[lo];
  1634. hival = ibuf[hi];
  1635. diff = hival - loval;
  1636. tbuf[k++] = (float)(loval + (diff * frac));
  1637. if(k > dz->overflowsize) {
  1638. sprintf(errstr,"TRANSPOSITION BUFFER OVERFLOW\n");
  1639. return PROGRAM_ERROR;
  1640. }
  1641. dpos += incr;
  1642. }
  1643. *transposcnt = k;
  1644. return FINISHED;
  1645. }
  1646. /********************** DOVIBRATO ********************************/
  1647. int dovibrato(int *delays,dataptr dz)
  1648. {
  1649. int cyclestart, n, exit_status;
  1650. int *vdelays;
  1651. double *sintab;
  1652. double vfrq, vdep, time, sintabincr, sintabpos, loval, hival, frac, diff, vval, srate = (double)dz->infile->srate;
  1653. double time_advance;
  1654. int k, samptime, lo, hi;
  1655. if((vdelays = (int *)malloc(sizeof(int) * dz->itemcnt))==NULL) {
  1656. sprintf(errstr,"INSUFFICIENT MEMORY establishing vibrato delays store.\n");
  1657. return(MEMORY_ERROR);
  1658. }
  1659. if((sintab = (double *)malloc(sizeof(double) * (TS_SINTABSIZE + 1)))==NULL) {
  1660. sprintf(errstr,"INSUFFICIENT MEMORY establishing sintable array for vibrato.\n");
  1661. return(MEMORY_ERROR);
  1662. }
  1663. for(n=0;n < TS_SINTABSIZE;n++) // Vibrato sintable
  1664. sintab[n] = sin((double)n/(double)TS_SINTABSIZE) * TWOPI;
  1665. sintab[n] = 0.0; // wraparound point
  1666. if(dz->brksize[ITF_VMIN]) { // Initialise vibrato params
  1667. if((exit_status = read_value_from_brktable(0,ITF_VMIN,dz))<0)
  1668. return exit_status;
  1669. }
  1670. if(dz->brksize[ITF_VMAX]) {
  1671. if((exit_status = read_value_from_brktable(0,ITF_VMAX,dz))<0)
  1672. return exit_status;
  1673. }
  1674. vfrq = drand48() * (dz->param[ITF_VMAX] - dz->param[ITF_VMIN]);
  1675. vfrq += dz->param[ITF_VMIN];
  1676. time_advance = (double)delays[0]/srate; // Set increment in sintable
  1677. sintabincr = time_advance * TS_SINTABSIZE * vfrq;
  1678. if(dz->brksize[ITF_DMIN]) {
  1679. if((exit_status = read_value_from_brktable(0,ITF_DMIN,dz))<0)
  1680. return exit_status;
  1681. }
  1682. if(dz->brksize[ITF_DMAX]) {
  1683. if((exit_status = read_value_from_brktable(0,ITF_DMAX,dz))<0)
  1684. return exit_status;
  1685. }
  1686. vdep = drand48() * (dz->param[ITF_DMAX] - dz->param[ITF_DMIN]);
  1687. vdep += dz->param[ITF_DMIN];
  1688. sintabpos = 0.0;
  1689. samptime = 0;
  1690. time = 0.0;
  1691. cyclestart = 0;
  1692. for(k=0;k < dz->itemcnt;k++) {
  1693. if(cyclestart) { // At the start of a vibrato sine-cycle
  1694. time = (double)samptime/srate; // Reset all vibrato values
  1695. if(dz->brksize[ITF_VMIN]) {
  1696. if((exit_status = read_value_from_brktable(time,ITF_VMIN,dz))<0)
  1697. return exit_status;
  1698. }
  1699. if(dz->brksize[ITF_VMAX]) {
  1700. if((exit_status = read_value_from_brktable(time,ITF_VMAX,dz))<0)
  1701. return exit_status;
  1702. }
  1703. vfrq = drand48() * (dz->param[ITF_VMAX] - dz->param[ITF_VMIN]);
  1704. vfrq += dz->param[ITF_VMIN];
  1705. time_advance = (double)delays[k]/srate; // Set increment in sintable
  1706. sintabincr = time_advance * TS_SINTABSIZE * vfrq;
  1707. if(dz->brksize[ITF_DMIN]) {
  1708. if((exit_status = read_value_from_brktable(time,ITF_DMIN,dz))<0)
  1709. return exit_status;
  1710. }
  1711. if(dz->brksize[ITF_DMAX]) {
  1712. if((exit_status = read_value_from_brktable(time,ITF_DMAX,dz))<0)
  1713. return exit_status;
  1714. }
  1715. vdep = drand48() * (dz->param[ITF_DMAX] - dz->param[ITF_DMIN]);
  1716. vdep += dz->param[ITF_DMIN];
  1717. cyclestart = 0;
  1718. }
  1719. lo = (int)floor(sintabpos); // Read sin table, interpolating
  1720. hi = (int)ceil(sintabpos);
  1721. frac = sintabpos - (double)lo;
  1722. loval = sintab[lo];
  1723. hival = sintab[hi];
  1724. diff = hival - loval;
  1725. vval = loval + (diff * frac);
  1726. vval *= vdep; // Multiply by semitone depth
  1727. vval = pow(2.0,vval/SEMITONES_PER_OCTAVE); // Convert to frq ratio
  1728. vval = 1.0/vval; // Convert to change in wavelen (> frq -> < wavelen)
  1729. vdelays[k] = (int)round((double)delays[k] * vval); // Vibrato modifies wavelen
  1730. sintabpos += sintabincr; // Advance in sintable
  1731. if(sintabpos >= TS_SINTABSIZE) { // Once sine-cycle completed, set flag to reset vibrato params (above)
  1732. cyclestart = 1;
  1733. sintabpos -= (double)TS_SINTABSIZE;
  1734. }
  1735. samptime += delays[k]; // Advance time
  1736. }
  1737. for(k=0;k < dz->itemcnt;k++) // Convert orig delays to vibratoed delays
  1738. delays[k] = vdelays[k];
  1739. free(vdelays);
  1740. return FINISHED;
  1741. }
  1742. /**************************** SETUP_LINEPORTION_PARAMS *****************************/
  1743. int setup_lineportion_params(double *linegains,double *linefades, double *upfades, double *linegaps,dataptr dz)
  1744. {
  1745. int exit_status;
  1746. double *p, *del = dz->brk[ITF_DEL], *delend = dz->brk[ITF_DEL] + (dz->brksize[ITF_DEL] * 2);
  1747. int line_portion_cnt = 0, line_portion_end = dz->brksize[ITF_DEL] + 1;
  1748. double time, linegain, diff;
  1749. p = del;
  1750. while(p < delend) {
  1751. time = *p;
  1752. if(line_portion_cnt >= line_portion_end) {
  1753. sprintf(errstr,"Array overrun setting line-portion parameter arrays.\n");
  1754. return PROGRAM_ERROR;
  1755. }
  1756. if(dz->brksize[ITF_GMIN]) {
  1757. if((exit_status = read_value_from_brktable(time,ITF_GMIN,dz))<0)
  1758. return exit_status;
  1759. }
  1760. if(dz->brksize[ITF_GMAX]) {
  1761. if((exit_status = read_value_from_brktable(time,ITF_GMAX,dz))<0)
  1762. return exit_status;
  1763. }
  1764. if(dz->brksize[ITF_FADE]) {
  1765. if((exit_status = read_value_from_brktable(time,ITF_FADE,dz))<0)
  1766. return exit_status;
  1767. }
  1768. if(dz->brksize[ITF_UFAD]) {
  1769. if((exit_status = read_value_from_brktable(time,ITF_UFAD,dz))<0)
  1770. return exit_status;
  1771. }
  1772. if(dz->brksize[ITF_GAPP]) {
  1773. if((exit_status = read_value_from_brktable(time,ITF_GAPP,dz))<0)
  1774. return exit_status;
  1775. }
  1776. if(!flteq(dz->param[ITF_GMIN],dz->param[ITF_GMAX])) {
  1777. diff = dz->param[ITF_GMAX] - dz->param[ITF_GMIN];
  1778. linegain = (drand48() * diff) + dz->param[ITF_GMIN];
  1779. } else
  1780. linegain = dz->param[ITF_GMAX];
  1781. linegains[line_portion_cnt] = linegain;
  1782. linefades[line_portion_cnt] = dz->param[ITF_FADE];
  1783. upfades[line_portion_cnt] = dz->param[ITF_UFAD];
  1784. linegaps[line_portion_cnt] = dz->param[ITF_GAPP];
  1785. line_portion_cnt++;
  1786. p +=2;
  1787. }
  1788. return(FINISHED);
  1789. }
  1790. /**************************** READ_LINEPORTION_GAIN *****************************/
  1791. int read_lineportion_gain(double thistime,double *lasttime,double *linegain,double *linegains,dataptr dz)
  1792. {
  1793. double *p, *del, *delend;
  1794. double portion_time;
  1795. int k = 0;
  1796. if(dz->brksize[ITF_DEL]) {
  1797. del = dz->brk[ITF_DEL];
  1798. delend = dz->brk[ITF_DEL] + (dz->brksize[ITF_DEL] * 2);
  1799. p = del;
  1800. while(thistime >= *p) { // Step forward in portion-pitches brkfile until time in brkfile exceeds NOW (thistime)
  1801. k++;
  1802. p += 2;
  1803. if(p >= delend) // If step off end of breakfile, quit
  1804. break;
  1805. }
  1806. k--;
  1807. p -= 2; // We must be in previous brkfile portion, so step back to starttime of previous portion
  1808. portion_time = *p;
  1809. if(portion_time > *lasttime) { // If starttime of current portion is NOT same as starttime of previous portion
  1810. *linegain = linegains[k]; // Reset the portion params
  1811. *lasttime = portion_time; // And set new starttime for current portion
  1812. }
  1813. } else
  1814. *linegain = 1.0;
  1815. return(FINISHED);
  1816. }
  1817. /**************************** READ_LINEPORTION_ENVELOPE *****************************/
  1818. int read_lineportion_envelope(double thistime,double *envlasttime,int *linedur,int *linefade,double *linefadeincr,int *upfade,double *upfadeincr, double *lineenv,
  1819. double *linefades,double *upfades,double *linegaps, int minfade, double *pshifts,dataptr dz)
  1820. {
  1821. double *p, *del, *delend;
  1822. double portion_time, ratio, lineend, penultdur, nulineend, srate = (double)dz->infile->srate;
  1823. int dur, fade, gap, up_fade, slopes;
  1824. int k = 0;
  1825. if(dz->brksize[ITF_DEL]) {
  1826. del = dz->brk[ITF_DEL];
  1827. delend = dz->brk[ITF_DEL] + (dz->brksize[ITF_DEL] * 2);
  1828. p = del;
  1829. while(thistime >= *p) { // Step forward in portion-pitches brkfile until time in brkfile exceeds NOW (thistime)
  1830. k++;
  1831. p += 2;
  1832. if(p >= delend) // If step off end of breakfile, quit
  1833. break;
  1834. }
  1835. k--;
  1836. p -= 2; // We must be in previous brkfile portion, so step back to starttime of previous portion
  1837. portion_time = *p;
  1838. if(portion_time > *envlasttime) { // If starttime of current portion is NOT same as starttime of previous portion
  1839. p += 2; // Find duration of new line-portion
  1840. if(p >= delend) {
  1841. lineend = dz->param[ITF_DUR] + (dz->duration/pshifts[dz->itemcnt-1]);
  1842. if(p-4 >= dz->brk[ITF_DEL]) {
  1843. penultdur = *(p-2) - *(p-4); // Curtail last pitch to duration of prevoius line-pitch
  1844. nulineend = *(p-2) + penultdur;
  1845. lineend = min(lineend,nulineend);
  1846. }
  1847. }
  1848. else
  1849. lineend = *p;
  1850. dur = (int)((double)(lineend - portion_time) * srate);
  1851. gap = (int)((double)dur * linegaps[k]); // Silence between portions is proportion of duration
  1852. dur -= gap; // Duration (at full level) reduced by length of gap
  1853. up_fade = (int)(upfades[k] * srate);
  1854. if(up_fade < minfade) // Upfade cannot be less than a (no-clicks) minimum
  1855. up_fade = minfade;
  1856. fade = (int)(linefades[k] * srate);
  1857. if(fade < minfade) // Fade cannot be less than a (no-clicks) minimum
  1858. fade = minfade;
  1859. if((slopes = up_fade + fade) >= dur) {
  1860. ratio = (double)slopes/(double)dur;
  1861. up_fade = (int)round(up_fade * ratio);
  1862. fade = (int)round(fade * ratio);
  1863. slopes = up_fade + fade;
  1864. while(slopes >= dur) {
  1865. if(up_fade >= fade)
  1866. up_fade--;
  1867. else
  1868. fade--;
  1869. slopes--;
  1870. }
  1871. }
  1872. dur -= slopes; // Duration (at full level) reduced by length of fades
  1873. *upfade = up_fade;
  1874. *upfadeincr = 1.0/(double)up_fade;
  1875. *linefadeincr = 1.0/fade;
  1876. *linedur = dur; // Set up line-counters
  1877. *linefade = fade;
  1878. *lineenv = 1.0; // initialise line envelope to 1.0
  1879. *envlasttime = portion_time; // And set new starttime for current portion
  1880. }
  1881. } else {
  1882. *linedur = INT_MAX;
  1883. *linefade = 0;
  1884. *lineenv = 1.0;
  1885. *upfade = 0;
  1886. }
  1887. return(FINISHED);
  1888. }
  1889. /**************************** PORTION_ENV *****************************/
  1890. double portion_env(int *linedur, int *linefade, double *lineenv, double linefadeincr,int *upfade,double upfadeincr)
  1891. {
  1892. double gain;
  1893. if(*upfade > 0) {
  1894. gain = max(0.0,1.0 - (*upfade * upfadeincr));
  1895. (*upfade)--;
  1896. } else if(*linedur > 0) { // Still in non-fade, non-silence part of portion
  1897. gain = 1.0;
  1898. (*linedur)--; // No change to level
  1899. } else {
  1900. if(*linefade > 0) { // If in fade
  1901. *lineenv = max(*lineenv - linefadeincr,0.0); // Adjust lineenv
  1902. gain = *lineenv; // Multiply element gain by lineenv
  1903. (*linefade)--;
  1904. } else {
  1905. gain = 0.0; // If fade has reached 0 , gain is zero
  1906. }
  1907. }
  1908. return gain;
  1909. }