libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cmDspBuiltIn.c 154KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmMath.h"
  12. #include "cmFile.h"
  13. #include "cmFileSys.h"
  14. #include "cmSymTbl.h"
  15. #include "cmJson.h"
  16. #include "cmPrefs.h"
  17. #include "cmProcObj.h"
  18. #include "cmDspValue.h"
  19. #include "cmDspCtx.h"
  20. #include "cmDspClass.h"
  21. #include "cmDspFx.h"
  22. #include "cmDspKr.h"
  23. #include "cmMsgProtocol.h"
  24. #include "cmThread.h"
  25. #include "cmUdpPort.h"
  26. #include "cmUdpNet.h"
  27. #include "cmAudioSys.h"
  28. #include "cmDspSys.h"
  29. #include "cmDspPreset.h" // required for cmDspNetSend
  30. #include "cmDspNet.h"
  31. #include "cmAudioFile.h"
  32. #include "cmThread.h" // used for threaded loading in wave table file mode
  33. #include "cmProcObj.h"
  34. #include "cmProcTemplateMain.h"
  35. #include "cmProc.h"
  36. #include "cmMidi.h"
  37. #include "cmProc2.h"
  38. #include "cmVectOpsTemplateMain.h"
  39. /*
  40. About variables:
  41. 1) Variables represent data fields within a DSP object.
  42. 2) Variables may also act as input (kInDsvFl) and/or output (kOutDsvFl) ports.
  43. A. Audio Ports
  44. - Audio output ports (kAudioBufDsvFl) publish a buffer of samples to subscribing
  45. audio input ports on other instances. (See cmDspSysConnectAudio(). )
  46. - Output audio ports are instantiated with physical buffers to hold samples of
  47. audio. Input audio ports contain internal pointers with point to the
  48. audio buffers of connected output ports.
  49. - Set cmDspVarArg_t.cn to the number of channels of audio the buffer will contain.
  50. B. Non-Audio Ports
  51. - Non-audio input ports may register to be called when output port values change.
  52. via cmDspSysInstallCb().
  53. - Type checking is done to guarantee that the output port data type matches one of the
  54. input port types or can be converted to one of the input port types.
  55. [TODO: Check how this is handled in cmDspSetEvent(). Let's say that an input port
  56. takes either a number or string. This conversion is not possible within the
  57. cmDspValue() framework - and yet it should work - or give an error ]
  58. 3) Creating variable instances.
  59. A. All variables must be given default values in the instance constructor
  60. _cmDspXXXAlloc(). This can be done automatically be giving default values
  61. in the var args section of the cmDspSysAllocInst() call or by explicitely
  62. setting default values via cmDspSetDefaultXXX() in _cmDspXXXAlloc().
  63. B. The _cmDspXXXReset() function automatically resets all the instance variables to their
  64. default value. By default, variables do not transmit their values when they are
  65. reset to their default value - unless the variable is marked with the kSendDfltDsvFl.
  66. (See cmDspClass.c:cmDspApplyDefault()). The 'out' port on cmDspScalar_t and cmDspButton_t
  67. are examples of instances that transmit their default value on reset.
  68. The order that instances are reset is determined by the order that they were created.
  69. A subtle case can arise when relying on default values to set the initial value of
  70. another object. Given two object instances A and B. Where The output of A has the
  71. the kSendDfltDsvFl set and is connected to an input port on B. If instance A was
  72. created before instance B then instance A will reset before instance B. During reset
  73. the output port of A will transmit it's default value to B - as expected. However
  74. when reset is called on instance B it will overwrite this value with it's own
  75. default value.
  76. In order for this scenario to work - that is the output of instance A sets the initial
  77. value of instance B - instance B must be created first.
  78. Since creation order also determines execution order this solution may not always
  79. be possible. It may be that we would simultaneously like instance A to exeute first
  80. and also recieve its initial value from instance B. Given the above example however
  81. these two goals are mutually exclusive.
  82. [*] This could be solved by explicitely allowing a variable to be reset via
  83. a callback. In this case if a variable was set via a callback during reset
  84. then it would not overwrite that value with its own internal value. This
  85. functionality would need to be set on a per instance bases - which might
  86. not work well with the current cmDspSysAllocInst() var args scheme.
  87. When a single output port is connected to multiple input ports the input ports
  88. are called in the order that the connections were made.
  89. To overcome this problem the reset cycle has been broken into two parts.
  90. In the first part the resetFunc is called on each instance in creation order.
  91. (as described above). Following this pass a second pass is made where the
  92. sysRecvFunc on any instance tagged with the '_reset' attribute is called.
  93. This allows an event callback chain to be executed prior to the first
  94. set of execFunc calls. Note that the event callback chain(s) are programmed
  95. by cmDspInstallCb() connections which are setup on a per application basis.
  96. This allows the initial state of the network to be set outside of the
  97. creation order.
  98. The 'button' instance implements a sysRecvFunc which will send the buttons
  99. value and symbol when called with the '_reset' attribute symbol. If a
  100. 'reset' button is created and assigned the '_reset' attribute symbol
  101. (via cmDspSysAssignAttrSymbol()) then the output of the button can be used
  102. to drive a networks initial state in an order determined by the application
  103. programmer.
  104. [TODO: Add a check that all instance variables have default values at the end of
  105. the network reset. This should be easy because the instance variable flag
  106. kDfltSetDsvFl is set in cmDspValueSet() when the default value is set.]
  107. C. The default value of variables may be set via the var args portion of cmDspSysAllocInst().
  108. There is currently a weakness during variable instance creation function
  109. cmDspInstAlloc() uses the cmDspVarArg_t.flags to determine the type of the var arg
  110. argument. cmDspVarArg_t.flags can therefore only be set to one DSV type - and the
  111. actual argument in the cmDspSysAllocInst() call must match that type. This is very
  112. easy to mess up - for example by setting the flag to kIntDsvFl and then putting
  113. 0.0 as the var arg value. This limitation also prevents ports which are also
  114. required arguments to cmDspSysAllocInst() (i.e. kReqArgDsvFl) from supporting
  115. multiple types.
  116. There are two possible ways to fix this:
  117. 1) Include the type flag in the var args list.
  118. 2) Specify a seperate var args flag in cmDspVarArg_t.
  119. Option 1 seems better because:
  120. a. [**] It would allow setting other per instance flags in the cmDspSysAllocInst() call [*].
  121. b. It would support setting a var arg termination flag rather than relying on the current
  122. explicit argument count.
  123. c. This is also the method used in cmJsonMemberValues() and it has worked well there.
  124. Either of these options requires a substantial change to existing code.
  125. This change should therefore be made sooner rather than later.
  126. As the system currently works it is possible, and it often happens, that
  127. an instance's recv function will be called before it is reset. It seems like
  128. this is a bad thing. Maybe the process of resetting and transmitting dflt
  129. values should be broken into seperate passes. This idea could be extended
  130. to type checking as follows:
  131. a. a reset-0 pass is made to set the internal state of each instance to
  132. known values.
  133. b. a type check pass is made where each output port
  134. sends a type msg to all connected input ports to provide the types that
  135. it will send.
  136. c. a reset-1 pass is made allowing all of the type information to be
  137. used to determine the initial state.
  138. d. an initial default value transmission pass is made where some instances
  139. (like scalars) may transmit initial values
  140. e. a reset-2 pass is made allowing the initial values to be acted on.
  141. f. runtime occurs
  142. D. Questions:
  143. 0) What are the ways that an instance variable can get set?
  144. a. cmDspValueSet() - This is the canonical value setting function.
  145. All other value setting function call this function.
  146. The cmDspSetXXX() are type safe wrappers to this function.
  147. b. cmDspApplyDefault() - assign the default value to the actual variable
  148. via a call to cmDspValueSet(). This function is automatically called
  149. by cmDspApplyAllDefaults() which is usually in the instance reset function.
  150. c. cmDspSetEvent() - assign the value embedded in an event message
  151. to a variable. This is a wrapper function for cmDspValueSet() which
  152. checks to see if the value need to be echoed to the UI. See
  153. more about UI echoing in the next section.
  154. 1) Where do events arriving at an instance receive function originate from?
  155. There are two sources of events:
  156. 1. The output ports of other instances.
  157. 2. The UI (client application).
  158. Events arriving from the UI can be distinguished from events arriving
  159. from other instances because they have the evt.kUiDspFl flag set.
  160. 2) How does UI updating and echoing actually work?
  161. a. Overview:
  162. On creation (in cmDsUi.c) variables whose value must be reflected to the UI
  163. are marked with the kUiDsvFl - these variables are called 'UI variables'.
  164. When a UI variable receive a new value
  165. the new value must be reflected in the associated UI GUI control.
  166. When and how this is accomplished depends on the source of the new
  167. variable value. There are two sources of events arriving at
  168. an instance's receive function: the output port of another instance
  169. or the UI.
  170. Events which originate from the UI are marked with a evt.kUiDspFl.
  171. (cmDspSys.c:_cmDspSysHandleUiMsg())
  172. When a UI variable receives a value from the output port of another
  173. instance (i.e. kUiDspFl not set) then it must always send that value to
  174. the UI. In other words if the evt.kUiDspFl is NOT set then the
  175. value must be sent to the UI.
  176. When a UI variable receives a value from the UI (i.e. the evt.kUiDspFl is set)
  177. it may or may not need to reflect the value. If the UI already
  178. reflects the value then the value does not need to be sent back
  179. otherwise it does.
  180. The UI control determines whether it wants to receive the value it
  181. is sending back by setting the kDuplexDuiFl flag in it's msg to the engine.
  182. Upon receipt of this msg, in cmDspSys.c:_cmDspSysHandleUiMsg(),
  183. the system converts the msg to an event. If the kDuplexDuiFl
  184. is set then the kUiEchoDspFl is set in the event.
  185. When a UI variable receives a value from the UI and the evt.kUiEchoDspFl
  186. is set then the value must be reflected, otherwise it must not.
  187. The rules for updating UI variables (var. w/ kUiDsvFl set) can
  188. be summarized as follows:
  189. kUiDspFl kUiEchoDspFl Send to UI Notes
  190. -------- ------------ ---------- -------------------------------------------------------------
  191. 0 0 Yes The value originated from another port and therefore must be reflected.
  192. 0 1 <invalid> If kUiEchoDspFl is set then so must kUiDspFl.
  193. 1 0 No
  194. 1 1 Yes The value originated from the UI and ehco was requested.
  195. This logic is automatically handled together by
  196. cmDspSetEvent() and cmDspValueSet().
  197. b. Variable values are sent and received to and from the UI using
  198. kValueDuiId messages.
  199. c. It is possible to prevent values generated in the engine from being
  200. reflected to the UI by setting the kNoUpdateUiDspFl in the call to
  201. cmDspValueSet().
  202. d. Instance variables are marked as UI variables by the cmDspUIXXXCreate()
  203. functions. Note that instances that have assoicated UI controls generally
  204. have multiple UI variables. (e.g. min,max,step,label,value).
  205. e. Messages arriving from the UI are handled by cmDspSys.c:_cmDspSysHandleUiMsg()
  206. where they are converted to cmDspEvt_t's. All events generated from
  207. msgs arriving from the UI are marked with the kUiDspFl. If the msg kDuplexDuiFl
  208. is set then the event flag kUiEchoDspFl is set - to indicate that the instance
  209. should send the value back to the UI.
  210. This leads to the following potential situation: The msg with kUiEchoDspFl
  211. set arrives at its target instance - which in turn calls cmDspSetEvent() to update
  212. the target variable value. Because kUiEchoDspFl is set the value is automatically
  213. reflected to the UI as expected. However as part of the call to
  214. cmDspValueSet() within cmDspSetEvent() the event is also sent to any connected
  215. instances - a problem would occur if kUiEchoDspFl remained set when it
  216. was sent to the connected instances - because they might then also try to update
  217. their own UI inappropriately. In fact this is not a problem because
  218. _cmDspSendEvt() zeros the the evt.flags value prior to resending the event.
  219. 3) Proposed Data Typing Framework:
  220. a. Instance variables are assigned 3 data types:
  221. 1. cmDspSysAllocInst() var args type. This is the type that the var args argument
  222. to the instance constructor must be. The value provided by this method
  223. becomes the default value for the variable. This type must be unique - multiple
  224. type flags cannot be used for this value.
  225. 2. Strict data type. This is the type used to define the variable value.
  226. Any values which will be used to set the value of this variable must be able
  227. to be converted to this type.
  228. If the variable is used as on input then the system will warn if an output port
  229. is assigned which cannot be converted to this type. If the variable is used as
  230. an output then this is the type the variable will publish as the output type.
  231. All values arriving at the functions 'recv' function are guaranteed to be
  232. able to be converted to this type.
  233. 3. Alternate data types. Multiple types may be given to this type.
  234. If no strict data type is given then this will be the set of types accepted
  235. for input and reported for output.
  236. All value messages for the instance which do not fit the strict data type,
  237. but do fit the alternate data type, will be sent to the 'altRecvFunc'
  238. function. The data type of events arriving at this function may therefore
  239. need to be decoded in order to use the assoicated values.
  240. b. Connections from an output with a strict data type can be type checked in
  241. advance - since they are guaranteed to emit a specific data type.
  242. Connections from outputs without strict data types can only be type checked
  243. at runtime - since it is possible for the type to change once the execution
  244. starts.
  245. c. Other notes about data types:
  246. It seems like cmDsvValues() should in general only allow one type flag (along with
  247. the kMtxDsvFl) to be set at a time if the flag state is legal. Is this always the
  248. case? Can a macro be included to routinely check this? Are we careful to
  249. not confuse the actual and possible type flags in DSP instance variables?
  250. This is important when checking the types of variables arriving at in input port.
  251. Include a macro to test the legality of the actual value type leaving output ports
  252. and entering input ports.
  253. Note that the cmXXXDsvFl flags have the problem that it is not possible to
  254. specify some multiple types. For example it is not possible to specify both
  255. scalar and matrix types simultaneously. Once the matrix flag is set it must
  256. be assumed that all specific data types then are matrices.
  257. THIS IS A FUNDAMENTAL PROBLEM THAT MUST BE ADDRESSED.
  258. Note that scalar numeric values can easily be cheaply converted to other numeric
  259. types. It could be expensive however if vectors required conversion.
  260. Vectors are currently being passed as pointers. No conversion is occurring.
  261. d. Another proposal:
  262. Following connection time there is a type determination pass. The network
  263. is traversed in execution order. Each instance computes and emits the single
  264. type assigned to each of its output ports. Once emited these types will not
  265. change during runtime.
  266. Note that this does not preclude an input receiving multiple types.
  267. If a variable arrives at an input port which does not match the type of
  268. the variable associated with that type then it is sent to the NoTypeRecv()
  269. instance function.
  270. 2. Do values only get sent on change? If not - why not?
  271. No - based on cmDspClass.ccmDspValueSet() values are transmitted
  272. whenever they are set - there is not check for a change of value.
  273. 3. Write a function to support generating multiple enumerated
  274. ports - as is required by AMix or ASplit.
  275. (This is now done: See cmDspClass.h:cmDspArgSetup() )
  276. Update the existing code to use this scheme. (This still needs to be done.)
  277. Similar functions need to be written for connecting groups of ports
  278. in cmDspPgm.c. (This still needs to be done.)
  279. 5. It does not appear that the kInDsvFl and kOutDsvFl are actually used during
  280. the connection process. This means that a variable marked as an output
  281. could be used as an input and v.v.. Likewise variable marked as neither input
  282. nor output could be accessed. What are the input and output flags actually
  283. used for?
  284. In fact kInDsvFl and kOutDsvFl are not used at all. (3/18/12).
  285. It might be nice to allow everything to be an output - but to
  286. force inputs to be explicitely named.
  287. 6. Is there any implication for marking a variable as both an input and an output?
  288. 7. The audio buffers allocated in cmDspInstAlloc() may not be memory aligned.
  289. 8. Is there a way to add a generic enable/disable function to all instances?
  290. 9. Should all output audio buffers be zeroed by default during _cmDspXXXReset()?
  291. 10. Is the kConstDsvFl used? respected? necessary?
  292. yes - it is necessary because some variables cannot be changed after the constructor
  293. is completed. For example any instance that take an argument giving the
  294. number of ports as a variable. The port count argument cannot change because it
  295. might invalidate connections which had been already made to the existing ports.
  296. TODO: find all variables which cannot be changed after the constructor and mark
  297. them as const and prevent them from being the target of connections or events.
  298. 11. Is it OK to not assign a variable as either an input or an output. (this would
  299. allow it to be set from cmDspSysAllocInst() but then only changed internally).
  300. 12. Write some template DSP instances that provide commented examples of the
  301. common scenarios which an actual instance might encounter.
  302. 13. All errors in instances should use cmDspClassErr() or cmDspInstErr() not cmErrMsg().
  303. Update existing code.
  304. 14. The way that the master controls are created is wrong. The master controls should
  305. be created during the cmDspSysLoad() process rather than being created in kcApp.cpp.
  306. This would make them essentially identical to other controls - and would allow the
  307. master controls to be manipulated easily from inside a DSP instance.
  308. 15. The code for creating and decoding messages seems to be distributed everywhere.
  309. All of this functionality should be moved to cmMsgProtocol.c. See the code
  310. for encoding/decoding messages in cmAudioSys.c as an example.
  311. 16. The default behavior of buttons should be to to NOT send out their default values or
  312. symbols on instance reset. Determining whether an output value is sent on instance
  313. reset (as they all are currently ??? or are only UI sent out on reset???)
  314. could be another argument flag setting [**].
  315. 17. cmDspInstAlloc() should include another version called cmDspInstAllocV()
  316. which takes the cmDspArg_t fields as var args. This would allow
  317. array variables which currently use cmDspArgSetupN() to be given
  318. in one call - which would be less error prone than using cmDspArgSetupN().
  319. 18. Add helper functions to create common dsp instances like:
  320. scalar,button,check,file,audio in,audio out. These functions should
  321. support default values through literals or through resource paths.
  322. 19. Design a sub-net function for making sub-nets of instance nets
  323. that can then be treated like instances themselves.
  324. For example make a network of audio sources:
  325. audio file, signal generator, audio input, with gain and frequency
  326. controls.
  327. 20. Network construction (cmDspPgm.c) is divided into two parts.
  328. First the instances are allocated and then they are connected.
  329. There should always be a test for a failure between construction
  330. phase and the connection phase and then again after the connection phase.
  331. 21. The existing instances are not using cmReal_t and cmSample_t as they should.
  332. 22. It is possible for cmDspInstAlloc() to fail in an instance constructor and
  333. yet we are not testing for it in many instances.
  334. When a failure occurs after cmDspInstAlloc() how is the instance deleted
  335. prior to returning? ... is it necessary to delete it prior to returning?
  336. 23. For instances which act as files and which take a file name as at an input
  337. port - the correct way to implement the object is to open/reopen the file
  338. both on reset and when a new file name is received.
  339. The reset open covers the case where the default filename is used.
  340. The receieve open covers the case where a filename is received via the input port.
  341. 24. After each call to an instance member function (reset,recv,exec,etc.) the
  342. interal error object should be checked. This way an invalid state can
  343. be signaled inside one of the functions without having to worry about
  344. propagating the error to the return value. THis means that as long as
  345. a member function can report and safely complete it doesn't have to do
  346. much error handling internally.
  347. 25. As it is currently implemented all audio system sub-system share a
  348. single UDP network managers. This is NOT thread-safe. If more than
  349. one audio sub-system is actually used the program will crash.
  350. This can be solved by giving each sub-system it's own UDP network
  351. manager, where each sub-system is given it's own port number.
  352. */
  353. //==========================================================================================================================================
  354. enum
  355. {
  356. kLblPrId,
  357. kMsPrId,
  358. kInPrId
  359. };
  360. cmDspClass_t _cmPrinterDC;
  361. typedef struct
  362. {
  363. cmDspInst_t inst;
  364. unsigned limitCycles;
  365. } cmDspPrinter_t;
  366. cmDspInst_t* _cmDspPrinterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  367. {
  368. cmDspVarArg_t args[] =
  369. {
  370. { "lbl", kLblPrId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label" },
  371. { "ms", kMsPrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Period"},
  372. { "in", kInPrId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  373. { NULL, 0, 0, 0, 0 }
  374. };
  375. cmDspPrinter_t* p = cmDspInstAlloc(cmDspPrinter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  376. cmDspSetDefaultUInt(ctx,&p->inst,kMsPrId, 0, 0 );
  377. return &p->inst;
  378. }
  379. cmDspRC_t _cmDspPrinterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  380. {
  381. cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
  382. p->limitCycles = ctx->cycleCnt;
  383. return kOkDspRC;
  384. }
  385. cmDspRC_t _cmDspPrinterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  386. {
  387. cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
  388. if( evt->dstVarId == kInPrId && ctx->cycleCnt >= p->limitCycles )
  389. {
  390. p->limitCycles = ctx->cycleCnt + (unsigned)(cmDspUInt(inst,kMsPrId) * cmDspSampleRate(ctx) / (1000.0 * cmDspSamplesPerCycle(ctx)) );
  391. const cmChar_t* lbl = cmDspStrcz(inst,kLblPrId);
  392. if( cmDsvIsSymbol(evt->valuePtr) )
  393. cmRptPrintf(ctx->rpt,"%s'%s'",lbl==NULL?"":lbl,cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  394. else
  395. cmDsvPrint(evt->valuePtr,lbl,ctx->rpt);
  396. cmRptPrint(ctx->rpt,"\n");
  397. }
  398. return kOkDspRC;
  399. }
  400. struct cmDspClass_str* cmPrinterClassCons( cmDspCtx_t* ctx )
  401. {
  402. cmDspClassSetup(&_cmPrinterDC,ctx,"Printer",
  403. NULL,
  404. _cmDspPrinterAlloc,
  405. NULL,
  406. _cmDspPrinterReset,
  407. NULL,
  408. _cmDspPrinterRecv,
  409. NULL,
  410. NULL,
  411. "Print the value of any event arriving at 'in'.");
  412. return &_cmPrinterDC;
  413. }
  414. //==========================================================================================================================================
  415. enum
  416. {
  417. kMinCntId,
  418. kMaxCntId,
  419. kIncCntId,
  420. kWrapCntId,
  421. kOutCntId,
  422. kCycCntId,
  423. kNxtCntId,
  424. };
  425. cmDspClass_t _cmCounterDC;
  426. typedef struct
  427. {
  428. cmDspInst_t inst;
  429. double val;
  430. bool disableFl;
  431. } cmDspCounter_t;
  432. cmDspRC_t _cmDspCounterValidate( cmDspInst_t* inst, double min, double max, double inc )
  433. {
  434. if( max < min )
  435. return cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"The counter maximum (%f) value must be greater than the counter minimum (%f) value.",max,min);
  436. if( max - min < inc )
  437. return cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"The counter increment value (%f) must be less than or equal to the maximum - minimum difference (%f).",inc,max-min);
  438. return kOkDspRC;
  439. }
  440. cmDspInst_t* _cmDspCounterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  441. {
  442. cmDspVarArg_t args[] =
  443. {
  444. { "min", kMinCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "minimum" },
  445. { "max", kMaxCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "maximum" },
  446. { "inc", kIncCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "increment"},
  447. { "wrap", kWrapCntId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "wrap"},
  448. { "out", kOutCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "out"},
  449. { "cycles", kCycCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "cycles"},
  450. { "next", kNxtCntId, 0, 0, kInDsvFl | kTypeDsvMask, "next"},
  451. { NULL, 0, 0, 0, 0 }
  452. };
  453. cmDspCounter_t* p = cmDspInstAlloc(cmDspCounter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  454. double min = cmDspDefaultDouble( &p->inst, kMinCntId );
  455. double max = cmDspDefaultDouble( &p->inst, kMaxCntId );
  456. double inc = cmDspDefaultDouble( &p->inst, kIncCntId );
  457. if( _cmDspCounterValidate(&p->inst, min, max, inc ) != kOkDspRC )
  458. return NULL;
  459. cmDspSetDefaultBool( ctx, &p->inst, kWrapCntId, false, true);
  460. cmDspSetDefaultDouble( ctx, &p->inst, kOutCntId, 0.0, min );
  461. cmDspSetDefaultDouble( ctx, &p->inst, kCycCntId, 0.0, 0.0 );
  462. return &p->inst;
  463. }
  464. cmDspRC_t _cmDspCounterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  465. {
  466. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  467. cmDspApplyAllDefaults(ctx,inst);
  468. p->val = cmDspDouble(inst,kMinCntId);
  469. p->disableFl = false; // the default values must be ok or the constructor fails
  470. return kOkDspRC;
  471. }
  472. void _cmDspCounterIncr( cmDspCtx_t* ctx, cmDspInst_t* inst )
  473. {
  474. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  475. double min = cmDspDouble(inst,kMinCntId);
  476. double max = cmDspDouble(inst,kMaxCntId);
  477. double inc = cmDspDouble(inst,kIncCntId);
  478. bool wrapFl = cmDspBool(inst,kWrapCntId);
  479. bool limitFl = min <= max;
  480. // If min > max then no upper/lower limit is set on the value.
  481. // In this case the ouput will continue to increment and
  482. // no 'cycle' output will be generated.
  483. // If wrapFl is not set then the 'cycle' output will fire
  484. // exactly once when the counter crosses its limit.
  485. // if the new value is in range then send it
  486. if( min <= p->val && p->val < max )
  487. cmDspSetDouble( ctx, inst, kOutCntId, p->val );
  488. // the current value is out of range and wrap flag is not set
  489. if( limitFl && (p->val < min || p->val >= max) && (wrapFl==false))
  490. return;
  491. // do the increment
  492. p->val += inc;
  493. // if the new value is out of range
  494. if( limitFl && (p->val < min || p->val >= max) )
  495. {
  496. // if wrapping is allowed
  497. if( wrapFl )
  498. {
  499. if( p->val >= max )
  500. p->val = min + (p->val - max); // wrap to begin
  501. else
  502. if( p->val < min )
  503. p->val = max - (min - p->val); // wrap to end
  504. }
  505. // increment the cycle counter
  506. cmDspSetDouble( ctx, inst, kCycCntId, cmDspDouble( inst, kCycCntId ) + 1 );
  507. }
  508. }
  509. cmDspRC_t _cmDspCounterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  510. {
  511. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  512. cmDspRC_t rc = kOkDspRC;
  513. switch( evt->dstVarId )
  514. {
  515. case kWrapCntId:
  516. cmDspSetEvent(ctx,inst, evt );
  517. break;
  518. case kMinCntId:
  519. case kMaxCntId:
  520. case kIncCntId:
  521. {
  522. cmDspSetEvent( ctx, inst, evt );
  523. double min = cmDspDouble( inst, kMinCntId );
  524. double max = cmDspDouble( inst, kMaxCntId );
  525. double inc = cmDspDouble( inst, kIncCntId );
  526. p->disableFl = (rc = _cmDspCounterValidate(inst, min, max, inc)) != kOkDspRC;
  527. }
  528. break;
  529. case kNxtCntId:
  530. if( !p->disableFl )
  531. _cmDspCounterIncr(ctx,inst);
  532. break;
  533. default:
  534. { assert(0); }
  535. }
  536. return rc;
  537. }
  538. struct cmDspClass_str* cmCounterClassCons( cmDspCtx_t* ctx )
  539. {
  540. cmDspClassSetup(&_cmCounterDC,ctx,"Counter",
  541. NULL,
  542. _cmDspCounterAlloc,
  543. NULL,
  544. _cmDspCounterReset,
  545. NULL,
  546. _cmDspCounterRecv,
  547. NULL,
  548. NULL,
  549. "Counter object. Set min => max to have no limit on the value." );
  550. return &_cmCounterDC;
  551. }
  552. //==========================================================================================================================================
  553. enum
  554. {
  555. kMaxPhId,
  556. kMultPhId,
  557. kPhsPhId,
  558. kOutPhId
  559. };
  560. cmDspClass_t _cmPhasorDC;
  561. typedef struct
  562. {
  563. cmDspInst_t inst;
  564. } cmDspPhasor_t;
  565. cmDspInst_t* _cmDspPhasorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  566. {
  567. unsigned chs = 1;
  568. cmDspVarArg_t args[] =
  569. {
  570. { "max", kMaxPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Maximum accumulator value" },
  571. { "mult", kMultPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Increment multiplier" },
  572. { "phs", kPhsPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Next phase value" },
  573. { "out", kOutPhId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  574. { NULL, 0, 0, 0, 0 }
  575. };
  576. // allocate the instance
  577. cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  578. // assign default values to any of the the optional arg's which may not have been set from vl.
  579. cmDspSetDefaultSample(ctx, &p->inst, kMaxPhId, 0.0, cmSample_MAX);
  580. cmDspSetDefaultSample(ctx, &p->inst, kMultPhId, 0.0, 1.0);
  581. cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId, 0.0, 0.0);
  582. return &p->inst;
  583. }
  584. cmDspRC_t _cmDspPhasorReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  585. {
  586. cmDspApplyAllDefaults(ctx,inst);
  587. cmDspZeroAudioBuf( ctx, inst, kOutPhId );
  588. return kOkDspRC;
  589. }
  590. cmDspRC_t _cmDspPhasorExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  591. {
  592. cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutPhId,0);
  593. const cmSample_t* ep = bp + cmDspAudioBufSmpCount(ctx,inst,kOutPhId,0);
  594. cmSample_t mult = cmDspSample(inst,kMultPhId);
  595. cmSample_t max = cmDspSample(inst,kMaxPhId);
  596. double phs = cmDspDouble(inst,kPhsPhId);
  597. cmSample_t inc = mult;
  598. for(; bp<ep; ++bp)
  599. {
  600. while( phs >= max )
  601. phs -= max;
  602. *bp = (cmSample_t)phs;
  603. phs += inc;
  604. }
  605. cmDspSetSample(ctx,inst,kPhsPhId,phs);
  606. return kOkDspRC;
  607. }
  608. cmDspRC_t _cmDspPhasorRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  609. {
  610. switch( evt->dstVarId )
  611. {
  612. case kMultPhId:
  613. case kMaxPhId:
  614. case kPhsPhId:
  615. cmDspSetEvent(ctx, inst, evt );;
  616. break;
  617. default:
  618. { assert(0); }
  619. }
  620. return kOkDspRC;
  621. }
  622. struct cmDspClass_str* cmPhasorClassCons( cmDspCtx_t* ctx )
  623. {
  624. cmDspClassSetup(&_cmPhasorDC,ctx,"Phasor",
  625. NULL,
  626. _cmDspPhasorAlloc,
  627. NULL,
  628. _cmDspPhasorReset,
  629. _cmDspPhasorExec,
  630. _cmDspPhasorRecv,
  631. NULL,
  632. NULL,
  633. "Ramp wave signal generator.");
  634. return &_cmPhasorDC;
  635. }
  636. //==========================================================================================================================================
  637. enum
  638. {
  639. kHzSgId,
  640. kShapeSgId,
  641. kGainSgId,
  642. kOtCntSgId,
  643. kOutSgId
  644. };
  645. enum
  646. {
  647. kWhiteSgId, // 0
  648. kPinkSgId, // 1
  649. kSineSgId, // 2
  650. kCosSgId, // 3
  651. kSawSgId, // 4
  652. kSqrSgId, // 5
  653. kTriSgId, // 6
  654. kPulseSgId, // 7
  655. kPhasorSgId // 8
  656. };
  657. cmDspClass_t _cmSigGenDC;
  658. typedef struct
  659. {
  660. cmDspInst_t inst;
  661. cmReal_t phs;
  662. cmSample_t reg;
  663. } cmDspSigGen_t;
  664. cmDspInst_t* _cmDspSigGenAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  665. {
  666. cmDspVarArg_t args[] =
  667. {
  668. { "hz", kHzSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Signal frequency in Hertz." },
  669. { "shape", kShapeSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=sine 1=cosine 2=white 3=pink" },
  670. { "gain", kGainSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain."},
  671. { "ot", kOtCntSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count."},
  672. { "out", kOutSgId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  673. { NULL, 0, 0, 0, 0 }
  674. };
  675. cmDspSigGen_t* p = cmDspInstAlloc(cmDspSigGen_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  676. cmDspSetDefaultDouble(ctx, &p->inst, kHzSgId, 0.0, 1000);
  677. cmDspSetDefaultUInt( ctx, &p->inst, kShapeSgId, 0, 0);
  678. cmDspSetDefaultUInt( ctx, &p->inst, kOtCntSgId, 0, 0);
  679. cmDspSetDefaultDouble(ctx, &p->inst, kGainSgId, 0.0, 0.9 );
  680. return &p->inst;
  681. }
  682. cmDspRC_t _cmDspSigGenReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  683. {
  684. cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
  685. p->phs = 0;
  686. cmDspApplyAllDefaults(ctx,inst);
  687. cmDspZeroAudioBuf( ctx, inst, kOutSgId );
  688. p->reg = 0;
  689. return kOkDspRC;
  690. }
  691. cmDspRC_t _cmDspSigGenExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  692. {
  693. cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
  694. unsigned chIdx = 0;
  695. cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutSgId,chIdx);
  696. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutSgId,chIdx);
  697. const cmSample_t* ep = bp + n;
  698. double hz = cmDspDouble(inst,kHzSgId);
  699. double sr = cmDspSampleRate(ctx);
  700. double fact = 2.0 * M_PI * hz / sr;
  701. int shape = cmDspUInt(inst,kShapeSgId);
  702. double offs = shape == kCosSgId ? (-M_PI / 2.0) : 0;
  703. double gain = cmDspDouble(inst,kGainSgId);
  704. unsigned otCnt = cmDspUInt(inst,kOtCntSgId);
  705. switch( shape )
  706. {
  707. case kWhiteSgId:
  708. while( bp < ep )
  709. *bp++ = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
  710. break;
  711. case kPinkSgId:
  712. while( bp < ep )
  713. {
  714. cmSample_t s = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
  715. *bp++ = (s + p->reg)/2;
  716. p->reg = s;
  717. }
  718. break;
  719. case kSineSgId:
  720. case kCosSgId:
  721. while( bp<ep )
  722. {
  723. *bp++ = (cmSample_t)(gain * sin( fact * p->phs + offs ));
  724. p->phs += 1.0;
  725. }
  726. break;
  727. case kSawSgId:
  728. p->phs = cmVOS_SynthSawtooth(bp,n,(unsigned)p->phs,sr,hz,otCnt);
  729. cmVOS_MultVS(bp,n,gain);
  730. break;
  731. case kSqrSgId:
  732. p->phs = cmVOS_SynthSquare( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  733. cmVOS_MultVS(bp,n,gain);
  734. break;
  735. case kTriSgId:
  736. p->phs = cmVOS_SynthTriangle( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  737. cmVOS_MultVS(bp,n,gain);
  738. break;
  739. case kPulseSgId:
  740. p->phs = cmVOS_SynthPulseCos( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  741. cmVOS_MultVS(bp,n,gain);
  742. break;
  743. case kPhasorSgId:
  744. p->phs = cmVOS_SynthPhasor( bp,n,(unsigned)p->phs,sr,hz );
  745. cmVOS_MultVS(bp,n,gain);
  746. break;
  747. default:
  748. { assert(0); }
  749. }
  750. return kOkDspRC;
  751. }
  752. cmDspRC_t _cmDspSigGenRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  753. {
  754. cmDspRC_t rc;
  755. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  756. {
  757. switch( evt->dstVarId )
  758. {
  759. case kShapeSgId:
  760. //printf("%s %i\n",cmDspInstLabel(ctx,inst),cmDspUInt(inst,kShapeSgId));
  761. break;
  762. }
  763. }
  764. return rc;
  765. }
  766. struct cmDspClass_str* cmSigGenClassCons( cmDspCtx_t* ctx )
  767. {
  768. cmDspClassSetup(&_cmSigGenDC,ctx,"SigGen",
  769. NULL,
  770. _cmDspSigGenAlloc,
  771. NULL,
  772. _cmDspSigGenReset,
  773. _cmDspSigGenExec,
  774. _cmDspSigGenRecv,
  775. NULL,
  776. NULL,
  777. "Variable frequency and waveshape signal generator." );
  778. return &_cmSigGenDC;
  779. }
  780. //==========================================================================================================================================
  781. enum
  782. {
  783. kChAiId,
  784. kGainAiId,
  785. kOutAiId
  786. };
  787. cmDspClass_t _cmAudioInDC;
  788. typedef struct
  789. {
  790. cmDspInst_t inst;
  791. bool errFl; // used to control error reporting
  792. } cmDspAudioIn_t;
  793. cmDspInst_t* _cmDspAudioInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  794. {
  795. cmDspVarArg_t args[] =
  796. {
  797. { "ch", kChAiId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio input channel index"},
  798. { "gain", kGainAiId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain multiplier" },
  799. { "out", kOutAiId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  800. { NULL, 0, 0, 0, 0 }
  801. };
  802. cmDspAudioIn_t* p = cmDspInstAlloc(cmDspAudioIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  803. cmDspSetDefaultUInt( ctx, &p->inst, kChAiId, 0, 0);
  804. cmDspSetDefaultDouble( ctx, &p->inst, kGainAiId, 0, 1.0);
  805. return &p->inst;
  806. }
  807. cmDspRC_t _cmDspAudioInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  808. {
  809. cmDspRC_t rc = kOkDspRC;
  810. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  811. p->errFl = false;
  812. cmDspApplyAllDefaults(ctx,inst);
  813. return rc;
  814. }
  815. cmDspRC_t _cmDspAudioInExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  816. {
  817. unsigned chIdx = cmDspUInt(inst,kChAiId);
  818. unsigned iChCnt = ctx->ctx->iChCnt;
  819. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  820. double gain = cmDspDouble(inst,kGainAiId);
  821. if( chIdx >= iChCnt )
  822. {
  823. if( p->errFl== false )
  824. {
  825. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The input channel index %i is invalid. Channel count:%i.",chIdx,iChCnt);
  826. p->errFl = true;
  827. }
  828. cmDspZeroAudioBuf(ctx,inst,kOutAiId);
  829. return kOkDspRC;
  830. }
  831. //unsigned n = cmDspSamplesPerCycle(ctx);
  832. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAiId,0);
  833. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAiId,0);
  834. assert( n == cmDspAudioBufSmpCount(ctx,inst,kOutAiId,chIdx));
  835. assert(dp != NULL);
  836. // if this channel is disabled then iChArray[chIdx] will be NULL
  837. if( ctx->ctx->iChArray[chIdx]!=NULL )
  838. cmVOS_MultVVS(dp,n,ctx->ctx->iChArray[chIdx],(cmSample_t)gain);
  839. return kOkDspRC;
  840. }
  841. cmDspRC_t _cmDspAudioInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  842. {
  843. cmDspRC_t rc = kOkDspRC;
  844. switch( evt->dstVarId )
  845. {
  846. case kChAiId:
  847. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  848. {
  849. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  850. if( inst->execFunc==NULL && cmDspUInt(inst,kChAiId) < ctx->ctx->iChCnt )
  851. inst->execFunc = _cmDspAudioInExec;
  852. }
  853. break;
  854. case kGainAiId:
  855. cmDspSetEvent(ctx,inst,evt);
  856. break;
  857. }
  858. return rc;
  859. }
  860. struct cmDspClass_str* cmAudioInClassCons( cmDspCtx_t* ctx )
  861. {
  862. cmDspClassSetup(&_cmAudioInDC,ctx,"AudioIn",
  863. NULL,
  864. _cmDspAudioInAlloc,
  865. NULL,
  866. _cmDspAudioInReset,
  867. _cmDspAudioInExec,
  868. _cmDspAudioInRecv,
  869. NULL,
  870. NULL,
  871. "Audio output port");
  872. return &_cmAudioInDC;
  873. }
  874. //==========================================================================================================================================
  875. enum
  876. {
  877. kChAoId,
  878. kGainAoId,
  879. kInAoId
  880. };
  881. cmDspClass_t _cmAudioOutDC;
  882. typedef struct
  883. {
  884. cmDspInst_t inst;
  885. } cmDspAudioOut_t;
  886. cmDspInst_t* _cmDspAudioOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  887. {
  888. cmDspVarArg_t args[] =
  889. {
  890. { "ch", kChAoId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio output channel index"},
  891. { "gain",kGainAoId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain multiplier"},
  892. { "in", kInAoId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  893. { NULL, 0, 0, 0, 0 }
  894. };
  895. cmDspAudioOut_t* p = cmDspInstAlloc(cmDspAudioOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  896. cmDspSetDefaultUInt( ctx, &p->inst, kChAoId, 0, 0);
  897. cmDspSetDefaultDouble( ctx, &p->inst, kGainAoId, 0, 1.0);
  898. return &p->inst;
  899. }
  900. cmDspRC_t _cmDspAudioOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  901. {
  902. cmDspRC_t rc = kOkDspRC;
  903. cmDspApplyAllDefaults(ctx,inst);
  904. return rc;
  905. }
  906. cmDspRC_t _cmDspAudioOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  907. {
  908. cmDspRC_t rc = kOkDspRC;
  909. unsigned chIdx = cmDspUInt(inst,kChAoId);
  910. unsigned oChCnt = ctx->ctx->oChCnt;
  911. double gain = cmDspDouble(inst,kGainAoId);
  912. if( chIdx >= oChCnt )
  913. {
  914. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The output channel index %i is invalid. Channel count:%i.",chIdx,oChCnt);
  915. inst->execFunc = NULL; // disable callbacks - this prevents the error msg from repeating
  916. return rc;
  917. }
  918. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAoId,0);
  919. if( sp == NULL )
  920. {
  921. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  922. return kOkDspRC;
  923. }
  924. unsigned n = cmDspSamplesPerCycle(ctx);
  925. assert( n == cmDspVarRows(inst,kInAoId) );
  926. // if this channel is disabled or set to pass-through then chArray[chIdx] will be NULL
  927. if( ctx->ctx->oChArray[chIdx] != NULL )
  928. cmVOS_MultVVS(ctx->ctx->oChArray[chIdx],n,sp,(cmSample_t)gain);
  929. return kOkDspRC;
  930. }
  931. cmDspRC_t _cmDspAudioOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  932. {
  933. cmDspRC_t rc = kOkDspRC;
  934. switch( evt->dstVarId )
  935. {
  936. case kChAoId:
  937. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  938. {
  939. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  940. if( inst->execFunc==NULL && cmDspUInt(inst,kChAoId) < ctx->ctx->oChCnt )
  941. inst->execFunc = _cmDspAudioOutExec;
  942. }
  943. break;
  944. case kGainAoId:
  945. cmDspSetEvent(ctx,inst,evt);
  946. break;
  947. }
  948. return rc;
  949. }
  950. struct cmDspClass_str* cmAudioOutClassCons( cmDspCtx_t* ctx )
  951. {
  952. cmDspClassSetup(&_cmAudioOutDC,ctx,"AudioOut",
  953. NULL,
  954. _cmDspAudioOutAlloc,
  955. NULL,
  956. _cmDspAudioOutReset,
  957. _cmDspAudioOutExec,
  958. _cmDspAudioOutRecv,
  959. NULL,NULL,
  960. "Audio output port");
  961. return &_cmAudioOutDC;
  962. }
  963. //==========================================================================================================================================
  964. enum
  965. {
  966. kFnAofId,
  967. kChCntAofId,
  968. kGain0AofId,
  969. kGain1AofId,
  970. kIn0AofId,
  971. kIn1AofId,
  972. kSelAofId
  973. };
  974. cmDspClass_t _cmAudioFileOutDC;
  975. typedef struct
  976. {
  977. cmDspInst_t inst;
  978. cmSample_t* smpBuf;
  979. unsigned smpCnt;
  980. unsigned bits;
  981. cmAudioFileH_t afH;
  982. unsigned openSymId;
  983. unsigned closeSymId;
  984. } cmDspAudioFileOut_t;
  985. cmDspRC_t _cmDspAudioFileOutCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned chCnt )
  986. {
  987. cmDspRC_t rc = kOkDspRC;
  988. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  989. const cmChar_t* fn = cmDspStrcz(inst,kFnAofId);
  990. if(cmAudioFileIsValid(p->afH) )
  991. cmAudioFileDelete(&p->afH);
  992. if( cmAudioFileIsValid(p->afH = cmAudioFileNewCreate(fn, cmDspSampleRate(ctx), p->bits, chCnt, &rc, ctx->rpt )) == false )
  993. rc = cmDspClassErr(ctx,inst->classPtr,kVarArgParseFailDspRC,"The output audio file '%s' create failed.",fn);
  994. return rc;
  995. }
  996. cmDspInst_t* _cmDspAudioFileOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  997. {
  998. cmDspVarArg_t args[] =
  999. {
  1000. { "fn", kFnAofId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file name"},
  1001. { "chs", kChCntAofId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Channel count"},
  1002. { "gain0", kGain0AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 0 multiplier"},
  1003. { "gain1", kGain1AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 1 multiplier"},
  1004. { "in0", kIn0AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 0"},
  1005. { "in1", kIn1AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 1"},
  1006. { "sel", kSelAofId, 0, 0, kInDsvFl, "Open | Close"},
  1007. { NULL, 0, 0, 0, 0 }
  1008. };
  1009. cmDspAudioFileOut_t* p = cmDspInstAlloc(cmDspAudioFileOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1010. cmDspValue_t chCntVal;
  1011. cmDsvSetUInt(&chCntVal,cmMin(2,cmDspUInt(&p->inst,kChCntAofId)));
  1012. cmDspSetDefault(ctx, &p->inst, kChCntAofId, &chCntVal );
  1013. cmDspSetDefaultDouble( ctx, &p->inst, kGain0AofId, 0, 1.0);
  1014. cmDspSetDefaultDouble( ctx, &p->inst, kGain1AofId, 0, 1.0);
  1015. p->bits = 16;
  1016. p->afH = cmNullAudioFileH;
  1017. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  1018. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  1019. return &p->inst;
  1020. }
  1021. cmDspRC_t _cmDspAudioFileOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1022. {
  1023. cmDspRC_t rc = kOkDspRC;
  1024. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1025. unsigned chCnt = cmDspUInt(inst,kChCntAofId);
  1026. cmDspApplyAllDefaults(ctx,inst);
  1027. p->smpCnt = cmDspSamplesPerCycle(ctx) * chCnt;
  1028. p->smpBuf = cmLhResizeN(ctx->lhH, cmSample_t, p->smpBuf, p->smpCnt);
  1029. rc = _cmDspAudioFileOutCreateFile( ctx, inst, chCnt );
  1030. return rc;
  1031. }
  1032. cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1033. {
  1034. cmDspRC_t rc = kOkDspRC;
  1035. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;;
  1036. unsigned fpc = cmDspSamplesPerCycle(ctx);
  1037. unsigned chCnt = cmDspUInt(inst,kChCntAofId);
  1038. cmSample_t* chArray[chCnt];
  1039. unsigned i;
  1040. // Assume that both channls have the same number of samples.
  1041. // (cmAudioWriteFile() has the same constraint )
  1042. int sn = cmDspAudioBufSmpCount(ctx,inst,kIn0AofId,0);
  1043. // This code can handle the case where the input channels contain
  1044. // more than or less than cmDspSamplesPerCycle().
  1045. while(sn>0)
  1046. {
  1047. // we can process at most 'fpc' samples on one iteration
  1048. unsigned n = cmMin(sn,fpc);
  1049. sn -= n;
  1050. // apply output gain
  1051. for(i=0; i<chCnt; ++i)
  1052. {
  1053. chArray[i] = p->smpBuf + i*fpc;
  1054. const cmSample_t* sp = i==0 ? cmDspAudioBuf(ctx,inst,kIn0AofId,0) : cmDspAudioBuf(ctx,inst,kIn1AofId,0);
  1055. cmSample_t gain = i==0 ? cmDspDouble(inst,kGain0AofId) : cmDspDouble(inst,kGain1AofId);
  1056. cmVOS_MultVVS(chArray[i], n, sp, (cmSample_t)gain);
  1057. }
  1058. // write the samples
  1059. if( cmAudioFileWriteSample(p->afH, n, chCnt, chArray ) != kOkAfRC )
  1060. rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed.");
  1061. }
  1062. return rc;
  1063. }
  1064. cmDspRC_t _cmDspAudioFileOutRecv( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1065. {
  1066. cmDspRC_t rc = kOkDspRC;
  1067. switch( evt->dstVarId )
  1068. {
  1069. case kGain1AofId:
  1070. case kGain0AofId:
  1071. cmDspSetEvent(ctx,inst,evt);
  1072. break;
  1073. case kFnAofId:
  1074. cmDspSetEvent(ctx,inst,evt);
  1075. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1076. break;
  1077. case kSelAofId:
  1078. {
  1079. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1080. unsigned symId = cmDsvSymbol(evt->valuePtr);
  1081. if( symId == p->openSymId )
  1082. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1083. else
  1084. {
  1085. if( symId == p->closeSymId )
  1086. {
  1087. if(cmAudioFileIsValid(p->afH) )
  1088. cmAudioFileDelete(&p->afH);
  1089. }
  1090. else
  1091. {
  1092. rc = cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"Unknown selector symbol (%i) %s.",symId,cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  1093. }
  1094. }
  1095. }
  1096. }
  1097. return rc;
  1098. }
  1099. cmDspRC_t _cmDspAudioFileOutFree( cmDspCtx_t* ctx, struct cmDspInst_str* inst, const cmDspEvt_t* evtPtr )
  1100. {
  1101. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1102. if(cmAudioFileIsValid(p->afH) )
  1103. cmAudioFileDelete(&p->afH);
  1104. return kOkDspRC;
  1105. }
  1106. struct cmDspClass_str* cmAudioFileOutClassCons( cmDspCtx_t* ctx )
  1107. {
  1108. cmDspClassSetup(&_cmAudioFileOutDC,ctx,"AudioFileOut",
  1109. NULL,
  1110. _cmDspAudioFileOutAlloc,
  1111. _cmDspAudioFileOutFree,
  1112. _cmDspAudioFileOutReset,
  1113. _cmDspAudioFileOutExec,
  1114. _cmDspAudioFileOutRecv,
  1115. NULL,NULL,
  1116. "Audio file output port");
  1117. return &_cmAudioFileOutDC;
  1118. }
  1119. //==========================================================================================================================================
  1120. enum
  1121. {
  1122. kTypScId,
  1123. kMinScId,
  1124. kMaxScId,
  1125. kStpScId,
  1126. kValScId,
  1127. kLblScId,
  1128. kSendScId
  1129. };
  1130. cmDspClass_t _cmScalarDC;
  1131. typedef struct
  1132. {
  1133. cmDspInst_t inst;
  1134. } cmDspScalar_t;
  1135. cmDspInst_t* _cmDspScalarAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1136. {
  1137. cmDspVarArg_t args[] =
  1138. {
  1139. { "typ", kTypScId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Type" },
  1140. { "min", kMinScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1141. { "max", kMaxScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1142. { "step", kStpScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Step value (set to 0 to ignore)"},
  1143. { "val", kValScId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current value"},
  1144. { "lbl", kLblScId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1145. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Send value on any msg."},
  1146. { NULL, 0, 0, 0, 0 }
  1147. };
  1148. cmDspScalar_t* p = cmDspInstAlloc(cmDspScalar_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1149. cmDspSetDefaultUInt( ctx, &p->inst, kTypScId, 0, kNumberDuiId);
  1150. cmDspSetDefaultDouble(ctx, &p->inst, kMinScId, 0.0, 0);
  1151. cmDspSetDefaultDouble(ctx, &p->inst, kMaxScId, 0.0, 1);
  1152. cmDspSetDefaultDouble(ctx, &p->inst, kStpScId, 0.0, 0);
  1153. unsigned typeId = cmDspDefaultUInt(&p->inst,kTypScId);
  1154. // create the UI control
  1155. cmDspUiScalarCreate(ctx,&p->inst,typeId,kMinScId,kMaxScId,kStpScId,kValScId,kLblScId);
  1156. return &p->inst;
  1157. }
  1158. cmDspRC_t _cmDspScalarReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1159. {
  1160. cmDspApplyAllDefaults(ctx,inst);
  1161. return kOkDspRC;
  1162. }
  1163. cmDspRC_t _cmDspScalarRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1164. {
  1165. if( evt->dstVarId == kSendScId )
  1166. {
  1167. // NOT SURE IF THIS IS CORRECT OR NOT - IT SHOULD FORCE THE CURRENT VALUE TO BE
  1168. // SENT - THE CURRENT VALUE SHOULD ALREADY BE AT THE UI SO THERE DOESN'T SEEM
  1169. // TO BE ANY REASON TO UPDATE IT THERE.
  1170. cmDspSetDouble( ctx, inst, kValScId, cmDspDouble(inst,kValScId ));
  1171. return kOkDspRC;
  1172. }
  1173. switch( evt->dstVarId )
  1174. {
  1175. case kMinScId:
  1176. case kMaxScId:
  1177. case kStpScId:
  1178. case kValScId:
  1179. cmDspSetEvent(ctx,inst,evt);
  1180. break;
  1181. default:
  1182. {assert(0);}
  1183. }
  1184. return kOkDspRC;
  1185. }
  1186. cmDspRC_t _cmDspScalarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1187. {
  1188. return cmDspVarPresetRdWr(ctx,inst,kValScId,storeFl);
  1189. }
  1190. struct cmDspClass_str* cmScalarClassCons( cmDspCtx_t* ctx )
  1191. {
  1192. cmDspClassSetup(&_cmScalarDC,ctx,"Scalar",
  1193. NULL,
  1194. _cmDspScalarAlloc,
  1195. NULL,
  1196. _cmDspScalarReset,
  1197. NULL,
  1198. _cmDspScalarRecv,
  1199. _cmDspScalarPresetRdWr,
  1200. NULL,
  1201. "Scalar value control.");
  1202. return &_cmScalarDC;
  1203. }
  1204. //==========================================================================================================================================
  1205. enum
  1206. {
  1207. kValTxId,
  1208. kLblTxId
  1209. };
  1210. cmDspClass_t _cmTextDC;
  1211. typedef struct
  1212. {
  1213. cmDspInst_t inst;
  1214. } cmDspText_t;
  1215. cmDspInst_t* _cmDspTextAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1216. {
  1217. cmDspVarArg_t args[] =
  1218. {
  1219. { "val", kValTxId, 0, 0, kInDsvFl | kOutDsvFl | kStrzDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current string"},
  1220. { "lbl", kLblTxId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1221. { NULL, 0, 0, 0, 0 }
  1222. };
  1223. cmDspText_t* p = cmDspInstAlloc(cmDspText_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1224. // create the UI control
  1225. cmDspUiTextCreate(ctx,&p->inst,kValTxId,kLblTxId);
  1226. return &p->inst;
  1227. }
  1228. cmDspRC_t _cmDspTextReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1229. {
  1230. cmDspApplyAllDefaults(ctx,inst);
  1231. return kOkDspRC;
  1232. }
  1233. cmDspRC_t _cmDspTextRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1234. {
  1235. switch( evt->dstVarId )
  1236. {
  1237. case kValTxId:
  1238. cmDspSetEvent(ctx,inst,evt);
  1239. break;
  1240. default:
  1241. {assert(0);}
  1242. }
  1243. return kOkDspRC;
  1244. }
  1245. struct cmDspClass_str* cmTextClassCons( cmDspCtx_t* ctx )
  1246. {
  1247. cmDspClassSetup(&_cmTextDC,ctx,"Text",
  1248. NULL,
  1249. _cmDspTextAlloc,
  1250. NULL,
  1251. _cmDspTextReset,
  1252. NULL,
  1253. _cmDspTextRecv,
  1254. NULL,NULL,
  1255. "Text value control.");
  1256. return &_cmTextDC;
  1257. }
  1258. //==========================================================================================================================================
  1259. enum
  1260. {
  1261. kInMtId,
  1262. kMinMtId,
  1263. kMaxMtId,
  1264. kTimeMtId,
  1265. kLblMtId,
  1266. };
  1267. cmDspClass_t _cmMeterDC;
  1268. typedef struct
  1269. {
  1270. cmDspInst_t inst;
  1271. unsigned updSmpCnt;
  1272. unsigned lastCycleCnt;
  1273. double value;
  1274. unsigned cnt;
  1275. } cmDspMeter_t;
  1276. cmDspInst_t* _cmDspMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1277. {
  1278. cmDspVarArg_t args[] =
  1279. {
  1280. { "in", kInMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Current value"},
  1281. { "min", kMinMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1282. { "max", kMaxMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1283. { "time", kTimeMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "UI update time in milliseconds (default:50)"},
  1284. { "label",kLblMtId, 0, 0, kStrzDsvFl, "Label"},
  1285. { NULL, 0, 0, 0, 0 }
  1286. };
  1287. cmDspMeter_t* p = cmDspInstAlloc(cmDspMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1288. cmDspSetDefaultDouble(ctx, &p->inst, kInMtId, 0.0, 0);
  1289. cmDspSetDefaultDouble(ctx, &p->inst, kMinMtId, 0.0, 0);
  1290. cmDspSetDefaultDouble(ctx, &p->inst, kMaxMtId, 0.0, 1);
  1291. cmDspSetDefaultDouble(ctx, &p->inst, kTimeMtId, 0.0, 50.0);
  1292. // create the UI control
  1293. cmDspUiMeterCreate(ctx,&p->inst,kMinMtId,kMaxMtId,kInMtId,kLblMtId);
  1294. return &p->inst;
  1295. }
  1296. cmDspRC_t _cmDspMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1297. {
  1298. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1299. cmDspApplyAllDefaults(ctx,inst);
  1300. double updateMs = cmDspDouble(inst,kTimeMtId);
  1301. p->updSmpCnt = floor(cmDspSampleRate(ctx) * updateMs / 1000.0);
  1302. p->lastCycleCnt = 0;
  1303. p->cnt = 0;
  1304. p->value = 0;
  1305. return kOkDspRC;
  1306. }
  1307. cmDspRC_t _cmDspMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1308. {
  1309. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1310. double curVal = p->value;
  1311. bool deltaFl = p->cnt!=0 && curVal != cmDspDouble(inst,kInMtId);
  1312. bool expireFl = (ctx->cycleCnt - p->lastCycleCnt) * cmDspSamplesPerCycle(ctx) > p->updSmpCnt;
  1313. // if the meter value changed and the update time has expired
  1314. if( deltaFl && expireFl )
  1315. {
  1316. cmDspSetDouble(ctx,inst,kInMtId,curVal);
  1317. p->value = 0;
  1318. p->cnt = 0;
  1319. p->lastCycleCnt = ctx->cycleCnt;
  1320. }
  1321. return kOkDspRC;
  1322. }
  1323. cmDspRC_t _cmDspMeterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1324. {
  1325. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1326. switch( evt->dstVarId )
  1327. {
  1328. case kInMtId:
  1329. p->value = cmDsvGetDouble(evt->valuePtr);
  1330. ++p->cnt;
  1331. return kOkDspRC;
  1332. case kTimeMtId:
  1333. p->updSmpCnt = floor(cmDspSampleRate(ctx) * cmDsvGetDouble(evt->valuePtr) / 1000.0);
  1334. break;
  1335. }
  1336. return cmDspSetEvent(ctx,inst,evt);
  1337. }
  1338. struct cmDspClass_str* cmMeterClassCons( cmDspCtx_t* ctx )
  1339. {
  1340. cmDspClassSetup(&_cmMeterDC,ctx,"Meter",
  1341. NULL,
  1342. _cmDspMeterAlloc,
  1343. NULL,
  1344. _cmDspMeterReset,
  1345. _cmDspMeterExec,
  1346. _cmDspMeterRecv,
  1347. NULL,NULL,
  1348. "Meter display.");
  1349. return &_cmMeterDC;
  1350. }
  1351. //==========================================================================================================================================
  1352. enum
  1353. {
  1354. kInLbId,
  1355. kAlignLbId
  1356. };
  1357. cmDspClass_t _cmLabelDC;
  1358. typedef struct
  1359. {
  1360. cmDspInst_t inst;
  1361. } cmDspLabel_t;
  1362. cmDspInst_t* _cmDspLabelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1363. {
  1364. cmDspVarArg_t args[] =
  1365. {
  1366. { "in", kInLbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "LabelText" },
  1367. { "align",kAlignLbId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Alignment 0=right 1=left 2=center" },
  1368. { NULL, 0, 0, 0, 0 }
  1369. };
  1370. cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1371. cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId, 0.0, kLeftAlignDuiId);
  1372. // create the UI control
  1373. cmDspUiLabelCreate(ctx,&p->inst,kInLbId,kAlignLbId);
  1374. return &p->inst;
  1375. }
  1376. cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1377. {
  1378. cmDspApplyAllDefaults(ctx,inst);
  1379. return kOkDspRC;
  1380. }
  1381. cmDspRC_t _cmDspLabelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1382. {
  1383. return cmDspSetEvent(ctx,inst,evt);
  1384. }
  1385. struct cmDspClass_str* cmLabelClassCons( cmDspCtx_t* ctx )
  1386. {
  1387. cmDspClassSetup(&_cmLabelDC,ctx,"Label",
  1388. NULL,
  1389. _cmDspLabelAlloc,
  1390. NULL,
  1391. _cmDspLabelReset,
  1392. NULL,
  1393. _cmDspLabelRecv,
  1394. NULL,NULL,
  1395. "Label control.");
  1396. return &_cmLabelDC;
  1397. }
  1398. //==========================================================================================================================================
  1399. enum
  1400. {
  1401. kTypBtId,
  1402. kOutBtId,
  1403. kSymBtId,
  1404. kLblBtId,
  1405. kInBtId
  1406. };
  1407. cmDspClass_t _cmButtonDC;
  1408. typedef struct
  1409. {
  1410. cmDspInst_t inst;
  1411. unsigned resetSymId;
  1412. } cmDspButton_t;
  1413. cmDspInst_t* _cmDspButtonAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1414. {
  1415. va_list vl1;
  1416. va_copy(vl1,vl);
  1417. assert( va_cnt >= 1 );
  1418. unsigned typeId = va_arg(vl,unsigned);
  1419. // check buttons should transmit their default values - push buttons should not.
  1420. unsigned sendDfltFl = typeId == kCheckDuiId ? kSendDfltDsvFl : 0;
  1421. cmDspVarArg_t args[] =
  1422. {
  1423. { "typ", kTypBtId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Type" },
  1424. { "out", kOutBtId, 0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | sendDfltFl, "Value"},
  1425. { "sym", kSymBtId, 0, 0, kOutDsvFl | kSymDsvFl | kOptArgDsvFl | sendDfltFl, "Symbol Value"},
  1426. { "label",kLblBtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label"},
  1427. { "in", kInBtId, 0, 0, kInDsvFl | kTypeDsvMask, "Simulate UI"},
  1428. { NULL, 0, 0, 0, 0 }
  1429. };
  1430. cmDspButton_t* p = cmDspInstAlloc(cmDspButton_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl1);
  1431. cmDspSetDefaultDouble(ctx, &p->inst, kOutBtId, 0.0, typeId==kCheckDuiId ? 0.0 : 1.0);
  1432. cmDspSetDefaultSymbol(ctx, &p->inst, kSymBtId, instSymId );
  1433. cmDspSetDefaultStrcz( ctx, &p->inst, kLblBtId, NULL, cmSymTblLabel(ctx->stH,instSymId));
  1434. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
  1435. // create the UI control
  1436. cmDspUiButtonCreate(ctx,&p->inst,typeId,kOutBtId,kLblBtId);
  1437. return &p->inst;
  1438. }
  1439. cmDspRC_t _cmDspButtonReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1440. {
  1441. cmDspApplyAllDefaults(ctx,inst);
  1442. return kOkDspRC;
  1443. }
  1444. cmDspRC_t _cmDspButtonRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1445. {
  1446. // the 'in' port is the only input port
  1447. // but the UI sends button pushes use kOutBtId - should this be changed?
  1448. assert( evt->dstVarId == kInBtId || evt->dstVarId == kOutBtId );
  1449. // We accept all types at the 'in' port but are only interested
  1450. // in transmitting doubles from the 'out' port.
  1451. if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
  1452. {
  1453. // Redirect events which can be converted to type kDoubleDsvFl
  1454. // to the output port.
  1455. //
  1456. // Convert the event dest var id from the 'kInBtId' to 'kOutBtId'
  1457. // and update the UI with the incoming value
  1458. cmDspSetEventUiId(ctx,inst,evt,kOutBtId);
  1459. }
  1460. // no matter what kind of msg enters the 'in' port send a symbol out the 'sym' port
  1461. if( inst->symId != cmInvalidId )
  1462. cmDspSetSymbol( ctx, inst, kSymBtId, inst->symId );
  1463. return kOkDspRC;
  1464. }
  1465. cmDspRC_t _cmDspButtonPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1466. {
  1467. cmDspRC_t rc = kOkDspRC;
  1468. if( cmDspUInt(inst,kTypBtId) == kCheckDuiId )
  1469. rc = cmDspVarPresetRdWr(ctx,inst,kOutBtId,storeFl);
  1470. return rc;
  1471. }
  1472. cmDspRC_t _cmDspButtonSysRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
  1473. {
  1474. cmDspButton_t* p = (cmDspButton_t*)inst;
  1475. if( attrSymId == p->resetSymId )
  1476. {
  1477. cmDspSetSymbol( ctx, inst, kSymBtId, p->resetSymId );
  1478. cmDspSetDouble(ctx,inst,kOutBtId, cmDspDouble(inst,kOutBtId));
  1479. }
  1480. return kOkDspRC;
  1481. }
  1482. struct cmDspClass_str* cmButtonClassCons( cmDspCtx_t* ctx )
  1483. {
  1484. cmDspClassSetup(&_cmButtonDC,ctx,"Button",
  1485. NULL,
  1486. _cmDspButtonAlloc,
  1487. NULL,
  1488. _cmDspButtonReset,
  1489. NULL,
  1490. _cmDspButtonRecv,
  1491. _cmDspButtonPresetRdWr,
  1492. _cmDspButtonSysRecvFunc,
  1493. "Button control.");
  1494. return &_cmButtonDC;
  1495. }
  1496. //==========================================================================================================================================
  1497. cmDspClass_t _cmReorderDC;
  1498. typedef struct
  1499. {
  1500. cmDspInst_t inst;
  1501. unsigned portCnt; // count of input ports and count of output ports
  1502. unsigned* execFlArray; // execFlArray[portCnt] - true for ports which should cause obj to generate output
  1503. unsigned* orderArray; // orderArray[portCnt] - port output order map
  1504. } cmDspReorder_t;
  1505. cmDspInst_t* _cmDspReorderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1506. {
  1507. if( va_cnt < 1 )
  1508. {
  1509. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"Reorder objects must have arguments.");
  1510. return NULL;
  1511. }
  1512. // the first argument is the count of input ports (which is also the count of output ports)
  1513. int portCnt = va_arg(vl,int);
  1514. if( portCnt < 2 )
  1515. {
  1516. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"Reorder objects must have at least 2 ports.");
  1517. return NULL;
  1518. }
  1519. cmDspVarArg_t args[portCnt*2+1];
  1520. unsigned i;
  1521. int maxLabelCharCnt = 15;
  1522. cmChar_t label[ maxLabelCharCnt+1 ];
  1523. label[maxLabelCharCnt] = 0;
  1524. for(i=0; i<portCnt*2; ++i)
  1525. {
  1526. snprintf(label,maxLabelCharCnt,"%s-%i", (i<portCnt?"in":"out"), i%portCnt);
  1527. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  1528. args[i].label = cmSymTblLabel(ctx->stH,symId);
  1529. args[i].constId = i;
  1530. args[i].rn = 0;
  1531. args[i].cn = 0;
  1532. args[i].flags = (i<portCnt ? kInDsvFl : kOutDsvFl) | kTypeDsvMask;
  1533. args[i].doc = i<portCnt ? "Any input" : "Any output";
  1534. }
  1535. memset(args+i,0,sizeof(args[0]));
  1536. cmDspReorder_t* p = cmDspInstAlloc(cmDspReorder_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1537. if( p != NULL )
  1538. {
  1539. p->orderArray = cmLhAllocZ(ctx->lhH,unsigned,portCnt*2);
  1540. p->execFlArray = p->orderArray + portCnt;
  1541. p->portCnt = portCnt;
  1542. if( va_cnt-1 < portCnt )
  1543. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The reorder constructor must contain an output order map.");
  1544. else
  1545. {
  1546. for(i=0; i<portCnt; ++i)
  1547. {
  1548. int order = va_arg(vl,int);
  1549. if( order >= portCnt )
  1550. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port order index %i is outside the valid range of 0-%i.",order,portCnt-1);
  1551. else
  1552. p->orderArray[ order ] = i;
  1553. }
  1554. va_cnt -= portCnt+1;
  1555. for(i=0; i<va_cnt; ++i)
  1556. {
  1557. int execPortIdx = va_arg(vl,int);
  1558. if( execPortIdx >= portCnt )
  1559. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port exec index %i is outside the valid range of 0-%i.",execPortIdx,portCnt-1);
  1560. else
  1561. p->execFlArray[ execPortIdx ] = true;
  1562. }
  1563. }
  1564. }
  1565. return &p->inst;
  1566. }
  1567. cmDspRC_t _cmDspReorderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1568. {
  1569. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1570. cmLhFree(ctx->lhH,p->orderArray);
  1571. return kOkDspRC;
  1572. }
  1573. cmDspRC_t _cmDspReorderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1574. {
  1575. cmDspRC_t rc = kOkDspRC;
  1576. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1577. cmDspSetEvent(ctx,inst,evt);
  1578. if( evt->dstVarId < p->portCnt )
  1579. if( p->execFlArray[evt->dstVarId] )
  1580. {
  1581. unsigned i;
  1582. for(i=0; i<p->portCnt; ++i)
  1583. {
  1584. unsigned inVarId = p->orderArray[i];
  1585. unsigned outVarId = inVarId + p->portCnt;
  1586. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(inst, inVarId );
  1587. assert(varPtr != NULL);
  1588. if((rc = cmDspValueSet(ctx, inst, outVarId, &varPtr->value, 0 )) != kOkDspRC )
  1589. break;
  1590. }
  1591. }
  1592. return rc;
  1593. }
  1594. struct cmDspClass_str* cmReorderClassCons( cmDspCtx_t* ctx )
  1595. {
  1596. cmDspClassSetup(&_cmReorderDC,ctx,"Reorder",
  1597. NULL,
  1598. _cmDspReorderAlloc,
  1599. _cmDspReorderFree,
  1600. NULL,
  1601. NULL,
  1602. _cmDspReorderRecv,
  1603. NULL,NULL,
  1604. "Reorder value control.");
  1605. return &_cmReorderDC;
  1606. }
  1607. //==========================================================================================================================================
  1608. enum
  1609. {
  1610. kDirFnId,
  1611. kPatFnId,
  1612. kValFnId,
  1613. };
  1614. cmDspClass_t _cmFnameDC;
  1615. typedef struct
  1616. {
  1617. cmDspInst_t inst;
  1618. } cmDspFname_t;
  1619. // Pattern string for HTML and image files:
  1620. // "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
  1621. // The va_list must include 3 args:
  1622. // A pointer to a string referning to a default filename or directory or NULL.
  1623. // A pointer to a string referring to a pattern string or NULL.
  1624. // A bool to set the 'dirFl'.
  1625. cmDspInst_t* _cmDspFnameAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1626. {
  1627. cmDspVarArg_t args[] =
  1628. {
  1629. { "dir", kDirFnId, 0, 0, kInDsvFl | kReqArgDsvFl | kBoolDsvFl, "Dir=true Filename=false" },
  1630. { "pat", kPatFnId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "File pattern string (e.g. HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png}))" },
  1631. { "out", kValFnId, 0, 0, kOutDsvFl | kOptArgDsvFl | kStrzDsvFl, "Current file or directory name." },
  1632. { NULL, 0, 0, 0, 0 }
  1633. };
  1634. cmDspFname_t* p = cmDspInstAlloc(cmDspFname_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1635. cmDspSetDefaultBool( ctx, &p->inst, kDirFnId, false, false);
  1636. cmDspSetDefaultStrcz(ctx, &p->inst, kPatFnId, NULL, "All Files (*.*)");
  1637. cmDspSetDefaultStrcz(ctx, &p->inst, kValFnId, NULL, cmFsUserDir());
  1638. cmDspUiFnameCreate(ctx,&p->inst,kValFnId,kPatFnId,kDirFnId);
  1639. return &p->inst;
  1640. }
  1641. cmDspRC_t _cmDspFnameReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1642. {
  1643. cmDspApplyAllDefaults(ctx,inst);
  1644. return kOkDspRC;
  1645. }
  1646. cmDspRC_t _cmDspFnameRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1647. {
  1648. cmDspSetEvent(ctx,inst,evt);
  1649. switch( evt->dstVarId )
  1650. {
  1651. case kDirFnId:
  1652. case kPatFnId:
  1653. case kValFnId:
  1654. break;
  1655. default:
  1656. {assert(0);}
  1657. }
  1658. return kOkDspRC;
  1659. }
  1660. struct cmDspClass_str* cmFnameClassCons( cmDspCtx_t* ctx )
  1661. {
  1662. cmDspClassSetup(&_cmFnameDC,ctx,"Fname",
  1663. NULL,
  1664. _cmDspFnameAlloc,
  1665. NULL,
  1666. _cmDspFnameReset,
  1667. NULL,
  1668. _cmDspFnameRecv,
  1669. NULL,NULL,
  1670. "File or directory chooser control.");
  1671. return &_cmFnameDC;
  1672. }
  1673. //==========================================================================================================================================
  1674. cmDspClass_t _cmMsgListDC;
  1675. typedef struct
  1676. {
  1677. cmDspInst_t inst;
  1678. cmJsonH_t jsH; // current JSON tree
  1679. cmJsonNode_t* np; // ptr to JSON array
  1680. unsigned colCnt; // number of elements in the JSON sub-arrays (rows)
  1681. cmJsonH_t dfltJsH; // default JSON tree (the default JSON array node ptr (np) is stored in the var array but we must also maintain the assoc'd JSON handle othwerwise the pointer will not be valid)
  1682. unsigned* typeIdArray; // JSON types for each column size: typeIdArray[ colCnt ]
  1683. unsigned symColCnt; // same as number of elements of typeIdArray[] == kStringTId
  1684. unsigned symRowCnt; // same as cmJsonChildCount(p->np)
  1685. unsigned* symM; // symM[symRowCnt,symColCnt] symbol matrix
  1686. } cmDspMsgList_t;
  1687. // create a matrix to hold the symbol id's associated with any string values
  1688. cmDspRC_t _cmDspMsgListLoadSymbolMtx( cmDspCtx_t* ctx, cmDspMsgList_t* p )
  1689. {
  1690. assert(cmJsonIsArray(p->np));
  1691. unsigned i,j,k;
  1692. // remove any existing symbols
  1693. if( p->symM != NULL )
  1694. for(i=0; i<p->symColCnt*p->symRowCnt; ++i)
  1695. if( p->symM[i] != cmInvalidId )
  1696. cmSymTblRemove(ctx->stH,p->symM[i]);
  1697. // reallocate the symbol matrix
  1698. p->symRowCnt = cmJsonChildCount(p->np);
  1699. p->symM = cmLhResizeN(ctx->lhH,unsigned,p->symM,p->symRowCnt*p->symColCnt);
  1700. // for each row in the JSON array
  1701. for(i=0; i<p->symRowCnt; ++i)
  1702. {
  1703. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,i);
  1704. // for each column in row whose data type is a string
  1705. for(j=0,k=0; k<p->symColCnt; ++j)
  1706. if( p->typeIdArray[j] == kStringTId )
  1707. {
  1708. const cmJsonNode_t* vnp = cmJsonArrayElementC(cnp,j);
  1709. unsigned idx = k*p->symRowCnt + i;
  1710. const cmChar_t* text;
  1711. // register the string with the symbol table
  1712. if((text = vnp->u.stringVal) != NULL )
  1713. p->symM[ idx ] = cmSymTblRegisterStaticSymbol(ctx->stH,text);
  1714. else
  1715. p->symM[ idx ] = cmInvalidId;
  1716. ++k;
  1717. }
  1718. }
  1719. return kOkDspRC;
  1720. }
  1721. // Load a JSON file and set the supplied cmJsonH_t handle.
  1722. cmDspRC_t _cmDspMsgListLoadFile( cmDspCtx_t* ctx, cmErr_t* err, const cmChar_t* rsrcLabel, const cmChar_t* fn, cmJsonH_t* hp, unsigned* colCntPtr, cmJsonNode_t** npp )
  1723. {
  1724. cmDspRC_t rc = kOkDspRC; //
  1725. cmJsonNode_t* np = NULL; //
  1726. cmJsonH_t jsH = cmJsonNullHandle; //
  1727. *hp = cmJsonNullHandle;
  1728. *colCntPtr = 0;
  1729. *npp = NULL;
  1730. // if no file name was given ...
  1731. if( fn==NULL || strlen(fn)==0 )
  1732. {
  1733. jsH = ctx->rsrcJsH; // ... use the rsrc file
  1734. if( cmJsonIsValid(ctx->rsrcJsH) == false )
  1735. return cmErrMsg(err,kJsonFailDspRC,"No JSON cfg resource exists for this DSP program.");
  1736. fn = NULL;
  1737. }
  1738. else
  1739. {
  1740. if( cmJsonInitializeFromFile(&jsH,fn,ctx->cmCtx) != kOkJsRC )
  1741. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON file load failed on '%s'.",fn);
  1742. }
  1743. // find the array named by rsrcLabel
  1744. if((np = cmJsonFindValue(jsH,rsrcLabel,NULL,cmInvalidId)) == NULL)
  1745. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON tree does not have an array named '%s'.",rsrcLabel);
  1746. // be sure the msg list really is an array
  1747. if( cmJsonIsArray(np) == false )
  1748. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element named '%s' is not an array.", rsrcLabel);
  1749. if( fn == NULL )
  1750. fn = "<resource file>";
  1751. // count of elements in the array
  1752. unsigned n = cmJsonChildCount(np);
  1753. unsigned m = 0;
  1754. unsigned i,j;
  1755. // for each line in the array
  1756. for(i=0; i<n; ++i)
  1757. {
  1758. const cmJsonNode_t* cnp;
  1759. // get the ith sub-array (row)
  1760. if((cnp = cmJsonArrayElementC(np,i)) != NULL )
  1761. {
  1762. // verify that it is an array
  1763. if( cmJsonIsArray(cnp ) == false )
  1764. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element in '%s' at index %i is not an array.",fn,i);
  1765. // track the number of elements (columns) per row
  1766. unsigned q = cmJsonChildCount(cnp);
  1767. // if this is the first row then use it to set the valid column count
  1768. if( i==0 )
  1769. m = q;
  1770. else
  1771. {
  1772. if( m != q )
  1773. return cmErrMsg(err,kJsonFailDspRC,"The msg list sub-array at index %i has a different number of elements than the preceding sub-arrays in '%s'.",i,fn);
  1774. }
  1775. }
  1776. }
  1777. //
  1778. // determine and validate the column types
  1779. //
  1780. unsigned typeIdArray[m];
  1781. // for each row
  1782. for(i=0; i<n; ++i)
  1783. {
  1784. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
  1785. // for each column
  1786. for(j=0; j<m; ++j)
  1787. {
  1788. const cmJsonNode_t* sap = cmJsonArrayElementC(cnp,j);
  1789. unsigned typeId = sap->typeId & kMaskTId;
  1790. // the first row sets the expected type id for each column
  1791. switch(i)
  1792. {
  1793. case 0:
  1794. if( typeId != kStringTId )
  1795. return cmErrMsg(err,kJsonFailDspRC,"The first row of a msg list (%s) file must contain string elements which set the colum labels.",rsrcLabel);
  1796. break;
  1797. case 1:
  1798. typeIdArray[j] = typeId;
  1799. break;
  1800. default:
  1801. {
  1802. // if the type is a string then it can only
  1803. // match if the column is a string or null
  1804. // so we assume an error
  1805. if( typeId == kStringTId )
  1806. rc = kJsonFailDspRC;
  1807. switch(typeIdArray[j])
  1808. {
  1809. case kStringTId:
  1810. if( typeId != kStringTId && typeId != kNullTId)
  1811. return cmErrMsg(err,kJsonFailDspRC,"The msg list element at row index %i column index %i cannot be converted to a string in '%s'.",i,j,fn);
  1812. rc = kOkDspRC; // clear the assummed error
  1813. break;
  1814. case kNullTId: // null can be converted to anything
  1815. typeIdArray[j] = typeId;
  1816. break;
  1817. case kIntTId:
  1818. if( typeId == kRealTId ) // ints may be promoted to reals
  1819. typeIdArray[j] = kRealTId;
  1820. break;
  1821. case kRealTId:
  1822. break;
  1823. case kFalseTId:
  1824. case kTrueTId: // bools may be promoted to ints or reals
  1825. if( typeId == kIntTId || typeId == kRealTId )
  1826. typeIdArray[j] = typeId;
  1827. break;
  1828. default:
  1829. return cmErrMsg(err,kJsonFailDspRC,"The msg list element at row index %i column index %i is not a string,int,real,bool, or null type in '%s'.",i,j,fn);
  1830. }
  1831. if( rc != kOkDspRC )
  1832. return cmErrMsg(err,kJsonFailDspRC,"The string msg list element at row index %i column index %i cannot be converted to the column type in '%s'.",i,j,fn);
  1833. } // end dflt
  1834. } // end switch
  1835. } // end row
  1836. } // end list
  1837. // VERY TRICKY - store the column type id's in the label columns type id.
  1838. // This is stupid but safe because the column type id's are known to be set to kStringTId.
  1839. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  1840. for(i=0; i<m; ++i,lnp=lnp->siblingPtr)
  1841. lnp->typeId = typeIdArray[i];
  1842. *hp = fn==NULL ? cmJsonNullHandle : jsH;
  1843. *npp = np;
  1844. *colCntPtr = m;
  1845. return rc;
  1846. }
  1847. // use the JSON list labels to setup the cmDspVarArg_t records associated with each msg output var.
  1848. cmDspMsgList_t* _cmDspMsgListCons( cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, const cmDspVarArg_t* args, cmJsonH_t jsH, unsigned colCnt, cmJsonNode_t* np, unsigned typeIdArray[], unsigned va_cnt, va_list vl )
  1849. {
  1850. unsigned i,j;
  1851. unsigned fixedArgCnt = 0;
  1852. for(i=0; args[i].label != NULL; ++i)
  1853. ++fixedArgCnt;
  1854. cmDspVarArg_t a[ fixedArgCnt + colCnt + 1 ];
  1855. // copy the fixed arg's into the first fixedArgCnt ele's of a[]
  1856. for(j=0; j<fixedArgCnt; ++j)
  1857. a[j] = args[j];
  1858. // remove the label row from the JSON array
  1859. cmJsonNode_t* lnp = np->u.childPtr; // store the pointer to the label row
  1860. //np->u.childPtr = lnp->siblingPtr; // remove the labels from the array
  1861. // for each column
  1862. for(i=0; i<colCnt; ++i,++j)
  1863. {
  1864. cmJsonNode_t* lp = cmJsonArrayElement(lnp,i);
  1865. // store a pointer to the label
  1866. a[j].label = lp->u.stringVal;
  1867. a[j].constId = j;
  1868. a[j].rn = 0;
  1869. a[j].cn = 0;
  1870. a[j].flags = kOutDsvFl;
  1871. a[j].doc = "Msg output";
  1872. typeIdArray[i] = lp->typeId;
  1873. // convert the JSON type to a DSV type
  1874. switch( lp->typeId )
  1875. {
  1876. case kNullTId: a[j].flags |= kUIntDsvFl; break;
  1877. case kIntTId: a[j].flags |= kIntDsvFl; break;
  1878. case kRealTId: a[j].flags |= kDoubleDsvFl; break;
  1879. case kTrueTId: a[j].flags |= kBoolDsvFl; break;
  1880. case kFalseTId: a[j].flags |= kBoolDsvFl; break;
  1881. case kStringTId: a[j].flags |= kSymDsvFl; break; // strings are treated as symbols (UInt)
  1882. default:
  1883. { assert(0); }
  1884. }
  1885. // undo the tricky bit with the label types
  1886. lp->typeId = kStringTId;
  1887. }
  1888. // set the null sentinel at the end of the arg array
  1889. memset(a + j,0,sizeof(cmDspVarArg_t));
  1890. return (cmDspMsgList_t*)cmDspInstAlloc(cmDspMsgList_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1891. }
  1892. cmDspRC_t _cmDspMsgListReload( cmDspCtx_t* ctx, cmDspMsgList_t* p, const cmChar_t* rsrcLabel, const cmChar_t* fn )
  1893. {
  1894. cmDspRC_t rc = kOkDspRC;
  1895. cmJsonH_t jsH = cmJsonNullHandle;
  1896. unsigned colCnt = 0;
  1897. cmJsonNode_t* np = NULL;
  1898. // load the file
  1899. if((rc = _cmDspMsgListLoadFile(ctx,&p->inst.classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) != kOkDspRC )
  1900. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  1901. // verify that the col count is correct
  1902. if( colCnt != p->colCnt )
  1903. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The column count (%i) of msg list file '%s does not match the msg list column count %i.",colCnt,p->colCnt);
  1904. unsigned fixArgCnt = p->inst.varCnt - p->colCnt;
  1905. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  1906. unsigned i,j;
  1907. for(i=0,j=fixArgCnt; i<colCnt; ++i,++j, lnp=lnp->siblingPtr)
  1908. {
  1909. const cmChar_t* labelStr = cmSymTblLabel(ctx->stH,p->inst.varArray[j].symId);
  1910. // the labels of the new file must match the labels of the previous file
  1911. if( strcmp(lnp->u.stringVal,labelStr) )
  1912. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' label '%s' does not match the msg list label '%s' in column index %i.",fn,labelStr,lnp->u.stringVal,i);
  1913. // if the msg column is a string ....
  1914. if( (p->inst.varArray[j].flags & kTypeDsvMask) == kStrzDsvFl )
  1915. {
  1916. // ... then the file column must also be a string or null
  1917. if( lnp->typeId != kStringTId && np->typeId != kNullTId )
  1918. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i must be a string or null.",fn,i);
  1919. }
  1920. else // otherwie if the msg column is a number ...
  1921. {
  1922. // ... then the file type can't be a string
  1923. if( lnp->typeId == kStringTId )
  1924. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i cannot be a string.",fn,i);
  1925. // TODO: maybe there are other type conversions to check for here.
  1926. }
  1927. // reset the typeid of the labels
  1928. // (this is a cleanup from the tricky bit at the end of _cmDspListLoadFile())
  1929. lnp->typeId = kStringTId;
  1930. }
  1931. if( cmJsonIsValid( jsH ) )
  1932. {
  1933. if( cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,ctx->rsrcJsH) )
  1934. cmJsonFinalize(&p->jsH);
  1935. p->jsH = jsH;
  1936. p->np = np;
  1937. _cmDspMsgListLoadSymbolMtx(ctx,p);
  1938. }
  1939. return rc;
  1940. }
  1941. enum
  1942. {
  1943. kRsrcMlId,
  1944. kFnMlId,
  1945. kSelMlId,
  1946. kListMlId,
  1947. kCntMlId,
  1948. kOutBaseMlId // identify the first output port
  1949. };
  1950. cmDspInst_t* _cmDspMsgListAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1951. {
  1952. va_list vl2;
  1953. cmDspVarArg_t args[] =
  1954. {
  1955. { "rsrc", kRsrcMlId, 0, 0, kInDsvFl | kReqArgDsvFl | kStrzDsvFl, "Msg list resource label"},
  1956. { "fn", kFnMlId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "Msg list file name" },
  1957. { "sel", kSelMlId, 0, 0, kOutDsvFl | kInDsvFl | kOptArgDsvFl | kUIntDsvFl, "Current selection index" },
  1958. { "list", kListMlId, 0, 0, kInDsvFl | kJsonDsvFl, "Msg list data as a JSON array"},
  1959. { "cnt", kCntMlId, 0, 0, kOutDsvFl | kSendDfltDsvFl | kUIntDsvFl , "Count of elements."},
  1960. { NULL, 0, 0, 0, 0 }
  1961. };
  1962. va_copy(vl2,vl);
  1963. if( va_cnt < 1 )
  1964. {
  1965. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"The message list constructor must contain at least two arguments.");
  1966. goto errLabel;
  1967. }
  1968. const cmChar_t* rsrcLabel = va_arg(vl,const char*);
  1969. const cmChar_t* fn = va_cnt>1 ? va_arg(vl,const char*) : NULL;
  1970. unsigned colCnt = 0;
  1971. cmJsonH_t jsH = cmJsonNullHandle;
  1972. cmDspMsgList_t* p;
  1973. cmJsonNode_t* np;
  1974. cmDspRC_t rc;
  1975. // be sure the rsrc label contains a valid string
  1976. if( rsrcLabel==NULL || strlen(rsrcLabel)==0 )
  1977. {
  1978. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"No msg list resource label was given.");
  1979. goto errLabel;
  1980. }
  1981. // load and validate the JSON file
  1982. if((rc = _cmDspMsgListLoadFile(ctx,&classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) == kOkDspRC )
  1983. {
  1984. unsigned i;
  1985. unsigned typeIdArray[colCnt];
  1986. // allocate the instance
  1987. if((p = _cmDspMsgListCons(ctx,classPtr,storeSymId,instSymId,id,args,jsH,colCnt, np, typeIdArray, va_cnt, vl2 )) != NULL )
  1988. {
  1989. p->jsH = jsH;
  1990. p->np = np;
  1991. p->dfltJsH = jsH;
  1992. p->colCnt = colCnt;
  1993. p->typeIdArray = cmLhAllocZ( ctx->lhH, unsigned, colCnt );
  1994. memcpy(p->typeIdArray,typeIdArray,sizeof(p->typeIdArray[0])*colCnt);
  1995. p->symColCnt = 0;
  1996. for(i=0; i<p->colCnt; ++i)
  1997. if( p->typeIdArray[i] == kStringTId )
  1998. ++p->symColCnt;
  1999. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2000. cmDspSetDefaultStrcz( ctx,&p->inst, kRsrcMlId,NULL, rsrcLabel); // rsrc label
  2001. cmDspSetDefaultStrcz( ctx,&p->inst, kFnMlId, NULL, fn); // file name var
  2002. cmDspSetDefaultJson( ctx,&p->inst, kListMlId,NULL, np); // default tree
  2003. cmDspSetDefaultUInt( ctx,&p->inst, kSelMlId, 0, 0); // selection
  2004. cmDspSetDefaultUInt( ctx,&p->inst, kCntMlId, 0, p->symRowCnt);
  2005. // if there is only one column then signal the UI to create a menu button rather
  2006. // than a list by setting the height to zero.
  2007. unsigned height = p->colCnt == 1 ? 0 : 5;
  2008. // create the list UI element
  2009. cmDspUiMsgListCreate(ctx, &p->inst, height, kListMlId, kSelMlId );
  2010. return &p->inst;
  2011. }
  2012. }
  2013. errLabel:
  2014. return NULL;
  2015. }
  2016. cmDspRC_t _cmDspMsgListFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2017. {
  2018. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2019. // be careful not release the ctx->rsrcJsH handle if is happens to be the p->dflJsH
  2020. if( cmJsonIsValid(p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH))
  2021. if( cmJsonFinalize(&p->dfltJsH) != kOkJsRC )
  2022. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON default tree finalize failed.");
  2023. // be careful not to release the ctx->rsrcJsH handle if it happens to be the p->jsH
  2024. if( cmJsonIsValid(p->jsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH) )
  2025. if( cmJsonFinalize( &p->jsH ) != kOkJsRC )
  2026. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON finalization failed.");
  2027. cmLhFree(ctx->lhH,p->typeIdArray);
  2028. p->typeIdArray = NULL;
  2029. cmLhFree(ctx->lhH,p->symM);
  2030. p->symM = NULL;
  2031. return kOkDspRC;
  2032. }
  2033. cmDspRC_t _cmDspMsgListReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2034. {
  2035. cmDspApplyAllDefaults(ctx,inst);
  2036. return kOkDspRC;
  2037. }
  2038. cmDspRC_t _cmDspMsgOnSel( cmDspCtx_t* ctx, cmDspMsgList_t* p, unsigned rowIdx )
  2039. {
  2040. cmDspRC_t rc = kOkDspRC;
  2041. unsigned i,j;
  2042. unsigned symId;
  2043. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,rowIdx);
  2044. assert( cnp != NULL );
  2045. // for each output variable (column)
  2046. for(i=0,j=0; i<p->colCnt; ++i)
  2047. {
  2048. unsigned varId = kOutBaseMlId + i; // variable id
  2049. const cmJsonNode_t* np = cmJsonArrayElementC(cnp,i); // json row array
  2050. assert( np != NULL );
  2051. switch( p->typeIdArray[i] )
  2052. {
  2053. case kNullTId:
  2054. break;
  2055. case kIntTId:
  2056. {
  2057. int v;
  2058. if( cmJsonIntValue(np,&v) != kOkJsRC )
  2059. { assert(0); }
  2060. rc = cmDspSetInt(ctx,&p->inst,varId,v);
  2061. }
  2062. break;
  2063. case kRealTId:
  2064. {
  2065. double v;
  2066. if( cmJsonRealValue(np,&v) != kOkJsRC )
  2067. { assert(0); }
  2068. rc = cmDspSetDouble(ctx,&p->inst,varId,v);
  2069. }
  2070. break;
  2071. case kTrueTId:
  2072. case kFalseTId:
  2073. {
  2074. bool v;
  2075. if( cmJsonBoolValue(np,&v) != kOkJsRC )
  2076. { assert(0); }
  2077. rc = cmDspSetBool(ctx,&p->inst,varId,v);
  2078. }
  2079. break;
  2080. case kStringTId:
  2081. {
  2082. assert( j < p->symColCnt );
  2083. if((symId = p->symM[ (j*p->symRowCnt) + rowIdx ]) != cmInvalidId )
  2084. if((rc = cmDspSetSymbol(ctx,&p->inst,varId,symId )) != kOkDspRC )
  2085. break;
  2086. ++j;
  2087. }
  2088. break;
  2089. default:
  2090. { assert(0); }
  2091. } // end switch
  2092. } // end for
  2093. return rc;
  2094. }
  2095. cmDspRC_t _cmDspMsgListRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2096. {
  2097. cmDspRC_t rc = kOkDspRC;
  2098. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2099. switch( evt->dstVarId )
  2100. {
  2101. case kRsrcMlId:
  2102. {
  2103. const cmChar_t* fn = cmDspStrcz(inst,kFnMlId);
  2104. const cmChar_t* rsrcLabel = cmDsvStrz(evt->valuePtr);
  2105. if( rsrcLabel != NULL )
  2106. {
  2107. if((rc = _cmDspMsgListReload(ctx,p,rsrcLabel,fn)) != kOkDspRC )
  2108. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2109. cmDspSetEvent(ctx,inst,evt);
  2110. cmDspSetJson(ctx,inst,kListMlId,p->np);
  2111. cmDspSetUInt(ctx,inst,kSelMlId,0);
  2112. cmDspSetUInt(ctx,inst,kCntMlId,p->symRowCnt);
  2113. }
  2114. }
  2115. break;
  2116. case kFnMlId:
  2117. // Store the new file name.
  2118. // A new file will not be loaded until the next rsrc label is received.
  2119. cmDspSetEvent(ctx,inst,evt);
  2120. break;
  2121. case kListMlId:
  2122. break;
  2123. case kSelMlId:
  2124. {
  2125. unsigned rowIdx = cmDsvGetUInt(evt->valuePtr);
  2126. assert( rowIdx < p->symRowCnt);
  2127. // set the current selection variable
  2128. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2129. rc = _cmDspMsgOnSel(ctx,p,rowIdx);
  2130. } //end case
  2131. break;
  2132. }
  2133. return rc;
  2134. }
  2135. cmDspRC_t _cmDspMsgListPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  2136. {
  2137. cmDspRC_t rc = kOkDspRC;
  2138. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2139. if((rc = cmDspVarPresetRdWr(ctx,inst,kSelMlId,storeFl)) == kOkDspRC )
  2140. {
  2141. if( !storeFl )
  2142. rc = _cmDspMsgOnSel(ctx,p, cmDspUInt(inst,kSelMlId) );
  2143. }
  2144. return rc;
  2145. }
  2146. struct cmDspClass_str* cmMsgListClassCons( cmDspCtx_t* ctx )
  2147. {
  2148. cmDspClassSetup(&_cmMsgListDC,ctx,"MsgList",
  2149. NULL,
  2150. _cmDspMsgListAlloc,
  2151. _cmDspMsgListFree,
  2152. _cmDspMsgListReset,
  2153. NULL,
  2154. _cmDspMsgListRecv,
  2155. _cmDspMsgListPresetRdWr,
  2156. NULL,
  2157. "Message list selection control.");
  2158. return &_cmMsgListDC;
  2159. }
  2160. //==========================================================================================================================================
  2161. enum
  2162. {
  2163. kLenWtId,
  2164. kShapeWtId,
  2165. kFnWtId,
  2166. kLoopWtId,
  2167. kBegWtId,
  2168. kEndWtId,
  2169. kCmdWtId,
  2170. kOtWtId,
  2171. kGainWtId,
  2172. kPhsWtId,
  2173. kOutWtId,
  2174. kCntWtId,
  2175. kFIdxWtId,
  2176. kDoneWtId
  2177. };
  2178. enum
  2179. {
  2180. kSilenceWtId, // 0
  2181. kFileWtId, // 1
  2182. kWhiteWtId, // 2
  2183. kPinkWtId, // 3
  2184. kSineWtId, // 4
  2185. kCosWtId, // 5
  2186. kSqrWtId, // 6
  2187. kTriWtId, // 7
  2188. kSawWtId, // 8
  2189. kPulseWtId, // 9
  2190. kImpulseWtId, // 10
  2191. kPhasorWtId, // 11
  2192. kShapeWtCnt
  2193. };
  2194. cmDspClass_t _cmWaveTableDC;
  2195. typedef struct
  2196. {
  2197. cmDspInst_t inst;
  2198. cmSample_t* wt; // wave table memory
  2199. unsigned wti; // next location to write samples into the wavetable
  2200. unsigned wtn; // count of empty samples (avail for writing over) in the wavetable.
  2201. unsigned fi; // absolute index into the file of the next sample to read
  2202. unsigned fn; // length of the file in samples
  2203. unsigned cfi; // absolute index into the file of the beginning of the current audio vector
  2204. unsigned cfn; // when cfi >= cfn and doneFl is set then the 'done' msg is sent
  2205. unsigned loopCnt; // current loop count
  2206. bool doneFl; // the wave table source is exhausted
  2207. cmAudioFileH_t afH; // current audio file handle
  2208. int nxtBegSmpIdx; // the beg/end sample index to use with the next filename to arrive at port 'fn'
  2209. int nxtEndSmpIdx; //
  2210. cmThreadH_t thH;
  2211. bool loadFileFl;
  2212. cmDspCtx_t* ctx;
  2213. cmSample_t phsOffs;
  2214. cmSample_t phsLast;
  2215. unsigned onSymId;
  2216. unsigned offSymId;
  2217. unsigned doneSymId;
  2218. bool useThreadFl;
  2219. unsigned wt_oi;
  2220. } cmDspWaveTable_t;
  2221. bool _cmDspWaveTableThreadFunc( void* param);
  2222. cmDspInst_t* _cmDspWaveTableAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2223. {
  2224. cmDspVarArg_t args[] =
  2225. {
  2226. { "len", kLenWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave table length in samples" },
  2227. { "shape", kShapeWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=silent 1=file 2=sine 3=white" },
  2228. { "fn", kFnWtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Optional audio file name" },
  2229. { "loop", kLoopWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "-1=loop forever >0=loop count (dflt:-1)"},
  2230. { "beg", kBegWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File begin sample index" },
  2231. { "end", kEndWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File end sample index (-1=play all)" },
  2232. { "cmd", kCmdWtId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on off"},
  2233. { "ot", kOtWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count"},
  2234. { "gain", kGainWtId, 0, 0, kInDsvFl | kDoubleDsvFl|kOptArgDsvFl, "Gain"},
  2235. { "phs", kPhsWtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Driving phase" },
  2236. { "out", kOutWtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  2237. { "cnt", kCntWtId, 0, 0, kOutDsvFl | kIntDsvFl, "Loop count event."},
  2238. { "fidx", kFIdxWtId, 0, 0, kOutDsvFl | kUIntDsvFl, "Current audio file index."},
  2239. { "done", kDoneWtId, 0, 0, kOutDsvFl | kSymDsvFl, "'done' sent after last loop."},
  2240. { NULL, 0, 0, 0, 0 }
  2241. };
  2242. cmDspWaveTable_t* p = cmDspInstAlloc(cmDspWaveTable_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2243. // get the filename given in the va_list (or NULL if no filename was given)
  2244. const cmChar_t* fn = cmDspDefaultStrcz(&p->inst,kFnWtId);
  2245. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2246. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2247. p->doneSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2248. cmDspSetDefaultUInt( ctx, &p->inst, kLenWtId, 0, cmDspSampleRate(ctx));
  2249. cmDspSetDefaultUInt( ctx, &p->inst, kShapeWtId, 0, kSilenceWtId );
  2250. cmDspSetDefaultStrcz( ctx, &p->inst, kFnWtId, NULL, fn );
  2251. cmDspSetDefaultInt( ctx, &p->inst, kLoopWtId, 0, -1 );
  2252. cmDspSetDefaultInt( ctx, &p->inst, kBegWtId, 0, 0 );
  2253. cmDspSetDefaultInt( ctx, &p->inst, kEndWtId, 0, -1 );
  2254. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdWtId, p->onSymId );
  2255. cmDspSetDefaultUInt( ctx, &p->inst, kOtWtId, 0, 5 );
  2256. cmDspSetDefaultDouble(ctx, &p->inst, kGainWtId, 0, 1.0 );
  2257. cmDspSetDefaultUInt( ctx, &p->inst, kFIdxWtId, 0, 0 );
  2258. p->useThreadFl = false;
  2259. return &p->inst;
  2260. }
  2261. cmDspRC_t _cmDspWaveTableFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2262. {
  2263. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2264. if( cmThreadIsValid(p->thH) )
  2265. if( cmThreadDestroy(&p->thH) != kOkThRC )
  2266. cmDspInstErr(ctx,inst,kThreadFailDspRC,"The wavetable file reader thread failed to close.");
  2267. if( cmAudioFileIsValid(p->afH) )
  2268. cmAudioFileDelete(&p->afH);
  2269. cmLhFree(ctx->lhH,p->wt);
  2270. return kOkDspRC;
  2271. }
  2272. // Read the next block of 'rdSmpCnt' samples starting at the absolute file index 'p->fi'
  2273. // into wt[rdSmpCnt].
  2274. // If the end of the file segment marked by absolute file indexes 'begSmpIdx' to 'endSmpIdx'
  2275. // is encountered in the middle of the requested block and the wave table is in loop
  2276. // mode then the the function will automatically begin reading from the begining of the
  2277. // file segment. If the end of the file segment is encountered and the wave table is not
  2278. // in loop mode then the empty portion of wt[] will be set to zero.
  2279. cmDspRC_t _cmDspWaveTableReadBlock( cmDspCtx_t* ctx, cmDspWaveTable_t* p, cmSample_t* wt, unsigned rdSmpCnt, int begSmpIdx, int endSmpIdx, int maxLoopCnt )
  2280. {
  2281. unsigned actFrmCnt = 0;
  2282. unsigned chIdx = 0;
  2283. unsigned chCnt = 1;
  2284. unsigned fn = endSmpIdx - p->fi + 1; // count of samples between p->fi and endSmpIdx
  2285. unsigned n0 = rdSmpCnt;
  2286. unsigned n1 = 0;
  2287. // if the requested sample count will go past the end of the file segment
  2288. if( rdSmpCnt > fn )
  2289. {
  2290. n1 = rdSmpCnt - fn;
  2291. n0 = rdSmpCnt - n1;
  2292. }
  2293. // if we don't have a valid file yet - then
  2294. if( cmAudioFileIsValid(p->afH) == false )
  2295. {
  2296. cmVOS_Zero(wt,n0);
  2297. return kOkDspRC;
  2298. }
  2299. // read the first block of samples
  2300. if( cmAudioFileReadSample(p->afH, n0, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2301. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2302. // BUG BUG BUG
  2303. // This assertion will fail if the file beg/end markers are not legal given the file length.
  2304. // An error msg would be more appropriate.
  2305. assert( actFrmCnt == n0 );
  2306. // increment the wave table pointer
  2307. wt += n0;
  2308. p->fi += n0;
  2309. // if n1 != 0 then we have encountered the end of the file segment
  2310. if( n1 > 0 )
  2311. {
  2312. ++p->loopCnt;
  2313. // send the loop count event
  2314. cmDspSetInt(ctx,&p->inst,kCntWtId,p->loopCnt);
  2315. // if we have played all the requested loops
  2316. if( maxLoopCnt != -1 && p->loopCnt >= maxLoopCnt )
  2317. {
  2318. p->doneFl = true;
  2319. cmVOS_Zero(wt,n1); // zero to the end of the buffer
  2320. p->cfn = p->cfi + cmDspUInt((cmDspInst_t*)p,kLenWtId) - p->wtn - n0;
  2321. assert( p->cfn >= p->cfi );
  2322. }
  2323. else
  2324. {
  2325. // seek to the first sample indicated by the 'beg' variable
  2326. if( cmAudioFileSeek(p->afH,begSmpIdx) != kOkAfRC )
  2327. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the wave table file.",fn);
  2328. // read the second block of samples
  2329. if( cmAudioFileReadSample(p->afH, n1, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2330. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2331. assert( actFrmCnt == n1 );
  2332. // reset the file index tracker
  2333. p->fi = begSmpIdx + n1;
  2334. p->cfi = begSmpIdx;
  2335. }
  2336. }
  2337. return kOkDspRC;
  2338. }
  2339. cmDspRC_t _cmDspWaveTableReadAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p, unsigned wtSmpCnt, unsigned rdSmpCnt )
  2340. {
  2341. unsigned n0 = rdSmpCnt;
  2342. unsigned n1 = 0;
  2343. int begSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2344. int endSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2345. int maxLoopCnt= cmDspInt(&p->inst,kLoopWtId);
  2346. if( endSmpIdx < begSmpIdx )
  2347. endSmpIdx = p->fn-1;
  2348. // if this read will wrap to the beginning of the wave table
  2349. if( p->wti + rdSmpCnt > wtSmpCnt )
  2350. {
  2351. n0 = wtSmpCnt - p->wti; // count of samples to read into the end of p->wt[]
  2352. n1 = rdSmpCnt - n0; // count of samples to read into the beg of p->wt[]
  2353. }
  2354. assert(n1<wtSmpCnt);
  2355. // the first read always starts at p->wt + p->wti
  2356. if( p->doneFl )
  2357. cmVOS_Zero(p->wt + p->wti,n0);
  2358. else
  2359. if( _cmDspWaveTableReadBlock(ctx, p, p->wt+p->wti, n0,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2360. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2361. p->wtn -= n0; // decrease the count of available samples
  2362. p->wti += n0;
  2363. if( n1 > 0 )
  2364. {
  2365. // the second read always starts at the beginning of the wave table
  2366. if( p->doneFl )
  2367. cmVOS_Zero(p->wt,n1);
  2368. else
  2369. if( _cmDspWaveTableReadBlock(ctx, p, p->wt, n1,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2370. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2371. p->wtn -= n1; // decrease the count of available samples
  2372. p->wti = n1;
  2373. }
  2374. //p->wtn -= rdSmpCnt; // decrease the count of available samples
  2375. return kOkDspRC;
  2376. }
  2377. cmDspRC_t _cmDspWaveTableInitAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2378. {
  2379. cmDspRC_t rc;
  2380. cmAudioFileH_t afH;
  2381. cmRC_t afRC;
  2382. cmAudioFileInfo_t afInfo;
  2383. const cmChar_t* fn = cmDspStrcz(&p->inst,kFnWtId);
  2384. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2385. int begSmpIdx= cmDspInt(&p->inst,kBegWtId);
  2386. // if the file name is valid
  2387. if( fn == NULL || strlen(fn)==0 )
  2388. {
  2389. cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Audio file loading was requested for the wave table but no file name was given.");
  2390. goto errLabel;
  2391. }
  2392. // open the audio file
  2393. afH = cmAudioFileNewOpen(fn,&afInfo,&afRC,ctx->rpt);
  2394. // check for file open errors
  2395. if( afRC != kOkAfRC )
  2396. {
  2397. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' could not be opened. ('%s').",fn,cmAudioFileErrorMsg(afRC));
  2398. goto errLabel;
  2399. }
  2400. // if the file opened but is invalid
  2401. if( cmAudioFileIsValid(p->afH) )
  2402. cmAudioFileDelete(&p->afH);
  2403. // seek to the first sample indicated by the 'beg' variable
  2404. if( cmAudioFileSeek(afH,begSmpIdx) != kOkAfRC )
  2405. {
  2406. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the audio file '%s'.",fn);
  2407. goto errLabel;
  2408. }
  2409. p->afH = afH;
  2410. p->fi = begSmpIdx;
  2411. p->cfi = begSmpIdx;
  2412. p->fn = afInfo.frameCnt;
  2413. p->wti = 0;
  2414. p->wtn = wtSmpCnt;
  2415. // read the first block of samples
  2416. if((rc= _cmDspWaveTableReadAudioFile(ctx,p,wtSmpCnt,wtSmpCnt))!= kOkDspRC )
  2417. goto errLabel;
  2418. //printf("Wt:%s %i %i\n",fn,begSmpIdx,cmDspInt(&p->inst,kEndWtId));
  2419. // set the shape param to kFileWtId
  2420. //if((rc= cmDspSetUInt(ctx,&p->inst,kShapeWtId,kFileWtId)) != kOkDspRC )
  2421. // goto errLabel;
  2422. errLabel:
  2423. if( rc != kOkDspRC )
  2424. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2425. return rc;
  2426. }
  2427. bool _cmDspWaveTableThreadFunc( void* param)
  2428. {
  2429. cmDspWaveTable_t* p = (cmDspWaveTable_t*)param;
  2430. if( p->loadFileFl )
  2431. {
  2432. p->loadFileFl = false;
  2433. if( _cmDspWaveTableInitAudioFile(p->ctx,p) == kOkDspRC )
  2434. {
  2435. p->phsOffs = p->phsLast;
  2436. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2437. }
  2438. cmThreadPause(p->thH,kPauseThFl);
  2439. }
  2440. return true;
  2441. }
  2442. // Files are loaded via a background thread.
  2443. cmDspRC_t _cmDspWaveTableStartFileLoadThread( cmDspCtx_t* ctx, cmDspWaveTable_t* p, const cmChar_t* fn )
  2444. {
  2445. cmDspRC_t rc = kOkDspRC;
  2446. if( fn == NULL )
  2447. return rc;
  2448. if( p->loadFileFl )
  2449. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because another file is in the process of being loaded.",cmStringNullGuard(fn));
  2450. if(p->useThreadFl && cmThreadIsValid(p->thH) == false)
  2451. cmThreadCreate(&p->thH,_cmDspWaveTableThreadFunc,p,ctx->rpt);
  2452. if(p->useThreadFl && cmThreadIsValid(p->thH) == false )
  2453. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because the audio load thread is invalid.",cmStringNullGuard(fn));
  2454. p->loadFileFl = true;
  2455. p->ctx = ctx;
  2456. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2457. cmDspSetStrcz(ctx,&p->inst,kFnWtId,fn);
  2458. if( p->useThreadFl == false )
  2459. {
  2460. // use non-threaded load
  2461. if((rc = _cmDspWaveTableInitAudioFile(p->ctx,p)) != kOkDspRC )
  2462. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' was not loaded.",cmStringNullGuard(fn));
  2463. p->phsOffs = p->phsLast;
  2464. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2465. p->loadFileFl = false;
  2466. }
  2467. else
  2468. {
  2469. // use threaded load - this works but it isn't really thread safe
  2470. if( cmThreadPause(p->thH,0) != kOkThRC )
  2471. return cmDspInstErr(ctx,&p->inst,kThreadFailDspRC,"The audio file '%s' was not loaded because audio thread enable failed.",cmStringNullGuard(fn));
  2472. }
  2473. return rc;
  2474. }
  2475. // This function is called whenever the source mode variable changes (or a new file name arrives)
  2476. cmDspRC_t _cmDspWaveTableCreateTable( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2477. {
  2478. cmDspRC_t rc = kOkDspRC;
  2479. unsigned shapeId = cmDspUInt(&p->inst,kShapeWtId);
  2480. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2481. unsigned otCnt = cmDspUInt(&p->inst,kOtWtId);
  2482. cmSample_t gain = 0.9;
  2483. double hz = 1.0;
  2484. double sr = cmDspSampleRate(ctx);
  2485. assert( wtSmpCnt > 0 );
  2486. if( p->wt == NULL )
  2487. p->wt = cmLhResizeNZ(ctx->lhH,cmSample_t,p->wt,wtSmpCnt);
  2488. else
  2489. cmVOS_Zero(p->wt,wtSmpCnt);
  2490. p->wtn = wtSmpCnt; // all samples in the wt are avail for filling
  2491. p->wti = 0; // beginning with the first sample
  2492. p->loopCnt = 0; // we are starting from a new source so set the loop cnt to 0
  2493. p->doneFl = false; // and the doneFl to false
  2494. assert( p->wt != NULL );
  2495. switch( shapeId )
  2496. {
  2497. case kSilenceWtId:
  2498. break;
  2499. case kFileWtId:
  2500. printf("Loading:%i %i %s\n",p->nxtBegSmpIdx,p->nxtEndSmpIdx,cmDspStrcz(&p->inst,kFnWtId));
  2501. rc = _cmDspWaveTableStartFileLoadThread(ctx,p,cmDspStrcz(&p->inst,kFnWtId));
  2502. break;
  2503. case kWhiteWtId:
  2504. cmVOS_Random(p->wt,wtSmpCnt,-gain,gain);
  2505. break;
  2506. case kPinkWtId:
  2507. cmVOS_SynthPinkNoise(p->wt,wtSmpCnt,0.0);
  2508. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2509. break;
  2510. case kSineWtId:
  2511. cmVOS_SynthSine(p->wt,wtSmpCnt,0,sr,hz);
  2512. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2513. break;
  2514. case kCosWtId:
  2515. cmVOS_SynthCosine(p->wt,wtSmpCnt,0,sr,hz);
  2516. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2517. break;
  2518. case kSawWtId:
  2519. cmVOS_SynthSawtooth(p->wt,wtSmpCnt,0,sr,hz,otCnt);
  2520. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2521. break;
  2522. case kSqrWtId:
  2523. cmVOS_SynthSquare( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2524. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2525. break;
  2526. case kTriWtId:
  2527. cmVOS_SynthTriangle( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2528. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2529. break;
  2530. case kPulseWtId:
  2531. cmVOS_SynthPulseCos( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2532. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2533. break;
  2534. case kPhasorWtId:
  2535. cmVOS_SynthPhasor( p->wt,wtSmpCnt,0,sr,hz );
  2536. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2537. break;
  2538. }
  2539. return rc;
  2540. }
  2541. cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2542. {
  2543. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2544. cmDspApplyAllDefaults(ctx,inst);
  2545. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2546. p->nxtBegSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2547. p->nxtEndSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2548. return _cmDspWaveTableCreateTable(ctx,p);
  2549. }
  2550. cmDspRC_t _cmDspWaveTableExec1(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2551. {
  2552. cmDspRC_t rc = kOkDspRC;
  2553. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2554. unsigned mode = cmDspSymbol(inst,kCmdWtId);
  2555. unsigned srcId = cmDspUInt(inst,kShapeWtId);
  2556. if( mode == p->offSymId || srcId == kSilenceWtId )
  2557. {
  2558. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2559. return kOkDspRC;
  2560. }
  2561. cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
  2562. unsigned outCnt = cmDspVarRows(inst,kOutWtId);
  2563. unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
  2564. double gain = cmDspDouble(inst,kGainWtId);
  2565. unsigned i;
  2566. // for each output sample
  2567. for(i=0; i<outCnt; ++i)
  2568. {
  2569. p->wt_oi = (p->wt_oi + 1) % wtSmpCnt;
  2570. outV[i] = gain * p->wt[p->wt_oi];
  2571. }
  2572. // if we are reading from a file ...
  2573. if( srcId == kFileWtId )
  2574. {
  2575. unsigned rdSmpCnt = 8192; // file read block sample count
  2576. p->wtn += outCnt;
  2577. // ... and there are rdSmpCnt avail locations in the wave table
  2578. if( p->wtn >= rdSmpCnt )
  2579. rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
  2580. // send the current audio file index
  2581. if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
  2582. cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
  2583. else
  2584. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
  2585. p->cfi += outCnt;
  2586. }
  2587. return rc;
  2588. }
  2589. cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2590. {
  2591. cmDspRC_t rc = kOkDspRC;
  2592. const cmSample_t* phsV = cmDspAudioBuf(ctx,inst,kPhsWtId,0);
  2593. if( phsV == NULL )
  2594. {
  2595. inst->execFunc = NULL; // disable this instance because it has no input
  2596. return kOkDspRC;
  2597. }
  2598. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2599. unsigned mode = cmDspSymbol(inst,kCmdWtId);
  2600. unsigned srcId = cmDspUInt(inst,kShapeWtId);
  2601. if( mode == p->offSymId || srcId == kSilenceWtId )
  2602. {
  2603. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2604. return kOkDspRC;
  2605. }
  2606. cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
  2607. unsigned outCnt = cmDspVarRows(inst,kOutWtId);
  2608. unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
  2609. double gain = cmDspDouble(inst,kGainWtId);
  2610. unsigned i;
  2611. // for each output sample
  2612. for(i=0; i<outCnt; ++i)
  2613. {
  2614. // get the wave table location
  2615. //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
  2616. unsigned x = fmodf(phsV[i],wtSmpCnt);
  2617. // if the wt loctn is passed the end of the table
  2618. /*
  2619. if( x >= wtSmpCnt )
  2620. {
  2621. offs += wtSmpCnt;
  2622. x -= wtSmpCnt;
  2623. }
  2624. */
  2625. outV[i] = gain * p->wt[x];
  2626. }
  2627. p->phsLast = phsV[outCnt-1];
  2628. // if we are reading from a file ...
  2629. if( srcId == kFileWtId )
  2630. {
  2631. unsigned rdSmpCnt = 8192; // file read block sample count
  2632. p->wtn += outCnt;
  2633. // ... and there are rdSmpCnt avail locations in the wave table
  2634. if( p->wtn >= rdSmpCnt )
  2635. rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
  2636. // send the current audio file index
  2637. if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
  2638. cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
  2639. else
  2640. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
  2641. p->cfi += outCnt;
  2642. }
  2643. return rc;
  2644. }
  2645. cmDspRC_t _cmDspWaveTableRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2646. {
  2647. cmDspRC_t rc = kOkDspRC;
  2648. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2649. switch( evt->dstVarId )
  2650. {
  2651. case kFnWtId: // a new file name arrived - this automatically switches the source mode to kFileWtId
  2652. {
  2653. const cmChar_t* fn = cmDsvStrz(evt->valuePtr);
  2654. if( cmFsIsFile( fn )==false )
  2655. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' does not exist.",cmStringNullGuard(fn));
  2656. else
  2657. {
  2658. cmDspSetEvent(ctx,inst,evt); // set the file name variable
  2659. cmDspSetInt(ctx,inst,kBegWtId,p->nxtBegSmpIdx); // set the beg/end smp idx var's from the stored nxtBeg/EndSmpIdx values
  2660. cmDspSetInt(ctx,inst,kEndWtId,p->nxtEndSmpIdx); //
  2661. cmDspSetUInt(ctx,inst,kShapeWtId,kFileWtId); // switch to file mode
  2662. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2663. }
  2664. }
  2665. break;
  2666. case kBegWtId:
  2667. // store for next incoming file name msg
  2668. p->nxtBegSmpIdx = cmDsvGetInt(evt->valuePtr);
  2669. break;
  2670. case kEndWtId:
  2671. // store for next incoming file name msg
  2672. p->nxtEndSmpIdx = cmDsvGetInt(evt->valuePtr);
  2673. break;
  2674. case kShapeWtId:
  2675. if( cmDsvGetUInt(evt->valuePtr) < kShapeWtCnt )
  2676. {
  2677. cmDspSetEvent(ctx,inst,evt); // switch modes
  2678. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2679. }
  2680. break;
  2681. case kLenWtId: // we don't support table size changes
  2682. break;
  2683. case kPhsWtId:
  2684. break;
  2685. case kCmdWtId:
  2686. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2687. {
  2688. if( cmDspSymbol(inst,kCmdWtId) == p->onSymId )
  2689. {
  2690. //rc = _cmDspWaveTableReset(ctx,inst, evt );
  2691. rc = _cmDspWaveTableCreateTable(ctx,p);
  2692. cmDspSetSymbol(ctx,inst,kCmdWtId,p->onSymId);
  2693. p->phsOffs = 0;
  2694. p->phsLast = 0;
  2695. }
  2696. }
  2697. break;
  2698. case kOtWtId:
  2699. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2700. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2701. break;
  2702. case kGainWtId:
  2703. rc = cmDspSetEvent(ctx,inst,evt);
  2704. break;
  2705. default:
  2706. { assert(0); }
  2707. }
  2708. return rc;
  2709. }
  2710. struct cmDspClass_str* cmWaveTableClassCons( cmDspCtx_t* ctx )
  2711. {
  2712. cmDspClassSetup(&_cmWaveTableDC,ctx,"WaveTable",
  2713. NULL,
  2714. _cmDspWaveTableAlloc,
  2715. _cmDspWaveTableFree,
  2716. _cmDspWaveTableReset,
  2717. _cmDspWaveTableExec,
  2718. _cmDspWaveTableRecv,
  2719. NULL,
  2720. NULL,
  2721. "Variable frequency and waveshape signal generator." );
  2722. return &_cmWaveTableDC;
  2723. }
  2724. //==========================================================================================================================================
  2725. enum
  2726. {
  2727. kFmtSpId,
  2728. kOutSpId,
  2729. kInSpId,
  2730. kSprintfLabelCharCnt = 15,
  2731. kSprintfDocCharCnt = 31,
  2732. kSprintfBufCharCnt = 1023
  2733. };
  2734. cmDspClass_t _cmSprintfDC;
  2735. typedef struct
  2736. {
  2737. unsigned flags; // dsv type id for this fmt conversion or 0 if it is a literal string
  2738. unsigned fsi; // index into the fmt string where the fmt starts (always points to an '%' char)
  2739. unsigned fsn; // length of the format string
  2740. cmChar_t label[ kSprintfLabelCharCnt+1 ];
  2741. cmChar_t doc[ kSprintfDocCharCnt+1 ];
  2742. } cmDspSprintfFmt_t;
  2743. typedef struct
  2744. {
  2745. cmDspInst_t inst;
  2746. cmChar_t buf[ kSprintfBufCharCnt+1]; // output string buffer
  2747. unsigned inCnt; // count conversion spec's in the fmtStr[]
  2748. cmDspSprintfFmt_t* fmtArray; // fmtArray[inCnt]
  2749. cmChar_t* fmtStr; // C-style printf format string.
  2750. } cmDspSprintf_t;
  2751. cmDspRC_t _cmSprintfGetInputCnt( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, unsigned* inCntPtr )
  2752. {
  2753. unsigned i,n;
  2754. unsigned inCnt = 0;
  2755. if( fmt== NULL || (n=strlen(fmt))==0 )
  2756. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2757. for(i=0; i<n; ++i)
  2758. {
  2759. // handle the escape character
  2760. if( fmt[i] == '\\' )
  2761. ++i; // for now we will just skip the next character
  2762. else
  2763. if( fmt[i] == '%' )
  2764. ++inCnt;
  2765. }
  2766. *inCntPtr = inCnt;
  2767. return kOkDspRC;
  2768. }
  2769. cmDspRC_t _cmSprintfGetInputTypes( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, cmDspSprintfFmt_t fmtArray[], unsigned inCnt )
  2770. {
  2771. unsigned i,j,n;
  2772. if( fmt== NULL || (n=strlen(fmt))==0 )
  2773. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2774. n = strlen(fmt);
  2775. for(i=0,j=0; i<n; ++i)
  2776. {
  2777. // handle the escape character
  2778. if( fmt[i] == '\\' )
  2779. ++i; // for now we will just skip the next character
  2780. else
  2781. if( fmt[i] == '%' )
  2782. {
  2783. unsigned fn;
  2784. if((fn = strcspn(fmt+i,"diouxXfeEgGcs")) == 0 )
  2785. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Invalid format string on input conversion at index:%i.",j);
  2786. ++fn;
  2787. fmtArray[j].fsi = i;
  2788. fmtArray[j].fsn = fn;
  2789. snprintf(fmtArray[j].label,kSprintfLabelCharCnt,"in-%i",j);
  2790. fmtArray[j].label[kSprintfLabelCharCnt]=0;
  2791. fmtArray[j].label[kSprintfDocCharCnt] = 0;
  2792. switch( fmt[ i + fn - 1 ] )
  2793. {
  2794. case 'd':
  2795. case 'i':
  2796. fmtArray[j].flags = kIntDsvFl;
  2797. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Integer input %i.",j);
  2798. break;
  2799. case 'o':
  2800. case 'u':
  2801. case 'x':
  2802. case 'X':
  2803. fmtArray[j].flags = kUIntDsvFl;
  2804. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned input %i.",j);
  2805. break;
  2806. case 'f':
  2807. case 'e':
  2808. case 'E':
  2809. case 'g':
  2810. case 'G':
  2811. fmtArray[j].flags = kDoubleDsvFl;
  2812. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Double input %i.",j);
  2813. break;
  2814. case 'c':
  2815. fmtArray[j].flags = kUCharDsvFl;
  2816. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned char input %i.",j);
  2817. break;
  2818. case 's':
  2819. fmtArray[j].flags = kStrzDsvFl | kSymDsvFl;
  2820. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"String input %i.",j);
  2821. break;
  2822. default:
  2823. { assert(0); }
  2824. }
  2825. i += fn - 1;
  2826. ++j;
  2827. }
  2828. }
  2829. return kOkDspRC;
  2830. }
  2831. cmDspRC_t _cmSprintfLoadFormat(cmDspSprintf_t** pp, cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instSymId, unsigned id, unsigned storeSymId, cmDspVarArg_t args[], unsigned va_cnt, va_list vl )
  2832. {
  2833. cmDspRC_t rc;
  2834. unsigned i,j;
  2835. va_list vl2;
  2836. unsigned inCnt = 0;
  2837. unsigned fixedArgCnt = 0;
  2838. cmDspSprintf_t* p = NULL;
  2839. const cmChar_t* fmt = NULL;
  2840. cmDspSprintfFmt_t* fmtArray = NULL;
  2841. va_copy(vl2,vl);
  2842. *pp = NULL;
  2843. if( va_cnt > 0 )
  2844. fmt = va_arg(vl,const char*);
  2845. if( va_cnt < 1 || fmt == NULL )
  2846. return cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"Expected the format string as the first argument.");
  2847. // calc the number of fixed args
  2848. for(i=0; args[i].label != NULL; ++i)
  2849. ++fixedArgCnt;
  2850. // calc the count of input args
  2851. if((rc = _cmSprintfGetInputCnt(ctx, classPtr, fmt, &inCnt)) != kOkDspRC )
  2852. return rc;
  2853. if( inCnt > 0 )
  2854. {
  2855. fmtArray = cmLhAllocZ( ctx->lhH, cmDspSprintfFmt_t, inCnt );
  2856. rc = _cmSprintfGetInputTypes(ctx, classPtr, fmt, fmtArray, inCnt );
  2857. }
  2858. if( rc == kOkDspRC )
  2859. {
  2860. unsigned argCnt = fixedArgCnt + inCnt;
  2861. cmDspVarArg_t argArray[ argCnt+1 ];
  2862. // copy in fixed args
  2863. for(i=0; i<fixedArgCnt; ++i)
  2864. argArray[i] = args[i];
  2865. // calc input args
  2866. for(j=0; i<argCnt; ++i,++j)
  2867. {
  2868. argArray[i].label = fmtArray[j].label;
  2869. argArray[i].constId = i;
  2870. argArray[i].rn = 0;
  2871. argArray[i].cn = 0;
  2872. argArray[i].flags = kInDsvFl | fmtArray[j].flags;
  2873. argArray[i].doc = fmtArray[j].doc;
  2874. }
  2875. // set the sentinel arg to all zeros
  2876. memset(argArray + argCnt,0,sizeof(cmDspVarArg_t));
  2877. if((p = cmDspInstAlloc(cmDspSprintf_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl2)) != NULL )
  2878. {
  2879. // make a copy of the format string - we need to be sure that it is in
  2880. // r/w memory in order that _cmDspSprintfGenString() can write to it
  2881. p->fmtStr = cmLhResizeN(ctx->lhH,cmChar_t,p->fmtStr,strlen(fmt)+1);
  2882. strcpy(p->fmtStr,fmt);
  2883. cmLhFree(ctx->lhH,p->fmtArray);
  2884. p->fmtArray = fmtArray;
  2885. p->inCnt = inCnt;
  2886. memset(p->buf,0,kSprintfBufCharCnt+1);
  2887. }
  2888. *pp = p;
  2889. }
  2890. if( cmErrLastRC(&classPtr->err) != kOkDspRC )
  2891. cmLhFree(ctx->lhH,fmtArray);
  2892. return rc;
  2893. }
  2894. cmDspRC_t _cmDspSprintfGenString(cmDspCtx_t* ctx, cmDspSprintf_t* p )
  2895. {
  2896. cmDspRC_t rc = kOkDspRC;
  2897. unsigned fsi = 0; // format string index
  2898. unsigned i = 0; // fmtArray[] index
  2899. unsigned bi = 0; // string buffer index
  2900. unsigned bn = kSprintfBufCharCnt; // available char's in the string buffer
  2901. cmChar_t* fmt = p->fmtStr;
  2902. unsigned fn = strlen(fmt)+1;
  2903. // for each
  2904. for(i=0; i<p->inCnt && bn>0; ++i)
  2905. {
  2906. const cmDspSprintfFmt_t* f = p->fmtArray + i;
  2907. unsigned varId = kInSpId + i;
  2908. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(&p->inst, varId);
  2909. assert(varPtr != NULL);
  2910. // if there are literal char's to copy prior to the format
  2911. if( fsi < f->fsi )
  2912. {
  2913. unsigned cn = cmMin(f->fsi-fsi,bn);
  2914. strncpy(p->buf+bi,fmt+fsi,cn);
  2915. bn -= cn;
  2916. fsi += cn;
  2917. bi += cn;
  2918. }
  2919. if( bn == 0 )
  2920. {
  2921. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The internal string buffer is too small.");
  2922. break;
  2923. }
  2924. unsigned pn = 0;
  2925. char c = fmt[ f->fsi + f->fsn ];
  2926. // zero terminate the format string for this input
  2927. fmt[ f->fsi + f->fsn] = 0;
  2928. // if the conversion fmt is for a string then the kSymDsvFl will be set
  2929. // which will prevent the switch() from working - so clear the sym flag here.
  2930. unsigned flags = cmClrFlag(f->flags,kSymDsvFl);
  2931. switch(flags)
  2932. {
  2933. case kUCharDsvFl:
  2934. // not implemented - need to implment a uchar variable type or
  2935. // assume a one character strz.
  2936. assert(0);
  2937. break;
  2938. case kIntDsvFl:
  2939. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspInt(&p->inst,varId));
  2940. break;
  2941. case kUIntDsvFl:
  2942. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspUInt(&p->inst,varId));
  2943. break;
  2944. case kDoubleDsvFl:
  2945. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspDouble(&p->inst,varId));
  2946. break;
  2947. case kStrzDsvFl:
  2948. if( cmDspIsSymbol(&p->inst,varId) )
  2949. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDspSymbol(&p->inst,varId))));
  2950. else
  2951. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspStrcz(&p->inst,varId));
  2952. break;
  2953. default:
  2954. { assert(0); }
  2955. }
  2956. // restore the char written over by the termination zero
  2957. fmt[ f->fsi + f->fsn] = c;
  2958. assert(pn<=bn);
  2959. bn -= pn;
  2960. bi += pn;
  2961. fsi += f->fsn;
  2962. }
  2963. // if there is literal text in the format string after the last conversion spec.
  2964. if( fsi < fn )
  2965. {
  2966. unsigned cn = cmMin(fn-fsi,bn);
  2967. strncpy(p->buf+bi,fmt+fsi,cn);
  2968. }
  2969. return rc;
  2970. }
  2971. cmDspInst_t* _cmDspSprintfAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2972. {
  2973. cmDspVarArg_t args[] =
  2974. {
  2975. { "fmt", kFmtSpId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Format string" },
  2976. { "out", kOutSpId, 0, 0, kOutDsvFl | kStrzDsvFl, "Output string" },
  2977. { NULL, 0, 0, 0, 0 }
  2978. };
  2979. cmDspSprintf_t* p = NULL;
  2980. if( _cmSprintfLoadFormat(&p, ctx, classPtr, instSymId, id, storeSymId, args, va_cnt, vl ) == kOkDspRC )
  2981. return &p->inst;
  2982. return NULL;
  2983. }
  2984. cmDspRC_t _cmDspSprintfFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2985. {
  2986. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  2987. cmLhFree(ctx->lhH,p->fmtArray);
  2988. cmLhFree(ctx->lhH,p->fmtStr);
  2989. p->fmtArray=NULL;
  2990. return kOkDspRC;
  2991. }
  2992. cmDspRC_t _cmDspSprintfReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2993. {
  2994. return kOkDspRC;
  2995. }
  2996. cmDspRC_t _cmDspSprintfRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2997. {
  2998. cmDspRC_t rc= kOkDspRC;
  2999. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3000. if( kInSpId <= evt->dstVarId && evt->dstVarId < kInSpId + p->inCnt )
  3001. {
  3002. cmDspSetEvent(ctx,inst,evt);
  3003. //if( evt->dstVarId == kInSpId )
  3004. if((rc = _cmDspSprintfGenString(ctx,p)) == kOkDspRC )
  3005. cmDspSetStrcz(ctx,inst,kOutSpId,p->buf);
  3006. }
  3007. return rc;
  3008. }
  3009. struct cmDspClass_str* cmSprintfClassCons( cmDspCtx_t* ctx )
  3010. {
  3011. cmDspClassSetup(&_cmSprintfDC,ctx,"Sprintf",
  3012. NULL,
  3013. _cmDspSprintfAlloc,
  3014. _cmDspSprintfFree,
  3015. _cmDspSprintfReset,
  3016. NULL,
  3017. _cmDspSprintfRecv,
  3018. NULL,NULL,
  3019. "String formatter." );
  3020. return &_cmSprintfDC;
  3021. }
  3022. //==========================================================================================================================================
  3023. enum
  3024. {
  3025. kOutAmId,
  3026. kBaseInAmId
  3027. };
  3028. cmDspClass_t _cmAMixDC;
  3029. typedef struct
  3030. {
  3031. cmDspInst_t inst;
  3032. unsigned inPortCnt;
  3033. unsigned baseGainId;
  3034. unsigned baseMuteId;
  3035. } cmDspAMix_t;
  3036. cmDspInst_t* _cmDspAMixAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3037. {
  3038. if( va_cnt < 1 )
  3039. {
  3040. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AMix' constructor must have a count of input ports.");
  3041. return NULL;
  3042. }
  3043. // args:
  3044. // <inPortCnt> <gain0>, <gain1> (the default gains are optional)
  3045. unsigned i;
  3046. int inPortCnt = va_arg(vl,int);
  3047. unsigned baseGainAmId = kBaseInAmId + inPortCnt;
  3048. unsigned baseMuteAmId = baseGainAmId + inPortCnt;
  3049. double dfltGain[ inPortCnt ];
  3050. if( va_cnt == 1 )
  3051. cmVOD_Fill(dfltGain,inPortCnt,1.0);
  3052. else
  3053. if( va_cnt == 2 )
  3054. {
  3055. dfltGain[0] = va_arg(vl,double);
  3056. cmVOD_Fill(dfltGain+1,inPortCnt-1,dfltGain[0]);
  3057. }
  3058. else
  3059. if( va_cnt == inPortCnt + 1 )
  3060. {
  3061. for(i=0; i<inPortCnt; ++i)
  3062. dfltGain[i] = va_arg(vl,double);
  3063. }
  3064. else
  3065. {
  3066. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The mix argument list must contain no default gain values, one default gain value, or all default gain values.");
  3067. return NULL;
  3068. }
  3069. cmDspAMix_t* p = cmDspInstAllocV(cmDspAMix_t,ctx,classPtr,instSymId,id,storeSymId,0,vl,
  3070. 1, "out", kOutAmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3071. inPortCnt, "in", kBaseInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3072. inPortCnt, "gain", baseGainAmId, 0, 0, kInDsvFl | kDoubleDsvFl, "Gain input",
  3073. inPortCnt, "mute", baseMuteAmId, 0, 0, kInDsvFl | kBoolDsvFl, "Mute input",
  3074. 0 );
  3075. p->inPortCnt = inPortCnt;
  3076. p->baseGainId = baseGainAmId;
  3077. p->baseMuteId = baseMuteAmId;
  3078. for(i=0; i<inPortCnt; ++i)
  3079. {
  3080. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dfltGain[i]);
  3081. cmDspSetDefaultBool( ctx, &p->inst, p->baseMuteId + i, false, false );
  3082. }
  3083. /*
  3084. // read any default gain settings
  3085. --va_cnt;
  3086. for(i=0; i<inPortCnt; ++i)
  3087. {
  3088. // if excplicit gains are not given then default to 1.0.
  3089. double dflt = 1.0;
  3090. if( i < va_cnt )
  3091. dflt = va_arg(vl,double);
  3092. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3093. }
  3094. */
  3095. return &p->inst;
  3096. }
  3097. cmDspRC_t _cmDspAMixReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3098. {
  3099. cmDspRC_t rc = kOkDspRC;
  3100. cmDspApplyAllDefaults(ctx,inst);
  3101. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3102. return rc;
  3103. }
  3104. cmDspRC_t _cmDspAMixExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3105. {
  3106. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3107. unsigned i;
  3108. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3109. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAmId,0);
  3110. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAmId,0);
  3111. for(i=0; i<p->inPortCnt; ++i)
  3112. {
  3113. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kBaseInAmId+i,0);
  3114. if( sp != NULL )
  3115. {
  3116. double gain = cmDspDouble(inst,p->baseGainId+i);
  3117. cmVOS_MultSumVVS(dp,n,sp,(cmSample_t)gain);
  3118. }
  3119. }
  3120. return kOkDspRC;
  3121. }
  3122. cmDspRC_t _cmDspAMixRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3123. {
  3124. cmDspRC_t rc = kOkDspRC;
  3125. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3126. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->inPortCnt )
  3127. {
  3128. cmDspSetEvent(ctx,inst,evt);
  3129. //printf("rcv:%i %f\n",evt->dstVarId,cmDspDouble(inst,evt->dstVarId));
  3130. }
  3131. return rc;
  3132. }
  3133. struct cmDspClass_str* cmAMixClassCons( cmDspCtx_t* ctx )
  3134. {
  3135. cmDspClassSetup(&_cmAMixDC,ctx,"AMix",
  3136. NULL,
  3137. _cmDspAMixAlloc,
  3138. NULL,
  3139. _cmDspAMixReset,
  3140. _cmDspAMixExec,
  3141. _cmDspAMixRecv,
  3142. NULL,NULL,
  3143. "Audio mixer");
  3144. return &_cmAMixDC;
  3145. }
  3146. //==========================================================================================================================================
  3147. enum
  3148. {
  3149. kInAsId,
  3150. kBaseOutAsId
  3151. };
  3152. cmDspClass_t _cmASplitDC;
  3153. typedef struct
  3154. {
  3155. cmDspInst_t inst;
  3156. unsigned outPortCnt;
  3157. unsigned baseGainId;
  3158. } cmDspASplit_t;
  3159. // A splitter has one audio input port and multiple audio output ports.
  3160. // A gain input is automatically provided for each output port.
  3161. cmDspInst_t* _cmDspASplitAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3162. {
  3163. cmDspVarArg_t args[] =
  3164. {
  3165. { "in", kInAsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  3166. { NULL, 0, 0, 0, 0 }
  3167. };
  3168. if( va_cnt < 1 )
  3169. {
  3170. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ASplit' constructor must have a count of input ports.");
  3171. return NULL;
  3172. }
  3173. // args:
  3174. // <outPortCnt> <gain0>, <gain1> (the default gains are optional)
  3175. unsigned i,j,k;
  3176. int outPortCnt = va_arg(vl,int);
  3177. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3178. unsigned argCnt = fixArgCnt + 2*outPortCnt + 1;
  3179. cmDspVarArg_t argArray[ argCnt ];
  3180. int labelCharCnt = 15;
  3181. cmChar_t label[ labelCharCnt + 1 ];
  3182. label[labelCharCnt] = 0;
  3183. //
  3184. for(i=0; i<fixArgCnt; ++i)
  3185. argArray[i] = args[i];
  3186. // define the audio output port specifications
  3187. for(j=0,k=0; j<outPortCnt; ++i,++j,++k)
  3188. {
  3189. snprintf(label,labelCharCnt,"out-%i",j);
  3190. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3191. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3192. argArray[i].constId = kBaseOutAsId + k;
  3193. argArray[i].rn = 0;
  3194. argArray[i].cn = 1;
  3195. argArray[i].flags = kOutDsvFl | kAudioBufDsvFl;
  3196. argArray[i].doc = "Audio Input";
  3197. }
  3198. // define the gain input specifications
  3199. for(j=0; j<outPortCnt; ++i,++j,++k)
  3200. {
  3201. snprintf(label,labelCharCnt,"gain-%i",j);
  3202. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3203. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3204. argArray[i].constId = kBaseOutAsId + k;
  3205. argArray[i].rn = 0;
  3206. argArray[i].cn = 0;
  3207. argArray[i].flags = kInDsvFl | kDoubleDsvFl;
  3208. argArray[i].doc = "Gain input";
  3209. }
  3210. // set the NULL end-of-arg-array sentinel
  3211. memset(argArray + i, 0, sizeof(argArray[0]));
  3212. cmDspASplit_t* p = cmDspInstAlloc(cmDspASplit_t,ctx,classPtr,argArray,instSymId,id,storeSymId,0,vl);
  3213. p->outPortCnt = outPortCnt;
  3214. p->baseGainId = kBaseOutAsId + outPortCnt;
  3215. // read any default gain settings
  3216. --va_cnt;
  3217. for(i=0; i<outPortCnt; ++i)
  3218. {
  3219. double dflt = 1.0;
  3220. if( i < va_cnt )
  3221. dflt = va_arg(vl,double);
  3222. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3223. }
  3224. return &p->inst;
  3225. }
  3226. cmDspRC_t _cmDspASplitReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3227. {
  3228. cmDspRC_t rc = kOkDspRC;
  3229. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3230. int i;
  3231. cmDspApplyAllDefaults(ctx,inst);
  3232. for(i=0; i<p->outPortCnt; ++i)
  3233. cmDspZeroAudioBuf(ctx,inst,kBaseOutAsId+i);
  3234. return rc;
  3235. }
  3236. cmDspRC_t _cmDspASplitExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3237. {
  3238. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3239. unsigned i;
  3240. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kBaseOutAsId,0);
  3241. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAsId,0);
  3242. for(i=0; i<p->outPortCnt; ++i)
  3243. {
  3244. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kBaseOutAsId+i,0);
  3245. double gain = cmDspDouble(inst,p->baseGainId+i);
  3246. cmVOS_MultVVS(dp,n,sp,(cmSample_t)gain);
  3247. }
  3248. return kOkDspRC;
  3249. }
  3250. cmDspRC_t _cmDspASplitRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3251. {
  3252. cmDspRC_t rc = kOkDspRC;
  3253. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3254. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->outPortCnt )
  3255. {
  3256. cmDspSetEvent(ctx,inst,evt);
  3257. }
  3258. return rc;
  3259. }
  3260. struct cmDspClass_str* cmASplitClassCons( cmDspCtx_t* ctx )
  3261. {
  3262. cmDspClassSetup(&_cmASplitDC,ctx,"ASplit",
  3263. NULL,
  3264. _cmDspASplitAlloc,
  3265. NULL,
  3266. _cmDspASplitReset,
  3267. _cmDspASplitExec,
  3268. _cmDspASplitRecv,
  3269. NULL,NULL,
  3270. "Audio splitter");
  3271. return &_cmASplitDC;
  3272. }
  3273. //==========================================================================================================================================
  3274. enum
  3275. {
  3276. kInAmId,
  3277. kMinAmId,
  3278. kMaxAmId,
  3279. kValAmId, // meter value
  3280. kLblAmId,
  3281. };
  3282. #define cmDspMeter_MIN (-100)
  3283. #define cmDspMeter_MAX (0)
  3284. cmDspClass_t _cmAMeterDC;
  3285. typedef struct
  3286. {
  3287. cmDspInst_t inst;
  3288. unsigned bufN;
  3289. unsigned idx;
  3290. cmReal_t sum;
  3291. cmReal_t val;
  3292. } cmDspAMeter_t;
  3293. cmDspInst_t* _cmDspAMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3294. {
  3295. cmDspVarArg_t args[] =
  3296. {
  3297. { "in", kInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3298. { "min", kMinAmId, 0, 0, kDoubleDsvFl, "Minimum value"},
  3299. { "max", kMaxAmId, 0, 0, kDoubleDsvFl, "Maximum value"},
  3300. { "val", kValAmId, 0, 0, kDoubleDsvFl, "Meter value"},
  3301. { "label",kLblAmId, 0, 0, kStrzDsvFl, "Label."},
  3302. { NULL, 0, 0, 0, 0 }
  3303. };
  3304. cmDspAMeter_t* p = cmDspInstAlloc(cmDspAMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3305. double updateMs = 100;
  3306. double sr = cmDspSampleRate(ctx);
  3307. unsigned spc = cmDspSamplesPerCycle(ctx);
  3308. p->bufN = cmMax(1,floor(updateMs * sr/ (1000.0 * spc)));
  3309. cmDspSetDefaultDouble(ctx, &p->inst, kValAmId, 0.0, cmDspMeter_MIN);
  3310. cmDspSetDefaultDouble(ctx, &p->inst, kMinAmId, 0.0, cmDspMeter_MIN);
  3311. cmDspSetDefaultDouble(ctx, &p->inst, kMaxAmId, 0.0, cmDspMeter_MAX);
  3312. // create the UI control
  3313. cmDspUiMeterCreate(ctx,&p->inst,kMinAmId,kMaxAmId,kValAmId,kLblAmId);
  3314. return &p->inst;
  3315. }
  3316. cmDspRC_t _cmDspAMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3317. {
  3318. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3319. cmDspApplyAllDefaults(ctx,inst);
  3320. //cmDspZeroAudioBuf(ctx,inst,kInAmId);
  3321. p->idx = 0;
  3322. p->sum = 0;
  3323. p->val = 0;
  3324. return kOkDspRC;
  3325. }
  3326. cmDspRC_t _cmDspAMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3327. {
  3328. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3329. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInAmId,0);
  3330. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAmId,0);
  3331. if( sp == NULL )
  3332. {
  3333. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  3334. return kOkDspRC;
  3335. }
  3336. p->sum += cmVOS_SquaredSum(sp,n);
  3337. ++p->idx;
  3338. if( p->idx == p->bufN )
  3339. {
  3340. cmReal_t coeff = 0.7;
  3341. cmReal_t rms = sqrt(p->sum/(n*p->bufN));
  3342. p->idx = 0;
  3343. p->sum = 0;
  3344. p->val = rms > p->val ? rms : (rms*(1.0-coeff)) + (p->val*coeff);
  3345. double db = cmMax(cmDspMeter_MIN,cmMin(cmDspMeter_MAX,20.0 * log10(p->val)));
  3346. cmDspSetDouble(ctx, inst, kValAmId, db);
  3347. }
  3348. return kOkDspRC;
  3349. }
  3350. struct cmDspClass_str* cmAMeterClassCons( cmDspCtx_t* ctx )
  3351. {
  3352. cmDspClassSetup(&_cmAMeterDC,ctx,"AMeter",
  3353. NULL,
  3354. _cmDspAMeterAlloc,
  3355. NULL,
  3356. _cmDspAMeterReset,
  3357. _cmDspAMeterExec,
  3358. NULL,
  3359. NULL,NULL,
  3360. "Audio meter display.");
  3361. return &_cmAMeterDC;
  3362. }
  3363. //==========================================================================================================================================
  3364. //
  3365. //
  3366. // Read files created by this object with the Octave function cmTextFile().
  3367. //
  3368. //
  3369. enum
  3370. {
  3371. kCntTfId,
  3372. kFnTfId,
  3373. kBaseTfId
  3374. };
  3375. cmDspClass_t _cmTextFileDC;
  3376. typedef struct
  3377. {
  3378. cmDspInst_t inst;
  3379. int inPortCnt;
  3380. cmFileH_t fH;
  3381. } cmDspTextFile_t;
  3382. cmDspInst_t* _cmDspTextFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3383. {
  3384. cmDspVarArg_t args[] =
  3385. {
  3386. { "cnt", kCntTfId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3387. { "fn", kFnTfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3388. { NULL, 0, 0, 0, 0 }
  3389. };
  3390. if( va_cnt < 1 )
  3391. {
  3392. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file object must be given a input port count argument.");
  3393. return NULL;
  3394. }
  3395. va_list vl1;
  3396. va_copy(vl1,vl);
  3397. int i,j;
  3398. int inPortCnt = va_arg(vl1,int);
  3399. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3400. unsigned argCnt = fixArgCnt + inPortCnt + 1;
  3401. cmDspVarArg_t argArray[ argCnt ];
  3402. if( inPortCnt <= 0 )
  3403. {
  3404. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file input port count must be a positive integer.");
  3405. return NULL;
  3406. }
  3407. //
  3408. for(i=0; i<fixArgCnt; ++i)
  3409. argArray[i] = args[i];
  3410. // define the input port specifications
  3411. for(j=0; j<inPortCnt; ++j,++i)
  3412. {
  3413. int labelCharCnt = 15;
  3414. cmChar_t label[ labelCharCnt + 1 ];
  3415. label[labelCharCnt] = 0;
  3416. snprintf(label,labelCharCnt,"in-%i",j);
  3417. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3418. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3419. argArray[i].constId = kBaseTfId + j;
  3420. argArray[i].rn = 0;
  3421. argArray[i].cn = 0;
  3422. argArray[i].flags = kInDsvFl | kDoubleDsvFl | kSymDsvFl;
  3423. argArray[i].doc = "Data input";
  3424. }
  3425. // set the NULL end-of-arg-array sentinel
  3426. memset(argArray + i, 0, sizeof(argArray[0]));
  3427. cmDspTextFile_t* p = cmDspInstAlloc(cmDspTextFile_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl);
  3428. p->inPortCnt = inPortCnt;
  3429. p->fH = cmFileNullHandle;
  3430. return &p->inst;
  3431. }
  3432. cmDspRC_t _cmDspTextFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3433. {
  3434. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3435. if( cmFileClose(&p->fH) )
  3436. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Text file close failed.");
  3437. return kOkDspRC;
  3438. }
  3439. cmDspRC_t _cmDspTextFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3440. {
  3441. cmDspRC_t rc = kOkDspRC;
  3442. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3443. cmDspApplyAllDefaults(ctx,inst);
  3444. const cmChar_t* fn = cmDspStrcz(inst,kFnTfId);
  3445. if( cmFileOpen( &p->fH, fn, kWriteFileFl, ctx->cmCtx->err.rpt ) != kOkFileRC )
  3446. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Text file open failed.");
  3447. return rc;
  3448. }
  3449. cmDspRC_t _cmDspTextFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3450. {
  3451. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3452. cmDspSetEvent(ctx,inst,evt);
  3453. if( cmFileIsValid(p->fH) && evt->dstVarId >= kBaseTfId )
  3454. {
  3455. double secs = ctx->ctx->begSmpIdx / cmDspSampleRate(ctx);
  3456. cmFilePrintf(p->fH,"%f %f %i ", secs, secs, evt->dstVarId-kBaseTfId);
  3457. if( cmIsFlag(evt->valuePtr->flags,kSymDsvFl) )
  3458. cmFilePrintf(p->fH,"%s\n", cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  3459. else
  3460. cmFilePrintf(p->fH,"%f\n", cmDspDouble(inst,evt->dstVarId));
  3461. }
  3462. return kOkDspRC;
  3463. }
  3464. struct cmDspClass_str* cmTextFileClassCons( cmDspCtx_t* ctx )
  3465. {
  3466. cmDspClassSetup(&_cmTextFileDC,ctx,"TextFile",
  3467. NULL,
  3468. _cmDspTextFileAlloc,
  3469. _cmDspTextFileFree,
  3470. _cmDspTextFileReset,
  3471. NULL,
  3472. _cmDspTextFileRecv,
  3473. NULL,NULL,
  3474. "Time tagged text file.");
  3475. return &_cmTextFileDC;
  3476. }
  3477. //==========================================================================================================================================
  3478. enum
  3479. {
  3480. kRsrcArId,
  3481. kCmdArId,
  3482. kIdxArId,
  3483. kValArId,
  3484. kCntArId,
  3485. kDoneArId,
  3486. kBaseOutArId
  3487. };
  3488. cmDspClass_t _cmArrayDC;
  3489. typedef struct
  3490. {
  3491. cmDspInst_t inst;
  3492. unsigned cnt;
  3493. cmReal_t* array;
  3494. unsigned printSymId;
  3495. unsigned sendSymId;
  3496. unsigned cntSymId;
  3497. unsigned doneSymId;
  3498. } cmDspArray_t;
  3499. cmDspInst_t* _cmDspArrayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3500. {
  3501. va_list vl1;
  3502. va_copy(vl1,vl);
  3503. if( va_cnt < 1 )
  3504. {
  3505. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object must be given a 'rsrc' parameter.");
  3506. return NULL;
  3507. }
  3508. const cmChar_t* rsrcStr = va_arg(vl,cmChar_t*);
  3509. unsigned outPortCnt = 0;
  3510. cmReal_t* array = NULL;
  3511. unsigned doneSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"done");
  3512. unsigned i;
  3513. if( rsrcStr == NULL )
  3514. {
  3515. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object 'rsrc' parameter appears to be missing.");
  3516. return NULL;
  3517. }
  3518. if( cmDspRsrcRealArray( ctx->dspH, &outPortCnt, &array, rsrcStr, NULL ) != kOkDspRC )
  3519. {
  3520. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array resource '%s' could not be read.",cmStringNullGuard(rsrcStr));
  3521. return NULL;
  3522. }
  3523. cmDspArray_t* p = cmDspInstAllocV(cmDspArray_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3524. 1, "rsrc", kRsrcArId, 0, 0, kStrzDsvFl | kInDsvFl | kReqArgDsvFl, "Array data resource label.",
  3525. 1, "cmd", kCmdArId, 0, 0, kSymDsvFl | kInDsvFl, "Command: send | print | count.",
  3526. 1, "idx", kIdxArId, 0, 0, kUIntDsvFl | kInDsvFl, "Send value at index out 'val' port.",
  3527. 1, "val", kValArId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Index output value.",
  3528. 1, "cnt", kCntArId, 0, 0, kUIntDsvFl | kOutDsvFl, "Count output value.",
  3529. 1, "done", kDoneArId, 0, 0, kSymDsvFl | kOutDsvFl, "'done' after last send.",
  3530. outPortCnt, "out", kBaseOutArId, 0, 0, kDoubleDsvFl | kOutDsvFl | kSendDfltDsvFl, "Individual real value outputs.",
  3531. 0 );
  3532. cmDspSetDefaultDouble( ctx, &p->inst, kValArId, 0, 0 );
  3533. cmDspSetDefaultUInt( ctx, &p->inst, kCntArId, 0, outPortCnt );
  3534. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneArId, doneSymId);
  3535. for(i=0; i<outPortCnt; ++i)
  3536. cmDspSetDefaultDouble( ctx, &p->inst, kBaseOutArId+i, 0, array[i] );
  3537. p->array = array;
  3538. p->cnt = outPortCnt;
  3539. p->sendSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"send");
  3540. p->printSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"print");
  3541. p->cntSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"count");
  3542. p->doneSymId = doneSymId;
  3543. return &p->inst;
  3544. }
  3545. cmDspRC_t _cmDspArrayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3546. {
  3547. cmDspArray_t* p = (cmDspArray_t*)inst;
  3548. cmDspRC_t rc;
  3549. // send 'out' values and then 'done' value
  3550. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3551. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3552. return rc;
  3553. }
  3554. cmDspRC_t _cmDspArrayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3555. {
  3556. cmDspRC_t rc;
  3557. cmDspArray_t* p = (cmDspArray_t*)inst;
  3558. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3559. return rc;
  3560. switch( evt->dstVarId )
  3561. {
  3562. case kCmdArId:
  3563. {
  3564. unsigned i;
  3565. unsigned cmdSymId = cmDsvSymbol(evt->valuePtr);
  3566. if( cmdSymId == p->printSymId )
  3567. {
  3568. for(i=0; i<p->cnt; ++i)
  3569. cmRptPrintf(ctx->rpt,"%f ",p->array[i]);
  3570. cmRptPrintf(ctx->rpt,"\n");
  3571. }
  3572. else
  3573. if( cmdSymId == p->sendSymId )
  3574. {
  3575. for(i=0; i<p->cnt; ++i)
  3576. cmDspSetDouble(ctx,inst,kBaseOutArId+i,p->array[i]);
  3577. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3578. }
  3579. else
  3580. if( cmdSymId == p->cntSymId )
  3581. {
  3582. cmDspSetUInt(ctx,inst,kCntArId,p->cnt);
  3583. }
  3584. }
  3585. break;
  3586. case kIdxArId:
  3587. {
  3588. unsigned idx = cmDsvUInt(evt->valuePtr);
  3589. if( idx < p->cnt )
  3590. cmDspSetDouble(ctx,inst,kValArId,p->array[idx]);
  3591. }
  3592. break;
  3593. }
  3594. return kOkDspRC;
  3595. }
  3596. struct cmDspClass_str* cmArrayClassCons( cmDspCtx_t* ctx )
  3597. {
  3598. cmDspClassSetup(&_cmArrayDC,ctx,"Array",
  3599. NULL,
  3600. _cmDspArrayAlloc,
  3601. NULL,
  3602. _cmDspArrayReset,
  3603. NULL,
  3604. _cmDspArrayRecv,
  3605. NULL,NULL,
  3606. "Time tagged text file.");
  3607. return &_cmArrayDC;
  3608. }
  3609. //==========================================================================================================================================
  3610. enum
  3611. {
  3612. kMidiPcId,
  3613. kHzPcId,
  3614. kOffsPcId,
  3615. kStrPcId,
  3616. kRatioPcId
  3617. };
  3618. cmDspClass_t _cmPitchCvtDC;
  3619. typedef struct
  3620. {
  3621. cmDspInst_t inst;
  3622. int midi;
  3623. double hz;
  3624. } cmDspPitchCvt_t;
  3625. cmDspInst_t* _cmDspPitchCvtAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3626. {
  3627. va_cnt = 0; // ignore an errant arguments
  3628. cmDspPitchCvt_t* p = cmDspInstAllocV(cmDspPitchCvt_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3629. 1, "midi", kMidiPcId, 0, 0, kUIntDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "MIDI pitch value input.",
  3630. 1, "hz", kHzPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "Hz pitch value.",
  3631. 1, "offs", kOffsPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kInDsvFl | kOptArgDsvFl, "Semitone offset.",
  3632. 1, "str", kStrPcId, 0, 0, kStrzDsvFl | kOutDsvFl, "Pitch string output.",
  3633. 1, "ratio", kRatioPcId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Offset as a ratio",
  3634. 0 );
  3635. cmDspSetDefaultUInt( ctx, &p->inst, kMidiPcId, 0, 0 );
  3636. cmDspSetDefaultDouble( ctx, &p->inst, kHzPcId, 0, 0.0 );
  3637. cmDspSetDefaultDouble( ctx, &p->inst, kOffsPcId, 0, 0 );
  3638. cmDspSetDefaultStrcz( ctx, &p->inst, kStrPcId, NULL, "" );
  3639. cmDspSetDefaultDouble( ctx, &p->inst, kRatioPcId, 0, 0 );
  3640. return &p->inst;
  3641. }
  3642. cmDspRC_t _cmDspPitchCvtReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3643. {
  3644. cmDspRC_t rc = cmDspApplyAllDefaults(ctx,inst);
  3645. return rc;
  3646. }
  3647. cmDspRC_t _cmDspPitchCvtOutput( cmDspCtx_t* ctx, cmDspPitchCvt_t* p )
  3648. {
  3649. cmDspInst_t* inst = &p->inst;
  3650. double offs = cmDspDouble( inst, kOffsPcId );
  3651. unsigned midi = cmMax(0, p->midi + rint(offs) );
  3652. double ratio= pow(2.0,offs/12.0);
  3653. double hz = p->hz * ratio;
  3654. //cmRptPrintf(ctx->rpt,"%i %i %f %f\n",offs,midi,hz,p->hz);
  3655. cmDspSetStrcz( ctx, inst, kStrPcId, cmMidiToSciPitch(midi,NULL,0));
  3656. cmDspSetUInt( ctx, inst, kMidiPcId, midi);
  3657. cmDspSetDouble( ctx, inst, kHzPcId, hz);
  3658. cmDspSetDouble( ctx, inst, kRatioPcId, ratio );
  3659. return kOkDspRC;
  3660. }
  3661. cmDspRC_t _cmDspPitchCvtRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3662. {
  3663. cmDspRC_t rc = kOkDspRC;
  3664. cmDspPitchCvt_t* p = (cmDspPitchCvt_t*)inst;
  3665. switch( evt->dstVarId )
  3666. {
  3667. case kMidiPcId:
  3668. p->midi = cmMax(0,cmDsvGetInt(evt->valuePtr));
  3669. p->hz = cmMidiToHz(p->midi);
  3670. rc = _cmDspPitchCvtOutput(ctx,p);
  3671. break;
  3672. case kHzPcId:
  3673. p->hz = cmDsvGetDouble(evt->valuePtr);
  3674. p->midi = cmHzToMidi(p->hz);
  3675. rc = _cmDspPitchCvtOutput(ctx,p);
  3676. break;
  3677. case kOffsPcId:
  3678. if((rc = cmDspSetEvent(ctx, inst, evt )) == kOkDspRC )
  3679. rc = _cmDspPitchCvtOutput(ctx,p);
  3680. break;
  3681. }
  3682. return rc;
  3683. }
  3684. struct cmDspClass_str* cmPitchCvtClassCons( cmDspCtx_t* ctx )
  3685. {
  3686. cmDspClassSetup(&_cmPitchCvtDC,ctx,"PitchCvt",
  3687. NULL,
  3688. _cmDspPitchCvtAlloc,
  3689. NULL,
  3690. _cmDspPitchCvtReset,
  3691. NULL,
  3692. _cmDspPitchCvtRecv,
  3693. NULL,NULL,
  3694. "Time tagged text file.");
  3695. return &_cmPitchCvtDC;
  3696. }
  3697. //==========================================================================================================================================
  3698. //
  3699. //
  3700. // Create a file which can be read by readBinFile.m
  3701. //
  3702. //
  3703. enum
  3704. {
  3705. kCntBmId,
  3706. kFnBmId,
  3707. kBaseBmId
  3708. };
  3709. cmDspClass_t _cmBinMtxFileDC;
  3710. typedef struct
  3711. {
  3712. cmDspInst_t inst;
  3713. int inPortCnt;
  3714. cmBinMtxFile_t* bmfp;
  3715. cmReal_t * valArray; // valArray[ inPortCnt ]
  3716. } cmDspBinMtxFile_t;
  3717. cmDspInst_t* _cmDspBinMtxFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3718. {
  3719. cmDspVarArg_t args[] =
  3720. {
  3721. { "cnt", kCntBmId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3722. { "fn", kFnBmId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3723. };
  3724. if( va_cnt < 1 )
  3725. {
  3726. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file object must be given a input port count argument.");
  3727. return NULL;
  3728. }
  3729. va_list vl1;
  3730. va_copy(vl1,vl);
  3731. int inPortCnt = va_arg(vl,int);
  3732. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  3733. unsigned argCnt = fixArgCnt + inPortCnt;
  3734. cmDspVarArg_t a[ argCnt+1 ];
  3735. cmDspBinMtxFile_t* p = NULL;
  3736. if( inPortCnt <= 0 )
  3737. {
  3738. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file must be a positive integer.");
  3739. return NULL;
  3740. }
  3741. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  3742. cmDspArgSetupN(ctx, a, argCnt, kBaseBmId, inPortCnt, "in", kBaseBmId, 0, 0, kInDsvFl | kDoubleDsvFl, "input ports");
  3743. cmDspArgSetupNull( a+argCnt );
  3744. if((p = cmDspInstAlloc(cmDspBinMtxFile_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  3745. return NULL;
  3746. p->bmfp = cmBinMtxFileAlloc(ctx->cmProcCtx, NULL, NULL );
  3747. p->inPortCnt = inPortCnt;
  3748. p->valArray = cmMemAllocZ(cmReal_t,inPortCnt);
  3749. return &p->inst;
  3750. }
  3751. cmDspRC_t _cmDspBinMtxFileOpen( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3752. {
  3753. cmDspRC_t rc = kOkDspRC;
  3754. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3755. if( p->bmfp != NULL )
  3756. {
  3757. // finalize the current file
  3758. if( cmBinMtxFileFinal(p->bmfp) != cmOkRC )
  3759. cmDspInstErr(ctx,inst,kFileCloseFailDspRC,"File close failed.");
  3760. // open a new one
  3761. if( cmBinMtxFileInit( p->bmfp, cmDspDefaultStrcz(&p->inst,kFnBmId) ) != cmOkRC)
  3762. rc = cmDspInstErr(ctx,inst,kFileOpenFailDspRC,"File open failed.");
  3763. }
  3764. return rc;
  3765. }
  3766. cmDspRC_t _cmDspBinMtxFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3767. {
  3768. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3769. cmBinMtxFileFree(&p->bmfp);
  3770. cmMemFree(p->valArray);
  3771. return kOkDspRC;
  3772. }
  3773. cmDspRC_t _cmDspBinMtxFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3774. {
  3775. cmDspApplyAllDefaults(ctx,inst);
  3776. return _cmDspBinMtxFileOpen(ctx,inst);
  3777. }
  3778. cmDspRC_t _cmDspBinMtxFileExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3779. {
  3780. cmDspRC_t rc = kOkDspRC;
  3781. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3782. // write the file
  3783. if( cmBinMtxFileIsValid( p->bmfp ) )
  3784. if( cmBinMtxFileExecR(p->bmfp, p->valArray, p->inPortCnt ) != cmOkRC )
  3785. return cmDspInstErr(ctx,inst,kFileWriteFailDspRC,"File write failure.");
  3786. return rc;
  3787. }
  3788. cmDspRC_t _cmDspBinMtxFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3789. {
  3790. cmDspRC_t rc = kOkDspRC;
  3791. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3792. cmDspSetEvent(ctx,inst,evt);
  3793. // new file name - create new output file
  3794. if( evt->dstVarId == kFnBmId )
  3795. {
  3796. rc = _cmDspBinMtxFileOpen(ctx,inst);
  3797. }
  3798. else
  3799. // new value - store in p->valArray[]
  3800. if( kBaseBmId <= evt->dstVarId && evt->dstVarId < kBaseBmId + p->inPortCnt )
  3801. {
  3802. p->valArray[ evt->dstVarId - kBaseBmId ] = cmDspDouble(inst,evt->dstVarId );
  3803. }
  3804. else
  3805. { assert(0); }
  3806. return rc;
  3807. }
  3808. struct cmDspClass_str* cmBinMtxFileClassCons( cmDspCtx_t* ctx )
  3809. {
  3810. cmDspClassSetup(&_cmBinMtxFileDC,ctx,"BinMtxFile",
  3811. NULL,
  3812. _cmDspBinMtxFileAlloc,
  3813. _cmDspBinMtxFileFree,
  3814. _cmDspBinMtxFileReset,
  3815. _cmDspBinMtxFileExec,
  3816. _cmDspBinMtxFileRecv,
  3817. NULL,NULL,
  3818. "Time tagged text file.");
  3819. return &_cmBinMtxFileDC;
  3820. }
  3821. //==========================================================================================================================================
  3822. enum
  3823. {
  3824. kHopMsSbId,
  3825. kWndFactSbId,
  3826. kInSbId,
  3827. kOutSbId
  3828. };
  3829. cmDspClass_t _cmShiftBufDC;
  3830. typedef struct
  3831. {
  3832. cmDspInst_t inst;
  3833. cmShiftBuf* sbp;
  3834. } cmDspShiftBuf_t;
  3835. void _cmDspShiftBufSetup( cmDspCtx_t* ctx, cmDspShiftBuf_t* p )
  3836. {
  3837. double hopMs = cmDspDouble(&p->inst,kHopMsSbId);
  3838. unsigned hopSmpCnt = lround(cmDspSampleRate(ctx) * hopMs / 1000.0 );
  3839. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndFactSbId) * hopSmpCnt;
  3840. if( p->sbp == NULL || hopSmpCnt != p->sbp->hopSmpCnt || wndSmpCnt != p->sbp->wndSmpCnt )
  3841. {
  3842. cmShiftBufFree(&p->sbp);
  3843. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx, NULL, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt );
  3844. }
  3845. }
  3846. cmDspInst_t* _cmDspShiftBufAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3847. {
  3848. cmDspVarArg_t args[] =
  3849. {
  3850. { "hopMs", kHopMsSbId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Hop size on milliseconds"},
  3851. { "wndFact", kWndFactSbId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Count of hops contained in each output buffer."},
  3852. { "in", kInSbId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3853. { "out", kOutSbId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio output"},
  3854. { NULL, 0, 0, 0, 0 }
  3855. };
  3856. // Note: by setting the column count of the output audio variable to zero
  3857. // we prevent it from being automatically assigned vector memory.
  3858. cmDspShiftBuf_t* p = cmDspInstAlloc(cmDspShiftBuf_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3859. return &p->inst;
  3860. }
  3861. cmDspRC_t _cmDspShiftBufFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3862. {
  3863. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  3864. cmShiftBufFree(&p->sbp);
  3865. return kOkDspRC;
  3866. }
  3867. cmDspRC_t _cmDspShiftBufReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3868. {
  3869. cmDspRC_t rc = kOkDspRC;
  3870. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  3871. cmDspApplyAllDefaults(ctx,inst);
  3872. _cmDspShiftBufSetup(ctx,p);
  3873. return rc;
  3874. }
  3875. cmDspRC_t _cmDspShiftBufRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3876. {
  3877. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  3878. cmDspSetEvent(ctx,inst,evt);
  3879. switch(evt->dstVarId)
  3880. {
  3881. case kHopMsSbId:
  3882. case kWndFactSbId:
  3883. _cmDspShiftBufSetup( ctx, p );
  3884. break;
  3885. };
  3886. return kOkDspRC;
  3887. }
  3888. cmDspRC_t _cmDspShiftBufExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3889. {
  3890. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  3891. unsigned sn = cmDspAudioBufSmpCount(ctx,inst,kInSbId,0);
  3892. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInSbId,0);
  3893. cmDspVar_t* varPtr = cmDspVarIdToPtr(inst,kOutSbId);
  3894. if( cmShiftBufExec(p->sbp, sp, sn ) )
  3895. cmDsvSetSampleMtx( &varPtr->value, p->sbp->outV, p->sbp->outN, 1);
  3896. else
  3897. cmDsvSetSampleMtx( &varPtr->value, NULL, 0, 0);
  3898. return kOkDspRC;
  3899. }
  3900. struct cmDspClass_str* cmShiftBufClassCons( cmDspCtx_t* ctx )
  3901. {
  3902. cmDspClassSetup(&_cmShiftBufDC,ctx,"ShiftBuf",
  3903. NULL,
  3904. _cmDspShiftBufAlloc,
  3905. _cmDspShiftBufFree,
  3906. _cmDspShiftBufReset,
  3907. _cmDspShiftBufExec,
  3908. _cmDspShiftBufRecv,
  3909. NULL,NULL,
  3910. "Time tagged text file.");
  3911. return &_cmShiftBufDC;
  3912. }
  3913. //==========================================================================================================================================
  3914. enum
  3915. {
  3916. kInNsId
  3917. };
  3918. cmDspClass_t _cmNetSendDC;
  3919. typedef struct
  3920. {
  3921. cmDspInst_t inst;
  3922. _cmDspSrcConn_t* srcConnPtr;
  3923. } cmDspNetSend_t;
  3924. cmDspInst_t* _cmDspNetSendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3925. {
  3926. cmDspVarArg_t args[] =
  3927. {
  3928. { "in", kInNsId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  3929. { NULL, 0, 0, 0, 0 }
  3930. };
  3931. assert( va_cnt == 1 );
  3932. _cmDspSrcConn_t* srcConnPtr = va_arg(vl,_cmDspSrcConn_t*);
  3933. assert( srcConnPtr != NULL );
  3934. cmDspNetSend_t* p = cmDspInstAlloc(cmDspNetSend_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl);
  3935. p->srcConnPtr = srcConnPtr;
  3936. return &p->inst;
  3937. }
  3938. cmDspRC_t _cmDspNetSendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3939. {
  3940. return kOkDspRC;
  3941. }
  3942. cmDspRC_t _cmDspNetSendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3943. {
  3944. cmDspNetSend_t* p = (cmDspNetSend_t*)inst;
  3945. return _cmDspSysNetSendEvent(ctx->dspH, p->srcConnPtr->dstNetNodeId, p->srcConnPtr->dstId, evt );
  3946. }
  3947. struct cmDspClass_str* cmNetSendClassCons( cmDspCtx_t* ctx )
  3948. {
  3949. cmDspClassSetup(&_cmNetSendDC,ctx,"NetSend",
  3950. NULL,
  3951. _cmDspNetSendAlloc,
  3952. NULL,
  3953. _cmDspNetSendReset,
  3954. NULL,
  3955. _cmDspNetSendRecv,
  3956. NULL,NULL,
  3957. "Print the value of any event arriving at 'in'.");
  3958. return &_cmNetSendDC;
  3959. }
  3960. //==========================================================================================================================================
  3961. //==========================================================================================================================================
  3962. cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
  3963. {
  3964. cmPrinterClassCons,
  3965. cmCounterClassCons,
  3966. cmPhasorClassCons,
  3967. cmAudioInClassCons,
  3968. cmAudioOutClassCons,
  3969. cmAudioFileOutClassCons,
  3970. cmSigGenClassCons,
  3971. cmScalarClassCons,
  3972. cmTextClassCons,
  3973. cmMeterClassCons,
  3974. cmLabelClassCons,
  3975. cmButtonClassCons,
  3976. cmReorderClassCons,
  3977. cmFnameClassCons,
  3978. cmMsgListClassCons,
  3979. cmWaveTableClassCons,
  3980. cmSprintfClassCons,
  3981. cmAMixClassCons,
  3982. cmASplitClassCons,
  3983. cmAMeterClassCons,
  3984. cmTextFileClassCons,
  3985. cmBinMtxFileClassCons,
  3986. cmArrayClassCons,
  3987. cmPitchCvtClassCons,
  3988. cmShiftBufClassCons,
  3989. cmNetSendClassCons,
  3990. cmDelayClassCons,
  3991. cmPShiftClassCons,
  3992. cmLoopRecdClassCons,
  3993. cmRectifyClassCons,
  3994. cmGateDetectClassCons,
  3995. cmAutoGainClassCons,
  3996. cmEnvFollowClassCons,
  3997. cmXfaderClassCons,
  3998. cmChCfgClassCons,
  3999. cmChordDetectClassCons,
  4000. cmFaderClassCons,
  4001. cmNoteSelectClassCons,
  4002. cmNetNoteSelectClassCons,
  4003. cmCombFiltClassCons,
  4004. cmScalarOpClassCons,
  4005. cmGroupSelClassCons,
  4006. cmAudioNofMClassCons,
  4007. cmRingModClassCons,
  4008. cmMsgDelayClassCons,
  4009. cmLineClassCons,
  4010. cmAdsrClassCons,
  4011. cmCompressorClassCons,
  4012. cmBiQuadEqClassCons,
  4013. cmDistDsClassCons,
  4014. cmDbToLinClassCons,
  4015. cmMtDelayClassCons,
  4016. cmNofMClassCons,
  4017. cm1ofNClassCons,
  4018. cm1UpClassCons,
  4019. cmGateToSymClassCons,
  4020. cmPortToSymClassCons,
  4021. cmRouterClassCons,
  4022. cmAvailChClassCons,
  4023. cmPresetClassCons,
  4024. cmBcastSymClassCons,
  4025. cmSegLineClassCons,
  4026. cmKrClassCons,
  4027. cmTimeLineClassCons,
  4028. cmScoreClassCons,
  4029. cmMidiFilePlayClassCons,
  4030. cmScFolClassCons,
  4031. NULL,
  4032. };
  4033. cmDspClassConsFunc_t cmDspClassGetBuiltIn( unsigned index )
  4034. {
  4035. unsigned n = sizeof(_cmDspClassBuiltInArray)/sizeof(cmDspClass_t*);
  4036. if( index >= n )
  4037. return NULL;
  4038. return _cmDspClassBuiltInArray[index];
  4039. }