libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspBuiltIn.c 170KB

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