multisynth.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125
  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 along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. // COMMENT
  22. // Works now for instruments with spectra defined internally
  23. // With note-loudness-envelope
  24. // With vibrato
  25. // Modified to accept an instrument, time, note, level, duration as parameters.
  26. // Tested with 1 note and with 2 notes.
  27. // Modified to accept several instruments and tested with 3 instruments.
  28. // Modified to make output stereo, and distrib outputs in (fixed) positions in stereo space.
  29. // Jitter (from quantised times) added
  30. // All instrument data consolidated into a structure "synstrument"
  31. // Multichan output option implemented
  32. // Orient multichan to be in front of listener.
  33. // TO DO
  34. // (0) Get (at least) 6 distinguishable instruments working
  35. // (1) Figure out how to enter data, and how to display entered data, in music-staff format.
  36. // (2) Work on permutation rules.
  37. // (3) Generating several outputs and playing side-by-side with input, for comparison.
  38. //
  39. // This is first stage of creating a multi-instrument synth engine, to test out chanber-music ideas.
  40. // Currently it does additive synth and packet synth, with input data files.
  41. // Idea is to choose a set of data values and use these as INTERNAL tables, to synthesize instrument notes.
  42. //
  43. // The synth must be extended as follows
  44. // (1) Add vibrato option
  45. // (2) Control the individual events generated (by each internal-table controlled instrument) from
  46. // new external params for pitch, level and duration (could be MIDI,MIDI,quantised-time)
  47. // (3) From an input score (several instruments) generate an output
  48. // (4) From an input score, generate several derived scores according to rules of transformation.
  49. // (5) Play the outputs, and hence select the best outcome .... ETC in an evloving sequence-of-steps.
  50. // (6) Convert the output data into staff notation.
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <structures.h>
  54. #include <tkglobals.h>
  55. #include <pnames.h>
  56. #include <filetype.h>
  57. #include <processno.h>
  58. #include <modeno.h>
  59. #include <logic.h>
  60. #include <globcon.h>
  61. #include <cdpmain.h>
  62. #include <math.h>
  63. #include <mixxcon.h>
  64. #include <osbind.h>
  65. #include <standalone.h>
  66. #include <ctype.h>
  67. #include <sfsys.h>
  68. #include <string.h>
  69. #include <srates.h>
  70. #ifdef unix
  71. #define round(x) lround((x))
  72. #endif
  73. #ifndef HUGE
  74. #define HUGE 3.40282347e+38F
  75. #endif
  76. #define SAFETY 256
  77. #define VIBSHIFT (1.25) // Extension of pre-vibrato output, to allow for any possible length-variation with addition of vibrato
  78. #define MS_MM 0
  79. #define MS_MAXJIT 1
  80. #define MS_OCHANS 2
  81. #define MAXOUTDUR 60 // 1 minute
  82. #define MSYNSRATE 44100
  83. //#define MSYNCHANS 1
  84. #define INS_NO (0)
  85. #define NOTE_TIME (0)
  86. #define NOTE_PICH (1)
  87. #define NOTE_LEVL (2)
  88. #define NOTE_DUR (3)
  89. #define MSYNMAXQDUR (768) // Assumes time specified in quantisation units of semiquavers, maxdur = semibreve * 8
  90. #define MAXENTRYCNT (129) // 64 partials+levels and 1 time value
  91. #define MAXLINECNT (16) // 16 possible sets of changing spectral data
  92. #define MAXENVPNTS (8) // Maximum number of points specifiying note-envelope for instrument
  93. #define MAXSCORLINE (48) // Maximum number of lines (and therefore instruments) in score
  94. #define MAXPARTIALS (64) // (MAXENTRYCNT - 1)/2;
  95. #define ROOT2 (1.4142136)
  96. #define SIGNAL_TO_LEFT (0)
  97. #define SIGNAL_TO_RIGHT (1)
  98. #define UNITSPERSEMIQ 3 // No of time-units per semiquaver
  99. #define FLUTE_PASSFRQ 260
  100. #define FLUTE_STOPFRQ 200
  101. #define FILTATTEN -96.0 /* RWD was integer, this seems more idiomatic */
  102. #define TRUMPET 0
  103. #define CLARINET 1
  104. #define PIANO_RH 2
  105. #define PIANO_LH 3
  106. #define FLUTE 4
  107. #define VIOLIN 5
  108. #define CELLO 6
  109. #define vibrato is_flat
  110. // Array-indices: indicate which array contains which data
  111. #define sinarray fzeroset // sinetable : for sound and vibrato creation
  112. #define pntarray zeroset // various pointers into sinetable, for all partials and for vibrato
  113. #define pakt_env is_transpos // Envelope of wave-packets used in synth
  114. #define temp_tab could_be_transpos // Temporary storage of modified wave-packet
  115. #define orig_tab could_be_pitch // Temporary storage of original wave-packet
  116. #define ins_spectrum is_rectified // Spectrum of current instrument being used
  117. #define ins_envel specenv_type // Loudness-envelope template of current instrument being used
  118. #define ins_vibd deal_with_chan_data // Time-changing data for depth of vibrato for current-note
  119. #define ins_vibf unspecified_filecnt // Time-changing data for frq of vibrato for current-note
  120. #define ochan_pos finished // Positions of instrument streams in multichan output
  121. #define scoredata is_mapping // Storage of input score-data
  122. #define partialtabs_cnt ringsize // Number of partial-data tables (partialno at time t, partiallevel at time t, for all t)
  123. #define filterbas duplicate_snds // Base of counter of filter arrays
  124. // Size of sound and envelope arrays
  125. #define max_notedur tempsize // samplelength of longest note to be generated
  126. #define maxoutsamp rampbrksize // Total samplelength of output sound
  127. // Global constants
  128. #define timeconvert is_sharp // Conversion from semiquavers to time at input MM
  129. // ALPHA : instrument specific data
  130. struct synstrument {
  131. char name[64];
  132. double *spectrum;
  133. int partial_cnt; // No of partials specified in spectrum
  134. int line_cnt; // No of lines, each specifying spectrum at a different time in its evolution
  135. int line_entrycnt; // Total no of vals to specify line of spec: = line_cnt * (1 + (partialcnt * 2)) (time + all partialno-loudness pairs)
  136. int spec_len; // Total no of vals to specify spec: = line_entrycnt * line_cnt
  137. double *env; // Envelope in time-val pairs,
  138. int env_len; // env_len counts all entries so with 3 pairs, env_len = 6 : 10 or 12 ONLY !!
  139. int has_vibrato; // flag (0 1)
  140. double maxvibdepth; // max depth of any vibrato, in semitones
  141. int packettype; // If packettype == 1, synthesis by packets, else, simple additive synth
  142. double squeeze; // Narrowing of packet envelope (0 - 1000). Below 1.0 broaden packet. Vals near zero or very high produce clicks or silence.
  143. double centre; // Centring of peak of packet envelope. 0 peak at centre: -1 peak at start: 1 peak at end.
  144. int rangebot; // Lowest note of instrument (MIDI)
  145. int rangetop; // Highest note of instrument (MIDI)
  146. double balance; // Level of instrument relative to other instruments
  147. int overlap; // Flags if successive instrument-notes can overlap (e.g. piano resonance).
  148. int doublestop; // Max number of simultaneous notes instrument can play (also see rules!!).
  149. };
  150. typedef struct synstrument *synptr;
  151. // Preloaded (time-changing) spectra and loudness-envelope for synthetic instruments
  152. static int orchestra_size = 7;
  153. static double trumpet[99] = {0.0,1,0.970464,2,0.991561,3,0.590717,4,0.759494,5,0.632911,6,1.000000,7,0.573840,8,0.358650,
  154. 9,0.177215,10,0.033755,11,0.025316,12,0.016878,13,0.016878,14,0.012658,15,0.008439,16,0.012658,
  155. 0.1,1,0.970464,2,0.991561,3,0.590717,4,0.759494,5,0.632911,6,1.000000,7,0.573840,8,0.358650,
  156. 9,0.177215,10,0.033755,11,0.025316,12,0.016878,13,0.016878,14,0.012658,15,0.008439,16,0.012658,
  157. 1.0,1,0.970464,2,0.991561,3,0.590717,4,0.759494,5,0.632911,6,1.000000,7,0.573840,8,0.358650,
  158. 9,0.177215,10,0.033755,11,0.025316,12,0.016878,13,0.016878,14,0.012658,15,0.008439,16,0.012658};
  159. static double trumpenv[12] = {0.0, 0,
  160. 0.035,1,
  161. 0.085, 1,
  162. 0.125,.3,
  163. 0.9,.3,
  164. 1.0, 0};
  165. static double clarinet[42] = {0.0,1,1,3,0.9,5,0.8,7,0.7,9,0.6,11,0.5,13,0.4,15,0.3,17,0.2,19,0.1,
  166. 1.0,1,1,3,0.9,5,0.8,7,0.7,9,0.6,11,0.5,13,0.4,15,0.3,17,0.2,19,0.1};
  167. static double clarenv[12] = {0.0, 0,
  168. 0.035,1,
  169. 0.085, 1,
  170. 0.125, 1,
  171. 0.9, 1,
  172. 1.0, 0};
  173. static double flute[62] = {0.0, 1,1.000000,2,0.028184,3,0.031623,4,0.007943,5,0.008913,6,0.003548,7,0.003162,8,0.001778,
  174. 9,0.001778,10,0.001585,11,0.001585,12,0.001413,13,0.001413,14,0.001259,15,0.001259,
  175. 1.0, 1,1.000000,2,0.028184,3,0.031623,4,0.007943,5,0.008913,6,0.003548,7,0.003162,8,0.001778,
  176. 9,0.001778,10,0.001585,11,0.001585,12,0.001413,13,0.001413,14,0.001259,15,0.001259};
  177. static double flutenv[12] = {0.0, 0,
  178. 0.065,0.5,
  179. 0.085,1,
  180. 0.125,1,
  181. 0.9, 1,
  182. 1.0, 0};
  183. static double piano[100] = {0.0,1,1,2,1 ,4,1 ,7,1 ,8,1 , 12,1 ,17,1 ,18,1 ,19,1, 33.67,1, 21.7262,1, 30.4134,1,
  184. 0.3,1,1,2,0.9 ,4,0.8, 7,0.7 ,8,0.6, 12,0.5 ,17,0.4,18,0.3 ,19,0.3, 33.67,0.3 ,21.7262,0.15,30.4134,0.15,
  185. 0.6,1,.5,2,0.45 ,4,0.4,7,0.35 ,8,0.3, 12,0.25 ,17,0.2,18,0.15,19,0.15,33.67,0.1 ,21.7262,0.15,30.4134,0.15,
  186. 1.0,1,.5,2,0.225,4,0.2,7,0.175,8,0.15,12,0.125,17,0.1,18,0.07,19,0.03,33.67,0.03,21.7262,0.0 ,30.4134,0.0};
  187. static double pianoenv[12] = {0.0, 1,
  188. 0.035,0.5,
  189. 0.07,0.25,
  190. 0.1, 0.125,
  191. 1.0, 0.0625,
  192. 3.0, 0};
  193. static double violin[100] = {0.0,1,1.0,2,0.707956,3,0.501184,4,0.141243,5,0.199535,6,0.177826,7,0.281860,8,0.281860,9,0.089114,10,0.223880,11,0.158485,12,0.141243,
  194. 0.07,1,1.0,2,0.707956,3,0.501184,4,0.141243,5,0.199535,6,0.177826,7,0.281860,8,0.281860,9,0.089114,10,0.223880,11,0.158485,12,0.141243,
  195. 0.09,1,1.000000,2,0.501202,3,0.251185,4,0.019950,5,0.039814,6,0.031622,7,0.079445,8,0.079445,9,0.007941,10,0.050122,11,0.025117,12,0.019950,
  196. 1.0, 1,1.000000,2,0.501202,3,0.251185,4,0.019950,5,0.039814,6,0.031622,7,0.079445,8,0.079445,9,0.007941,10,0.050122,11,0.025117,12,0.019950};
  197. static double violenv[12] = {0.0, 0,
  198. 0.07,1,
  199. 0.085,1,
  200. 0.125,1,
  201. 0.9, 1,
  202. 1.0, 0};
  203. static double cello[92] = {0.0,1,1,2,.5,3,.07,4,.33,5,0.09,6,0.09,7,0.11,8,0.1,9,0.1,10,0.045,11,0.02,
  204. 0.07,1,1,2,.5,3,.07,4,.33,5,0.09,6,0.09,7,0.11,8,0.1,9,0.1,10,0.045,11,0.02,
  205. 0.09,1,1,2,.5,3,.07,4,.33,5,0.09,6,0.09,7,0.11,8,0.1,9,0.1,10,0.045,11,0.02,
  206. 1.0, 1,1,2,.5,3,.07,4,.33,5,0.09,6,0.09,7,0.11,8,0.1,9,0.1,10,0.045,11,0.02};
  207. static double cellenv[12] = {0.0, 0,
  208. 0.07,1,
  209. 0.085,1,
  210. 0.125,1,
  211. 0.8, 1,
  212. 1.0, 0};
  213. char errstr[2400];
  214. //static int testing = 0;
  215. int anal_infiles = 1;
  216. int sloom = 0;
  217. int sloombatch = 0;
  218. const char* cdp_version = "7.0.0";
  219. //CDP LIB REPLACEMENTS
  220. static int setup_synthesizer_application(dataptr dz);
  221. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  222. static int setup_synthesis_param_ranges_and_defaults(dataptr dz);
  223. static int handle_the_outfile(char *cmdline,dataptr dz);
  224. static int open_the_outfile(dataptr dz);
  225. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  226. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  227. static int establish_application(dataptr dz);
  228. static int initialise_vflags(dataptr dz);
  229. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  230. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  231. static int mark_parameter_types(dataptr dz,aplptr ap);
  232. static int assign_file_data_storage(int infilecnt,dataptr dz);
  233. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  234. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  235. //static int get_the_mode_from_cmdline(char *str,dataptr dz);
  236. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  237. static int synthesis_param_preprocess(dataptr dz);
  238. static int score_synthesis(int inlinecnt,double onehzincr,synptr *orchestra,int flt_cnt,double flt_mul,dataptr dz);
  239. static void incr_sinptr(int n,double time,double onehzincr,double frq,double *partialfrq,dataptr dz);
  240. static double read_level(int n,double time,dataptr dz);
  241. static int create_synthesizer_sndbufs(dataptr dz);
  242. static int generate_packet_envelope (dataptr dz);
  243. static double read_packet_envelope(int kk,double incr,dataptr dz);
  244. static int modify_packet_envelope(double synth_ctr,double synth_sqz,dataptr dz);
  245. //static double get_frq_with_vibrato(int *init,int n,double onehzincr,double srate,dataptr dz);
  246. static int pretest_the_special_data(char *cmdline,int *inlinecnt,synptr *orchestra,dataptr dz);
  247. static int setup_the_special_data_for_given_instr(int instrno,double *data,synptr instrument,dataptr dz);
  248. static int setup_the_data_arrays(int linecnt,double synth_ctr,double synth_sqz,dataptr dz);
  249. static int impose_note_envelope(float *obuf,double *notenv,int envlen,int sampcnt,int linecnt,int notecnt,synptr instrument,dataptr dz);
  250. static void generate_vibrato_curves(double maxdepth, double notedur, dataptr dz);
  251. static int add_packet_vibrato(int *init,double onehzincr,double srate,int *sampcnt,int packet_dur,dataptr dz);
  252. static int read_instrument_name_from_start_of_line(char **p,char *insnam,int *insno,synptr *orchestra,int linecnt);
  253. static int note_synthesis(int totaloutsamps,double frq,double levl,double onehzincr,int linecnt,int notecnt,int envlen,int *sampcnt,
  254. synptr instrument,int instrno,int flt_cnt,double flt_mul,dataptr dz);
  255. static int retest_count_and_store_the_special_data(char *str,synptr *orchestra,int *flt_cnt,double *flt_mul,dataptr dz);
  256. static void pancalc(double position,double *leftgain,double *rightgain);
  257. static int initialise_instruments(synptr **orchestra);
  258. static int check_clustering(int insno,int chordcnt,int doublestop,char *insname,int time, int *midival);
  259. static int valid_cluster(int insno,double *linestor,int datacnt,synptr *orchestra);
  260. static int add_vibrato(double onehzincr,double srate,int outsampcnt,float *obuf,float *obuf2,dataptr dz);
  261. static int msetup_lphp_filter(int *flt_cnt,double *flt_mul,double passfrq,double stopfrq,dataptr dz);
  262. static int mdo_lphp_filter(int sampcnt,int flt_cnt,double flt_mul,dataptr dz);
  263. /**************************************** MAIN *********************************************/
  264. int main(int argc,char *argv[])
  265. {
  266. int exit_status, inlinecnt, flt_cnt = 0;
  267. dataptr dz = NULL;
  268. synptr *orchestra = NULL;
  269. char **cmdline;
  270. char indatafile[400];
  271. int cmdlinecnt;
  272. double synth_ctr = 0.0, synth_sqz = 0.0, flt_mul = 0.0;
  273. aplptr ap;
  274. int is_launched = FALSE;
  275. double onehzincr = (double)SYNTH_TABSIZE/(double)MSYNSRATE;
  276. srand(16);
  277. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  278. fprintf(stdout,"%s\n",cdp_version);
  279. fflush(stdout);
  280. return 0;
  281. }
  282. /* CHECK FOR SOUNDLOOM */
  283. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  284. sloom = 0;
  285. sloombatch = 1;
  286. }
  287. if(sflinit("cdp")){
  288. sfperror("cdp: initialisation\n");
  289. return(FAILED);
  290. }
  291. if((exit_status = initialise_instruments(&orchestra))<0) {
  292. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  293. return(FAILED);
  294. }
  295. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  296. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  297. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  298. return(FAILED);
  299. }
  300. if(!sloom) {
  301. if(argc == 1) {
  302. usage1();
  303. return(FAILED);
  304. } else if(argc == 2) {
  305. usage2(argv[1]);
  306. return(FAILED);
  307. }
  308. }
  309. if(!sloom) {
  310. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  311. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  312. return(FAILED);
  313. }
  314. cmdline = argv;
  315. cmdlinecnt = argc;
  316. if((get_the_process_no(argv[0],dz))<0)
  317. return(FAILED);
  318. cmdline++;
  319. cmdlinecnt--;
  320. dz->maxmode = 0;
  321. // setup_particular_application =
  322. if((exit_status = setup_synthesizer_application(dz))<0) {
  323. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  324. return(FAILED);
  325. }
  326. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  327. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  328. return(FAILED);
  329. }
  330. } else {
  331. //parse_TK_data() =
  332. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  333. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  334. return(exit_status);
  335. }
  336. }
  337. ap = dz->application;
  338. // parse_infile_and_hone_type() =
  339. // setup_param_ranges_and_defaults() =
  340. if((exit_status = setup_synthesis_param_ranges_and_defaults(dz))<0) {
  341. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  342. return(FAILED);
  343. }
  344. // open_first_infile() : redundant
  345. // handle_extra_infiles() : redundant
  346. // handle_outfile() =
  347. if((exit_status = handle_the_outfile(cmdline[0],dz))<0) {
  348. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  349. return(FAILED);
  350. }
  351. cmdlinecnt--;
  352. cmdline++;
  353. // handle_formants() redundant
  354. // handle_formant_quiksearch() redundant
  355. // handle_special_data()
  356. if((exit_status = pretest_the_special_data(cmdline[0],&inlinecnt,orchestra,dz))<0) {
  357. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  358. return(FAILED);
  359. }
  360. strcpy(indatafile,cmdline[0]);
  361. cmdlinecnt--;
  362. cmdline++;
  363. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  364. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  365. return(FAILED);
  366. }
  367. if((exit_status = synthesis_param_preprocess(dz))<0) {
  368. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  369. return(FAILED);
  370. }
  371. // check_param_validity_and_consistency() redundant
  372. if((exit_status = setup_the_data_arrays(inlinecnt,synth_ctr,synth_sqz,dz))<0) {
  373. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  374. return(FAILED);
  375. }
  376. if((exit_status = retest_count_and_store_the_special_data(indatafile,orchestra,&flt_cnt,&flt_mul,dz))<0) {
  377. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  378. return(FAILED);
  379. }
  380. is_launched = TRUE;
  381. if((exit_status = create_synthesizer_sndbufs(dz))<0) { // CDP LIB
  382. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  383. return(FAILED);
  384. }
  385. // Must come AFTER the special_data function
  386. if((exit_status = open_the_outfile(dz))<0) {
  387. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  388. return(FAILED);
  389. }
  390. //process_file =
  391. if((exit_status = score_synthesis(inlinecnt,onehzincr,orchestra,flt_cnt,flt_mul,dz))<0) {
  392. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  393. return(FAILED);
  394. }
  395. if((exit_status = complete_output(dz))<0) { // CDP LIB
  396. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  397. return(FAILED);
  398. }
  399. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  400. free(dz);
  401. return(SUCCEEDED);
  402. }
  403. /**********************************************
  404. REPLACED CDP LIB FUNCTIONS
  405. **********************************************/
  406. /****************************** SET_PARAM_DATA *********************************/
  407. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  408. {
  409. ap->special_data = (char)special_data;
  410. ap->param_cnt = (char)paramcnt;
  411. ap->max_param_cnt = (char)maxparamcnt;
  412. if(ap->max_param_cnt>0) {
  413. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  414. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  415. return(MEMORY_ERROR);
  416. }
  417. strcpy(ap->param_list,paramlist);
  418. }
  419. return(FINISHED);
  420. }
  421. /****************************** SET_VFLGS *********************************/
  422. int set_vflgs
  423. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  424. {
  425. ap->option_cnt = (char) optcnt; /*RWD added cast */
  426. if(optcnt) {
  427. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  428. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  429. return(MEMORY_ERROR);
  430. }
  431. strcpy(ap->option_list,optlist);
  432. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  433. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  434. return(MEMORY_ERROR);
  435. }
  436. strcpy(ap->option_flags,optflags);
  437. }
  438. ap->vflag_cnt = (char) vflagcnt;
  439. ap->variant_param_cnt = (char) vparamcnt;
  440. if(vflagcnt) {
  441. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  442. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  443. return(MEMORY_ERROR);
  444. }
  445. strcpy(ap->variant_list,varlist);
  446. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  447. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  448. return(MEMORY_ERROR);
  449. }
  450. strcpy(ap->variant_flags,varflags);
  451. }
  452. return(FINISHED);
  453. }
  454. /***************************** APPLICATION_INIT **************************/
  455. int application_init(dataptr dz)
  456. {
  457. int exit_status;
  458. int storage_cnt;
  459. int tipc, brkcnt;
  460. aplptr ap = dz->application;
  461. if(ap->vflag_cnt>0)
  462. initialise_vflags(dz);
  463. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  464. ap->total_input_param_cnt = (char)tipc;
  465. if(tipc>0) {
  466. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  467. return(exit_status);
  468. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  469. return(exit_status);
  470. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  471. return(exit_status);
  472. }
  473. brkcnt = tipc;
  474. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  475. if(brkcnt>0) {
  476. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  477. return(exit_status);
  478. }
  479. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  480. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  481. return(exit_status);
  482. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  483. return(exit_status);
  484. }
  485. if((exit_status = mark_parameter_types(dz,ap))<0)
  486. return(exit_status);
  487. // establish_infile_constants() replaced by
  488. dz->infilecnt = 1;
  489. //establish_bufptrs_and_extra_buffers():
  490. return(FINISHED);
  491. }
  492. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  493. /* RWD mallo changed to calloc; helps debug verison run as release! */
  494. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  495. {
  496. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  497. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  498. return(MEMORY_ERROR);
  499. }
  500. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  501. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  502. return(MEMORY_ERROR);
  503. }
  504. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  505. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  506. return(MEMORY_ERROR);
  507. }
  508. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  509. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  510. return(MEMORY_ERROR);
  511. }
  512. return(FINISHED);
  513. }
  514. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  515. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  516. {
  517. int n;
  518. for(n=0;n<storage_cnt;n++) {
  519. dz->is_int[n] = (char)0;
  520. dz->no_brk[n] = (char)0;
  521. }
  522. return(FINISHED);
  523. }
  524. /***************************** MARK_PARAMETER_TYPES **************************/
  525. int mark_parameter_types(dataptr dz,aplptr ap)
  526. {
  527. int n, m; /* PARAMS */
  528. for(n=0;n<ap->max_param_cnt;n++) {
  529. switch(ap->param_list[n]) {
  530. case('0'): break; /* dz->is_active[n] = 0 is default */
  531. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  532. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  533. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  534. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  535. default:
  536. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  537. return(PROGRAM_ERROR);
  538. }
  539. } /* OPTIONS */
  540. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  541. switch(ap->option_list[n]) {
  542. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  543. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  544. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  545. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  546. default:
  547. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  548. return(PROGRAM_ERROR);
  549. }
  550. } /* VARIANTS */
  551. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  552. switch(ap->variant_list[n]) {
  553. case('0'): break;
  554. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  555. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  556. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  557. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  558. default:
  559. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  560. return(PROGRAM_ERROR);
  561. }
  562. } /* INTERNAL */
  563. for(n=0,
  564. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  565. switch(ap->internal_param_list[n]) {
  566. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  567. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  568. case('d'): dz->no_brk[m] = (char)1; break;
  569. default:
  570. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  571. return(PROGRAM_ERROR);
  572. }
  573. }
  574. return(FINISHED);
  575. }
  576. /************************ HANDLE_THE_OUTFILE *********************/
  577. int handle_the_outfile(char *str,dataptr dz)
  578. {
  579. char filename[2000];
  580. char *p,*q;
  581. strcpy(filename,str);
  582. if(!sloom) {
  583. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  584. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  585. return(DATA_ERROR);
  586. }
  587. }
  588. q = filename;
  589. p = filename; // Drop file extension
  590. while(*p != ENDOFSTR) {
  591. if(*p == '.') {
  592. *p = ENDOFSTR;
  593. break;
  594. }
  595. p++;
  596. }
  597. p--;
  598. while(p != filename) { // Drop file path
  599. if(*p == '/') {
  600. q = p+1;
  601. break;
  602. }
  603. p--;
  604. }
  605. strcpy(dz->outfilename,q);
  606. return(FINISHED);
  607. }
  608. /************************ OPEN_THE_OUTFILE *********************/
  609. int open_the_outfile(dataptr dz)
  610. {
  611. int exit_status;
  612. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  613. return(exit_status);
  614. return(FINISHED);
  615. }
  616. /***************************** ESTABLISH_APPLICATION **************************/
  617. int establish_application(dataptr dz)
  618. {
  619. aplptr ap;
  620. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  621. sprintf(errstr,"establish_application()\n");
  622. return(MEMORY_ERROR);
  623. }
  624. ap = dz->application;
  625. memset((char *)ap,0,sizeof(struct applic));
  626. return(FINISHED);
  627. }
  628. /************************* INITIALISE_VFLAGS *************************/
  629. int initialise_vflags(dataptr dz)
  630. {
  631. int n;
  632. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  633. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  634. return(MEMORY_ERROR);
  635. }
  636. for(n=0;n<dz->application->vflag_cnt;n++)
  637. dz->vflag[n] = FALSE;
  638. return FINISHED;
  639. }
  640. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  641. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  642. {
  643. int n;
  644. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  645. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  646. return(MEMORY_ERROR);
  647. }
  648. for(n=0;n<tipc;n++)
  649. ap->default_val[n] = 0.0;
  650. return(FINISHED);
  651. }
  652. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  653. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  654. {
  655. int n;
  656. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  657. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  658. return(MEMORY_ERROR);
  659. }
  660. for(n=0;n<tipc;n++)
  661. dz->is_active[n] = (char)0;
  662. return(FINISHED);
  663. }
  664. /************************* SETUP_SYNTHESIZER_APPLICATION *******************/
  665. int setup_synthesizer_application(dataptr dz)
  666. {
  667. int exit_status;
  668. aplptr ap;
  669. if((exit_status = establish_application(dz))<0) // GLOBAL
  670. return(FAILED);
  671. ap = dz->application;
  672. // SEE parstruct FOR EXPLANATION of next 2 functions
  673. if((exit_status = set_param_data(ap,SCOREDATA,1,1,"dD"))<0)
  674. return(FAILED);
  675. if((exit_status = set_vflgs(ap,"jo",2,"di","b",1,0,"0"))<0)
  676. return(exit_status);
  677. // set_legal_infile_structure -->
  678. dz->has_otherfile = FALSE;
  679. // assign_process_logic -->
  680. dz->input_data_type = NO_FILE_AT_ALL;
  681. dz->process_type = UNEQUAL_SNDFILE;
  682. dz->outfiletype = SNDFILE_OUT;
  683. return application_init(dz); //GLOBAL
  684. }
  685. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  686. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  687. {
  688. int exit_status;
  689. infileptr infile_info;
  690. if(!sloom) {
  691. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  692. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  693. return(MEMORY_ERROR);
  694. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  695. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  696. return(PROGRAM_ERROR);
  697. } else if(infile_info->filetype != SNDFILE) {
  698. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  699. return(DATA_ERROR);
  700. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  701. sprintf(errstr,"Failed to copy file parsing information\n");
  702. return(PROGRAM_ERROR);
  703. }
  704. free(infile_info);
  705. }
  706. return(FINISHED);
  707. }
  708. /************************* SETUP_SYNTHESIS_PARAM_RANGES_AND_DEFAULTS *******************/
  709. int setup_synthesis_param_ranges_and_defaults(dataptr dz)
  710. {
  711. int exit_status;
  712. aplptr ap = dz->application;
  713. // set_param_ranges()
  714. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  715. // NB total_input_param_cnt is > 0 !!!
  716. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  717. return(FAILED);
  718. // get_param_ranges()
  719. ap->lo[MS_MM] = 30.0;
  720. ap->hi[MS_MM] = 500.0;
  721. ap->default_val[MS_MM] = 60.0;
  722. ap->lo[MS_MAXJIT] = 0;
  723. ap->hi[MS_MAXJIT] = 20;
  724. ap->default_val[MS_MAXJIT] = 15;
  725. ap->lo[MS_OCHANS] = 2;
  726. ap->hi[MS_OCHANS] = 8;
  727. ap->default_val[MS_OCHANS] = 2;
  728. dz->maxmode = 0;
  729. if(!sloom)
  730. put_default_vals_in_all_params(dz);
  731. return(FINISHED);
  732. }
  733. /********************************* PARSE_SLOOM_DATA *********************************/
  734. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  735. {
  736. int exit_status;
  737. int cnt = 1, infilecnt;
  738. int filesize, insams, inbrksize;
  739. double dummy;
  740. int true_cnt = 0;
  741. aplptr ap;
  742. while(cnt<=PRE_CMDLINE_DATACNT) {
  743. if(cnt > argc) {
  744. sprintf(errstr,"Insufficient data sent from TK\n");
  745. return(DATA_ERROR);
  746. }
  747. switch(cnt) {
  748. case(1):
  749. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  750. sprintf(errstr,"Cannot read process no. sent from TK\n");
  751. return(DATA_ERROR);
  752. }
  753. break;
  754. case(2):
  755. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  756. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  757. return(DATA_ERROR);
  758. }
  759. if(dz->mode > 0)
  760. dz->mode--;
  761. //setup_particular_application() =
  762. if((exit_status = setup_synthesizer_application(dz))<0)
  763. return(exit_status);
  764. ap = dz->application;
  765. break;
  766. case(3):
  767. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  768. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  769. return(DATA_ERROR);
  770. }
  771. if(infilecnt < 1) {
  772. true_cnt = cnt + 1;
  773. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  774. }
  775. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  776. return(exit_status);
  777. break;
  778. case(INPUT_FILETYPE+4):
  779. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  780. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  781. return(DATA_ERROR);
  782. }
  783. break;
  784. case(INPUT_FILESIZE+4):
  785. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  786. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  787. return(DATA_ERROR);
  788. }
  789. dz->insams[0] = filesize;
  790. break;
  791. case(INPUT_INSAMS+4):
  792. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  793. sprintf(errstr,"Cannot read insams sent from TK\n");
  794. return(DATA_ERROR);
  795. }
  796. dz->insams[0] = insams;
  797. break;
  798. case(INPUT_SRATE+4):
  799. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  800. sprintf(errstr,"Cannot read srate sent from TK\n");
  801. return(DATA_ERROR);
  802. }
  803. break;
  804. case(INPUT_CHANNELS+4):
  805. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  806. sprintf(errstr,"Cannot read channels sent from TK\n");
  807. return(DATA_ERROR);
  808. }
  809. break;
  810. case(INPUT_STYPE+4):
  811. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  812. sprintf(errstr,"Cannot read stype sent from TK\n");
  813. return(DATA_ERROR);
  814. }
  815. break;
  816. case(INPUT_ORIGSTYPE+4):
  817. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  818. sprintf(errstr,"Cannot read origstype sent from TK\n");
  819. return(DATA_ERROR);
  820. }
  821. break;
  822. case(INPUT_ORIGRATE+4):
  823. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  824. sprintf(errstr,"Cannot read origrate sent from TK\n");
  825. return(DATA_ERROR);
  826. }
  827. break;
  828. case(INPUT_MLEN+4):
  829. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  830. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  831. return(DATA_ERROR);
  832. }
  833. break;
  834. case(INPUT_DFAC+4):
  835. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  836. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  837. return(DATA_ERROR);
  838. }
  839. break;
  840. case(INPUT_ORIGCHANS+4):
  841. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  842. sprintf(errstr,"Cannot read origchans sent from TK\n");
  843. return(DATA_ERROR);
  844. }
  845. break;
  846. case(INPUT_SPECENVCNT+4):
  847. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  848. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  849. return(DATA_ERROR);
  850. }
  851. dz->specenvcnt = dz->infile->specenvcnt;
  852. break;
  853. case(INPUT_WANTED+4):
  854. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  855. sprintf(errstr,"Cannot read wanted sent from TK\n");
  856. return(DATA_ERROR);
  857. }
  858. break;
  859. case(INPUT_WLENGTH+4):
  860. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  861. sprintf(errstr,"Cannot read wlength sent from TK\n");
  862. return(DATA_ERROR);
  863. }
  864. break;
  865. case(INPUT_OUT_CHANS+4):
  866. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  867. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  868. return(DATA_ERROR);
  869. }
  870. break;
  871. /* RWD these chanegs to samps - tk will have to deal with that! */
  872. case(INPUT_DESCRIPTOR_BYTES+4):
  873. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  874. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  875. return(DATA_ERROR);
  876. }
  877. break;
  878. case(INPUT_IS_TRANSPOS+4):
  879. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  880. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  881. return(DATA_ERROR);
  882. }
  883. break;
  884. case(INPUT_COULD_BE_TRANSPOS+4):
  885. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  886. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  887. return(DATA_ERROR);
  888. }
  889. break;
  890. case(INPUT_COULD_BE_PITCH+4):
  891. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  892. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  893. return(DATA_ERROR);
  894. }
  895. break;
  896. case(INPUT_DIFFERENT_SRATES+4):
  897. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  898. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  899. return(DATA_ERROR);
  900. }
  901. break;
  902. case(INPUT_DUPLICATE_SNDS+4):
  903. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  904. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  905. return(DATA_ERROR);
  906. }
  907. break;
  908. case(INPUT_BRKSIZE+4):
  909. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  910. sprintf(errstr,"Cannot read brksize sent from TK\n");
  911. return(DATA_ERROR);
  912. }
  913. if(inbrksize > 0) {
  914. switch(dz->input_data_type) {
  915. case(WORDLIST_ONLY):
  916. break;
  917. case(PITCH_AND_PITCH):
  918. case(PITCH_AND_TRANSPOS):
  919. case(TRANSPOS_AND_TRANSPOS):
  920. dz->tempsize = inbrksize;
  921. break;
  922. case(BRKFILES_ONLY):
  923. case(UNRANGED_BRKFILE_ONLY):
  924. case(DB_BRKFILES_ONLY):
  925. case(ALL_FILES):
  926. case(ANY_NUMBER_OF_ANY_FILES):
  927. if(dz->extrabrkno < 0) {
  928. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  929. return(DATA_ERROR);
  930. }
  931. if(dz->brksize == NULL) {
  932. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  933. return(PROGRAM_ERROR);
  934. }
  935. dz->brksize[dz->extrabrkno] = inbrksize;
  936. break;
  937. default:
  938. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  939. dz->input_data_type);
  940. return(PROGRAM_ERROR);
  941. }
  942. break;
  943. }
  944. break;
  945. case(INPUT_NUMSIZE+4):
  946. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  947. sprintf(errstr,"Cannot read numsize sent from TK\n");
  948. return(DATA_ERROR);
  949. }
  950. break;
  951. case(INPUT_LINECNT+4):
  952. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  953. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  954. return(DATA_ERROR);
  955. }
  956. break;
  957. case(INPUT_ALL_WORDS+4):
  958. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  959. sprintf(errstr,"Cannot read all_words sent from TK\n");
  960. return(DATA_ERROR);
  961. }
  962. break;
  963. case(INPUT_ARATE+4):
  964. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  965. sprintf(errstr,"Cannot read arate sent from TK\n");
  966. return(DATA_ERROR);
  967. }
  968. break;
  969. case(INPUT_FRAMETIME+4):
  970. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  971. sprintf(errstr,"Cannot read frametime sent from TK\n");
  972. return(DATA_ERROR);
  973. }
  974. dz->frametime = (float)dummy;
  975. break;
  976. case(INPUT_WINDOW_SIZE+4):
  977. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  978. sprintf(errstr,"Cannot read window_size sent from TK\n");
  979. return(DATA_ERROR);
  980. }
  981. break;
  982. case(INPUT_NYQUIST+4):
  983. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  984. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  985. return(DATA_ERROR);
  986. }
  987. break;
  988. case(INPUT_DURATION+4):
  989. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  990. sprintf(errstr,"Cannot read duration sent from TK\n");
  991. return(DATA_ERROR);
  992. }
  993. break;
  994. case(INPUT_MINBRK+4):
  995. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  996. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  997. return(DATA_ERROR);
  998. }
  999. break;
  1000. case(INPUT_MAXBRK+4):
  1001. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  1002. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  1003. return(DATA_ERROR);
  1004. }
  1005. break;
  1006. case(INPUT_MINNUM+4):
  1007. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  1008. sprintf(errstr,"Cannot read minnum sent from TK\n");
  1009. return(DATA_ERROR);
  1010. }
  1011. break;
  1012. case(INPUT_MAXNUM+4):
  1013. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  1014. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  1015. return(DATA_ERROR);
  1016. }
  1017. break;
  1018. default:
  1019. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  1020. return(PROGRAM_ERROR);
  1021. }
  1022. cnt++;
  1023. }
  1024. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  1025. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  1026. return(DATA_ERROR);
  1027. }
  1028. if(true_cnt)
  1029. cnt = true_cnt;
  1030. *cmdlinecnt = 0;
  1031. while(cnt < argc) {
  1032. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  1033. return(exit_status);
  1034. cnt++;
  1035. }
  1036. return(FINISHED);
  1037. }
  1038. /********************************* GET_TK_CMDLINE_WORD *********************************/
  1039. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  1040. {
  1041. if(*cmdlinecnt==0) {
  1042. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  1043. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1044. return(MEMORY_ERROR);
  1045. }
  1046. } else {
  1047. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  1048. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1049. return(MEMORY_ERROR);
  1050. }
  1051. }
  1052. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1053. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  1054. return(MEMORY_ERROR);
  1055. }
  1056. strcpy((*cmdline)[*cmdlinecnt],q);
  1057. (*cmdlinecnt)++;
  1058. return(FINISHED);
  1059. }
  1060. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1061. int assign_file_data_storage(int infilecnt,dataptr dz)
  1062. {
  1063. int exit_status;
  1064. int no_sndfile_system_files = FALSE;
  1065. dz->infilecnt = infilecnt;
  1066. if((exit_status = allocate_filespace(dz))<0)
  1067. return(exit_status);
  1068. if(no_sndfile_system_files)
  1069. dz->infilecnt = 0;
  1070. return(FINISHED);
  1071. }
  1072. /************************* redundant functions: to ensure libs compile OK *******************/
  1073. int assign_process_logic(dataptr dz)
  1074. {
  1075. return(FINISHED);
  1076. }
  1077. void set_legal_infile_structure(dataptr dz)
  1078. {}
  1079. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1080. {
  1081. return(FINISHED);
  1082. }
  1083. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1084. {
  1085. return(FINISHED);
  1086. }
  1087. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1088. {
  1089. return(FINISHED);
  1090. }
  1091. int read_special_data(char *str,dataptr dz)
  1092. {
  1093. return(FINISHED);
  1094. }
  1095. int inner_loop
  1096. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1097. {
  1098. return(FINISHED);
  1099. }
  1100. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1101. {
  1102. return(FINISHED);
  1103. }
  1104. /******************************** USAGE1 ********************************/
  1105. int usage1(void)
  1106. {
  1107. usage2("synth");
  1108. return(USAGE_ONLY);
  1109. }
  1110. /********************************************************************************************/
  1111. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1112. {
  1113. if(!strcmp(prog_identifier_from_cmdline,"synth")) dz->process = MULTISYN;
  1114. else {
  1115. fprintf(stderr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1116. return(USAGE_ONLY);
  1117. }
  1118. return(FINISHED);
  1119. }
  1120. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  1121. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  1122. {
  1123. int n;
  1124. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1125. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  1126. return(MEMORY_ERROR);
  1127. }
  1128. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1129. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  1130. return(MEMORY_ERROR);
  1131. }
  1132. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1133. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  1134. return(MEMORY_ERROR);
  1135. }
  1136. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1137. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  1138. return(MEMORY_ERROR);
  1139. }
  1140. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1141. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  1142. return(MEMORY_ERROR);
  1143. }
  1144. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1145. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  1146. return(MEMORY_ERROR);
  1147. }
  1148. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1149. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  1150. return(MEMORY_ERROR);
  1151. }
  1152. for(n=0;n<brkcnt;n++) {
  1153. dz->brk[n] = NULL;
  1154. dz->brkptr[n] = NULL;
  1155. dz->brkinit[n] = 0;
  1156. dz->brksize[n] = 0;
  1157. }
  1158. return(FINISHED);
  1159. }
  1160. /******************************** USAGE2 ********************************/
  1161. int usage2(char *str)
  1162. {
  1163. if(!strcmp(str,"synth")) {
  1164. fprintf(stderr,
  1165. "USAGE:\n"
  1166. "multisynth synth outfile score MM [-jjitter] [-oochans] [-b]\n"
  1167. "\n"
  1168. "Synthesize several sound-streams from a score.\n"
  1169. "\n"
  1170. "SCORE Text file with each line in the form:.\n"
  1171. " \"Insname\" followed by any number of sets-of-4-values representing\n"
  1172. " \"Time\" \"Pitch\" \"Loudness\" and \"Duration\".\n"
  1173. " e.g. (with two 4-sets) \"trumpet 0 62 .7 2 8 64 .9 12\"\n"
  1174. "\n"
  1175. "Insname name of instrument: one of the following.\n"
  1176. " \"flute\" \"clarinet\" \"trumpet\" \"violin\" \"cello\"\n"
  1177. " \"pianoRH\" \"pianoLH\" (LH must immediately follow RH in score file).\n"
  1178. "\n"
  1179. "Time Note onset time, measured in thirds-of-semiquavers from time 0 (integer).\n"
  1180. " \"Cello\" & \"violin\" & and \"piano\" (LH and RH)\n"
  1181. " may play up to 4 different notes simultaneously,\n"
  1182. " so long as the note-combinations are possible on the real instrument.\n"
  1183. "\n"
  1184. "Pitch Pitch of the note, as a MIDI value (integer).\n"
  1185. "Loudness A positive number no bigger than 1.\n"
  1186. "Duration Duration of the note, measured in thirds-of-semiquavers (integer).\n"
  1187. " \"Piano\" notes may overlay one another (sustained resonance)\n"
  1188. " but other instruments may not, so their duration values must not\n"
  1189. " create a note sustained beyond the start of the following note.\n"
  1190. " Simultaneously sounding notes must have the same duration.\n"
  1191. "\n"
  1192. "MM Metronome mark determining tempo of sound output.\n"
  1193. "JITTER Max random divergence from regular note placement in mS (default 15).\n"
  1194. "OCHANS Number of output channels (2-8): default stereo.\n"
  1195. " The sounds in successive lines will be spaced in the output-space\n"
  1196. " from line-1, at the far left, to the last-line, at far right.\n"
  1197. " (single-line score output will be placed at front centre).\n"
  1198. "\n"
  1199. "-b If \"ochans\" is greater than stereo, lspkr array assumed to be surround.\n"
  1200. " To force a bounded array (with a left extreme and right extreme)\n"
  1201. " use the \"-b\" flag.\n"
  1202. " For surround, outputs numbered in order clockwise from front-centre.\n"
  1203. " For bounded, outputs numbered in order from far-left to far-right.\n"
  1204. "\n");
  1205. } else
  1206. fprintf(stdout,"Unknown option '%s'\n",str);
  1207. return(USAGE_ONLY);
  1208. }
  1209. int usage3(char *str1,char *str2)
  1210. {
  1211. fprintf(stderr,"Insufficient parameters on command line.\n");
  1212. return(USAGE_ONLY);
  1213. }
  1214. /**************************** SYNTHESIS_PARAM_PREPROCESS *************************/
  1215. int synthesis_param_preprocess (dataptr dz)
  1216. {
  1217. dz->infile->channels = dz->iparam[MS_OCHANS];
  1218. dz->infile->srate = MSYNSRATE;
  1219. dz->nyquist = (double)MSYNSRATE/2.0;
  1220. // Duration of thirds of semiquavers
  1221. dz->timeconvert = (60.0/dz->param[MS_MM])/12.0;
  1222. dz->param[MS_MAXJIT] *= MS_TO_SECS;
  1223. return(FINISHED);
  1224. }
  1225. /******************************** SCORE_SYNTHESIS *********************************/
  1226. #define LEFT 0
  1227. #define RITE 1
  1228. int score_synthesis(int inlinecnt,double onehzincr,synptr *orchestra,int flt_cnt,double flt_mul,dataptr dz)
  1229. {
  1230. int exit_status, linecnt, m, typ, instrno, notecnt, pos, envlen, outchans = dz->iparam[MS_OCHANS], leftchan, ritechan, packet_type;
  1231. double *data, *env = dz->parray[dz->ins_envel], *ochanpos = dz->parray[dz->ochan_pos];
  1232. double srate = (double)MSYNSRATE, normaliser = 0.0, synth_ctr = 1.0, synth_sqz = 0.0, changain[2], jitter;
  1233. double time, frq = 0.0, levl = 1.0, dur = 0.0, maxval;
  1234. float *obuf = dz->sampbuf[0], *notebuf = dz->sampbuf[1];
  1235. unsigned int startsamp = 0, totaloutsamps = 0, outsampcnt;
  1236. int notesamps = 0, sampcnt, n;
  1237. synptr instrument;
  1238. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1239. if(!sloom)
  1240. fprintf(stdout,"INFO: First Score-pass. ");
  1241. for(linecnt = 0, pos = 0; linecnt < inlinecnt; linecnt++,pos +=3) {
  1242. leftchan = (int)round(ochanpos[pos]);
  1243. ritechan = (leftchan + 1) % outchans;
  1244. changain[LEFT] = ochanpos[pos+1];
  1245. changain[RITE] = ochanpos[pos+2];
  1246. notecnt = 0;
  1247. data = dz->parray[dz->scoredata + linecnt];
  1248. instrno = (int)round(data[INS_NO]);
  1249. instrument = orchestra[instrno]; // Point to relevant instrument-structure
  1250. data++; // Move into note data
  1251. if((exit_status = setup_the_special_data_for_given_instr(instrno,data,instrument,dz))<0)
  1252. return(exit_status);
  1253. packet_type = instrument->packettype;
  1254. if(packet_type) {
  1255. synth_sqz = instrument->squeeze; // Centring of peak of packet envelope.
  1256. synth_ctr = instrument->centre; // Narrowing of packet envelope.
  1257. if((exit_status = modify_packet_envelope(synth_ctr,synth_sqz,dz))<0)
  1258. return(exit_status);
  1259. }
  1260. m = 0;
  1261. while(data[m] >= 0) {
  1262. typ = m % 4;
  1263. switch(typ) {
  1264. case(NOTE_TIME):
  1265. time = data[m] * dz->timeconvert;
  1266. if(time > 0.0 && dz->param[MS_MAXJIT] > 0.0) {
  1267. jitter = drand48() * 2.0;
  1268. jitter -= 1.0;
  1269. jitter *= dz->param[MS_MAXJIT];
  1270. time += jitter;
  1271. time = max(time,0.0);
  1272. }
  1273. startsamp = (int)round(time * srate) * outchans; // Position of note in total output
  1274. break;
  1275. case(NOTE_PICH):
  1276. frq = miditohz(data[m]);
  1277. break;
  1278. case(NOTE_LEVL):
  1279. levl = data[m];
  1280. break;
  1281. case(NOTE_DUR):
  1282. dur = data[m] * dz->timeconvert;
  1283. if(instrument->overlap) // If "overlap" allow note to decay completely
  1284. dur = max(dur,instrument->env[instrument->env_len - 2]);
  1285. notesamps = (int)round(dur * srate); // Length of note in (MONO) note-synth buf
  1286. break;
  1287. }
  1288. m++;
  1289. if(m%4 == 0) { // Once a whole note-data-set (4 items) is gathered
  1290. notecnt++;
  1291. envlen = instrument->env_len; // (envlen modifiable later, if note is very short)
  1292. if(instrument->has_vibrato)
  1293. generate_vibrato_curves(instrument->maxvibdepth,dur,dz);
  1294. for(n=0;n < envlen;n++) // copy complete instrument envelope template into note-envelope array
  1295. env[n] = instrument->env[n]; // Get note envelope (this will be modified when true duration of note is known)
  1296. if((exit_status = note_synthesis(notesamps,frq,levl,onehzincr,linecnt,notecnt,envlen,&sampcnt,instrument,instrno,flt_cnt,flt_mul,dz))<0)
  1297. return(exit_status);
  1298. for(n = 0,outsampcnt = startsamp;n < sampcnt;n++,outsampcnt+=outchans) {
  1299. obuf[outsampcnt+leftchan] = (float)(obuf[outsampcnt+leftchan] + (notebuf[n] * levl * changain[LEFT]));
  1300. obuf[outsampcnt+ritechan] = (float)(obuf[outsampcnt+ritechan] + (notebuf[n] * levl * changain[RITE]));
  1301. }
  1302. totaloutsamps = max(totaloutsamps,outsampcnt);
  1303. }
  1304. }
  1305. }
  1306. maxval = 0.0;
  1307. for(outsampcnt=0;outsampcnt<totaloutsamps;outsampcnt++)
  1308. maxval = max(maxval,fabs(obuf[outsampcnt]));
  1309. normaliser = 0.95/maxval;
  1310. if(!sloom) {
  1311. fprintf(stdout,"\nNormalising output.\n");
  1312. fflush(stdout);
  1313. }
  1314. for(outsampcnt=0;outsampcnt<totaloutsamps;outsampcnt++)
  1315. obuf[outsampcnt] = (float)(obuf[outsampcnt] * normaliser);
  1316. if((exit_status = write_samps(obuf,totaloutsamps,dz))<0)
  1317. return(exit_status);
  1318. return FINISHED;
  1319. }
  1320. /******************************** NOTE_SYNTHESIS *********************************/
  1321. int note_synthesis(int totaloutsamps,double frq,double levl,double onehzincr,int linecnt,int notecnt,int envlen,int *sampcnt,synptr instrument,int instrno,
  1322. int flt_cnt,double flt_mul,dataptr dz)
  1323. {
  1324. int exit_status, n;
  1325. int loindex, hiindex, packet_dur = 0, kk, total_outsamps;
  1326. double loval, hival, valdiff, timefrac, val, level, maxval = 0.0, packet_incr = 0, envv, passfrq, stopfrq;
  1327. float *obuf = dz->sampbuf[1], *obuf2 = dz->sampbuf[2];
  1328. double srate = (double)dz->infile->srate;
  1329. double *sintab = dz->parray[dz->sinarray], *sinptr = dz->parray[dz->pntarray], *notenv = dz->parray[dz->ins_envel];
  1330. double time = 0.0, normaliser, partialfrq = HUGE;
  1331. int packet_phase = 1, partialcnt;
  1332. int init, packet_type;
  1333. packet_type = instrument->packettype;
  1334. if(packet_type) {
  1335. packet_dur = (int)round((1.0/frq) * srate);
  1336. packet_incr = (double)TREMOLO_TABSIZE/(double)(packet_dur - 1); // Forces last read to be at end of packet envelope (zero)
  1337. }
  1338. if(!sloom)
  1339. fprintf(stdout,"*");
  1340. *sampcnt = 0;
  1341. init = 1;
  1342. memset((char *)obuf,0,dz->max_notedur * sizeof(float));
  1343. memset((char *)obuf2,0,dz->max_notedur * sizeof(float));
  1344. for(n=0;n<instrument->partial_cnt;n++) {
  1345. partialfrq = frq * dz->parray[n][1]; // Get initial partial-frq
  1346. if(partialfrq >= dz->nyquist) // Ignore any partials beyond the nyquist
  1347. break;
  1348. }
  1349. partialcnt = n;
  1350. if(packet_type) {
  1351. while(*sampcnt < totaloutsamps) {
  1352. for(n=0;n<partialcnt;n++)
  1353. sinptr[n] = 0.0;
  1354. for(kk = 0; kk<packet_dur;kk++) {
  1355. time = (double)(*sampcnt)/srate;
  1356. for(n=0;n<partialcnt;n++) {
  1357. loindex = (int)floor(sinptr[n]);
  1358. hiindex = loindex + 1;
  1359. loval = sintab[loindex];
  1360. hival = sintab[hiindex];
  1361. valdiff = hival - loval;
  1362. timefrac = sinptr[n] - (double)loindex;
  1363. val = loval + (valdiff * timefrac);
  1364. level = read_level(n,time,dz);
  1365. val *= level;
  1366. obuf[*sampcnt] = (float)(obuf[*sampcnt] + val);
  1367. incr_sinptr(n,time,onehzincr,frq,&partialfrq,dz);
  1368. }
  1369. envv = read_packet_envelope(kk,packet_incr,dz);
  1370. obuf[*sampcnt] = (float)(obuf[*sampcnt] * envv * packet_phase);
  1371. (*sampcnt)++;
  1372. }
  1373. if(instrument->has_vibrato)
  1374. add_packet_vibrato(&init,onehzincr,srate,sampcnt,packet_dur,dz);
  1375. packet_phase = -packet_phase;
  1376. }
  1377. for(kk=0;kk<*sampcnt;kk++)
  1378. maxval = max(maxval,fabs(obuf[kk]));
  1379. } else {
  1380. for(n=0;n<partialcnt;n++)
  1381. sinptr[n] = 0.0;
  1382. if(instrument->has_vibrato)
  1383. total_outsamps = (int)round(totaloutsamps * VIBSHIFT); // Generate more than required, to allow for vibrato
  1384. else
  1385. total_outsamps = totaloutsamps;
  1386. for(kk = 0; kk<total_outsamps;kk++) {
  1387. time = (double)kk/srate;
  1388. for(n=0;n<partialcnt;n++) {
  1389. loindex = (int)floor(sinptr[n]);
  1390. hiindex = loindex + 1;
  1391. loval = sintab[loindex];
  1392. hival = sintab[hiindex];
  1393. valdiff = hival - loval;
  1394. timefrac = sinptr[n] - (double)loindex;
  1395. val = loval + (valdiff * timefrac);
  1396. level = read_level(n,time,dz);
  1397. val *= level;
  1398. obuf[kk] = (float)(obuf[kk] + val);
  1399. incr_sinptr(n,time,onehzincr,frq,&partialfrq,dz);
  1400. }
  1401. }
  1402. if(instrument->has_vibrato)
  1403. add_vibrato(onehzincr,srate,totaloutsamps,obuf,obuf2,dz);
  1404. if(instrno == FLUTE) {
  1405. passfrq = FLUTE_PASSFRQ;
  1406. stopfrq = FLUTE_STOPFRQ;
  1407. if((exit_status = msetup_lphp_filter(&flt_cnt,&flt_mul,passfrq,stopfrq,dz))<0)
  1408. return exit_status;
  1409. if((exit_status = mdo_lphp_filter(totaloutsamps,flt_cnt,flt_mul,dz))<0)
  1410. return exit_status;
  1411. if((exit_status = mdo_lphp_filter(totaloutsamps,flt_cnt,flt_mul,dz))<0)
  1412. return exit_status;
  1413. }
  1414. for(kk=0;kk<total_outsamps;kk++)
  1415. maxval = max(maxval,fabs(obuf[kk]));
  1416. }
  1417. packet_phase = 1;
  1418. normaliser = levl/maxval;
  1419. time = 0.0;
  1420. *sampcnt = 0;
  1421. if(!sloom)
  1422. fprintf(stdout,"*");
  1423. memset((char *)obuf,0,dz->max_notedur * sizeof(float));
  1424. memset((char *)obuf2,0,dz->max_notedur * sizeof(float));
  1425. init = 1;
  1426. if(packet_type) {
  1427. while(*sampcnt < totaloutsamps) {
  1428. for(n=0;n<partialcnt;n++) // Zero sine-table pointers for all partials, at start of packet
  1429. sinptr[n] = 0.0;
  1430. for(kk = 0; kk<packet_dur;kk++) {
  1431. time = (double)(*sampcnt)/srate;
  1432. for(n=0;n<partialcnt;n++) {
  1433. loindex = (int)floor(sinptr[n]); // Read from sintable, using partial-increment, for each partial
  1434. hiindex = loindex + 1;
  1435. loval = sintab[loindex];
  1436. hival = sintab[hiindex];
  1437. valdiff = hival - loval;
  1438. timefrac = sinptr[n] - (double)loindex;
  1439. val = loval + (valdiff * timefrac);
  1440. level = read_level(n,time,dz); // Read corresponding level
  1441. val *= level;
  1442. obuf[*sampcnt] = (float)(obuf[*sampcnt] + val);
  1443. incr_sinptr(n,time,onehzincr,frq,&partialfrq,dz); // Track (modify if ness) the partial-incr value for this partial
  1444. }
  1445. // Once all partial-samples added, impose packet envelope
  1446. envv = read_packet_envelope(kk,packet_incr,dz);
  1447. obuf[*sampcnt] = (float)(obuf[*sampcnt] * envv * normaliser * packet_phase);
  1448. (*sampcnt)++;
  1449. }
  1450. if(instrument->has_vibrato)
  1451. add_packet_vibrato(&init,onehzincr,srate,sampcnt,packet_dur,dz);
  1452. packet_phase = -packet_phase;
  1453. }
  1454. } else {
  1455. for(n=0;n<partialcnt;n++)
  1456. sinptr[n] = 0.0;
  1457. if(instrument->has_vibrato)
  1458. total_outsamps = (int)round(totaloutsamps * VIBSHIFT); // Generate more than required, to allow for vibrato
  1459. else
  1460. total_outsamps = totaloutsamps;
  1461. for(kk = 0; kk<total_outsamps;kk++) {
  1462. time = (double)kk/srate;
  1463. for(n=0;n<partialcnt;n++) {
  1464. loindex = (int)floor(sinptr[n]);
  1465. hiindex = loindex + 1;
  1466. loval = sintab[loindex];
  1467. hival = sintab[hiindex];
  1468. valdiff = hival - loval;
  1469. timefrac = sinptr[n] - (double)loindex;
  1470. val = loval + (valdiff * timefrac);
  1471. level = read_level(n,time,dz);
  1472. val *= level;
  1473. obuf[kk] = (float)(obuf[kk] + val);
  1474. incr_sinptr(n,time,onehzincr,frq,&partialfrq,dz);
  1475. }
  1476. obuf[kk] = (float)(obuf[kk] * normaliser);
  1477. }
  1478. if(instrument->has_vibrato)
  1479. add_vibrato(onehzincr,srate,totaloutsamps,obuf,obuf2,dz);
  1480. if(instrno == FLUTE) {
  1481. if((exit_status = mdo_lphp_filter(totaloutsamps,flt_cnt,flt_mul,dz))<0)
  1482. return exit_status;
  1483. if((exit_status = mdo_lphp_filter(totaloutsamps,flt_cnt,flt_mul,dz))<0)
  1484. return exit_status;
  1485. }
  1486. *sampcnt = totaloutsamps;
  1487. }
  1488. if((exit_status = impose_note_envelope(obuf,notenv,envlen,totaloutsamps,linecnt,notecnt,instrument,dz))<0)
  1489. return exit_status;
  1490. return FINISHED;
  1491. }
  1492. /**************************** IMPOSE_NOTE_ENVELOPE ****************************/
  1493. int impose_note_envelope(float *obuf,double *notenv,int envlen,int sampcnt,int linecnt,int notecnt,synptr instrument,dataptr dz)
  1494. {
  1495. double time, srate = (double)MSYNSRATE, envval, dur, fade, endenvtime;
  1496. int n;
  1497. int envnext = 2;
  1498. double thistime, thisval, nexttime, nextval, timediff, valdiff, tratio;
  1499. endenvtime = instrument->env[instrument->env_len-2];
  1500. thistime = notenv[0];
  1501. thisval = notenv[1];
  1502. nexttime = notenv[2],
  1503. nextval = notenv[3];
  1504. timediff = nexttime - thistime;
  1505. valdiff = nextval - thisval;
  1506. dur = (double)sampcnt/srate;
  1507. if(instrument->overlap)
  1508. // Ensure (post-vibrato) duration of note does not spill over end of envelope data
  1509. dur = min(endenvtime,dur);
  1510. else {
  1511. // Curtail or extend prototype instr envelope to (post-vibrato) duration of note
  1512. if(envlen == 12) { // rise, on, down, hold,off
  1513. fade = notenv[10] - notenv[8];
  1514. if(dur > notenv[6] + fade) { // x x
  1515. notenv[10] = dur; // x <-x->
  1516. notenv[8] = dur - fade; // x <-x->
  1517. } else if(dur > notenv[6]) {
  1518. notenv[6] = dur; // x x
  1519. notenv[7] = 0.0; // envlen NOW = 8
  1520. // x x
  1521. } else if(dur > notenv[4]) {
  1522. notenv[4] = dur; // x
  1523. notenv[5] = 0.0; // envlen NOW = 6
  1524. // x x
  1525. } else {
  1526. sprintf(errstr,"Note too short in line %d : note %d\n",linecnt+1,notecnt);
  1527. return DATA_ERROR;
  1528. }
  1529. } else { // rise, on, down, decay
  1530. if(dur > notenv[6]) { // x x
  1531. notenv[8] = dur; // x
  1532. // x <-x->
  1533. } else if(dur > notenv[4]) {
  1534. notenv[4] = dur; // x
  1535. notenv[5] = 0.0; // envlen NOW = 6
  1536. // x x
  1537. } else {
  1538. sprintf(errstr,"Note too short in line %d : note %d\n",linecnt+1,notecnt);
  1539. return DATA_ERROR;
  1540. }
  1541. }
  1542. }
  1543. for(n=0;n<sampcnt;n++) {
  1544. time = (double)n/srate;
  1545. if(time == 0.0 || time >= dur)
  1546. envval = 0.0;
  1547. else {
  1548. while (nexttime <= time) {
  1549. thistime = nexttime;
  1550. thisval = nextval;
  1551. envnext += 2;
  1552. nexttime = notenv[envnext];
  1553. nextval = notenv[envnext+1];
  1554. timediff = nexttime - thistime;
  1555. valdiff = nextval - thisval;
  1556. }
  1557. tratio = (time - thistime)/timediff;
  1558. envval = (valdiff * tratio) + thisval;
  1559. }
  1560. obuf[n] = (float)(obuf[n] * envval * instrument->balance);
  1561. }
  1562. memset((char *)(obuf + sampcnt),0,(dz->max_notedur - sampcnt) * sizeof(float));
  1563. return FINISHED;
  1564. }
  1565. /**************************** INCR_SINPTR ****************************/
  1566. void incr_sinptr(int n,double time,double onehzincr,double frq,double *partialfrq,dataptr dz)
  1567. {
  1568. int m;
  1569. double hival, loval, hitime, lotime, timediff, timefrac, valdiff, partialval, thisincr;
  1570. double *sinptr = dz->parray[dz->pntarray];
  1571. double *thispartial = dz->parray[n];
  1572. m = 0;
  1573. while(thispartial[m] < time) {
  1574. m += 2;
  1575. if(m >= dz->partialtabs_cnt)
  1576. break;
  1577. }
  1578. if(m==0)
  1579. partialval = thispartial[1];
  1580. else if(m < dz->partialtabs_cnt) {
  1581. hival = thispartial[m+1];
  1582. loval = thispartial[m-1];
  1583. hitime = thispartial[m];
  1584. lotime = thispartial[m-2];
  1585. timediff = hitime - lotime;
  1586. timefrac = (time - lotime)/timediff;
  1587. valdiff = hival - loval;
  1588. partialval = loval + (valdiff * timefrac);
  1589. } else
  1590. partialval = thispartial[dz->partialtabs_cnt-1];
  1591. // Convert partial numbers to table-increments
  1592. thisincr = partialval * onehzincr;
  1593. thisincr *= frq;
  1594. sinptr[n] += thisincr;
  1595. if(sinptr[n] >= SYNTH_TABSIZE)
  1596. sinptr[n] -= (double)SYNTH_TABSIZE;
  1597. }
  1598. /**************************** READ_LEVEL ****************************/
  1599. double read_level(int n,double time,dataptr dz)
  1600. {
  1601. int m;
  1602. double hival, loval, hitime, lotime, timediff, timefrac, valdiff, level;
  1603. double *thislevel = dz->parray[n + MAXPARTIALS];
  1604. m = 0;
  1605. while(thislevel[m] < time) {
  1606. m += 2;
  1607. if(m >= dz->partialtabs_cnt)
  1608. break;
  1609. }
  1610. if(m==0) {
  1611. level = thislevel[1];
  1612. } else if(m < dz->partialtabs_cnt) {
  1613. hival = thislevel[m+1];
  1614. loval = thislevel[m-1];
  1615. hitime = thislevel[m];
  1616. lotime = thislevel[m-2];
  1617. timediff = hitime - lotime;
  1618. timefrac = (time - lotime)/timediff;
  1619. valdiff = hival - loval;
  1620. level = loval + (valdiff * timefrac);
  1621. } else {
  1622. level = thislevel[dz->partialtabs_cnt-1];
  1623. }
  1624. return level;
  1625. }
  1626. /**************************** CREATE_SYNTHESIZER_SNDBUFS ****************************/
  1627. int create_synthesizer_sndbufs(dataptr dz)
  1628. {
  1629. unsigned int bigbufsize;
  1630. int framesize, segsize;
  1631. int safety = (int)(dz->param[MS_MAXJIT] * dz->infile->srate) * dz->infile->channels;
  1632. dz->bufcnt = 3;
  1633. framesize = F_SECSIZE * dz->infile->channels;
  1634. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  1635. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  1636. return(MEMORY_ERROR);
  1637. }
  1638. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  1639. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  1640. return(MEMORY_ERROR);
  1641. }
  1642. dz->buflen = dz->maxoutsamp + dz->max_notedur + safety; // Allow for jitter and vibrato
  1643. segsize = dz->buflen / framesize;
  1644. if(segsize * framesize < dz->buflen)
  1645. segsize++;
  1646. dz->buflen = segsize * framesize;
  1647. bigbufsize = (dz->buflen + (2 * dz->max_notedur)) * sizeof(float);
  1648. if((dz->bigbuf = (float *)malloc(bigbufsize)) == NULL) {
  1649. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1650. return(PROGRAM_ERROR);
  1651. }
  1652. dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf; // Whole output sound
  1653. dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + dz->buflen; // Output note
  1654. dz->sbufptr[2] = dz->sampbuf[2] = dz->sampbuf[1] + dz->max_notedur; // Any vibrato transform of output note
  1655. dz->sampbuf[3] = dz->sampbuf[2] + dz->max_notedur;
  1656. return(FINISHED);
  1657. }
  1658. /**************************** GENERATE_PACKET_ENVELOPE *************************/
  1659. int generate_packet_envelope (dataptr dz)
  1660. {
  1661. int n;
  1662. double *costab, *origtab;
  1663. if((dz->parray[dz->pakt_env] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  1664. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1665. return(MEMORY_ERROR);
  1666. }
  1667. costab = dz->parray[dz->pakt_env];
  1668. if((dz->parray[dz->temp_tab] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  1669. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1670. return(MEMORY_ERROR);
  1671. }
  1672. if((dz->parray[dz->orig_tab] = (double *)malloc((TREMOLO_TABSIZE + 1) * sizeof(double)))==NULL) {
  1673. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1674. return(MEMORY_ERROR);
  1675. }
  1676. origtab = dz->parray[dz->orig_tab];
  1677. for(n=0;n<TREMOLO_TABSIZE;n++) {
  1678. costab[n] = cos(PI * 2.0 * ((double)n/(double)TREMOLO_TABSIZE));
  1679. costab[n] += 1.0;
  1680. costab[n] /= 2.0;
  1681. costab[n] = 1.0 - costab[n];
  1682. origtab[n] = costab[n];
  1683. }
  1684. costab[n] = 0.0; /* wrap around point */
  1685. origtab[n] = 0.0; /* wrap around point */
  1686. return(FINISHED);
  1687. }
  1688. /**************************** MODIFY_PACKET_ENVELOPE *************************/
  1689. int modify_packet_envelope (double synth_ctr,double synth_sqz,dataptr dz)
  1690. {
  1691. int n, halftabsize = TREMOLO_TABSIZE/2;
  1692. int isneg = 0, tablopos, tabhipos;
  1693. double *costab, *temptab, *origtab, diff, tabrem, tabincr, lotabincr, hitabincr, readpos, frac;
  1694. costab = dz->parray[dz->pakt_env];
  1695. temptab = dz->parray[dz->temp_tab];
  1696. origtab = dz->parray[dz->orig_tab];
  1697. if(flteq(synth_sqz,1.0)) {
  1698. for(n=0;n<=TREMOLO_TABSIZE;n++)
  1699. temptab[n] = origtab[n];
  1700. } else {
  1701. for(n=0;n<=TREMOLO_TABSIZE;n++)
  1702. temptab[n] = pow(origtab[n],synth_sqz);
  1703. }
  1704. if(flteq(synth_ctr,1.0)) {
  1705. for(n=0;n<=TREMOLO_TABSIZE;n++)
  1706. costab[n] = temptab[n];
  1707. } else {
  1708. if(synth_ctr < 0.0) {
  1709. frac = 1.0 + synth_ctr;
  1710. isneg = 1;
  1711. } else
  1712. frac = 1.0 - synth_ctr;
  1713. if(isneg) {
  1714. lotabincr = 1.0/frac;
  1715. hitabincr = 1.0/(2.0 - frac);
  1716. } else {
  1717. lotabincr = 1.0/(2.0 - frac);
  1718. hitabincr = 1.0/frac;
  1719. }
  1720. readpos = 0;
  1721. tabincr = lotabincr;
  1722. for(n=0;n<TREMOLO_TABSIZE;n++) {
  1723. if(readpos >= halftabsize) {
  1724. tabincr = hitabincr;
  1725. }
  1726. tablopos = (int)floor(readpos);
  1727. tabhipos = min(tablopos + 1,TREMOLO_TABSIZE);
  1728. tabrem = readpos - (double)tablopos;
  1729. diff = temptab[tabhipos] - temptab[tablopos];
  1730. costab[n] = temptab[tablopos] + (diff * tabrem);
  1731. readpos += tabincr;
  1732. }
  1733. }
  1734. return(FINISHED);
  1735. }
  1736. /**************************** READ_PACKET_ENVELOPE *************************/
  1737. double read_packet_envelope(int kk,double incr,dataptr dz)
  1738. {
  1739. double *costab, tabpos, tabrem, diff, envv;
  1740. int tablopos, tabhipos;
  1741. costab = dz->parray[dz->pakt_env];
  1742. tabpos = (double)kk * incr;
  1743. tablopos = (int)floor(tabpos);
  1744. tabhipos = min(tablopos + 1,TREMOLO_TABSIZE);
  1745. tabrem = tabpos - (double)tablopos;
  1746. diff = costab[tabhipos] - costab[tablopos];
  1747. envv = costab[tablopos] + (diff * tabrem);
  1748. return envv;
  1749. }
  1750. /**************************** SETUP_THE_DATA_ARRAYS ****************************/
  1751. int setup_the_data_arrays(int inlinecnt,double synth_ctr,double synth_sqz,dataptr dz)
  1752. {
  1753. int exit_status;
  1754. double *sintab;
  1755. int n;
  1756. int zz;
  1757. dz->array_cnt = (MAXPARTIALS * 2) + inlinecnt + 10 + 7;
  1758. // An array for every partial-pno, every partial-level,
  1759. // An array for every input line of data
  1760. // + (0) snd-sintable
  1761. // + (1) sintab-incr-pointers
  1762. // + (2-4) packet envelope + 2 packet-envelope-temp-arrays
  1763. // + (5) storage of instrument-note-spectral_data
  1764. // + (6) storage if instr-note-envelope-data
  1765. // + (7) storage of note-vibrato-depth-curve
  1766. // + (8) storage of note-vibrato-frq-curve
  1767. // + (9) multichan positions of output streams
  1768. // + 7 for filter calculations
  1769. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  1770. sprintf(errstr,"INSUFFICIENT MEMORY to create partial data arrays.\n");
  1771. return(MEMORY_ERROR);
  1772. }
  1773. for(n=0;n <dz->array_cnt;n++)
  1774. dz->parray[n] = NULL;
  1775. dz->filterbas = dz->array_cnt - 7;
  1776. zz = MAXPARTIALS * 2;
  1777. for(n=0;n <zz;n++) { // 2 entries (time and value) for every line in the data.
  1778. if((dz->parray[n] = (double *)malloc((MAXLINECNT * 2) * sizeof(double)))==NULL) {
  1779. sprintf(errstr,"INSUFFICIENT MEMORY to store partial data.\n");
  1780. return(MEMORY_ERROR);
  1781. }
  1782. }
  1783. dz->scoredata = (MAXPARTIALS * 2);
  1784. dz->sinarray = (MAXPARTIALS * 2) + inlinecnt;
  1785. dz->pntarray = dz->sinarray + 1;
  1786. dz->pakt_env = dz->sinarray + 2;
  1787. dz->temp_tab = dz->sinarray + 3;
  1788. dz->orig_tab = dz->sinarray + 4;
  1789. dz->ins_spectrum = dz->sinarray + 5;
  1790. dz->ins_envel = dz->sinarray + 6;
  1791. dz->ins_vibd = dz->sinarray + 7;
  1792. dz->ins_vibf = dz->sinarray + 8;
  1793. dz->ochan_pos = dz->sinarray + 9;
  1794. // Establish sine-table
  1795. if((dz->parray[dz->sinarray] = (double *)malloc((SYNTH_TABSIZE + 1) * sizeof(double)))==NULL) {
  1796. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1797. return(MEMORY_ERROR);
  1798. }
  1799. sintab = dz->parray[dz->sinarray];
  1800. for(n=0;n<SYNTH_TABSIZE;n++)
  1801. sintab[n] = sin(PI * 2.0 * ((double)n/(double)SYNTH_TABSIZE));
  1802. sintab[n] = sintab[0]; /* wrap around point */
  1803. // Pointers into sintable for all partials, and one for vibrato read
  1804. if((dz->parray[dz->pntarray] = (double *)malloc((MAXPARTIALS+1) * sizeof(double)))==NULL) {
  1805. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1806. return(MEMORY_ERROR);
  1807. }
  1808. if((exit_status = generate_packet_envelope(dz))<0)
  1809. return(exit_status);
  1810. for(n=0;n<=MAXPARTIALS;n++) // Zero sine-table pointers for all partials and for vibrato
  1811. dz->parray[dz->pntarray][n] = 0.0;
  1812. // Max size array for time-changing spectrum of instrument
  1813. if((dz->parray[dz->ins_spectrum] = (double *)malloc(MAXENTRYCNT * sizeof(double)))==NULL) {
  1814. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1815. return(MEMORY_ERROR);
  1816. }
  1817. // Max size array for loudness envelope of instrument
  1818. if((dz->parray[dz->ins_envel] = (double *)malloc((MAXENVPNTS * 2) * sizeof(double)))==NULL) {
  1819. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1820. return(MEMORY_ERROR);
  1821. }
  1822. // Small arrays for vibrato params of notes
  1823. if((dz->parray[dz->ins_vibd] = (double *)malloc(6 * sizeof(double)))==NULL) {
  1824. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1825. return(MEMORY_ERROR);
  1826. }
  1827. if((dz->parray[dz->ins_vibf] = (double *)malloc(6 * sizeof(double)))==NULL) {
  1828. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1829. return(MEMORY_ERROR);
  1830. }
  1831. // Array for multichan output positions (channo of left-chan plus pair of L-R levels) of streams
  1832. if((dz->parray[dz->ochan_pos] = (double *)malloc((MAXSCORLINE * 3) * sizeof(double)))==NULL) {
  1833. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  1834. return(MEMORY_ERROR);
  1835. }
  1836. return FINISHED;
  1837. }
  1838. /**************************** GENERATE_VIBRATO_CURVES *************************/
  1839. void generate_vibrato_curves(double maxdepth, double notedur, dataptr dz)
  1840. {
  1841. double *vibdepth = dz->parray[dz->ins_vibd];
  1842. double *vibfrq = dz->parray[dz->ins_vibf];
  1843. double time, depth, frq;
  1844. vibdepth[0] = 0.0;
  1845. vibdepth[1] = 0.0;
  1846. time = drand48() * notedur/2;
  1847. time += notedur/4;
  1848. vibdepth[2] = time;
  1849. depth = drand48() * maxdepth/2;
  1850. depth += maxdepth/2;
  1851. vibdepth[3] = depth;
  1852. vibdepth[4] = notedur;
  1853. vibdepth[5] = 0.0;
  1854. frq = drand48() * 4.0;
  1855. frq += 4.0;
  1856. vibfrq[0] = 0.0;
  1857. vibfrq[1] = frq;
  1858. frq = drand48() * 4.0;
  1859. frq += 4.0;
  1860. vibfrq[2] = time;
  1861. vibfrq[3] = frq;
  1862. frq = drand48() * 4.0;
  1863. frq += 4.0;
  1864. vibfrq[4] = notedur;
  1865. vibfrq[5] = frq;
  1866. }
  1867. /**************************** ADD_VIBRATO *************************/
  1868. int add_vibrato(double onehzincr,double srate,int outsampcnt,float *obuf,float *obuf2,dataptr dz)
  1869. {
  1870. int vibnext = 2;
  1871. double *vib_depth = dz->parray[dz->ins_vibd], obufptr = 0.0;
  1872. double *vib_frq = dz->parray[dz->ins_vibf];
  1873. double *sintab = dz->parray[dz->sinarray];
  1874. static double thistime, thisfrq, thisdepth, nexttime, nextfrq, nextdepth, timediff, depthdiff, frqdiff, endtime;
  1875. double time, vibdepth, vibfrq, timefrac, tratio, val, loval, hival, valdiff, frac;
  1876. int loindex, hiindex, sampcnt, obufi = 0;
  1877. double vib_sinpntr = dz->parray[dz->pntarray][MAXPARTIALS];
  1878. // Initialise variables
  1879. thistime = 0.0;
  1880. thisdepth = vib_depth[1];
  1881. nexttime = vib_depth[2];
  1882. nextdepth = vib_depth[3];
  1883. endtime = vib_depth[4];
  1884. thisfrq = vib_frq[1];
  1885. nextfrq = vib_frq[3];
  1886. depthdiff = nextdepth - thisdepth;
  1887. frqdiff = nextfrq - thisfrq;
  1888. timediff = nexttime - thistime;
  1889. vib_sinpntr = 0.0;
  1890. sampcnt = 0;
  1891. obuf2[sampcnt++] = obuf[0];
  1892. while(sampcnt < outsampcnt) {
  1893. // Write vibratoed value to obuf2
  1894. time = (double)sampcnt/srate;
  1895. obufi = (int)floor(obufptr);
  1896. loval = obuf[obufi];
  1897. hival = obuf[obufi+1];
  1898. valdiff = hival - loval;
  1899. frac = obufptr - obufi;
  1900. val = loval + (valdiff * frac);
  1901. obuf2[sampcnt] = (float)val;
  1902. // Find current frequency and depth of vibrato
  1903. if(time >= endtime) {
  1904. vibdepth = vib_depth[5];
  1905. vibfrq = vib_frq[5];
  1906. } else {
  1907. while (nexttime <= time) {
  1908. thistime = nexttime;
  1909. thisdepth = nextdepth;
  1910. thisfrq = nextfrq;
  1911. vibnext += 2;
  1912. nexttime = vib_depth[vibnext];
  1913. nextdepth = vib_depth[vibnext+1];
  1914. nextfrq = vib_frq[vibnext+1];
  1915. depthdiff = nextdepth - thisdepth;
  1916. frqdiff = nextfrq - thisfrq;
  1917. timediff = nexttime - thistime;
  1918. }
  1919. tratio = (time - thistime)/timediff;
  1920. vibfrq = (frqdiff * tratio) + thisfrq;
  1921. vibdepth = (depthdiff * tratio) + thisdepth;
  1922. }
  1923. // Use current pointer-position for vibrato, in sintable, to read sinusoidal vibrato-divergence
  1924. loindex = (int)floor(vib_sinpntr); // Read from sintable, using vibrato-sinptr
  1925. hiindex = loindex + 1;
  1926. loval = sintab[loindex];
  1927. hival = sintab[hiindex];
  1928. valdiff = hival - loval;
  1929. timefrac = vib_sinpntr - (double)loindex;
  1930. val = loval + (valdiff * timefrac);
  1931. // Advance vibrato sin-table-read-pointer
  1932. vib_sinpntr += vibfrq * onehzincr;
  1933. if(vib_sinpntr >= SYNTH_TABSIZE)
  1934. vib_sinpntr -= (double)SYNTH_TABSIZE;
  1935. // Multiply sinusoidal val by current semitone-depth of vib
  1936. val *= vibdepth;
  1937. // convert semitone-offset to frq-ratio (>1 for up, <1 for down) and advance in pre-vibrato sound by frq-ratio
  1938. obufptr += SEMITONES_AS_RATIO(val);
  1939. sampcnt++;
  1940. } // Copy vibratoed output to original obuf
  1941. memset((char *)obuf,0,dz->max_notedur * sizeof(float));
  1942. memcpy((char *)obuf,(char *)obuf2,sampcnt * sizeof(float));
  1943. return FINISHED;
  1944. }
  1945. /**************************** ADD_PACKET_VIBRATO *************************/
  1946. int add_packet_vibrato(int *init,double onehzincr,double srate,int *sampcnt,int packet_dur,dataptr dz)
  1947. {
  1948. int vibnext = 2;
  1949. double *vib_depth = dz->parray[dz->ins_vibd];
  1950. double *vib_frq = dz->parray[dz->ins_vibf];
  1951. double *sintab = dz->parray[dz->sinarray];
  1952. static double thistime, thisfrq, thisdepth, nexttime, nextfrq, nextdepth, timediff, depthdiff, frqdiff, endtime;
  1953. double time, vibdepth, vibfrq, timefrac, tratio, val, loval, hival, valdiff, incr;
  1954. int loindex, hiindex, new_packet_dur, packet_shift;
  1955. double *vib_sinpntr = &(dz->parray[dz->pntarray][MAXPARTIALS]);
  1956. // Initialise variables
  1957. if(*init) {
  1958. thistime = 0.0;
  1959. thisdepth = vib_depth[1];
  1960. nexttime = vib_depth[2];
  1961. nextdepth = vib_depth[3];
  1962. endtime = vib_depth[4];
  1963. thisfrq = vib_frq[1];
  1964. nextfrq = vib_frq[3];
  1965. depthdiff = nextdepth - thisdepth;
  1966. frqdiff = nextfrq - thisfrq;
  1967. timediff = nexttime - thistime;
  1968. *vib_sinpntr = 0.0;
  1969. *init = 0;
  1970. }
  1971. // Find current frequency and depth of vibrato
  1972. time = (double)(*sampcnt)/srate;
  1973. if(time == 0.0) {
  1974. vibdepth = thisdepth;
  1975. vibfrq = thisfrq;
  1976. } else if(time >= endtime) {
  1977. vibdepth = vib_depth[5];
  1978. vibfrq = vib_frq[5];
  1979. } else {
  1980. while (nexttime <= time) {
  1981. thistime = nexttime;
  1982. thisdepth = nextdepth;
  1983. thisfrq = nextfrq;
  1984. vibnext += 2;
  1985. nexttime = vib_depth[vibnext];
  1986. nextdepth = vib_depth[vibnext+1];
  1987. nextfrq = vib_frq[vibnext+1];
  1988. depthdiff = nextdepth - thisdepth;
  1989. frqdiff = nextfrq - thisfrq;
  1990. timediff = nexttime - thistime;
  1991. }
  1992. tratio = (time - thistime)/timediff;
  1993. vibfrq = (frqdiff * tratio) + thisfrq;
  1994. vibdepth = (depthdiff * tratio) + thisdepth;
  1995. }
  1996. // Use current pointer-position for vibrato, in sintable, to read sinusoidal vibrato-divergence
  1997. loindex = (int)floor(*vib_sinpntr); // Read from sintable, using vibrato-sinptr
  1998. hiindex = loindex + 1;
  1999. loval = sintab[loindex];
  2000. hival = sintab[hiindex];
  2001. valdiff = hival - loval;
  2002. timefrac = *vib_sinpntr - (double)loindex;
  2003. val = loval + (valdiff * timefrac);
  2004. // Advance vibrato sin-table-read-pointer by duration of one packet
  2005. *vib_sinpntr += vibfrq * onehzincr * packet_dur;
  2006. if(*vib_sinpntr >= SYNTH_TABSIZE)
  2007. *vib_sinpntr -= (double)SYNTH_TABSIZE;
  2008. // Multiply sinusoidal val by current semitone-depth of vib
  2009. val *= vibdepth;
  2010. // convert semitone offset to frq-ratio : THEN wavelen (packet) duration = 1.0/frq-ratio
  2011. incr = 1.0/SEMITONES_AS_RATIO(val);
  2012. new_packet_dur = (int)round((double)packet_dur * incr);
  2013. packet_shift = new_packet_dur - packet_dur;
  2014. *sampcnt += packet_shift;
  2015. return FINISHED;
  2016. }
  2017. /**************************** SETUP_THE_SPECIAL_DATA_FOR_GIVEN_INSTR ****************************/
  2018. int setup_the_special_data_for_given_instr(int instrno,double *data,synptr instrument,dataptr dz)
  2019. {
  2020. double val, timeval = 0.0;
  2021. int timepos, valpos, pno_cnt, lev_cnt = 0;
  2022. double *instr = dz->parray[dz->ins_spectrum], *env = dz->parray[dz->ins_envel];
  2023. int lstart;
  2024. int line, mm, cnt;
  2025. // Clearly arrays for spectrum and envelope
  2026. memset((char *)instr,0, MAXENTRYCNT * sizeof(double));
  2027. memset((char *)env,0, (MAXENVPNTS * 2) * sizeof(double));
  2028. // ALPHA : Setup instrument specific spectrum
  2029. instr = instrument->spectrum; // instrument spectrum
  2030. timepos = 0; // Pointer to time-values in all arrays
  2031. valpos = 1; // Pointer to val-at-time in all arrays
  2032. pno_cnt = 0; // Pointer to partial-pno table
  2033. lstart = MAXPARTIALS; // Start of partial-level table
  2034. for(line=0; line < instrument->line_cnt; line++) {
  2035. mm = instrument->line_entrycnt * line; // Index of entry in the instrument array, at start of current line
  2036. for(cnt = 0;cnt < instrument->line_entrycnt;cnt++) {
  2037. val = instr[mm];
  2038. switch(cnt) {
  2039. case(0):
  2040. pno_cnt = 0; // Point to start of pnos, and levels
  2041. lev_cnt = lstart;
  2042. timeval = val;
  2043. break;
  2044. default:
  2045. if(ODD(cnt)) { // Put pno in appropriate pno-array
  2046. dz->parray[pno_cnt][timepos] = timeval;
  2047. dz->parray[pno_cnt][valpos] = val;
  2048. pno_cnt++;
  2049. } else { // Put level in appropriate level-array
  2050. dz->parray[lev_cnt][timepos] = timeval;
  2051. dz->parray[lev_cnt][valpos] = val;
  2052. lev_cnt++;
  2053. }
  2054. break;
  2055. }
  2056. mm++;
  2057. }
  2058. timepos += 2; // Advance pointers in pno and level tables
  2059. valpos +=2;
  2060. }
  2061. dz->partialtabs_cnt = instrument->line_cnt * 2; // Store lengths of partial tables (1 time and 1 value entry from each dataline)
  2062. return(FINISHED);
  2063. }
  2064. /**************************** PRETEST_THE_SPECIAL_DATA ****************************/
  2065. int pretest_the_special_data(char *str,int *inlinecnt,synptr *orchestra,dataptr dz)
  2066. {
  2067. double dummy = 0.0, lasttime = 0.0, lastpitch[16];
  2068. synptr instrument;
  2069. double timestep;
  2070. int insno, typ, coincident = 0, doublestop, lastdur, pianoRHcnt = 0, pianoLHcnt = 0, pianoRHpos = -1, pianoLHpos = -1;
  2071. FILE *fp;
  2072. int cnt, linecnt, n, m;
  2073. char temp[8000], insnam[200],*p;
  2074. if((fp = fopen(str,"r"))==NULL) {
  2075. sprintf(errstr,"Cannot open file %s to read times.\n",str);
  2076. return(DATA_ERROR);
  2077. }
  2078. linecnt = 1;
  2079. while(fgets(temp,8000,fp)!=NULL) {
  2080. p = temp;
  2081. while(isspace(*p))
  2082. p++;
  2083. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2084. continue;
  2085. if((read_instrument_name_from_start_of_line(&p,insnam,&insno,orchestra,linecnt)) < 0) {
  2086. return DATA_ERROR;
  2087. }
  2088. if(!(strcmp(insnam,"pianoRH"))) {
  2089. pianoRHcnt++;
  2090. if(pianoRHcnt > 1) {
  2091. sprintf(errstr,"Cannot handle more than 1 piano right-hand.\n");
  2092. return DATA_ERROR;
  2093. }
  2094. pianoRHpos = linecnt;
  2095. } else if(!(strcmp(insnam,"pianoLH"))) {
  2096. pianoLHcnt++;
  2097. if(pianoLHcnt > 1) {
  2098. sprintf(errstr,"Cannot handle more than 1 piano left-hand.\n");
  2099. return DATA_ERROR;
  2100. }
  2101. pianoLHpos = linecnt;
  2102. }
  2103. instrument = orchestra[insno]; // ALPHA : Sets up instrument specific pitch-ranges
  2104. doublestop = instrument->doublestop;
  2105. cnt = 0;
  2106. lastdur = 0;
  2107. while(get_float_from_within_string(&p,&dummy)) {
  2108. typ = cnt % 4;
  2109. switch(typ) {
  2110. case(0): // time
  2111. if(cnt > 0) {
  2112. timestep = dummy - lasttime;
  2113. if(timestep < 0) {
  2114. sprintf(errstr,"Times do not advance (%lf %lf) in line %d\n",lasttime,dummy,linecnt);
  2115. return DATA_ERROR;
  2116. }
  2117. if(timestep == 0)
  2118. coincident++;
  2119. else if(timestep < 3) {
  2120. sprintf(errstr,"Times do not advance sufficiently (%d to %d = %d units) in line %d (min: 3 units)\n",(int)round(lasttime),(int)round(dummy),(int)round(dummy - lasttime),linecnt);
  2121. return DATA_ERROR;
  2122. } else
  2123. coincident = 1;
  2124. if(coincident > instrument->doublestop) {
  2125. sprintf(errstr,"Too many coincident notes at time %lf in line %d\n",lasttime,linecnt);
  2126. return DATA_ERROR;
  2127. }
  2128. } else
  2129. coincident = 1;
  2130. lasttime = dummy;
  2131. break;
  2132. case(1): // pitch
  2133. if(dummy < instrument->rangebot || dummy > instrument->rangetop) {
  2134. sprintf(errstr,"Pitch value (%d) out of range (%d to %d) for instrument %s line %d\n",(int)round(dummy),instrument->rangebot,instrument->rangetop,insnam,linecnt);
  2135. return DATA_ERROR;
  2136. }
  2137. lastpitch[coincident - 1] = dummy;
  2138. if(coincident > 1) {
  2139. for(n = 0;n < coincident-1;n++) {
  2140. for(m = n+1;m < coincident;m++) {
  2141. if(lastpitch[n] == lastpitch[m]) {
  2142. sprintf(errstr,"Identical Pitch values (%d) used at same time (%d) for instrument %s line %d\n",(int)round(lastpitch[n]),(int)round(lasttime),insnam,linecnt);
  2143. return DATA_ERROR;
  2144. }
  2145. }
  2146. }
  2147. }
  2148. break;
  2149. case(2): // level
  2150. if(dummy <= 0.0 || dummy > 1.0) {
  2151. sprintf(errstr,"Level value (%lf) out of range (>0 to 1) for instrument %s\n",dummy,insnam);
  2152. return DATA_ERROR;
  2153. }
  2154. break;
  2155. case(3): // dur
  2156. if(dummy < 1 || dummy > MSYNMAXQDUR) {
  2157. sprintf(errstr,"Duration in semiquavers (%d) out of range (1 to %d) for instrument %s\n",(int)round(dummy),MSYNMAXQDUR,insnam);
  2158. return DATA_ERROR;
  2159. }
  2160. if(coincident > 1 && dummy != lastdur) {
  2161. sprintf(errstr,"Notes in \"%s\" at same time %d, have different durations (%d and %d)\n",insnam,(int)round(lasttime),lastdur,(int)round(dummy));
  2162. return DATA_ERROR;
  2163. }
  2164. lastdur = (int)round(dummy);
  2165. break;
  2166. }
  2167. cnt++;
  2168. }
  2169. if(cnt % 4 != 0) {
  2170. sprintf(errstr,"Invalid number of numeric entries (%d) on line %d (must be in sets of 4: time,pitch,level,duration).\n",cnt,linecnt);
  2171. return(DATA_ERROR);
  2172. }
  2173. linecnt++;
  2174. }
  2175. linecnt--;
  2176. fclose(fp);
  2177. if(linecnt == 0) {
  2178. sprintf(errstr,"No significant data found in score file.\n");
  2179. return(DATA_ERROR);
  2180. }
  2181. if(linecnt >= MAXSCORLINE) {
  2182. sprintf(errstr,"Too many data lines (%d) found in score file (max %d).\n",linecnt,MAXSCORLINE);
  2183. return(DATA_ERROR);
  2184. }
  2185. *inlinecnt = linecnt;
  2186. if(pianoLHcnt != pianoRHcnt) {
  2187. sprintf(errstr,"Piano LH and RH not both present.\n");
  2188. return(DATA_ERROR);
  2189. }
  2190. if(pianoRHpos >= 0 && pianoRHpos+1 != pianoLHpos) {
  2191. sprintf(errstr,"Piano LH does not immediately follow piano RH.\n");
  2192. return(DATA_ERROR);
  2193. }
  2194. return FINISHED;
  2195. }
  2196. /**************************** RETEST_COUNT_AND_STORE_THE_SPECIAL_DATA ****************************
  2197. *
  2198. * In Pass 1
  2199. * (a) makes an instrument dependent test, to check that successive notes don't overlap, if this not possible for specific instrument.
  2200. * (b) uses dz->timeconvert, derived from the input MM parameter to calculate sizes of buffers required.
  2201. * (c) creates the correct-sized arrays to store the scoredata lines.
  2202. * In Pass 2
  2203. * (a)stores the scoredata lines.
  2204. */
  2205. int retest_count_and_store_the_special_data(char *str,synptr *orchestra,int *flt_cnt,double *flt_mul,dataptr dz)
  2206. {
  2207. double dummy = 0.0, lastdur = 0.0, maxdur = 0.0, maxnotedur = 0.0, lasttime = 0.0, timestep, leftgain, rightgain;
  2208. double *linestor, *ochanpos = dz->parray[dz->ochan_pos], sustain = 0.0, lrpos;
  2209. int insno, cnt, typ, linecnt, n, m, itimestep, idummy, ilastdur = 0, ochans = dz->iparam[MS_OCHANS];
  2210. int surround = 0, leftmostchan, ispiano = -1, instrcnt = 0, coincidence;
  2211. synptr instrument;
  2212. FILE *fp;
  2213. char temp[8000], insnam[200],*p;
  2214. if((fp = fopen(str,"r"))==NULL) {
  2215. sprintf(errstr,"Cannot open file %s to read times.\n",str);
  2216. return(DATA_ERROR);
  2217. }
  2218. if(ochans > 2 && !dz->vflag[0])
  2219. surround = 1;
  2220. linecnt = 0;
  2221. coincidence = 1;
  2222. while(fgets(temp,8000,fp)!=NULL) {
  2223. p = temp;
  2224. while(isspace(*p))
  2225. p++;
  2226. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2227. continue;
  2228. read_instrument_name_from_start_of_line(&p,insnam,&insno,orchestra,(int)(linecnt+1));
  2229. if(!strcmp(insnam,"pianoRH"))
  2230. ispiano = linecnt;
  2231. instrument = orchestra[insno]; // ALPHA Establish note-overlap type
  2232. if(instrument->overlap) // If sustaining instrument, check entire decay-length of note
  2233. sustain = instrument->env[instrument->env_len - 2];
  2234. else
  2235. sustain = 0.0;
  2236. cnt = 0;
  2237. while(get_float_from_within_string(&p,&dummy)) {
  2238. typ = cnt % 4;
  2239. switch(typ) {
  2240. case(0): // time
  2241. if(cnt > 0) {
  2242. idummy = (int)floor(dummy);
  2243. if(idummy != dummy) {
  2244. sprintf(errstr,"Bad time value (%lf) (Times must be integers) on line %d (%s).\n",dummy,linecnt+1,insnam);
  2245. return(DATA_ERROR);
  2246. }
  2247. if((idummy % 3 != 0) && (idummy % 4 != 0)) {
  2248. sprintf(errstr,"Bad time value (%d) (Times must be multiples of 3 or 4) on line %d (%s).\n",idummy,linecnt+1,insnam);
  2249. return(DATA_ERROR);
  2250. }
  2251. timestep = dummy - lasttime;
  2252. itimestep = (int)round(timestep);
  2253. timestep *= dz->timeconvert; // Convert thirds-of-semiquavers to seconds
  2254. if(itimestep == 0) {
  2255. coincidence++;
  2256. if(coincidence > instrument->doublestop) {
  2257. sprintf(errstr,"Too many coincident notes at time %d in line %d (%s)\n",ilastdur,linecnt+1,insnam);
  2258. return DATA_ERROR;
  2259. }
  2260. } else if(itimestep < UNITSPERSEMIQ) {
  2261. sprintf(errstr,"Timestep between entries %d and %d (= %d) too small (MIN 3) on line %d (%s)\n",(int)round(lasttime),(int)round(dummy),itimestep,linecnt+1,insnam);
  2262. return DATA_ERROR;
  2263. } else
  2264. coincidence = 1;
  2265. if(instrument->doublestop < 2 && timestep < lastdur) { // Test instrument for note-overlaps
  2266. sprintf(errstr,"Duration (%d) at time %d too long for timestep (%d) in line %d (%s)\n",ilastdur,(int)round(lasttime),itimestep,linecnt+1,insnam);
  2267. return DATA_ERROR;
  2268. }
  2269. } else
  2270. coincidence = 1;
  2271. lasttime = dummy;
  2272. break;
  2273. case(3): // dur
  2274. idummy = (int)floor(dummy);
  2275. if(idummy != dummy) {
  2276. sprintf(errstr,"Bad duration value (%lf) (Durations must be integers) on line %d (%s).\n",dummy,linecnt+1,insnam);
  2277. return(DATA_ERROR);
  2278. }
  2279. if((idummy % 3 != 0) && (idummy % 4 != 0)) {
  2280. sprintf(errstr,"Bad duration (%d) (must be multiples of 3 or 4) on line %d (%s).\n",idummy,linecnt+1,insnam);
  2281. return(DATA_ERROR);
  2282. }
  2283. if(coincidence > 1) {
  2284. if(idummy != ilastdur) {
  2285. sprintf(errstr,"Coincident notes must have same duration (time %d on line %d : %s).\n",(int)round(lasttime),linecnt+1,insnam);
  2286. return(DATA_ERROR);
  2287. }
  2288. }
  2289. ilastdur = idummy;
  2290. lastdur = dummy * dz->timeconvert; // Convert thirds-of-semiquavers to seconds
  2291. lastdur = max(lastdur,sustain);
  2292. maxdur = max(maxdur,lasttime+lastdur);
  2293. maxnotedur = max(maxnotedur,lastdur);
  2294. break;
  2295. }
  2296. cnt++;
  2297. }
  2298. if((dz->parray[dz->scoredata+linecnt] = (double *)malloc((cnt+1) * sizeof(double)))==NULL) {
  2299. sprintf(errstr,"INSUFFICIENT MEMORY for score line %d.\n",linecnt+1);
  2300. return(MEMORY_ERROR);
  2301. }
  2302. linecnt++;
  2303. }
  2304. dz->maxoutsamp = (int)ceil(maxdur * (double)MSYNSRATE) * dz->iparam[MS_OCHANS];
  2305. dz->max_notedur = (int)ceil(maxnotedur * 2 * (double)MSYNSRATE); // Instrument streams are mono, but we have to allow for vibrato
  2306. // Position the instrument streams in the multichan-output space
  2307. // single instrument-stream
  2308. instrcnt = linecnt;
  2309. if(ispiano >= 0)
  2310. instrcnt--;
  2311. if(instrcnt == 1) {
  2312. if(ochans == 2) { // if stereo, create central image
  2313. ochanpos[0] = 0; // Left channel is channel 1(0)
  2314. ochanpos[1] = 0.5; // signal on 1+2
  2315. ochanpos[2] = 0.5;
  2316. } else if(surround) { // if multichan-surround, create image on front-centre channel 1(0)
  2317. ochanpos[0] = 0; // Left channel is channel 1(0): front centre
  2318. ochanpos[1] = 1.0; // Signal on 1
  2319. ochanpos[2] = 0.0;
  2320. } else { // if multichan linear, create image on central channel(s)
  2321. if(ODD(ochans)) {
  2322. ochanpos[0] = ochans/2; // e.g. 7 --> 3
  2323. ochanpos[1] = 1.0; // all signal on 3
  2324. ochanpos[2] = 0.0;
  2325. } else {
  2326. ochanpos[0] = ochans/2 - 1; // e.g. 8 --> 3
  2327. ochanpos[1] = 0.5; // signal on 3+4
  2328. ochanpos[2] = 0.5;
  2329. }
  2330. }
  2331. if(ispiano) { // Both LH and RH on same channel
  2332. if(ochans == 2) {
  2333. ochanpos[3] = 0;
  2334. ochanpos[4] = 0.5;
  2335. ochanpos[5] = 0.5;
  2336. } else if(surround) {
  2337. ochanpos[3] = 0;
  2338. ochanpos[4] = 1.0;
  2339. ochanpos[5] = 0.0;
  2340. } else {
  2341. if(ODD(ochans)) {
  2342. ochanpos[3] = ochans/2;
  2343. ochanpos[4] = 1.0;
  2344. ochanpos[5] = 0.0;
  2345. } else {
  2346. ochanpos[3] = ochans/2 - 1;
  2347. ochanpos[4] = 0.5;
  2348. ochanpos[5] = 0.5;
  2349. }
  2350. }
  2351. }
  2352. } else if(instrcnt <= ochans) {
  2353. // 1 instrument to a channel
  2354. for(n = 0,m = 0; m < linecnt * 3; n++,m+=3) {
  2355. ochanpos[m] = n; // Assign each instrument-stream to (successive) output channel
  2356. ochanpos[m+1] = 1.0; // All level on this channel
  2357. ochanpos[m+2] = 0.0; // None on adjacent channel
  2358. if(n == ispiano) { // Force both piano outputs to same channel
  2359. n--;
  2360. ispiano = -1; // Prevent this happening again
  2361. }
  2362. }
  2363. if(surround) { // If sound_surround, orient instruments to front of space.
  2364. leftmostchan = ochans/2 + 1;
  2365. for(n = 0,m = 0; n < linecnt; n++,m+=3)
  2366. ochanpos[m] = (double)(((int)round(ochanpos[m]) + leftmostchan) % ochans);
  2367. }
  2368. } else if(ochans == 2 || dz->vflag[0]) {
  2369. // if linear array, distribute instruments equispaced over multichannel linear array
  2370. for(n = 0,m = 0;m < (linecnt-1) * 3; n++,m+=3) {
  2371. if(n >= instrcnt - 1) { // If piano (double entry) is last item : say instrcnt = 5, but linecnt =6
  2372. ochanpos[m] = (double)(ochans - 1); // then n will reach instrcnt-1 before m reaches (linecnt-1)*3
  2373. ochanpos[m+1] = 0.0; // In this case, stick piano RH in rightmost channel of array, then go on
  2374. ochanpos[m+2] = 1.0; // (outside loop) to add pianoLH at same position in array.
  2375. } else {
  2376. ochanpos[m] = (double)n/(double)(instrcnt - 1); // e.g for 5 instr
  2377. // 0 1/4 2/4 3/4 () (Range 0 to 1)
  2378. ochanpos[m] *= ochans-1; // e.g. stereo (*1) --> 0 1/4 2/4 3/4 () (Range 0 to 1)
  2379. // e.g. 4-chan (*3) --> 0 3/4 1+1/2 2+1/4 () (Range 0 to 3)
  2380. lrpos = ochanpos[m];
  2381. while(lrpos > 1.0) // stereo 0 1/4 2/4 3/4 () : 4-chan 0 3/4 1+1/2 2+1/4 ()
  2382. lrpos -= 1.0; // stereo 0 1/4 2/4 3/4 () : 4-chan 0 3/4 1/2 1/4 ()
  2383. pancalc(lrpos,&leftgain,&rightgain);
  2384. ochanpos[m] = floor(ochanpos[m]); // stereo 0 0 0 0 () : 4-chan 0 0 1 2 ()
  2385. ochanpos[m+1] = leftgain;
  2386. ochanpos[m+2] = rightgain;
  2387. if(n == ispiano) { // Force both piano outputs to same channel
  2388. n--;
  2389. ispiano = -1;
  2390. }
  2391. }
  2392. }
  2393. ochanpos[m] = (double)(ochans - 1); // stereo(0) (0) (0) (0) 1 : 4-chan(0)(0) (1) (2) 3
  2394. ochanpos[m+1] = 0.0;
  2395. ochanpos[m+2] = 1.0; // all signal to rightmost speaker
  2396. } else {
  2397. // if sound-surround, distribute instruments equispaced around multichannel suround-array
  2398. for(n = 0,m = 0;m < linecnt*3; n++,m+=3) {
  2399. ochanpos[m] = (double)n/(double)instrcnt; // e.g for 5 instr
  2400. // 0 1/5 2/5 3/5 4/5 (Range 0 to 1)
  2401. ochanpos[m] *= ochans; // e.g. 4-chan (*4) --> 0 4/5 1+3/5 2+2/5 3+1/5 (Range 0 to 4)
  2402. // e.g. 8-chan (*8) --> 0 1+3/5 3+1/5 4+4/5 6+2/5 (Range 0 to 8)
  2403. lrpos = ochanpos[m];
  2404. while(lrpos > 1.0) // 4-chan 0 4/5 1+3/5 2+2/5 3+1/5 : 8-chan 0 1+3/5 3+1/5 4+4/5 6+2/5
  2405. lrpos -= 1.0; // 4-chan 0 4/5 3/5 2/5 1/5 : 8-chan 0 3/5 1/5 4/5 2/5
  2406. pancalc(lrpos,&leftgain,&rightgain);
  2407. ochanpos[m] = floor(ochanpos[m]); // 4-chan 0 0 1 2 3 : 8-chan 0 1 3 4 6
  2408. ochanpos[m+1] = leftgain;
  2409. ochanpos[m+2] = rightgain;
  2410. if(n == ispiano) { // Force both piano outputs to same channel
  2411. n--;
  2412. ispiano = -1; // prevent this happening again
  2413. }
  2414. }
  2415. leftmostchan = ochans/2 + 1; // Orient data
  2416. for(n = 0,m = 0; n < linecnt; n++,m+=3)
  2417. ochanpos[m] = (double)(((int)round(ochanpos[m]) + leftmostchan) % ochans);
  2418. }
  2419. fseek(fp,0,0);
  2420. linecnt = 0;
  2421. while(fgets(temp,8000,fp)!=NULL) {
  2422. cnt = 0;
  2423. linestor = dz->parray[dz->scoredata + linecnt];
  2424. p = temp;
  2425. while(isspace(*p))
  2426. p++;
  2427. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2428. continue;
  2429. read_instrument_name_from_start_of_line(&p,insnam,&insno,orchestra,(int)(linecnt+1));
  2430. linestor[cnt] = (double)insno;
  2431. cnt++;
  2432. while(get_float_from_within_string(&p,&dummy)) {
  2433. linestor[cnt] = dummy;
  2434. cnt++;
  2435. }
  2436. if(!valid_cluster(insno,linestor,cnt,orchestra))
  2437. return DATA_ERROR;
  2438. linestor[cnt] = -1; // End of scoredataline marker
  2439. linecnt++;
  2440. }
  2441. fclose(fp);
  2442. return(FINISHED);
  2443. }
  2444. /************************************ PANCALC *******************************/
  2445. void pancalc(double position,double *leftgain,double *rightgain)
  2446. {
  2447. int dirflag;
  2448. double temp;
  2449. double relpos;
  2450. double reldist, invsquare;
  2451. if(position < 0.0)
  2452. dirflag = SIGNAL_TO_LEFT; /* signal on left */
  2453. else
  2454. dirflag = SIGNAL_TO_RIGHT;
  2455. if(position < 0)
  2456. relpos = -position;
  2457. else
  2458. relpos = position;
  2459. if(relpos <= 1.0){ /* between the speakers */
  2460. temp = 1.0 + (relpos * relpos);
  2461. reldist = ROOT2 / sqrt(temp);
  2462. temp = (position + 1.0) / 2.0;
  2463. *rightgain = temp * reldist;
  2464. *leftgain = (1.0 - temp ) * reldist;
  2465. } else { /* outside the speakers */
  2466. temp = (relpos * relpos) + 1.0;
  2467. reldist = sqrt(temp) / ROOT2; /* relative distance to source */
  2468. invsquare = 1.0 / (reldist * reldist);
  2469. if(dirflag == SIGNAL_TO_LEFT){
  2470. *leftgain = invsquare;
  2471. *rightgain = 0.0;
  2472. } else { /* SIGNAL_TO_RIGHT */
  2473. *rightgain = invsquare;
  2474. *leftgain = 0;
  2475. }
  2476. }
  2477. }
  2478. /**************************** READ_INSTRUMENT_NAME_FROM_START_OF_LINE ****************************/
  2479. int read_instrument_name_from_start_of_line(char **p,char *insnam,int *insno,synptr *orchestra,int linecnt)
  2480. {
  2481. int n = 0;
  2482. char *q;
  2483. q = *p;
  2484. while(!isspace(*q) && *q != ENDOFSTR) {
  2485. insnam[n++] = *q;
  2486. q++;
  2487. }
  2488. if(n == 0) {
  2489. sprintf(errstr,"No instrument name in line %d\n",linecnt);
  2490. return DATA_ERROR;
  2491. }
  2492. insnam[n] = ENDOFSTR;
  2493. while(isspace(*q))
  2494. q++;
  2495. if(*q == ENDOFSTR) {
  2496. sprintf(errstr,"No further data in line %d\n",linecnt);
  2497. return DATA_ERROR;
  2498. }
  2499. for(n=0;n<orchestra_size;n++) {
  2500. if(!strcmp(insnam,orchestra[n]->name)) {
  2501. *insno = n;
  2502. break;
  2503. }
  2504. }
  2505. if(n == orchestra_size) {
  2506. sprintf(errstr,"Unknown instrument name (%s) in line %d\n",insnam,linecnt);
  2507. return DATA_ERROR;
  2508. }
  2509. *p = q;
  2510. return FINISHED;
  2511. }
  2512. /**************************** VALID_CLUSTER ****************************/
  2513. int valid_cluster(int insno,double *linestor,int datacnt,synptr *orchestra)
  2514. {
  2515. int midival[64];
  2516. char insname[64];
  2517. int n = 0, chordcnt = 0, time, midi, lasttime = 0, lastmidi = 0;
  2518. synptr instrument = orchestra[insno];
  2519. int doublestop = instrument->doublestop;
  2520. switch(insno) {
  2521. case(2): strcpy(insname,"pianoRH"); break;
  2522. case(3): strcpy(insname,"pianoLH"); break;
  2523. case(5): strcpy(insname,"violin"); break;
  2524. case(6): strcpy(insname,"cello"); break;
  2525. }
  2526. n = 1;
  2527. while(n < datacnt) {
  2528. time = (int)round(linestor[n++]);
  2529. midi = (int)round(linestor[n++]);
  2530. n += 2;
  2531. if(n > 5) {
  2532. if(time == lasttime) {
  2533. if(chordcnt == 0)
  2534. midival[chordcnt++] = lastmidi;
  2535. midival[chordcnt++] = midi;
  2536. } else {
  2537. if(!check_clustering(insno,chordcnt,doublestop,insname,lasttime,midival))
  2538. return DATA_ERROR;
  2539. chordcnt = 0;
  2540. }
  2541. }
  2542. lasttime = time;
  2543. lastmidi = midi;
  2544. }
  2545. return FINISHED;
  2546. }
  2547. /**************************** CHECK_CLUSTERING ****************************/
  2548. #define FIFTH 7 // semitones
  2549. #define OCTAV 12 // semitones
  2550. int check_clustering(int insno,int chordcnt,int doublestop,char *insname,int time, int *midival)
  2551. {
  2552. int maxmidi = -1000, minmidi = 1000, k, n, range, openstring, position;
  2553. int minposition = 1000, maxposition = -1;
  2554. char openname[4];
  2555. if(chordcnt < 1)
  2556. return FINISHED;
  2557. if(chordcnt > doublestop) {
  2558. sprintf(errstr,"CLUSTER SIZE TOO LARGE in %s at time %d\n",insname,time);
  2559. return DATA_ERROR;
  2560. }
  2561. for(k = 0;k < chordcnt;k++) {
  2562. maxmidi = max(midival[k],maxmidi);
  2563. minmidi = min(midival[k],minmidi);
  2564. }
  2565. range = maxmidi - minmidi;
  2566. switch(insno) {
  2567. case(PIANO_RH): // pianoRH
  2568. case(PIANO_LH): // pianoLH
  2569. if (range > OCTAV) {
  2570. sprintf(errstr,"CLUSTER RANGE (%d) TOO LARGE (MAX %d) in %s at time %d\n",range,OCTAV,insname,time);
  2571. return DATA_ERROR;
  2572. }
  2573. break;
  2574. case(VIOLIN): // violin
  2575. case(CELLO): // cello
  2576. if(insno == VIOLIN)
  2577. openstring = 55;
  2578. else
  2579. openstring = 36;
  2580. n = 0;
  2581. for(k=0;k<chordcnt;k++) {
  2582. if(insno == 5) {
  2583. switch(n) {
  2584. case(0): strcpy(openname,"G"); break;
  2585. case(1): strcpy(openname,"D"); break;
  2586. case(2): strcpy(openname,"A"); break;
  2587. case(3): strcpy(openname,"E"); break;
  2588. }
  2589. } else {
  2590. switch(n) {
  2591. case(0): strcpy(openname,"C"); break;
  2592. case(1): strcpy(openname,"G"); break;
  2593. case(2): strcpy(openname,"D"); break;
  2594. case(3): strcpy(openname,"A"); break;
  2595. }
  2596. }
  2597. if(midival[k] < openstring) {
  2598. sprintf(errstr,"CLUSTER NOTE %d NOT ACCESSIBLE ON %s STRING OF %s",midival[k],openname,insname);
  2599. return DATA_ERROR;
  2600. }
  2601. position = midival[k] - (n * FIFTH);
  2602. minposition = min(minposition,position);
  2603. maxposition = max(maxposition,position);
  2604. n++;
  2605. openstring += FIFTH;
  2606. }
  2607. if (maxposition - minposition > FIFTH) {
  2608. sprintf(errstr,"CLUSTER HAS IMPOSSIBLE STRETCH FROM POSITION %d TO POSITION %d ON %s\n",minposition,maxposition,insname);
  2609. return DATA_ERROR;
  2610. }
  2611. }
  2612. return FINISHED;
  2613. }
  2614. /***************************** MDO_LPHP_FILTER *************************************/
  2615. int mdo_lphp_filter(int sampcnt,int flt_cnt,double flt_mul,dataptr dz)
  2616. {
  2617. int flt_den1 = dz->filterbas;
  2618. int flt_den2 = dz->filterbas+1;
  2619. int flt_count = dz->filterbas+2;
  2620. int flt_s1 = dz->filterbas+3;
  2621. int flt_s2 = dz->filterbas+4;
  2622. int flt_e1 = dz->filterbas+5;
  2623. int flt_e2 = dz->filterbas+6;
  2624. int i;
  2625. int k;
  2626. float *buf = dz->sampbuf[1];
  2627. double ip, op = 0.0, b1;
  2628. double *e1 = dz->parray[flt_e1];
  2629. double *e2 = dz->parray[flt_e2];
  2630. double *s1 = dz->parray[flt_s1];
  2631. double *s2 = dz->parray[flt_s2];
  2632. double *den1 = dz->parray[flt_den1];
  2633. double *den2 = dz->parray[flt_den2];
  2634. double *cn = dz->parray[flt_count];
  2635. for (i = 0 ; i < sampcnt; i++) {
  2636. ip = (double) buf[i];
  2637. for (k = 0 ; k < flt_cnt; k++) {
  2638. b1 = flt_mul * cn[k];
  2639. op = (cn[k] * ip) + (den1[k] * s1[k]) + (den2[k] * s2[k]) + (b1 * e1[k]) + (cn[k] * e2[k]);
  2640. s2[k] = s1[k];
  2641. s1[k] = op;
  2642. e2[k] = e1[k];
  2643. e1[k] = ip;
  2644. }
  2645. if (fabs(op) > 1.0) {
  2646. dz->iparam[FLT_OVFLW]++;
  2647. dz->param[FLT_PRESCALE] *= .9999;
  2648. if (op > 0.0)
  2649. op = 1.0;
  2650. else
  2651. op = -1.0;
  2652. }
  2653. buf[i] = (float)op;
  2654. }
  2655. return FINISHED;
  2656. }
  2657. /********************************* MSETUP_LPHP_FILTER *****************************/
  2658. int msetup_lphp_filter(int *flt_cnt,double *flt_mul,double passfrq,double stopfrq,dataptr dz)
  2659. {
  2660. int flt_den1 = dz->filterbas;
  2661. int flt_den2 = dz->filterbas+1;
  2662. int flt_count = dz->filterbas+2;
  2663. int flt_s1 = dz->filterbas+3;
  2664. int flt_s2 = dz->filterbas+4;
  2665. int flt_e1 = dz->filterbas+5;
  2666. int flt_e2 = dz->filterbas+6;
  2667. int filter_order, k;
  2668. double tc, tp, tt, pii, xx, yy, flt_gain;
  2669. double sr = MSYNSRATE, nyquist = sr/2.0;
  2670. double ss, aa, tppwr, x1, x2, cc;
  2671. *flt_mul = -2.0;
  2672. passfrq = nyquist - passfrq;
  2673. stopfrq = nyquist - stopfrq;
  2674. pii = 4.0 * atan(1.0);
  2675. passfrq = pii * passfrq/sr;
  2676. tp = tan(passfrq);
  2677. stopfrq = pii * stopfrq/sr;
  2678. tc = tan(stopfrq);
  2679. tt = tc / tp ;
  2680. tt = (tt * tt);
  2681. flt_gain = fabs(FILTATTEN);
  2682. flt_gain = flt_gain * log(10.0)/10.0 ;
  2683. flt_gain = exp(flt_gain) - 1.0 ;
  2684. xx = log(flt_gain)/log(tt) ;
  2685. yy = floor(xx);
  2686. if ((xx - yy) == 0.0 )
  2687. yy = yy - 1.0 ;
  2688. filter_order = ((int)yy) + 1;
  2689. if (filter_order <= 1)
  2690. filter_order = 2;
  2691. *flt_cnt = filter_order/2 ;
  2692. filter_order = 2 * *flt_cnt;
  2693. filter_order = 2 * *flt_cnt;
  2694. if(dz->parray[flt_den1] != NULL) {
  2695. free(dz->parray[flt_den1]);
  2696. free(dz->parray[flt_den2]);
  2697. free(dz->parray[flt_count]);
  2698. free(dz->parray[flt_s1]);
  2699. free(dz->parray[flt_e1]);
  2700. free(dz->parray[flt_s2]);
  2701. free(dz->parray[flt_e2]);
  2702. }
  2703. if((dz->parray[flt_den1] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2704. || (dz->parray[flt_den2] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2705. || (dz->parray[flt_count]= (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2706. || (dz->parray[flt_s1] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2707. || (dz->parray[flt_e1] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2708. || (dz->parray[flt_s2] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL
  2709. || (dz->parray[flt_e2] = (double *)malloc(*flt_cnt * sizeof(double)))==NULL) {
  2710. sprintf(errstr,"INSUFFICIENT MEMORY for arrays of filter parameters.\n");
  2711. return(MEMORY_ERROR);
  2712. }
  2713. ss = pii / (double)(2 * filter_order);
  2714. for (k = 0; k < *flt_cnt; k++ ) {
  2715. xx = (double) ((2.0 * (k+1)) - 1.0);
  2716. aa = -sin(xx * ss);
  2717. tppwr = pow(tp,2.0);
  2718. cc = 1.0 - (2.0 * aa * tp) + tppwr;
  2719. x1 = 2.0 * (tppwr - 1.0)/cc ;
  2720. x2 = (1.0 + (2.0 * aa * tp) + tppwr)/cc ;
  2721. dz->parray[flt_den1][k] = x1;
  2722. dz->parray[flt_den2][k] = -x2 ;
  2723. dz->parray[flt_count][k] = pow(tp,2.0)/cc ;
  2724. }
  2725. for (k = 0; k < *flt_cnt; k++) {
  2726. dz->parray[flt_s1][k] = 0.0;
  2727. dz->parray[flt_s2][k] = 0.0;
  2728. dz->parray[flt_e1][k] = 0.0;
  2729. dz->parray[flt_e2][k] = 0.0;
  2730. }
  2731. return(FINISHED);
  2732. }
  2733. /**************************** INITIALISE_INSTRUMENTS ****************************/
  2734. int initialise_instruments(synptr **orchestra)
  2735. {
  2736. int n;
  2737. synptr instrument;
  2738. if((*orchestra = (synptr *)malloc(MAXSCORLINE * sizeof(synptr)))==NULL) {
  2739. sprintf(errstr,"INSUFFICIENT MEMORY for storing pointers to internal instrument data.\n");
  2740. return(MEMORY_ERROR); // Allocate pointers to all instrument data
  2741. }
  2742. for(n = 0;n < MAXSCORLINE; n++) {
  2743. if(((*orchestra)[n] = (synptr)malloc(sizeof(struct synstrument)))==NULL) {
  2744. sprintf(errstr,"INSUFFICIENT MEMORY for storing internal instrument data %d.\n",n+1);
  2745. return(MEMORY_ERROR); // Allocate pointers to this instrument data
  2746. }
  2747. instrument = (*orchestra)[n];
  2748. switch(n) {
  2749. case(0): // trumpet
  2750. strcpy(instrument->name,"trumpet");
  2751. instrument->partial_cnt = 16; // Must tally with no. of partials in instrument spectrum definition
  2752. instrument->line_cnt = 3; // Must tally with no. of (timed) lines in instrument spectrum definition
  2753. instrument->env_len = 12; // Must tally with no. of entries (count time and val as separate entries) in instrument envelope definition
  2754. instrument->has_vibrato = 1;
  2755. instrument->maxvibdepth = 0.3;
  2756. instrument->packettype = 0;
  2757. instrument->squeeze = 1.0;
  2758. instrument->centre = -0.8;
  2759. instrument->rangebot = 54;
  2760. instrument->rangetop = 79;
  2761. instrument->overlap = 0;
  2762. instrument->doublestop = 1;
  2763. instrument->balance = 2;
  2764. instrument->spectrum = trumpet; // Point to relevant static array
  2765. instrument->env = trumpenv; // Point to relevant static array
  2766. break;
  2767. case(1): // clarinet
  2768. strcpy(instrument->name,"clarinet");
  2769. instrument->partial_cnt = 10;
  2770. instrument->line_cnt = 2;
  2771. instrument->env_len = 12;
  2772. instrument->has_vibrato = 0;
  2773. instrument->maxvibdepth = 0.0;
  2774. instrument->packettype = 1;
  2775. instrument->squeeze = 1.0;
  2776. instrument->centre = -0.8;
  2777. instrument->rangebot = 50;
  2778. instrument->rangetop = 91;
  2779. instrument->overlap = 0;
  2780. instrument->doublestop = 1;
  2781. instrument->balance = 1;
  2782. instrument->spectrum = clarinet; // Point to relevant static array
  2783. instrument->env = clarenv; // Point to relevant static array
  2784. break;
  2785. case(2): // piano
  2786. strcpy(instrument->name,"pianoRH");
  2787. instrument->partial_cnt = 12;
  2788. instrument->line_cnt = 4;
  2789. instrument->env_len = 12;
  2790. instrument->has_vibrato = 0;
  2791. instrument->maxvibdepth = 0.0;
  2792. instrument->packettype = 0;
  2793. instrument->squeeze = 1.0;
  2794. instrument->centre = -0.8;
  2795. instrument->rangebot = 48;
  2796. instrument->rangetop = 108;
  2797. instrument->overlap = 1;
  2798. instrument->doublestop = 4;
  2799. instrument->balance = 2.5;
  2800. instrument->spectrum = piano; // Point to relevant static array
  2801. instrument->env = pianoenv; // Point to relevant static array
  2802. break;
  2803. case(3): // piano
  2804. strcpy(instrument->name,"pianoLH");
  2805. instrument->partial_cnt = 12;
  2806. instrument->line_cnt = 4;
  2807. instrument->env_len = 12;
  2808. instrument->has_vibrato = 0;
  2809. instrument->maxvibdepth = 0.0;
  2810. instrument->packettype = 0;
  2811. instrument->squeeze = 1.0;
  2812. instrument->centre = -0.8;
  2813. instrument->rangebot = 21;
  2814. instrument->rangetop = 72;
  2815. instrument->overlap = 1;
  2816. instrument->doublestop = 4;
  2817. instrument->balance = 2.5;
  2818. instrument->spectrum = piano; // Point to relevant static array
  2819. instrument->env = pianoenv; // Point to relevant static array
  2820. break;
  2821. case(4): // flute
  2822. strcpy(instrument->name,"flute");
  2823. instrument->partial_cnt = 20;
  2824. instrument->line_cnt = 2;
  2825. instrument->env_len = 12;
  2826. instrument->has_vibrato = 1;
  2827. instrument->maxvibdepth = 0.5;
  2828. instrument->packettype = 0;
  2829. instrument->squeeze = 1.0;
  2830. instrument->centre = -0.8;
  2831. instrument->rangebot = 60;
  2832. instrument->rangetop = 92;
  2833. instrument->overlap = 0;
  2834. instrument->doublestop = 1;
  2835. instrument->balance = 0.35;
  2836. instrument->spectrum = flute; // Point to relevant static array
  2837. instrument->env = flutenv; // Point to relevant static array
  2838. break;
  2839. case(5): // violin
  2840. strcpy(instrument->name,"violin");
  2841. instrument->partial_cnt = 12;
  2842. instrument->line_cnt = 4;
  2843. instrument->env_len = 12;
  2844. instrument->has_vibrato = 1;
  2845. instrument->maxvibdepth = 1.5;
  2846. instrument->packettype = 0;
  2847. instrument->squeeze = 1.0;
  2848. instrument->centre = -0.8;
  2849. instrument->rangebot = 55;
  2850. instrument->rangetop = 96;
  2851. instrument->overlap = 0;
  2852. instrument->doublestop = 4;
  2853. instrument->balance = 0.5;
  2854. instrument->spectrum = violin; // Point to relevant static array
  2855. instrument->env = violenv; // Point to relevant static array
  2856. break;
  2857. case(6): // cello
  2858. strcpy(instrument->name,"cello");
  2859. instrument->partial_cnt = 11;
  2860. instrument->line_cnt = 4;
  2861. instrument->env_len = 12;
  2862. instrument->has_vibrato = 1;
  2863. instrument->maxvibdepth = 1.8;
  2864. instrument->packettype = 1;
  2865. instrument->squeeze = 1.0;
  2866. instrument->centre = -0.8;
  2867. instrument->rangebot = 36;
  2868. instrument->rangetop = 72;
  2869. instrument->overlap = 0;
  2870. instrument->doublestop = 4;
  2871. instrument->balance = 0.7;
  2872. instrument->spectrum = cello; // Point to relevant static array
  2873. instrument->env = cellenv; // Point to relevant static array
  2874. break;
  2875. // NB: BE SURE TO INCREASE orchestra_size WHEN ADDING A NEW INSTRUMENT!!!!
  2876. }
  2877. instrument->line_entrycnt = (instrument->partial_cnt * 2) + 1; // No of items in line defining spectrum at specific time
  2878. instrument->spec_len = instrument->line_entrycnt * instrument->line_cnt; // Total number of entries for spectral data
  2879. }
  2880. return FINISHED;
  2881. }