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

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