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

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