libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmDspBuiltIn.c 170KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619
  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. };
  1980. cmDspClass_t _cmFnameDC;
  1981. typedef struct
  1982. {
  1983. cmDspInst_t inst;
  1984. } cmDspFname_t;
  1985. // Pattern string for HTML and image files:
  1986. // "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
  1987. // The va_list must include 3 args:
  1988. // A pointer to a string referning to a default filename or directory or NULL.
  1989. // A pointer to a string referring to a pattern string or NULL.
  1990. // A bool to set the 'dirFl'.
  1991. cmDspInst_t* _cmDspFnameAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1992. {
  1993. cmDspVarArg_t args[] =
  1994. {
  1995. { "dir", kDirFnId, 0, 0, kInDsvFl | kReqArgDsvFl | kBoolDsvFl, "Dir=true Filename=false" },
  1996. { "pat", kPatFnId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "File pattern string (e.g. HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png}))" },
  1997. { "out", kValFnId, 0, 0, kOutDsvFl | kOptArgDsvFl | kStrzDsvFl, "Current file or directory name." },
  1998. { NULL, 0, 0, 0, 0 }
  1999. };
  2000. cmDspFname_t* p = cmDspInstAlloc(cmDspFname_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2001. cmDspSetDefaultBool( ctx, &p->inst, kDirFnId, false, false);
  2002. cmDspSetDefaultStrcz(ctx, &p->inst, kPatFnId, NULL, "All Files (*.*)");
  2003. cmDspSetDefaultStrcz(ctx, &p->inst, kValFnId, NULL, cmFsUserDir());
  2004. cmDspUiFnameCreate(ctx,&p->inst,kValFnId,kPatFnId,kDirFnId);
  2005. return &p->inst;
  2006. }
  2007. cmDspRC_t _cmDspFnameReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2008. {
  2009. cmDspApplyAllDefaults(ctx,inst);
  2010. return kOkDspRC;
  2011. }
  2012. cmDspRC_t _cmDspFnameRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2013. {
  2014. cmDspSetEvent(ctx,inst,evt);
  2015. switch( evt->dstVarId )
  2016. {
  2017. case kDirFnId:
  2018. case kPatFnId:
  2019. case kValFnId:
  2020. break;
  2021. default:
  2022. {assert(0);}
  2023. }
  2024. return kOkDspRC;
  2025. }
  2026. struct cmDspClass_str* cmFnameClassCons( cmDspCtx_t* ctx )
  2027. {
  2028. cmDspClassSetup(&_cmFnameDC,ctx,"Fname",
  2029. NULL,
  2030. _cmDspFnameAlloc,
  2031. NULL,
  2032. _cmDspFnameReset,
  2033. NULL,
  2034. _cmDspFnameRecv,
  2035. NULL,NULL,
  2036. "File or directory chooser control.");
  2037. return &_cmFnameDC;
  2038. }
  2039. //==========================================================================================================================================
  2040. cmDspClass_t _cmMsgListDC;
  2041. typedef struct
  2042. {
  2043. cmDspInst_t inst;
  2044. cmJsonH_t jsH; // current JSON tree
  2045. cmJsonNode_t* np; // ptr to JSON array
  2046. unsigned colCnt; // number of elements in the JSON sub-arrays (rows)
  2047. 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)
  2048. unsigned* typeIdArray; // JSON types for each column size: typeIdArray[ colCnt ]
  2049. unsigned symColCnt; // same as number of elements of typeIdArray[] == kStringTId
  2050. unsigned symRowCnt; // same as cmJsonChildCount(p->np)
  2051. unsigned* symM; // symM[symRowCnt,symColCnt] symbol matrix
  2052. } cmDspMsgList_t;
  2053. // create a matrix to hold the symbol id's associated with any string values
  2054. cmDspRC_t _cmDspMsgListLoadSymbolMtx( cmDspCtx_t* ctx, cmDspMsgList_t* p )
  2055. {
  2056. assert(cmJsonIsArray(p->np));
  2057. unsigned i,j,k;
  2058. // remove any existing symbols
  2059. if( p->symM != NULL )
  2060. for(i=0; i<p->symColCnt*p->symRowCnt; ++i)
  2061. if( p->symM[i] != cmInvalidId )
  2062. cmSymTblRemove(ctx->stH,p->symM[i]);
  2063. // reallocate the symbol matrix
  2064. p->symRowCnt = cmJsonChildCount(p->np);
  2065. p->symM = cmLhResizeN(ctx->lhH,unsigned,p->symM,p->symRowCnt*p->symColCnt);
  2066. // for each row in the JSON array
  2067. for(i=0; i<p->symRowCnt; ++i)
  2068. {
  2069. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,i);
  2070. // for each column in row whose data type is a string
  2071. for(j=0,k=0; k<p->symColCnt; ++j)
  2072. if( p->typeIdArray[j] == kStringTId )
  2073. {
  2074. const cmJsonNode_t* vnp = cmJsonArrayElementC(cnp,j);
  2075. unsigned idx = k*p->symRowCnt + i;
  2076. const cmChar_t* text;
  2077. // register the string with the symbol table
  2078. if((text = vnp->u.stringVal) != NULL )
  2079. p->symM[ idx ] = cmSymTblRegisterStaticSymbol(ctx->stH,text);
  2080. else
  2081. p->symM[ idx ] = cmInvalidId;
  2082. ++k;
  2083. }
  2084. }
  2085. return kOkDspRC;
  2086. }
  2087. // Load a JSON file and set the supplied cmJsonH_t handle.
  2088. 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 )
  2089. {
  2090. cmDspRC_t rc = kOkDspRC; //
  2091. cmJsonNode_t* np = NULL; //
  2092. cmJsonH_t jsH = cmJsonNullHandle; //
  2093. *hp = cmJsonNullHandle;
  2094. *colCntPtr = 0;
  2095. *npp = NULL;
  2096. // if no file name was given ...
  2097. if( fn==NULL || strlen(fn)==0 )
  2098. {
  2099. jsH = ctx->rsrcJsH; // ... use the rsrc file
  2100. if( cmJsonIsValid(ctx->rsrcJsH) == false )
  2101. return cmErrMsg(err,kJsonFailDspRC,"No JSON cfg resource exists for this DSP program.");
  2102. fn = NULL;
  2103. }
  2104. else
  2105. {
  2106. if( cmJsonInitializeFromFile(&jsH,fn,ctx->cmCtx) != kOkJsRC )
  2107. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON file load failed on '%s'.",fn);
  2108. }
  2109. // find the array named by rsrcLabel
  2110. if((np = cmJsonFindValue(jsH,rsrcLabel,NULL,cmInvalidId)) == NULL)
  2111. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON tree does not have an array named '%s'.",rsrcLabel);
  2112. // be sure the msg list really is an array
  2113. if( cmJsonIsArray(np) == false )
  2114. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element named '%s' is not an array.", rsrcLabel);
  2115. if( fn == NULL )
  2116. fn = "<resource file>";
  2117. // count of elements in the array
  2118. unsigned n = cmJsonChildCount(np);
  2119. unsigned m = 0;
  2120. unsigned i,j;
  2121. // for each line in the array
  2122. for(i=0; i<n; ++i)
  2123. {
  2124. const cmJsonNode_t* cnp;
  2125. // get the ith sub-array (row)
  2126. if((cnp = cmJsonArrayElementC(np,i)) != NULL )
  2127. {
  2128. // verify that it is an array
  2129. if( cmJsonIsArray(cnp ) == false )
  2130. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element in '%s' at index %i is not an array.",fn,i);
  2131. // track the number of elements (columns) per row
  2132. unsigned q = cmJsonChildCount(cnp);
  2133. // if this is the first row then use it to set the valid column count
  2134. if( i==0 )
  2135. m = q;
  2136. else
  2137. {
  2138. if( m != q )
  2139. 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);
  2140. }
  2141. }
  2142. }
  2143. //
  2144. // determine and validate the column types
  2145. //
  2146. unsigned typeIdArray[m];
  2147. // for each row
  2148. for(i=0; i<n; ++i)
  2149. {
  2150. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
  2151. // for each column
  2152. for(j=0; j<m; ++j)
  2153. {
  2154. const cmJsonNode_t* sap = cmJsonArrayElementC(cnp,j);
  2155. unsigned typeId = sap->typeId & kMaskTId;
  2156. // the first row sets the expected type id for each column
  2157. switch(i)
  2158. {
  2159. case 0:
  2160. if( typeId != kStringTId )
  2161. return cmErrMsg(err,kJsonFailDspRC,"The first row of a msg list (%s) file must contain string elements which set the colum labels.",rsrcLabel);
  2162. break;
  2163. case 1:
  2164. typeIdArray[j] = typeId;
  2165. break;
  2166. default:
  2167. {
  2168. // if the type is a string then it can only
  2169. // match if the column is a string or null
  2170. // so we assume an error
  2171. if( typeId == kStringTId )
  2172. rc = kJsonFailDspRC;
  2173. switch(typeIdArray[j])
  2174. {
  2175. case kStringTId:
  2176. if( typeId != kStringTId && typeId != kNullTId)
  2177. 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);
  2178. rc = kOkDspRC; // clear the assummed error
  2179. break;
  2180. case kNullTId: // null can be converted to anything
  2181. typeIdArray[j] = typeId;
  2182. break;
  2183. case kIntTId:
  2184. if( typeId == kRealTId ) // ints may be promoted to reals
  2185. typeIdArray[j] = kRealTId;
  2186. break;
  2187. case kRealTId:
  2188. break;
  2189. case kFalseTId:
  2190. case kTrueTId: // bools may be promoted to ints or reals
  2191. if( typeId == kIntTId || typeId == kRealTId )
  2192. typeIdArray[j] = typeId;
  2193. break;
  2194. default:
  2195. 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);
  2196. }
  2197. if( rc != kOkDspRC )
  2198. 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);
  2199. } // end dflt
  2200. } // end switch
  2201. } // end row
  2202. } // end list
  2203. // VERY TRICKY - store the column type id's in the label columns type id.
  2204. // This is stupid but safe because the column type id's are known to be set to kStringTId.
  2205. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  2206. for(i=0; i<m; ++i,lnp=lnp->siblingPtr)
  2207. lnp->typeId = typeIdArray[i];
  2208. *hp = fn==NULL ? cmJsonNullHandle : jsH;
  2209. *npp = np;
  2210. *colCntPtr = m;
  2211. return rc;
  2212. }
  2213. // use the JSON list labels to setup the cmDspVarArg_t records associated with each msg output var.
  2214. 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 )
  2215. {
  2216. unsigned i,j;
  2217. unsigned fixedArgCnt = 0;
  2218. for(i=0; args[i].label != NULL; ++i)
  2219. ++fixedArgCnt;
  2220. cmDspVarArg_t a[ fixedArgCnt + colCnt + 1 ];
  2221. // copy the fixed arg's into the first fixedArgCnt ele's of a[]
  2222. for(j=0; j<fixedArgCnt; ++j)
  2223. a[j] = args[j];
  2224. // remove the label row from the JSON array
  2225. cmJsonNode_t* lnp = np->u.childPtr; // store the pointer to the label row
  2226. //np->u.childPtr = lnp->siblingPtr; // remove the labels from the array
  2227. // for each column
  2228. for(i=0; i<colCnt; ++i,++j)
  2229. {
  2230. cmJsonNode_t* lp = cmJsonArrayElement(lnp,i);
  2231. // store a pointer to the label
  2232. a[j].label = lp->u.stringVal;
  2233. a[j].constId = j;
  2234. a[j].rn = 0;
  2235. a[j].cn = 0;
  2236. a[j].flags = kOutDsvFl;
  2237. a[j].doc = "Msg output";
  2238. typeIdArray[i] = lp->typeId;
  2239. // convert the JSON type to a DSV type
  2240. switch( lp->typeId )
  2241. {
  2242. case kNullTId: a[j].flags |= kUIntDsvFl; break;
  2243. case kIntTId: a[j].flags |= kIntDsvFl; break;
  2244. case kRealTId: a[j].flags |= kDoubleDsvFl; break;
  2245. case kTrueTId: a[j].flags |= kBoolDsvFl; break;
  2246. case kFalseTId: a[j].flags |= kBoolDsvFl; break;
  2247. case kStringTId: a[j].flags |= kSymDsvFl; break; // strings are treated as symbols (UInt)
  2248. default:
  2249. { assert(0); }
  2250. }
  2251. // undo the tricky bit with the label types
  2252. lp->typeId = kStringTId;
  2253. }
  2254. // set the null sentinel at the end of the arg array
  2255. memset(a + j,0,sizeof(cmDspVarArg_t));
  2256. return (cmDspMsgList_t*)cmDspInstAlloc(cmDspMsgList_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  2257. }
  2258. cmDspRC_t _cmDspMsgListReload( cmDspCtx_t* ctx, cmDspMsgList_t* p, const cmChar_t* rsrcLabel, const cmChar_t* fn )
  2259. {
  2260. cmDspRC_t rc = kOkDspRC;
  2261. cmJsonH_t jsH = cmJsonNullHandle;
  2262. unsigned colCnt = 0;
  2263. cmJsonNode_t* np = NULL;
  2264. // load the file
  2265. if((rc = _cmDspMsgListLoadFile(ctx,&p->inst.classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) != kOkDspRC )
  2266. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2267. // verify that the col count is correct
  2268. if( colCnt != p->colCnt )
  2269. 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);
  2270. unsigned fixArgCnt = p->inst.varCnt - p->colCnt;
  2271. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  2272. unsigned i,j;
  2273. for(i=0,j=fixArgCnt; i<colCnt; ++i,++j, lnp=lnp->siblingPtr)
  2274. {
  2275. const cmChar_t* labelStr = cmSymTblLabel(ctx->stH,p->inst.varArray[j].symId);
  2276. // the labels of the new file must match the labels of the previous file
  2277. if( strcmp(lnp->u.stringVal,labelStr) )
  2278. 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);
  2279. // if the msg column is a string ....
  2280. if( (p->inst.varArray[j].flags & kTypeDsvMask) == kStrzDsvFl )
  2281. {
  2282. // ... then the file column must also be a string or null
  2283. if( lnp->typeId != kStringTId && np->typeId != kNullTId )
  2284. 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);
  2285. }
  2286. else // otherwie if the msg column is a number ...
  2287. {
  2288. // ... then the file type can't be a string
  2289. if( lnp->typeId == kStringTId )
  2290. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i cannot be a string.",fn,i);
  2291. // TODO: maybe there are other type conversions to check for here.
  2292. }
  2293. // reset the typeid of the labels
  2294. // (this is a cleanup from the tricky bit at the end of _cmDspListLoadFile())
  2295. lnp->typeId = kStringTId;
  2296. }
  2297. if( cmJsonIsValid( jsH ) )
  2298. {
  2299. if( cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,ctx->rsrcJsH) )
  2300. cmJsonFinalize(&p->jsH);
  2301. p->jsH = jsH;
  2302. p->np = np;
  2303. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2304. }
  2305. return rc;
  2306. }
  2307. enum
  2308. {
  2309. kRsrcMlId,
  2310. kFnMlId,
  2311. kSelMlId,
  2312. kListMlId,
  2313. kCntMlId,
  2314. kOutBaseMlId // identify the first output port
  2315. };
  2316. cmDspInst_t* _cmDspMsgListAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2317. {
  2318. va_list vl2;
  2319. cmDspVarArg_t args[] =
  2320. {
  2321. { "rsrc", kRsrcMlId, 0, 0, kInDsvFl | kReqArgDsvFl | kStrzDsvFl, "Msg list resource label"},
  2322. { "fn", kFnMlId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "Msg list file name" },
  2323. { "sel", kSelMlId, 0, 0, kOutDsvFl | kInDsvFl | kOptArgDsvFl | kUIntDsvFl, "Current selection index" },
  2324. { "list", kListMlId, 0, 0, kInDsvFl | kJsonDsvFl, "Msg list data as a JSON array"},
  2325. { "cnt", kCntMlId, 0, 0, kOutDsvFl | kSendDfltDsvFl | kUIntDsvFl , "Count of elements."},
  2326. { NULL, 0, 0, 0, 0 }
  2327. };
  2328. va_copy(vl2,vl);
  2329. if( va_cnt < 1 )
  2330. {
  2331. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"The message list constructor must contain at least two arguments.");
  2332. goto errLabel;
  2333. }
  2334. const cmChar_t* rsrcLabel = va_arg(vl,const char*);
  2335. const cmChar_t* fn = va_cnt>1 ? va_arg(vl,const char*) : NULL;
  2336. unsigned colCnt = 0;
  2337. cmJsonH_t jsH = cmJsonNullHandle;
  2338. cmDspMsgList_t* p;
  2339. cmJsonNode_t* np;
  2340. cmDspRC_t rc;
  2341. // be sure the rsrc label contains a valid string
  2342. if( rsrcLabel==NULL || strlen(rsrcLabel)==0 )
  2343. {
  2344. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"No msg list resource label was given.");
  2345. goto errLabel;
  2346. }
  2347. // load and validate the JSON file
  2348. if((rc = _cmDspMsgListLoadFile(ctx,&classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) == kOkDspRC )
  2349. {
  2350. unsigned i;
  2351. unsigned typeIdArray[colCnt];
  2352. // allocate the instance
  2353. if((p = _cmDspMsgListCons(ctx,classPtr,storeSymId,instSymId,id,args,jsH,colCnt, np, typeIdArray, va_cnt, vl2 )) != NULL )
  2354. {
  2355. p->jsH = jsH;
  2356. p->np = np;
  2357. p->dfltJsH = jsH;
  2358. p->colCnt = colCnt;
  2359. p->typeIdArray = cmLhAllocZ( ctx->lhH, unsigned, colCnt );
  2360. memcpy(p->typeIdArray,typeIdArray,sizeof(p->typeIdArray[0])*colCnt);
  2361. p->symColCnt = 0;
  2362. for(i=0; i<p->colCnt; ++i)
  2363. if( p->typeIdArray[i] == kStringTId )
  2364. ++p->symColCnt;
  2365. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2366. cmDspSetDefaultStrcz( ctx,&p->inst, kRsrcMlId,NULL, rsrcLabel); // rsrc label
  2367. cmDspSetDefaultStrcz( ctx,&p->inst, kFnMlId, NULL, fn); // file name var
  2368. cmDspSetDefaultJson( ctx,&p->inst, kListMlId,NULL, np); // default tree
  2369. cmDspSetDefaultUInt( ctx,&p->inst, kSelMlId, 0, 0); // selection
  2370. cmDspSetDefaultUInt( ctx,&p->inst, kCntMlId, 0, p->symRowCnt);
  2371. // if there is only one column then signal the UI to create a menu button rather
  2372. // than a list by setting the height to zero.
  2373. unsigned height = p->colCnt == 1 ? 0 : 5;
  2374. // create the list UI element
  2375. cmDspUiMsgListCreate(ctx, &p->inst, height, kListMlId, kSelMlId );
  2376. return &p->inst;
  2377. }
  2378. }
  2379. errLabel:
  2380. return NULL;
  2381. }
  2382. cmDspRC_t _cmDspMsgListFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2383. {
  2384. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2385. // be careful not release the ctx->rsrcJsH handle if is happens to be the p->dflJsH
  2386. if( cmJsonIsValid(p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH))
  2387. if( cmJsonFinalize(&p->dfltJsH) != kOkJsRC )
  2388. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON default tree finalize failed.");
  2389. // be careful not to release the ctx->rsrcJsH handle if it happens to be the p->jsH
  2390. if( cmJsonIsValid(p->jsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH) )
  2391. if( cmJsonFinalize( &p->jsH ) != kOkJsRC )
  2392. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON finalization failed.");
  2393. cmLhFree(ctx->lhH,p->typeIdArray);
  2394. p->typeIdArray = NULL;
  2395. cmLhFree(ctx->lhH,p->symM);
  2396. p->symM = NULL;
  2397. return kOkDspRC;
  2398. }
  2399. cmDspRC_t _cmDspMsgListReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2400. {
  2401. cmDspApplyAllDefaults(ctx,inst);
  2402. return kOkDspRC;
  2403. }
  2404. cmDspRC_t _cmDspMsgOnSel( cmDspCtx_t* ctx, cmDspMsgList_t* p, unsigned rowIdx )
  2405. {
  2406. cmDspRC_t rc = kOkDspRC;
  2407. unsigned i,j;
  2408. unsigned symId;
  2409. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,rowIdx);
  2410. assert( cnp != NULL );
  2411. // for each output variable (column)
  2412. for(i=0,j=0; i<p->colCnt; ++i)
  2413. {
  2414. unsigned varId = kOutBaseMlId + i; // variable id
  2415. const cmJsonNode_t* np = cmJsonArrayElementC(cnp,i); // json row array
  2416. assert( np != NULL );
  2417. switch( p->typeIdArray[i] )
  2418. {
  2419. case kNullTId:
  2420. break;
  2421. case kIntTId:
  2422. {
  2423. int v;
  2424. if( cmJsonIntValue(np,&v) != kOkJsRC )
  2425. { assert(0); }
  2426. rc = cmDspSetInt(ctx,&p->inst,varId,v);
  2427. }
  2428. break;
  2429. case kRealTId:
  2430. {
  2431. double v;
  2432. if( cmJsonRealValue(np,&v) != kOkJsRC )
  2433. { assert(0); }
  2434. rc = cmDspSetDouble(ctx,&p->inst,varId,v);
  2435. }
  2436. break;
  2437. case kTrueTId:
  2438. case kFalseTId:
  2439. {
  2440. bool v;
  2441. if( cmJsonBoolValue(np,&v) != kOkJsRC )
  2442. { assert(0); }
  2443. rc = cmDspSetBool(ctx,&p->inst,varId,v);
  2444. }
  2445. break;
  2446. case kStringTId:
  2447. {
  2448. assert( j < p->symColCnt );
  2449. if((symId = p->symM[ (j*p->symRowCnt) + rowIdx ]) != cmInvalidId )
  2450. if((rc = cmDspSetSymbol(ctx,&p->inst,varId,symId )) != kOkDspRC )
  2451. break;
  2452. ++j;
  2453. }
  2454. break;
  2455. default:
  2456. { assert(0); }
  2457. } // end switch
  2458. } // end for
  2459. return rc;
  2460. }
  2461. cmDspRC_t _cmDspMsgListRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2462. {
  2463. cmDspRC_t rc = kOkDspRC;
  2464. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2465. switch( evt->dstVarId )
  2466. {
  2467. case kRsrcMlId:
  2468. {
  2469. const cmChar_t* fn = cmDspStrcz(inst,kFnMlId);
  2470. const cmChar_t* rsrcLabel = cmDsvStrz(evt->valuePtr);
  2471. if( rsrcLabel != NULL )
  2472. {
  2473. if((rc = _cmDspMsgListReload(ctx,p,rsrcLabel,fn)) != kOkDspRC )
  2474. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2475. cmDspSetEvent(ctx,inst,evt);
  2476. cmDspSetJson(ctx,inst,kListMlId,p->np);
  2477. cmDspSetUInt(ctx,inst,kSelMlId,0);
  2478. cmDspSetUInt(ctx,inst,kCntMlId,p->symRowCnt);
  2479. }
  2480. }
  2481. break;
  2482. case kFnMlId:
  2483. // Store the new file name.
  2484. // A new file will not be loaded until the next rsrc label is received.
  2485. cmDspSetEvent(ctx,inst,evt);
  2486. break;
  2487. case kListMlId:
  2488. break;
  2489. case kSelMlId:
  2490. {
  2491. unsigned rowIdx = cmDsvGetUInt(evt->valuePtr);
  2492. assert( rowIdx < p->symRowCnt);
  2493. // set the current selection variable
  2494. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2495. rc = _cmDspMsgOnSel(ctx,p,rowIdx);
  2496. } //end case
  2497. break;
  2498. }
  2499. return rc;
  2500. }
  2501. cmDspRC_t _cmDspMsgListPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  2502. {
  2503. cmDspRC_t rc = kOkDspRC;
  2504. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2505. if((rc = cmDspVarPresetRdWr(ctx,inst,kSelMlId,storeFl)) == kOkDspRC )
  2506. {
  2507. if( !storeFl )
  2508. rc = _cmDspMsgOnSel(ctx,p, cmDspUInt(inst,kSelMlId) );
  2509. }
  2510. return rc;
  2511. }
  2512. struct cmDspClass_str* cmMsgListClassCons( cmDspCtx_t* ctx )
  2513. {
  2514. cmDspClassSetup(&_cmMsgListDC,ctx,"MsgList",
  2515. NULL,
  2516. _cmDspMsgListAlloc,
  2517. _cmDspMsgListFree,
  2518. _cmDspMsgListReset,
  2519. NULL,
  2520. _cmDspMsgListRecv,
  2521. _cmDspMsgListPresetRdWr,
  2522. NULL,
  2523. "Message list selection control.");
  2524. return &_cmMsgListDC;
  2525. }
  2526. //==========================================================================================================================================
  2527. enum
  2528. {
  2529. kLenWtId,
  2530. kShapeWtId,
  2531. kFnWtId,
  2532. kLoopWtId,
  2533. kBegWtId,
  2534. kEndWtId,
  2535. kCmdWtId,
  2536. kOtWtId,
  2537. kGainWtId,
  2538. kPhsWtId,
  2539. kOutWtId,
  2540. kCntWtId,
  2541. kFIdxWtId,
  2542. kDoneWtId
  2543. };
  2544. enum
  2545. {
  2546. kSilenceWtId, // 0
  2547. kFileWtId, // 1
  2548. kWhiteWtId, // 2
  2549. kPinkWtId, // 3
  2550. kSineWtId, // 4
  2551. kCosWtId, // 5
  2552. kSqrWtId, // 6
  2553. kTriWtId, // 7
  2554. kSawWtId, // 8
  2555. kPulseWtId, // 9
  2556. kImpulseWtId, // 10
  2557. kPhasorWtId, // 11
  2558. kShapeWtCnt
  2559. };
  2560. cmDspClass_t _cmWaveTableDC;
  2561. typedef struct
  2562. {
  2563. cmDspInst_t inst;
  2564. cmSample_t* wt; // wave table memory
  2565. unsigned wti; // next location to write samples into the wavetable
  2566. unsigned wtn; // count of empty samples (avail for writing over) in the wavetable.
  2567. unsigned fi; // absolute index into the file of the next sample to read
  2568. unsigned fn; // length of the file in samples
  2569. unsigned cfi; // absolute index into the file of the beginning of the current audio vector
  2570. unsigned cfn; // when cfi >= cfn and doneFl is set then the 'done' msg is sent
  2571. unsigned loopCnt; // current loop count
  2572. bool doneFl; // the wave table source is exhausted
  2573. cmAudioFileH_t afH; // current audio file handle
  2574. int nxtBegSmpIdx; // the beg/end sample index to use with the next filename to arrive at port 'fn'
  2575. int nxtEndSmpIdx; //
  2576. cmThreadH_t thH;
  2577. bool loadFileFl;
  2578. cmDspCtx_t* ctx;
  2579. cmSample_t phsOffs;
  2580. cmSample_t phsLast;
  2581. unsigned onSymId;
  2582. unsigned offSymId;
  2583. unsigned doneSymId;
  2584. bool useThreadFl;
  2585. unsigned minAfIndexRptCnt; // min count of audio samples between transmitting the current audio file index
  2586. unsigned afIndexRptCnt; // current audio file sample index count
  2587. } cmDspWaveTable_t;
  2588. bool _cmDspWaveTableThreadFunc( void* param);
  2589. cmDspInst_t* _cmDspWaveTableAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2590. {
  2591. cmDspVarArg_t args[] =
  2592. {
  2593. { "len", kLenWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave table length in samples" },
  2594. { "shape", kShapeWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=silent 1=file 2=sine 3=white" },
  2595. { "fn", kFnWtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Optional audio file name" },
  2596. { "loop", kLoopWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "-1=loop forever >0=loop count (dflt:-1)"},
  2597. { "beg", kBegWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File begin sample index" },
  2598. { "end", kEndWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File end sample index (-1=play all)" },
  2599. { "cmd", kCmdWtId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on off"},
  2600. { "ot", kOtWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count"},
  2601. { "gain", kGainWtId, 0, 0, kInDsvFl | kDoubleDsvFl|kOptArgDsvFl, "Gain"},
  2602. { "phs", kPhsWtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Driving phase" },
  2603. { "out", kOutWtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  2604. { "cnt", kCntWtId, 0, 0, kOutDsvFl | kIntDsvFl, "Loop count event."},
  2605. { "fidx", kFIdxWtId, 0, 0, kOutDsvFl | kUIntDsvFl, "Current audio file index."},
  2606. { "done", kDoneWtId, 0, 0, kOutDsvFl | kSymDsvFl, "'done' sent after last loop."},
  2607. { NULL, 0, 0, 0, 0 }
  2608. };
  2609. cmDspWaveTable_t* p = cmDspInstAlloc(cmDspWaveTable_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2610. // get the filename given in the va_list (or NULL if no filename was given)
  2611. const cmChar_t* fn = cmDspDefaultStrcz(&p->inst,kFnWtId);
  2612. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2613. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2614. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2615. double adCurFileIdxRptPeriodMs = 100.0;
  2616. p->minAfIndexRptCnt = floor(adCurFileIdxRptPeriodMs * cmDspSampleRate(ctx) / 1000.0);
  2617. cmDspSetDefaultUInt( ctx, &p->inst, kLenWtId, 0, cmDspSampleRate(ctx));
  2618. cmDspSetDefaultUInt( ctx, &p->inst, kShapeWtId, 0, kSilenceWtId );
  2619. cmDspSetDefaultStrcz( ctx, &p->inst, kFnWtId, NULL, fn );
  2620. cmDspSetDefaultInt( ctx, &p->inst, kLoopWtId, 0, -1 );
  2621. cmDspSetDefaultInt( ctx, &p->inst, kBegWtId, 0, 0 );
  2622. cmDspSetDefaultInt( ctx, &p->inst, kEndWtId, 0, -1 );
  2623. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdWtId, p->onSymId );
  2624. cmDspSetDefaultUInt( ctx, &p->inst, kOtWtId, 0, 5 );
  2625. cmDspSetDefaultDouble(ctx, &p->inst, kGainWtId, 0, 1.0 );
  2626. cmDspSetDefaultUInt( ctx, &p->inst, kFIdxWtId, 0, 0 );
  2627. p->useThreadFl = false;
  2628. return &p->inst;
  2629. }
  2630. cmDspRC_t _cmDspWaveTableFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2631. {
  2632. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2633. if( cmThreadIsValid(p->thH) )
  2634. if( cmThreadDestroy(&p->thH) != kOkThRC )
  2635. cmDspInstErr(ctx,inst,kThreadFailDspRC,"The wavetable file reader thread failed to close.");
  2636. if( cmAudioFileIsValid(p->afH) )
  2637. cmAudioFileDelete(&p->afH);
  2638. cmLhFree(ctx->lhH,p->wt);
  2639. return kOkDspRC;
  2640. }
  2641. // Read the next block of 'rdSmpCnt' samples starting at the absolute file index 'p->fi'
  2642. // into wt[rdSmpCnt].
  2643. // If the end of the file segment marked by absolute file indexes 'begSmpIdx' to 'endSmpIdx'
  2644. // is encountered in the middle of the requested block and the wave table is in loop
  2645. // mode then the the function will automatically begin reading from the begining of the
  2646. // file segment. If the end of the file segment is encountered and the wave table is not
  2647. // in loop mode then the empty portion of wt[] will be set to zero.
  2648. cmDspRC_t _cmDspWaveTableReadBlock( cmDspCtx_t* ctx, cmDspWaveTable_t* p, cmSample_t* wt, unsigned rdSmpCnt, int begSmpIdx, int endSmpIdx, int maxLoopCnt )
  2649. {
  2650. unsigned actFrmCnt = 0;
  2651. unsigned chIdx = 0;
  2652. unsigned chCnt = 1;
  2653. unsigned fn = endSmpIdx - p->fi + 1; // count of samples between p->fi and endSmpIdx
  2654. unsigned n0 = rdSmpCnt;
  2655. unsigned n1 = 0;
  2656. // if the requested sample count will go past the end of the file segment
  2657. if( rdSmpCnt > fn )
  2658. {
  2659. n1 = rdSmpCnt - fn;
  2660. n0 = rdSmpCnt - n1;
  2661. }
  2662. // if we don't have a valid file yet - then
  2663. if( cmAudioFileIsValid(p->afH) == false )
  2664. {
  2665. cmVOS_Zero(wt,n0);
  2666. return kOkDspRC;
  2667. }
  2668. // read the first block of samples
  2669. if( cmAudioFileReadSample(p->afH, n0, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2670. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2671. // BUG BUG BUG
  2672. // This assertion will fail if the file beg/end markers are not legal given the file length.
  2673. // An error msg would be more appropriate.
  2674. assert( actFrmCnt == n0 );
  2675. // increment the wave table pointer
  2676. wt += n0;
  2677. p->fi += n0;
  2678. // if n1 != 0 then we have encountered the end of the file segment
  2679. if( n1 > 0 )
  2680. {
  2681. ++p->loopCnt;
  2682. // send the loop count event
  2683. cmDspSetInt(ctx,&p->inst,kCntWtId,p->loopCnt);
  2684. // if we have played all the requested loops
  2685. if( maxLoopCnt != -1 && p->loopCnt >= maxLoopCnt )
  2686. {
  2687. p->doneFl = true;
  2688. cmVOS_Zero(wt,n1); // zero to the end of the buffer
  2689. p->cfn = p->cfi + cmDspUInt((cmDspInst_t*)p,kLenWtId) - p->wtn - n0;
  2690. assert( p->cfn >= p->cfi );
  2691. }
  2692. else
  2693. {
  2694. // seek to the first sample indicated by the 'beg' variable
  2695. if( cmAudioFileSeek(p->afH,begSmpIdx) != kOkAfRC )
  2696. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the wave table file.",fn);
  2697. // read the second block of samples
  2698. if( cmAudioFileReadSample(p->afH, n1, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2699. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2700. assert( actFrmCnt == n1 );
  2701. // reset the file index tracker
  2702. p->fi = begSmpIdx + n1;
  2703. p->cfi = begSmpIdx;
  2704. }
  2705. }
  2706. return kOkDspRC;
  2707. }
  2708. cmDspRC_t _cmDspWaveTableReadAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p, unsigned wtSmpCnt, unsigned rdSmpCnt )
  2709. {
  2710. unsigned n0 = rdSmpCnt;
  2711. unsigned n1 = 0;
  2712. int begSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2713. int endSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2714. int maxLoopCnt= cmDspInt(&p->inst,kLoopWtId);
  2715. if( endSmpIdx < begSmpIdx )
  2716. endSmpIdx = p->fn-1;
  2717. // if this read will wrap to the beginning of the wave table
  2718. if( p->wti + rdSmpCnt > wtSmpCnt )
  2719. {
  2720. n0 = wtSmpCnt - p->wti; // count of samples to read into the end of p->wt[]
  2721. n1 = rdSmpCnt - n0; // count of samples to read into the beg of p->wt[]
  2722. }
  2723. assert(n1<wtSmpCnt);
  2724. // the first read always starts at p->wt + p->wti
  2725. if( p->doneFl )
  2726. cmVOS_Zero(p->wt + p->wti,n0);
  2727. else
  2728. if( _cmDspWaveTableReadBlock(ctx, p, p->wt+p->wti, n0,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2729. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2730. p->wtn -= n0; // decrease the count of available samples
  2731. p->wti += n0;
  2732. if( n1 > 0 )
  2733. {
  2734. // the second read always starts at the beginning of the wave table
  2735. if( p->doneFl )
  2736. cmVOS_Zero(p->wt,n1);
  2737. else
  2738. if( _cmDspWaveTableReadBlock(ctx, p, p->wt, n1,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2739. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2740. p->wtn -= n1; // decrease the count of available samples
  2741. p->wti = n1;
  2742. }
  2743. //p->wtn -= rdSmpCnt; // decrease the count of available samples
  2744. return kOkDspRC;
  2745. }
  2746. cmDspRC_t _cmDspWaveTableInitAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2747. {
  2748. cmDspRC_t rc = kOkDspRC;
  2749. cmAudioFileH_t afH;
  2750. cmRC_t afRC;
  2751. cmAudioFileInfo_t afInfo;
  2752. const cmChar_t* fn = cmDspStrcz(&p->inst,kFnWtId);
  2753. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2754. int begSmpIdx= cmDspInt(&p->inst,kBegWtId);
  2755. // if the file name is valid
  2756. if( fn == NULL || strlen(fn)==0 )
  2757. {
  2758. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Audio file loading was requested for the wave table but no file name was given.");
  2759. goto errLabel;
  2760. }
  2761. // open the audio file
  2762. afH = cmAudioFileNewOpen(fn,&afInfo,&afRC,ctx->rpt);
  2763. // check for file open errors
  2764. if( afRC != kOkAfRC )
  2765. {
  2766. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' could not be opened. ('%s').",fn,cmAudioFileErrorMsg(afRC));
  2767. goto errLabel;
  2768. }
  2769. // if the file opened but is invalid
  2770. if( cmAudioFileIsValid(p->afH) )
  2771. cmAudioFileDelete(&p->afH);
  2772. // seek to the first sample indicated by the 'beg' variable
  2773. if( cmAudioFileSeek(afH,begSmpIdx) != kOkAfRC )
  2774. {
  2775. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the audio file '%s'.",fn);
  2776. goto errLabel;
  2777. }
  2778. p->afH = afH;
  2779. p->fi = begSmpIdx;
  2780. p->cfi = begSmpIdx;
  2781. p->fn = afInfo.frameCnt;
  2782. p->wti = 0;
  2783. p->wtn = wtSmpCnt;
  2784. // read the first block of samples
  2785. if((rc= _cmDspWaveTableReadAudioFile(ctx,p,wtSmpCnt,wtSmpCnt))!= kOkDspRC )
  2786. goto errLabel;
  2787. //printf("Wt:%s %i %i\n",fn,begSmpIdx,cmDspInt(&p->inst,kEndWtId));
  2788. // set the shape param to kFileWtId
  2789. //if((rc= cmDspSetUInt(ctx,&p->inst,kShapeWtId,kFileWtId)) != kOkDspRC )
  2790. // goto errLabel;
  2791. errLabel:
  2792. if( rc != kOkDspRC )
  2793. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2794. return rc;
  2795. }
  2796. bool _cmDspWaveTableThreadFunc( void* param)
  2797. {
  2798. cmDspWaveTable_t* p = (cmDspWaveTable_t*)param;
  2799. if( p->loadFileFl )
  2800. {
  2801. p->loadFileFl = false;
  2802. if( _cmDspWaveTableInitAudioFile(p->ctx,p) == kOkDspRC )
  2803. {
  2804. p->phsOffs = p->phsLast;
  2805. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2806. }
  2807. cmThreadPause(p->thH,kPauseThFl);
  2808. }
  2809. return true;
  2810. }
  2811. // Files are loaded via a background thread.
  2812. cmDspRC_t _cmDspWaveTableStartFileLoadThread( cmDspCtx_t* ctx, cmDspWaveTable_t* p, const cmChar_t* fn )
  2813. {
  2814. cmDspRC_t rc = kOkDspRC;
  2815. if( fn == NULL )
  2816. return rc;
  2817. if( p->loadFileFl )
  2818. 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));
  2819. if(p->useThreadFl && cmThreadIsValid(p->thH) == false)
  2820. cmThreadCreate(&p->thH,_cmDspWaveTableThreadFunc,p,ctx->rpt);
  2821. if(p->useThreadFl && cmThreadIsValid(p->thH) == false )
  2822. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because the audio load thread is invalid.",cmStringNullGuard(fn));
  2823. p->loadFileFl = true;
  2824. p->ctx = ctx;
  2825. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2826. cmDspSetStrcz(ctx,&p->inst,kFnWtId,fn);
  2827. if( p->useThreadFl == false )
  2828. {
  2829. // use non-threaded load
  2830. if((rc = _cmDspWaveTableInitAudioFile(p->ctx,p)) != kOkDspRC )
  2831. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' was not loaded.",cmStringNullGuard(fn));
  2832. p->phsOffs = p->phsLast;
  2833. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2834. p->loadFileFl = false;
  2835. }
  2836. else
  2837. {
  2838. // use threaded load - this works but it isn't really thread safe
  2839. if( cmThreadPause(p->thH,0) != kOkThRC )
  2840. return cmDspInstErr(ctx,&p->inst,kThreadFailDspRC,"The audio file '%s' was not loaded because audio thread enable failed.",cmStringNullGuard(fn));
  2841. }
  2842. return rc;
  2843. }
  2844. // This function is called whenever the source mode variable changes (or a new file name arrives)
  2845. cmDspRC_t _cmDspWaveTableCreateTable( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2846. {
  2847. cmDspRC_t rc = kOkDspRC;
  2848. unsigned shapeId = cmDspUInt(&p->inst,kShapeWtId);
  2849. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2850. unsigned otCnt = cmDspUInt(&p->inst,kOtWtId);
  2851. cmSample_t gain = 0.9;
  2852. double hz = 1.0;
  2853. double sr = cmDspSampleRate(ctx);
  2854. assert( wtSmpCnt > 0 );
  2855. if( p->wt == NULL )
  2856. p->wt = cmLhResizeNZ(ctx->lhH,cmSample_t,p->wt,wtSmpCnt);
  2857. else
  2858. cmVOS_Zero(p->wt,wtSmpCnt);
  2859. p->wtn = wtSmpCnt; // all samples in the wt are avail for filling
  2860. p->wti = 0; // beginning with the first sample
  2861. p->loopCnt = 0; // we are starting from a new source so set the loop cnt to 0
  2862. p->doneFl = false; // and the doneFl to false
  2863. assert( p->wt != NULL );
  2864. switch( shapeId )
  2865. {
  2866. case kSilenceWtId:
  2867. break;
  2868. case kFileWtId:
  2869. printf("Loading:%i %i %s\n",p->nxtBegSmpIdx,p->nxtEndSmpIdx,cmDspStrcz(&p->inst,kFnWtId));
  2870. rc = _cmDspWaveTableStartFileLoadThread(ctx,p,cmDspStrcz(&p->inst,kFnWtId));
  2871. break;
  2872. case kWhiteWtId:
  2873. cmVOS_Random(p->wt,wtSmpCnt,-gain,gain);
  2874. break;
  2875. case kPinkWtId:
  2876. cmVOS_SynthPinkNoise(p->wt,wtSmpCnt,0.0);
  2877. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2878. break;
  2879. case kSineWtId:
  2880. cmVOS_SynthSine(p->wt,wtSmpCnt,0,sr,hz);
  2881. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2882. break;
  2883. case kCosWtId:
  2884. cmVOS_SynthCosine(p->wt,wtSmpCnt,0,sr,hz);
  2885. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2886. break;
  2887. case kSawWtId:
  2888. cmVOS_SynthSawtooth(p->wt,wtSmpCnt,0,sr,hz,otCnt);
  2889. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2890. break;
  2891. case kSqrWtId:
  2892. cmVOS_SynthSquare( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2893. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2894. break;
  2895. case kTriWtId:
  2896. cmVOS_SynthTriangle( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2897. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2898. break;
  2899. case kPulseWtId:
  2900. cmVOS_SynthPulseCos( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2901. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2902. break;
  2903. case kPhasorWtId:
  2904. cmVOS_SynthPhasor( p->wt,wtSmpCnt,0,sr,hz );
  2905. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2906. break;
  2907. }
  2908. return rc;
  2909. }
  2910. cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2911. {
  2912. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2913. cmDspApplyAllDefaults(ctx,inst);
  2914. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2915. p->nxtBegSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2916. p->nxtEndSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2917. return _cmDspWaveTableCreateTable(ctx,p);
  2918. }
  2919. cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2920. {
  2921. cmDspRC_t rc = kOkDspRC;
  2922. const cmSample_t* phsV = cmDspAudioBuf(ctx,inst,kPhsWtId,0);
  2923. if( phsV == NULL )
  2924. {
  2925. inst->execFunc = NULL; // disable this instance because it has no input
  2926. return kOkDspRC;
  2927. }
  2928. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2929. unsigned mode = cmDspSymbol(inst,kCmdWtId);
  2930. unsigned srcId = cmDspUInt(inst,kShapeWtId);
  2931. if( mode == p->offSymId || srcId == kSilenceWtId )
  2932. {
  2933. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2934. return kOkDspRC;
  2935. }
  2936. cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
  2937. unsigned outCnt = cmDspVarRows(inst,kOutWtId);
  2938. unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
  2939. double gain = cmDspDouble(inst,kGainWtId);
  2940. unsigned i;
  2941. // for each output sample
  2942. for(i=0; i<outCnt; ++i)
  2943. {
  2944. // get the wave table location
  2945. //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
  2946. unsigned x = fmodf(phsV[i],wtSmpCnt);
  2947. // if the wt loctn is passed the end of the table
  2948. /*
  2949. if( x >= wtSmpCnt )
  2950. {
  2951. offs += wtSmpCnt;
  2952. x -= wtSmpCnt;
  2953. }
  2954. */
  2955. outV[i] = gain * p->wt[x];
  2956. }
  2957. p->phsLast = phsV[outCnt-1];
  2958. // if we are reading from a file ...
  2959. if( srcId == kFileWtId )
  2960. {
  2961. unsigned rdSmpCnt = 8192; // file read block sample count
  2962. p->wtn += outCnt;
  2963. // ... and there are rdSmpCnt avail locations in the wave table
  2964. if( p->wtn >= rdSmpCnt )
  2965. rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
  2966. // send the current audio file index
  2967. if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
  2968. {
  2969. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfn);
  2970. cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
  2971. }
  2972. else
  2973. {
  2974. if( p->afIndexRptCnt >= p->minAfIndexRptCnt )
  2975. {
  2976. p->afIndexRptCnt -= p->minAfIndexRptCnt;
  2977. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
  2978. }
  2979. }
  2980. p->afIndexRptCnt += outCnt;
  2981. p->cfi += outCnt;
  2982. }
  2983. return rc;
  2984. }
  2985. cmDspRC_t _cmDspWaveTableRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2986. {
  2987. cmDspRC_t rc = kOkDspRC;
  2988. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2989. switch( evt->dstVarId )
  2990. {
  2991. case kFnWtId: // a new file name arrived - this automatically switches the source mode to kFileWtId
  2992. {
  2993. const cmChar_t* fn = cmDsvStrz(evt->valuePtr);
  2994. if( cmFsIsFile( fn )==false )
  2995. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' does not exist.",cmStringNullGuard(fn));
  2996. else
  2997. {
  2998. cmDspSetEvent(ctx,inst,evt); // set the file name variable
  2999. cmDspSetInt(ctx,inst,kBegWtId,p->nxtBegSmpIdx); // set the beg/end smp idx var's from the stored nxtBeg/EndSmpIdx values
  3000. cmDspSetInt(ctx,inst,kEndWtId,p->nxtEndSmpIdx); //
  3001. cmDspSetUInt(ctx,inst,kShapeWtId,kFileWtId); // switch to file mode
  3002. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  3003. }
  3004. }
  3005. break;
  3006. case kBegWtId:
  3007. // store for next incoming file name msg
  3008. p->nxtBegSmpIdx = cmDsvGetInt(evt->valuePtr);
  3009. break;
  3010. case kEndWtId:
  3011. // store for next incoming file name msg
  3012. p->nxtEndSmpIdx = cmDsvGetInt(evt->valuePtr);
  3013. break;
  3014. case kShapeWtId:
  3015. if( cmDsvGetUInt(evt->valuePtr) < kShapeWtCnt )
  3016. {
  3017. cmDspSetEvent(ctx,inst,evt); // switch modes
  3018. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  3019. }
  3020. break;
  3021. case kLenWtId: // we don't support table size changes
  3022. break;
  3023. case kPhsWtId:
  3024. break;
  3025. case kCmdWtId:
  3026. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3027. {
  3028. if( cmDspSymbol(inst,kCmdWtId) == p->onSymId )
  3029. {
  3030. //rc = _cmDspWaveTableReset(ctx,inst, evt );
  3031. rc = _cmDspWaveTableCreateTable(ctx,p);
  3032. cmDspSetSymbol(ctx,inst,kCmdWtId,p->onSymId);
  3033. p->phsOffs = 0;
  3034. p->phsLast = 0;
  3035. }
  3036. }
  3037. break;
  3038. case kOtWtId:
  3039. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3040. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  3041. break;
  3042. case kGainWtId:
  3043. rc = cmDspSetEvent(ctx,inst,evt);
  3044. break;
  3045. default:
  3046. { assert(0); }
  3047. }
  3048. return rc;
  3049. }
  3050. struct cmDspClass_str* cmWaveTableClassCons( cmDspCtx_t* ctx )
  3051. {
  3052. cmDspClassSetup(&_cmWaveTableDC,ctx,"WaveTable",
  3053. NULL,
  3054. _cmDspWaveTableAlloc,
  3055. _cmDspWaveTableFree,
  3056. _cmDspWaveTableReset,
  3057. _cmDspWaveTableExec,
  3058. _cmDspWaveTableRecv,
  3059. NULL,
  3060. NULL,
  3061. "Variable frequency and waveshape signal generator." );
  3062. return &_cmWaveTableDC;
  3063. }
  3064. //==========================================================================================================================================
  3065. enum
  3066. {
  3067. kFmtSpId,
  3068. kOutSpId,
  3069. kInSpId,
  3070. kSprintfLabelCharCnt = 15,
  3071. kSprintfDocCharCnt = 31,
  3072. kSprintfBufCharCnt = 1023
  3073. };
  3074. cmDspClass_t _cmSprintfDC;
  3075. typedef struct
  3076. {
  3077. unsigned flags; // dsv type id for this fmt conversion or 0 if it is a literal string
  3078. unsigned fsi; // index into the fmt string where the fmt starts (always points to an '%' char)
  3079. unsigned fsn; // length of the format string
  3080. cmChar_t label[ kSprintfLabelCharCnt+1 ];
  3081. cmChar_t doc[ kSprintfDocCharCnt+1 ];
  3082. } cmDspSprintfFmt_t;
  3083. typedef struct
  3084. {
  3085. cmDspInst_t inst;
  3086. cmChar_t buf[ kSprintfBufCharCnt+1]; // output string buffer
  3087. unsigned inCnt; // count conversion spec's in the fmtStr[]
  3088. cmDspSprintfFmt_t* fmtArray; // fmtArray[inCnt]
  3089. cmChar_t* fmtStr; // C-style printf format string.
  3090. } cmDspSprintf_t;
  3091. cmDspRC_t _cmSprintfGetInputCnt( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, unsigned* inCntPtr )
  3092. {
  3093. unsigned i,n;
  3094. unsigned inCnt = 0;
  3095. if( fmt== NULL || (n=strlen(fmt))==0 )
  3096. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  3097. for(i=0; i<n; ++i)
  3098. {
  3099. // handle the escape character
  3100. if( fmt[i] == '\\' )
  3101. ++i; // for now we will just skip the next character
  3102. else
  3103. if( fmt[i] == '%' )
  3104. ++inCnt;
  3105. }
  3106. *inCntPtr = inCnt;
  3107. return kOkDspRC;
  3108. }
  3109. cmDspRC_t _cmSprintfGetInputTypes( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, cmDspSprintfFmt_t fmtArray[], unsigned inCnt )
  3110. {
  3111. unsigned i,j,n;
  3112. if( fmt== NULL || (n=strlen(fmt))==0 )
  3113. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  3114. n = strlen(fmt);
  3115. for(i=0,j=0; i<n; ++i)
  3116. {
  3117. // handle the escape character
  3118. if( fmt[i] == '\\' )
  3119. ++i; // for now we will just skip the next character
  3120. else
  3121. if( fmt[i] == '%' )
  3122. {
  3123. unsigned fn;
  3124. if((fn = strcspn(fmt+i,"diouxXfeEgGcs")) == 0 )
  3125. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Invalid format string on input conversion at index:%i.",j);
  3126. ++fn;
  3127. fmtArray[j].fsi = i;
  3128. fmtArray[j].fsn = fn;
  3129. snprintf(fmtArray[j].label,kSprintfLabelCharCnt,"in-%i",j);
  3130. fmtArray[j].label[kSprintfLabelCharCnt]=0;
  3131. fmtArray[j].doc[kSprintfDocCharCnt] = 0;
  3132. switch( fmt[ i + fn - 1 ] )
  3133. {
  3134. case 'd':
  3135. case 'i':
  3136. fmtArray[j].flags = kIntDsvFl;
  3137. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Integer input %i.",j);
  3138. break;
  3139. case 'o':
  3140. case 'u':
  3141. case 'x':
  3142. case 'X':
  3143. fmtArray[j].flags = kUIntDsvFl;
  3144. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned input %i.",j);
  3145. break;
  3146. case 'f':
  3147. case 'e':
  3148. case 'E':
  3149. case 'g':
  3150. case 'G':
  3151. fmtArray[j].flags = kDoubleDsvFl;
  3152. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Double input %i.",j);
  3153. break;
  3154. case 'c':
  3155. fmtArray[j].flags = kUCharDsvFl;
  3156. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned char input %i.",j);
  3157. break;
  3158. case 's':
  3159. fmtArray[j].flags = kStrzDsvFl | kSymDsvFl;
  3160. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"String input %i.",j);
  3161. break;
  3162. default:
  3163. { assert(0); }
  3164. }
  3165. i += fn - 1;
  3166. ++j;
  3167. }
  3168. }
  3169. return kOkDspRC;
  3170. }
  3171. 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 )
  3172. {
  3173. cmDspRC_t rc;
  3174. unsigned i,j;
  3175. va_list vl2;
  3176. unsigned inCnt = 0;
  3177. unsigned fixedArgCnt = 0;
  3178. cmDspSprintf_t* p = NULL;
  3179. const cmChar_t* fmt = NULL;
  3180. cmDspSprintfFmt_t* fmtArray = NULL;
  3181. va_copy(vl2,vl);
  3182. *pp = NULL;
  3183. if( va_cnt > 0 )
  3184. fmt = va_arg(vl,const char*);
  3185. if( va_cnt < 1 || fmt == NULL )
  3186. return cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"Expected the format string as the first argument.");
  3187. // calc the number of fixed args
  3188. for(i=0; args[i].label != NULL; ++i)
  3189. ++fixedArgCnt;
  3190. // calc the count of input args
  3191. if((rc = _cmSprintfGetInputCnt(ctx, classPtr, fmt, &inCnt)) != kOkDspRC )
  3192. return rc;
  3193. if( inCnt > 0 )
  3194. {
  3195. fmtArray = cmLhAllocZ( ctx->lhH, cmDspSprintfFmt_t, inCnt );
  3196. rc = _cmSprintfGetInputTypes(ctx, classPtr, fmt, fmtArray, inCnt );
  3197. }
  3198. if( rc == kOkDspRC )
  3199. {
  3200. unsigned argCnt = fixedArgCnt + inCnt;
  3201. cmDspVarArg_t argArray[ argCnt+1 ];
  3202. // copy in fixed args
  3203. for(i=0; i<fixedArgCnt; ++i)
  3204. argArray[i] = args[i];
  3205. // calc input args
  3206. for(j=0; i<argCnt; ++i,++j)
  3207. {
  3208. argArray[i].label = fmtArray[j].label;
  3209. argArray[i].constId = i;
  3210. argArray[i].rn = 0;
  3211. argArray[i].cn = 0;
  3212. argArray[i].flags = kInDsvFl | fmtArray[j].flags;
  3213. argArray[i].doc = fmtArray[j].doc;
  3214. }
  3215. // set the sentinel arg to all zeros
  3216. memset(argArray + argCnt,0,sizeof(cmDspVarArg_t));
  3217. if((p = cmDspInstAlloc(cmDspSprintf_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl2)) != NULL )
  3218. {
  3219. // make a copy of the format string - we need to be sure that it is in
  3220. // r/w memory in order that _cmDspSprintfGenString() can write to it
  3221. p->fmtStr = cmLhResizeN(ctx->lhH,cmChar_t,p->fmtStr,strlen(fmt)+1);
  3222. strcpy(p->fmtStr,fmt);
  3223. cmLhFree(ctx->lhH,p->fmtArray);
  3224. p->fmtArray = fmtArray;
  3225. p->inCnt = inCnt;
  3226. memset(p->buf,0,kSprintfBufCharCnt+1);
  3227. }
  3228. *pp = p;
  3229. }
  3230. if( cmErrLastRC(&classPtr->err) != kOkDspRC )
  3231. cmLhFree(ctx->lhH,fmtArray);
  3232. return rc;
  3233. }
  3234. cmDspRC_t _cmDspSprintfGenString(cmDspCtx_t* ctx, cmDspSprintf_t* p )
  3235. {
  3236. cmDspRC_t rc = kOkDspRC;
  3237. unsigned fsi = 0; // format string index
  3238. unsigned i = 0; // fmtArray[] index
  3239. unsigned bi = 0; // string buffer index
  3240. unsigned bn = kSprintfBufCharCnt; // available char's in the string buffer
  3241. cmChar_t* fmt = p->fmtStr;
  3242. unsigned fn = strlen(fmt)+1;
  3243. // for each
  3244. for(i=0; i<p->inCnt && bn>0; ++i)
  3245. {
  3246. const cmDspSprintfFmt_t* f = p->fmtArray + i;
  3247. unsigned varId = kInSpId + i;
  3248. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(&p->inst, varId);
  3249. assert(varPtr != NULL);
  3250. // if there are literal char's to copy prior to the format
  3251. if( fsi < f->fsi )
  3252. {
  3253. unsigned cn = cmMin(f->fsi-fsi,bn);
  3254. strncpy(p->buf+bi,fmt+fsi,cn);
  3255. bn -= cn;
  3256. fsi += cn;
  3257. bi += cn;
  3258. }
  3259. if( bn == 0 )
  3260. {
  3261. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The internal string buffer is too small.");
  3262. break;
  3263. }
  3264. unsigned pn = 0;
  3265. char c = fmt[ f->fsi + f->fsn ];
  3266. // zero terminate the format string for this input
  3267. fmt[ f->fsi + f->fsn] = 0;
  3268. // if the conversion fmt is for a string then the kSymDsvFl will be set
  3269. // which will prevent the switch() from working - so clear the sym flag here.
  3270. unsigned flags = cmClrFlag(f->flags,kSymDsvFl);
  3271. switch(flags)
  3272. {
  3273. case kUCharDsvFl:
  3274. // not implemented - need to implment a uchar variable type or
  3275. // assume a one character strz.
  3276. assert(0);
  3277. break;
  3278. case kIntDsvFl:
  3279. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspInt(&p->inst,varId));
  3280. break;
  3281. case kUIntDsvFl:
  3282. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspUInt(&p->inst,varId));
  3283. break;
  3284. case kDoubleDsvFl:
  3285. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspDouble(&p->inst,varId));
  3286. break;
  3287. case kStrzDsvFl:
  3288. if( cmDspIsSymbol(&p->inst,varId) )
  3289. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDspSymbol(&p->inst,varId))));
  3290. else
  3291. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspStrcz(&p->inst,varId));
  3292. break;
  3293. default:
  3294. { assert(0); }
  3295. }
  3296. // restore the char written over by the termination zero
  3297. fmt[ f->fsi + f->fsn] = c;
  3298. assert(pn<=bn);
  3299. bn -= pn;
  3300. bi += pn;
  3301. fsi += f->fsn;
  3302. }
  3303. // if there is literal text in the format string after the last conversion spec.
  3304. if( fsi < fn )
  3305. {
  3306. unsigned cn = cmMin(fn-fsi,bn);
  3307. strncpy(p->buf+bi,fmt+fsi,cn);
  3308. }
  3309. return rc;
  3310. }
  3311. cmDspInst_t* _cmDspSprintfAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3312. {
  3313. cmDspVarArg_t args[] =
  3314. {
  3315. { "fmt", kFmtSpId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Format string" },
  3316. { "out", kOutSpId, 0, 0, kOutDsvFl | kStrzDsvFl, "Output string" },
  3317. { NULL, 0, 0, 0, 0 }
  3318. };
  3319. cmDspSprintf_t* p = NULL;
  3320. if( _cmSprintfLoadFormat(&p, ctx, classPtr, instSymId, id, storeSymId, args, va_cnt, vl ) == kOkDspRC )
  3321. return &p->inst;
  3322. return NULL;
  3323. }
  3324. cmDspRC_t _cmDspSprintfFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3325. {
  3326. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3327. cmLhFree(ctx->lhH,p->fmtArray);
  3328. cmLhFree(ctx->lhH,p->fmtStr);
  3329. p->fmtArray=NULL;
  3330. return kOkDspRC;
  3331. }
  3332. cmDspRC_t _cmDspSprintfReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3333. {
  3334. return kOkDspRC;
  3335. }
  3336. cmDspRC_t _cmDspSprintfRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3337. {
  3338. cmDspRC_t rc= kOkDspRC;
  3339. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3340. if( kInSpId <= evt->dstVarId && evt->dstVarId < kInSpId + p->inCnt )
  3341. {
  3342. cmDspSetEvent(ctx,inst,evt);
  3343. //if( evt->dstVarId == kInSpId )
  3344. if((rc = _cmDspSprintfGenString(ctx,p)) == kOkDspRC )
  3345. cmDspSetStrcz(ctx,inst,kOutSpId,p->buf);
  3346. }
  3347. return rc;
  3348. }
  3349. struct cmDspClass_str* cmSprintfClassCons( cmDspCtx_t* ctx )
  3350. {
  3351. cmDspClassSetup(&_cmSprintfDC,ctx,"Sprintf",
  3352. NULL,
  3353. _cmDspSprintfAlloc,
  3354. _cmDspSprintfFree,
  3355. _cmDspSprintfReset,
  3356. NULL,
  3357. _cmDspSprintfRecv,
  3358. NULL,NULL,
  3359. "String formatter." );
  3360. return &_cmSprintfDC;
  3361. }
  3362. //==========================================================================================================================================
  3363. enum
  3364. {
  3365. kOutAmId,
  3366. kBaseInAmId
  3367. };
  3368. cmDspClass_t _cmAMixDC;
  3369. typedef struct
  3370. {
  3371. cmDspInst_t inst;
  3372. unsigned inPortCnt;
  3373. unsigned baseGainId;
  3374. unsigned baseMuteId;
  3375. } cmDspAMix_t;
  3376. cmDspInst_t* _cmDspAMixAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3377. {
  3378. if( va_cnt < 1 )
  3379. {
  3380. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AMix' constructor must have a count of input ports.");
  3381. return NULL;
  3382. }
  3383. // args:
  3384. // <inPortCnt> <gain0>, <gain1> (the default gains are optional)
  3385. unsigned i;
  3386. int inPortCnt = va_arg(vl,int);
  3387. unsigned baseGainAmId = kBaseInAmId + inPortCnt;
  3388. unsigned baseMuteAmId = baseGainAmId + inPortCnt;
  3389. double dfltGain[ inPortCnt ];
  3390. if( va_cnt == 1 )
  3391. cmVOD_Fill(dfltGain,inPortCnt,1.0);
  3392. else
  3393. if( va_cnt == 2 )
  3394. {
  3395. dfltGain[0] = va_arg(vl,double);
  3396. cmVOD_Fill(dfltGain+1,inPortCnt-1,dfltGain[0]);
  3397. }
  3398. else
  3399. if( va_cnt == inPortCnt + 1 )
  3400. {
  3401. for(i=0; i<inPortCnt; ++i)
  3402. dfltGain[i] = va_arg(vl,double);
  3403. }
  3404. else
  3405. {
  3406. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The mix argument list must contain no default gain values, one default gain value, or all default gain values.");
  3407. return NULL;
  3408. }
  3409. cmDspAMix_t* p = cmDspInstAllocV(cmDspAMix_t,ctx,classPtr,instSymId,id,storeSymId,0,vl,
  3410. 1, "out", kOutAmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3411. inPortCnt, "in", kBaseInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3412. inPortCnt, "gain", baseGainAmId, 0, 0, kInDsvFl | kDoubleDsvFl, "Gain input",
  3413. inPortCnt, "mute", baseMuteAmId, 0, 0, kInDsvFl | kBoolDsvFl, "Mute input",
  3414. 0 );
  3415. p->inPortCnt = inPortCnt;
  3416. p->baseGainId = baseGainAmId;
  3417. p->baseMuteId = baseMuteAmId;
  3418. for(i=0; i<inPortCnt; ++i)
  3419. {
  3420. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dfltGain[i]);
  3421. cmDspSetDefaultBool( ctx, &p->inst, p->baseMuteId + i, false, false );
  3422. }
  3423. /*
  3424. // read any default gain settings
  3425. --va_cnt;
  3426. for(i=0; i<inPortCnt; ++i)
  3427. {
  3428. // if excplicit gains are not given then default to 1.0.
  3429. double dflt = 1.0;
  3430. if( i < va_cnt )
  3431. dflt = va_arg(vl,double);
  3432. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3433. }
  3434. */
  3435. return &p->inst;
  3436. }
  3437. cmDspRC_t _cmDspAMixReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3438. {
  3439. cmDspRC_t rc = kOkDspRC;
  3440. cmDspApplyAllDefaults(ctx,inst);
  3441. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3442. return rc;
  3443. }
  3444. cmDspRC_t _cmDspAMixExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3445. {
  3446. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3447. unsigned i;
  3448. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3449. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAmId,0);
  3450. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAmId,0);
  3451. for(i=0; i<p->inPortCnt; ++i)
  3452. {
  3453. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kBaseInAmId+i,0);
  3454. if( sp != NULL )
  3455. {
  3456. double gain = cmDspDouble(inst,p->baseGainId+i);
  3457. cmVOS_MultSumVVS(dp,n,sp,(cmSample_t)gain);
  3458. }
  3459. }
  3460. return kOkDspRC;
  3461. }
  3462. cmDspRC_t _cmDspAMixRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3463. {
  3464. cmDspRC_t rc = kOkDspRC;
  3465. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3466. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->inPortCnt )
  3467. {
  3468. cmDspSetEvent(ctx,inst,evt);
  3469. //printf("rcv:%i %f\n",evt->dstVarId,cmDspDouble(inst,evt->dstVarId));
  3470. }
  3471. return rc;
  3472. }
  3473. struct cmDspClass_str* cmAMixClassCons( cmDspCtx_t* ctx )
  3474. {
  3475. cmDspClassSetup(&_cmAMixDC,ctx,"AMix",
  3476. NULL,
  3477. _cmDspAMixAlloc,
  3478. NULL,
  3479. _cmDspAMixReset,
  3480. _cmDspAMixExec,
  3481. _cmDspAMixRecv,
  3482. NULL,NULL,
  3483. "Audio mixer");
  3484. return &_cmAMixDC;
  3485. }
  3486. //==========================================================================================================================================
  3487. enum
  3488. {
  3489. kInAsId,
  3490. kBaseOutAsId
  3491. };
  3492. cmDspClass_t _cmASplitDC;
  3493. typedef struct
  3494. {
  3495. cmDspInst_t inst;
  3496. unsigned outPortCnt;
  3497. unsigned baseGainId;
  3498. } cmDspASplit_t;
  3499. // A splitter has one audio input port and multiple audio output ports.
  3500. // A gain input is automatically provided for each output port.
  3501. cmDspInst_t* _cmDspASplitAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3502. {
  3503. cmDspVarArg_t args[] =
  3504. {
  3505. { "in", kInAsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  3506. { NULL, 0, 0, 0, 0 }
  3507. };
  3508. if( va_cnt < 1 )
  3509. {
  3510. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ASplit' constructor must have a count of input ports.");
  3511. return NULL;
  3512. }
  3513. // args:
  3514. // <outPortCnt> <gain0>, <gain1> (the default gains are optional)
  3515. unsigned i,j,k;
  3516. int outPortCnt = va_arg(vl,int);
  3517. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3518. unsigned argCnt = fixArgCnt + 2*outPortCnt + 1;
  3519. cmDspVarArg_t argArray[ argCnt ];
  3520. int labelCharCnt = 15;
  3521. cmChar_t label[ labelCharCnt + 1 ];
  3522. label[labelCharCnt] = 0;
  3523. //
  3524. for(i=0; i<fixArgCnt; ++i)
  3525. argArray[i] = args[i];
  3526. // define the audio output port specifications
  3527. for(j=0,k=0; j<outPortCnt; ++i,++j,++k)
  3528. {
  3529. snprintf(label,labelCharCnt,"out-%i",j);
  3530. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3531. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3532. argArray[i].constId = kBaseOutAsId + k;
  3533. argArray[i].rn = 0;
  3534. argArray[i].cn = 1;
  3535. argArray[i].flags = kOutDsvFl | kAudioBufDsvFl;
  3536. argArray[i].doc = "Audio Input";
  3537. }
  3538. // define the gain input specifications
  3539. for(j=0; j<outPortCnt; ++i,++j,++k)
  3540. {
  3541. snprintf(label,labelCharCnt,"gain-%i",j);
  3542. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3543. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3544. argArray[i].constId = kBaseOutAsId + k;
  3545. argArray[i].rn = 0;
  3546. argArray[i].cn = 0;
  3547. argArray[i].flags = kInDsvFl | kDoubleDsvFl;
  3548. argArray[i].doc = "Gain input";
  3549. }
  3550. // set the NULL end-of-arg-array sentinel
  3551. memset(argArray + i, 0, sizeof(argArray[0]));
  3552. cmDspASplit_t* p = cmDspInstAlloc(cmDspASplit_t,ctx,classPtr,argArray,instSymId,id,storeSymId,0,vl);
  3553. p->outPortCnt = outPortCnt;
  3554. p->baseGainId = kBaseOutAsId + outPortCnt;
  3555. // read any default gain settings
  3556. --va_cnt;
  3557. for(i=0; i<outPortCnt; ++i)
  3558. {
  3559. double dflt = 1.0;
  3560. if( i < va_cnt )
  3561. dflt = va_arg(vl,double);
  3562. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3563. }
  3564. return &p->inst;
  3565. }
  3566. cmDspRC_t _cmDspASplitReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3567. {
  3568. cmDspRC_t rc = kOkDspRC;
  3569. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3570. int i;
  3571. cmDspApplyAllDefaults(ctx,inst);
  3572. for(i=0; i<p->outPortCnt; ++i)
  3573. cmDspZeroAudioBuf(ctx,inst,kBaseOutAsId+i);
  3574. return rc;
  3575. }
  3576. cmDspRC_t _cmDspASplitExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3577. {
  3578. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3579. unsigned i;
  3580. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kBaseOutAsId,0);
  3581. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAsId,0);
  3582. for(i=0; i<p->outPortCnt; ++i)
  3583. {
  3584. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kBaseOutAsId+i,0);
  3585. double gain = cmDspDouble(inst,p->baseGainId+i);
  3586. cmVOS_MultVVS(dp,n,sp,(cmSample_t)gain);
  3587. }
  3588. return kOkDspRC;
  3589. }
  3590. cmDspRC_t _cmDspASplitRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3591. {
  3592. cmDspRC_t rc = kOkDspRC;
  3593. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3594. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->outPortCnt )
  3595. {
  3596. cmDspSetEvent(ctx,inst,evt);
  3597. }
  3598. return rc;
  3599. }
  3600. struct cmDspClass_str* cmASplitClassCons( cmDspCtx_t* ctx )
  3601. {
  3602. cmDspClassSetup(&_cmASplitDC,ctx,"ASplit",
  3603. NULL,
  3604. _cmDspASplitAlloc,
  3605. NULL,
  3606. _cmDspASplitReset,
  3607. _cmDspASplitExec,
  3608. _cmDspASplitRecv,
  3609. NULL,NULL,
  3610. "Audio splitter");
  3611. return &_cmASplitDC;
  3612. }
  3613. //==========================================================================================================================================
  3614. enum
  3615. {
  3616. kInAmId,
  3617. kMinAmId,
  3618. kMaxAmId,
  3619. kValAmId, // meter value
  3620. kLblAmId,
  3621. };
  3622. #define cmDspMeter_MIN (-100)
  3623. #define cmDspMeter_MAX (0)
  3624. cmDspClass_t _cmAMeterDC;
  3625. typedef struct
  3626. {
  3627. cmDspInst_t inst;
  3628. unsigned bufN;
  3629. unsigned idx;
  3630. cmReal_t sum;
  3631. cmReal_t val;
  3632. } cmDspAMeter_t;
  3633. cmDspInst_t* _cmDspAMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3634. {
  3635. cmDspVarArg_t args[] =
  3636. {
  3637. { "in", kInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3638. { "min", kMinAmId, 0, 0, kDoubleDsvFl, "Minimum value"},
  3639. { "max", kMaxAmId, 0, 0, kDoubleDsvFl, "Maximum value"},
  3640. { "val", kValAmId, 0, 0, kDoubleDsvFl, "Meter value"},
  3641. { "label",kLblAmId, 0, 0, kStrzDsvFl, "Label."},
  3642. { NULL, 0, 0, 0, 0 }
  3643. };
  3644. cmDspAMeter_t* p = cmDspInstAlloc(cmDspAMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3645. double updateMs = 100;
  3646. double sr = cmDspSampleRate(ctx);
  3647. unsigned spc = cmDspSamplesPerCycle(ctx);
  3648. p->bufN = cmMax(1,floor(updateMs * sr/ (1000.0 * spc)));
  3649. cmDspSetDefaultDouble(ctx, &p->inst, kValAmId, 0.0, cmDspMeter_MIN);
  3650. cmDspSetDefaultDouble(ctx, &p->inst, kMinAmId, 0.0, cmDspMeter_MIN);
  3651. cmDspSetDefaultDouble(ctx, &p->inst, kMaxAmId, 0.0, cmDspMeter_MAX);
  3652. // create the UI control
  3653. cmDspUiMeterCreate(ctx,&p->inst,kMinAmId,kMaxAmId,kValAmId,kLblAmId);
  3654. return &p->inst;
  3655. }
  3656. cmDspRC_t _cmDspAMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3657. {
  3658. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3659. cmDspApplyAllDefaults(ctx,inst);
  3660. //cmDspZeroAudioBuf(ctx,inst,kInAmId);
  3661. p->idx = 0;
  3662. p->sum = 0;
  3663. p->val = 0;
  3664. return kOkDspRC;
  3665. }
  3666. cmDspRC_t _cmDspAMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3667. {
  3668. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3669. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInAmId,0);
  3670. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAmId,0);
  3671. if( sp == NULL )
  3672. {
  3673. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  3674. return kOkDspRC;
  3675. }
  3676. p->sum += cmVOS_SquaredSum(sp,n);
  3677. ++p->idx;
  3678. if( p->idx == p->bufN )
  3679. {
  3680. cmReal_t coeff = 0.7;
  3681. cmReal_t rms = sqrt(p->sum/(n*p->bufN));
  3682. p->idx = 0;
  3683. p->sum = 0;
  3684. p->val = rms > p->val ? rms : (rms*(1.0-coeff)) + (p->val*coeff);
  3685. double db = cmMax(cmDspMeter_MIN,cmMin(cmDspMeter_MAX,20.0 * log10(p->val)));
  3686. cmDspSetDouble(ctx, inst, kValAmId, db);
  3687. }
  3688. return kOkDspRC;
  3689. }
  3690. struct cmDspClass_str* cmAMeterClassCons( cmDspCtx_t* ctx )
  3691. {
  3692. cmDspClassSetup(&_cmAMeterDC,ctx,"AMeter",
  3693. NULL,
  3694. _cmDspAMeterAlloc,
  3695. NULL,
  3696. _cmDspAMeterReset,
  3697. _cmDspAMeterExec,
  3698. NULL,
  3699. NULL,NULL,
  3700. "Audio meter display.");
  3701. return &_cmAMeterDC;
  3702. }
  3703. //==========================================================================================================================================
  3704. //
  3705. //
  3706. // Read files created by this object with the Octave function cmTextFile().
  3707. //
  3708. //
  3709. enum
  3710. {
  3711. kCntTfId,
  3712. kFnTfId,
  3713. kBaseTfId
  3714. };
  3715. cmDspClass_t _cmTextFileDC;
  3716. typedef struct
  3717. {
  3718. cmDspInst_t inst;
  3719. int inPortCnt;
  3720. cmFileH_t fH;
  3721. } cmDspTextFile_t;
  3722. cmDspInst_t* _cmDspTextFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3723. {
  3724. cmDspVarArg_t args[] =
  3725. {
  3726. { "cnt", kCntTfId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3727. { "fn", kFnTfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3728. { NULL, 0, 0, 0, 0 }
  3729. };
  3730. if( va_cnt < 1 )
  3731. {
  3732. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file object must be given a input port count argument.");
  3733. return NULL;
  3734. }
  3735. va_list vl1;
  3736. va_copy(vl1,vl);
  3737. int i,j;
  3738. int inPortCnt = va_arg(vl1,int);
  3739. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3740. unsigned argCnt = fixArgCnt + inPortCnt + 1;
  3741. cmDspVarArg_t argArray[ argCnt ];
  3742. if( inPortCnt <= 0 )
  3743. {
  3744. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file input port count must be a positive integer.");
  3745. return NULL;
  3746. }
  3747. //
  3748. for(i=0; i<fixArgCnt; ++i)
  3749. argArray[i] = args[i];
  3750. // define the input port specifications
  3751. for(j=0; j<inPortCnt; ++j,++i)
  3752. {
  3753. int labelCharCnt = 15;
  3754. cmChar_t label[ labelCharCnt + 1 ];
  3755. label[labelCharCnt] = 0;
  3756. snprintf(label,labelCharCnt,"in-%i",j);
  3757. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3758. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3759. argArray[i].constId = kBaseTfId + j;
  3760. argArray[i].rn = 0;
  3761. argArray[i].cn = 0;
  3762. argArray[i].flags = kInDsvFl | kDoubleDsvFl | kSymDsvFl;
  3763. argArray[i].doc = "Data input";
  3764. }
  3765. // set the NULL end-of-arg-array sentinel
  3766. memset(argArray + i, 0, sizeof(argArray[0]));
  3767. cmDspTextFile_t* p = cmDspInstAlloc(cmDspTextFile_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl);
  3768. p->inPortCnt = inPortCnt;
  3769. p->fH = cmFileNullHandle;
  3770. return &p->inst;
  3771. }
  3772. cmDspRC_t _cmDspTextFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3773. {
  3774. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3775. if( cmFileClose(&p->fH) )
  3776. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Text file close failed.");
  3777. return kOkDspRC;
  3778. }
  3779. cmDspRC_t _cmDspTextFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3780. {
  3781. cmDspRC_t rc = kOkDspRC;
  3782. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3783. cmDspApplyAllDefaults(ctx,inst);
  3784. const cmChar_t* fn = cmDspStrcz(inst,kFnTfId);
  3785. if( cmFileOpen( &p->fH, fn, kWriteFileFl, ctx->cmCtx->err.rpt ) != kOkFileRC )
  3786. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Text file open failed.");
  3787. return rc;
  3788. }
  3789. cmDspRC_t _cmDspTextFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3790. {
  3791. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3792. cmDspSetEvent(ctx,inst,evt);
  3793. if( cmFileIsValid(p->fH) && evt->dstVarId >= kBaseTfId )
  3794. {
  3795. double secs = ctx->ctx->begSmpIdx / cmDspSampleRate(ctx);
  3796. cmFilePrintf(p->fH,"%f %f %i ", secs, secs, evt->dstVarId-kBaseTfId);
  3797. if( cmIsFlag(evt->valuePtr->flags,kSymDsvFl) )
  3798. cmFilePrintf(p->fH,"%s\n", cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  3799. else
  3800. cmFilePrintf(p->fH,"%f\n", cmDspDouble(inst,evt->dstVarId));
  3801. }
  3802. return kOkDspRC;
  3803. }
  3804. struct cmDspClass_str* cmTextFileClassCons( cmDspCtx_t* ctx )
  3805. {
  3806. cmDspClassSetup(&_cmTextFileDC,ctx,"TextFile",
  3807. NULL,
  3808. _cmDspTextFileAlloc,
  3809. _cmDspTextFileFree,
  3810. _cmDspTextFileReset,
  3811. NULL,
  3812. _cmDspTextFileRecv,
  3813. NULL,NULL,
  3814. "Time tagged text file.");
  3815. return &_cmTextFileDC;
  3816. }
  3817. //==========================================================================================================================================
  3818. enum
  3819. {
  3820. kRsrcArId,
  3821. kCmdArId,
  3822. kIdxArId,
  3823. kValArId,
  3824. kCntArId,
  3825. kDoneArId,
  3826. kBaseOutArId
  3827. };
  3828. cmDspClass_t _cmArrayDC;
  3829. typedef struct
  3830. {
  3831. cmDspInst_t inst;
  3832. unsigned cnt;
  3833. cmReal_t* array;
  3834. unsigned printSymId;
  3835. unsigned sendSymId;
  3836. unsigned cntSymId;
  3837. unsigned doneSymId;
  3838. } cmDspArray_t;
  3839. cmDspInst_t* _cmDspArrayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3840. {
  3841. va_list vl1;
  3842. va_copy(vl1,vl);
  3843. if( va_cnt < 1 )
  3844. {
  3845. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object must be given a 'rsrc' parameter.");
  3846. return NULL;
  3847. }
  3848. const cmChar_t* rsrcStr = va_arg(vl,cmChar_t*);
  3849. unsigned outPortCnt = 0;
  3850. cmReal_t* array = NULL;
  3851. unsigned doneSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"done");
  3852. unsigned i;
  3853. if( rsrcStr == NULL )
  3854. {
  3855. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object 'rsrc' parameter appears to be missing.");
  3856. return NULL;
  3857. }
  3858. if( cmDspRsrcRealArray( ctx->dspH, &outPortCnt, &array, rsrcStr, NULL ) != kOkDspRC )
  3859. {
  3860. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array resource '%s' could not be read.",cmStringNullGuard(rsrcStr));
  3861. return NULL;
  3862. }
  3863. cmDspArray_t* p = cmDspInstAllocV(cmDspArray_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3864. 1, "rsrc", kRsrcArId, 0, 0, kStrzDsvFl | kInDsvFl | kReqArgDsvFl, "Array data resource label.",
  3865. 1, "cmd", kCmdArId, 0, 0, kSymDsvFl | kInDsvFl, "Command: send | print | count.",
  3866. 1, "idx", kIdxArId, 0, 0, kUIntDsvFl | kInDsvFl, "Send value at index out 'val' port.",
  3867. 1, "val", kValArId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Index output value.",
  3868. 1, "cnt", kCntArId, 0, 0, kUIntDsvFl | kOutDsvFl, "Count output value.",
  3869. 1, "done", kDoneArId, 0, 0, kSymDsvFl | kOutDsvFl, "'done' after last send.",
  3870. outPortCnt, "out", kBaseOutArId, 0, 0, kDoubleDsvFl | kOutDsvFl | kSendDfltDsvFl, "Individual real value outputs.",
  3871. 0 );
  3872. cmDspSetDefaultDouble( ctx, &p->inst, kValArId, 0, 0 );
  3873. cmDspSetDefaultUInt( ctx, &p->inst, kCntArId, 0, outPortCnt );
  3874. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneArId, doneSymId);
  3875. for(i=0; i<outPortCnt; ++i)
  3876. cmDspSetDefaultDouble( ctx, &p->inst, kBaseOutArId+i, 0, array[i] );
  3877. p->array = array;
  3878. p->cnt = outPortCnt;
  3879. p->sendSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"send");
  3880. p->printSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"print");
  3881. p->cntSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"count");
  3882. p->doneSymId = doneSymId;
  3883. return &p->inst;
  3884. }
  3885. cmDspRC_t _cmDspArrayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3886. {
  3887. cmDspArray_t* p = (cmDspArray_t*)inst;
  3888. cmDspRC_t rc;
  3889. // send 'out' values and then 'done' value
  3890. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3891. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3892. return rc;
  3893. }
  3894. cmDspRC_t _cmDspArrayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3895. {
  3896. cmDspRC_t rc;
  3897. cmDspArray_t* p = (cmDspArray_t*)inst;
  3898. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3899. return rc;
  3900. switch( evt->dstVarId )
  3901. {
  3902. case kCmdArId:
  3903. {
  3904. unsigned i;
  3905. unsigned cmdSymId = cmDsvSymbol(evt->valuePtr);
  3906. if( cmdSymId == p->printSymId )
  3907. {
  3908. for(i=0; i<p->cnt; ++i)
  3909. cmRptPrintf(ctx->rpt,"%f ",p->array[i]);
  3910. cmRptPrintf(ctx->rpt,"\n");
  3911. }
  3912. else
  3913. if( cmdSymId == p->sendSymId )
  3914. {
  3915. for(i=0; i<p->cnt; ++i)
  3916. cmDspSetDouble(ctx,inst,kBaseOutArId+i,p->array[i]);
  3917. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3918. }
  3919. else
  3920. if( cmdSymId == p->cntSymId )
  3921. {
  3922. cmDspSetUInt(ctx,inst,kCntArId,p->cnt);
  3923. }
  3924. }
  3925. break;
  3926. case kIdxArId:
  3927. {
  3928. unsigned idx = cmDsvUInt(evt->valuePtr);
  3929. if( idx < p->cnt )
  3930. cmDspSetDouble(ctx,inst,kValArId,p->array[idx]);
  3931. }
  3932. break;
  3933. }
  3934. return kOkDspRC;
  3935. }
  3936. struct cmDspClass_str* cmArrayClassCons( cmDspCtx_t* ctx )
  3937. {
  3938. cmDspClassSetup(&_cmArrayDC,ctx,"Array",
  3939. NULL,
  3940. _cmDspArrayAlloc,
  3941. NULL,
  3942. _cmDspArrayReset,
  3943. NULL,
  3944. _cmDspArrayRecv,
  3945. NULL,NULL,
  3946. "Time tagged text file.");
  3947. return &_cmArrayDC;
  3948. }
  3949. //==========================================================================================================================================
  3950. enum
  3951. {
  3952. kMidiPcId,
  3953. kHzPcId,
  3954. kOffsPcId,
  3955. kStrPcId,
  3956. kRatioPcId
  3957. };
  3958. cmDspClass_t _cmPitchCvtDC;
  3959. typedef struct
  3960. {
  3961. cmDspInst_t inst;
  3962. int midi;
  3963. double hz;
  3964. } cmDspPitchCvt_t;
  3965. cmDspInst_t* _cmDspPitchCvtAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3966. {
  3967. va_cnt = 0; // ignore an errant arguments
  3968. cmDspPitchCvt_t* p = cmDspInstAllocV(cmDspPitchCvt_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3969. 1, "midi", kMidiPcId, 0, 0, kUIntDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "MIDI pitch value input.",
  3970. 1, "hz", kHzPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "Hz pitch value.",
  3971. 1, "offs", kOffsPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kInDsvFl | kOptArgDsvFl, "Semitone offset.",
  3972. 1, "str", kStrPcId, 0, 0, kStrzDsvFl | kOutDsvFl, "Pitch string output.",
  3973. 1, "ratio", kRatioPcId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Offset as a ratio",
  3974. 0 );
  3975. cmDspSetDefaultUInt( ctx, &p->inst, kMidiPcId, 0, 0 );
  3976. cmDspSetDefaultDouble( ctx, &p->inst, kHzPcId, 0, 0.0 );
  3977. cmDspSetDefaultDouble( ctx, &p->inst, kOffsPcId, 0, 0 );
  3978. cmDspSetDefaultStrcz( ctx, &p->inst, kStrPcId, NULL, "" );
  3979. cmDspSetDefaultDouble( ctx, &p->inst, kRatioPcId, 0, 0 );
  3980. return &p->inst;
  3981. }
  3982. cmDspRC_t _cmDspPitchCvtReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3983. {
  3984. cmDspRC_t rc = cmDspApplyAllDefaults(ctx,inst);
  3985. return rc;
  3986. }
  3987. cmDspRC_t _cmDspPitchCvtOutput( cmDspCtx_t* ctx, cmDspPitchCvt_t* p )
  3988. {
  3989. cmDspInst_t* inst = &p->inst;
  3990. double offs = cmDspDouble( inst, kOffsPcId );
  3991. unsigned midi = cmMax(0, p->midi + rint(offs) );
  3992. double ratio= pow(2.0,offs/12.0);
  3993. double hz = p->hz * ratio;
  3994. //cmRptPrintf(ctx->rpt,"%i %i %f %f\n",offs,midi,hz,p->hz);
  3995. cmDspSetStrcz( ctx, inst, kStrPcId, cmMidiToSciPitch(midi,NULL,0));
  3996. cmDspSetUInt( ctx, inst, kMidiPcId, midi);
  3997. cmDspSetDouble( ctx, inst, kHzPcId, hz);
  3998. cmDspSetDouble( ctx, inst, kRatioPcId, ratio );
  3999. return kOkDspRC;
  4000. }
  4001. cmDspRC_t _cmDspPitchCvtRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4002. {
  4003. cmDspRC_t rc = kOkDspRC;
  4004. cmDspPitchCvt_t* p = (cmDspPitchCvt_t*)inst;
  4005. switch( evt->dstVarId )
  4006. {
  4007. case kMidiPcId:
  4008. p->midi = cmMax(0,cmDsvGetInt(evt->valuePtr));
  4009. p->hz = cmMidiToHz(p->midi);
  4010. rc = _cmDspPitchCvtOutput(ctx,p);
  4011. break;
  4012. case kHzPcId:
  4013. p->hz = cmDsvGetDouble(evt->valuePtr);
  4014. p->midi = cmHzToMidi(p->hz);
  4015. rc = _cmDspPitchCvtOutput(ctx,p);
  4016. break;
  4017. case kOffsPcId:
  4018. if((rc = cmDspSetEvent(ctx, inst, evt )) == kOkDspRC )
  4019. rc = _cmDspPitchCvtOutput(ctx,p);
  4020. break;
  4021. }
  4022. return rc;
  4023. }
  4024. struct cmDspClass_str* cmPitchCvtClassCons( cmDspCtx_t* ctx )
  4025. {
  4026. cmDspClassSetup(&_cmPitchCvtDC,ctx,"PitchCvt",
  4027. NULL,
  4028. _cmDspPitchCvtAlloc,
  4029. NULL,
  4030. _cmDspPitchCvtReset,
  4031. NULL,
  4032. _cmDspPitchCvtRecv,
  4033. NULL,NULL,
  4034. "Time tagged text file.");
  4035. return &_cmPitchCvtDC;
  4036. }
  4037. //==========================================================================================================================================
  4038. //
  4039. //
  4040. // Create a file which can be read by readBinFile.m
  4041. //
  4042. //
  4043. enum
  4044. {
  4045. kCntBmId,
  4046. kFnBmId,
  4047. kBaseBmId
  4048. };
  4049. cmDspClass_t _cmBinMtxFileDC;
  4050. typedef struct
  4051. {
  4052. cmDspInst_t inst;
  4053. int inPortCnt;
  4054. cmBinMtxFile_t* bmfp;
  4055. cmReal_t * valArray; // valArray[ inPortCnt ]
  4056. } cmDspBinMtxFile_t;
  4057. cmDspInst_t* _cmDspBinMtxFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4058. {
  4059. cmDspVarArg_t args[] =
  4060. {
  4061. { "cnt", kCntBmId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  4062. { "fn", kFnBmId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  4063. };
  4064. if( va_cnt < 1 )
  4065. {
  4066. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file object must be given a input port count argument.");
  4067. return NULL;
  4068. }
  4069. va_list vl1;
  4070. va_copy(vl1,vl);
  4071. int inPortCnt = va_arg(vl,int);
  4072. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  4073. unsigned argCnt = fixArgCnt + inPortCnt;
  4074. cmDspVarArg_t a[ argCnt+1 ];
  4075. cmDspBinMtxFile_t* p = NULL;
  4076. if( inPortCnt <= 0 )
  4077. {
  4078. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file must be a positive integer.");
  4079. return NULL;
  4080. }
  4081. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  4082. cmDspArgSetupN(ctx, a, argCnt, kBaseBmId, inPortCnt, "in", kBaseBmId, 0, 0, kInDsvFl | kDoubleDsvFl, "input ports");
  4083. cmDspArgSetupNull( a+argCnt );
  4084. if((p = cmDspInstAlloc(cmDspBinMtxFile_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  4085. return NULL;
  4086. p->bmfp = cmBinMtxFileAlloc(ctx->cmProcCtx, NULL, NULL );
  4087. p->inPortCnt = inPortCnt;
  4088. p->valArray = cmMemAllocZ(cmReal_t,inPortCnt);
  4089. return &p->inst;
  4090. }
  4091. cmDspRC_t _cmDspBinMtxFileOpen( cmDspCtx_t* ctx, cmDspInst_t* inst )
  4092. {
  4093. cmDspRC_t rc = kOkDspRC;
  4094. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  4095. if( p->bmfp != NULL )
  4096. {
  4097. // finalize the current file
  4098. if( cmBinMtxFileFinal(p->bmfp) != cmOkRC )
  4099. cmDspInstErr(ctx,inst,kFileCloseFailDspRC,"File close failed.");
  4100. // open a new one
  4101. if( cmBinMtxFileInit( p->bmfp, cmDspDefaultStrcz(&p->inst,kFnBmId) ) != cmOkRC)
  4102. rc = cmDspInstErr(ctx,inst,kFileOpenFailDspRC,"File open failed.");
  4103. }
  4104. return rc;
  4105. }
  4106. cmDspRC_t _cmDspBinMtxFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4107. {
  4108. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  4109. cmBinMtxFileFree(&p->bmfp);
  4110. cmMemFree(p->valArray);
  4111. return kOkDspRC;
  4112. }
  4113. cmDspRC_t _cmDspBinMtxFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4114. {
  4115. cmDspApplyAllDefaults(ctx,inst);
  4116. return _cmDspBinMtxFileOpen(ctx,inst);
  4117. }
  4118. cmDspRC_t _cmDspBinMtxFileExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4119. {
  4120. cmDspRC_t rc = kOkDspRC;
  4121. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  4122. // write the file
  4123. if( cmBinMtxFileIsValid( p->bmfp ) )
  4124. if( cmBinMtxFileExecR(p->bmfp, p->valArray, p->inPortCnt ) != cmOkRC )
  4125. return cmDspInstErr(ctx,inst,kFileWriteFailDspRC,"File write failure.");
  4126. return rc;
  4127. }
  4128. cmDspRC_t _cmDspBinMtxFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4129. {
  4130. cmDspRC_t rc = kOkDspRC;
  4131. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  4132. cmDspSetEvent(ctx,inst,evt);
  4133. // new file name - create new output file
  4134. if( evt->dstVarId == kFnBmId )
  4135. {
  4136. rc = _cmDspBinMtxFileOpen(ctx,inst);
  4137. }
  4138. else
  4139. // new value - store in p->valArray[]
  4140. if( kBaseBmId <= evt->dstVarId && evt->dstVarId < kBaseBmId + p->inPortCnt )
  4141. {
  4142. p->valArray[ evt->dstVarId - kBaseBmId ] = cmDspDouble(inst,evt->dstVarId );
  4143. }
  4144. else
  4145. { assert(0); }
  4146. return rc;
  4147. }
  4148. struct cmDspClass_str* cmBinMtxFileClassCons( cmDspCtx_t* ctx )
  4149. {
  4150. cmDspClassSetup(&_cmBinMtxFileDC,ctx,"BinMtxFile",
  4151. NULL,
  4152. _cmDspBinMtxFileAlloc,
  4153. _cmDspBinMtxFileFree,
  4154. _cmDspBinMtxFileReset,
  4155. _cmDspBinMtxFileExec,
  4156. _cmDspBinMtxFileRecv,
  4157. NULL,NULL,
  4158. "Time tagged text file.");
  4159. return &_cmBinMtxFileDC;
  4160. }
  4161. //==========================================================================================================================================
  4162. enum
  4163. {
  4164. kHopMsSbId,
  4165. kWndFactSbId,
  4166. kInSbId,
  4167. kOutSbId
  4168. };
  4169. cmDspClass_t _cmShiftBufDC;
  4170. typedef struct
  4171. {
  4172. cmDspInst_t inst;
  4173. cmShiftBuf* sbp;
  4174. } cmDspShiftBuf_t;
  4175. void _cmDspShiftBufSetup( cmDspCtx_t* ctx, cmDspShiftBuf_t* p )
  4176. {
  4177. double hopMs = cmDspDouble(&p->inst,kHopMsSbId);
  4178. unsigned hopSmpCnt = lround(cmDspSampleRate(ctx) * hopMs / 1000.0 );
  4179. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndFactSbId) * hopSmpCnt;
  4180. if( p->sbp == NULL || hopSmpCnt != p->sbp->hopSmpCnt || wndSmpCnt != p->sbp->wndSmpCnt )
  4181. {
  4182. cmShiftBufFree(&p->sbp);
  4183. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx, NULL, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt );
  4184. }
  4185. }
  4186. cmDspInst_t* _cmDspShiftBufAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4187. {
  4188. cmDspVarArg_t args[] =
  4189. {
  4190. { "hopMs", kHopMsSbId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Hop size on milliseconds"},
  4191. { "wndFact", kWndFactSbId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Count of hops contained in each output buffer."},
  4192. { "in", kInSbId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  4193. { "out", kOutSbId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio output"},
  4194. { NULL, 0, 0, 0, 0 }
  4195. };
  4196. // Note: by setting the column count of the output audio variable to zero
  4197. // we prevent it from being automatically assigned vector memory.
  4198. cmDspShiftBuf_t* p = cmDspInstAlloc(cmDspShiftBuf_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  4199. return &p->inst;
  4200. }
  4201. cmDspRC_t _cmDspShiftBufFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4202. {
  4203. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4204. cmShiftBufFree(&p->sbp);
  4205. return kOkDspRC;
  4206. }
  4207. cmDspRC_t _cmDspShiftBufReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4208. {
  4209. cmDspRC_t rc = kOkDspRC;
  4210. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4211. cmDspApplyAllDefaults(ctx,inst);
  4212. _cmDspShiftBufSetup(ctx,p);
  4213. return rc;
  4214. }
  4215. cmDspRC_t _cmDspShiftBufRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4216. {
  4217. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4218. cmDspSetEvent(ctx,inst,evt);
  4219. switch(evt->dstVarId)
  4220. {
  4221. case kHopMsSbId:
  4222. case kWndFactSbId:
  4223. _cmDspShiftBufSetup( ctx, p );
  4224. break;
  4225. };
  4226. return kOkDspRC;
  4227. }
  4228. cmDspRC_t _cmDspShiftBufExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4229. {
  4230. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4231. unsigned sn = cmDspAudioBufSmpCount(ctx,inst,kInSbId,0);
  4232. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInSbId,0);
  4233. cmDspVar_t* varPtr = cmDspVarIdToPtr(inst,kOutSbId);
  4234. if( cmShiftBufExec(p->sbp, sp, sn ) )
  4235. cmDsvSetSampleMtx( &varPtr->value, p->sbp->outV, p->sbp->outN, 1);
  4236. else
  4237. cmDsvSetSampleMtx( &varPtr->value, NULL, 0, 0);
  4238. return kOkDspRC;
  4239. }
  4240. struct cmDspClass_str* cmShiftBufClassCons( cmDspCtx_t* ctx )
  4241. {
  4242. cmDspClassSetup(&_cmShiftBufDC,ctx,"ShiftBuf",
  4243. NULL,
  4244. _cmDspShiftBufAlloc,
  4245. _cmDspShiftBufFree,
  4246. _cmDspShiftBufReset,
  4247. _cmDspShiftBufExec,
  4248. _cmDspShiftBufRecv,
  4249. NULL,NULL,
  4250. "Time tagged text file.");
  4251. return &_cmShiftBufDC;
  4252. }
  4253. //==========================================================================================================================================
  4254. enum
  4255. {
  4256. kInNsId
  4257. };
  4258. cmDspClass_t _cmNetSendDC;
  4259. typedef struct
  4260. {
  4261. cmDspInst_t inst;
  4262. _cmDspSrcConn_t* srcConnPtr;
  4263. } cmDspNetSend_t;
  4264. cmDspInst_t* _cmDspNetSendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4265. {
  4266. cmDspVarArg_t args[] =
  4267. {
  4268. { "in", kInNsId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  4269. { NULL, 0, 0, 0, 0 }
  4270. };
  4271. assert( va_cnt == 1 );
  4272. _cmDspSrcConn_t* srcConnPtr = va_arg(vl,_cmDspSrcConn_t*);
  4273. assert( srcConnPtr != NULL );
  4274. cmDspNetSend_t* p = cmDspInstAlloc(cmDspNetSend_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl);
  4275. p->srcConnPtr = srcConnPtr;
  4276. return &p->inst;
  4277. }
  4278. cmDspRC_t _cmDspNetSendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4279. {
  4280. return kOkDspRC;
  4281. }
  4282. cmDspRC_t _cmDspNetSendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4283. {
  4284. cmDspNetSend_t* p = (cmDspNetSend_t*)inst;
  4285. return _cmDspSysNetSendEvent(ctx->dspH, p->srcConnPtr->dstNetNodeId, p->srcConnPtr->dstId, evt );
  4286. }
  4287. struct cmDspClass_str* cmNetSendClassCons( cmDspCtx_t* ctx )
  4288. {
  4289. cmDspClassSetup(&_cmNetSendDC,ctx,"NetSend",
  4290. NULL,
  4291. _cmDspNetSendAlloc,
  4292. NULL,
  4293. _cmDspNetSendReset,
  4294. NULL,
  4295. _cmDspNetSendRecv,
  4296. NULL,NULL,
  4297. "Print the value of any event arriving at 'in'.");
  4298. return &_cmNetSendDC;
  4299. }
  4300. //==========================================================================================================================================
  4301. enum
  4302. {
  4303. kBaseInPtsId,
  4304. };
  4305. cmDspClass_t _cmRsrcWrDC;
  4306. typedef struct
  4307. {
  4308. cmDspInst_t inst;
  4309. char** pathArray;
  4310. unsigned pathCnt;
  4311. } cmDspRsrcWr_t;
  4312. cmDspInst_t* _cmDspRsrcWrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4313. {
  4314. va_list vl1;
  4315. va_copy(vl1,vl);
  4316. if( va_cnt < 1 )
  4317. {
  4318. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RsrcWr' constructor argument list must contain at least one resource path specificiation.");
  4319. return NULL;
  4320. }
  4321. unsigned pathCnt = va_cnt;
  4322. unsigned argCnt = pathCnt;
  4323. cmDspVarArg_t args[argCnt+1];
  4324. char** pathArray = cmMemAllocZ(char*,pathCnt);
  4325. unsigned i;
  4326. for(i=0; i<pathCnt; ++i)
  4327. {
  4328. // get the path
  4329. const cmChar_t* pathLabel = va_arg(vl,const char*);
  4330. assert( pathLabel != NULL );
  4331. // store the path name
  4332. pathArray[i] = cmMemAllocStr(pathLabel);
  4333. cmDspArgSetup(ctx, args+kBaseInPtsId+i, pathLabel, cmInvalidId, kBaseInPtsId+i, 0, 0, kInDsvFl | kTypeDsvMask, cmTsPrintfH(ctx->lhH,"%s Input.",pathLabel) );
  4334. }
  4335. cmDspArgSetupNull(args + argCnt);
  4336. cmDspRsrcWr_t* p = cmDspInstAlloc(cmDspRsrcWr_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl1);
  4337. p->pathCnt = pathCnt;
  4338. p->pathArray = pathArray;
  4339. return &p->inst;
  4340. }
  4341. cmDspRC_t _cmDspRsrcWrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4342. {
  4343. cmDspRsrcWr_t* p = (cmDspRsrcWr_t*)inst;
  4344. int i;
  4345. for(i=0; i<p->pathCnt; ++i)
  4346. cmMemFree(p->pathArray[i]);
  4347. cmMemFree(p->pathArray);
  4348. return kOkDspRC;
  4349. }
  4350. cmDspRC_t _cmDspRsrcWrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4351. {
  4352. return kOkDspRC;
  4353. }
  4354. cmDspRC_t _cmDspRsrcWrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4355. {
  4356. cmDspRsrcWr_t* p = (cmDspRsrcWr_t*)inst;
  4357. cmDspRC_t rc = kOkDspRC;
  4358. // if a msg of any type is recieved on an input port - send out the associated symbol
  4359. if( kBaseInPtsId <= evt->dstVarId && evt->dstVarId < kBaseInPtsId + p->pathCnt )
  4360. {
  4361. unsigned idx = evt->dstVarId - kBaseInPtsId;
  4362. assert( idx < p->pathCnt );
  4363. if( cmDsvIsStrz(evt->valuePtr) )
  4364. {
  4365. rc = cmDspRsrcWriteString( ctx->dspH, cmDsvStrz(evt->valuePtr), p->pathArray[idx], NULL );
  4366. }
  4367. }
  4368. return rc;
  4369. }
  4370. struct cmDspClass_str* cmRsrcWrClassCons( cmDspCtx_t* ctx )
  4371. {
  4372. cmDspClassSetup(&_cmRsrcWrDC,ctx,"RsrcWr",
  4373. NULL,
  4374. _cmDspRsrcWrAlloc,
  4375. _cmDspRsrcWrFree,
  4376. _cmDspRsrcWrReset,
  4377. NULL,
  4378. _cmDspRsrcWrRecv,
  4379. NULL,
  4380. NULL,
  4381. "Set the value of a resource variable.");
  4382. return &_cmRsrcWrDC;
  4383. }
  4384. //==========================================================================================================================================
  4385. //==========================================================================================================================================
  4386. cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
  4387. {
  4388. cmPrinterClassCons,
  4389. cmCounterClassCons,
  4390. cmPhasorClassCons,
  4391. cmMidiOutClassCons,
  4392. cmMidiInClassCons,
  4393. cmAudioInClassCons,
  4394. cmAudioOutClassCons,
  4395. cmAudioFileOutClassCons,
  4396. cmSigGenClassCons,
  4397. cmScalarClassCons,
  4398. cmTextClassCons,
  4399. cmMeterClassCons,
  4400. cmLabelClassCons,
  4401. cmButtonClassCons,
  4402. cmCheckboxClassCons,
  4403. cmReorderClassCons,
  4404. cmFnameClassCons,
  4405. cmMsgListClassCons,
  4406. cmWaveTableClassCons,
  4407. cmSprintfClassCons,
  4408. cmAMixClassCons,
  4409. cmASplitClassCons,
  4410. cmAMeterClassCons,
  4411. cmTextFileClassCons,
  4412. cmBinMtxFileClassCons,
  4413. cmArrayClassCons,
  4414. cmPitchCvtClassCons,
  4415. cmShiftBufClassCons,
  4416. cmNetSendClassCons,
  4417. cmRsrcWrClassCons,
  4418. cmDelayClassCons,
  4419. cmPShiftClassCons,
  4420. cmLoopRecdClassCons,
  4421. cmRectifyClassCons,
  4422. cmGateDetectClassCons,
  4423. cmAutoGainClassCons,
  4424. cmEnvFollowClassCons,
  4425. cmXfaderClassCons,
  4426. cmChCfgClassCons,
  4427. cmChordDetectClassCons,
  4428. cmFaderClassCons,
  4429. cmNoteSelectClassCons,
  4430. cmNetNoteSelectClassCons,
  4431. cmCombFiltClassCons,
  4432. cmScalarOpClassCons,
  4433. cmGroupSelClassCons,
  4434. cmAudioNofMClassCons,
  4435. cmRingModClassCons,
  4436. cmMsgDelayClassCons,
  4437. cmLineClassCons,
  4438. cmAdsrClassCons,
  4439. cmCompressorClassCons,
  4440. cmBiQuadEqClassCons,
  4441. cmDistDsClassCons,
  4442. cmDbToLinClassCons,
  4443. cmMtDelayClassCons,
  4444. cmNofMClassCons,
  4445. cm1ofNClassCons,
  4446. cm1UpClassCons,
  4447. cmGateToSymClassCons,
  4448. cmPortToSymClassCons,
  4449. cmRouterClassCons,
  4450. cmAvailChClassCons,
  4451. cmPresetClassCons,
  4452. cmBcastSymClassCons,
  4453. cmSegLineClassCons,
  4454. cmKrClassCons,
  4455. cmTimeLineClassCons,
  4456. cmScoreClassCons,
  4457. cmMidiFilePlayClassCons,
  4458. cmScFolClassCons,
  4459. cmScModClassCons,
  4460. cmGSwitchClassCons,
  4461. cmScaleRangeClassCons,
  4462. cmActiveMeasClassCons,
  4463. cmAmSyncClassCons,
  4464. cmNanoMapClassCons,
  4465. cmRecdPlayClassCons,
  4466. cmGoertzelClassCons,
  4467. cmSyncRecdClassCons,
  4468. NULL,
  4469. };
  4470. cmDspClassConsFunc_t cmDspClassGetBuiltIn( unsigned index )
  4471. {
  4472. unsigned n = sizeof(_cmDspClassBuiltInArray)/sizeof(cmDspClass_t*);
  4473. if( index >= n )
  4474. return NULL;
  4475. return _cmDspClassBuiltInArray[index];
  4476. }