synthesis.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License aint 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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <structures.h>
  24. #include <tkglobals.h>
  25. #include <pnames.h>
  26. #include <filetype.h>
  27. #include <processno.h>
  28. #include <modeno.h>
  29. #include <logic.h>
  30. #include <globcon.h>
  31. #include <cdpmain.h>
  32. #include <math.h>
  33. #include <mixxcon.h>
  34. #include <osbind.h>
  35. #include <standalone.h>
  36. #include <ctype.h>
  37. #include <sfsys.h>
  38. #include <string.h>
  39. #include <srates.h>
  40. #ifdef unix
  41. #include <aaio.h>
  42. #endif
  43. #define S_OFF 0
  44. #define S_ON 1
  45. #define SIGNAL_TO_LEFT (0)
  46. #define SIGNAL_TO_RIGHT (1)
  47. #define ROOT2 (1.4142136)
  48. #define SYN_FROMROOT 0
  49. #define SYN_TOROOT 1
  50. #define SYN_SPACED 2
  51. #define SYN_X 3
  52. #define SYN_JUMP 4
  53. #define SYNDUFF_MIN
  54. #if defined unix || defined __GNUC__
  55. #define round(x) lround((x))
  56. #endif
  57. #ifndef HUGE
  58. #define HUGE 3.40282347e+38F
  59. #endif
  60. char errstr[2400];
  61. int anal_infiles = 1;
  62. int sloom = 0;
  63. int sloombatch = 0;
  64. const char* cdp_version = "7.1.0";
  65. //CDP LIB REPLACEMENTS
  66. static int setup_synthesizer_application(dataptr dz);
  67. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  68. static int setup_synthesis_param_ranges_and_defaults(dataptr dz);
  69. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  70. static int open_the_outfile(dataptr dz);
  71. static int handle_the_special_data(char *str,dataptr dz);
  72. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  73. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  74. static int establish_application(dataptr dz);
  75. static int initialise_vflags(dataptr dz);
  76. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  77. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  78. static int mark_parameter_types(dataptr dz,aplptr ap);
  79. static int assign_file_data_storage(int infilecnt,dataptr dz);
  80. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  81. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  82. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  83. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  84. static int synthesis_param_preprocess(int **perm,int **permon,int **permoff,int **superperm,double *minrate,int *maxsteps,dataptr dz);
  85. static int synthesis(int *perm,int *permon,int *permoff,int *superperm,double minrate,int maxsteps,dataptr dz);
  86. static void incr_sinptr(int n,double time,double onehzincr,dataptr dz);
  87. static double read_level(int n,double time,dataptr dz);
  88. static int create_synthesizer_sndbufs(dataptr dz);
  89. static int generate_packet_envelope (dataptr dz);
  90. static double read_packet_envelope(int kk,double incr,dataptr dz);
  91. static int modify_packet_envelope(dataptr dz);
  92. static void rndintperm(int *perm,int cnt);
  93. static void get_current_partial_vals(double time,double *pvals,int totalpartials,dataptr dz);
  94. static void pancalc(double position,double *leftgain,double *rightgain);
  95. static void sort_partials_into_ascending_frq_order(int total_partialcnt,double *pvals,double *sinptr,
  96. double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz);
  97. static void resort_partials_into_original_frq_order(int total_partialcnt,double *pvals,double *sinptr,
  98. double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz);
  99. static void xclusive(int *perm,int *permon,int *permoff,int max_partials_cnt,int partials_in_play, int **onoff,int stepcnt);
  100. static double emergepos(int emergchan,int chans,double time,double timespan);
  101. static double convergepos(int converchan,int chans,double time,double convergetime,double dur);
  102. static void spacebox_apply(double pos, double lev,int chans,int *lmost, int *rmost,double *rlev,double *llev,int spacetyp);
  103. static void output_special_spatialisation_sample(float *obuf,int sampcnt,int switchpos,int chans,double val,double valr,int lmost,int rmost,int spacetyp);
  104. static void spacebox(double *pos, int *switchpos, double posstep, int chans, int spacetyp, int configno, int configcnt,int *superperm);
  105. static double sinread(double *tabpos,double frq,dataptr dz);
  106. static void duffing_osc(double *val,double *vel, double delta_t,double *tabpos,dataptr dz);
  107. static int duffing(dataptr dz);
  108. /**************************************** MAIN *********************************************/
  109. int main(int argc,char *argv[])
  110. {
  111. int exit_status;
  112. dataptr dz = NULL;
  113. char **cmdline, sfnam[400];
  114. int cmdlinecnt;
  115. aplptr ap;
  116. int is_launched = FALSE;
  117. int *perm, *permon, *permoff, *superperm;
  118. int maxsteps = 0;
  119. double minrate = 0.0;
  120. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  121. fprintf(stdout,"%s\n",cdp_version);
  122. fflush(stdout);
  123. return 0;
  124. }
  125. /* CHECK FOR SOUNDLOOM */
  126. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  127. sloom = 0;
  128. sloombatch = 1;
  129. }
  130. if(sflinit("cdp")){
  131. sfperror("cdp: initialisation\n");
  132. return(FAILED);
  133. }
  134. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  135. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  136. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  137. return(FAILED);
  138. }
  139. if(!sloom) {
  140. if(argc == 1) {
  141. usage1();
  142. return(FAILED);
  143. } else if(argc == 2) {
  144. usage2(argv[1]);
  145. return(FAILED);
  146. }
  147. }
  148. if(!sloom) {
  149. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  150. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(FAILED);
  152. }
  153. cmdline = argv;
  154. cmdlinecnt = argc;
  155. if((get_the_process_no(argv[0],dz))<0)
  156. return(FAILED);
  157. cmdline++;
  158. cmdlinecnt--;
  159. dz->maxmode = 4;
  160. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  161. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  162. return(exit_status);
  163. }
  164. cmdline++;
  165. cmdlinecnt--;
  166. // setup_particular_application =
  167. if(dz->mode == 2 && cmdlinecnt < 8) {
  168. usage2("synthesis");
  169. return(FAILED);
  170. }
  171. if((exit_status = setup_synthesizer_application(dz))<0) {
  172. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  173. return(FAILED);
  174. }
  175. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  176. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  177. return(FAILED);
  178. }
  179. } else {
  180. //parse_TK_data() =
  181. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  182. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  183. return(exit_status);
  184. }
  185. }
  186. ap = dz->application;
  187. dz->infile->channels = 1;
  188. // parse_infile_and_hone_type() =
  189. // setup_param_ranges_and_defaults() =
  190. if((exit_status = setup_synthesis_param_ranges_and_defaults(dz))<0) {
  191. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  192. return(FAILED);
  193. }
  194. // open_first_infile() : redundant
  195. // handle_extra_infiles() : redundant
  196. // handle_outfile() =
  197. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  198. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(FAILED);
  200. }
  201. // handle_formants() redundant
  202. // handle_formant_quiksearch() redundant
  203. if(dz->mode != 3) {
  204. strcpy(sfnam,cmdline[0]);
  205. cmdlinecnt--;
  206. cmdline++;
  207. }
  208. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  209. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  210. return(FAILED);
  211. }
  212. // check_param_validity_and_consistency() redundant
  213. if(dz->mode == 3) {
  214. if((dz->parray = (double **)malloc(sizeof(double *)))==NULL) {
  215. sprintf(errstr,"INSUFFICIENT MEMORY to create sinetable array.\n");
  216. return(MEMORY_ERROR);
  217. }
  218. } else {
  219. if((exit_status = handle_the_special_data(sfnam,dz))<0) {
  220. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  221. return(FAILED);
  222. }
  223. }
  224. is_launched = TRUE;
  225. if((exit_status = create_synthesizer_sndbufs(dz))<0) { // CDP LIB
  226. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  227. return(FAILED);
  228. }
  229. if((exit_status = synthesis_param_preprocess(&perm,&permon,&permoff,&superperm,&minrate,&maxsteps,dz))<0) {
  230. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  231. return(FAILED);
  232. }
  233. if(dz->mode == 2 && dz->iparam[SYNTH_CHANS] > 1)
  234. dz->infile->channels = dz->iparam[SYNTH_CHANS];
  235. if((exit_status = open_the_outfile(dz))<0) {
  236. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  237. return(FAILED);
  238. }
  239. //spec_process_file =
  240. if(dz->mode == 3) {
  241. // NB sintable is in dz->parray[0]
  242. // dz->rampbrksize = sample-length of final splice
  243. if((exit_status = duffing(dz))<0) {
  244. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  245. return(FAILED);
  246. }
  247. } else {
  248. if((exit_status = synthesis(perm,permon,permoff,superperm,minrate,maxsteps,dz))<0) {
  249. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  250. return(FAILED);
  251. }
  252. }
  253. if((exit_status = complete_output(dz))<0) { // CDP LIB
  254. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  255. return(FAILED);
  256. }
  257. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  258. free(dz);
  259. return(SUCCEEDED);
  260. }
  261. /**********************************************
  262. REPLACED CDP LIB FUNCTIONS
  263. **********************************************/
  264. /****************************** SET_PARAM_DATA *********************************/
  265. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  266. {
  267. ap->special_data = (char)special_data;
  268. ap->param_cnt = (char)paramcnt;
  269. ap->max_param_cnt = (char)maxparamcnt;
  270. if(ap->max_param_cnt>0) {
  271. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  272. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  273. return(MEMORY_ERROR);
  274. }
  275. strcpy(ap->param_list,paramlist);
  276. }
  277. return(FINISHED);
  278. }
  279. /****************************** SET_VFLGS *********************************/
  280. int set_vflgs
  281. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  282. {
  283. ap->option_cnt = (char) optcnt; /*RWD added cast */
  284. if(optcnt) {
  285. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  286. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  287. return(MEMORY_ERROR);
  288. }
  289. strcpy(ap->option_list,optlist);
  290. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  291. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  292. return(MEMORY_ERROR);
  293. }
  294. strcpy(ap->option_flags,optflags);
  295. }
  296. ap->vflag_cnt = (char) vflagcnt;
  297. ap->variant_param_cnt = (char) vparamcnt;
  298. if(vflagcnt) {
  299. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  300. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  301. return(MEMORY_ERROR);
  302. }
  303. strcpy(ap->variant_list,varlist);
  304. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  305. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  306. return(MEMORY_ERROR);
  307. }
  308. strcpy(ap->variant_flags,varflags);
  309. }
  310. return(FINISHED);
  311. }
  312. /***************************** APPLICATION_INIT **************************/
  313. int application_init(dataptr dz)
  314. {
  315. int exit_status;
  316. int storage_cnt;
  317. int tipc, brkcnt;
  318. aplptr ap = dz->application;
  319. if(ap->vflag_cnt>0)
  320. initialise_vflags(dz);
  321. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  322. ap->total_input_param_cnt = (char)tipc;
  323. if(tipc>0) {
  324. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  325. return(exit_status);
  326. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  327. return(exit_status);
  328. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  329. return(exit_status);
  330. }
  331. brkcnt = tipc;
  332. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  333. if(brkcnt>0) {
  334. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  335. return(exit_status);
  336. }
  337. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  338. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  339. return(exit_status);
  340. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  341. return(exit_status);
  342. }
  343. if((exit_status = mark_parameter_types(dz,ap))<0)
  344. return(exit_status);
  345. // establish_infile_constants() replaced by
  346. dz->infilecnt = 1;
  347. //establish_bufptrs_and_extra_buffers():
  348. return(FINISHED);
  349. }
  350. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  351. /* RWD mallo changed to calloc; helps debug verison run as release! */
  352. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  353. {
  354. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  355. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  356. return(MEMORY_ERROR);
  357. }
  358. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  359. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  360. return(MEMORY_ERROR);
  361. }
  362. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  363. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  364. return(MEMORY_ERROR);
  365. }
  366. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  367. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  368. return(MEMORY_ERROR);
  369. }
  370. return(FINISHED);
  371. }
  372. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  373. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  374. {
  375. int n;
  376. for(n=0;n<storage_cnt;n++) {
  377. dz->is_int[n] = (char)0;
  378. dz->no_brk[n] = (char)0;
  379. }
  380. return(FINISHED);
  381. }
  382. /***************************** MARK_PARAMETER_TYPES **************************/
  383. int mark_parameter_types(dataptr dz,aplptr ap)
  384. {
  385. int n, m; /* PARAMS */
  386. for(n=0;n<ap->max_param_cnt;n++) {
  387. switch(ap->param_list[n]) {
  388. case('0'): break; /* dz->is_active[n] = 0 is default */
  389. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  390. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  391. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  392. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  393. default:
  394. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  395. return(PROGRAM_ERROR);
  396. }
  397. } /* OPTIONS */
  398. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  399. switch(ap->option_list[n]) {
  400. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  401. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  402. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  403. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  404. default:
  405. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  406. return(PROGRAM_ERROR);
  407. }
  408. } /* VARIANTS */
  409. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  410. switch(ap->variant_list[n]) {
  411. case('0'): break;
  412. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  413. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  414. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  415. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  416. default:
  417. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  418. return(PROGRAM_ERROR);
  419. }
  420. } /* INTERNAL */
  421. for(n=0,
  422. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  423. switch(ap->internal_param_list[n]) {
  424. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  425. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  426. case('d'): dz->no_brk[m] = (char)1; break;
  427. default:
  428. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  429. return(PROGRAM_ERROR);
  430. }
  431. }
  432. return(FINISHED);
  433. }
  434. /************************ HANDLE_THE_OUTFILE *********************/
  435. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  436. {
  437. char *filename = (*cmdline)[0], *p;
  438. if(filename[0]=='-' && filename[1]=='f') {
  439. dz->floatsam_output = 1;
  440. dz->true_outfile_stype = SAMP_FLOAT;
  441. filename+= 2;
  442. }
  443. if(!sloom) {
  444. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  445. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  446. return(DATA_ERROR);
  447. }
  448. }
  449. p = filename; // Drop file extension
  450. while(*p != ENDOFSTR) {
  451. if(*p == '.') {
  452. *p = ENDOFSTR;
  453. break;
  454. }
  455. p++;
  456. }
  457. strcpy(dz->outfilename,filename);
  458. (*cmdline)++;
  459. (*cmdlinecnt)--;
  460. return(FINISHED);
  461. }
  462. /************************ OPEN_THE_OUTFILE *********************/
  463. int open_the_outfile(dataptr dz)
  464. {
  465. int exit_status;
  466. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  467. return(exit_status);
  468. return(FINISHED);
  469. }
  470. /***************************** ESTABLISH_APPLICATION **************************/
  471. int establish_application(dataptr dz)
  472. {
  473. aplptr ap;
  474. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  475. sprintf(errstr,"establish_application()\n");
  476. return(MEMORY_ERROR);
  477. }
  478. ap = dz->application;
  479. memset((char *)ap,0,sizeof(struct applic));
  480. return(FINISHED);
  481. }
  482. /************************* INITIALISE_VFLAGS *************************/
  483. int initialise_vflags(dataptr dz)
  484. {
  485. int n;
  486. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  487. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  488. return(MEMORY_ERROR);
  489. }
  490. for(n=0;n<dz->application->vflag_cnt;n++)
  491. dz->vflag[n] = FALSE;
  492. return FINISHED;
  493. }
  494. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  495. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  496. {
  497. int n;
  498. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  499. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  500. return(MEMORY_ERROR);
  501. }
  502. for(n=0;n<tipc;n++)
  503. ap->default_val[n] = 0.0;
  504. return(FINISHED);
  505. }
  506. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  507. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  508. {
  509. int n;
  510. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  511. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  512. return(MEMORY_ERROR);
  513. }
  514. for(n=0;n<tipc;n++)
  515. dz->is_active[n] = (char)0;
  516. return(FINISHED);
  517. }
  518. /************************* SETUP_SYNTHESIZER_APPLICATION *******************/
  519. int setup_synthesizer_application(dataptr dz)
  520. {
  521. int exit_status;
  522. aplptr ap;
  523. if((exit_status = establish_application(dz))<0) // GLOBAL
  524. return(FAILED);
  525. ap = dz->application;
  526. // SEE parstruct FOR EXPLANATION of next 2 functions
  527. switch(dz->mode) {
  528. case(0):
  529. if((exit_status = set_param_data(ap,SYN_PARTIALS,3,3,"idD"))<0)
  530. return(FAILED);
  531. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  532. return(exit_status);
  533. break;
  534. case(1):
  535. if((exit_status = set_param_data(ap,SYN_PARTIALS,3,3,"idD"))<0)
  536. return(FAILED);
  537. if((exit_status = set_vflgs(ap,"nc",2,"DD","f",1,0,"0"))<0)
  538. return(exit_status);
  539. break;
  540. case(2):
  541. if((exit_status = set_param_data(ap,SYN_PARTIALS,6,6,"idDiiD"))<0)
  542. return(FAILED);
  543. if((exit_status = set_vflgs(ap,"udfsneEcCtr",11,"ddddiididid","azmxj",5,0,"00000"))<0)
  544. return(exit_status);
  545. break;
  546. case(3):
  547. if((exit_status = set_param_data(ap,0,6,6,"idDDdd"))<0)
  548. return(FAILED);
  549. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  550. return(exit_status);
  551. break;
  552. }
  553. // set_legal_infile_structure -->
  554. dz->has_otherfile = FALSE;
  555. // assign_process_logic -->
  556. dz->input_data_type = NO_FILE_AT_ALL;
  557. dz->process_type = UNEQUAL_SNDFILE;
  558. dz->outfiletype = SNDFILE_OUT;
  559. return application_init(dz); //GLOBAL
  560. }
  561. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  562. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  563. {
  564. int exit_status;
  565. infileptr infile_info;
  566. if(!sloom) {
  567. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  568. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  569. return(MEMORY_ERROR);
  570. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  571. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  572. return(PROGRAM_ERROR);
  573. } else if(infile_info->filetype != SNDFILE) {
  574. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  575. return(DATA_ERROR);
  576. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  577. sprintf(errstr,"Failed to copy file parsing information\n");
  578. return(PROGRAM_ERROR);
  579. }
  580. free(infile_info);
  581. }
  582. return(FINISHED);
  583. }
  584. /************************* SETUP_SYNTHESIS_PARAM_RANGES_AND_DEFAULTS *******************/
  585. int setup_synthesis_param_ranges_and_defaults(dataptr dz)
  586. {
  587. int exit_status;
  588. aplptr ap = dz->application;
  589. // set_param_ranges()
  590. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  591. // NB total_input_param_cnt is > 0 !!!
  592. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  593. return(FAILED);
  594. // get_param_ranges()
  595. ap->lo[SYNTHSRAT] = 16000;
  596. ap->hi[SYNTHSRAT] = 96000;
  597. ap->default_val[SYNTHSRAT] = 44100.0;
  598. ap->lo[SYNTH_DUR] = 0.0;
  599. ap->hi[SYNTH_DUR] = 32767.0;
  600. ap->default_val[SYNTH_DUR] = 1.0;
  601. ap->lo[SYNTH_FRQ] = .001;
  602. ap->hi[SYNTH_FRQ] = 10000;
  603. ap->default_val[SYNTH_FRQ] = 440;
  604. if(dz->mode == 1) {
  605. ap->lo[SYNTH_SQZ] = 0.0;
  606. ap->hi[SYNTH_SQZ] = 1000.0;
  607. ap->default_val[SYNTH_SQZ] = 1.0;
  608. ap->lo[SYNTH_CTR] = -1.0;
  609. ap->hi[SYNTH_CTR] = 1.0;
  610. ap->default_val[SYNTH_CTR] = 0.0;
  611. } else if(dz->mode == 2) {
  612. ap->lo[SYNTH_CHANS] = 1;
  613. ap->hi[SYNTH_CHANS] = 16.0;
  614. ap->default_val[SYNTH_CHANS] = 1.0;
  615. ap->lo[SYNTH_MAX] = 1.0;
  616. ap->hi[SYNTH_MAX] = 8.0;
  617. ap->default_val[SYNTH_MAX] = 3.0;
  618. ap->lo[SYNTH_RATE] = 0.004;
  619. ap->hi[SYNTH_RATE] = 100;
  620. ap->default_val[SYNTH_RATE] = 0.1;
  621. ap->lo[SYNTH_RISE] = 0.0;
  622. ap->hi[SYNTH_RISE] = 100;
  623. ap->default_val[SYNTH_RISE] = 0;
  624. ap->lo[SYNTH_FALL] = 0.0;
  625. ap->hi[SYNTH_FALL] = 100;
  626. ap->default_val[SYNTH_FALL] = 0;
  627. ap->lo[SYNTH_STDY] = 0.0;
  628. ap->hi[SYNTH_STDY] = 3600;
  629. ap->default_val[SYNTH_STDY] = 0;
  630. ap->lo[SYNTH_SPLEN] = 2;
  631. ap->hi[SYNTH_SPLEN] = 50;
  632. ap->default_val[SYNTH_SPLEN] = 5;
  633. ap->lo[SYNTH_NUM] = 0;
  634. ap->hi[SYNTH_NUM] = 1000;
  635. ap->default_val[SYNTH_NUM] = 0;
  636. ap->lo[SYNTH_EFROM] = 0;
  637. ap->hi[SYNTH_EFROM] = 16.0;
  638. ap->default_val[SYNTH_EFROM] = 0;
  639. ap->lo[SYNTH_ETIME] = 0;
  640. ap->hi[SYNTH_ETIME] = 32767.0;
  641. ap->default_val[SYNTH_ETIME] = 0;
  642. ap->lo[SYNTH_CTO] = 0;
  643. ap->hi[SYNTH_CTO] = 16.0;
  644. ap->default_val[SYNTH_CTO] = 0;
  645. ap->lo[SYNTH_CTIME] = 0;
  646. ap->hi[SYNTH_CTIME] = 32767.0;
  647. ap->default_val[SYNTH_CTIME] = 0;
  648. ap->lo[SYNTH_STYPE] = 0;
  649. ap->hi[SYNTH_STYPE] = 14;
  650. ap->default_val[SYNTH_STYPE] = 0;
  651. ap->lo[SYNTH_RSPEED] = -20;
  652. ap->hi[SYNTH_RSPEED] = 20;
  653. ap->default_val[SYNTH_RSPEED] = 0;
  654. } else if(dz->mode == 3) {
  655. ap->lo[SYNTH_FRQ] = .1;
  656. ap->hi[SYNTH_FRQ] = 200;
  657. ap->default_val[SYNTH_FRQ] = 70;
  658. ap->lo[SYNTH_DAMP] = .15;
  659. ap->hi[SYNTH_DAMP] = 2;
  660. ap->default_val[SYNTH_DAMP] = .5;
  661. ap->lo[SYNTH_K] = -10;
  662. ap->hi[SYNTH_K] = 10;
  663. ap->default_val[SYNTH_K] = 1;
  664. ap->lo[SYNTH_B] = 20;
  665. ap->hi[SYNTH_B] = 50;
  666. ap->default_val[SYNTH_B] = 30;
  667. }
  668. dz->maxmode = 4;
  669. if(!sloom)
  670. put_default_vals_in_all_params(dz);
  671. return(FINISHED);
  672. }
  673. /********************************* PARSE_SLOOM_DATA *********************************/
  674. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  675. {
  676. int exit_status;
  677. int cnt = 1, infilecnt;
  678. int filesize, insams, inbrksize;
  679. double dummy;
  680. int true_cnt = 0;
  681. aplptr ap;
  682. while(cnt<=PRE_CMDLINE_DATACNT) {
  683. if(cnt > argc) {
  684. sprintf(errstr,"Insufficient data sent from TK\n");
  685. return(DATA_ERROR);
  686. }
  687. switch(cnt) {
  688. case(1):
  689. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  690. sprintf(errstr,"Cannot read process no. sent from TK\n");
  691. return(DATA_ERROR);
  692. }
  693. break;
  694. case(2):
  695. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  696. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  697. return(DATA_ERROR);
  698. }
  699. if(dz->mode > 0)
  700. dz->mode--;
  701. //setup_particular_application() =
  702. if((exit_status = setup_synthesizer_application(dz))<0)
  703. return(exit_status);
  704. ap = dz->application;
  705. break;
  706. case(3):
  707. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  708. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  709. return(DATA_ERROR);
  710. }
  711. if(infilecnt < 1) {
  712. true_cnt = cnt + 1;
  713. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  714. }
  715. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  716. return(exit_status);
  717. break;
  718. case(INPUT_FILETYPE+4):
  719. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  720. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  721. return(DATA_ERROR);
  722. }
  723. break;
  724. case(INPUT_FILESIZE+4):
  725. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  726. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  727. return(DATA_ERROR);
  728. }
  729. dz->insams[0] = filesize;
  730. break;
  731. case(INPUT_INSAMS+4):
  732. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  733. sprintf(errstr,"Cannot read insams sent from TK\n");
  734. return(DATA_ERROR);
  735. }
  736. dz->insams[0] = insams;
  737. break;
  738. case(INPUT_SRATE+4):
  739. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  740. sprintf(errstr,"Cannot read srate sent from TK\n");
  741. return(DATA_ERROR);
  742. }
  743. break;
  744. case(INPUT_CHANNELS+4):
  745. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  746. sprintf(errstr,"Cannot read channels sent from TK\n");
  747. return(DATA_ERROR);
  748. }
  749. break;
  750. case(INPUT_STYPE+4):
  751. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  752. sprintf(errstr,"Cannot read stype sent from TK\n");
  753. return(DATA_ERROR);
  754. }
  755. break;
  756. case(INPUT_ORIGSTYPE+4):
  757. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  758. sprintf(errstr,"Cannot read origstype sent from TK\n");
  759. return(DATA_ERROR);
  760. }
  761. break;
  762. case(INPUT_ORIGRATE+4):
  763. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  764. sprintf(errstr,"Cannot read origrate sent from TK\n");
  765. return(DATA_ERROR);
  766. }
  767. break;
  768. case(INPUT_MLEN+4):
  769. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  770. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  771. return(DATA_ERROR);
  772. }
  773. break;
  774. case(INPUT_DFAC+4):
  775. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  776. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  777. return(DATA_ERROR);
  778. }
  779. break;
  780. case(INPUT_ORIGCHANS+4):
  781. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  782. sprintf(errstr,"Cannot read origchans sent from TK\n");
  783. return(DATA_ERROR);
  784. }
  785. break;
  786. case(INPUT_SPECENVCNT+4):
  787. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  788. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  789. return(DATA_ERROR);
  790. }
  791. dz->specenvcnt = dz->infile->specenvcnt;
  792. break;
  793. case(INPUT_WANTED+4):
  794. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  795. sprintf(errstr,"Cannot read wanted sent from TK\n");
  796. return(DATA_ERROR);
  797. }
  798. break;
  799. case(INPUT_WLENGTH+4):
  800. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  801. sprintf(errstr,"Cannot read wlength sent from TK\n");
  802. return(DATA_ERROR);
  803. }
  804. break;
  805. case(INPUT_OUT_CHANS+4):
  806. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  807. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  808. return(DATA_ERROR);
  809. }
  810. break;
  811. /* RWD these chanegs to samps - tk will have to deal with that! */
  812. case(INPUT_DESCRIPTOR_BYTES+4):
  813. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  814. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  815. return(DATA_ERROR);
  816. }
  817. break;
  818. case(INPUT_IS_TRANSPOS+4):
  819. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  820. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  821. return(DATA_ERROR);
  822. }
  823. break;
  824. case(INPUT_COULD_BE_TRANSPOS+4):
  825. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  826. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  827. return(DATA_ERROR);
  828. }
  829. break;
  830. case(INPUT_COULD_BE_PITCH+4):
  831. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  832. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  833. return(DATA_ERROR);
  834. }
  835. break;
  836. case(INPUT_DIFFERENT_SRATES+4):
  837. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  838. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  839. return(DATA_ERROR);
  840. }
  841. break;
  842. case(INPUT_DUPLICATE_SNDS+4):
  843. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  844. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  845. return(DATA_ERROR);
  846. }
  847. break;
  848. case(INPUT_BRKSIZE+4):
  849. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  850. sprintf(errstr,"Cannot read brksize sent from TK\n");
  851. return(DATA_ERROR);
  852. }
  853. if(inbrksize > 0) {
  854. switch(dz->input_data_type) {
  855. case(WORDLIST_ONLY):
  856. break;
  857. case(PITCH_AND_PITCH):
  858. case(PITCH_AND_TRANSPOS):
  859. case(TRANSPOS_AND_TRANSPOS):
  860. dz->tempsize = inbrksize;
  861. break;
  862. case(BRKFILES_ONLY):
  863. case(UNRANGED_BRKFILE_ONLY):
  864. case(DB_BRKFILES_ONLY):
  865. case(ALL_FILES):
  866. case(ANY_NUMBER_OF_ANY_FILES):
  867. if(dz->extrabrkno < 0) {
  868. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  869. return(DATA_ERROR);
  870. }
  871. if(dz->brksize == NULL) {
  872. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  873. return(PROGRAM_ERROR);
  874. }
  875. dz->brksize[dz->extrabrkno] = inbrksize;
  876. break;
  877. default:
  878. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  879. dz->input_data_type);
  880. return(PROGRAM_ERROR);
  881. }
  882. break;
  883. }
  884. break;
  885. case(INPUT_NUMSIZE+4):
  886. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  887. sprintf(errstr,"Cannot read numsize sent from TK\n");
  888. return(DATA_ERROR);
  889. }
  890. break;
  891. case(INPUT_LINECNT+4):
  892. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  893. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  894. return(DATA_ERROR);
  895. }
  896. break;
  897. case(INPUT_ALL_WORDS+4):
  898. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  899. sprintf(errstr,"Cannot read all_words sent from TK\n");
  900. return(DATA_ERROR);
  901. }
  902. break;
  903. case(INPUT_ARATE+4):
  904. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  905. sprintf(errstr,"Cannot read arate sent from TK\n");
  906. return(DATA_ERROR);
  907. }
  908. break;
  909. case(INPUT_FRAMETIME+4):
  910. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  911. sprintf(errstr,"Cannot read frametime sent from TK\n");
  912. return(DATA_ERROR);
  913. }
  914. dz->frametime = (float)dummy;
  915. break;
  916. case(INPUT_WINDOW_SIZE+4):
  917. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  918. sprintf(errstr,"Cannot read window_size sent from TK\n");
  919. return(DATA_ERROR);
  920. }
  921. break;
  922. case(INPUT_NYQUIST+4):
  923. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  924. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  925. return(DATA_ERROR);
  926. }
  927. break;
  928. case(INPUT_DURATION+4):
  929. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  930. sprintf(errstr,"Cannot read duration sent from TK\n");
  931. return(DATA_ERROR);
  932. }
  933. break;
  934. case(INPUT_MINBRK+4):
  935. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  936. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  937. return(DATA_ERROR);
  938. }
  939. break;
  940. case(INPUT_MAXBRK+4):
  941. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  942. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  943. return(DATA_ERROR);
  944. }
  945. break;
  946. case(INPUT_MINNUM+4):
  947. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  948. sprintf(errstr,"Cannot read minnum sent from TK\n");
  949. return(DATA_ERROR);
  950. }
  951. break;
  952. case(INPUT_MAXNUM+4):
  953. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  954. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  955. return(DATA_ERROR);
  956. }
  957. break;
  958. default:
  959. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  960. return(PROGRAM_ERROR);
  961. }
  962. cnt++;
  963. }
  964. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  965. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  966. return(DATA_ERROR);
  967. }
  968. if(true_cnt)
  969. cnt = true_cnt;
  970. *cmdlinecnt = 0;
  971. while(cnt < argc) {
  972. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  973. return(exit_status);
  974. cnt++;
  975. }
  976. return(FINISHED);
  977. }
  978. /********************************* GET_TK_CMDLINE_WORD *********************************/
  979. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  980. {
  981. if(*cmdlinecnt==0) {
  982. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  983. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  984. return(MEMORY_ERROR);
  985. }
  986. } else {
  987. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  988. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  989. return(MEMORY_ERROR);
  990. }
  991. }
  992. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  993. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  994. return(MEMORY_ERROR);
  995. }
  996. strcpy((*cmdline)[*cmdlinecnt],q);
  997. (*cmdlinecnt)++;
  998. return(FINISHED);
  999. }
  1000. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1001. int assign_file_data_storage(int infilecnt,dataptr dz)
  1002. {
  1003. int exit_status;
  1004. int no_sndfile_system_files = FALSE;
  1005. dz->infilecnt = infilecnt;
  1006. if((exit_status = allocate_filespace(dz))<0)
  1007. return(exit_status);
  1008. if(no_sndfile_system_files)
  1009. dz->infilecnt = 0;
  1010. return(FINISHED);
  1011. }
  1012. /************************* redundant functions: to ensure libs compile OK *******************/
  1013. int assign_process_logic(dataptr dz)
  1014. {
  1015. return(FINISHED);
  1016. }
  1017. void set_legal_infile_structure(dataptr dz)
  1018. {}
  1019. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1020. {
  1021. return(FINISHED);
  1022. }
  1023. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1024. {
  1025. return(FINISHED);
  1026. }
  1027. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1028. {
  1029. return(FINISHED);
  1030. }
  1031. int read_special_data(char *str,dataptr dz)
  1032. {
  1033. return(FINISHED);
  1034. }
  1035. int inner_loop
  1036. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1037. {
  1038. return(FINISHED);
  1039. }
  1040. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1041. {
  1042. return(FINISHED);
  1043. }
  1044. /******************************** USAGE1 ********************************/
  1045. int usage1(void)
  1046. {
  1047. usage2("synthesis");
  1048. return(USAGE_ONLY);
  1049. }
  1050. /********************************************************************************************/
  1051. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1052. {
  1053. if(!strcmp(prog_identifier_from_cmdline,"synthesis")) dz->process = SYNTHESIZER;
  1054. else {
  1055. fprintf(stderr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1056. return(USAGE_ONLY);
  1057. }
  1058. return(FINISHED);
  1059. }
  1060. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  1061. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  1062. {
  1063. int n;
  1064. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1065. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  1066. return(MEMORY_ERROR);
  1067. }
  1068. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1069. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  1070. return(MEMORY_ERROR);
  1071. }
  1072. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1073. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  1074. return(MEMORY_ERROR);
  1075. }
  1076. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1077. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  1078. return(MEMORY_ERROR);
  1079. }
  1080. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1081. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  1082. return(MEMORY_ERROR);
  1083. }
  1084. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1085. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  1086. return(MEMORY_ERROR);
  1087. }
  1088. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1089. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  1090. return(MEMORY_ERROR);
  1091. }
  1092. for(n=0;n<brkcnt;n++) {
  1093. dz->brk[n] = NULL;
  1094. dz->brkptr[n] = NULL;
  1095. dz->brkinit[n] = 0;
  1096. dz->brksize[n] = 0;
  1097. }
  1098. return(FINISHED);
  1099. }
  1100. /******************************** USAGE2 ********************************/
  1101. int usage2(char *str)
  1102. {
  1103. int k;
  1104. if(!strcmp(str,"synthesis")) {
  1105. fprintf(stderr,
  1106. "USAGE:\n"
  1107. "newsynth synthesis 1 outf spectrum srate dur frq\n"
  1108. "newsynth synthesis 2 outf spectrum srate dur frq [-nnarrowing] [-ccentring]\n"
  1109. "newsynth synthesis 3 outf spectrum srate dur frq chans maxrange rate\n"
  1110. " [-urise] [-dfall] [-fsteady] [-ssplice] [-nN] [-a] [-z] [-x]\n"
  1111. " [-tspacetype] [-rrotspeed]\n"
  1112. " [[-m] [-j] [efrom -Etime] [cto -Ctime]]\n"
  1113. "newsynth synthesis 4 outf srate dur frq damping k b\n"
  1114. "\n"
  1115. "Synthesize complex spectra.\n"
  1116. "\n"
  1117. "MODE 1 Generates tones with any number of (possibly varying) partials.\n"
  1118. "MODE 2 Generates wave-packet streams with any number of (possibly varying) partials.\n"
  1119. "MODE 3 Multichan Mode1 where partials (spread over N octaves) fade in-out randomly.\n"
  1120. "MODE 4 Duffing damped oscillator (frq, amplitude and damping can vary through time).\n"
  1121. "\n"
  1122. "Press any key to see further information.\n");
  1123. while(!kbhit())
  1124. ;
  1125. if(kbhit()) {
  1126. fprintf(stderr,
  1127. "SPECTRUM Listing of partial ratios and relative levels, against time.\n"
  1128. " Data: text file of data lines. Every line has same number of entries.\n"
  1129. " 1ST ENTRY on each line is time. Times must start at zero and increase.\n"
  1130. " ALL EVEN NUMBERED ENTRIES are partial numbers.\n"
  1131. " For tone-generation, the first partial number on each line must be 1.\n"
  1132. " Partial numbers must increase from entry to entry.\n"
  1133. " ALL OTHER ODD NUMBERED ENTRIES are partial levels, and may have any value.\n"
  1134. " -ve values invert the phase of the partial.\n"
  1135. "SRATE Sample rate of synthesized sound.\n"
  1136. "DUR Duration of synthesized sound.\n"
  1137. "FRQ Possibly time-varying Fundamental frq of output (0.001 to 10000Hz) OR\n"
  1138. " (Mode 4) of forcing oscillation (1-200Hz).\n"
  1139. "DAMPING (Mode 4) Possibly time-varying damping of forced oscillation (0.15 to 2).\n"
  1140. "K, B Coefficients determe nature of damping. (k -10 to 10 : b 20 to 50)\n"
  1141. "NARROWING Narrowing of packet envelope (0 - 1000).\n"
  1142. " Values below 1.0 broaden the packet.\n"
  1143. " Values very close to zero may produce clicks (square-wave envelope).\n"
  1144. " Very high vals with very high frqs may produce click-impulses or silence.\n"
  1145. "CENTRING Centring of peak of packet envelope.\n"
  1146. " 0 peak at centre: -1 peak at start: 1 peak at end.\n"
  1147. "CHANS Number of output channels.\n"
  1148. "MAXRANGE Max range of transposition of spectral components (in whole 8vas).\n"
  1149. "STEP Average time between changes to partial-content of output.\n"
  1150. "RISE Time to expand to maximum range.\n"
  1151. "FALL Time to return to initial range, before end.\n"
  1152. "STEADY Duration of steady state at sound end.\n"
  1153. "SPLICE Splices for partial entry and exit, in mS.\n"
  1154. "-nN (Number) Same fixed number (N) of partials chosen for each event.\n"
  1155. "-a Initial rise in number of partials from only-the-fundamental.\n"
  1156. "-z Fall in number of partials , during \"steady state\" to fundamental.\n"
  1157. "-x (Xclusive) change all partials (as far as poss) from event to event.\n"
  1158. "-m (Move) Distribute partials in space.\n"
  1159. "-j (Jump) All partials assigned to same location for any one event.\n"
  1160. "SPACETYPE Type of output spatialisation.\n"
  1161. "ROTSPEED rotation speed (for certain spatialisation types).\n"
  1162. "-e -E (Emerge) sound emerges from channel \"from\" over time \"time\" at start.\n"
  1163. "-c -C (Converge) Sound converges to channel \"to\" over time \"time\" at end.\n"
  1164. "NB: Flags -j,-e,-E,-c,-C only operational if -m set.\n"
  1165. "NB: Flags with NO params must be placed AFTER any flags WITH params, on the cmdline.\n"
  1166. "\n"
  1167. "Hit key 's' to continue to \"SPACETYPE\" information, or 'e' to exit.\n");
  1168. }
  1169. while((k = getch())!='s' && k != 'e')
  1170. ;
  1171. if(k == 's') {
  1172. fprintf(stderr,
  1173. "\n"
  1174. "SPACETYPE options : For 8-channel output only.\n"
  1175. "\n"
  1176. "1 Positions alternate between Left and Right sides, but are otherwise random.\n"
  1177. "2 Positions alternate between Front and Back, but are otherwise random.\n"
  1178. "3 Rotating clockwise or anticlockwise.\n"
  1179. "4 Random permutations of all 8 channels.\n"
  1180. "5 ... plus all possible pairs of channels.\n"
  1181. "6 ... plus all possible meaningful small and large triangles.\n"
  1182. "7 ... plus square, diamond and all-at-once.\n"
  1183. " In types 4 to 7, all members of perm used before next perm starts.\n"
  1184. "8 Alternate between all-left and all-right.\n"
  1185. "9 Alternate between all-front and all-back.\n"
  1186. "10 Alternate between all-square and all-diamond.\n"
  1187. "11 Rotate triangle formed by lspkrs 2-apart clockwise.\n"
  1188. "12 Rotate triangle formed by lspkrs 3-apart clockwise.\n"
  1189. "13 Rotate triangle formed by lspkrs 2-apart anticlockwise.\n"
  1190. "14 Rotate triangle formed by lspkrs 3-apart anticlockwise.\n");
  1191. }
  1192. } else
  1193. fprintf(stdout,"Unknown option '%s'\n",str);
  1194. return(USAGE_ONLY);
  1195. }
  1196. int usage3(char *str1,char *str2)
  1197. {
  1198. fprintf(stderr,"Insufficient parameters on command line.\n");
  1199. return(USAGE_ONLY);
  1200. }
  1201. /**************************** SYNTHESIS_PARAM_PREPROCESS *************************/
  1202. int synthesis_param_preprocess (int **perm,int **permon,int **permoff,int **superperm,double *minrate,int *maxsteps,dataptr dz)
  1203. {
  1204. int exit_status, sinarray, pntarray = 0, chans, configno;
  1205. int n, m;
  1206. double *sintab, *thispartials, srate, nyquist, endsplice, time, top, maxrate;
  1207. int partialscnt = dz->itemcnt, entrycnt = dz->ringsize;
  1208. if(BAD_SR(dz->param[SYNTHSRAT])) {
  1209. sprintf(errstr,"Invalid sample rate (%lf) entered.\n",dz->param[SYNTHSRAT]);
  1210. return(MEMORY_ERROR);
  1211. }
  1212. dz->infile->srate = dz->iparam[SYNTHSRAT];
  1213. srate = (double)dz->iparam[SYNTHSRAT];
  1214. nyquist = srate/2.0;
  1215. chans = dz->iparam[SYNTH_CHANS];
  1216. // Establish end splice length
  1217. dz->iparam[SYNTH_DUR] = (int)round(dz->param[SYNTH_DUR] * srate);
  1218. if(dz->mode == 2) {
  1219. if(dz->brksize[SYNTH_RATE]) {
  1220. if((exit_status = get_maxvalue(SYNTH_RATE,&maxrate,dz))<0)
  1221. return PROGRAM_ERROR;
  1222. if((exit_status = get_minvalue_in_brktable(minrate,SYNTH_RATE,dz))<0)
  1223. return PROGRAM_ERROR;
  1224. } else {
  1225. maxrate = dz->param[SYNTH_RATE];
  1226. *minrate = dz->param[SYNTH_RATE];
  1227. }
  1228. if(maxrate >= dz->param[SYNTH_DUR]/2.0) {
  1229. sprintf(errstr,"(max) Rate (%lf) must be less than half duration (%lf).\n",maxrate,dz->param[SYNTH_DUR]);
  1230. return(DATA_ERROR);
  1231. }
  1232. if(dz->iparam[SYNTH_STYPE] > 0) {
  1233. if(*minrate <= dz->param[SYNTH_SPLEN] * MS_TO_SECS * 2.0) {
  1234. sprintf(errstr,"(min) Rate (%lf) must be greater than 2 * splice (%lf) For special spatialisation types\n",*minrate,dz->param[SYNTH_SPLEN] * MS_TO_SECS * 2.0);
  1235. return(DATA_ERROR);
  1236. }
  1237. } else {
  1238. if(*minrate <= dz->param[SYNTH_SPLEN] * MS_TO_SECS) {
  1239. sprintf(errstr,"(min) Rate (%lf) must be greater than splicelength (%lf).\n",*minrate,dz->param[SYNTH_SPLEN] * MS_TO_SECS);
  1240. return(DATA_ERROR);
  1241. }
  1242. }
  1243. if(dz->vflag[SYN_SPACED] && dz->iparam[SYNTH_CHANS] < 2) { // Can't spatialise to mono output!!
  1244. fprintf(stdout,"WARNING: Mono output: Can't spatialise the output.\n");
  1245. fflush(stdout);;
  1246. dz->vflag[SYN_SPACED] = 0;
  1247. }
  1248. endsplice = 50.0 * MS_TO_SECS;
  1249. dz->rampbrksize = (int)floor(endsplice * srate);
  1250. } else {
  1251. endsplice = 50.0 * MS_TO_SECS; // Go for big splice
  1252. if(dz->param[SYNTH_DUR] <= endsplice * 2)
  1253. endsplice = min(dz->param[SYNTH_DUR]/4.0,5.0 * MS_TO_SECS); // Else go for small splice
  1254. dz->rampbrksize = (int)floor(endsplice * srate); // Establish size of final splice
  1255. }
  1256. // Check no partials go over nyquist
  1257. if(dz->mode != 3) {
  1258. for(n=0;n < partialscnt;n++) {
  1259. thispartials = dz->parray[n];
  1260. for(m=0;m<entrycnt;m+=2) {
  1261. if(dz->brksize[SYNTH_FRQ]) {
  1262. time = thispartials[m];
  1263. if((exit_status = read_value_from_brktable(time,SYNTH_FRQ,dz))<0)
  1264. return(exit_status);
  1265. }
  1266. top = thispartials[m+1] * dz->param[SYNTH_FRQ];
  1267. if(top >= nyquist) {
  1268. sprintf(errstr,"Partial %lf at time %lf (frq %lf) is above the nyquist (%lf)\n",thispartials[m+1],thispartials[m],dz->param[SYNTH_FRQ],nyquist);
  1269. return(DATA_ERROR);
  1270. }
  1271. }
  1272. }
  1273. }
  1274. // Establish sine-table
  1275. if(dz->mode == 3)
  1276. sinarray = 0;
  1277. else {
  1278. sinarray = partialscnt * 2;
  1279. pntarray = sinarray + 1;
  1280. }
  1281. if((dz->parray[sinarray] = (double *)malloc((SYNTH_TABSIZE + 1) * sizeof(double)))==NULL) {
  1282. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1283. return(MEMORY_ERROR);
  1284. }
  1285. sintab = dz->parray[sinarray];
  1286. for(n=0;n<SYNTH_TABSIZE;n++)
  1287. sintab[n] = sin(PI * 2.0 * ((double)n/(double)SYNTH_TABSIZE));
  1288. sintab[n] = sintab[0]; /* wrap around point */
  1289. // Pointers into sintable for all partials
  1290. if(dz->mode == 3)
  1291. return FINISHED;
  1292. else if(dz->mode == 2) {
  1293. /*
  1294. * | |
  1295. * MODE 2 arrays |positions
  1296. * pcnt = partialcnt mpcnt = maxpartial cnt (partials + all transpositions) | | |
  1297. * | current
  1298. * | frqs|
  1299. * parray |----------|----------|-|-|-----------------|-----------------|-|-|-|
  1300. * | tvarying pno+plevel |s|s| left_level | right-level step| |
  1301. * | (Mpcnt*2) |i|i| mpcnt | mpcnt times |
  1302. * | |n|n| | | | | |
  1303. * address 0 mpcnt*2| |p|(mpcnt*2)+2 |(mpcnt*3)+2 | (mpcnt*4)+3
  1304. * | | |t| | (mpcnt*4)+2
  1305. * | | |r| | | | (mpcnt*4)+4
  1306. * lengths | linelen of srcdata | | | maxsteps | maxsteps | |m| |
  1307. * |s|t| |t|p|t|
  1308. * (slen = sintablen) |l|o| |o|c|o|
  1309. * (totl = estimate of no |e|t| |t|n|t|
  1310. * of timesteps used) |n|l| |l|t|l|
  1311. *
  1312. *
  1313. * iparray |-----------------|-----------------|-----------------|-| switchpos
  1314. * | on-off flags | leftmost chan | spo |s|porder
  1315. * | (mpcnt) | (mpcnt) | (mpcnt) |p| | (mpcnt*3)+2
  1316. * | | | |l| (mpcnt*3)+1
  1317. * address 0 mpcnt mpcnt*2 (mpcnt*3)
  1318. * | | | |c| |
  1319. * lengths | maxsteps | maxsteps | maxsteps |nmpcnt
  1320. * |t| mpcnt
  1321. * |r| |
  1322. * (splcntrs = splice counters) |s| |
  1323. * (spo = orig values of splice counters)
  1324. */
  1325. // A sine-table pointer for every partial and every partial transposition
  1326. if((dz->parray[pntarray] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1327. sprintf(errstr,"INSUFFICIENT MEMORY for sine table pointers.\n");
  1328. return(MEMORY_ERROR);
  1329. }
  1330. // An array for every partials on-off markers, every leftmost-chan, every splice-counter-orig-vals, plus actual splice-cntrs + porder
  1331. if((dz->iparray = (int **)malloc(((partialscnt * 3) + 3) * sizeof(int *)))==NULL) {
  1332. sprintf(errstr,"INSUFFICIENT MEMORY for integer array fpr partial on-off markers.\n");
  1333. return(MEMORY_ERROR);
  1334. }
  1335. *maxsteps = (int)ceil(dz->param[SYNTH_DUR]/(*minrate)) + 100; // SAFETY
  1336. for(n=0,m=dz->temp_sampsize;n<partialscnt;n++,m++) {
  1337. // An array of on-off switching vals at steptimes, for every partial and partial-transposition
  1338. if((dz->iparray[n] = (int *)malloc((*maxsteps) * sizeof(int)))==NULL) {
  1339. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off marker array %d.\n",n); // base address = 0
  1340. return(MEMORY_ERROR);
  1341. }
  1342. // An array of leftmost output channel at steptimes, for every partial and partial-transposition
  1343. if((dz->iparray[n+partialscnt] = (int *)malloc((*maxsteps) * sizeof(int)))==NULL) {
  1344. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off marker array %d.\n",n); // base address = partialscnt
  1345. return(MEMORY_ERROR);
  1346. }
  1347. // An array of original_vals of splice_counters, at steptimes for every partial and partial-transposition
  1348. if((dz->iparray[n+(partialscnt*2)] = (int *)malloc((*maxsteps) * sizeof(int)))==NULL) {
  1349. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off marker array %d.\n",n); // base address = partialscnt
  1350. return(MEMORY_ERROR);
  1351. }
  1352. // An array levels (or of left levels), at every steptime, for each partial and partial-transposition // base address = dz->temp_sampsize
  1353. if((dz->parray[m] = (double *)malloc((*maxsteps) * sizeof(double)))==NULL) {
  1354. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1355. return(MEMORY_ERROR);
  1356. }
  1357. // An array of right levels, at every steptime, for each partial and partial-transposition // base address = dz->temp_sampsize + partialscnt
  1358. if((dz->parray[m+partialscnt] = (double *)malloc((*maxsteps) * sizeof(double)))==NULL) {
  1359. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1360. return(MEMORY_ERROR);
  1361. }
  1362. }
  1363. // An array of steptimes
  1364. if((dz->parray[m+partialscnt] = (double *)malloc((*maxsteps) * sizeof(double)))==NULL) { // address = dz->temp_sampsize + (partialscnt * 2)
  1365. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1366. return(MEMORY_ERROR);
  1367. }
  1368. // An array of current frqs of partials
  1369. if((dz->parray[m+partialscnt+1] = (double *)malloc(partialscnt * sizeof(double)))==NULL) { // address = dz->temp_sampsize + (partialscnt * 2) + 1
  1370. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1371. return(MEMORY_ERROR);
  1372. }
  1373. // An array of current spatial positions
  1374. if((dz->parray[m+partialscnt+2] = (double *)malloc((*maxsteps) * sizeof(double)))==NULL) { // address = dz->temp_sampsize + (partialscnt * 2) + 2
  1375. sprintf(errstr,"INSUFFICIENT MEMORY for step positions.\n");
  1376. return(MEMORY_ERROR);
  1377. }
  1378. // An array of splice-counters for every partial
  1379. if((dz->iparray[partialscnt*3] = (int*)malloc(partialscnt * sizeof(int)))==NULL) { // address = (partialscnt * 3)
  1380. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1381. return(MEMORY_ERROR);
  1382. }
  1383. // An array of to remember the partials order, for 2nd run
  1384. if((dz->iparray[(partialscnt*3)+1] = (int*)malloc(partialscnt * sizeof(int)))==NULL) { // address = (partialscnt * 3)+1
  1385. sprintf(errstr,"INSUFFICIENT MEMORY for partial on-off gain and step times vals, array %d.\n",n);
  1386. return(MEMORY_ERROR);
  1387. }
  1388. for(n=0;n<partialscnt;n++) // Store original order
  1389. dz->iparray[(partialscnt*3)+1][n] = n;
  1390. // An array of to remember the switchpos
  1391. if((dz->iparray[(partialscnt*3)+2] = (int*)malloc((*maxsteps) * sizeof(int)))==NULL) { // address = (partialscnt * 3)+2
  1392. sprintf(errstr,"INSUFFICIENT MEMORY for switching between channels %d.\n",n);
  1393. return(MEMORY_ERROR);
  1394. }
  1395. // A permutation array for randomly permuting partials
  1396. if((*perm = (int *)malloc(partialscnt*sizeof(int)))==NULL) {
  1397. sprintf(errstr,"NO MEMORY FOR PARTIALS PERMUTATIONS\n");
  1398. return(DATA_ERROR);
  1399. }
  1400. if((*permon = (int *)malloc(partialscnt*sizeof(int)))==NULL) {
  1401. sprintf(errstr,"NO MEMORY FOR PARTIALS PERMUTATIONS\n");
  1402. return(DATA_ERROR);
  1403. }
  1404. if((*permoff = (int *)malloc(partialscnt*sizeof(int)))==NULL) {
  1405. sprintf(errstr,"NO MEMORY FOR PARTIALS PERMUTATIONS\n");
  1406. return(DATA_ERROR);
  1407. }
  1408. configno = chans;
  1409. configno += (chans * ((chans/2) - 1)) + chans/2;
  1410. configno += chans * 2;
  1411. configno += 3;
  1412. if((*superperm = (int *)malloc(configno*sizeof(int)))==NULL) {
  1413. sprintf(errstr,"NO MEMORY FOR PARTIALS PERMUTATIONS\n");
  1414. return(DATA_ERROR);
  1415. }
  1416. } else {
  1417. if((dz->parray[pntarray] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1418. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1419. return(MEMORY_ERROR);
  1420. }
  1421. }
  1422. for(n=0;n<dz->itemcnt;n++) // Zero sine-table pointers for all partials
  1423. dz->parray[pntarray][n] = 0.0;
  1424. if(dz->mode == 1) {
  1425. if((exit_status = generate_packet_envelope(dz))<0)
  1426. return(exit_status);
  1427. } else if(dz->mode == 2) {
  1428. if(dz->param[SYNTH_RISE] + dz->param[SYNTH_FALL] + dz->param[SYNTH_STDY] > dz->param[SYNTH_DUR]) {
  1429. sprintf(errstr,"Rise, Fall and Steady-state parameters not compatible with output duration.\n");
  1430. return(DATA_ERROR);
  1431. }
  1432. dz->param[SYNTH_STDY] = dz->param[SYNTH_DUR] - dz->param[SYNTH_STDY];
  1433. dz->param[SYNTH_FALL] = dz->param[SYNTH_STDY] - dz->param[SYNTH_FALL];
  1434. if(dz->param[SYNTH_ETIME] + dz->param[SYNTH_CTIME] >= dz->param[SYNTH_DUR]) {
  1435. sprintf(errstr,"Emerge and Converge times, combined, must be LESS than Output duration.\n");
  1436. return(DATA_ERROR);
  1437. }
  1438. if(dz->iparam[SYNTH_NUM] > partialscnt) {
  1439. sprintf(errstr,"Number of partials in play must be <= total number of partials and their transpositions (%d)\n",partialscnt);
  1440. return(DATA_ERROR);
  1441. }
  1442. if(dz->iparam[SYNTH_EFROM] > dz->iparam[SYNTH_CHANS] || dz->iparam[SYNTH_CTO] > dz->iparam[SYNTH_CHANS]) {
  1443. sprintf(errstr,"Channel to emerge from or converge to must be <= output channel count.\n");
  1444. return(DATA_ERROR);
  1445. }
  1446. if(dz->iparam[SYNTH_EFROM] > 0 && dz->param[SYNTH_ETIME] == 0.0) {
  1447. fprintf(stdout,"WARNING: Emergence time set to zero: Ignoring emergence channel.\n");
  1448. fflush(stdout);
  1449. dz->iparam[SYNTH_EFROM] = 0;
  1450. }
  1451. if(dz->iparam[SYNTH_EFROM] == 0 && dz->param[SYNTH_ETIME] > 0.0) {
  1452. fprintf(stdout,"WARNING: Emergence channel not set: Ignoring emergence duration.\n");
  1453. fflush(stdout);
  1454. dz->param[SYNTH_ETIME] = 0.0;
  1455. }
  1456. if(dz->iparam[SYNTH_CTO] > 0 && dz->param[SYNTH_CTIME] == 0.0) {
  1457. fprintf(stdout,"WARNING: Convergence time set to zero: Ignoring convergence channel.\n");
  1458. fflush(stdout);
  1459. dz->iparam[SYNTH_CTO] = 0;
  1460. }
  1461. if(dz->iparam[SYNTH_CTO] == 0 && dz->param[SYNTH_CTIME] > 0.0) {
  1462. fprintf(stdout,"WARNING: Convergence channel not set: Ignoring convergence duration.\n");
  1463. fflush(stdout);
  1464. dz->param[SYNTH_ETIME] = 0.0;
  1465. }
  1466. if(!dz->vflag[SYN_SPACED] && (dz->iparam[SYNTH_CTO] > 0 || dz->iparam[SYNTH_EFROM] > 0)) {
  1467. fprintf(stdout,"WARNING: Spatialisation flag not set: ignoring emerge/converge parameters.\n");
  1468. fflush(stdout);
  1469. dz->iparam[SYNTH_CTO] = 0;
  1470. dz->iparam[SYNTH_EFROM] = 0;
  1471. dz->param[SYNTH_ETIME] = 0.0;
  1472. dz->param[SYNTH_CTIME] = 0.0;
  1473. }
  1474. dz->param[SYNTH_CTIME] = dz->param[SYNTH_DUR] - dz->param[SYNTH_CTIME];
  1475. if(dz->vflag[SYN_JUMP] && !dz->vflag[SYN_SPACED]) {
  1476. fprintf(stdout,"WARNING: Spatialisation flag not set: ignoring Jump flag.\n");
  1477. fflush(stdout);
  1478. dz->vflag[SYN_JUMP] = 0;
  1479. }
  1480. if(dz->iparam[SYNTH_STYPE] < 0) {
  1481. if(chans != 8) {
  1482. sprintf(errstr,"Special Spatialisation types Only available for 8-channel output.\n");
  1483. return(DATA_ERROR);
  1484. }
  1485. if(dz->iparam[SYNTH_EFROM] || dz->iparam[SYNTH_CTO]) {
  1486. fprintf(stdout,"WARNING: Emergence/convergence not available with Special Spatialisation types.\n");
  1487. fflush(stdout);
  1488. dz->iparam[SYNTH_CTO] = 0;
  1489. dz->iparam[SYNTH_EFROM] = 0;
  1490. dz->param[SYNTH_ETIME] = 0.0;
  1491. dz->param[SYNTH_CTIME] = 0.0;
  1492. }
  1493. if(dz->vflag[SYN_JUMP]) {
  1494. sprintf(errstr,"Special Spatialisation types incompatible with Jump flag. Choose one or the other.\n");
  1495. return(DATA_ERROR);
  1496. }
  1497. if(dz->iparam[SYNTH_STYPE] == SB_ROTATE && dz->param[SYNTH_RSPEED] == 0.0) {
  1498. sprintf(errstr,"No rotation speed given for Special Spatialisation type %d, \"Rotation\".\n",SB_ROTATE);
  1499. return(DATA_ERROR);
  1500. }
  1501. if(dz->iparam[SYNTH_STYPE] != SB_ROTATE && dz->param[SYNTH_RSPEED] > 0.0) {
  1502. sprintf(errstr,"Special Spatialisation type %d, \"Rotation\" not set: Ignoring Rotation Speed.\n",SB_ROTATE);
  1503. fflush(stdout);
  1504. }
  1505. dz->vflag[SYN_SPACED] = 1;
  1506. }
  1507. }
  1508. return(FINISHED);
  1509. }
  1510. /******************************** SYNTHESIS ********************************
  1511. *
  1512. * MODE 2 arrays
  1513. * pcnt = partialcnt mpcnt = maxpartial cnt (partials + all transpositions) | | |
  1514. * | current
  1515. * | frqs|
  1516. * parray |----------|----------|-|-|-----------------|-----------------|-|-|-|
  1517. * | tvarying pno+plevel |s|s| left_level | right-level step|p|
  1518. * | (Mpcnt*2) |i|i| mpcnt | mpcnt timeso|
  1519. * | |n|n| | | | |s|
  1520. * address 0 mpcnt*2| |p|(mpcnt*2)+2 |(mpcnt*3)+2 | (mpcnt*4)+3
  1521. * | | |t| | (mpcnt*4)+2
  1522. * | | |r| | | | (mpcnt*4)+4
  1523. * lengths | linelen of srcdata | | | maxsteps | maxsteps | |m| |
  1524. * |s|t| |t|p|t|
  1525. * (slen = sintablen) |l|o| |o|c|o|
  1526. * (totl = estimate of no |e|t| |t|n|t|
  1527. * of timesteps used) |n|l| |l|t|l|
  1528. *
  1529. * iparray |-----------------|-----------------|-----------------|-| switchpos
  1530. * | on-off flags | leftmost chan | spo |s|porder
  1531. * | (mpcnt) | (mpcnt) | (mpcnt) |p| | (mpcnt*3)+2
  1532. * | | | |l| (mpcnt*3)+1
  1533. * address 0 mpcnt mpcnt*2 (mpcnt*3)
  1534. * | | | |c| |
  1535. * lengths | maxsteps | maxsteps | maxsteps |nmpcnt
  1536. * |t| mpcnt
  1537. * |r| |
  1538. * (splcntrs = splice counters) |s| |
  1539. * (spo = orig values of splice counters)
  1540. */
  1541. int synthesis(int *perm,int *permon,int *permoff,int *superperm, double minrate,int maxsteps,dataptr dz)
  1542. {
  1543. int exit_status, n, chans, rangxs = 0, max_partials_cnt, partials_in_play, rmost, k, terminate = 0, israngechange = 0;
  1544. int loindex, hiindex, packet_dur, kk, stepcnt = 0, totaloutsamps, base_sampcnt;
  1545. double loval, hival, valdiff, timefrac, val, valr = 0.0, vall = 0.0, level, maxval = 1.0, onehzincr, packet_incr, envv, pos = 0.0;
  1546. float *obuf = dz->sampbuf[0];
  1547. double srate = (double)dz->infile->srate, thisstep, xsrange_frac = 0.0, thisrangxs, rangetop, posstep = 0.0;
  1548. int partialcnt = dz->itemcnt, total_partialcnt = 0, sintable = partialcnt * 2, splen = 0, spacetyp = dz->iparam[SYNTH_STYPE];
  1549. double *sintab = dz->parray[sintable], *sinptr = dz->parray[sintable+1];
  1550. double **llev = NULL, **rlev = NULL;
  1551. double *steptimes = NULL, *pvals = NULL, *position = NULL;
  1552. int **onoff = NULL, **lmost = NULL, **origspl = NULL, *splcntr = NULL, *splordr = NULL, *swpos = NULL;
  1553. int inendsplice, instartsplice, total_samps_synthed = 0, jlmost = 0, switchpos = 0;
  1554. int configcnt = 0, configno = 0, l_most = 0, r_most = 0, special_onoff = 0, indownsplice = 0;
  1555. int sampcnt = 0, startspliceend = dz->rampbrksize, endsplicestart = dz->iparam[1] - dz->rampbrksize;
  1556. double time = 0.0, spliceincr, spliceval, localspliceval, normaliser, falldur = 0.0, enddur = 0.0, nexttime = -1.0, leftgain = 0.0, rightgain = 0.0;
  1557. onehzincr = (double)SYNTH_TABSIZE/srate;
  1558. spliceincr = 1.0/(double)dz->rampbrksize;
  1559. spliceval = 0.0;
  1560. instartsplice = 1;
  1561. inendsplice = 0;
  1562. totaloutsamps = dz->iparam[1];
  1563. if(dz->mode == 2) {
  1564. chans = dz->iparam[SYNTH_CHANS];
  1565. if(spacetyp > 0) {
  1566. switch(spacetyp) {
  1567. case(SB_SUPERSPACE4): // Square, diamond and All-at-once
  1568. configno = 3; // For 8 chan = 3 + (8*2) + [((8*(4-1))+4] + 8 = 3 + 16 + 28 + 8 = 55
  1569. // fall thro
  1570. case(SB_SUPERSPACE3): // all possible meaningful small and large triangles
  1571. configno += chans * 2;
  1572. // fall thro
  1573. case(SB_SUPERSPACE2): // all possible pairs
  1574. configno += (chans * ((chans/2) - 1)) + chans/2;
  1575. // fall thro
  1576. case(SB_SUPERSPACE): // all single chans
  1577. configno += chans;
  1578. break;
  1579. }
  1580. }
  1581. totaloutsamps *= chans;
  1582. endsplicestart = dz->iparam[1] - (int)floor(50 * MS_TO_SECS * srate); // Force long splice at end
  1583. endsplicestart *= chans;
  1584. startspliceend = (int)floor(50 * MS_TO_SECS * srate); // Force long splice at start
  1585. startspliceend *= chans;
  1586. instartsplice = 1;
  1587. inendsplice = 0;
  1588. splen = (int)round(dz->param[SYNTH_SPLEN] * MS_TO_SECS * srate);
  1589. total_partialcnt = partialcnt;
  1590. llev = dz->parray + (partialcnt * 2) + 2;
  1591. rlev = dz->parray + (partialcnt * 3) + 2;
  1592. steptimes = dz->parray[(partialcnt * 4) + 2];
  1593. pvals = dz->parray[(partialcnt * 4) + 3];
  1594. position = dz->parray[(partialcnt * 4) + 4];
  1595. onoff = dz->iparray;
  1596. lmost = dz->iparray + partialcnt;
  1597. origspl = dz->iparray + (partialcnt * 2);
  1598. splcntr = dz->iparray[partialcnt * 3];
  1599. splordr = dz->iparray[(partialcnt * 3) + 1];
  1600. swpos = dz->iparray[(partialcnt * 3) + 2];
  1601. falldur = dz->param[SYNTH_STDY] - dz->param[SYNTH_FALL];
  1602. enddur = dz->param[SYNTH_DUR] - dz->param[SYNTH_STDY];
  1603. rangxs = dz->iparam[SYNTH_MAX] - 1; // Max no partial-transpositions (apart from orig vals)
  1604. for(n=0;n < partialcnt;n++) {
  1605. onoff[n][0] = S_OFF;// all partials initially flagged off
  1606. lmost[n][0] = 0; // all leftmost-outchan initially set to left - SAFETY
  1607. origspl[n][0] = 0; // all original-settings of splice-counters to zero
  1608. splcntr[n] = 0; // all splicecounters initially set to zero - SAFETY
  1609. llev[n][0] = 0.0; // all partial gains initially set to zero - SAFETY
  1610. rlev[n][0] = 0.0;
  1611. }
  1612. nexttime = 0.0; // initialise "nexttime" to trigger 1st setting of partials
  1613. steptimes[0] = nexttime;// initial steptime set to zero
  1614. stepcnt = 0;
  1615. } else {
  1616. chans = 1;
  1617. }
  1618. fprintf(stdout,"INFO: First pass: assessing level.\n");
  1619. fflush(stdout);
  1620. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1621. while(total_samps_synthed < totaloutsamps) {
  1622. time = (double)(total_samps_synthed/chans)/srate;
  1623. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1624. return exit_status;
  1625. switch(dz->mode) {
  1626. case(0):
  1627. for(n=0;n<dz->itemcnt;n++) {
  1628. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment
  1629. hiindex = loindex + 1;
  1630. loval = sintab[loindex];
  1631. hival = sintab[hiindex];
  1632. valdiff = hival - loval;
  1633. timefrac = sinptr[n] - (double)loindex;
  1634. val = loval + (valdiff * timefrac);
  1635. level = read_level(n,time,dz); // Read corresponding level
  1636. val *= level;
  1637. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  1638. incr_sinptr(n,time,onehzincr,dz); // Track (modify if ness) the partial-incr value for this partial
  1639. }
  1640. if(instartsplice) {
  1641. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  1642. spliceval += spliceincr;
  1643. spliceval = min(spliceval,1.0);
  1644. } else if(inendsplice) {
  1645. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  1646. spliceval -= spliceincr;
  1647. spliceval = max(spliceval,0.0);
  1648. }
  1649. maxval = max(maxval,fabs(obuf[sampcnt]));
  1650. if(++sampcnt >= dz->buflen) {
  1651. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1652. sampcnt = 0;
  1653. }
  1654. total_samps_synthed++;
  1655. if(!inendsplice && (total_samps_synthed >= endsplicestart)) {
  1656. inendsplice = 1;
  1657. spliceval = 1.0;
  1658. }
  1659. if(instartsplice && (total_samps_synthed >= startspliceend))
  1660. instartsplice = 0;
  1661. break;
  1662. case(1):
  1663. if(dz->brksize[SYNTH_SQZ] || dz->brksize[SYNTH_CTR]) {
  1664. if(!(flteq(dz->param[SYNTH_SQZ],1.0)) || !(flteq(dz->param[SYNTH_CTR],0.0)))
  1665. modify_packet_envelope(dz);
  1666. }
  1667. packet_dur = (int)round((1.0/dz->param[SYNTH_FRQ]) * srate);
  1668. packet_incr = (double)TREMOLO_TABSIZE/(double)(packet_dur - 1); // Forces last read to be at end of packet envelope (zero)
  1669. for(n=0;n<dz->itemcnt;n++)
  1670. sinptr[n] = 0.0;
  1671. for(kk = 0; kk<packet_dur;kk++) {
  1672. for(n=0;n<dz->itemcnt;n++) {
  1673. if(!dz->vflag[0])
  1674. time = (double)(total_samps_synthed + n)/srate;
  1675. loindex = (int)floor(sinptr[n]);
  1676. hiindex = loindex + 1;
  1677. loval = sintab[loindex];
  1678. hival = sintab[hiindex];
  1679. valdiff = hival - loval;
  1680. timefrac = sinptr[n] - (double)loindex;
  1681. val = loval + (valdiff * timefrac);
  1682. level = read_level(n,time,dz);
  1683. val *= level;
  1684. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  1685. incr_sinptr(n,time,onehzincr,dz);
  1686. }
  1687. envv = read_packet_envelope(kk,packet_incr,dz);
  1688. obuf[sampcnt] = (float)(obuf[sampcnt] * envv);
  1689. maxval = max(maxval,fabs(obuf[sampcnt]));
  1690. if(++sampcnt >= dz->buflen) {
  1691. sampcnt = 0;
  1692. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1693. }
  1694. total_samps_synthed++;
  1695. }
  1696. break;
  1697. case(2):
  1698. if(time >= steptimes[stepcnt]) { // If we've reached the next partials-change time
  1699. if(sloom && ((stepcnt % 200) == 0)) {
  1700. fprintf(stdout,"INFO: at %.1lf secs\n",time);
  1701. fflush(stdout);
  1702. }
  1703. if(spacetyp > 0) {
  1704. if(configcnt == 0)
  1705. rndintperm(superperm,configno);
  1706. if(++configcnt >= configno)
  1707. configcnt = 0;
  1708. }
  1709. thisstep = (drand48() * 2.0) - 1.0; // -1 to 1
  1710. thisstep *= dz->param[SYNTH_RATE]/2.0; // -(1/2) rate to +(1/2) rate
  1711. thisstep += dz->param[SYNTH_RATE]; // (1/2) rate to 1+(1/2) rate
  1712. nexttime = time + thisstep;
  1713. stepcnt++;
  1714. if(spacetyp == SB_ROTATE)
  1715. posstep = thisstep * dz->param[SYNTH_RSPEED] * chans;
  1716. if(stepcnt >= maxsteps - 1) { // If we run out of memory (as steps have random length) despite safety margin
  1717. terminate = 1; // Force all partials to turn off, and terminate at end of fade
  1718. totaloutsamps = total_samps_synthed + (splen * chans);
  1719. }
  1720. steptimes[stepcnt] = nexttime;
  1721. // Find current value of all partials, + sort to ascending order
  1722. get_current_partial_vals(time,pvals,total_partialcnt,dz);
  1723. sort_partials_into_ascending_frq_order(total_partialcnt,pvals,sinptr,llev,rlev,onoff,lmost,origspl,splordr,dz);
  1724. // FIND THE RANGE OF PARTIALS WHICH CAN BE USED
  1725. if(terminate) { // TURN EVERYTHING OFF!!
  1726. for(n=0;n<total_partialcnt;n++) {
  1727. onoff[n][stepcnt] = S_OFF;
  1728. if(onoff[n][stepcnt-1] == S_ON) {
  1729. origspl[n][stepcnt] = splen; // Partial is switched off
  1730. splcntr[n] = splen; // Set up dnsplice, retaining previous level
  1731. llev[n][stepcnt] = llev[n][stepcnt-1];
  1732. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1733. if(dz->vflag[SYN_SPACED]) // Retain previous level(s)
  1734. rlev[n][stepcnt] = rlev[n][stepcnt-1];
  1735. else
  1736. rlev[n][stepcnt] = 0.0;
  1737. } else if(onoff[n][stepcnt-1] == S_OFF) {
  1738. origspl[n][stepcnt] = 0; // Partial already OFF
  1739. splcntr[n] = 0; // SAFETY
  1740. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1741. }
  1742. }
  1743. } else {
  1744. if(dz->iparam[SYNTH_MAX] > 1) { // Find which partials can be used at this time....
  1745. israngechange = 0;
  1746. if(time < dz->param[SYNTH_RISE]) { // If we're in the initial fade-up or final fade-down
  1747. xsrange_frac = (dz->param[SYNTH_RISE] - time)/dz->param[SYNTH_RISE]; // Frac Range 1-0
  1748. israngechange = 1;
  1749. } else if(time > dz->param[SYNTH_FALL] && time < dz->param[SYNTH_STDY]) { // Calc the active octave-range at this point
  1750. xsrange_frac = (time - dz->param[SYNTH_FALL])/falldur; // Frac Range 0-1
  1751. israngechange = 2;
  1752. } else if (time >= dz->param[SYNTH_STDY]) {
  1753. if(dz->vflag[SYN_TOROOT]) // If we're in final steady-state or final fade
  1754. xsrange_frac = (time - dz->param[SYNTH_STDY])/enddur; // Frac Range 0-1
  1755. else
  1756. xsrange_frac = 1.0; // Flat
  1757. israngechange = 3;
  1758. }
  1759. if(israngechange) {
  1760. xsrange_frac = 1.0 - xsrange_frac; // Frac Range 1-0
  1761. if((israngechange == 1) && dz->vflag[SYN_FROMROOT]) {
  1762. thisrangxs = dz->iparam[SYNTH_MAX] * xsrange_frac; // Expand from root to max-range
  1763. rangetop = thisrangxs * dz->scalefact;
  1764. } else if((israngechange == 3) && dz->vflag[SYN_TOROOT]) {
  1765. thisrangxs = xsrange_frac; // Contract from min-range to root
  1766. rangetop = thisrangxs * dz->scalefact;
  1767. } else {
  1768. thisrangxs = rangxs * xsrange_frac; // Contract xs-range
  1769. rangetop = (1.0 + thisrangxs) * dz->scalefact; // Find current top of range
  1770. } // Multiplying by original range of partials (scalefact)
  1771. for(n = 0;n<total_partialcnt;n++) {
  1772. if(pvals[n] >= rangetop) // Find max partial we can use
  1773. break;
  1774. }
  1775. max_partials_cnt = n+1; // Max range of partials-and-transpositions we might use
  1776. max_partials_cnt = min(max_partials_cnt,total_partialcnt); // FAILSAFE
  1777. } else // Otherwise, use maximum octave range
  1778. max_partials_cnt = total_partialcnt;
  1779. } else
  1780. max_partials_cnt = total_partialcnt;
  1781. // P-and-ts we'll actually use at this moment (random)
  1782. if(dz->iparam[SYNTH_NUM] > 0)
  1783. partials_in_play = min(dz->iparam[SYNTH_NUM],max_partials_cnt);
  1784. else
  1785. partials_in_play = (int)floor(drand48() * (double)max_partials_cnt) + 1;
  1786. // If Jump flag set, do spatialisation for ALL partials FIRST
  1787. special_onoff = 0;
  1788. if(dz->vflag[SYN_SPACED]) {
  1789. if(dz->vflag[SYN_JUMP]) {
  1790. if(dz->iparam[SYNTH_EFROM] && (time < dz->param[SYNTH_ETIME]))
  1791. pos = emergepos(dz->iparam[SYNTH_EFROM],chans,time,dz->param[SYNTH_ETIME]);
  1792. else if(dz->iparam[SYNTH_CTO] && (time > dz->param[SYNTH_CTIME]))
  1793. pos = convergepos(dz->iparam[SYNTH_CTO],chans,time,dz->param[SYNTH_CTIME],dz->param[SYNTH_DUR]);
  1794. else
  1795. pos = chans * drand48();
  1796. jlmost = (int)floor(pos);
  1797. pos -= (double)jlmost;
  1798. pos = (pos * 2.0) - 1.0;
  1799. pancalc(pos,&leftgain,&rightgain);
  1800. } else if(spacetyp > 0) {
  1801. spacebox(&pos,&switchpos,posstep,chans,spacetyp,configno,configcnt,superperm);
  1802. position[stepcnt] = pos;
  1803. swpos[stepcnt] = switchpos;
  1804. if((position[stepcnt] != position[stepcnt-1]) || (swpos[stepcnt] != swpos[stepcnt-1]))
  1805. special_onoff = 1; // Where partial changes position, will need to fade-out then refade-in
  1806. }
  1807. }
  1808. // Randomly-> CHOOSE PARTIALS ON or OFF, ESTABLISH RELATIVE LEVEL, SET SPATIAL POSITION (if flagged)
  1809. if(partials_in_play == max_partials_cnt) { // If partials fill available range
  1810. for(n=0;n<partials_in_play;n++) { // All partials in range are on
  1811. onoff[n][stepcnt] = S_ON;
  1812. if(onoff[n][stepcnt-1] == S_OFF) { // If previously off
  1813. origspl[n][stepcnt] = splen; // Mark as fade-up
  1814. splcntr[n] = splen; // Set splice-counter to count back down to zero
  1815. llev[n][stepcnt] = (drand48() * 0.5) + 0.5; // Set new (rand)level [llev stands in for mono level]
  1816. if(dz->vflag[SYN_SPACED] && (spacetyp == 0)) { // If SPATIALISED
  1817. if(dz->vflag[SYN_JUMP]) { // If Jump flag in use, leftmost chan and levels already set
  1818. lmost[n][stepcnt] = jlmost;
  1819. rlev[n][stepcnt] = llev[n][stepcnt] * rightgain;
  1820. llev[n][stepcnt] *= leftgain;
  1821. } else { // Else create position for each individual partial
  1822. if(dz->iparam[SYNTH_EFROM] && (time < dz->param[SYNTH_ETIME]))
  1823. pos = emergepos(dz->iparam[SYNTH_EFROM],chans,time,dz->param[SYNTH_ETIME]);
  1824. else if(dz->iparam[SYNTH_CTO] && (time > dz->param[SYNTH_CTIME]))
  1825. pos = convergepos(dz->iparam[SYNTH_CTO],chans,time,dz->param[SYNTH_CTIME],dz->param[SYNTH_DUR]);
  1826. else
  1827. pos = chans * drand48();// Create spatial position at random (range 0 - chans)
  1828. lmost[n][stepcnt] = (int)floor(pos); // Find leftmost lspkr
  1829. pos -= (double)lmost[n][stepcnt]; // Range 0-1
  1830. pos = (pos * 2.0) - 1.0; // Range (-1 to 1)
  1831. pancalc(pos,&leftgain,&rightgain); // Calc relative levels of left and right signals
  1832. rlev[n][stepcnt] = llev[n][stepcnt] * rightgain;
  1833. llev[n][stepcnt] *= leftgain; // Readjust output levels
  1834. }
  1835. } else {
  1836. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1837. rlev[n][stepcnt] = 0.0;
  1838. }
  1839. } else { // Else, already on
  1840. llev[n][stepcnt] = llev[n][stepcnt-1];// Retain previous level(s)
  1841. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1842. if(dz->vflag[SYN_SPACED])
  1843. rlev[n][stepcnt] = rlev[n][stepcnt-1];
  1844. else
  1845. rlev[n][stepcnt] = 0.0;
  1846. if(special_onoff) {
  1847. splcntr[n] = splen * 2;
  1848. origspl[n][stepcnt] = splen * 2;
  1849. } else {
  1850. origspl[n][stepcnt] = 0;
  1851. splcntr[n] = 0; // SAFETY
  1852. }
  1853. }
  1854. }
  1855. while(n < total_partialcnt) { // For all remaining (unused) partials
  1856. onoff[n][stepcnt] = S_OFF;
  1857. if(onoff[n][stepcnt-1] == S_ON) { // If partial was on
  1858. origspl[n][stepcnt] = splen; // Mark it as fading out
  1859. splcntr[n] = splen; // Set splice-counter to count down to zero
  1860. llev[n][stepcnt] = llev[n][stepcnt-1];
  1861. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1862. if(dz->vflag[SYN_SPACED]) // retaining previous level(s)
  1863. rlev[n][stepcnt] = rlev[n][stepcnt-1];
  1864. else
  1865. rlev[n][stepcnt] = 0.0;
  1866. } else { // Else it was previously off
  1867. origspl[n][stepcnt] = 0;
  1868. splcntr[n] = 0; // SAFETY
  1869. origspl[n][stepcnt] = 0;
  1870. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1871. }
  1872. n++;
  1873. }
  1874. } else {
  1875. rndintperm(perm,max_partials_cnt); // Randomly permute all possible partials
  1876. if(dz->vflag[SYN_X]) // If Exclusuve, Force currently OFF-partials to top of list
  1877. xclusive(perm,permon,permoff,max_partials_cnt,partials_in_play,onoff,stepcnt);
  1878. for(n=0;n<partials_in_play;n++) // Switch first p_in_p partials in perm, ON
  1879. onoff[perm[n]][stepcnt] = S_ON;
  1880. while(n < max_partials_cnt) { // and switch remainder of those in range off
  1881. onoff[perm[n]][stepcnt] = S_OFF;
  1882. n++;
  1883. }
  1884. while(n < total_partialcnt) { // and switch remainder off
  1885. onoff[n][stepcnt] = S_OFF;
  1886. n++;
  1887. }
  1888. // ALGO ASSUMES THAT, BY THE TIME WE REACH NEXT STEP, splice has ended
  1889. for(n=0;n<total_partialcnt;n++) { // Switch first p_in_p partials in perm, ON
  1890. if(onoff[n][stepcnt] == S_ON) {
  1891. if(onoff[n][stepcnt-1] == S_ON) { // Partial remains on
  1892. llev[n][stepcnt] = llev[n][stepcnt-1];
  1893. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1894. if(dz->vflag[SYN_SPACED]) // Retain previous level(s)
  1895. rlev[n][stepcnt] = rlev[n][stepcnt-1];
  1896. else
  1897. rlev[n][stepcnt] = 0.0;
  1898. if(special_onoff) {
  1899. origspl[n][stepcnt] = splen * 2;
  1900. splcntr[n] = splen * 2;
  1901. } else {
  1902. origspl[n][stepcnt] = 0;
  1903. splcntr[n] = 0; // SAFETY
  1904. }
  1905. } else if(onoff[n][stepcnt-1] == S_OFF) {
  1906. origspl[n][stepcnt] = splen;
  1907. splcntr[n] = splen; // Partial is switched on
  1908. llev[n][stepcnt] = (drand48() * 0.5) + 0.5; // Set new (rand)level
  1909. if(dz->vflag[SYN_SPACED] && (spacetyp == 0)) { // If SPATIALISED...etc
  1910. if(dz->vflag[SYN_JUMP]) { // If Jump flag in use, leftmost chan and levels already set
  1911. lmost[n][stepcnt] = jlmost;
  1912. rlev[n][stepcnt] = llev[n][stepcnt] * rightgain;
  1913. llev[n][stepcnt] *= leftgain;
  1914. } else { // Else create position for each individual partial
  1915. if(dz->iparam[SYNTH_EFROM] && (time < dz->param[SYNTH_ETIME]))
  1916. pos = emergepos(dz->iparam[SYNTH_EFROM],chans,time,dz->param[SYNTH_ETIME]);
  1917. else if(dz->iparam[SYNTH_CTO] && (time > dz->param[SYNTH_CTIME]))
  1918. pos = convergepos(dz->iparam[SYNTH_CTO],chans,time,dz->param[SYNTH_CTIME],dz->param[SYNTH_DUR]);
  1919. else
  1920. pos = chans * drand48();
  1921. lmost[n][stepcnt] = (int)floor(pos);
  1922. pos -= (double)lmost[n][stepcnt];
  1923. pos = (pos * 2.0) - 1.0;
  1924. pancalc(pos,&leftgain,&rightgain);
  1925. rlev[n][stepcnt] = llev[n][stepcnt] * rightgain;
  1926. llev[n][stepcnt] *= leftgain;
  1927. }
  1928. } else {
  1929. rlev[n][stepcnt] = 0.0;
  1930. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1931. }
  1932. }
  1933. } else { // Marked as OFF
  1934. if(onoff[n][stepcnt-1] == S_ON) {
  1935. origspl[n][stepcnt] = splen; // Partial is switched off
  1936. splcntr[n] = splen; // Set up dnsplice, retaining previous level
  1937. llev[n][stepcnt] = llev[n][stepcnt-1];
  1938. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1939. if(dz->vflag[SYN_SPACED]) // Retain previous level(s)
  1940. rlev[n][stepcnt] = rlev[n][stepcnt-1];
  1941. else
  1942. rlev[n][stepcnt] = 0.0;
  1943. } else if(onoff[n][stepcnt-1] == S_OFF) {
  1944. origspl[n][stepcnt] = 0; // Partial already OFF
  1945. splcntr[n] = 0; // SAFETY
  1946. lmost[n][stepcnt] = lmost[n][stepcnt-1];
  1947. }
  1948. }
  1949. }
  1950. }
  1951. }
  1952. }
  1953. // USING THE ON/OFF, RELATIVE LEVEL, SPLICING, AND SPATIALISATION INFO, WRITE VARIOUS PARTIALS
  1954. base_sampcnt = sampcnt;
  1955. for(n=0;n<total_partialcnt;n++) {
  1956. sampcnt = base_sampcnt;
  1957. if(onoff[n][stepcnt]) { // If partial is NOW on
  1958. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment
  1959. hiindex = loindex + 1;
  1960. loval = sintab[loindex];
  1961. hival = sintab[hiindex];
  1962. valdiff = hival - loval;
  1963. timefrac = sinptr[n] - (double)loindex;
  1964. val = loval + (valdiff * timefrac);
  1965. level = read_level(n,time,dz); // Read corresponding level
  1966. indownsplice = 0;
  1967. if(splcntr[n] > 0) { // Get any splice contribution
  1968. if(splcntr[n] > splen) { // This indicates an OFF/ON splice
  1969. localspliceval = (double)(splcntr[n] - splen)/(double)splen;
  1970. indownsplice = 1; // Down-splice
  1971. } else {
  1972. indownsplice = 0; // Up-splice
  1973. localspliceval = (double)(splen - splcntr[n])/(double)splen;
  1974. }
  1975. val *= localspliceval; // Upfade, splcntr falling, splen-splcntr rising
  1976. splcntr[n]--; // Advance splicecnt towards zero
  1977. }
  1978. if(dz->vflag[SYN_SPACED]) { // If spatialisation, get spatial contributions
  1979. if(spacetyp > 0) {
  1980. if(indownsplice) {
  1981. pos = position[stepcnt-1];
  1982. switchpos = swpos[stepcnt-1];
  1983. spacebox_apply(pos,llev[n][stepcnt-1],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  1984. } else {
  1985. pos = position[stepcnt];
  1986. switchpos = swpos[stepcnt];
  1987. spacebox_apply(pos,llev[n][stepcnt],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  1988. }
  1989. valr = val * valr;
  1990. val = val * vall;
  1991. } else { // If spatialisation, get spatial contributions
  1992. valr = val * rlev[n][stepcnt];
  1993. val *= llev[n][stepcnt];
  1994. }
  1995. } else
  1996. val *= llev[n][stepcnt]; // Or just incorporate calculated atten for this element
  1997. if(dz->vflag[SYN_SPACED]) { // if spatialised, find rightmost channel from leftmost
  1998. if(spacetyp > 0) {
  1999. output_special_spatialisation_sample(obuf,sampcnt,switchpos,chans,val,valr,l_most,r_most,spacetyp);
  2000. sampcnt += chans;
  2001. } else {
  2002. rmost = (lmost[n][stepcnt] + 1) % chans;
  2003. for(k = 0;k< chans;k++) {
  2004. if(k == lmost[n][stepcnt]) // Add output only to the 2 relevant channels
  2005. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2006. else if(k == rmost)
  2007. obuf[sampcnt] = (float)(obuf[sampcnt] + valr);
  2008. sampcnt++;
  2009. }
  2010. }
  2011. } else { // If NOT spatialised, add output to all outchans
  2012. for(k = 0;k < chans;k++) {
  2013. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2014. sampcnt++;
  2015. }
  2016. }
  2017. } else { // Partial is OFF
  2018. if(splcntr[n] > 0) { // BUT IF its still a fade-out, Get any splice contribution
  2019. loindex = (int)floor(sinptr[n]);
  2020. hiindex = loindex + 1;
  2021. loval = sintab[loindex];
  2022. hival = sintab[hiindex];
  2023. valdiff = hival - loval;
  2024. timefrac = sinptr[n] - (double)loindex;
  2025. val = loval + (valdiff * timefrac);
  2026. level = read_level(n,time,dz);
  2027. localspliceval = (double)splcntr[n]/(double)splen; // Downfade, splcntr falling
  2028. val *= localspliceval;
  2029. splcntr[n]--; // Advance splicecnt towards zero
  2030. if(dz->vflag[SYN_SPACED]) {
  2031. if(spacetyp > 0) {
  2032. pos = position[stepcnt-1];
  2033. switchpos = swpos[stepcnt-1];
  2034. spacebox_apply(pos,llev[n][stepcnt-1],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  2035. valr = val * valr;
  2036. val = val * vall;
  2037. } else {
  2038. valr = val * rlev[n][stepcnt];
  2039. val *= llev[n][stepcnt];
  2040. }
  2041. } else
  2042. val *= llev[n][stepcnt];
  2043. if(dz->vflag[SYN_SPACED]) {
  2044. if(spacetyp > 0) {
  2045. output_special_spatialisation_sample(obuf,sampcnt,switchpos,chans,val,valr,l_most,r_most,spacetyp);
  2046. sampcnt += chans;
  2047. } else {
  2048. rmost = (lmost[n][stepcnt] + 1) % chans;
  2049. for(k = 0;k < chans;k++) {
  2050. if(k == lmost[n][stepcnt])
  2051. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2052. else if(k == rmost)
  2053. obuf[sampcnt] = (float)(obuf[sampcnt] + valr);
  2054. sampcnt++;
  2055. }
  2056. }
  2057. } else {
  2058. for(k = 0;k < chans;k++) {
  2059. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2060. sampcnt++;
  2061. }
  2062. }
  2063. }
  2064. }
  2065. incr_sinptr(n,time,onehzincr,dz);
  2066. }
  2067. if(instartsplice) { // Do big splice at start of output
  2068. sampcnt = base_sampcnt;
  2069. for(k = 0;k < chans;k++) {
  2070. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2071. sampcnt++;
  2072. }
  2073. spliceval += spliceincr;
  2074. spliceval = min(spliceval,1.0);
  2075. } else if(inendsplice) { // Do big splice at end of output
  2076. sampcnt = base_sampcnt;
  2077. for(k = 0;k < chans;k++) {
  2078. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2079. sampcnt++;
  2080. }
  2081. spliceval -= spliceincr;
  2082. spliceval = max(spliceval,0.0);
  2083. }
  2084. sampcnt = base_sampcnt; // Find maxval over all channels
  2085. for(k = 0;k< chans;k++) {
  2086. maxval = max(maxval,fabs(obuf[sampcnt]));
  2087. sampcnt++;
  2088. }
  2089. if(sampcnt >= dz->buflen) { // Check if buffer full - refresh
  2090. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2091. sampcnt = 0;
  2092. }
  2093. total_samps_synthed += chans; // Find out if (still) in startsplice or endsplice
  2094. if(!inendsplice && (total_samps_synthed >= endsplicestart)) {
  2095. inendsplice = 1;
  2096. spliceval = 1.0;
  2097. }
  2098. if(instartsplice && (total_samps_synthed >= startspliceend))
  2099. instartsplice = 0;
  2100. break;
  2101. }
  2102. }
  2103. if(sloom) {
  2104. fprintf(stdout,"INFO: at %.1lf secs\n",time);
  2105. fflush(stdout);
  2106. }
  2107. normaliser = 0.85/maxval;
  2108. time = 0.0;
  2109. spliceval = 0.0;
  2110. instartsplice = 1;
  2111. inendsplice = 0;
  2112. total_samps_synthed = 0;
  2113. sampcnt = 0;
  2114. for(n=0;n<dz->itemcnt;n++) // Zero sine-table pointers for all partials
  2115. sinptr[n] = 0.0;
  2116. if(dz->mode == 2) {
  2117. for(n=0;n < partialcnt;n++) {
  2118. onoff[n][0] = S_OFF;// all partials initially flagged off
  2119. lmost[n][0] = 0; // all leftmost-outchan initially set to left - SAFETY
  2120. origspl[n][0] = 0; // all original-settings of splice-counters to zero
  2121. splcntr[n] = 0; // all splicecounters initially set to zero - SAFETY
  2122. llev[n][0] = 0.0; // all partial gains initially set to zero - SAFETY
  2123. rlev[n][0] = 0.0;
  2124. }
  2125. stepcnt = 0;
  2126. terminate = 0;
  2127. resort_partials_into_original_frq_order(total_partialcnt,pvals,sinptr,llev,rlev,onoff,lmost,origspl,splordr,dz);
  2128. }
  2129. fprintf(stdout,"INFO: Second pass: synthesis.\n");
  2130. fflush(stdout);
  2131. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2132. while(total_samps_synthed < totaloutsamps) {
  2133. time = (double)(total_samps_synthed/chans)/srate;
  2134. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  2135. return exit_status;
  2136. switch(dz->mode) {
  2137. case(0):
  2138. for(n=0;n<dz->itemcnt;n++) {
  2139. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment
  2140. hiindex = loindex + 1;
  2141. loval = sintab[loindex];
  2142. hival = sintab[hiindex];
  2143. valdiff = hival - loval;
  2144. timefrac = sinptr[n] - (double)loindex;
  2145. val = loval + (valdiff * timefrac);
  2146. level = read_level(n,time,dz); // Read corresponding level
  2147. val *= level;
  2148. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2149. incr_sinptr(n,time,onehzincr,dz); // Track (modify if ness) the partial-incr value for this partial
  2150. }
  2151. obuf[sampcnt] = (float)(obuf[sampcnt] * normaliser);
  2152. if(instartsplice) {
  2153. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2154. spliceval += spliceincr;
  2155. spliceval = min(spliceval,1.0);
  2156. } else if(inendsplice) {
  2157. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2158. spliceval -= spliceincr;
  2159. spliceval = max(spliceval,0.0);
  2160. }
  2161. total_samps_synthed++;
  2162. if(!inendsplice && (total_samps_synthed >= endsplicestart)) {
  2163. inendsplice = 1;
  2164. spliceval = 1.0;
  2165. }
  2166. if(instartsplice && (total_samps_synthed >= startspliceend))
  2167. instartsplice = 0;
  2168. if(++sampcnt >= dz->buflen) {
  2169. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  2170. return(exit_status);
  2171. sampcnt = 0;
  2172. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2173. }
  2174. break;
  2175. case(1): // At start of each packet, set up packet shape, size and increment
  2176. if(!(flteq(dz->param[SYNTH_SQZ],1.0)) || !(flteq(dz->param[SYNTH_CTR],0.0)))
  2177. modify_packet_envelope(dz); // Packet duration determined by fundamental frq
  2178. packet_dur = (int)round((1.0/dz->param[SYNTH_FRQ]) * srate);
  2179. packet_incr = (double)TREMOLO_TABSIZE/(double)(packet_dur - 1);
  2180. for(n=0;n<dz->itemcnt;n++) // Zero sine-table pointers for all partials, at start of packet
  2181. sinptr[n] = 0.0;
  2182. for(kk = 0; kk<packet_dur;kk++) {
  2183. for(n=0;n<dz->itemcnt;n++) { // If not holding partial values steady WITHIN packets,
  2184. if(!dz->vflag[0]) // use absolute time to update partial frqs and levels.
  2185. time = (double)(total_samps_synthed + n)/srate; // Otherwise use packet_start-time,
  2186. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment
  2187. hiindex = loindex + 1;
  2188. loval = sintab[loindex];
  2189. hival = sintab[hiindex];
  2190. valdiff = hival - loval;
  2191. timefrac = sinptr[n] - (double)loindex;
  2192. val = loval + (valdiff * timefrac);
  2193. level = read_level(n,time,dz); // Read corresponding level
  2194. val *= level;
  2195. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2196. incr_sinptr(n,time,onehzincr,dz); // Track (modify if ness) the partial-incr value for this partial
  2197. }
  2198. // Once all partial-samples added, impose packet envelope
  2199. envv = read_packet_envelope(kk,packet_incr,dz);
  2200. obuf[sampcnt] = (float)(obuf[sampcnt] * envv * normaliser);
  2201. total_samps_synthed++;
  2202. if(++sampcnt >= dz->buflen) {
  2203. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  2204. return(exit_status);
  2205. sampcnt = 0;
  2206. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2207. }
  2208. }
  2209. break;
  2210. case(2):
  2211. if(sloom) // Forces correct read-out of time-bar
  2212. dz->insams[0] = dz->iparam[SYNTH_DUR] * chans;
  2213. if(time >= steptimes[stepcnt]) { // If we've reached the next partials-change time
  2214. stepcnt++; // Advance to next vals
  2215. get_current_partial_vals(time,pvals,total_partialcnt,dz);
  2216. sort_partials_into_ascending_frq_order(total_partialcnt,pvals,sinptr,llev,rlev,onoff,lmost,origspl,splordr,dz);
  2217. for(n=0;n<total_partialcnt;n++) // Set any splice counters needed
  2218. splcntr[n] = origspl[n][stepcnt];
  2219. }
  2220. base_sampcnt = sampcnt;
  2221. for(n=0;n<total_partialcnt;n++) {
  2222. sampcnt = base_sampcnt;
  2223. if(onoff[n][stepcnt]) { // If partial is on
  2224. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment
  2225. hiindex = loindex + 1;
  2226. loval = sintab[loindex];
  2227. hival = sintab[hiindex];
  2228. valdiff = hival - loval;
  2229. timefrac = sinptr[n] - (double)loindex;
  2230. val = loval + (valdiff * timefrac);
  2231. level = read_level(n,time,dz); // Read corresponding level
  2232. indownsplice = 0;
  2233. if(splcntr[n] > 0) { // Get any splice contribution
  2234. if(splcntr[n] > splen) { // This indicates an OFF/ON splice
  2235. localspliceval = (double)(splcntr[n] - splen)/(double)splen;
  2236. indownsplice = 1; // Down-splice
  2237. } else {
  2238. localspliceval = (double)(splen - splcntr[n])/(double)splen;
  2239. indownsplice = 0; // Up-splice
  2240. }
  2241. val *= localspliceval; // Upfade, splcntr falling, splen-splcntr rising
  2242. splcntr[n]--; // Advance splicecnt towards zero
  2243. }
  2244. if(dz->vflag[SYN_SPACED]) { // If spatialisation, get spatial contributions
  2245. if(spacetyp > 0) {
  2246. if(indownsplice) {
  2247. pos = position[stepcnt-1];
  2248. switchpos = swpos[stepcnt-1];
  2249. spacebox_apply(pos,llev[n][stepcnt-1],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  2250. } else {
  2251. pos = position[stepcnt];
  2252. switchpos = swpos[stepcnt];
  2253. spacebox_apply(pos,llev[n][stepcnt],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  2254. }
  2255. valr = val * valr;
  2256. val = val * vall;
  2257. } else { // If spatialisation, get spatial contributions
  2258. valr = val * rlev[n][stepcnt];
  2259. val = val * llev[n][stepcnt];
  2260. }
  2261. } else
  2262. val = val * llev[n][stepcnt]; // Or just incorporate calculated atten for this element
  2263. if(dz->vflag[SYN_SPACED]) { // if spatialised, find rightmost channel from leftmost
  2264. if(spacetyp > 0) {
  2265. output_special_spatialisation_sample(obuf,sampcnt,switchpos,chans,val,valr,l_most,r_most,spacetyp);
  2266. sampcnt += chans;
  2267. } else {
  2268. rmost = (lmost[n][stepcnt] + 1) % chans;
  2269. for(k = 0;k< chans;k++) {
  2270. if(k == lmost[n][stepcnt]) // Add output only to the 2 relevant channels
  2271. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2272. else if(k == rmost)
  2273. obuf[sampcnt] = (float)(obuf[sampcnt] + valr);
  2274. sampcnt++;
  2275. }
  2276. }
  2277. } else { // If NOT spatialised, add output to all outchans
  2278. for(k = 0;k < chans;k++) {
  2279. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2280. sampcnt++;
  2281. }
  2282. }
  2283. } else { // Partial is OFF
  2284. if(splcntr[n] > 0) { // BUT IF its still a fade-out, Get any splice contribution
  2285. loindex = (int)floor(sinptr[n]);
  2286. hiindex = loindex + 1;
  2287. loval = sintab[loindex];
  2288. hival = sintab[hiindex];
  2289. valdiff = hival - loval;
  2290. timefrac = sinptr[n] - (double)loindex;
  2291. val = loval + (valdiff * timefrac);
  2292. level = read_level(n,time,dz);
  2293. localspliceval = (double)splcntr[n]/(double)splen; // Downfade, splcntr falling
  2294. val *= localspliceval;
  2295. splcntr[n]--; // Advance splicecnt towards zero
  2296. if(dz->vflag[SYN_SPACED]) { // If spatialisation, get spatial contributions FROM PREVIOUS STEP
  2297. if(spacetyp > 0) {
  2298. pos = position[stepcnt-1];
  2299. switchpos = swpos[stepcnt-1];
  2300. spacebox_apply(pos,llev[n][stepcnt-1],chans,&l_most,&r_most,&valr,&vall,spacetyp);
  2301. valr = val * valr;
  2302. val = val * vall;
  2303. } else { // If spatialisation, get spatial contributions
  2304. valr = val * rlev[n][stepcnt-1];
  2305. val = val * llev[n][stepcnt-1];
  2306. }
  2307. } else
  2308. val = val * llev[n][stepcnt-1];
  2309. if(dz->vflag[SYN_SPACED]) { // As fadeout of last sound, keep PREVIOUS STEP spatial coords
  2310. if(spacetyp > 0) {
  2311. output_special_spatialisation_sample(obuf,sampcnt,switchpos,chans,val,valr,l_most,r_most,spacetyp);
  2312. sampcnt += chans;
  2313. } else {
  2314. rmost = (lmost[n][stepcnt-1] + 1) % chans;
  2315. for(k = 0;k < chans;k++) {
  2316. if(k == lmost[n][stepcnt-1])
  2317. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2318. else if(k == rmost)
  2319. obuf[sampcnt] = (float)(obuf[sampcnt] + valr);
  2320. sampcnt++;
  2321. }
  2322. }
  2323. } else {
  2324. for(k = 0;k < chans;k++) {
  2325. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  2326. sampcnt++;
  2327. }
  2328. }
  2329. }
  2330. }
  2331. incr_sinptr(n,time,onehzincr,dz); // Track (modify if ness) the partial-incr value for this partial
  2332. }
  2333. if(instartsplice) { // Do big splice at start of output
  2334. sampcnt = base_sampcnt;
  2335. for(k = 0;k < chans;k++) {
  2336. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2337. sampcnt++;
  2338. }
  2339. spliceval += spliceincr;
  2340. spliceval = min(spliceval,1.0);
  2341. } else if(inendsplice) { // Do big splice at end of output
  2342. sampcnt = base_sampcnt;
  2343. for(k = 0;k < chans;k++) {
  2344. obuf[sampcnt] = (float)(obuf[sampcnt] * spliceval);
  2345. sampcnt++;
  2346. }
  2347. spliceval -= spliceincr;
  2348. spliceval = max(spliceval,0.0);
  2349. }
  2350. sampcnt = base_sampcnt; // Normalise output
  2351. for(k = 0;k < chans;k++) {
  2352. obuf[sampcnt] = (float)(obuf[sampcnt] * normaliser);
  2353. sampcnt++;
  2354. }
  2355. if(sampcnt >= dz->buflen) { // Check if buffer full - write_samps and refresh
  2356. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  2357. return(exit_status);
  2358. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2359. sampcnt = 0;
  2360. }
  2361. total_samps_synthed += chans; // Find out if (still) in startsplice or endsplice
  2362. if(!inendsplice && (total_samps_synthed >= endsplicestart)) {
  2363. inendsplice = 1;
  2364. spliceval = 1.0;
  2365. }
  2366. if(instartsplice && (total_samps_synthed >= startspliceend))
  2367. instartsplice = 0;
  2368. break;
  2369. }
  2370. }
  2371. if(sampcnt) {
  2372. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  2373. return(exit_status);
  2374. }
  2375. return FINISHED;
  2376. }
  2377. /**************************** INCR_SINPTR ****************************/
  2378. void incr_sinptr(int n,double time,double onehzincr,dataptr dz)
  2379. {
  2380. int m;
  2381. double hival, loval, hitime, lotime, timediff, timefrac, valdiff, partialval, thisincr;
  2382. double *sinptr = dz->parray[(dz->itemcnt * 2) + 1];
  2383. double *thispartial = dz->parray[n];
  2384. m = 0;
  2385. while(thispartial[m] < time) {
  2386. m += 2;
  2387. if(m >= dz->ringsize)
  2388. break;
  2389. }
  2390. if(m==0)
  2391. partialval = thispartial[1];
  2392. else if(m < dz->ringsize) {
  2393. hival = thispartial[m+1];
  2394. loval = thispartial[m-1];
  2395. hitime = thispartial[m];
  2396. lotime = thispartial[m-2];
  2397. timediff = hitime - lotime;
  2398. timefrac = (time - lotime)/timediff;
  2399. valdiff = hival - loval;
  2400. partialval = loval + (valdiff * timefrac);
  2401. } else
  2402. partialval = thispartial[dz->ringsize-1];
  2403. // Convert partial numbers to table-increments
  2404. thisincr = partialval * onehzincr;
  2405. thisincr *= dz->param[SYNTH_FRQ];
  2406. sinptr[n] += thisincr;
  2407. if(sinptr[n] >= SYNTH_TABSIZE)
  2408. sinptr[n] -= (double)SYNTH_TABSIZE;
  2409. }
  2410. /**************************** GET_CURRENT_PARTIAL_VALS ****************************/
  2411. void get_current_partial_vals(double time,double *pvals,int partialcnt,dataptr dz)
  2412. {
  2413. int m, n;
  2414. double hival, loval, hitime, lotime, timediff, timefrac, valdiff, partialval;
  2415. double *thispartial;
  2416. for(n = 0;n < partialcnt;n++ ) {
  2417. thispartial = dz->parray[n];
  2418. m = 0;
  2419. while(thispartial[m] < time) {
  2420. m += 2;
  2421. if(m >= dz->ringsize)
  2422. break;
  2423. }
  2424. if(m==0)
  2425. partialval = thispartial[1];
  2426. else if(m < dz->ringsize) {
  2427. hival = thispartial[m+1];
  2428. loval = thispartial[m-1];
  2429. hitime = thispartial[m];
  2430. lotime = thispartial[m-2];
  2431. timediff = hitime - lotime;
  2432. timefrac = (time - lotime)/timediff;
  2433. valdiff = hival - loval;
  2434. partialval = loval + (valdiff * timefrac);
  2435. } else
  2436. partialval = thispartial[dz->ringsize-1];
  2437. pvals[n] = partialval;
  2438. }
  2439. }
  2440. /**************************** READ_LEVEL ****************************/
  2441. double read_level(int n,double time,dataptr dz)
  2442. {
  2443. int m;
  2444. double hival, loval, hitime, lotime, timediff, timefrac, valdiff, level;
  2445. double *thislevel = dz->parray[n + dz->itemcnt];
  2446. m = 0;
  2447. while(thislevel[m] < time) {
  2448. m += 2;
  2449. if(m >= dz->ringsize)
  2450. break;
  2451. }
  2452. if(m==0) {
  2453. level = thislevel[1];
  2454. } else if(m < dz->ringsize) {
  2455. hival = thislevel[m+1];
  2456. loval = thislevel[m-1];
  2457. hitime = thislevel[m];
  2458. lotime = thislevel[m-2];
  2459. timediff = hitime - lotime;
  2460. timefrac = (time - lotime)/timediff;
  2461. valdiff = hival - loval;
  2462. level = loval + (valdiff * timefrac);
  2463. } else {
  2464. level = thislevel[dz->ringsize-1];
  2465. }
  2466. return level;
  2467. }
  2468. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  2469. int handle_the_special_data(char *str,dataptr dz)
  2470. {
  2471. double dummy = 0.0, lasttime = 0.0, lastpartial = 1.0, maxval = 0.0, normaliser;
  2472. int entrycnt = 0, partialcnt, n, timepos, valpos, pno_cnt, lev_cnt = 0;
  2473. int zz, nupno_cnt = 0, nulev_cnt,lstart, m, k, nn, mm;
  2474. double *sortptr;
  2475. int totalpartials = 0, tablecnt, lno_cnt;
  2476. FILE *fp;
  2477. int cnt, linecnt;
  2478. char temp[8000], *p;
  2479. if((fp = fopen(str,"r"))==NULL) {
  2480. sprintf(errstr,"Cannot open file %s to read times.\n",str);
  2481. return(DATA_ERROR);
  2482. }
  2483. linecnt = 0;
  2484. while(fgets(temp,8000,fp)!=NULL) {
  2485. p = temp;
  2486. while(isspace(*p))
  2487. p++;
  2488. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2489. continue;
  2490. cnt = 0;
  2491. while(get_float_from_within_string(&p,&dummy)) {
  2492. switch(cnt) {
  2493. case(0):
  2494. if(linecnt == 0) {
  2495. if(dummy != 0) {
  2496. sprintf(errstr,"First time in partials data (%lf) must be zero.\n",dummy);
  2497. return(DATA_ERROR);
  2498. } else
  2499. lasttime = dummy;
  2500. } else {
  2501. if(dummy <= lasttime) {
  2502. sprintf(errstr,"Times do not advance at line %d in partials data.\n",linecnt+1);
  2503. return(DATA_ERROR);
  2504. }
  2505. }
  2506. break;
  2507. default:
  2508. if(ODD(cnt)) { // ODD entries, partial numbers
  2509. if(dummy < 1.0) {
  2510. sprintf(errstr,"Invalid partial (%lf) (less than 1) on line %d.\n",dummy,linecnt+1);
  2511. return(DATA_ERROR);
  2512. }
  2513. if(cnt == 1) {
  2514. if(dz->mode == 0 && dummy != 1.0) {
  2515. sprintf(errstr,"Invalid first partial (%lf) (must be 1 in this tone-generation mode)\n",dummy);
  2516. return(DATA_ERROR);
  2517. }
  2518. lastpartial = dummy;
  2519. }
  2520. else {
  2521. if(dummy <= lastpartial) {
  2522. sprintf(errstr,"Partial numbers do not increase through line %d.\n",linecnt+1);
  2523. return(DATA_ERROR);
  2524. }
  2525. lastpartial = dummy;
  2526. }
  2527. } else // EVEN values are levels, which can be -ve (inverted phase)
  2528. maxval = max(maxval,fabs(dummy));
  2529. break;
  2530. }
  2531. cnt++;
  2532. }
  2533. if(cnt < 3 || EVEN(cnt)) {
  2534. sprintf(errstr,"Invalid number of entries (%d) on line %d\n",cnt,linecnt+1);
  2535. return(DATA_ERROR);
  2536. }
  2537. if(linecnt == 0)
  2538. entrycnt = cnt;
  2539. else if(cnt != entrycnt) {
  2540. sprintf(errstr,"Line %d has different number of entries (%d) to previous lines which have (%d)\n",linecnt+1,cnt,entrycnt);
  2541. return(DATA_ERROR);
  2542. }
  2543. linecnt++;
  2544. }
  2545. if(linecnt == 0) {
  2546. sprintf(errstr,"No data found in partials data file.\n");
  2547. return(DATA_ERROR);
  2548. }
  2549. if(flteq(maxval,0.0)) {
  2550. sprintf(errstr,"No significant level found in partials data file.\n");
  2551. return(DATA_ERROR);
  2552. }
  2553. normaliser = 1.0/maxval;
  2554. partialcnt = (entrycnt - 1)/2;
  2555. if(dz->mode == 2) {
  2556. /*
  2557. * MODE 2 arrays
  2558. * mpcnt = maxpartial cnt (partials + all transpositions) | | |
  2559. * | current
  2560. * | frqs
  2561. * parray |----------|----------|-|-|-----------------|-----------------|-|-|-|
  2562. * | tvarying pno+plevel |s|s| left_level | right-level step|p|
  2563. * | (Mpcnt*2) |i|i| mpcnt | mpcnt timeso|
  2564. * | |n|n| | | | |s|
  2565. * address 0 mpcnt*2| |p|(mpcnt*2)+2 |(mpcnt*3)+2 | (mpcnt*4)+3
  2566. * | | |t| | (mpcnt*4)+2
  2567. * | | |r| | | | (mpcnt*4)+4
  2568. * lengths | linelen of srcdata | | | maxsteps | maxsteps | |m|
  2569. * |s|t| |t|p|
  2570. * (slen = sintablen) |l|o| |o|c|
  2571. * (totl = estimate of no |e|t| |t|n|
  2572. * of timesteps used) |n|l| |l|t|
  2573. */
  2574. totalpartials = partialcnt * dz->iparam[SYNTH_MAX];
  2575. dz->array_cnt = (totalpartials * 2) + 2;// An array for every partial-and-partial-transposition, every pandp-level,
  2576. // and Sin-table + sintab-incr-pointers
  2577. dz->temp_sampsize = dz->array_cnt;
  2578. dz->array_cnt += (totalpartials * 2) + 3; // An array for the left and right level of every partial-and-partial-transposition.
  2579. // and One array for the steptimes, and one array for the frqs of partials at current-time
  2580. dz->itemcnt = totalpartials; // Array for every partial-pno and partial-level + Array for position at every step.
  2581. } else {
  2582. dz->array_cnt = (partialcnt * 2) + 5; // An array for every partial-pno, every partial-level,
  2583. // + snd-sintable + sintab-incr-pointers + packet envelope + 2 packet-envelope-temp-arrays
  2584. dz->itemcnt = partialcnt; // Array for every partial-pno and partial-level.
  2585. }
  2586. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  2587. sprintf(errstr,"INSUFFICIENT MEMORY to create partial data arrays.\n");
  2588. return(MEMORY_ERROR);
  2589. }
  2590. if(dz->mode == 2)
  2591. zz = totalpartials * 2;
  2592. else
  2593. zz = partialcnt * 2;
  2594. for(n=0;n <zz;n++) { // 2 entries (time and value) for every line in the data.
  2595. if((dz->parray[n] = (double *)malloc((linecnt * 2) * sizeof(double)))==NULL) {
  2596. sprintf(errstr,"INSUFFICIENT MEMORY to store partial data.\n");
  2597. return(MEMORY_ERROR);
  2598. }
  2599. }
  2600. fseek(fp,0,0);
  2601. timepos = 0; // Pointer to time-values in all arrays
  2602. valpos = 1; // Pointer to val-at-time in all arrays
  2603. pno_cnt = 0; // Pointer to partial-pno table
  2604. lstart = partialcnt; // Start of partial-level table
  2605. if(dz->mode == 2)
  2606. lstart *= dz->iparam[SYNTH_MAX];
  2607. while(fgets(temp,8000,fp)!=NULL) {
  2608. p = temp;
  2609. if(*p == ';') // Allow comments in file
  2610. continue;
  2611. cnt = 0;
  2612. while(get_float_from_within_string(&p,&dummy)) {
  2613. switch(cnt) {
  2614. case(0):
  2615. for(n=0;n <partialcnt;n++) // Put time in all pno arrays
  2616. dz->parray[n][timepos] = dummy;
  2617. for(m = lstart,n=0;n <partialcnt;n++,m++)// Put time in all level arrays
  2618. dz->parray[m][timepos] = dummy;
  2619. pno_cnt = 0; // Point to start of pnos, and levels
  2620. lev_cnt = lstart;
  2621. break;
  2622. default:
  2623. if(ODD(cnt)) // Put pno in appropriate pno-array
  2624. dz->parray[pno_cnt++][valpos] = dummy;
  2625. else // Put level in appropriate level-array
  2626. dz->parray[lev_cnt++][valpos] = dummy * normaliser;
  2627. break;
  2628. }
  2629. cnt++;
  2630. }
  2631. if(cnt) {
  2632. timepos += 2; // Advance pointers in pno and level tables
  2633. valpos +=2;
  2634. }
  2635. }
  2636. if(fclose(fp)<0) {
  2637. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",str);
  2638. fflush(stdout);
  2639. }
  2640. if(dz->mode == 2) {
  2641. dz->scalefact = dz->parray[partialcnt-1][1]; // Remember the original range
  2642. tablecnt = partialcnt; // Total number of original partial-no (or level) tables
  2643. entrycnt = timepos; // Total number of entries in each table
  2644. if(dz->iparam[SYNTH_MAX] > 1) {
  2645. // COPY ORIGINAL PARTIAL-NO AND LEVEL TABLES INTO HIGHER OCTAVES
  2646. nupno_cnt = partialcnt; // Start of new partial-transpositions tables
  2647. nulev_cnt = lstart + partialcnt;// Pointer to new levels tables
  2648. for(n=1;n<dz->iparam[SYNTH_MAX];n++) { // For every additional 8va
  2649. for(pno_cnt=0,lno_cnt=lstart;pno_cnt<tablecnt;pno_cnt++,lno_cnt++) {// For every original partial-table, and level-table
  2650. for(k=0;k<entrycnt;k+=2) { // For every entry in original tables
  2651. dz->parray[nupno_cnt][k] = dz->parray[pno_cnt][k]; // At same time
  2652. dz->parray[nupno_cnt][k+1] = (dz->parray[pno_cnt][k+1]) * (n+1); // Create new table, partials up n octs
  2653. dz->parray[nulev_cnt][k] = dz->parray[lno_cnt][k]; // At same time
  2654. dz->parray[nulev_cnt][k+1] = dz->parray[lno_cnt][k+1]; // Create new table with same levels
  2655. }
  2656. nupno_cnt++;
  2657. nulev_cnt++;
  2658. }
  2659. }
  2660. // SORT PARTIAL-NOS INTO ASCENDING ORDER
  2661. for(n = 0, m = lstart; n < nupno_cnt-1; n++,m++) {
  2662. for(nn = n+1, mm = m+1; nn < nupno_cnt;nn++, mm++) {
  2663. if(dz->parray[nn][1] < dz->parray[n][1]) { // Sort of first partialval in array
  2664. sortptr = dz->parray[nn];
  2665. dz->parray[nn] = dz->parray[n];
  2666. dz->parray[n] = sortptr;
  2667. sortptr = dz->parray[mm];
  2668. dz->parray[mm] = dz->parray[m];
  2669. dz->parray[m] = sortptr;
  2670. }
  2671. }
  2672. }
  2673. }
  2674. }
  2675. dz->ringsize = linecnt * 2; // Store lengths of partial tables (1 time and 1 value entry from each dataline)
  2676. return(FINISHED);
  2677. }
  2678. /**************************** CREATE_SYNTHESIZER_SNDBUFS ****************************/
  2679. int create_synthesizer_sndbufs(dataptr dz)
  2680. {
  2681. int n;
  2682. size_t bigbufsize;
  2683. long framesize;
  2684. framesize = F_SECSIZE * dz->infile->channels;
  2685. if(dz->mode == 2)
  2686. framesize = F_SECSIZE * dz->iparam[SYNTH_CHANS];
  2687. dz->bufcnt = 1;
  2688. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  2689. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  2690. return(MEMORY_ERROR);
  2691. }
  2692. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  2693. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  2694. return(MEMORY_ERROR);
  2695. }
  2696. bigbufsize = (size_t)Malloc(-1);
  2697. bigbufsize /= dz->bufcnt;
  2698. if(bigbufsize <=0)
  2699. bigbufsize = framesize * sizeof(float);
  2700. dz->buflen = bigbufsize / sizeof(float);
  2701. dz->buflen = (dz->buflen / framesize) * framesize;
  2702. bigbufsize = dz->buflen * sizeof(float);
  2703. if((dz->bigbuf = (float *)malloc(bigbufsize * dz->bufcnt)) == NULL) {
  2704. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  2705. return(PROGRAM_ERROR);
  2706. }
  2707. for(n=0;n<dz->bufcnt;n++)
  2708. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  2709. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  2710. return(FINISHED);
  2711. }
  2712. /****************************** GET_THE_MODE_FROM_CMDLINE *********************************/
  2713. int get_the_mode_from_cmdline(char *str,dataptr dz)
  2714. {
  2715. char temp[200], *p;
  2716. if(sscanf(str,"%s",temp)!=1) {
  2717. fprintf(stderr,"Cannot read mode of program.\n");
  2718. return(USAGE_ONLY);
  2719. }
  2720. p = temp + strlen(temp) - 1;
  2721. while(p >= temp) {
  2722. if(!isdigit(*p)) {
  2723. fprintf(stderr,"Invalid mode of program entered.\n");
  2724. return(USAGE_ONLY);
  2725. }
  2726. p--;
  2727. }
  2728. if(sscanf(str,"%d",&dz->mode)!=1) {
  2729. fprintf(stderr,"Cannot read mode of program.\n");
  2730. return(USAGE_ONLY);
  2731. }
  2732. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  2733. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  2734. return(USAGE_ONLY);
  2735. }
  2736. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  2737. return(FINISHED);
  2738. }
  2739. /**************************** GENERATE_PACKET_ENVELOPE *************************/
  2740. int generate_packet_envelope (dataptr dz)
  2741. {
  2742. int n, halftabsize = TREMOLO_TABSIZE/2;
  2743. int isneg = 0, tablopos, tabhipos, cosarray = (dz->itemcnt * 2) + 2;
  2744. double *costab, *temptab, *origtab, diff, tabrem, tabincr, lotabincr, hitabincr, readpos, frac;
  2745. if((dz->parray[cosarray] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  2746. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  2747. return(MEMORY_ERROR);
  2748. }
  2749. costab = dz->parray[cosarray];
  2750. if((dz->parray[cosarray+1] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  2751. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  2752. return(MEMORY_ERROR);
  2753. }
  2754. temptab = dz->parray[cosarray + 1];
  2755. if((dz->parray[cosarray+2] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  2756. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  2757. return(MEMORY_ERROR);
  2758. }
  2759. origtab = dz->parray[cosarray + 2];
  2760. for(n=0;n<TREMOLO_TABSIZE;n++) {
  2761. costab[n] = cos(PI * 2.0 * ((double)n/(double)TREMOLO_TABSIZE));
  2762. costab[n] += 1.0;
  2763. costab[n] /= 2.0;
  2764. costab[n] = 1.0 - costab[n];
  2765. origtab[n] = costab[n];
  2766. }
  2767. costab[n] = 0.0; /* wrap around point */
  2768. origtab[n] = 0.0; /* wrap around point */
  2769. if(!dz->brksize[SYNTH_SQZ] && !dz->brksize[SYNTH_CTR]) {
  2770. if(flteq(dz->param[SYNTH_SQZ],1.0)) {
  2771. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2772. temptab[n] = origtab[n];
  2773. } else {
  2774. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2775. temptab[n] = pow(origtab[n],dz->param[SYNTH_SQZ]);
  2776. }
  2777. if(flteq(dz->param[SYNTH_CTR],1.0)) {
  2778. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2779. costab[n] = temptab[n];
  2780. } else {
  2781. if(dz->param[SYNTH_CTR] < 0.0) {
  2782. frac = 1.0 + dz->param[SYNTH_CTR];
  2783. isneg = 1;
  2784. } else
  2785. frac = 1.0 - dz->param[SYNTH_CTR];
  2786. if(isneg) {
  2787. lotabincr = 1.0/frac;
  2788. hitabincr = 1.0/(2.0 - frac);
  2789. } else {
  2790. lotabincr = 1.0/(2.0 - frac);
  2791. hitabincr = 1.0/frac;
  2792. }
  2793. readpos = 0;
  2794. tabincr = lotabincr;
  2795. for(n=0;n<TREMOLO_TABSIZE;n++) {
  2796. if(readpos >= halftabsize) {
  2797. tabincr = hitabincr;
  2798. }
  2799. tablopos = (int)floor(readpos);
  2800. tabhipos = min(tablopos + 1,TREMOLO_TABSIZE);
  2801. tabrem = readpos - (double)tablopos;
  2802. diff = temptab[tabhipos] - temptab[tablopos];
  2803. costab[n] = temptab[tablopos] + (diff * tabrem);
  2804. readpos += tabincr;
  2805. }
  2806. }
  2807. }
  2808. return(FINISHED);
  2809. }
  2810. /**************************** MODIFY_PACKET_ENVELOPE *************************/
  2811. int modify_packet_envelope (dataptr dz)
  2812. {
  2813. int n, halftabsize = TREMOLO_TABSIZE/2;
  2814. int isneg = 0, tablopos, tabhipos, cosarray = (dz->itemcnt * 2) + 2;
  2815. double *costab, *temptab, *origtab, diff, tabrem, tabincr, lotabincr, hitabincr, readpos, frac;
  2816. costab = dz->parray[cosarray];
  2817. temptab = dz->parray[cosarray + 1];
  2818. origtab = dz->parray[cosarray + 2];
  2819. if(flteq(dz->param[SYNTH_SQZ],1.0)) {
  2820. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2821. temptab[n] = origtab[n];
  2822. } else {
  2823. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2824. temptab[n] = pow(origtab[n],dz->param[SYNTH_SQZ]);
  2825. }
  2826. if(flteq(dz->param[SYNTH_CTR],1.0)) {
  2827. for(n=0;n<=TREMOLO_TABSIZE;n++)
  2828. costab[n] = temptab[n];
  2829. } else {
  2830. if(dz->param[SYNTH_CTR] < 0.0) {
  2831. frac = 1.0 + dz->param[SYNTH_CTR];
  2832. isneg = 1;
  2833. } else
  2834. frac = 1.0 - dz->param[SYNTH_CTR];
  2835. if(isneg) {
  2836. lotabincr = 1.0/frac;
  2837. hitabincr = 1.0/(2.0 - frac);
  2838. } else {
  2839. lotabincr = 1.0/(2.0 - frac);
  2840. hitabincr = 1.0/frac;
  2841. }
  2842. readpos = 0;
  2843. tabincr = lotabincr;
  2844. for(n=0;n<TREMOLO_TABSIZE;n++) {
  2845. if(readpos >= halftabsize) {
  2846. tabincr = hitabincr;
  2847. }
  2848. tablopos = (int)floor(readpos);
  2849. tabhipos = min(tablopos + 1,TREMOLO_TABSIZE);
  2850. tabrem = readpos - (double)tablopos;
  2851. diff = temptab[tabhipos] - temptab[tablopos];
  2852. costab[n] = temptab[tablopos] + (diff * tabrem);
  2853. readpos += tabincr;
  2854. }
  2855. }
  2856. return(FINISHED);
  2857. }
  2858. /**************************** READ_PACKET_ENVELOPE *************************/
  2859. double read_packet_envelope(int kk,double incr,dataptr dz)
  2860. {
  2861. double *costab, tabpos, tabrem, diff, envv;
  2862. int tablopos, tabhipos, cosarray = (dz->itemcnt * 2) + 2;
  2863. costab = dz->parray[cosarray];
  2864. tabpos = (double)kk * incr;
  2865. tablopos = (int)floor(tabpos);
  2866. tabhipos = min(tablopos + 1,TREMOLO_TABSIZE);
  2867. tabrem = tabpos - (double)tablopos;
  2868. diff = costab[tabhipos] - costab[tablopos];
  2869. envv = costab[tablopos] + (diff * tabrem);
  2870. return envv;
  2871. }
  2872. /*********************** RNDINTPERM ************************/
  2873. void rndintperm(int *perm,int cnt)
  2874. {
  2875. int n,t,k;
  2876. memset((char *)perm,0,cnt * sizeof(int));
  2877. for(n=0;n<cnt;n++) {
  2878. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  2879. if(t==n) {
  2880. for(k=n;k>0;k--)
  2881. perm[k] = perm[k-1];
  2882. perm[0] = n;
  2883. } else {
  2884. for(k=n;k>t;k--)
  2885. perm[k] = perm[k-1];
  2886. perm[t] = n;
  2887. }
  2888. }
  2889. }
  2890. /************************************ PANCALC *******************************/
  2891. void pancalc(double position,double *leftgain,double *rightgain)
  2892. {
  2893. int dirflag;
  2894. double temp;
  2895. double relpos;
  2896. double reldist, invsquare;
  2897. if(position < 0.0)
  2898. dirflag = SIGNAL_TO_LEFT; /* signal on left */
  2899. else
  2900. dirflag = SIGNAL_TO_RIGHT;
  2901. if(position < 0)
  2902. relpos = -position;
  2903. else
  2904. relpos = position;
  2905. if(relpos <= 1.0){ /* between the speakers */
  2906. temp = 1.0 + (relpos * relpos);
  2907. reldist = ROOT2 / sqrt(temp);
  2908. temp = (position + 1.0) / 2.0;
  2909. *rightgain = temp * reldist;
  2910. *leftgain = (1.0 - temp ) * reldist;
  2911. } else { /* outside the speakers */
  2912. temp = (relpos * relpos) + 1.0;
  2913. reldist = sqrt(temp) / ROOT2; /* relative distance to source */
  2914. invsquare = 1.0 / (reldist * reldist);
  2915. if(dirflag == SIGNAL_TO_LEFT){
  2916. *leftgain = invsquare;
  2917. *rightgain = 0.0;
  2918. } else { /* SIGNAL_TO_RIGHT */
  2919. *rightgain = invsquare;
  2920. *leftgain = 0;
  2921. }
  2922. }
  2923. }
  2924. /************************************ SORT_PARTIALS_INTO_ASCENDING_FRQ_ORDER *******************************/
  2925. void sort_partials_into_ascending_frq_order(int total_partialcnt,double *pvals,double *sinptr,double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz)
  2926. {
  2927. double *sortptr, temp;
  2928. int n, m, nn, mm, itemp;
  2929. int *iptr;
  2930. for(n = 0, m = total_partialcnt; n < total_partialcnt-1; n++,m++) { // m indexes levels
  2931. for(nn = n+1, mm = m+1; nn < total_partialcnt;nn++, mm++) {
  2932. if(pvals[nn] < pvals[n]) { // Sort on partialval
  2933. // Shuffle arrays so they're in ascending frq order, of CURRENT frqs
  2934. sortptr = dz->parray[nn];
  2935. dz->parray[nn] = dz->parray[n];
  2936. dz->parray[n] = sortptr;
  2937. sortptr = dz->parray[mm];
  2938. dz->parray[mm] = dz->parray[m];
  2939. dz->parray[m] = sortptr;
  2940. // Shuffle associated sinptrs
  2941. temp = sinptr[nn];
  2942. sinptr[nn] = sinptr[n];
  2943. sinptr[n] = temp;
  2944. // Shuffle associated (left-)level pointers
  2945. sortptr = llev[nn];
  2946. llev[nn] = llev[n];
  2947. llev[n] = sortptr;
  2948. // Shuffle associated right-level pointers
  2949. sortptr = rlev[nn];
  2950. rlev[nn] = rlev[n];
  2951. rlev[n] = sortptr;
  2952. // Shuffle associated onoff flags
  2953. iptr = onoff[nn];
  2954. onoff[nn] = onoff[n];
  2955. onoff[n] = iptr;
  2956. // Shuffle associated lmost-spkr info
  2957. iptr = lmost[nn];
  2958. lmost[nn] = lmost[n];
  2959. lmost[n] = iptr;
  2960. // Shuffle associated splicectr origins
  2961. iptr = origspl[nn];
  2962. origspl[nn] = origspl[n];
  2963. origspl[n] = iptr;
  2964. // Finally swap frqs into correct order
  2965. temp = pvals[nn];
  2966. pvals[nn] = pvals[n];
  2967. pvals[n] = temp;
  2968. // And keep track of reordering, for 2nd pass
  2969. itemp = splordr[nn];
  2970. splordr[nn] = splordr[n];
  2971. splordr[n] = itemp;
  2972. }
  2973. }
  2974. }
  2975. }
  2976. /************************************ RESORT_PARTIALS_INTO_ORIGINAL_FRQ_ORDER *******************************/
  2977. void resort_partials_into_original_frq_order(int total_partialcnt,double *pvals,double *sinptr,double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz)
  2978. {
  2979. double *sortptr, temp;
  2980. int n, m, nn, mm;
  2981. int *iptr;
  2982. for(n = 0, m = total_partialcnt; n < total_partialcnt-1; n++,m++) { // m indexes levels
  2983. for(nn = n+1, mm = m+1; nn < total_partialcnt;nn++, mm++) {
  2984. if(splordr[nn] < splordr[n]) { // Sort on original order value
  2985. // Shuffle arrays so they're in original order
  2986. sortptr = dz->parray[nn];
  2987. dz->parray[nn] = dz->parray[n];
  2988. dz->parray[n] = sortptr;
  2989. sortptr = dz->parray[mm];
  2990. dz->parray[mm] = dz->parray[m];
  2991. dz->parray[m] = sortptr;
  2992. // Shuffle associated sinptrs
  2993. temp = sinptr[nn];
  2994. sinptr[nn] = sinptr[n];
  2995. sinptr[n] = temp;
  2996. // Shuffle associated (left-)level pointers
  2997. sortptr = llev[nn];
  2998. llev[nn] = llev[n];
  2999. llev[n] = sortptr;
  3000. // Shuffle associated right-level pointers
  3001. sortptr = rlev[nn];
  3002. rlev[nn] = rlev[n];
  3003. rlev[n] = sortptr;
  3004. // Shuffle associated onoff flags
  3005. iptr = onoff[nn];
  3006. onoff[nn] = onoff[n];
  3007. onoff[n] = iptr;
  3008. // Shuffle associated lmost-spkr info
  3009. iptr = lmost[nn];
  3010. lmost[nn] = lmost[n];
  3011. lmost[n] = iptr;
  3012. // Shuffle associated splicectr origins
  3013. iptr = origspl[nn];
  3014. origspl[nn] = origspl[n];
  3015. origspl[n] = iptr;
  3016. // Finally swap frqs into correct order
  3017. temp = pvals[nn];
  3018. pvals[nn] = pvals[n];
  3019. pvals[n] = temp;
  3020. }
  3021. }
  3022. }
  3023. }
  3024. /**************************************** XCLUSIVE **************************************
  3025. *
  3026. * Resort an existing permuutation (of partials chosen)
  3027. * so they already ON partials occur after all the corrently-OFF partials
  3028. */
  3029. void xclusive(int *perm,int *permon,int *permoff,int max_partials_cnt,int partials_in_play, int **onoff,int stepcnt)
  3030. {
  3031. int permoncnt = 0, permoffcnt = 0, n, ptl;
  3032. if(partials_in_play == max_partials_cnt)
  3033. return;
  3034. for(n = 0;n < max_partials_cnt;n++) {
  3035. ptl = perm[n];
  3036. if(onoff[ptl][stepcnt]) // If this partial is already ON
  3037. permon[permoncnt++] = ptl; // Store the ON-partials, in order they were in initial perm
  3038. else
  3039. permoff[permoffcnt++] = ptl; // If this partial is OFF
  3040. } // Store the OFF-partials, in order they were in initial perm
  3041. for(n=0;n<permoffcnt;n++) // Place the OFF partials first in the permlist,
  3042. perm[n] = permoff[n]; // But otherwise preserving perm order.
  3043. while(n < max_partials_cnt) {
  3044. perm[n] = permon[n];
  3045. n++;
  3046. }
  3047. }
  3048. /**************************************** EMERGEPOS **************************************
  3049. *
  3050. * Find spatial position, where image emerging from single channel to gradually fill all channels
  3051. */
  3052. double emergepos(int emergchan,int chans,double time,double timespan)
  3053. {
  3054. double frac, chanspan, pos, lmost;
  3055. emergchan--;
  3056. frac = time/timespan; // Fraction of emerge-time covered
  3057. if(frac < 0.33)
  3058. chanspan = 0;
  3059. else {
  3060. frac = pow((frac - 0.33),1.5);
  3061. chanspan = (double)chans * frac; // Fraction of total-channels available
  3062. }
  3063. pos = drand48() * chanspan; // Position randomly within chanspan
  3064. lmost = (double)emergchan - (chanspan/2.0); // Find leftmost position (relative to emergence chan)
  3065. pos += lmost; // Find true position
  3066. if(pos < 0.0) // Adjust for %N chans
  3067. pos += (double)chans;
  3068. else if(pos >= chans) // Adjust for %N chans
  3069. pos -= (double)chans;
  3070. return pos;
  3071. }
  3072. /**************************************** EMERGEPOS **************************************
  3073. *
  3074. * Find spatial position, where image converging to single channel from all channels
  3075. */
  3076. double convergepos(int converchan,int chans,double time,double convergetime,double dur)
  3077. {
  3078. double frac, chanspan, pos, lmost; // Fraction of converge-time covered
  3079. int ipos;
  3080. converchan--;
  3081. frac = (time - convergetime)/(dur - convergetime);
  3082. frac = 1.0 - frac; // Amount of convergence
  3083. if(frac < 0.33)
  3084. chanspan = 0;
  3085. else {
  3086. frac = pow((frac - 0.33),2.0);
  3087. chanspan = (double)chans * frac; // Fraction of total-channels available
  3088. }
  3089. pos = drand48() * chanspan; // Position randomly within chanspan
  3090. ipos = (int)round(pos/0.1);
  3091. pos = ipos * 0.1;
  3092. lmost = (double)converchan - (chanspan/2.0);// Find leftmost position (relative to convergence chan)
  3093. pos += lmost; // Find true position
  3094. if(pos < 0.0) // Adjust for %N chans
  3095. pos += (double)chans;
  3096. else if(pos >= chans) // Adjust for %N chans
  3097. pos -= (double)chans;
  3098. return pos;
  3099. }
  3100. /**************************************** SPACEBOX **************************************/
  3101. void spacebox(double *pos, int *switchpos, double posstep, int chans, int spacetyp, int configno, int configcnt,int *superperm)
  3102. {
  3103. switch(spacetyp) {
  3104. case(SB_LRRAND): // Alternate Left and Right sides, random position
  3105. *pos = chans/2 * drand48(); // Random choice of half of chan positions
  3106. if(*switchpos) // If switch on, put in 2nd half
  3107. *pos += chans/2;
  3108. *switchpos = -(*switchpos);
  3109. break;
  3110. case(SB_FBRAND): // Alternate Front and Back sides, random position
  3111. *pos = chans/2 * drand48(); // Simil for front and back
  3112. if(*switchpos) {
  3113. *pos += 2;
  3114. if(*pos >= chans)
  3115. *pos -= chans;
  3116. } else {
  3117. *pos += 6;
  3118. if(*pos >= chans)
  3119. *pos -= chans;
  3120. }
  3121. *switchpos = -(*switchpos);
  3122. break;
  3123. case(SB_ROTATE): // Rotating clockwise or anticlockwise
  3124. *pos += posstep;
  3125. if(*pos >= chans)
  3126. *pos -= chans;
  3127. else if(*pos < 0.0)
  3128. *pos += chans;
  3129. break;
  3130. case(SB_SUPERSPACE):
  3131. case(SB_SUPERSPACE2):
  3132. case(SB_SUPERSPACE3):
  3133. case(SB_SUPERSPACE4): // Get item in current permutaion of possibilities
  3134. *switchpos = superperm[configcnt];
  3135. break;
  3136. case(SB_LR): // Alternate all-left/all-right Switch between the 2 alternatives
  3137. case(SB_FB): // Alternate all-back/all-front
  3138. case(SB_FRAMESWITCH): // Switch all-square/all-diamond
  3139. *switchpos = !(*switchpos);
  3140. break;
  3141. case(SB_TRIROT1): // Rotate triangle formed by spkrs 2-apart clockwise
  3142. case(SB_TRIROT2): // Rotate triangle formed by spkrs 3-apart clockwise
  3143. (*switchpos)++; // Advance apex of triangle
  3144. if(*switchpos >= chans)
  3145. *switchpos -= chans;
  3146. break;
  3147. case(SB_ANTITRIROT1): // Rotate triangle formed by spkrs 2-apart anticlockwise
  3148. case(SB_ANTITRIROT2): // Rotate triangle formed by spkrs 2-apart anticlockwise
  3149. (*switchpos)--; // Regress apex of triangle
  3150. if(*switchpos < chans)
  3151. *switchpos += chans;
  3152. break;
  3153. }
  3154. }
  3155. /**************************************** SPACEBOX_APPLY **************************************/
  3156. void spacebox_apply(double pos, double lev,int chans,int *lmost, int *rmost,double *rlev,double *llev,int spacetyp)
  3157. {
  3158. double leftgain, rightgain;
  3159. switch(spacetyp) {
  3160. case(SB_LRRAND): // These options use true stereo between adjacent speakers
  3161. case(SB_FBRAND): // Find levels and left/right lspkrs
  3162. case(SB_ROTATE):
  3163. *lmost = (int)floor(pos);
  3164. pos -= (double)(*lmost);
  3165. pos = (pos * 2.0) - 1.0;
  3166. pancalc(pos,&leftgain,&rightgain);
  3167. *rlev = lev * rightgain;
  3168. *llev = lev * leftgain;
  3169. *rmost = (*lmost + 1) % chans;
  3170. break;
  3171. case(SB_LR):
  3172. case(SB_FB):
  3173. case(SB_TRIROT1):
  3174. case(SB_ANTITRIROT1):
  3175. case(SB_TRIROT2):
  3176. case(SB_ANTITRIROT2):
  3177. case(SB_FRAMESWITCH):
  3178. case(SB_SUPERSPACE):
  3179. case(SB_SUPERSPACE2):
  3180. case(SB_SUPERSPACE3):
  3181. case(SB_SUPERSPACE4):
  3182. *llev = lev; // Input level is distributed (as is) amongst various lspkrs
  3183. break;
  3184. }
  3185. }
  3186. /**************************************** OUTPUT_SPECIAL_SPATIALISATION_SAMPLE **************************************/
  3187. void output_special_spatialisation_sample(float *obuf,int sampcnt,int switchpos,int chans,double val,double valr,int lmost,int rmost,int spacetyp)
  3188. {
  3189. int k, tri1, tri2, tri3, a, b;
  3190. switch(spacetyp) {
  3191. case(SB_LR):
  3192. if(switchpos) {
  3193. for(k = (chans/2)+1;k < chans;k++)
  3194. obuf[sampcnt+k] = (float)(obuf[sampcnt+k] + val);
  3195. } else {
  3196. for(k = 1;k < chans/2;k++)
  3197. obuf[sampcnt+k] = (float)(obuf[sampcnt+k] + val);
  3198. }
  3199. break;
  3200. case(SB_FB):
  3201. if(switchpos) {
  3202. for(k = 0;k < chans;k++) {
  3203. if(k < 2 || k == 7)
  3204. obuf[sampcnt+k] = (float)(obuf[sampcnt+k] + val);
  3205. }
  3206. } else {
  3207. for(k = 3;k < 6;k++)
  3208. obuf[sampcnt+k] = (float)(obuf[sampcnt+k] + val);
  3209. }
  3210. break;
  3211. case(SB_TRIROT1):
  3212. case(SB_ANTITRIROT1):
  3213. tri1 = switchpos;
  3214. tri2 = (switchpos + 2) % chans;
  3215. tri3 = (switchpos + 6) % chans;
  3216. for(k = 0;k< chans;k++) {
  3217. if(k == tri1 || k == tri2 || k == tri3) // Add output only to the 2 relevant channels
  3218. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3219. sampcnt++;
  3220. }
  3221. break;
  3222. case(SB_TRIROT2):
  3223. case(SB_ANTITRIROT2):
  3224. tri1 = switchpos;
  3225. tri2 = (switchpos + 3) % chans;
  3226. tri3 = (switchpos + 5) % chans;
  3227. for(k = 0;k< chans;k++) {
  3228. if(k == tri1 || k == tri2 || k == tri3) // Add output only to the 2 relevant channels
  3229. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3230. sampcnt++;
  3231. }
  3232. break;
  3233. case(SB_FRAMESWITCH):
  3234. if(switchpos) {
  3235. for(k = 0;k< chans;k++) { // SQUARE
  3236. if(ODD(k))
  3237. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3238. sampcnt++;
  3239. }
  3240. } else {
  3241. for(k = 0;k< chans;k++) { // DIAMOND
  3242. if(EVEN(k))
  3243. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3244. sampcnt++;
  3245. }
  3246. }
  3247. break;
  3248. case(SB_SUPERSPACE):
  3249. case(SB_SUPERSPACE2):
  3250. case(SB_SUPERSPACE3):
  3251. case(SB_SUPERSPACE4):
  3252. if(switchpos <= 7) { // 0 - 7 Single chans
  3253. obuf[sampcnt+switchpos] = (float)(obuf[sampcnt+switchpos] + val);
  3254. } else if(switchpos <=35) { // 8 - 35
  3255. switchpos -= 8; // 0 - 27
  3256. if(switchpos >=24) { // 24 - 27
  3257. switchpos -= 24; // 0 - 3 paired with its opposite
  3258. obuf[sampcnt+switchpos] = (float)(obuf[sampcnt+switchpos] + val);
  3259. switchpos += chans/2; // 4 - 7
  3260. obuf[sampcnt+switchpos] = (float)(obuf[sampcnt+switchpos] + val);
  3261. } else { // 0 - 23
  3262. a = switchpos/3; // 0-7 = a
  3263. b = switchpos - (a*3); // 0-2
  3264. b++; // 1-3
  3265. b = (a + b) % chans; // a+(1-3)
  3266. obuf[sampcnt+a] = (float)(obuf[sampcnt+a] + val);
  3267. obuf[sampcnt+b] = (float)(obuf[sampcnt+b] + val);
  3268. }
  3269. } else if(switchpos <= 43) { // 36 - 43 TRIANGLE 1
  3270. switchpos -=36; // 0 - 7
  3271. tri1 = switchpos; // 0,1,2...
  3272. tri2 = (switchpos + 2) % chans; // 2,3,4...
  3273. tri3 = (switchpos + 6) % chans; // 7,6,0...
  3274. for(k = 0;k< chans;k++) {
  3275. if(k == tri1 || k == tri2 || k == tri3) // Add output only to the 2 relevant channels
  3276. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3277. sampcnt++;
  3278. }
  3279. } else if(switchpos <= 51) { // 44 - 51 TRIANGLE 2
  3280. switchpos -= 44; // 0 - 7
  3281. tri1 = switchpos; // 0,1,2,...
  3282. tri2 = (switchpos + 3) % chans; // 3,4,5...
  3283. tri3 = (switchpos + 5) % chans; // 5,6,7...
  3284. for(k = 0;k< chans;k++) {
  3285. if(k == tri1 || k == tri2 || k == tri3) // Add output only to the 2 relevant channels
  3286. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3287. sampcnt++;
  3288. }
  3289. } else if(switchpos == 52) { // SQUARE
  3290. for(k = 0;k< chans;k++) {
  3291. if(EVEN(k)) // 0,2,4,6
  3292. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3293. sampcnt++;
  3294. }
  3295. break;
  3296. } else if(switchpos == 53) { // DIAMOND
  3297. for(k = 0;k< chans;k++) {
  3298. if(ODD(k)) // 1,3,5,7
  3299. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3300. sampcnt++;
  3301. }
  3302. break;
  3303. } else { // 54 ALL
  3304. for(k = 0;k< chans;k++) { // 0,1,2,3,4,5,6,7
  3305. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3306. sampcnt++;
  3307. }
  3308. break;
  3309. }
  3310. break;
  3311. default: // STEREO POSITIONED BETWEEN SOME PAIR OF CHANNELS
  3312. for(k = 0;k< chans;k++) {
  3313. if(k == lmost) // Add output only to the 2 relevant channels
  3314. obuf[sampcnt] = (float)(obuf[sampcnt] + val);
  3315. else if(k == rmost)
  3316. obuf[sampcnt] = (float)(obuf[sampcnt] + valr);
  3317. sampcnt++;
  3318. }
  3319. }
  3320. }
  3321. /**************************** DUFFING *************************/
  3322. int duffing(dataptr dz)
  3323. {
  3324. int exit_status, passno;
  3325. int sampcnt, bufpos, n;
  3326. double time, maxsamp = 0.0, spliceamp, normaliser = 1.0, srate = (double)dz->iparam[SYNTHSRAT], tabpos, val, vel;
  3327. double delta_t = 1.0/srate; // Time-increment between sample-generation
  3328. float *obuf = dz->sampbuf[0];
  3329. int outlen = (int)round(dz->infile->srate * dz->param[SYNTH_DUR]);
  3330. double spliceincr = 1.0/dz->rampbrksize;
  3331. int splicestart = outlen - dz->rampbrksize;
  3332. dz->scalefact = (double)SYNTH_TABSIZE/srate; // Constant in sintable read
  3333. for(passno = 0;passno < 2;passno++) {
  3334. switch(passno) {
  3335. case(0): fprintf(stdout,"INFO: Assessing level.\n"); break;
  3336. case(1): fprintf(stdout,"INFO: Generating output sound.\n"); break;
  3337. }
  3338. fflush(stdout);
  3339. sampcnt = 0;
  3340. bufpos = 0;
  3341. tabpos = 0.0; // Initial position in table reading VELOCITY of point
  3342. val = 0.0; // Initial POSITION of point
  3343. vel = 0.0; // Initial velocity of point
  3344. if(splicestart < 0) // If outduration too short to include whole endsplice
  3345. spliceamp = outlen/dz->rampbrksize; // Preset start-amp at appropriate level within endsplice
  3346. else
  3347. spliceamp = 1.0;
  3348. while(sampcnt < outlen) {
  3349. obuf[bufpos] = (float)val;
  3350. if(sampcnt >= splicestart) { // If in endsplice, do end splice
  3351. obuf[bufpos] = (float)(obuf[bufpos] * spliceamp);
  3352. spliceamp -= spliceincr;
  3353. spliceamp = max(spliceamp,0.0);
  3354. }
  3355. bufpos++;
  3356. if(bufpos >= dz->buflen) {
  3357. switch(passno) {
  3358. case(0):
  3359. for(n=0;n<dz->buflen;n++)
  3360. maxsamp = max(maxsamp,fabs(obuf[n]));
  3361. break;
  3362. case(1):
  3363. for(n=0;n<dz->buflen;n++)
  3364. obuf[n] = (float)(obuf[n] * normaliser);
  3365. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3366. return(exit_status);
  3367. break;
  3368. }
  3369. bufpos = 0;
  3370. }
  3371. time = (double)sampcnt/srate;
  3372. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3373. return exit_status;
  3374. duffing_osc(&val,&vel,delta_t,&tabpos,dz);
  3375. sampcnt++;
  3376. }
  3377. if(bufpos > 0) {
  3378. switch(passno) {
  3379. case(0):
  3380. for(n=0;n<bufpos;n++)
  3381. maxsamp = max(maxsamp,fabs(obuf[n]));
  3382. if(maxsamp <= 0.0) {
  3383. sprintf(errstr,"NO SIGNIFICANT SOUND-LEVEL PRODUCED.\n");
  3384. return(GOAL_FAILED);
  3385. } else if(maxsamp > 0.95) {
  3386. if(maxsamp >= HUGE) {
  3387. fprintf(stdout,"INFO: Output blew up\n");
  3388. fflush(stdout);
  3389. exit(1);
  3390. }
  3391. normaliser = 1.0/maxsamp;
  3392. fprintf(stdout,"INFO: Max Level %lf Normalising output by %lf\n",maxsamp,normaliser);
  3393. fflush(stdout);
  3394. }
  3395. break;
  3396. case(1):
  3397. for(n=0;n<bufpos;n++)
  3398. obuf[n] = (float)(obuf[n] * normaliser);
  3399. if((exit_status = write_samps(obuf,bufpos,dz))<0)
  3400. return(exit_status);
  3401. break;
  3402. }
  3403. }
  3404. }
  3405. return FINISHED;
  3406. }
  3407. /**************************** SINREAD *************************/
  3408. double sinread(double *tabpos,double frq,dataptr dz)
  3409. {
  3410. double tabincr, val, valdiff, timefrac, *sintab = dz->parray[0];
  3411. int lopos, hipos;
  3412. lopos = (int)floor(*tabpos);
  3413. hipos = (int)ceil(*tabpos);
  3414. timefrac = *tabpos - (double)lopos;
  3415. val = sintab[lopos];
  3416. valdiff = sintab[hipos] - val;
  3417. val += valdiff * timefrac;
  3418. tabincr = frq * dz->scalefact;
  3419. *tabpos += tabincr;
  3420. if(*tabpos >= SYNTH_TABSIZE)
  3421. *tabpos -= SYNTH_TABSIZE;
  3422. return val;
  3423. }
  3424. /**************************** DUFFING_OSC *************************
  3425. *
  3426. *
  3427. * dx/dt = y velocity is rate of change of position (x)
  3428. *
  3429. * 3
  3430. * dy/dt = x - x - Dy + Fcoswt acceleration is driven partly by sinusoid-forcing and partly by system-damping
  3431. *
  3432. * where Fcoswt is a driving sinusoidal oscillation
  3433. * and x - x3 - Dy is the damping due to the Duffing double well.
  3434. */
  3435. void duffing_osc(double *val,double *vel, double delta_t,double *tabpos,dataptr dz)
  3436. {
  3437. double delta_vel, damped_acc, forced_acc;
  3438. damped_acc = (dz->param[SYNTH_K] * (*val)) - (dz->param[SYNTH_B] * pow((*val),3.0)); // Duffing damping of acceleration
  3439. damped_acc -= dz->param[SYNTH_DAMP] * (*vel);
  3440. forced_acc = sinread(tabpos,dz->param[SYNTH_FRQ],dz); // Sinusoidally varying acceleration
  3441. forced_acc *= 1000000; // scaled in amplitude
  3442. delta_vel = (damped_acc + forced_acc) * delta_t; // Change in velocity caused by acceleration
  3443. *val += *vel * delta_t; // position changed due to velocity
  3444. *vel += delta_vel; // velocity changed due to aceleration
  3445. }