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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmText.h"
  12. #include "cmMath.h"
  13. #include "cmFile.h"
  14. #include "cmFileSys.h"
  15. #include "cmSymTbl.h"
  16. #include "cmJson.h"
  17. #include "cmPrefs.h"
  18. #include "cmDspValue.h"
  19. #include "cmMsgProtocol.h"
  20. #include "cmThread.h"
  21. #include "cmUdpPort.h"
  22. #include "cmUdpNet.h"
  23. #include "cmAudioSys.h"
  24. #include "cmProcObj.h"
  25. #include "cmDspCtx.h"
  26. #include "cmDspClass.h"
  27. #include "cmDspUi.h"
  28. #include "cmAudioFile.h"
  29. #include "cmProcObj.h"
  30. #include "cmProcTemplateMain.h"
  31. #include "cmProc.h"
  32. #include "cmMidi.h"
  33. #include "cmProc2.h"
  34. #include "cmProc3.h"
  35. #include "cmVectOpsTemplateMain.h"
  36. #include "app/cmPickup.h"
  37. #include "cmDspSys.h"
  38. //==========================================================================================================================================
  39. enum
  40. {
  41. kBypassDyId,
  42. kTimeDyId,
  43. kFbDyId,
  44. kInDyId,
  45. kOutDyId
  46. };
  47. cmDspClass_t _cmDelayDC;
  48. typedef struct
  49. {
  50. cmDspInst_t inst;
  51. cmMDelay* delay;
  52. } cmDspDelay_t;
  53. cmDspInst_t* _cmDspDelayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  54. {
  55. unsigned chs = 1;
  56. cmDspVarArg_t args[] =
  57. {
  58. { "bypass",kBypassDyId,0,0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  59. { "time", kTimeDyId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Max delay time in milliseconds" },
  60. { "fb", kFbDyId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Feedback" },
  61. { "in", kInDyId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  62. { "out", kOutDyId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  63. { NULL, 0, 0, 0, 0 }
  64. };
  65. cmDspDelay_t* p = cmDspInstAlloc(cmDspDelay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  66. // set default values for the parameters that were not explicitely set in the va_arg list
  67. cmDspSetDefaultBool( ctx, &p->inst, kBypassDyId,0, 0 );
  68. cmDspSetDefaultUInt( ctx, &p->inst, kTimeDyId, 0, 1000 );
  69. cmDspSetDefaultDouble( ctx, &p->inst ,kFbDyId, 0.0, 0.0 );
  70. cmReal_t dtimeMs = cmDspDefaultUInt(&p->inst,kTimeDyId);
  71. cmReal_t fbCoeff = cmDspDefaultDouble(&p->inst,kFbDyId);
  72. p->delay = cmMDelayAlloc(ctx->cmProcCtx,NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), fbCoeff, 1, &dtimeMs, NULL );
  73. return &p->inst;
  74. }
  75. cmDspRC_t _cmDspDelayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  76. {
  77. cmDspRC_t rc = kOkDspRC;
  78. cmDspDelay_t* p = (cmDspDelay_t*)inst;
  79. cmMDelayFree(&p->delay);
  80. //cmCtxFree(&p->ctx);
  81. return rc;
  82. }
  83. cmDspRC_t _cmDspDelayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  84. {
  85. cmDspRC_t rc = kOkDspRC;
  86. rc = cmDspApplyAllDefaults(ctx,inst);
  87. return rc;
  88. }
  89. cmDspRC_t _cmDspDelayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  90. {
  91. cmDspDelay_t* p = (cmDspDelay_t*)inst;
  92. cmDspRC_t rc = kOkDspRC;
  93. unsigned iChIdx = 0;
  94. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInDyId,iChIdx);
  95. unsigned iSmpCnt = cmDspVarRows(inst,kInDyId);
  96. unsigned oChIdx = 0;
  97. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutDyId,oChIdx);
  98. unsigned oSmpCnt = cmDspVarRows(inst,kOutDyId);
  99. bool bypassFl= cmDspBool(inst,kBypassDyId);
  100. cmMDelayExec(p->delay,ip,op,cmMin(iSmpCnt,oSmpCnt),bypassFl);
  101. return rc;
  102. }
  103. cmDspRC_t _cmDspDelayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  104. {
  105. cmDspDelay_t* p = (cmDspDelay_t*)inst;
  106. cmDspRC_t rc= kOkDspRC;
  107. cmDspSetEvent(ctx,inst,evt);
  108. switch( evt->dstVarId )
  109. {
  110. case kBypassDyId:
  111. break;
  112. case kTimeDyId:
  113. p->delay->delayArray[0].delayMs = cmDspDouble(inst,kTimeDyId);
  114. break;
  115. case kFbDyId:
  116. p->delay->fbCoeff = cmDspDouble(inst,kFbDyId);
  117. break;
  118. default:
  119. { assert(0); }
  120. }
  121. return rc;
  122. }
  123. struct cmDspClass_str* cmDelayClassCons( cmDspCtx_t* ctx )
  124. {
  125. cmDspClassSetup(&_cmDelayDC,ctx,"Delay",
  126. NULL,
  127. _cmDspDelayAlloc,
  128. _cmDspDelayFree,
  129. _cmDspDelayReset,
  130. _cmDspDelayExec,
  131. _cmDspDelayRecv,
  132. NULL,NULL,
  133. "Simple delay.");
  134. return &_cmDelayDC;
  135. }
  136. //==========================================================================================================================================
  137. enum
  138. {
  139. kBypassMtId,
  140. kScaleMtId,
  141. kFbMtId,
  142. kInMtId,
  143. kOutMtId,
  144. kBaseMsMtId
  145. };
  146. cmDspClass_t _cmMtDelayDC;
  147. typedef struct
  148. {
  149. cmDspInst_t inst;
  150. cmMDelay* p;
  151. unsigned baseGainMtId;
  152. unsigned tapCnt;
  153. cmReal_t* msV;
  154. cmReal_t* gainV;
  155. unsigned printSymId;
  156. } cmDspMtDelay_t;
  157. // args: bypassFl, time_scale, feedback, tap_ms0, tap_gain0, tapms1, tap_gain1, ....
  158. cmDspInst_t* _cmDspMtDelayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  159. {
  160. va_list vl1;
  161. va_copy(vl1,vl);
  162. cmDspVarArg_t args[] =
  163. {
  164. { "bypass",kBypassMtId,0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable flag." },
  165. { "scale", kScaleMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Scale tap times. (0.0 - 1.0)" },
  166. { "fb", kFbMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Feedback" },
  167. { "in", kInMtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  168. { "out", kOutMtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  169. };
  170. // verify that at least one var arg exists
  171. if( va_cnt < 5 || cmIsEvenU(va_cnt) )
  172. {
  173. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The 'multi-tap delay requires at least 5 arguments. Three fixed arguments and groups of two tap specification arguments.");
  174. return NULL;
  175. }
  176. unsigned reqArgCnt = 3;
  177. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  178. unsigned tapCnt = (va_cnt - reqArgCnt)/2;
  179. cmReal_t* msV = cmMemAllocZ(cmReal_t,tapCnt);
  180. cmReal_t* gainV = cmMemAllocZ(cmReal_t,tapCnt);
  181. unsigned argCnt = fixArgCnt + 2 * tapCnt;
  182. unsigned baseGainMtId = kBaseMsMtId + tapCnt;
  183. cmDspVarArg_t a[ argCnt+1 ];
  184. unsigned i;
  185. // Get the taps and gains
  186. va_arg(vl1,int); // enable
  187. va_arg(vl1,double); // time scale
  188. va_arg(vl1,double); // feedback
  189. for(i=0; i<tapCnt; ++i)
  190. {
  191. msV[i] = va_arg(vl1,double);
  192. gainV[i] = va_arg(vl1,double);
  193. }
  194. // setup the output gain args
  195. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  196. cmDspArgSetupN(ctx, a, argCnt, kBaseMsMtId, tapCnt, "ms", kBaseMsMtId, 0, 0, kInDsvFl | kDoubleDsvFl, "Tap delay times in milliseconds.");
  197. cmDspArgSetupN(ctx, a, argCnt, baseGainMtId, tapCnt, "gain", baseGainMtId, 0, 0, kInDsvFl | kDoubleDsvFl, "Tap delay linear gain.");
  198. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  199. cmDspMtDelay_t* p = cmDspInstAlloc(cmDspMtDelay_t,ctx,classPtr,a,instSymId,id,storeSymId,reqArgCnt,vl1);
  200. p->p = cmMDelayAlloc(ctx->cmProcCtx,NULL,0, 0, 0, 0, NULL, NULL );
  201. p->baseGainMtId = baseGainMtId;
  202. p->tapCnt = tapCnt;
  203. p->msV = msV;
  204. p->gainV = gainV;
  205. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_print");
  206. for(i=0; i<tapCnt; ++i)
  207. {
  208. cmDspSetDefaultDouble(ctx,&p->inst,kBaseMsMtId+i, 0.0, msV[i]);
  209. cmDspSetDefaultDouble(ctx,&p->inst,baseGainMtId+i, 0.0, gainV[i]);
  210. }
  211. return &p->inst;
  212. }
  213. cmDspRC_t _cmDspMtDelayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  214. {
  215. cmDspRC_t rc = kOkDspRC;
  216. cmDspMtDelay_t* p = (cmDspMtDelay_t*)inst;
  217. cmMemFree(p->msV);
  218. cmMemFree(p->gainV);
  219. cmMDelayFree(&p->p);
  220. return rc;
  221. }
  222. cmDspRC_t _cmDspMtDelayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  223. {
  224. cmDspRC_t rc = kOkDspRC;
  225. cmDspMtDelay_t* p = (cmDspMtDelay_t*)inst;
  226. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  227. {
  228. cmMDelayInit(p->p,cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), cmDspDouble(&p->inst,kFbMtId), p->tapCnt, p->msV, p->gainV );
  229. }
  230. return rc;
  231. }
  232. cmDspRC_t _cmDspMtDelayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  233. {
  234. cmDspMtDelay_t* p = (cmDspMtDelay_t*)inst;
  235. cmDspRC_t rc = kOkDspRC;
  236. unsigned iChIdx = 0;
  237. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInMtId,iChIdx);
  238. unsigned iSmpCnt = cmDspVarRows(inst,kInMtId);
  239. unsigned oChIdx = 0;
  240. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutMtId,oChIdx);
  241. unsigned oSmpCnt = cmDspVarRows(inst,kOutMtId);
  242. bool bypassFl= cmDspBool(inst,kBypassMtId);
  243. if( ip != NULL )
  244. cmMDelayExec(p->p,ip,op,cmMin(iSmpCnt,oSmpCnt),bypassFl);
  245. return rc;
  246. }
  247. cmDspRC_t _cmDspMtDelayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  248. {
  249. cmDspMtDelay_t* p = (cmDspMtDelay_t*)inst;
  250. cmDspRC_t rc= kOkDspRC;
  251. cmDspSetEvent(ctx,inst,evt);
  252. // set tap times
  253. if( kBaseMsMtId <= evt->dstVarId && evt->dstVarId < kBaseMsMtId + p->p->delayCnt )
  254. cmMDelaySetTapMs(p->p,evt->dstVarId - kBaseMsMtId, cmDspDouble(inst,evt->dstVarId));
  255. else
  256. // set tap gains
  257. if( p->baseGainMtId <= evt->dstVarId && evt->dstVarId < p->baseGainMtId + p->p->delayCnt )
  258. cmMDelaySetTapGain(p->p,evt->dstVarId - p->baseGainMtId, cmDspDouble(inst,evt->dstVarId));
  259. else
  260. {
  261. switch( evt->dstVarId )
  262. {
  263. case kScaleMtId:
  264. //cmDspDouble(inst,kScaleMtId);
  265. break;
  266. case kFbMtId:
  267. p->p->fbCoeff = cmDspDouble(inst,kFbMtId);
  268. break;
  269. }
  270. }
  271. return rc;
  272. }
  273. cmDspRC_t _cmDspMtDelayRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
  274. {
  275. cmDspRC_t rc = kOkDspRC;
  276. cmDspMtDelay_t* p = (cmDspMtDelay_t*)inst;
  277. if( cmDsvIsSymbol(value) && (cmDsvSymbol(value)==p->printSymId) )
  278. {
  279. int i;
  280. cmRptPrintf(ctx->rpt,"taps:%i\n",p->tapCnt);
  281. for(i=0; i<p->tapCnt; ++i)
  282. cmRptPrintf(ctx->rpt,"%f %f\n",p->msV[i],p->gainV[i]);
  283. cmMDelayReport(p->p, ctx->rpt );
  284. }
  285. return rc;
  286. }
  287. struct cmDspClass_str* cmMtDelayClassCons( cmDspCtx_t* ctx )
  288. {
  289. cmDspClassSetup(&_cmMtDelayDC,ctx,"MtDelay",
  290. NULL,
  291. _cmDspMtDelayAlloc,
  292. _cmDspMtDelayFree,
  293. _cmDspMtDelayReset,
  294. _cmDspMtDelayExec,
  295. _cmDspMtDelayRecv,
  296. NULL,
  297. _cmDspMtDelayRecvFunc,
  298. "Multi-tap delay.");
  299. return &_cmMtDelayDC;
  300. }
  301. //==========================================================================================================================================
  302. enum
  303. {
  304. kBypassPsId,
  305. kRatioPsId,
  306. kInPsId,
  307. kOutPsId
  308. };
  309. cmDspClass_t _cmPShiftDC;
  310. typedef struct
  311. {
  312. cmDspInst_t inst;
  313. //cmCtx* ctx;
  314. cmPitchShift* pshift;
  315. } cmDspPShift_t;
  316. cmDspInst_t* _cmDspPShiftAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  317. {
  318. unsigned chs = 1;
  319. cmDspVarArg_t args[] =
  320. {
  321. { "bypass",kBypassPsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  322. { "ratio", kRatioPsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Ratio" },
  323. { "in", kInPsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  324. { "out", kOutPsId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  325. { NULL, 0, 0, 0, 0 }
  326. };
  327. cmDspPShift_t* p = cmDspInstAlloc(cmDspPShift_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  328. // set default values for the parameters that were not explicitely set in the va_arg list
  329. cmDspSetDefaultBool( ctx, &p->inst, kBypassPsId, 0, 0 );
  330. cmDspSetDefaultDouble( ctx, &p->inst ,kRatioPsId, 0.0, 1.0 );
  331. p->pshift = cmPitchShiftAlloc(ctx->cmProcCtx,NULL,cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx) );
  332. return &p->inst;
  333. }
  334. cmDspRC_t _cmDspPShiftFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  335. {
  336. cmDspRC_t rc = kOkDspRC;
  337. cmDspPShift_t* p = (cmDspPShift_t*)inst;
  338. cmPitchShiftFree(&p->pshift);
  339. //cmCtxFree(&p->ctx);
  340. return rc;
  341. }
  342. cmDspRC_t _cmDspPShiftReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  343. {
  344. cmDspRC_t rc = kOkDspRC;
  345. rc = cmDspApplyAllDefaults(ctx,inst);
  346. return rc;
  347. }
  348. cmDspRC_t _cmDspPShiftExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  349. {
  350. cmDspPShift_t* p = (cmDspPShift_t*)inst;
  351. cmDspRC_t rc = kOkDspRC;
  352. unsigned iChIdx = 0;
  353. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInPsId,iChIdx);
  354. unsigned iSmpCnt = cmDspVarRows(inst,kInPsId);
  355. unsigned oChIdx = 0;
  356. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutPsId,oChIdx);
  357. unsigned oSmpCnt = cmDspVarRows(inst,kOutPsId);
  358. bool bypassFl= cmDspBool(inst,kBypassPsId);
  359. cmPitchShiftExec(p->pshift,ip,op,cmMin(iSmpCnt,oSmpCnt),cmDspDouble(inst,kRatioPsId),bypassFl);
  360. return rc;
  361. }
  362. cmDspRC_t _cmDspPShiftRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  363. {
  364. return cmDspSetEvent(ctx,inst,evt);
  365. }
  366. struct cmDspClass_str* cmPShiftClassCons( cmDspCtx_t* ctx )
  367. {
  368. cmDspClassSetup(&_cmPShiftDC,ctx,"PShift",
  369. NULL,
  370. _cmDspPShiftAlloc,
  371. _cmDspPShiftFree,
  372. _cmDspPShiftReset,
  373. _cmDspPShiftExec,
  374. _cmDspPShiftRecv,
  375. NULL,NULL,
  376. "Pitch Shifter.");
  377. return &_cmPShiftDC;
  378. }
  379. //==========================================================================================================================================
  380. enum
  381. {
  382. kTimeLrId,
  383. kPGainLrId,
  384. kRGainLrId,
  385. kBypassLrId,
  386. kPlayLrId,
  387. kRecdLrId,
  388. kRatioLrId,
  389. kInLrId,
  390. kOutLrId
  391. };
  392. cmDspClass_t _cmLoopRecdDC;
  393. typedef struct
  394. {
  395. cmDspInst_t inst;
  396. //cmCtx* ctx;
  397. bool playFl;
  398. cmLoopRecord* lrp;
  399. } cmDspLoopRecd_t;
  400. cmDspInst_t* _cmDspLoopRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  401. {
  402. unsigned chs = 1;
  403. cmDspVarArg_t args[] =
  404. {
  405. { "time", kTimeLrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Max record time in seconds" },
  406. { "pgain", kPGainLrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Pass-through gain."},
  407. { "rgain", kRGainLrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Recorder out gain."},
  408. { "bypass",kBypassLrId,0, 0, kInDsvFl | kBoolDsvFl, "Bypass flag"},
  409. { "play", kPlayLrId, 0, 0, kInDsvFl | kBoolDsvFl, "Play gate flag" },
  410. { "recd", kRecdLrId, 0, 0, kInDsvFl | kBoolDsvFl, "Recd gate flag" },
  411. { "ratio", kRatioLrId, 0, 0, kInDsvFl | kDoubleDsvFl, "Playback speed ratio"},
  412. { "in", kInLrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  413. { "out", kOutLrId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  414. { NULL, 0, 0, 0, 0 }
  415. };
  416. cmDspLoopRecd_t* p = cmDspInstAlloc(cmDspLoopRecd_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  417. // set default values for the parameters that were not explicitely set in the va_arg list
  418. cmDspSetDefaultDouble( ctx, &p->inst, kTimeLrId, 0, 10 );
  419. cmDspSetDefaultDouble( ctx, &p->inst, kPGainLrId, 0, 1.0 );
  420. cmDspSetDefaultDouble( ctx, &p->inst, kRGainLrId, 0, 1.0 );
  421. cmDspSetDefaultBool( ctx, &p->inst, kBypassLrId, 0, 0 );
  422. cmDspSetDefaultBool( ctx, &p->inst, kPlayLrId, 0, 0 );
  423. cmDspSetDefaultBool( ctx, &p->inst, kRecdLrId, 0, 0 );
  424. cmDspSetDefaultDouble( ctx, &p->inst, kRatioLrId, 0, 1.0);
  425. p->lrp = cmLoopRecordAlloc(ctx->cmProcCtx,NULL,0,0,0 );
  426. return &p->inst;
  427. }
  428. cmDspRC_t _cmDspLoopRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  429. {
  430. cmDspRC_t rc = kOkDspRC;
  431. cmDspLoopRecd_t* p = (cmDspLoopRecd_t*)inst;
  432. cmLoopRecordFree(&p->lrp);
  433. //cmCtxFree(&p->ctx);
  434. return rc;
  435. }
  436. cmDspRC_t _cmDspLoopRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  437. {
  438. cmDspRC_t rc = kOkDspRC;
  439. cmDspLoopRecd_t* p = (cmDspLoopRecd_t*)inst;
  440. rc = cmDspApplyAllDefaults(ctx,inst);
  441. cmReal_t maxRecdTimeSecs = cmDspDefaultDouble(&p->inst,kTimeLrId);
  442. unsigned maxRecdTimeSmps = floor(cmDspSampleRate(ctx) * maxRecdTimeSecs);
  443. unsigned xfadeTimeSmps = floor(cmDspSampleRate(ctx) * 50.0/1000.0);
  444. if( maxRecdTimeSmps != p->lrp->maxRecdSmpCnt || xfadeTimeSmps != p->lrp->xfadeSmpCnt )
  445. cmLoopRecordInit(p->lrp,cmDspSamplesPerCycle(ctx),maxRecdTimeSmps,xfadeTimeSmps);
  446. return rc;
  447. }
  448. cmDspRC_t _cmDspLoopRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  449. {
  450. cmDspLoopRecd_t* p = (cmDspLoopRecd_t*)inst;
  451. cmDspRC_t rc = kOkDspRC;
  452. unsigned iChIdx = 0;
  453. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInLrId,iChIdx);
  454. unsigned iSmpCnt = cmDspVarRows(inst,kInLrId);
  455. unsigned oChIdx = 0;
  456. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutLrId,oChIdx);
  457. unsigned oSmpCnt = cmDspVarRows(inst,kOutLrId);
  458. bool recdFl = cmDspBool(inst,kRecdLrId);
  459. bool bypassFl = cmDspBool(inst,kBypassLrId);
  460. double ratio = cmDspDouble(inst,kRatioLrId);
  461. double rgain = cmDspDouble(inst,kRGainLrId); // recorder output gain
  462. double pgain = cmDspDouble(inst,kPGainLrId); // pass through gain
  463. if( ip != NULL && op != NULL )
  464. cmLoopRecordExec(p->lrp,ip,op,cmMin(iSmpCnt,oSmpCnt), bypassFl, recdFl, p->playFl, ratio, pgain, rgain );
  465. p->playFl = false;
  466. return rc;
  467. }
  468. cmDspRC_t _cmDspLoopRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  469. {
  470. cmDspLoopRecd_t* p = (cmDspLoopRecd_t*)inst;
  471. cmDspRC_t rc = cmDspSetEvent(ctx,inst,evt);
  472. switch(evt->dstVarId)
  473. {
  474. case kPlayLrId:
  475. p->playFl = cmDspBool(inst,kPlayLrId);
  476. break;
  477. }
  478. return rc;
  479. }
  480. struct cmDspClass_str* cmLoopRecdClassCons( cmDspCtx_t* ctx )
  481. {
  482. cmDspClassSetup(&_cmLoopRecdDC,ctx,"LoopRecd",
  483. NULL,
  484. _cmDspLoopRecdAlloc,
  485. _cmDspLoopRecdFree,
  486. _cmDspLoopRecdReset,
  487. _cmDspLoopRecdExec,
  488. _cmDspLoopRecdRecv,
  489. NULL,NULL,
  490. "Loop recorder.");
  491. return &_cmLoopRecdDC;
  492. }
  493. //==========================================================================================================================================
  494. enum
  495. {
  496. kBypassRcId,
  497. kCoeffRcId,
  498. kInRcId,
  499. kOutRcId
  500. };
  501. cmDspClass_t _cmRectifyDC;
  502. typedef struct
  503. {
  504. cmDspInst_t inst;
  505. } cmDspRectify_t;
  506. cmDspInst_t* _cmDspRectifyAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  507. {
  508. unsigned chs = 1;
  509. cmDspVarArg_t args[] =
  510. {
  511. { "bypass",kBypassRcId,0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  512. { "coeff", kCoeffRcId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Coefficient" },
  513. { "in", kInRcId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  514. { "out", kOutRcId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  515. { NULL, 0, 0, 0, 0 }
  516. };
  517. cmDspRectify_t* p = cmDspInstAlloc(cmDspRectify_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  518. // set default values for the parameters that were not explicitely set in the va_arg list
  519. cmDspSetDefaultBool( ctx, &p->inst, kBypassRcId,0, 0 );
  520. cmDspSetDefaultDouble( ctx, &p->inst, kCoeffRcId, 0, 0.0 );
  521. return &p->inst;
  522. }
  523. cmDspRC_t _cmDspRectifyFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  524. {
  525. return kOkDspRC;
  526. }
  527. cmDspRC_t _cmDspRectifyReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  528. {
  529. cmDspRC_t rc = kOkDspRC;
  530. rc = cmDspApplyAllDefaults(ctx,inst);
  531. return rc;
  532. }
  533. cmDspRC_t _cmDspRectifyExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  534. {
  535. unsigned iChIdx = 0;
  536. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInRcId,iChIdx);
  537. unsigned iSmpCnt = cmDspVarRows(inst,kInRcId);
  538. unsigned oChIdx = 0;
  539. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutRcId,oChIdx);
  540. unsigned oSmpCnt = cmDspVarRows(inst,kOutRcId);
  541. bool bypassFl= cmDspBool(inst,kBypassRcId);
  542. unsigned n = cmMin(iSmpCnt,oSmpCnt);
  543. unsigned i;
  544. if( bypassFl )
  545. memcpy(op,ip,n*sizeof(cmSample_t));
  546. else
  547. {
  548. for(i=0; i<n; ++i)
  549. op[i] = ip[i] > 0 ? ip[i] : 0;
  550. }
  551. return kOkDspRC;
  552. }
  553. cmDspRC_t _cmDspRectifyRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  554. {
  555. return cmDspSetEvent(ctx,inst,evt);
  556. }
  557. struct cmDspClass_str* cmRectifyClassCons( cmDspCtx_t* ctx )
  558. {
  559. cmDspClassSetup(&_cmRectifyDC,ctx,"Rectify",
  560. NULL,
  561. _cmDspRectifyAlloc,
  562. _cmDspRectifyFree,
  563. _cmDspRectifyReset,
  564. _cmDspRectifyExec,
  565. _cmDspRectifyRecv,
  566. NULL,NULL,
  567. "Half-wave rectifier.");
  568. return &_cmRectifyDC;
  569. }
  570. //==========================================================================================================================================
  571. enum
  572. {
  573. kWndMsGdId,
  574. kOnThreshPctGdId,
  575. kOnThreshDbGdId,
  576. kOffThreshDbGdId,
  577. kInGdId,
  578. kGateGdId,
  579. kRmsGdId,
  580. kMeanGdId
  581. };
  582. cmDspClass_t _cmGateDetectDC;
  583. typedef struct
  584. {
  585. cmDspInst_t inst;
  586. //cmCtx* ctx;
  587. cmShiftBuf* sbp;
  588. cmGateDetect* gdp;
  589. } cmDspGateDetect_t;
  590. cmDspInst_t* _cmDspGateDetectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  591. {
  592. cmDspVarArg_t args[] =
  593. {
  594. { "wnd", kWndMsGdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Window length in milliseconds." },
  595. { "onpct", kOnThreshPctGdId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Onset slope threshold [0.0 - 1.0]." },
  596. { "ondb", kOnThreshDbGdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Onset threshold dB [-Inf to 0]" },
  597. { "offdb", kOffThreshDbGdId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset threshold dB [-Inf to 0]" },
  598. { "in", kInGdId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  599. { "gate", kGateGdId, 0, 0, kOutDsvFl | kBoolDsvFl, "Gate state output." },
  600. { "rms", kRmsGdId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Signal level RMS"},
  601. { "mean", kMeanGdId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Derr mean."},
  602. { NULL, 0, 0, 0, 0 }
  603. };
  604. cmDspGateDetect_t* p = cmDspInstAlloc(cmDspGateDetect_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  605. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx,NULL,0,0,0);
  606. p->gdp = cmGateDetectAlloc(ctx->cmProcCtx,NULL,0,0,0,0);
  607. // set default values for the parameters that were not explicitely set in the va_arg list
  608. cmDspSetDefaultDouble( ctx, &p->inst, kWndMsGdId, 0, 42 );
  609. cmDspSetDefaultDouble( ctx, &p->inst, kOnThreshPctGdId, 0, 0.8 );
  610. cmDspSetDefaultDouble( ctx, &p->inst, kOnThreshDbGdId, 0, -30.0 );
  611. cmDspSetDefaultDouble( ctx, &p->inst, kOffThreshDbGdId, 0, -60.0 );
  612. return &p->inst;
  613. }
  614. cmDspRC_t _cmDspGateDetectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  615. {
  616. cmDspGateDetect_t* p = (cmDspGateDetect_t*)inst;
  617. cmGateDetectFree(&p->gdp);
  618. cmShiftBufFree(&p->sbp);
  619. //cmCtxFree(&p->ctx);
  620. return kOkDspRC;
  621. }
  622. cmDspRC_t _cmDspGateDetectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  623. {
  624. cmDspGateDetect_t* p = (cmDspGateDetect_t*)inst;
  625. cmDspRC_t rc = kOkDspRC;
  626. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  627. return rc;
  628. double wndMs = cmDspDouble(inst,kWndMsGdId);
  629. double sr = cmDspSampleRate(ctx);
  630. unsigned wndSmpCnt = floor(wndMs * sr / 1000.0);
  631. if( cmShiftBufInit(p->sbp, cmDspSamplesPerCycle(ctx), wndSmpCnt, wndSmpCnt/4 ) != cmOkRC )
  632. return cmErrMsg(&ctx->cmCtx->err,kInstResetFailDspRC,"The gate detector shift buffer initialization failed.");
  633. if( cmGateDetectInit(p->gdp, cmDspSamplesPerCycle(ctx), cmDspDouble(inst,kOnThreshPctGdId), cmDspDouble(inst,kOnThreshDbGdId), cmDspDouble(inst,kOffThreshDbGdId) ) != cmOkRC )
  634. return cmErrMsg(&ctx->cmCtx->err,kInstResetFailDspRC,"The gate detector shift buffer initialization failed.");
  635. return rc;
  636. }
  637. cmDspRC_t _cmDspGateDetectExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  638. {
  639. cmDspGateDetect_t* p = (cmDspGateDetect_t*)inst;
  640. unsigned iChIdx = 0;
  641. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInGdId,iChIdx);
  642. unsigned iSmpCnt = cmDspVarRows(inst,kInGdId);
  643. while( cmShiftBufExec(p->sbp, ip, iSmpCnt ) )
  644. {
  645. cmGateDetectExec(p->gdp,p->sbp->outV, p->sbp->outN );
  646. cmDspSetDouble(ctx,inst,kRmsGdId,p->gdp->rms);
  647. cmDspSetDouble(ctx,inst,kMeanGdId,p->gdp->mean);
  648. if( p->gdp->deltaFl )
  649. cmDspSetBool( ctx,inst,kGateGdId,p->gdp->gateFl);
  650. }
  651. return kOkDspRC;
  652. }
  653. cmDspRC_t _cmDspGateDetectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  654. {
  655. cmDspRC_t rc;
  656. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  657. return rc;
  658. switch(evt->dstVarId)
  659. {
  660. case kWndMsGdId:
  661. break;
  662. case kOnThreshPctGdId:
  663. break;
  664. case kOnThreshDbGdId:
  665. break;
  666. case kOffThreshDbGdId:
  667. break;
  668. case kInGdId:
  669. break;
  670. }
  671. return rc;
  672. }
  673. struct cmDspClass_str* cmGateDetectClassCons( cmDspCtx_t* ctx )
  674. {
  675. cmDspClassSetup(&_cmGateDetectDC,ctx,"GateDetect",
  676. NULL,
  677. _cmDspGateDetectAlloc,
  678. _cmDspGateDetectFree,
  679. _cmDspGateDetectReset,
  680. _cmDspGateDetectExec,
  681. _cmDspGateDetectRecv,
  682. NULL,NULL,
  683. "Gate detector.");
  684. return &_cmGateDetectDC;
  685. }
  686. //==========================================================================================================================================
  687. /*
  688. The purpose of this object is to calculate, store and retrieve gain coefficents
  689. for a set of audio channels. The gain coefficients are designed to balance the
  690. volume of each channel relative to the others. During gain calibration
  691. a sample of each channel is taken and it's average volume is determined.
  692. After an example of all channels has been received a new set of gain coefficients
  693. is calculated which decreases the volume of loud channels and increases the
  694. volume of quiet channels.
  695. The gain coefficents are made available via a set of 'gain-###' output ports.
  696. This object acts as an interface to the cmAutoGain processor.
  697. As input it takes a channel configuration JSON file of the form:
  698. {
  699. ch_array :
  700. [ ["ch","ssi","pitch","midi","gain"]
  701. [ 0, 0, "C4", 60, 1.0 ]
  702. ....
  703. [ n 0, "C5", 72, 1.0 ]
  704. ]
  705. }
  706. Each array in 'ch_array' gives the configuration of a channel.
  707. It also requires a JSON resource object of the form
  708. gdParms:
  709. {
  710. medCnt: 5
  711. avgCnt: 9
  712. suprCnt: 6
  713. offCnt: 3
  714. suprCoeff: 1.400000
  715. onThreshDb: -53.000000
  716. offThreshDb: -80.000000
  717. }
  718. These arguments are used to configure the cmAutoGain Proessor gate detector function.
  719. During runtime the object accepts the following action selector symbol id's:
  720. a. start - begin a new calibration session
  721. b. proc - end a calibration session and calculate new gain coeff's
  722. c. cancel - cancel a calibration session
  723. d. write - write the channel configuration file
  724. e. print - print the current auto gain calibration state
  725. After a 'start' msg the object accepts channel id's throught its 'id' input port.
  726. Each 'id' identifies the channel which it will process next.
  727. Upon reception of a channel id the object routes subsequent audio to its
  728. internal cmAutoGain processor until it receives the next channel id
  729. or a 'proc' or 'cancel' symbol.
  730. */
  731. //==========================================================================================================================================
  732. enum
  733. {
  734. kChCntAgId,
  735. kHopAgId,
  736. kMedNAgId,
  737. kAvgNAgId,
  738. kSupNAgId,
  739. kOffNAgId,
  740. kSupCoefAgId,
  741. kOnThrAgId,
  742. kOffThrAgId,
  743. kSelAgId,
  744. kIdAgId,
  745. kInBaseAgId
  746. };
  747. typedef struct
  748. {
  749. cmDspInst_t inst;
  750. cmAutoGain* agp;
  751. unsigned gainBaseAgId;
  752. unsigned chCnt;
  753. unsigned chIdx;
  754. unsigned startSymId;
  755. unsigned procSymId;
  756. unsigned cancelSymId;
  757. unsigned writeSymId;
  758. unsigned printSymId;
  759. } cmDspAutoGain_t;
  760. cmDspClass_t _cmAutoGainDC;
  761. cmDspInst_t* _cmDspAutoGainAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  762. {
  763. cmDspVarArg_t args[] =
  764. {
  765. { "chCnt",kChCntAgId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Audio channel count."},
  766. { "hop", kHopAgId, 0, 0, kDoubleDsvFl | kReqArgDsvFl, "RMS hop in milliseconds."},
  767. { "med", kMedNAgId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Median filter hop count."},
  768. { "avg", kAvgNAgId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Average filter hop count."},
  769. { "sup", kSupNAgId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Supression filter hop count."},
  770. { "off", kOffNAgId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Offset filter hop count."},
  771. { "supC", kSupCoefAgId,0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Suppression coefficent."},
  772. { "onThr",kOnThrAgId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Onset threshold in dB."},
  773. { "offThr",kOffThrAgId,0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Offset threshold in dB."},
  774. { "sel", kSelAgId, 0, 0, kInDsvFl | kSymDsvFl | kNoArgDsvFl, "Action Selector: start | proc | cancel." },
  775. { "id", kIdAgId, 0, 0, kInDsvFl | kIntDsvFl | kNoArgDsvFl, "Channel id input."},
  776. };
  777. va_list vl1;
  778. unsigned i;
  779. // copy the va_list so that it can be used again in cmDspInstAlloc()
  780. va_copy(vl1,vl);
  781. // verify that at least one var arg exists
  782. if( va_cnt < 1 )
  783. {
  784. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The AutoGain constructor must be given the audio channel count as its first argument.");
  785. return NULL;
  786. }
  787. // get the first var arg which should be a filename
  788. unsigned chCnt = va_arg(vl,unsigned);
  789. if( chCnt == 0 )
  790. {
  791. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The AutoGain constructor requires at least 1 audio channel.");
  792. return NULL;
  793. }
  794. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  795. unsigned argCnt = fixArgCnt + 2 * chCnt;
  796. unsigned gainBaseAgId = kInBaseAgId + chCnt;
  797. cmDspVarArg_t a[ argCnt+1 ];
  798. assert( fixArgCnt == kInBaseAgId );
  799. // setup the output gain args
  800. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  801. cmDspArgSetupN(ctx, a, argCnt, kInBaseAgId, chCnt, "in", kInBaseAgId, 0, 0, kInDsvFl | kAudioBufDsvFl, "audio in");
  802. cmDspArgSetupN(ctx, a, argCnt, gainBaseAgId, chCnt, "gain", gainBaseAgId, 0, 0, kOutDsvFl | kDoubleDsvFl, "calibrated channel gain");
  803. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  804. // instantiate the object
  805. cmDspAutoGain_t* p = cmDspInstAlloc(cmDspAutoGain_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1);
  806. // assign the current gain coefficients
  807. for(i=0; i<chCnt; ++i)
  808. cmDspSetDefaultDouble( ctx, &p->inst, gainBaseAgId + i, 0.0, 1.0);
  809. // allocate the auto gain calculation proc
  810. p->agp = cmAutoGainAlloc(ctx->cmProcCtx,NULL,0,0,0,0,NULL);
  811. p->chCnt = chCnt;
  812. p->chIdx = cmInvalidIdx;
  813. p->gainBaseAgId= gainBaseAgId;
  814. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  815. p->procSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"proc");
  816. p->cancelSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"cancel");
  817. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  818. cmDspSetDefaultSymbol( ctx, &p->inst, kSelAgId, p->cancelSymId );
  819. cmDspSetDefaultInt( ctx, &p->inst, kIdAgId, 0, cmInvalidId );
  820. return &p->inst;
  821. }
  822. cmDspRC_t _cmDspAutoGainFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  823. {
  824. cmDspAutoGain_t* p = (cmDspAutoGain_t*)inst;
  825. cmAutoGainFree(&p->agp);
  826. return kOkDspRC;
  827. }
  828. cmDspRC_t _cmDspAutoGainReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  829. {
  830. cmDspRC_t rc = kOkDspRC;
  831. rc = cmDspApplyAllDefaults(ctx,inst);
  832. return rc;
  833. }
  834. cmDspRC_t _cmDspAutoGainExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  835. {
  836. cmDspAutoGain_t* p = (cmDspAutoGain_t*)inst;
  837. unsigned curInChIdx = cmDspInt( inst, kIdAgId);
  838. if( cmDspSymbol( inst, kSelAgId ) == p->startSymId && curInChIdx != cmInvalidId )
  839. {
  840. unsigned inChVarId = kInBaseAgId+curInChIdx;
  841. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,inChVarId,0);
  842. unsigned iSmpCnt = cmDspVarRows(inst,inChVarId);
  843. cmAutoGainProcCh( p->agp, ip, iSmpCnt );
  844. }
  845. return kOkDspRC;
  846. }
  847. cmDspRC_t _cmDspAutoGainInit( cmDspCtx_t* ctx, cmDspInst_t* inst )
  848. {
  849. cmDspAutoGain_t* p = (cmDspAutoGain_t*)inst;
  850. cmGateDetectParams gd;
  851. unsigned i;
  852. // collect the params into a cmGateDetectParams recd
  853. gd.medCnt = cmDspUInt( inst, kMedNAgId );
  854. gd.avgCnt = cmDspUInt( inst, kAvgNAgId );
  855. gd.suprCnt = cmDspUInt( inst, kSupNAgId );
  856. gd.offCnt = cmDspUInt( inst, kOffNAgId );
  857. gd.suprCoeff = cmDspDouble( inst, kSupCoefAgId );
  858. gd.onThreshDb = cmDspDouble( inst, kOnThrAgId );
  859. gd.offThreshDb = cmDspDouble( inst, kOffThrAgId );
  860. // setup the internal auto-gain object
  861. if( cmAutoGainInit(p->agp, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), cmDspDouble(inst,kHopAgId), p->chCnt, &gd ) != cmOkRC )
  862. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The internal auto-gain instance could not be initialized.");
  863. // send out gain's of 1.0 so that the input audio is not
  864. // biased by any existing scaling.
  865. for(i=0; i<p->chCnt; ++i)
  866. cmDspSetDouble( ctx, inst, p->gainBaseAgId+i, 1.0);
  867. return kOkDspRC;
  868. }
  869. cmDspRC_t _cmDspAutoGainRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  870. {
  871. cmDspAutoGain_t* p = (cmDspAutoGain_t*)inst;
  872. cmDspRC_t rc = kOkDspRC;
  873. switch( evt->dstVarId )
  874. {
  875. case kSelAgId:
  876. {
  877. // store the current 'sel' state
  878. unsigned prvSymId = cmDspSymbol(inst,kSelAgId);
  879. // update it 'sel' to the new state
  880. cmDspSetEvent(ctx,inst,evt);
  881. // get the new state
  882. unsigned newSymId = cmDspSymbol(inst,kSelAgId);
  883. //
  884. // if PRINTing was requested
  885. //
  886. if( newSymId == p->printSymId )
  887. {
  888. cmRptPrintf(&ctx->cmCtx->rpt,"Auto-Gain Report\n");
  889. cmAutoGainPrint( p->agp, &ctx->cmCtx->rpt );
  890. goto doneLabel;
  891. }
  892. //
  893. // if calibration was CANCELLED
  894. //
  895. if( newSymId == p->cancelSymId )
  896. {
  897. cmDspSetInt( ctx, inst, kIdAgId, cmInvalidId );
  898. cmRptPrintf(&ctx->cmCtx->rpt,"cancelled\n");
  899. goto doneLabel;
  900. }
  901. //
  902. // if calibration STARTup was requested - initialize the autogain proc
  903. //
  904. if( newSymId == p->startSymId )
  905. {
  906. _cmDspAutoGainInit(ctx,inst);
  907. cmRptPrintf(&ctx->cmCtx->rpt,"started\n");
  908. goto doneLabel;
  909. }
  910. //
  911. // if calibration PROCessing was requested
  912. //
  913. if( newSymId == p->procSymId && prvSymId == p->startSymId )
  914. {
  915. cmRptPrintf(&ctx->cmCtx->rpt,"proc\n");
  916. // set the current channel id to 'cmInvalidId' to stop calls to
  917. // cmAutoGainProcCh() in _cmDspAutoGainExec()
  918. cmDspSetInt( ctx, inst, kIdAgId, cmInvalidId );
  919. // update the auto gain coefficients
  920. if( cmAutoGainCalcGains(p->agp) == cmOkRC )
  921. {
  922. // send the new auto gain coefficients to the output ports
  923. unsigned i;
  924. for(i=0; i<p->chCnt; ++i)
  925. cmDspSetDouble( ctx, inst, p->gainBaseAgId+i, p->agp->chArray[i].gain);
  926. }
  927. goto doneLabel;
  928. }
  929. }
  930. break;
  931. case kIdAgId:
  932. cmDspSetEvent(ctx,inst,evt);
  933. if( cmDspSymbol(inst,kSelAgId) == p->startSymId )
  934. {
  935. cmRptPrintf(&ctx->cmCtx->rpt,"id:%i\n", cmDspInt(inst,kIdAgId));
  936. cmAutoGainStartCh(p->agp, p->chIdx = cmDspInt(inst,kIdAgId));
  937. }
  938. break;
  939. default:
  940. { assert(0); }
  941. }
  942. doneLabel:
  943. return rc;
  944. }
  945. struct cmDspClass_str* cmAutoGainClassCons( cmDspCtx_t* ctx )
  946. {
  947. cmDspClassSetup(&_cmAutoGainDC,ctx,"AutoGain",
  948. NULL,
  949. _cmDspAutoGainAlloc,
  950. _cmDspAutoGainFree,
  951. _cmDspAutoGainReset,
  952. _cmDspAutoGainExec,
  953. _cmDspAutoGainRecv,
  954. NULL,NULL,
  955. "Auto-gain calibrator.");
  956. return &_cmAutoGainDC;
  957. }
  958. //==========================================================================================================================================
  959. enum
  960. {
  961. kHopMsEfId, // RMS window length in milliseconds
  962. kMedCntEfId, //
  963. kAvgCntEfId, //
  964. kSuprCntEfId, //
  965. kOffCntEfId, //
  966. kSuprCoefEfId, //
  967. kOnThrDbEfId, //
  968. kOffThrDbEfId, //
  969. kMaxDbEfId,
  970. kInEfId,
  971. kGateEfId,
  972. kRmsEfId,
  973. kLevelEfId,
  974. kOnEfId,
  975. kOffEfId
  976. };
  977. cmDspClass_t _cmEnvFollowDC;
  978. typedef struct
  979. {
  980. cmDspInst_t inst;
  981. cmShiftBuf* sbp;
  982. cmGateDetect2* gdp;
  983. } cmDspEnvFollow_t;
  984. cmDspInst_t* _cmDspEnvFollowAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  985. {
  986. cmDspVarArg_t args[] =
  987. {
  988. { "wnd", kHopMsEfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "RMS Window length"},
  989. { "med", kMedCntEfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Median filter length." },
  990. { "avg", kAvgCntEfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Averaging filter length." },
  991. { "sup", kSuprCntEfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Supression filter length." },
  992. { "off", kOffCntEfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Offset detection window length" },
  993. { "supc", kSuprCoefEfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Suppression shape coefficient" },
  994. { "ondb", kOnThrDbEfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Onset threshold dB." },
  995. { "offdb", kOffThrDbEfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset threshold dB" },
  996. { "maxdb", kMaxDbEfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Max reference dB" },
  997. { "in", kInEfId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  998. { "gate", kGateEfId, 0, 0, kOutDsvFl | kBoolDsvFl, "Gate state output." },
  999. { "rms", kRmsEfId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Signal level RMS"},
  1000. { "level", kLevelEfId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Signal level 0.0-1.0 as scale between offset thresh dB and max dB"},
  1001. { "ons", kOnEfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Onset counter"},
  1002. { "offs", kOffEfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Offset counter"},
  1003. { NULL, 0, 0, 0, 0 }
  1004. };
  1005. cmDspEnvFollow_t* p = cmDspInstAlloc(cmDspEnvFollow_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1006. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx,NULL,0,0,0);
  1007. p->gdp = cmGateDetectAlloc2(ctx->cmProcCtx,NULL,0,NULL);
  1008. // set default values for the parameters that were not explicitely set in the va_arg list
  1009. cmDspSetDefaultDouble( ctx, &p->inst, kHopMsEfId, 0, 12 );
  1010. cmDspSetDefaultUInt( ctx, &p->inst, kMedCntEfId, 0, 5 );
  1011. cmDspSetDefaultUInt( ctx, &p->inst, kAvgCntEfId, 0, 9 );
  1012. cmDspSetDefaultUInt( ctx, &p->inst, kSuprCntEfId, 0, 6 );
  1013. cmDspSetDefaultUInt( ctx, &p->inst, kOffCntEfId, 0, 3 );
  1014. cmDspSetDefaultDouble( ctx, &p->inst, kSuprCoefEfId, 0, 1.4 );
  1015. cmDspSetDefaultDouble( ctx, &p->inst, kOnThrDbEfId, 0, -45 );
  1016. cmDspSetDefaultDouble( ctx, &p->inst, kOffThrDbEfId, 0, -80 );
  1017. cmDspSetDefaultDouble( ctx, &p->inst, kMaxDbEfId, 0, -10.0 );
  1018. cmDspSetDefaultUInt( ctx, &p->inst, kOnEfId, 0, 0 );
  1019. cmDspSetDefaultUInt( ctx, &p->inst, kOffEfId, 0, 0 );
  1020. return &p->inst;
  1021. }
  1022. cmDspRC_t _cmDspEnvFollowFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1023. {
  1024. cmDspEnvFollow_t* p = (cmDspEnvFollow_t*)inst;
  1025. cmGateDetectFree2(&p->gdp);
  1026. cmShiftBufFree(&p->sbp);
  1027. return kOkDspRC;
  1028. }
  1029. cmDspRC_t _cmDspEnvFollowReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1030. {
  1031. cmDspEnvFollow_t* p = (cmDspEnvFollow_t*)inst;
  1032. cmDspRC_t rc = kOkDspRC;
  1033. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  1034. return rc;
  1035. cmGateDetectParams r;
  1036. r.medCnt = cmDspUInt( inst, kMedCntEfId );
  1037. r.avgCnt = cmDspUInt( inst, kAvgCntEfId );
  1038. r.suprCnt = cmDspUInt( inst, kSuprCntEfId );
  1039. r.offCnt = cmDspUInt( inst, kOffCntEfId );
  1040. r.suprCoeff = cmDspDouble( inst, kSuprCoefEfId );
  1041. r.onThreshDb = cmDspDouble( inst, kOnThrDbEfId );
  1042. r.offThreshDb = cmDspDouble( inst, kOffThrDbEfId );
  1043. double sr = cmDspSampleRate(ctx);
  1044. double hopSmpCnt = floor(sr * cmDspDouble(inst,kHopMsEfId) / 1000 );
  1045. unsigned wndSmpCnt = floor(r.medCnt * hopSmpCnt);
  1046. if( cmShiftBufInit(p->sbp, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt ) != cmOkRC )
  1047. return cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The gate detector shift buffer initialization failed.");
  1048. if( cmGateDetectInit2(p->gdp, cmDspSamplesPerCycle(ctx), &r ) != cmOkRC )
  1049. return cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The gate detector shift buffer initialization failed.");
  1050. return rc;
  1051. }
  1052. cmDspRC_t _cmDspEnvFollowExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1053. {
  1054. cmDspEnvFollow_t* p = (cmDspEnvFollow_t*)inst;
  1055. unsigned iChIdx = 0;
  1056. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInEfId,iChIdx);
  1057. unsigned iSmpCnt = cmDspVarRows(inst,kInEfId);
  1058. double maxDb = cmDspDouble(inst,kMaxDbEfId);
  1059. double offDb = cmDspDouble(inst,kOffThrDbEfId);
  1060. while( cmShiftBufExec(p->sbp, ip, iSmpCnt ) )
  1061. {
  1062. cmGateDetectExec2(p->gdp,p->sbp->outV, p->sbp->outN );
  1063. // RMS is going out at the audio rate - maybe there should be an option
  1064. // to send it only when the gate changes - this could significantly
  1065. // cut down on unnecessary transmission if the RMS is only used
  1066. // when the gate changes
  1067. cmDspSetDouble(ctx,inst,kRmsEfId, p->gdp->rms );
  1068. double rmsDb = p->gdp->rms < 0.00001 ? -100.0 : 20.0 * log10(p->gdp->rms);
  1069. double level = maxDb <= offDb ? 0 : fabs((offDb - cmMax( offDb, cmMin( maxDb, rmsDb ))) / (maxDb - offDb));
  1070. cmDspSetDouble(ctx,inst,kLevelEfId, level );
  1071. if( p->gdp->onFl || p->gdp->offFl )
  1072. {
  1073. cmDspSetBool(ctx, inst, kGateEfId, p->gdp->gateFl);
  1074. if( p->gdp->onFl )
  1075. cmDspSetUInt( ctx, inst, kOnEfId, cmDspUInt(inst,kOnEfId) + 1 );
  1076. if( p->gdp->offFl )
  1077. cmDspSetUInt( ctx, inst, kOffEfId, cmDspUInt(inst,kOffEfId) + 1 );
  1078. }
  1079. }
  1080. return kOkDspRC;
  1081. }
  1082. cmDspRC_t _cmDspEnvFollowRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1083. {
  1084. cmDspRC_t rc;
  1085. cmDspEnvFollow_t* p = (cmDspEnvFollow_t*)inst;
  1086. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1087. {
  1088. switch( evt->dstVarId )
  1089. {
  1090. case kOnThrDbEfId:
  1091. cmGateDetectSetOnThreshDb2(p->gdp, cmDspDouble(inst,kOnThrDbEfId) );
  1092. break;
  1093. case kOffThrDbEfId:
  1094. cmGateDetectSetOffThreshDb2(p->gdp, cmDspDouble(inst,kOffThrDbEfId) );
  1095. break;
  1096. }
  1097. }
  1098. return rc;
  1099. }
  1100. struct cmDspClass_str* cmEnvFollowClassCons( cmDspCtx_t* ctx )
  1101. {
  1102. cmDspClassSetup(&_cmEnvFollowDC,ctx,"EnvFollow",
  1103. NULL,
  1104. _cmDspEnvFollowAlloc,
  1105. _cmDspEnvFollowFree,
  1106. _cmDspEnvFollowReset,
  1107. _cmDspEnvFollowExec,
  1108. _cmDspEnvFollowRecv,
  1109. NULL,NULL,
  1110. "Envelope follower and gate detector.");
  1111. return &_cmEnvFollowDC;
  1112. }
  1113. //==========================================================================================================================================
  1114. // Fade in and out an arbitrary number of audio signals based on gate signals.
  1115. // When the gate is high the signal fades in and when the gate is low the signal fades out.
  1116. // Constructor Args:
  1117. // Required: Count of input and output channels.
  1118. // Optional: Fade time in milliseconds.
  1119. //
  1120. // Inputs:
  1121. // bool Control gates
  1122. // audio Input audio.
  1123. // Outputs:
  1124. // audio Output audio
  1125. // double Channel gains.
  1126. enum
  1127. {
  1128. kChCntXfId,
  1129. kFadeTimeMsXfId,
  1130. kMstrGateXfId,
  1131. kOnXfId,
  1132. kOffXfId,
  1133. kGateBaseXfId,
  1134. };
  1135. cmDspClass_t _cmXfaderDC;
  1136. typedef struct
  1137. {
  1138. cmDspInst_t inst;
  1139. cmXfader* xfdp;
  1140. unsigned inBaseXfId;
  1141. unsigned outBaseXfId;
  1142. unsigned gainBaseXfId;
  1143. unsigned chCnt;
  1144. bool* chGateV;
  1145. unsigned onSymId;
  1146. unsigned offSymId;
  1147. } cmDspXfader_t;
  1148. cmDspInst_t* _cmDspXfaderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1149. {
  1150. cmDspVarArg_t args[] =
  1151. {
  1152. { "chs", kChCntXfId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input and Output channel count"},
  1153. { "ms", kFadeTimeMsXfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade time in milliseonds."},
  1154. { "mgate", kMstrGateXfId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Master gate - can be used to set all gates."},
  1155. { "on", kOnXfId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'on' when all ch's transition from off to on."},
  1156. { "off", kOffXfId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'off' when all ch's transition from on to off."},
  1157. };
  1158. if( va_cnt < 1 )
  1159. {
  1160. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Xfader object must be given a channel count argument.");
  1161. return NULL;
  1162. }
  1163. va_list vl1;
  1164. va_copy(vl1,vl);
  1165. unsigned chCnt = va_arg(vl,int);
  1166. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1167. unsigned argCnt = fixArgCnt + 4*chCnt;
  1168. unsigned inBaseXfId = kGateBaseXfId + chCnt;
  1169. unsigned outBaseXfId = inBaseXfId + chCnt;
  1170. unsigned gainBaseXfId= outBaseXfId + chCnt;
  1171. cmDspVarArg_t a[ argCnt+1 ];
  1172. // setup the input gate detectors and the output gain args
  1173. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1174. cmDspArgSetupN(ctx, a, argCnt, kGateBaseXfId, chCnt, "gate", kGateBaseXfId, 0, 0, kInDsvFl | kBoolDsvFl, "gate flags");
  1175. cmDspArgSetupN(ctx, a, argCnt, inBaseXfId, chCnt, "in", inBaseXfId, 0, 0, kInDsvFl | kAudioBufDsvFl, "audio input");
  1176. cmDspArgSetupN(ctx, a, argCnt, outBaseXfId, chCnt, "out", outBaseXfId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "audio output");
  1177. cmDspArgSetupN(ctx, a, argCnt, gainBaseXfId, chCnt, "gain", gainBaseXfId, 0, 0, kOutDsvFl | kDoubleDsvFl, "gain output");
  1178. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1179. cmDspXfader_t* p = cmDspInstAlloc(cmDspXfader_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1);
  1180. double fadeTimeMs = cmDspDouble(&p->inst, kFadeTimeMsXfId );
  1181. p->xfdp = cmXfaderAlloc(ctx->cmProcCtx,NULL,cmDspSampleRate(ctx), chCnt, fadeTimeMs);
  1182. p->inBaseXfId = inBaseXfId;
  1183. p->outBaseXfId = outBaseXfId;
  1184. p->gainBaseXfId = gainBaseXfId;
  1185. p->chCnt = chCnt;
  1186. p->chGateV = cmMemAllocZ(bool,p->chCnt);
  1187. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  1188. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  1189. // set default values for the parameters that were not explicitely set in the va_arg list
  1190. cmDspSetDefaultDouble( ctx, &p->inst, kFadeTimeMsXfId, 0, 100 );
  1191. cmDspSetDefaultBool( ctx, &p->inst, kMstrGateXfId, false, false);
  1192. cmDspSetDefaultSymbol( ctx, &p->inst, kOnXfId, p->onSymId );
  1193. cmDspSetDefaultSymbol( ctx, &p->inst, kOffXfId, p->offSymId );
  1194. return &p->inst;
  1195. }
  1196. cmDspRC_t _cmDspXfaderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1197. {
  1198. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1199. cmMemFree(p->chGateV);
  1200. cmXfaderFree(&p->xfdp);
  1201. return kOkDspRC;
  1202. }
  1203. cmDspRC_t _cmDspXfaderReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1204. {
  1205. cmDspRC_t rc = kOkDspRC;
  1206. rc = cmDspApplyAllDefaults(ctx,inst);
  1207. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1208. // TODO: zeroing of output audio buffers should be built into cmDspApplyAllDefaults().
  1209. unsigned i;
  1210. for(i=0; i<p->chCnt; ++i)
  1211. cmDspZeroAudioBuf(ctx,inst,p->outBaseXfId + i);
  1212. return rc;
  1213. }
  1214. cmDspRC_t _cmDspXfaderExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1215. {
  1216. cmDspRC_t rc = cmOkRC;
  1217. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1218. unsigned i;
  1219. // update the internal cross fader by providing it with new gate settings and generate new gain values
  1220. cmXfaderExec( p->xfdp, cmDspSamplesPerCycle(ctx), p->chGateV, p->chCnt );
  1221. for(i=0; i<p->chCnt; ++i)
  1222. {
  1223. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->outBaseXfId+i,0);
  1224. cmSample_t* op = cmDspAudioBuf(ctx,inst,p->outBaseXfId+i,0);
  1225. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,p->inBaseXfId+i,0);
  1226. cmSample_t gain = (cmSample_t)p->xfdp->chArray[i].gain;
  1227. if( op != NULL )
  1228. {
  1229. if( ip == NULL )
  1230. cmVOS_Zero(op,n);
  1231. else
  1232. cmVOS_MultVVS(op,n,ip,gain);
  1233. }
  1234. // send the gain output
  1235. cmDspSetDouble(ctx,inst,p->gainBaseXfId+i,gain);
  1236. }
  1237. if( p->xfdp->onFl )
  1238. cmDspSetSymbol(ctx,inst,kOnXfId,p->onSymId);
  1239. if( p->xfdp->offFl )
  1240. cmDspSetSymbol(ctx,inst,kOffXfId,p->offSymId);
  1241. return rc;
  1242. }
  1243. cmDspRC_t _cmDspXfaderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1244. {
  1245. cmDspRC_t rc;
  1246. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1247. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  1248. return rc;
  1249. switch( evt->dstVarId )
  1250. {
  1251. case kFadeTimeMsXfId:
  1252. // if this is an xfade time event then transfer the new xfade time to the xfade proc
  1253. cmXfaderSetXfadeTime(p->xfdp,cmDspDouble(inst,kFadeTimeMsXfId));
  1254. break;
  1255. case kMstrGateXfId:
  1256. {
  1257. bool fl = cmDspBool(inst,kMstrGateXfId);
  1258. printf("mstr:%i\n",fl);
  1259. unsigned i;
  1260. for(i=0; i<p->chCnt; ++i)
  1261. p->chGateV[i] = fl;
  1262. }
  1263. break;
  1264. }
  1265. // record gate changes into p->chGateV[] for later use in _cmDspXfaderExec().
  1266. if( kGateBaseXfId <= evt->dstVarId && evt->dstVarId < kGateBaseXfId + p->chCnt )
  1267. {
  1268. p->chGateV[ evt->dstVarId - kGateBaseXfId ] = cmDspBool( inst, evt->dstVarId );
  1269. }
  1270. return rc;
  1271. }
  1272. struct cmDspClass_str* cmXfaderClassCons( cmDspCtx_t* ctx )
  1273. {
  1274. cmDspClassSetup(&_cmXfaderDC,ctx,"Xfader",
  1275. NULL,
  1276. _cmDspXfaderAlloc,
  1277. _cmDspXfaderFree,
  1278. _cmDspXfaderReset,
  1279. _cmDspXfaderExec,
  1280. _cmDspXfaderRecv,
  1281. NULL,NULL,
  1282. "Cross fade gain generator.");
  1283. return &_cmXfaderDC;
  1284. }
  1285. //==========================================================================================================================================
  1286. enum
  1287. {
  1288. kFnCcId,
  1289. kSelCcId,
  1290. kDoneCcId,
  1291. kGainBaseCcId
  1292. };
  1293. cmDspClass_t _cmChCfgDC;
  1294. typedef struct
  1295. {
  1296. cmDspInst_t inst;
  1297. cmChCfg* ccp;
  1298. unsigned midiBaseCcId;
  1299. unsigned hzBaseCcId;
  1300. unsigned chBaseCcId;
  1301. unsigned nsflBaseCcId;
  1302. unsigned nshzBaseCcId;
  1303. unsigned printSymId;
  1304. unsigned writeSymId;
  1305. unsigned nsCmdSymId;
  1306. unsigned hzCmdSymId;
  1307. unsigned resetSymId;
  1308. } cmDspChCfg_t;
  1309. cmDspInst_t* _cmDspChCfgAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1310. {
  1311. cmDspVarArg_t args[] =
  1312. {
  1313. { "fn", kFnCcId, 0, 0, kStrzDsvFl| kReqArgDsvFl, "Channel configuration JSON file name."},
  1314. { "sel", kSelCcId, 0, 0, kInDsvFl | kSymDsvFl, "Action selector: print | write | ns | reset"},
  1315. { "done", kDoneCcId,0, 0, kOutDsvFl | kSymDsvFl, "Trigger following action."}
  1316. };
  1317. if( va_cnt < 1 )
  1318. {
  1319. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The channel configuration object must be given a file name argument.");
  1320. return NULL;
  1321. }
  1322. va_list vl1;
  1323. va_copy(vl1,vl);
  1324. const cmChar_t* chCfgFn = va_arg(vl,cmChar_t*);
  1325. cmChCfg* ccp = cmChCfgAlloc( ctx->cmProcCtx, NULL, ctx->cmCtx, chCfgFn );
  1326. if( ccp == NULL || ccp->chCnt==0 )
  1327. {
  1328. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The channel configuration object could not be initialized with the file name '%s'.",cmStringNullGuard(chCfgFn));
  1329. return NULL;
  1330. }
  1331. unsigned chCnt = ccp->chCnt;
  1332. unsigned nsChCnt = ccp->nsChCnt;
  1333. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1334. unsigned argCnt = fixArgCnt + 5*chCnt + nsChCnt;
  1335. unsigned midiBaseCcId = kGainBaseCcId + chCnt;
  1336. unsigned hzBaseCcId = midiBaseCcId + chCnt;
  1337. unsigned chBaseCcId = hzBaseCcId + chCnt;
  1338. unsigned nsflBaseCcId = chBaseCcId + chCnt;
  1339. unsigned nshzBaseCcId = nsflBaseCcId + chCnt;
  1340. cmDspChCfg_t* p = NULL;
  1341. cmDspVarArg_t a[ argCnt+1 ];
  1342. unsigned i,j;
  1343. // setup the input gate detectors and the output gain args
  1344. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1345. cmDspArgSetupN(ctx, a, argCnt, kGainBaseCcId, chCnt, "gain", kGainBaseCcId, 0, 0, kSendDfltDsvFl | kInDsvFl | kOutDsvFl | kDoubleDsvFl, "Gain input and output.");
  1346. cmDspArgSetupN(ctx, a, argCnt, midiBaseCcId, chCnt, "midi", midiBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kUIntDsvFl, "MIDI pitch output");
  1347. cmDspArgSetupN(ctx, a, argCnt, hzBaseCcId, chCnt, "hz", hzBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kDoubleDsvFl, "pitch output in Hz");
  1348. cmDspArgSetupN(ctx, a, argCnt, chBaseCcId, chCnt, "ch", chBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kUIntDsvFl , "Audio channel index");
  1349. cmDspArgSetupN(ctx, a, argCnt, nsflBaseCcId, chCnt, "nsfl", nsflBaseCcId, 0, 0, kOutDsvFl | kBoolDsvFl, "noise shaper enables");
  1350. cmDspArgSetupN(ctx, a, argCnt, nshzBaseCcId, nsChCnt, "nshz", nshzBaseCcId, 0, 0, kOutDsvFl | kDoubleDsvFl, "noise-shaper pitch output in Hz");
  1351. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1352. if((p = cmDspInstAlloc(cmDspChCfg_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1353. return NULL;
  1354. p->ccp = ccp;
  1355. p->midiBaseCcId = midiBaseCcId;
  1356. p->hzBaseCcId = hzBaseCcId;
  1357. p->chBaseCcId = chBaseCcId;
  1358. p->nsflBaseCcId = nsflBaseCcId;
  1359. p->nshzBaseCcId = nshzBaseCcId;
  1360. p->writeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"write");
  1361. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  1362. p->nsCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"ns");
  1363. p->hzCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"hz");
  1364. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"reset");
  1365. for(i=0,j=0; i<chCnt; ++i)
  1366. {
  1367. double hz = cmMidiToHz(ccp->chArray[i].midi);
  1368. cmDspSetDefaultDouble(ctx, &p->inst, kGainBaseCcId + i, 0.0, ccp->chArray[i].gain);
  1369. cmDspSetDefaultUInt( ctx, &p->inst, p->midiBaseCcId + i, 0, ccp->chArray[i].midi );
  1370. cmDspSetDefaultDouble(ctx, &p->inst, p->hzBaseCcId + i, 0.0, hz );
  1371. cmDspSetDefaultUInt( ctx, &p->inst, p->chBaseCcId + i, 0, ccp->chArray[i].ch );
  1372. cmDspSetDefaultBool( ctx, &p->inst, p->nsflBaseCcId+i, false, false );
  1373. if( ccp->chArray[i].nsFl )
  1374. {
  1375. cmDspSetDefaultDouble(ctx,&p->inst, p->nshzBaseCcId+j, 0.0, hz);
  1376. ++j;
  1377. }
  1378. }
  1379. cmDspSetDefaultSymbol(ctx, &p->inst, kDoneCcId, cmInvalidId );
  1380. cmDspSetDefaultSymbol(ctx, &p->inst, kSelCcId, cmInvalidId );
  1381. return &p->inst;
  1382. }
  1383. cmDspRC_t _cmDspChCfgFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1384. {
  1385. cmDspChCfg_t* p = (cmDspChCfg_t*)inst;
  1386. cmChCfgFree(&p->ccp);
  1387. return kOkDspRC;
  1388. }
  1389. cmDspRC_t _cmDspChCfgReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1390. {
  1391. cmDspRC_t rc = kOkDspRC;
  1392. rc = cmDspApplyAllDefaults(ctx,inst);
  1393. return rc;
  1394. }
  1395. cmDspRC_t _cmDspChCfgRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1396. {
  1397. cmDspRC_t rc;
  1398. cmDspChCfg_t* p = (cmDspChCfg_t*)inst;
  1399. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1400. {
  1401. if( evt->dstVarId == kSelCcId )
  1402. {
  1403. unsigned selId = cmDspSymbol(inst,kSelCcId);
  1404. if( selId == p->resetSymId )
  1405. {
  1406. _cmDspChCfgReset(ctx,inst,evt);
  1407. }
  1408. else
  1409. if( selId == p->hzCmdSymId )
  1410. {
  1411. unsigned i;
  1412. // snd the hz
  1413. for(i=0; i<p->ccp->chCnt; ++i)
  1414. cmDspSetDouble(ctx,inst,p->hzBaseCcId+i,cmDspDouble(inst,p->hzBaseCcId+i));
  1415. }
  1416. else
  1417. if( selId == p->nsCmdSymId )
  1418. {
  1419. cmRptPrintf(ctx->rpt,"ChCfg:NS\n");
  1420. unsigned i;
  1421. // send the ns flags
  1422. for(i=0; i<p->ccp->chCnt; ++i)
  1423. cmDspSetBool(ctx,inst,p->nsflBaseCcId+i,p->ccp->chArray[i].nsFl);
  1424. // snd the ns hz
  1425. for(i=0; i<p->ccp->nsChCnt; ++i)
  1426. cmDspSetDouble(ctx,inst,p->nshzBaseCcId+i,cmDspDouble(inst,p->nshzBaseCcId+i));
  1427. cmDspSetSymbol(ctx,inst,kDoneCcId,p->nsCmdSymId);
  1428. }
  1429. else
  1430. if( selId == p->printSymId )
  1431. {
  1432. cmRptPrintf(&ctx->cmCtx->rpt,"Channel Cfg Report\n");
  1433. cmChCfgPrint(p->ccp, ctx->rpt );
  1434. }
  1435. else
  1436. {
  1437. if( selId == p->writeSymId )
  1438. {
  1439. unsigned i;
  1440. cmRptPrintf(&ctx->cmCtx->rpt,"writing\n");
  1441. // copy the gain values into the internal chCfg object ...
  1442. for(i=0; i<p->ccp->chCnt; ++i)
  1443. p->ccp->chArray[i].gain = cmDspDouble(inst,kGainBaseCcId+i);
  1444. // ... and write the object
  1445. cmChCfgWrite(p->ccp);
  1446. }
  1447. }
  1448. }
  1449. }
  1450. return rc;
  1451. }
  1452. struct cmDspClass_str* cmChCfgClassCons( cmDspCtx_t* ctx )
  1453. {
  1454. cmDspClassSetup(&_cmChCfgDC,ctx,"ChCfg",
  1455. NULL,
  1456. _cmDspChCfgAlloc,
  1457. _cmDspChCfgFree,
  1458. _cmDspChCfgReset,
  1459. NULL,
  1460. _cmDspChCfgRecv,
  1461. NULL,NULL,
  1462. "PP Channel Configuration Object.");
  1463. return &_cmChCfgDC;
  1464. }
  1465. //==========================================================================================================================================
  1466. enum
  1467. {
  1468. kRsrcCdId,
  1469. kMaxTimeSpanCdId,
  1470. kMinNoteCntCdId,
  1471. kDetectCdId,
  1472. kCountCdId,
  1473. kGateBaseCdId
  1474. };
  1475. cmDspClass_t _cmChordDetectDC;
  1476. typedef struct
  1477. {
  1478. cmDspInst_t inst;
  1479. cmChordDetect* cdp;
  1480. unsigned rmsBaseCdId;
  1481. unsigned chCnt;
  1482. bool* chGateV; // chGateV[ chCnt ]
  1483. cmReal_t* chRmsV; // chRmsV[ chCnt ]
  1484. unsigned* chEnaV; // chEnaV[ chCnt ]
  1485. unsigned count;
  1486. } cmDspChordDetect_t;
  1487. cmDspInst_t* _cmDspChordDetectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1488. {
  1489. cmDspVarArg_t args[] =
  1490. {
  1491. { "rsrc", kRsrcCdId, 0, 0, kStrzDsvFl | kReqArgDsvFl, "Channel enable flag array."},
  1492. { "span", kMaxTimeSpanCdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Max. onset time span."},
  1493. { "notes", kMinNoteCntCdId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Min. note count per chord."},
  1494. { "detect", kDetectCdId, 0, 0, kOutDsvFl | kBoolDsvFl, "Chord detect flag."},
  1495. { "count", kCountCdId, 0, 0, kOutDsvFl | kUIntDsvFl, "Count of chords detected since last reset."}
  1496. };
  1497. if( va_cnt < 1 )
  1498. {
  1499. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector must be given a channel enable flags array resource argument .");
  1500. return NULL;
  1501. }
  1502. va_list vl1;
  1503. va_copy(vl1,vl);
  1504. const cmChar_t* rsrc = va_arg(vl,const cmChar_t*);
  1505. unsigned* enaV = NULL;
  1506. unsigned chCnt = 0;
  1507. if( cmDspRsrcUIntArray( ctx->dspH, &chCnt, &enaV, rsrc, NULL ) != kOkDspRC )
  1508. {
  1509. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(rsrc));
  1510. return NULL;
  1511. }
  1512. //cmRptPrintf(ctx->rpt,"cd %s chs:%i\n",rsrc,chCnt);
  1513. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1514. unsigned argCnt = fixArgCnt + 2*chCnt;
  1515. unsigned rmsBaseCdId = kGateBaseCdId + chCnt;
  1516. cmDspVarArg_t a[ argCnt+1 ];
  1517. unsigned i;
  1518. cmDspChordDetect_t* p;
  1519. // setup the input gate detectors and the output gain args
  1520. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1521. cmDspArgSetupN(ctx, a, argCnt, kGateBaseCdId, chCnt, "gate", kGateBaseCdId, 0, 0, kInDsvFl | kOutDsvFl | kBoolDsvFl, "Channel gate input and output.");
  1522. cmDspArgSetupN(ctx, a, argCnt, rmsBaseCdId, chCnt, "rms", rmsBaseCdId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl, "Channel RMS input and output");
  1523. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1524. if((p = cmDspInstAlloc(cmDspChordDetect_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1525. return NULL;
  1526. double dfltMaxTimeSpanMs = 50.0;
  1527. unsigned dfltMinNoteCnt = 2;
  1528. cmDspSetDefaultDouble( ctx, &p->inst, kMaxTimeSpanCdId, 0.0, dfltMaxTimeSpanMs );
  1529. cmDspSetDefaultUInt( ctx, &p->inst, kMinNoteCntCdId, 0, dfltMinNoteCnt );
  1530. cmDspSetDefaultBool( ctx, &p->inst, kDetectCdId, false, false );
  1531. cmDspSetDefaultUInt( ctx, &p->inst, kCountCdId, 0, 0 );
  1532. for(i=0; i<chCnt; ++i)
  1533. {
  1534. cmDspSetDefaultBool( ctx, &p->inst, kGateBaseCdId + i, false, false );
  1535. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseCdId + i, 0.0, 0.0 );
  1536. }
  1537. p->cdp = cmChordDetectAlloc( ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), chCnt, cmDspDouble(&p->inst,kMaxTimeSpanCdId), cmDspUInt(&p->inst,kMinNoteCntCdId) );
  1538. p->rmsBaseCdId = rmsBaseCdId;
  1539. p->chCnt = chCnt;
  1540. p->chGateV = cmMemAllocZ(bool, chCnt);
  1541. p->chRmsV = cmMemAllocZ(cmReal_t, chCnt);
  1542. p->chEnaV = enaV;
  1543. return &p->inst;
  1544. }
  1545. cmDspRC_t _cmDspChordDetectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1546. {
  1547. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1548. cmChordDetectFree(&p->cdp);
  1549. cmMemFree(p->chGateV);
  1550. cmMemFree(p->chRmsV);
  1551. return kOkDspRC;
  1552. }
  1553. cmDspRC_t _cmDspChordDetectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1554. {
  1555. cmDspRC_t rc = kOkDspRC;
  1556. rc = cmDspApplyAllDefaults(ctx,inst);
  1557. return rc;
  1558. }
  1559. cmDspRC_t _cmDspChordDetectExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1560. {
  1561. cmDspRC_t rc = kOkDspRC;
  1562. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1563. cmChordDetectExec(p->cdp, cmDspSamplesPerCycle(ctx), p->chGateV, p->chRmsV, p->chCnt );
  1564. if( p->cdp->detectFl )
  1565. {
  1566. unsigned i;
  1567. for(i=0; i<p->chCnt; ++i)
  1568. {
  1569. bool fl = p->cdp->chArray[i].chordFl;
  1570. cmDspSetBool( ctx, inst, kGateBaseCdId + i, fl );
  1571. cmDspSetDouble( ctx, inst, p->rmsBaseCdId + i, fl ? p->cdp->chArray[i].candRMS : 0 );
  1572. }
  1573. cmDspSetBool(ctx, inst, kDetectCdId, true);
  1574. cmDspSetUInt(ctx, inst, kCountCdId, cmDspUInt(inst,kCountCdId) + 1 );
  1575. }
  1576. cmVOB_Zero(p->chGateV,p->chCnt);
  1577. cmVOR_Zero(p->chRmsV,p->chCnt);
  1578. return rc;
  1579. }
  1580. cmDspRC_t _cmDspChordDetectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1581. {
  1582. cmDspRC_t rc = kOkDspRC;
  1583. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1584. if( kGateBaseCdId <= evt->dstVarId && evt->dstVarId < kGateBaseCdId + p->chCnt )
  1585. {
  1586. unsigned idx = evt->dstVarId - kGateBaseCdId;
  1587. if( p->chEnaV[idx] )
  1588. p->chGateV[ idx ] = cmDsvGetBool(evt->valuePtr);
  1589. //cmRptPrintf(ctx->rpt,"cd gate:%i e:%i v:%i\n",idx,p->chEnaV[idx],p->chGateV[idx]);
  1590. }
  1591. else
  1592. if( p->rmsBaseCdId <= evt->dstVarId && evt->dstVarId < p->rmsBaseCdId + p->chCnt )
  1593. {
  1594. unsigned idx = evt->dstVarId - p->rmsBaseCdId;
  1595. if( p->chEnaV[idx] )
  1596. p->chRmsV[ idx ] = cmDsvGetReal( evt->valuePtr );
  1597. }
  1598. else
  1599. {
  1600. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1601. {
  1602. switch( evt->dstVarId )
  1603. {
  1604. case kMaxTimeSpanCdId:
  1605. cmChordDetectSetSpanMs(p->cdp,cmDspDouble(inst,kMaxTimeSpanCdId));
  1606. break;
  1607. case kMinNoteCntCdId:
  1608. p->cdp->minNotesPerChord = cmDspUInt(inst,kMinNoteCntCdId);
  1609. break;
  1610. }
  1611. }
  1612. }
  1613. return rc;
  1614. }
  1615. struct cmDspClass_str* cmChordDetectClassCons( cmDspCtx_t* ctx )
  1616. {
  1617. cmDspClassSetup(&_cmChordDetectDC,ctx,"ChordDetect",
  1618. NULL,
  1619. _cmDspChordDetectAlloc,
  1620. _cmDspChordDetectFree,
  1621. _cmDspChordDetectReset,
  1622. _cmDspChordDetectExec,
  1623. _cmDspChordDetectRecv,
  1624. NULL,NULL,
  1625. "Chord detector.");
  1626. return &_cmChordDetectDC;
  1627. }
  1628. //==========================================================================================================================================
  1629. enum
  1630. {
  1631. kTimeFaId,
  1632. kGateFaId,
  1633. kInFaId,
  1634. kGainFaId,
  1635. kOutFaId
  1636. };
  1637. cmDspClass_t _cmFaderDC;
  1638. typedef struct
  1639. {
  1640. cmDspInst_t inst;
  1641. cmFader* fdp;
  1642. } cmDspFader_t;
  1643. cmDspInst_t* _cmDspFaderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1644. {
  1645. cmDspVarArg_t args[] =
  1646. {
  1647. { "time", kTimeFaId, 0, 0, kDoubleDsvFl | kOptArgDsvFl, "Fade time in milliseconds."},
  1648. { "gate", kGateFaId, 0, 0, kInDsvFl | kBoolDsvFl, "Gate control signal."},
  1649. { "in", kInFaId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input."},
  1650. { "gain", kGainFaId, 0, 0, kOutDsvFl | kDoubleDsvFl, "gain output."},
  1651. { "out", kOutFaId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio out."},
  1652. { NULL, 0, 0, 0, 0, NULL }
  1653. };
  1654. cmDspFader_t* p;
  1655. if((p = cmDspInstAlloc(cmDspFader_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  1656. return NULL;
  1657. double dfltFadeTimeMs = 100.0;
  1658. cmDspSetDefaultDouble( ctx, &p->inst, kTimeFaId, 0.0, dfltFadeTimeMs );
  1659. p->fdp = cmFaderAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), dfltFadeTimeMs );
  1660. return &p->inst;
  1661. }
  1662. cmDspRC_t _cmDspFaderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1663. {
  1664. cmDspFader_t* p = (cmDspFader_t*)inst;
  1665. cmFaderFree(&p->fdp);
  1666. return kOkDspRC;
  1667. }
  1668. cmDspRC_t _cmDspFaderReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1669. {
  1670. cmDspRC_t rc = kOkDspRC;
  1671. cmDspFader_t* p = (cmDspFader_t*)inst;
  1672. rc = cmDspApplyAllDefaults(ctx,inst);
  1673. cmDspZeroAudioBuf(ctx,inst,kOutFaId);
  1674. cmFaderSetFadeTime(p->fdp,cmDspDouble(inst,kTimeFaId));
  1675. return rc;
  1676. }
  1677. cmDspRC_t _cmDspFaderExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1678. {
  1679. cmDspRC_t rc = kOkDspRC;
  1680. cmDspFader_t* p = (cmDspFader_t*)inst;
  1681. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutFaId,0);
  1682. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutFaId,0);
  1683. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInFaId,0);
  1684. cmFaderExec(p->fdp,n,cmDspBool(inst,kGateFaId),false,ip,op);
  1685. cmDspSetDouble(ctx,inst,kGainFaId,p->fdp->gain);
  1686. return rc;
  1687. }
  1688. cmDspRC_t _cmDspFaderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1689. {
  1690. cmDspRC_t rc = kOkDspRC;
  1691. cmDspFader_t* p = (cmDspFader_t*)inst;
  1692. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1693. {
  1694. if( evt->dstVarId == kTimeFaId )
  1695. cmFaderSetFadeTime(p->fdp,cmDspDouble(inst,kTimeFaId));
  1696. }
  1697. return rc;
  1698. }
  1699. struct cmDspClass_str* cmFaderClassCons( cmDspCtx_t* ctx )
  1700. {
  1701. cmDspClassSetup(&_cmFaderDC,ctx,"Fader",
  1702. NULL,
  1703. _cmDspFaderAlloc,
  1704. _cmDspFaderFree,
  1705. _cmDspFaderReset,
  1706. _cmDspFaderExec,
  1707. _cmDspFaderRecv,
  1708. NULL,NULL,
  1709. "Audio fade in/out controller.");
  1710. return &_cmFaderDC;
  1711. }
  1712. //==========================================================================================================================================
  1713. enum
  1714. {
  1715. kChCntNsId,
  1716. kTrigNsId,
  1717. kDoneNsId,
  1718. kGateBaseNsId
  1719. };
  1720. enum
  1721. {
  1722. kGroupNonNsId,
  1723. kGroup0NsId,
  1724. kGroup1NsId
  1725. };
  1726. cmDspClass_t _cmNoteSelectDC;
  1727. typedef struct
  1728. {
  1729. cmDspInst_t inst;
  1730. unsigned chCnt;
  1731. unsigned rmsBaseNsId;
  1732. unsigned gate0BaseNsId;
  1733. unsigned gate1BaseNsId;
  1734. unsigned gate2BaseNsId;
  1735. unsigned gate3BaseNsId;
  1736. unsigned gate4BaseNsId;
  1737. bool* chGateV; // chGateV[chCnt]
  1738. cmReal_t* chRmsV; // chRmsV[ chCnt ];
  1739. unsigned* chGroupV; // chGroupV[ chCnt ] (0=non-chord 1=low/high 2=middle)
  1740. unsigned count;
  1741. unsigned doneSymId;
  1742. } cmDspNoteSelect_t;
  1743. cmDspInst_t* _cmDspNoteSelectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1744. {
  1745. cmDspVarArg_t args[] =
  1746. {
  1747. { "ch_cnt", kChCntNsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count."},
  1748. { "trig", kTrigNsId, 0, 0, kInDsvFl | kBoolDsvFl, "Trigger note selection."},
  1749. { "done", kDoneNsId, 0, 0, kOutDsvFl | kSymDsvFl, "Sends 'done' after new set of outputs have been sent."},
  1750. };
  1751. if( va_cnt < 1 )
  1752. {
  1753. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The note selector must be given a channel count argument .");
  1754. return NULL;
  1755. }
  1756. va_list vl1;
  1757. va_copy(vl1,vl);
  1758. unsigned CD0chanN = 0;
  1759. unsigned CD1chanN = 0;
  1760. unsigned* CD0chan = NULL;
  1761. unsigned* CD1chan = NULL;
  1762. const cmChar_t* CD0rsrc = "CD0chan";
  1763. const cmChar_t* CD1rsrc = "CD1chan";
  1764. if( cmDspRsrcUIntArray( ctx->dspH, &CD0chanN, &CD0chan, CD0rsrc, NULL ) != kOkDspRC )
  1765. {
  1766. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(CD0rsrc));
  1767. return NULL;
  1768. }
  1769. if( cmDspRsrcUIntArray( ctx->dspH, &CD1chanN, &CD1chan, CD1rsrc, NULL ) != kOkDspRC )
  1770. {
  1771. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(CD1rsrc));
  1772. return NULL;
  1773. }
  1774. unsigned chCnt = va_arg(vl,unsigned);
  1775. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1776. unsigned argCnt = fixArgCnt + 7*chCnt;
  1777. unsigned rmsBaseNsId = kGateBaseNsId + 1 * chCnt;
  1778. unsigned gate0BaseNsId = kGateBaseNsId + 2 * chCnt;
  1779. unsigned gate1BaseNsId = kGateBaseNsId + 3 * chCnt;
  1780. unsigned gate2BaseNsId = kGateBaseNsId + 4 * chCnt;
  1781. unsigned gate3BaseNsId = kGateBaseNsId + 5 * chCnt;
  1782. unsigned gate4BaseNsId = kGateBaseNsId + 6 * chCnt;
  1783. cmDspVarArg_t a[ argCnt+1 ];
  1784. unsigned i;
  1785. cmDspNoteSelect_t* p;
  1786. // setup the input gate detectors and the output gain args
  1787. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1788. cmDspArgSetupN(ctx, a, argCnt, kGateBaseNsId, chCnt, "gate", kGateBaseNsId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  1789. cmDspArgSetupN(ctx, a, argCnt, rmsBaseNsId, chCnt, "rms", rmsBaseNsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  1790. cmDspArgSetupN(ctx, a, argCnt, gate0BaseNsId, chCnt, "gate-0", gate0BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 0 output.");
  1791. cmDspArgSetupN(ctx, a, argCnt, gate1BaseNsId, chCnt, "gate-1", gate1BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 1 output.");
  1792. cmDspArgSetupN(ctx, a, argCnt, gate2BaseNsId, chCnt, "gate-2", gate2BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 2 output.");
  1793. cmDspArgSetupN(ctx, a, argCnt, gate3BaseNsId, chCnt, "gate-3", gate3BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 3 output.");
  1794. cmDspArgSetupN(ctx, a, argCnt, gate4BaseNsId, chCnt, "gate-4", gate4BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 4 output.");
  1795. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1796. if((p = cmDspInstAlloc(cmDspNoteSelect_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1797. return NULL;
  1798. cmDspSetDefaultBool( ctx, &p->inst, kTrigNsId, false, false );
  1799. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneNsId, cmInvalidId );
  1800. p->rmsBaseNsId = rmsBaseNsId;
  1801. p->gate0BaseNsId = gate0BaseNsId;
  1802. p->gate1BaseNsId = gate1BaseNsId;
  1803. p->gate2BaseNsId = gate2BaseNsId;
  1804. p->gate3BaseNsId = gate3BaseNsId;
  1805. p->gate4BaseNsId = gate4BaseNsId;
  1806. p->chCnt = chCnt;
  1807. p->chGateV = cmMemAllocZ(bool,chCnt);
  1808. p->chRmsV = cmMemAllocZ(cmReal_t,chCnt);
  1809. p->chGroupV = cmMemAllocZ(unsigned,chCnt);
  1810. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  1811. for(i=0; i<CD0chanN; ++i)
  1812. {
  1813. if( CD0chan[i] >= chCnt )
  1814. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The chord detector resource array '%s' value %i is out of range %i.",cmStringNullGuard(CD0rsrc),CD0chan[i],chCnt);
  1815. else
  1816. p->chGroupV[ CD0chan[i] ] = kGroup0NsId;
  1817. }
  1818. for(i=0; i<CD1chanN; ++i)
  1819. {
  1820. if( CD1chan[i] >= chCnt )
  1821. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The chord detector resource array '%s' value %i is out of range %i.",cmStringNullGuard(CD1rsrc),CD1chan[i],chCnt);
  1822. else
  1823. p->chGroupV[ CD1chan[i] ] = kGroup1NsId;
  1824. }
  1825. for(i=0; i<chCnt; ++i)
  1826. {
  1827. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseNsId+i, 0.0, 0.0 );
  1828. cmDspSetDefaultBool( ctx, &p->inst, gate0BaseNsId+i, false, false );
  1829. cmDspSetDefaultBool( ctx, &p->inst, gate1BaseNsId+i, false, false );
  1830. cmDspSetDefaultBool( ctx, &p->inst, gate2BaseNsId+i, false, false );
  1831. cmDspSetDefaultBool( ctx, &p->inst, gate3BaseNsId+i, false, false );
  1832. // the non-chord channel selections should always be on
  1833. cmDspSetDefaultBool( ctx, &p->inst, gate4BaseNsId+i, false, p->chGroupV[i] == kGroupNonNsId );
  1834. }
  1835. return &p->inst;
  1836. }
  1837. cmDspRC_t _cmDspNoteSelectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1838. {
  1839. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1840. cmMemFree(p->chGateV);
  1841. cmMemFree(p->chRmsV);
  1842. cmMemFree(p->chGroupV);
  1843. return kOkDspRC;
  1844. }
  1845. cmDspRC_t _cmDspNoteSelectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1846. {
  1847. cmDspRC_t rc = kOkDspRC;
  1848. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1849. rc = cmDspApplyAllDefaults(ctx,inst);
  1850. cmVOR_Zero(p->chRmsV,p->chCnt);
  1851. return rc;
  1852. }
  1853. cmDspRC_t _cmDspNoteSelectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1854. {
  1855. cmDspRC_t rc = kOkDspRC;
  1856. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1857. // store incoming gate values
  1858. if( kGateBaseNsId <= evt->dstVarId && evt->dstVarId < kGateBaseNsId + p->chCnt )
  1859. p->chGateV[ evt->dstVarId - kGateBaseNsId ] = cmDsvGetBool(evt->valuePtr);
  1860. else
  1861. // store incoming RMS values
  1862. if( p->rmsBaseNsId <= evt->dstVarId && evt->dstVarId < p->rmsBaseNsId + p->chCnt )
  1863. p->chRmsV[ evt->dstVarId - p->rmsBaseNsId ] = cmDsvGetReal( evt->valuePtr );
  1864. else
  1865. {
  1866. // if a chord detection was triggered
  1867. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && evt->dstVarId == kTrigNsId )
  1868. {
  1869. unsigned i;
  1870. cmReal_t maxRms = 0;
  1871. unsigned maxIdx = cmInvalidIdx;
  1872. for(i=1; i<p->chCnt; ++i)
  1873. {
  1874. // if this channel had an onset and is a possible chord note and is the max RMS chord note
  1875. if( p->chGroupV[i] != kGroupNonNsId && p->chGateV[i] && (maxIdx==cmInvalidIdx || p->chRmsV[i] > maxRms) )
  1876. {
  1877. maxRms = p->chRmsV[i];
  1878. maxIdx = i;
  1879. }
  1880. }
  1881. for(i=0; i<p->chCnt; ++i)
  1882. {
  1883. bool fl = p->chGroupV[i] != kGroupNonNsId;
  1884. bool chosenFl = fl && i==maxIdx;
  1885. bool otherFl = fl && i!=maxIdx && p->chGateV[i];
  1886. bool cd0Fl = p->chGroupV[i]==kGroup0NsId && (!otherFl) && (!chosenFl);
  1887. bool cd1Fl = p->chGroupV[i]==kGroup1NsId && (!otherFl) && (!chosenFl);
  1888. // gate set 0: set output gate for max chord note
  1889. cmDspSetBool(ctx,inst,p->gate0BaseNsId+i, chosenFl );
  1890. // gate set 1: set output gate for non-max chord notes
  1891. cmDspSetBool(ctx,inst,p->gate1BaseNsId+i, otherFl );
  1892. // gate set 2: set output gate for non-chord notes
  1893. cmDspSetBool(ctx,inst,p->gate2BaseNsId+i, cd0Fl );
  1894. // gate set 3: set output gate for non-chord notes
  1895. cmDspSetBool(ctx,inst,p->gate3BaseNsId+i, cd1Fl);
  1896. // gate set 4: set output gate for non-chord notes
  1897. cmDspSetBool(ctx,inst,p->gate4BaseNsId+i, !fl );
  1898. }
  1899. // send the 'done' symbol to notify the gate receivers that the
  1900. // new set of gates is complete
  1901. cmDspSetSymbol(ctx,inst,kDoneNsId, p->doneSymId);
  1902. // zero the RMS vector
  1903. cmVOR_Zero(p->chRmsV,p->chCnt);
  1904. }
  1905. }
  1906. return rc;
  1907. }
  1908. struct cmDspClass_str* cmNoteSelectClassCons( cmDspCtx_t* ctx )
  1909. {
  1910. cmDspClassSetup(&_cmNoteSelectDC,ctx,"NoteSelect",
  1911. NULL,
  1912. _cmDspNoteSelectAlloc,
  1913. _cmDspNoteSelectFree,
  1914. _cmDspNoteSelectReset,
  1915. NULL,
  1916. _cmDspNoteSelectRecv,
  1917. NULL,NULL,
  1918. "Chord detector.");
  1919. return &_cmNoteSelectDC;
  1920. }
  1921. //==========================================================================================================================================
  1922. enum
  1923. {
  1924. kTrigNnId,
  1925. kDoneNnId,
  1926. kGateBaseNnId
  1927. };
  1928. cmDspClass_t _cmNetNoteSelectDC;
  1929. #define _cmNetNoteSelPortCnt (10)
  1930. typedef struct
  1931. {
  1932. cmDspInst_t inst;
  1933. unsigned chCnt;
  1934. unsigned rmsBaseNnId;
  1935. unsigned gateBaseNNId[ _cmNetNoteSelPortCnt ];
  1936. bool* chGateV; // chGateV[chCnt]
  1937. cmReal_t* chRmsV; // chRmsV[ chCnt ];
  1938. unsigned* portChCntV; // portChCntV[ 10 ]
  1939. unsigned* portBaseIdV; // portBaseIdV[ 10 ]
  1940. unsigned* chPortV; // chPortV[ chCnt ]
  1941. unsigned* chPortIdxV; // chPortIdxV[ chCnt ]
  1942. unsigned* ncPortV; // ncPortV[ chCnt ]
  1943. unsigned* ncPortIdxV; // ncPortIdxV[ chCnt ]
  1944. unsigned count;
  1945. unsigned doneSymId;
  1946. } cmDspNetNoteSelect_t;
  1947. cmDspInst_t* _cmDspNetNoteSelectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1948. {
  1949. unsigned chCnt = 0;
  1950. const cmChar_t* label = NULL;
  1951. const cmChar_t* chPortRsrc = "nsChSelChV";
  1952. unsigned* chPortV = NULL;
  1953. const cmChar_t* chPortIdxRsrc = "nsChSelChIdxV";
  1954. unsigned* chPortIdxV = NULL;
  1955. const cmChar_t* ncPortRsrc = "nsNcSelChV";
  1956. unsigned* ncPortV = NULL;
  1957. const cmChar_t* ncPortIdxRsrc = "nsNcSelChIdxV";
  1958. unsigned* ncPortIdxV = NULL;
  1959. unsigned i,j,n;
  1960. cmDspVarArg_t args[] =
  1961. {
  1962. { "trig", kTrigNnId, 0, 0, kInDsvFl | kBoolDsvFl, "Trigger note selection."},
  1963. { "done", kDoneNnId, 0, 0, kOutDsvFl | kSymDsvFl, "Sends 'done' after new set of outputs have been sent."},
  1964. };
  1965. if( cmDspRsrcUIntArray( ctx->dspH, &chCnt, &chPortV, label = chPortRsrc, NULL ) != kOkDspRC )
  1966. {
  1967. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1968. return NULL;
  1969. }
  1970. if( cmDspRsrcUIntArray( ctx->dspH, &n, &chPortIdxV, label = chPortIdxRsrc, NULL ) != kOkDspRC )
  1971. {
  1972. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1973. return NULL;
  1974. }
  1975. assert(n == chCnt );
  1976. if( cmDspRsrcUIntArray( ctx->dspH, &n, &ncPortV, label = ncPortRsrc, NULL ) != kOkDspRC )
  1977. {
  1978. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1979. return NULL;
  1980. }
  1981. assert(n == chCnt );
  1982. if( cmDspRsrcUIntArray( ctx->dspH, &n, &ncPortIdxV, label = ncPortIdxRsrc, NULL ) != kOkDspRC )
  1983. {
  1984. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1985. return NULL;
  1986. }
  1987. assert(n == chCnt );
  1988. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1989. unsigned rmsBaseNnId = kGateBaseNnId + chCnt;
  1990. // get the count of ch's on each port
  1991. unsigned* portChCntV = cmLhAllocZ( ctx->lhH, unsigned, _cmNetNoteSelPortCnt );
  1992. unsigned* portBaseIdV = cmLhAllocZ( ctx->lhH, unsigned, _cmNetNoteSelPortCnt );
  1993. for(i=0; i<_cmNetNoteSelPortCnt; ++i)
  1994. {
  1995. // get the count of ch's in the ith gate output port
  1996. portChCntV[i] = cmVOU_Count( chPortV, chCnt, i ) + cmVOU_Count( ncPortV, chCnt, i );
  1997. // ports 1 and 6 are duplicates of ports 0 and 5
  1998. if( i == 1 || i == 6 )
  1999. portChCntV[i] = portChCntV[i-1];
  2000. // set the base port id for this port
  2001. if( i > 0 )
  2002. portBaseIdV[i] = portBaseIdV[i-1] + portChCntV[i-1];
  2003. else
  2004. portBaseIdV[ i ] = rmsBaseNnId + chCnt;
  2005. }
  2006. unsigned argCnt = fixArgCnt + (2*chCnt) + cmVOU_Sum(portChCntV,_cmNetNoteSelPortCnt );
  2007. cmDspVarArg_t a[ argCnt+1 ];
  2008. cmDspNetNoteSelect_t* p;
  2009. // setup the input gate detectors and the output gain args
  2010. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2011. cmDspArgSetupN(ctx, a, argCnt, kGateBaseNnId, chCnt, "gate", kGateBaseNnId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  2012. cmDspArgSetupN(ctx, a, argCnt, rmsBaseNnId, chCnt, "rms", rmsBaseNnId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  2013. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[0], portChCntV[0], "gate-0", portBaseIdV[0], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 0 output.");
  2014. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[1], portChCntV[1], "gate-1", portBaseIdV[1], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 1 output.");
  2015. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[2], portChCntV[2], "gate-2", portBaseIdV[2], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 2 output.");
  2016. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[3], portChCntV[3], "gate-3", portBaseIdV[3], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 3 output.");
  2017. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[4], portChCntV[4], "gate-4", portBaseIdV[4], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 4 output.");
  2018. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[5], portChCntV[5], "gate-5", portBaseIdV[5], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 5 output.");
  2019. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[6], portChCntV[6], "gate-6", portBaseIdV[6], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 6 output.");
  2020. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[7], portChCntV[7], "gate-7", portBaseIdV[7], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 7 output.");
  2021. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[8], portChCntV[8], "gate-8", portBaseIdV[8], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 8 output.");
  2022. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[9], portChCntV[9], "gate-9", portBaseIdV[9], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 9 output.");
  2023. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2024. if((p = cmDspInstAlloc(cmDspNetNoteSelect_t,ctx,classPtr,a,instSymId,id,storeSymId,0,vl)) == NULL )
  2025. return NULL;
  2026. cmDspSetDefaultBool( ctx, &p->inst, kTrigNnId, false, false );
  2027. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneNnId, cmInvalidId );
  2028. p->rmsBaseNnId = rmsBaseNnId;
  2029. p->chCnt = chCnt;
  2030. p->chGateV = cmMemAllocZ(bool,chCnt);
  2031. p->chRmsV = cmMemAllocZ(cmReal_t,chCnt);
  2032. p->portChCntV = portChCntV;
  2033. p->portBaseIdV = portBaseIdV;
  2034. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2035. p->chPortV = chPortV;
  2036. p->chPortIdxV = chPortIdxV;
  2037. p->ncPortV = ncPortV;
  2038. p->ncPortIdxV = ncPortIdxV;
  2039. for(i=0; i<chCnt; ++i)
  2040. {
  2041. cmDspSetDefaultBool( ctx, &p->inst, kGateBaseNnId+i, false, false );
  2042. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseNnId+i, 0.0, 0.0 );
  2043. }
  2044. for(i=0; i<_cmNetNoteSelPortCnt; ++i)
  2045. for(j=0; j<p->portChCntV[i]; ++j)
  2046. cmDspSetDefaultBool( ctx, &p->inst, p->portBaseIdV[i]+j, false, false );
  2047. return &p->inst;
  2048. }
  2049. cmDspRC_t _cmDspNetNoteSelectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2050. {
  2051. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2052. cmMemFree(p->chGateV);
  2053. cmMemFree(p->chRmsV);
  2054. return kOkDspRC;
  2055. }
  2056. cmDspRC_t _cmDspNetNoteSelectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2057. {
  2058. cmDspRC_t rc = kOkDspRC;
  2059. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2060. rc = cmDspApplyAllDefaults(ctx,inst);
  2061. cmVOR_Zero(p->chRmsV,p->chCnt);
  2062. return rc;
  2063. }
  2064. cmDspRC_t _cmDspNetNoteSelectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2065. {
  2066. cmDspRC_t rc = kOkDspRC;
  2067. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2068. // store incoming gate values
  2069. if( kGateBaseNnId <= evt->dstVarId && evt->dstVarId < kGateBaseNnId + p->chCnt )
  2070. {
  2071. p->chGateV[ evt->dstVarId - kGateBaseNnId ] = cmDsvGetBool(evt->valuePtr);
  2072. //unsigned idx = evt->dstVarId - kGateBaseNnId;
  2073. //cmRptPrintf(ctx->rpt,"ns gate:%i %i\n",idx, p->chGateV[ idx ]);
  2074. }
  2075. else
  2076. // store incoming RMS values
  2077. if( p->rmsBaseNnId <= evt->dstVarId && evt->dstVarId < p->rmsBaseNnId + p->chCnt )
  2078. {
  2079. p->chRmsV[ evt->dstVarId - p->rmsBaseNnId ] = cmDsvGetReal( evt->valuePtr );
  2080. }
  2081. else
  2082. {
  2083. // if a chord detection was triggered
  2084. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && evt->dstVarId == kTrigNnId )
  2085. {
  2086. unsigned i;
  2087. cmReal_t maxRms = 0;
  2088. unsigned maxIdx = cmInvalidIdx;
  2089. for(i=1; i<p->chCnt; ++i)
  2090. {
  2091. // if this channel had an onset and is a possible chord note and is the max RMS chord note
  2092. if( p->chGateV[i] && (maxIdx==cmInvalidIdx || p->chRmsV[i] > maxRms) )
  2093. {
  2094. maxRms = p->chRmsV[i];
  2095. maxIdx = i;
  2096. }
  2097. }
  2098. for(i=0; i<p->chCnt; ++i)
  2099. {
  2100. bool chosenFl = i==maxIdx;
  2101. bool otherFl = i!=maxIdx && p->chGateV[i];
  2102. bool nonFl = chosenFl==false && otherFl==false;
  2103. unsigned k;
  2104. // if this is a chord channel
  2105. if( p->chPortV[i] != cmInvalidIdx )
  2106. {
  2107. // get the port associated with this chord note
  2108. k = p->chPortV[i];
  2109. assert( k+1 < _cmNetNoteSelPortCnt );
  2110. assert( p->chPortIdxV[i] < p->portChCntV[k] && p->chPortIdxV[i] < p->portChCntV[k+1] );
  2111. // set the chosen and other gate outputs based on the state of
  2112. // chosenFl and otherFl
  2113. cmDspSetBool(ctx,inst,p->portBaseIdV[ k ] + p->chPortIdxV[i],chosenFl);
  2114. cmDspSetBool(ctx,inst,p->portBaseIdV[ k+1 ] + p->chPortIdxV[i],otherFl);
  2115. }
  2116. // all channels have a 'single' note channel
  2117. assert( p->ncPortV[i] != cmInvalidIdx );
  2118. k = p->ncPortV[i];
  2119. assert( k < _cmNetNoteSelPortCnt );
  2120. assert( p->ncPortIdxV[i] < p->portChCntV[k] );
  2121. cmDspSetBool(ctx,inst,p->portBaseIdV[k] + p->ncPortIdxV[i],nonFl);
  2122. }
  2123. // send the 'done' symbol to notify the gate receivers that the
  2124. // new set of gates is complete
  2125. cmDspSetSymbol(ctx,inst,kDoneNnId, p->doneSymId);
  2126. // zero the RMS vector
  2127. cmVOR_Zero(p->chRmsV,p->chCnt);
  2128. }
  2129. }
  2130. return rc;
  2131. }
  2132. struct cmDspClass_str* cmNetNoteSelectClassCons( cmDspCtx_t* ctx )
  2133. {
  2134. cmDspClassSetup(&_cmNetNoteSelectDC,ctx,"NetNoteSelect",
  2135. NULL,
  2136. _cmDspNetNoteSelectAlloc,
  2137. _cmDspNetNoteSelectFree,
  2138. _cmDspNetNoteSelectReset,
  2139. NULL,
  2140. _cmDspNetNoteSelectRecv,
  2141. NULL,NULL,
  2142. "Chord detector.");
  2143. return &_cmNetNoteSelectDC;
  2144. }
  2145. //==========================================================================================================================================
  2146. enum
  2147. {
  2148. kBypassCfId,
  2149. kMinHzCfId,
  2150. kFbFlCfId,
  2151. kHzCfId,
  2152. kAlphaCfId,
  2153. kInCfId,
  2154. kOutCfId
  2155. };
  2156. cmDspClass_t _cmCombFiltDC;
  2157. typedef struct
  2158. {
  2159. cmDspInst_t inst;
  2160. cmCombFilt* cfp;
  2161. } cmDspCombFilt_t;
  2162. cmDspInst_t* _cmDspCombFiltAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2163. {
  2164. cmDspVarArg_t args[] =
  2165. {
  2166. { "bypass",kBypassCfId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable flag." },
  2167. { "minhz", kMinHzCfId, 0, 0, kDoubleDsvFl | kReqArgDsvFl, "Minimum frequency limit."},
  2168. { "fb", kFbFlCfId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Configure the filter in feedback mode."},
  2169. { "hz", kHzCfId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Lowest comb frequency." },
  2170. { "alpha", kAlphaCfId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Filter coefficent."},
  2171. { "in", kInCfId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input."},
  2172. { "out", kOutCfId, 0, 1, kOutDsvFl| kAudioBufDsvFl, "Audio out."},
  2173. { NULL, 0, 0, 0, 0, NULL }
  2174. };
  2175. cmDspCombFilt_t* p;
  2176. if((p = cmDspInstAlloc(cmDspCombFilt_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  2177. return NULL;
  2178. p->cfp = cmCombFiltAlloc(ctx->cmProcCtx, NULL,
  2179. cmDspSampleRate(ctx),
  2180. cmDspBool(&p->inst,kFbFlCfId),
  2181. cmDspDouble(&p->inst,kMinHzCfId),
  2182. cmDspDouble(&p->inst,kAlphaCfId),
  2183. cmDspDouble(&p->inst,kHzCfId),
  2184. cmDspBool(&p->inst,kBypassCfId));
  2185. return &p->inst;
  2186. }
  2187. cmDspRC_t _cmDspCombFiltFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2188. {
  2189. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2190. cmCombFiltFree(&p->cfp);
  2191. return kOkDspRC;
  2192. }
  2193. cmDspRC_t _cmDspCombFiltReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2194. {
  2195. cmDspRC_t rc = kOkDspRC;
  2196. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2197. rc = cmDspApplyAllDefaults(ctx,inst);
  2198. cmDspZeroAudioBuf(ctx,inst,kOutCfId);
  2199. cmCombFiltInit(p->cfp,
  2200. cmDspSampleRate(ctx),
  2201. cmDspBool(inst,kFbFlCfId),
  2202. cmDspDouble(inst,kMinHzCfId),
  2203. cmDspDouble(inst,kAlphaCfId),
  2204. cmDspDouble(inst,kHzCfId),
  2205. cmDspBool(inst,kBypassCfId));
  2206. return rc;
  2207. }
  2208. cmDspRC_t _cmDspCombFiltExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2209. {
  2210. cmDspRC_t rc = kOkDspRC;
  2211. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2212. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutCfId,0);
  2213. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutCfId,0);
  2214. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInCfId,0);
  2215. cmCombFiltExec(p->cfp,ip,op,n);
  2216. return rc;
  2217. }
  2218. cmDspRC_t _cmDspCombFiltRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2219. {
  2220. cmDspRC_t rc = kOkDspRC;
  2221. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2222. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2223. {
  2224. switch( evt->dstVarId )
  2225. {
  2226. case kHzCfId:
  2227. cmCombFiltSetHz(p->cfp,cmDspDouble(inst,evt->dstVarId));
  2228. //printf("%s hz:%f\n",cmSymTblLabel(ctx->stH,inst->symId),cmDspDouble(inst,evt->dstVarId));
  2229. break;
  2230. case kAlphaCfId:
  2231. cmCombFiltSetAlpha(p->cfp,cmDspDouble(inst,evt->dstVarId));
  2232. break;
  2233. case kBypassCfId:
  2234. p->cfp->bypassFl = cmDspBool(inst,evt->dstVarId);
  2235. break;
  2236. }
  2237. }
  2238. return rc;
  2239. }
  2240. struct cmDspClass_str* cmCombFiltClassCons( cmDspCtx_t* ctx )
  2241. {
  2242. cmDspClassSetup(&_cmCombFiltDC,ctx,"CombFilt",
  2243. NULL,
  2244. _cmDspCombFiltAlloc,
  2245. _cmDspCombFiltFree,
  2246. _cmDspCombFiltReset,
  2247. _cmDspCombFiltExec,
  2248. _cmDspCombFiltRecv,
  2249. NULL,NULL,
  2250. "Comb Filter");
  2251. return &_cmCombFiltDC;
  2252. }
  2253. //==========================================================================================================================================
  2254. enum
  2255. {
  2256. kPortCntSoId,
  2257. kOpSoId,
  2258. kOutSoId,
  2259. kBaseOpdSoId
  2260. };
  2261. cmDspClass_t _cmScalarOpDC;
  2262. struct cmDspScalarOp_str;
  2263. typedef cmDspRC_t (*_cmDspScalarOpFunc_t)(cmDspCtx_t* ctx, cmDspInst_t* inst );
  2264. typedef struct cmDspScalar_str
  2265. {
  2266. cmDspInst_t inst;
  2267. _cmDspScalarOpFunc_t func;
  2268. unsigned inPortCnt;
  2269. } cmDspScalarOp_t;
  2270. cmDspRC_t _cmDspScalarOpFuncMult(cmDspCtx_t* ctx, cmDspInst_t* inst )
  2271. {
  2272. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2273. double val = 1.0;
  2274. unsigned i;
  2275. for(i=0; i<p->inPortCnt; ++i)
  2276. val *= cmDspDouble( inst, kBaseOpdSoId+i );
  2277. cmDspSetDouble( ctx, inst, kOutSoId, val );
  2278. return kOkDspRC;
  2279. }
  2280. cmDspRC_t _cmDspScalarOpFuncAdd(cmDspCtx_t* ctx, cmDspInst_t* inst )
  2281. {
  2282. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2283. double val = 0;
  2284. unsigned i;
  2285. for(i=0; i<p->inPortCnt; ++i)
  2286. val += cmDspDouble( inst, kBaseOpdSoId+i );
  2287. cmDspSetDouble( ctx, inst, kOutSoId, val );
  2288. return kOkDspRC;
  2289. }
  2290. // var args syntax: "<in_port_cnt> <op_string> <opd_label_0> <opd_dflt_val_0> <opd_label_1> <opd_dflt_val_1> ... <opd_label_n> <opd_dflt_val_n>
  2291. cmDspInst_t* _cmDspScalarOpAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2292. {
  2293. cmDspVarArg_t args[] =
  2294. {
  2295. { "cnt", kPortCntSoId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input port count" },
  2296. { "op", kOpSoId, 0, 0, kStrzDsvFl | kReqArgDsvFl, "Operation symbol as a string."},
  2297. { "out", kOutSoId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Operation output."},
  2298. };
  2299. cmDspScalarOp_t* p;
  2300. if( va_cnt < 2 )
  2301. {
  2302. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ScalarOp' constructor must have a count of input ports and operation identifier string.");
  2303. return NULL;
  2304. }
  2305. va_list vl1;
  2306. va_copy(vl1,vl);
  2307. unsigned inPortCnt = va_arg(vl,unsigned);
  2308. const cmChar_t* opIdStr = va_arg(vl,const cmChar_t*);
  2309. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  2310. unsigned argCnt = fixArgCnt + inPortCnt;
  2311. cmDspVarArg_t a[ argCnt+1 ];
  2312. double dfltVal[ inPortCnt ];
  2313. unsigned i;
  2314. _cmDspScalarOpFunc_t fp = NULL;
  2315. // validate the count of input ports
  2316. if( inPortCnt == 0 )
  2317. {
  2318. cmDspClassErr(ctx,classPtr,kVarNotValidDspRC,"The 'ScalarOp' constructor input port argument must be non-zero.");
  2319. return NULL;
  2320. }
  2321. // locate the operation function
  2322. if( strcmp(opIdStr,"*") == 0 )
  2323. fp = _cmDspScalarOpFuncMult;
  2324. else
  2325. if( strcmp(opIdStr,"+") == 0 )
  2326. fp = _cmDspScalarOpFuncAdd;
  2327. // validate the operation function
  2328. if( fp == NULL )
  2329. {
  2330. cmDspClassErr(ctx,classPtr,kVarNotValidDspRC,"The 'ScalarOp' constructor operation string id '%s' did not match a known operation.",cmStringNullGuard(opIdStr));
  2331. return NULL;
  2332. }
  2333. // setup the fixed args
  2334. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2335. for(i=0; i<inPortCnt; ++i)
  2336. {
  2337. // get the operand label
  2338. const cmChar_t* label = va_arg(vl,const cmChar_t*);
  2339. // get the operand default value
  2340. dfltVal[i] = va_arg(vl,double);
  2341. // setup the arg recd
  2342. cmDspArgSetup(ctx,a + fixArgCnt + i, label, cmInvalidId, kBaseOpdSoId+i,0,0,kDoubleDsvFl|kInDsvFl,"Operand");
  2343. }
  2344. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2345. if((p = cmDspInstAlloc(cmDspScalarOp_t,ctx,classPtr,a,instSymId,id,storeSymId,2,vl1)) == NULL )
  2346. return NULL;
  2347. for(i=0; i<inPortCnt; ++i)
  2348. cmDspSetDefaultDouble(ctx,&p->inst,kBaseOpdSoId+i,0.0,dfltVal[i]);
  2349. p->inPortCnt = inPortCnt;
  2350. p->func = fp;
  2351. return &p->inst;
  2352. }
  2353. cmDspRC_t _cmDspScalarOpReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2354. {
  2355. cmDspRC_t rc = kOkDspRC;
  2356. rc = cmDspApplyAllDefaults(ctx,inst);
  2357. return rc;
  2358. }
  2359. cmDspRC_t _cmDspScalarOpRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2360. {
  2361. cmDspRC_t rc = kOkDspRC;
  2362. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2363. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2364. {
  2365. if( evt->dstVarId == kBaseOpdSoId )
  2366. p->func(ctx,inst);
  2367. }
  2368. return rc;
  2369. }
  2370. struct cmDspClass_str* cmScalarOpClassCons( cmDspCtx_t* ctx )
  2371. {
  2372. cmDspClassSetup(&_cmScalarOpDC,ctx,"ScalarOp",
  2373. NULL,
  2374. _cmDspScalarOpAlloc,
  2375. NULL,
  2376. _cmDspScalarOpReset,
  2377. NULL,
  2378. _cmDspScalarOpRecv,
  2379. NULL,NULL,
  2380. "Scalar Operations");
  2381. return &_cmScalarOpDC;
  2382. }
  2383. //==========================================================================================================================================
  2384. enum
  2385. {
  2386. kChCntGsId,
  2387. kGroupCntGsId,
  2388. kChsPerGroupGsId,
  2389. kBaseGateGsId,
  2390. };
  2391. cmDspClass_t _cmGroupSelDC;
  2392. typedef struct
  2393. {
  2394. cmDspInst_t inst;
  2395. unsigned chCnt;
  2396. unsigned groupCnt;
  2397. cmGroupSel* gsp;
  2398. unsigned baseRmsGsId;
  2399. unsigned baseOutGsId;
  2400. } cmDspGroupSel_t;
  2401. cmDspInst_t* _cmDspGroupSelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2402. {
  2403. cmDspVarArg_t args[] =
  2404. {
  2405. { "chCnt", kChCntGsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count." },
  2406. { "groupCnt", kGroupCntGsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Group count." },
  2407. { "chsPerGroup", kChsPerGroupGsId, 0, 0, kUIntDsvFl | kInDsvFl | kReqArgDsvFl, "Channels per group." }
  2408. };
  2409. if( va_cnt < 2 )
  2410. {
  2411. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'GroupSel' constructor must have a channel and group count.");
  2412. return NULL;
  2413. }
  2414. va_list vl1;
  2415. va_copy(vl1,vl);
  2416. cmDspGroupSel_t* p;
  2417. unsigned i;
  2418. unsigned chCnt = va_arg(vl,unsigned);
  2419. unsigned groupCnt = va_arg(vl,unsigned);
  2420. unsigned outCnt = chCnt * groupCnt;
  2421. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  2422. unsigned baseRmsGsId = kBaseGateGsId + chCnt;
  2423. unsigned baseOutGsId = baseRmsGsId + chCnt;
  2424. unsigned argCnt = baseOutGsId + outCnt;
  2425. cmDspVarArg_t a[ argCnt + 1 ];
  2426. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2427. cmDspArgSetupN(ctx, a, argCnt, kBaseGateGsId, chCnt, "gate", kBaseGateGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  2428. cmDspArgSetupN(ctx, a, argCnt, baseRmsGsId, chCnt, "rms", baseRmsGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  2429. for(i=0; i<groupCnt; ++i)
  2430. {
  2431. int labelCharCnt = 31;
  2432. char label[ labelCharCnt + 1 ];
  2433. snprintf(label,labelCharCnt,"gate-%i",i);
  2434. cmDspArgSetupN(ctx, a, argCnt, baseOutGsId + (i*chCnt), chCnt, label, baseOutGsId + (i*chCnt), 0, 0, kOutDsvFl | kBoolDsvFl, "Output gates");
  2435. }
  2436. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2437. if((p = cmDspInstAlloc(cmDspGroupSel_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  2438. return NULL;
  2439. p->chCnt = chCnt;
  2440. p->groupCnt = groupCnt;
  2441. p->gsp = cmGroupSelAlloc(ctx->cmProcCtx, NULL, 0, 0, 0 );
  2442. p->baseRmsGsId = baseRmsGsId;
  2443. p->baseOutGsId = baseOutGsId;
  2444. for(i=0; i<chCnt; ++i)
  2445. {
  2446. cmDspSetDefaultBool( ctx, &p->inst, kBaseGateGsId, false, false );
  2447. cmDspSetDefaultDouble(ctx,&p->inst, baseRmsGsId, 0.0, 0.0 );
  2448. }
  2449. for(i=0; i<outCnt; ++i)
  2450. cmDspSetDefaultBool( ctx, &p->inst, baseOutGsId, false, false );
  2451. return &p->inst;
  2452. }
  2453. cmDspRC_t _cmDspGroupSelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2454. {
  2455. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2456. cmGroupSelFree(&p->gsp);
  2457. return kOkDspRC;
  2458. }
  2459. cmDspRC_t _cmDspGroupSelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2460. {
  2461. cmDspRC_t rc = kOkDspRC;
  2462. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2463. rc = cmDspApplyAllDefaults(ctx,inst);
  2464. cmGroupSelInit(p->gsp,p->chCnt,p->groupCnt,cmDspUInt(&p->inst,kChsPerGroupGsId));
  2465. return rc;
  2466. }
  2467. cmDspRC_t _cmDspGroupSelExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2468. {
  2469. cmDspRC_t rc = kOkDspRC;
  2470. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2471. if( cmGroupSelExec(p->gsp) == cmOkRC && p->gsp->updateFl )
  2472. {
  2473. unsigned i,j;
  2474. for(i=0; i<p->groupCnt; ++i)
  2475. {
  2476. cmGroupSelGrp* gp = p->gsp->groupArray + i;
  2477. if( gp->releaseFl )
  2478. {
  2479. for(j=0; j<gp->chIdxCnt; ++j)
  2480. cmDspSetBool(ctx,inst,p->baseOutGsId + (i*p->chCnt) + gp->chIdxArray[j], false);
  2481. }
  2482. if( gp->createFl )
  2483. {
  2484. for(j=0; j<gp->chIdxCnt; ++j)
  2485. cmDspSetBool(ctx,inst,p->baseOutGsId + (i*p->chCnt) + gp->chIdxArray[j], true);
  2486. }
  2487. }
  2488. }
  2489. return rc;
  2490. }
  2491. cmDspRC_t _cmDspGroupSelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2492. {
  2493. cmDspRC_t rc = kOkDspRC;
  2494. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2495. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2496. {
  2497. if( evt->dstVarId == kChsPerGroupGsId )
  2498. p->gsp->chsPerGroup = cmDspUInt(inst, kChsPerGroupGsId );
  2499. else
  2500. if( kBaseGateGsId <= evt->dstVarId && evt->dstVarId < (kBaseGateGsId + p->chCnt) )
  2501. cmGroupSetChannelGate(p->gsp, evt->dstVarId - kBaseGateGsId, cmDspDouble(inst,evt->dstVarId));
  2502. else
  2503. if( p->baseRmsGsId <= evt->dstVarId && evt->dstVarId < (p->baseRmsGsId + p->chCnt) )
  2504. cmGroupSetChannelRMS(p->gsp, evt->dstVarId - p->baseRmsGsId, cmDspDouble(inst,evt->dstVarId));
  2505. }
  2506. return rc;
  2507. }
  2508. struct cmDspClass_str* cmGroupSelClassCons( cmDspCtx_t* ctx )
  2509. {
  2510. cmDspClassSetup(&_cmGroupSelDC,ctx,"GroupSel",
  2511. NULL,
  2512. _cmDspGroupSelAlloc,
  2513. _cmDspGroupSelFree,
  2514. _cmDspGroupSelReset,
  2515. _cmDspGroupSelExec,
  2516. _cmDspGroupSelRecv,
  2517. NULL,NULL,
  2518. "Group selector.");
  2519. return &_cmGroupSelDC;
  2520. }
  2521. //==========================================================================================================================================
  2522. enum
  2523. {
  2524. kInChCntNmId,
  2525. kOutChCntNmId,
  2526. kFadeTimeNmId,
  2527. kBaseGateNmId,
  2528. };
  2529. cmDspClass_t _cmAudioNofM_DC;
  2530. typedef struct
  2531. {
  2532. cmDspInst_t inst;
  2533. unsigned inChCnt;
  2534. unsigned outChCnt;
  2535. cmAudioNofM* nmp;
  2536. unsigned baseInNmId;
  2537. unsigned baseOutNmId;
  2538. unsigned baseGainNmId;
  2539. } cmDspAudioNofM_t;
  2540. cmDspInst_t* _cmDspAudioNofM_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2541. {
  2542. if( va_cnt < 2 )
  2543. {
  2544. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AudioNofM' constructor must given input and output channel counts.");
  2545. return NULL;
  2546. }
  2547. va_list vl1;
  2548. va_copy(vl1,vl);
  2549. int inChCnt = va_arg(vl,int);
  2550. int outChCnt = va_arg(vl,int);
  2551. unsigned baseInNmId = kBaseGateNmId + inChCnt;
  2552. unsigned baseOutNmId = baseInNmId + inChCnt;
  2553. unsigned baseGainNmId= baseOutNmId + outChCnt;
  2554. unsigned i;
  2555. cmDspAudioNofM_t* p = cmDspInstAllocV(cmDspAudioNofM_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2556. 1, "ichs", kInChCntNmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input channel count.",
  2557. 1, "ochs", kOutChCntNmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Output channel count.",
  2558. 1, "time", kFadeTimeNmId, 0, 0, kDoubleDsvFl | kOptArgDsvFl | kInDsvFl, "Fade time in milliseconds.",
  2559. inChCnt, "gate", kBaseGateNmId, 0, 0, kBoolDsvFl | kInDsvFl, "Gate inputs.",
  2560. inChCnt, "in", baseInNmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2561. outChCnt, "out", baseOutNmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2562. outChCnt, "gain", baseGainNmId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Gain output",
  2563. 0 );
  2564. cmDspSetDefaultDouble( ctx, &p->inst, kFadeTimeNmId, 0.0, 25.0 );
  2565. p->inChCnt = inChCnt;
  2566. p->outChCnt = outChCnt;
  2567. p->nmp = cmAudioNofMAlloc(ctx->cmProcCtx,NULL,0,0,0,0);
  2568. p->baseInNmId = baseInNmId;
  2569. p->baseOutNmId = baseOutNmId;
  2570. p->baseGainNmId = baseGainNmId;
  2571. for(i=0; i<outChCnt; ++i)
  2572. cmDspSetDefaultDouble( ctx, &p->inst, baseGainNmId + i, 0.0, 0.0 );
  2573. return &p->inst;
  2574. }
  2575. cmDspRC_t _cmDspAudioNofM_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2576. {
  2577. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2578. cmAudioNofMFree(&p->nmp);
  2579. return kOkDspRC;
  2580. }
  2581. cmDspRC_t _cmDspAudioNofM_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2582. {
  2583. cmDspRC_t rc = kOkDspRC;
  2584. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2585. unsigned i;
  2586. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  2587. {
  2588. for(i=0; i<p->outChCnt; ++i)
  2589. cmDspZeroAudioBuf(ctx,inst,p->baseOutNmId+i);
  2590. cmAudioNofMInit(p->nmp, cmDspSampleRate(ctx), p->inChCnt, p->outChCnt, cmDspDouble(&p->inst, kFadeTimeNmId ));
  2591. }
  2592. return rc;
  2593. }
  2594. cmDspRC_t _cmDspAudioNofM_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2595. {
  2596. cmDspRC_t rc = kOkDspRC;
  2597. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2598. unsigned i;
  2599. const cmSample_t* x[ p->inChCnt ];
  2600. cmSample_t* y[ p->outChCnt ];
  2601. unsigned n;
  2602. for(i=0; i<p->inChCnt; ++i)
  2603. {
  2604. if( i==0 )
  2605. n = cmDspAudioBufSmpCount(ctx,inst,p->baseInNmId+i,0);
  2606. else
  2607. { assert( n == cmDspAudioBufSmpCount(ctx,inst,p->baseInNmId+i,0)); }
  2608. x[i] = cmDspAudioBuf(ctx,inst,p->baseInNmId+i,0);
  2609. }
  2610. for(i=0; i<p->outChCnt; ++i)
  2611. {
  2612. y[i] = cmDspAudioBuf(ctx,inst,p->baseOutNmId+i,0);
  2613. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->baseOutNmId+i,0));
  2614. cmVOS_Zero(y[i],n);
  2615. }
  2616. cmAudioNofMExec(p->nmp,x,p->inChCnt,y,p->outChCnt,n);
  2617. for(i=0; i<p->outChCnt; ++i)
  2618. {
  2619. cmAudioNofM_In* ip = p->nmp->outArray[i].list;
  2620. double v = 0;
  2621. for(; ip != NULL; ip=ip->link)
  2622. v += ip->fader->gain;
  2623. cmDspSetDouble(ctx, inst,p->baseGainNmId + i,v );
  2624. }
  2625. return rc;
  2626. }
  2627. cmDspRC_t _cmDspAudioNofM_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2628. {
  2629. cmDspRC_t rc = kOkDspRC;
  2630. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2631. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2632. {
  2633. if( kBaseGateNmId <= evt->dstVarId && evt->dstVarId <= kBaseGateNmId + p->inChCnt )
  2634. cmAudioNofMSetChannelGate( p->nmp, evt->dstVarId - kBaseGateNmId, cmDspBool(inst,evt->dstVarId) );
  2635. }
  2636. return rc;
  2637. }
  2638. struct cmDspClass_str* cmAudioNofMClassCons( cmDspCtx_t* ctx )
  2639. {
  2640. cmDspClassSetup(&_cmAudioNofM_DC,ctx,"AudioNofM",
  2641. NULL,
  2642. _cmDspAudioNofM_Alloc,
  2643. _cmDspAudioNofM_Free,
  2644. _cmDspAudioNofM_Reset,
  2645. _cmDspAudioNofM_Exec,
  2646. _cmDspAudioNofM_Recv,
  2647. NULL,NULL,
  2648. "Audio N of M Switch");
  2649. return &_cmAudioNofM_DC;
  2650. }
  2651. //==========================================================================================================================================
  2652. enum
  2653. {
  2654. kInChCntRmId,
  2655. kBypassRmId,
  2656. kGainRmId,
  2657. kOutRmId,
  2658. kBaseInRmId
  2659. };
  2660. cmDspClass_t _cmRingModDC;
  2661. typedef struct
  2662. {
  2663. cmDspInst_t inst;
  2664. unsigned inChCnt;
  2665. } cmDspRingMod_t;
  2666. cmDspInst_t* _cmDspRingModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2667. {
  2668. if( va_cnt < 1 )
  2669. {
  2670. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RingMod' constructor must given an input channel counts.");
  2671. return NULL;
  2672. }
  2673. va_list vl1;
  2674. va_copy(vl1,vl);
  2675. int inChCnt = va_arg(vl,int);
  2676. cmDspRingMod_t* p = cmDspInstAllocV(cmDspRingMod_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2677. 1, "ichs", kInChCntRmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input channel count.",
  2678. 1, "bypass",kBypassRmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable",
  2679. 1, "gain", kGainRmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain (default:1.0)",
  2680. 1, "out", kOutRmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2681. inChCnt, "in", kBaseInRmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2682. 0 );
  2683. cmDspSetDefaultBool( ctx, &p->inst, kBypassRmId, false, false );
  2684. cmDspSetDefaultDouble( ctx, &p->inst, kGainRmId, 0.0, 1.0 );
  2685. p->inChCnt = inChCnt;
  2686. return &p->inst;
  2687. }
  2688. cmDspRC_t _cmDspRingModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2689. {
  2690. cmDspRC_t rc = kOkDspRC;
  2691. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  2692. {
  2693. cmDspZeroAudioBuf(ctx,inst,kOutRmId);
  2694. }
  2695. return rc;
  2696. }
  2697. cmDspRC_t _cmDspRingModExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2698. {
  2699. cmDspRC_t rc = kOkDspRC;
  2700. cmDspRingMod_t* p = (cmDspRingMod_t*)inst;
  2701. unsigned i,j;
  2702. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutRmId, 0);
  2703. const cmSample_t* x0 = cmDspAudioBuf(ctx,inst,kBaseInRmId,0);
  2704. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutRmId,0);
  2705. double gain = cmDspDouble(inst,kGainRmId);
  2706. bool bypassFl = cmDspBool(inst,kBypassRmId);
  2707. for(i=1; i<p->inChCnt; ++i)
  2708. {
  2709. assert( n == cmDspAudioBufSmpCount(ctx,inst,kBaseInRmId+i,0));
  2710. const cmSample_t* x1 = cmDspAudioBuf(ctx,inst,kBaseInRmId+i,0);
  2711. if( bypassFl )
  2712. {
  2713. for(j=0; j<n; ++j)
  2714. y[j] = x0[j] + x1[j];
  2715. }
  2716. else
  2717. {
  2718. for(j=0; j<n; ++j)
  2719. y[j] = x0[j] * x1[j] * gain;
  2720. }
  2721. x0 = y;
  2722. }
  2723. return rc;
  2724. }
  2725. cmDspRC_t _cmDspRingModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2726. {
  2727. return cmDspSetEvent(ctx,inst,evt);
  2728. }
  2729. struct cmDspClass_str* cmRingModClassCons( cmDspCtx_t* ctx )
  2730. {
  2731. cmDspClassSetup(&_cmRingModDC,ctx,"RingMod",
  2732. NULL,
  2733. _cmDspRingModAlloc,
  2734. NULL,
  2735. _cmDspRingModReset,
  2736. _cmDspRingModExec,
  2737. _cmDspRingModRecv,
  2738. NULL,NULL,
  2739. "Ring modulator");
  2740. return &_cmRingModDC;
  2741. }
  2742. //==========================================================================================================================================
  2743. enum
  2744. {
  2745. kMaxCntMdId,
  2746. kDelayMdId,
  2747. kInMdId,
  2748. kOutMdId,
  2749. };
  2750. cmDspClass_t _cmMsgDelayDC;
  2751. typedef struct cmDspMsgDelayEle_str
  2752. {
  2753. unsigned outTimeSmp;
  2754. cmDspValue_t value;
  2755. struct cmDspMsgDelayEle_str* link;
  2756. } cmDspMsgDelayEle_t;
  2757. typedef struct
  2758. {
  2759. cmDspInst_t inst;
  2760. unsigned maxCnt;
  2761. cmDspMsgDelayEle_t* array; // array[maxCnt];
  2762. cmDspMsgDelayEle_t* avail;
  2763. cmDspMsgDelayEle_t* active;
  2764. } cmDspMsgDelay_t;
  2765. cmDspInst_t* _cmDspMsgDelayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2766. {
  2767. cmDspMsgDelay_t* p = cmDspInstAllocV(cmDspMsgDelay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2768. 1, "maxcnt", kMaxCntMdId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Maximum count of elements in the delay",
  2769. 1, "delay", kDelayMdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Delay time in millisecond.",
  2770. 1, "in", kInMdId, 0, 0, kInDsvFl | kTypeDsvMask, "Msg input",
  2771. 1, "out", kOutMdId, 0, 0, kOutDsvFl | kTypeDsvMask, "Msg input",
  2772. 0 );
  2773. if( p == NULL )
  2774. return NULL;
  2775. cmDspSetDefaultDouble( ctx, &p->inst, kDelayMdId, 0.0, 0.0 );
  2776. cmDspSetDefaultBool( ctx, &p->inst, kOutMdId, false, false );
  2777. p->maxCnt = cmDspUInt(&p->inst,kMaxCntMdId);
  2778. p->array = cmMemAllocZ(cmDspMsgDelayEle_t, p->maxCnt );
  2779. return &p->inst;
  2780. }
  2781. cmDspRC_t _cmDspMsgDelayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2782. {
  2783. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2784. cmMemFree(p->array);
  2785. return kOkDspRC;
  2786. }
  2787. // insert a value in the delay list
  2788. cmDspRC_t _cmDspMsgDelayInsert( cmDspCtx_t* ctx, cmDspMsgDelay_t* p, unsigned delayTimeSmp, const cmDspValue_t* valPtr )
  2789. {
  2790. cmDspRC_t rc = kOkDspRC;
  2791. // protect against pre-reset calls
  2792. if( p->avail == NULL && p->active == NULL )
  2793. return kOkDspRC;
  2794. // if there are no available delay elements
  2795. if( p->avail == NULL )
  2796. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The message delay has exhausted it's internal message store.");
  2797. // we only do the simplest kind of copying to avoid allocating memory
  2798. // TODO: fix this
  2799. if( cmDsvIsMtx(valPtr) || cmIsFlag(valPtr->flags,kProxyDsvFl))
  2800. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The message delay cannot yet store matrix or proxy types.");
  2801. // get a pointer to the next available element
  2802. cmDspMsgDelayEle_t* np = p->avail;
  2803. // remove the new ele from the avail list
  2804. p->avail = np->link;
  2805. // calc the new ele's exec time
  2806. np->outTimeSmp = ctx->cycleCnt * cmDspSamplesPerCycle(ctx) + delayTimeSmp;
  2807. // copy the msg value into the delay line element
  2808. // TODO: this should be a real copy that supports all types
  2809. np->value = *valPtr;
  2810. cmDspMsgDelayEle_t* ep = p->active;
  2811. cmDspMsgDelayEle_t* pp = NULL;
  2812. // if the active list is empty ...
  2813. if( ep == NULL )
  2814. {
  2815. // ... make the avail element the first on the list
  2816. p->active = np;
  2817. np->link = NULL;
  2818. }
  2819. else
  2820. {
  2821. // iterate through the list and find the active links which
  2822. // the new ele falls between based on its execution time
  2823. while(ep != NULL )
  2824. {
  2825. // ep's exec time is greater than the new ele's exec time
  2826. if( ep->outTimeSmp > np->outTimeSmp )
  2827. {
  2828. // insert the new ele in the active list before 'ep'
  2829. if( pp == NULL )
  2830. {
  2831. np->link = p->active;
  2832. p->active = np;
  2833. }
  2834. else
  2835. {
  2836. np->link = pp->link;
  2837. pp->link = np;
  2838. }
  2839. break;
  2840. }
  2841. pp = ep;
  2842. ep = ep->link;
  2843. }
  2844. // if the new element is last on the list
  2845. if( ep == NULL )
  2846. {
  2847. assert(pp != NULL && pp->link == NULL);
  2848. pp->link = np;
  2849. np->link = NULL;
  2850. }
  2851. }
  2852. return rc;
  2853. }
  2854. cmDspRC_t _cmDspMsgDelayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2855. {
  2856. cmDspRC_t rc = kOkDspRC;
  2857. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2858. if((rc = cmDspApplyDefault(ctx,inst,kDelayMdId)) == kOkDspRC )
  2859. {
  2860. unsigned i;
  2861. unsigned maxCnt = cmDspUInt(inst,kMaxCntMdId);
  2862. p->active = NULL;
  2863. p->avail = NULL;
  2864. // put all ele's on the available list
  2865. for(i=0; i<maxCnt; ++i)
  2866. {
  2867. p->array[i].link = p->avail;
  2868. p->avail = p->array + i;
  2869. }
  2870. }
  2871. return rc;
  2872. }
  2873. cmDspRC_t _cmDspMsgDelayExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* e )
  2874. {
  2875. cmDspRC_t rc = kOkDspRC;
  2876. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2877. unsigned framesPerCycle = cmDspSamplesPerCycle(ctx);
  2878. unsigned begTimeSmp = ctx->cycleCnt * framesPerCycle;
  2879. unsigned endTimeSmp = begTimeSmp + framesPerCycle;
  2880. while( p->active != NULL )
  2881. {
  2882. if( p->active->outTimeSmp >= endTimeSmp )
  2883. break;
  2884. cmDspMsgDelayEle_t* ep = p->active;
  2885. // remove the element from the active list and place it on the available list.
  2886. p->active = p->active->link; // advance the active list
  2887. ep->link = p->avail; // put the cur. element on the avail list
  2888. p->avail = ep; //
  2889. // output the element value
  2890. if((rc = cmDspValueSet(ctx,inst,kOutMdId,&ep->value,0)) != kOkDspRC )
  2891. return cmDspInstErr(ctx,inst,rc,"Message delay output failed.");
  2892. }
  2893. return rc;
  2894. }
  2895. cmDspRC_t _cmDspMsgDelayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2896. {
  2897. cmDspRC_t rc = kOkDspRC;
  2898. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2899. switch( evt->dstVarId )
  2900. {
  2901. case kDelayMdId:
  2902. rc = cmDspSetEvent(ctx,inst,evt);
  2903. break;
  2904. case kInMdId:
  2905. {
  2906. unsigned delayTimeSmp = floor(cmDspDouble(&p->inst,kDelayMdId) * cmDspSampleRate(ctx) / 1000.0);
  2907. rc = _cmDspMsgDelayInsert(ctx,p,delayTimeSmp,evt->valuePtr);
  2908. }
  2909. break;
  2910. }
  2911. return rc;
  2912. }
  2913. struct cmDspClass_str* cmMsgDelayClassCons( cmDspCtx_t* ctx )
  2914. {
  2915. cmDspClassSetup(&_cmMsgDelayDC,ctx,"MsgDelay",
  2916. NULL,
  2917. _cmDspMsgDelayAlloc,
  2918. _cmDspMsgDelayFree,
  2919. _cmDspMsgDelayReset,
  2920. _cmDspMsgDelayExec,
  2921. _cmDspMsgDelayRecv,
  2922. NULL,NULL,
  2923. "Message Delay");
  2924. return &_cmMsgDelayDC;
  2925. }
  2926. //==========================================================================================================================================
  2927. enum
  2928. {
  2929. kBegLnId,
  2930. kEndLnId,
  2931. kDurLnId,
  2932. kCmdLnId,
  2933. kOutLnId,
  2934. };
  2935. cmDspClass_t _cmLineDC;
  2936. typedef struct cmDspLineEle_str
  2937. {
  2938. unsigned outTimeSmp;
  2939. cmDspValue_t value;
  2940. struct cmDspLineEle_str* link;
  2941. } cmDspLineEle_t;
  2942. typedef struct
  2943. {
  2944. cmDspInst_t inst;
  2945. unsigned onSymId;
  2946. unsigned offSymId;
  2947. unsigned resetSymId;
  2948. unsigned curSmpIdx;
  2949. bool onFl;
  2950. } cmDspLine_t;
  2951. cmDspInst_t* _cmDspLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2952. {
  2953. cmDspLine_t* p = cmDspInstAllocV(cmDspLine_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2954. 1, "beg", kBegLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Begin value.",
  2955. 1, "end", kEndLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "End value.",
  2956. 1, "dur", kDurLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Duration (ms)",
  2957. 1, "cmd", kCmdLnId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on | off | reset",
  2958. 1, "out", kOutLnId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  2959. 0 );
  2960. if( p == NULL )
  2961. return NULL;
  2962. cmDspSetDefaultDouble( ctx, &p->inst, kOutLnId, 0.0, cmDspDefaultDouble(&p->inst,kBegLnId) );
  2963. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2964. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2965. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"reset");
  2966. return &p->inst;
  2967. }
  2968. cmDspRC_t _cmDspLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2969. {
  2970. return kOkDspRC;
  2971. }
  2972. cmDspRC_t _cmDspLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2973. {
  2974. cmDspLine_t* p = (cmDspLine_t*)inst;
  2975. p->curSmpIdx = 0;
  2976. p->onFl = false;
  2977. return cmDspApplyAllDefaults(ctx,inst);
  2978. }
  2979. cmDspRC_t _cmDspLineExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* e )
  2980. {
  2981. cmDspRC_t rc = kOkDspRC;
  2982. cmDspLine_t* p = (cmDspLine_t*)inst;
  2983. if( p->onFl == false )
  2984. return kOkDspRC;
  2985. double beg = cmDspDouble(inst,kBegLnId);
  2986. double end = cmDspDouble(inst,kEndLnId);
  2987. double ms = cmDspDouble(inst,kDurLnId);
  2988. double durSmpCnt = floor(ms * cmDspSampleRate(ctx) / 1000);
  2989. double out = beg + (end - beg) * p->curSmpIdx / durSmpCnt;
  2990. if( beg < end )
  2991. {
  2992. if( out >= end )
  2993. {
  2994. out = end;
  2995. p->onFl = false;
  2996. }
  2997. }
  2998. else
  2999. {
  3000. if( out <= end )
  3001. {
  3002. out = end;
  3003. p->onFl = false;
  3004. }
  3005. }
  3006. cmDspSetDouble(ctx,inst,kOutLnId,out);
  3007. p->curSmpIdx += cmDspSamplesPerCycle(ctx);
  3008. return rc;
  3009. }
  3010. cmDspRC_t _cmDspLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3011. {
  3012. cmDspRC_t rc = kOkDspRC;
  3013. cmDspLine_t* p = (cmDspLine_t*)inst;
  3014. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3015. {
  3016. switch( evt->dstVarId )
  3017. {
  3018. case kCmdLnId:
  3019. {
  3020. unsigned symId = cmDspSymbol(inst,kCmdLnId);
  3021. if( symId == p->onSymId )
  3022. p->onFl = true;
  3023. else
  3024. if( symId == p->offSymId )
  3025. p->onFl = false;
  3026. else
  3027. if( symId == p->resetSymId )
  3028. {
  3029. p->curSmpIdx = 0;
  3030. p->onFl = true;
  3031. }
  3032. }
  3033. break;
  3034. }
  3035. }
  3036. return rc;
  3037. }
  3038. struct cmDspClass_str* cmLineClassCons( cmDspCtx_t* ctx )
  3039. {
  3040. cmDspClassSetup(&_cmLineDC,ctx,"Line",
  3041. NULL,
  3042. _cmDspLineAlloc,
  3043. _cmDspLineFree,
  3044. _cmDspLineReset,
  3045. _cmDspLineExec,
  3046. _cmDspLineRecv,
  3047. NULL,NULL,
  3048. "Line");
  3049. return &_cmLineDC;
  3050. }
  3051. //==========================================================================================================================================
  3052. enum
  3053. {
  3054. kTrigModeAdId,
  3055. kMinLvlAdId,
  3056. kDlyMsAdId,
  3057. kAtkMsAdId,
  3058. kAtkLvlAdId,
  3059. kDcyMsAdId,
  3060. kSusLvlAdId,
  3061. kSusMsAdId,
  3062. kRlsMsAdId,
  3063. kTScaleAdId,
  3064. kAScaleAdId,
  3065. kGateAdId,
  3066. kRmsAdId,
  3067. kOutAdId,
  3068. kCmdAdId
  3069. };
  3070. cmDspClass_t _cmAdsrDC;
  3071. typedef struct
  3072. {
  3073. cmDspInst_t inst;
  3074. cmAdsr* p;
  3075. } cmDspAdsr_t;
  3076. cmDspInst_t* _cmDspAdsrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3077. {
  3078. cmDspAdsr_t* p = cmDspInstAllocV(cmDspAdsr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3079. 1, "trig", kTrigModeAdId, 0, 0, kBoolDsvFl | kInDsvFl | kOptArgDsvFl, "Trigger mode (offset ignored).",
  3080. 1, "min", kMinLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Minimum level (dflt:0.0).",
  3081. 1, "dly", kDlyMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Delay milliseconds.",
  3082. 1, "atk", kAtkMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Attack milliseconds.",
  3083. 1, "alvl", kAtkLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Attack Level.",
  3084. 1, "dcy", kDcyMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Decay milliseconds.",
  3085. 1, "sus", kSusLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Sustain Level.",
  3086. 1, "hold", kSusMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Sustain Ms (trig mode only).",
  3087. 1, "rls", kRlsMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Release milliseconds.",
  3088. 1, "tscale",kTScaleAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "Time scale.",
  3089. 1, "ascale",kAScaleAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "Amplitude scale.",
  3090. 1, "gate", kGateAdId, 0, 0, kBoolDsvFl | kInDsvFl, "Gate input.",
  3091. 1, "rms", kRmsAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "RMS input.",
  3092. 1, "out", kOutAdId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Level output.",
  3093. 1, "cmd", kCmdAdId, 0, 0, kSymDsvFl | kInDsvFl, "Command input.",
  3094. 0 );
  3095. cmDspSetDefaultBool( ctx, &p->inst, kTrigModeAdId, false, false );
  3096. cmDspSetDefaultDouble( ctx, &p->inst, kMinLvlAdId, 0.0, 0.0 );
  3097. cmDspSetDefaultDouble( ctx, &p->inst, kDlyMsAdId, 0.0, 0.0 );
  3098. cmDspSetDefaultDouble( ctx, &p->inst, kAtkMsAdId, 0.0, 5.0 );
  3099. cmDspSetDefaultDouble( ctx, &p->inst, kAtkLvlAdId, 0.0, 1.0 );
  3100. cmDspSetDefaultDouble( ctx, &p->inst, kDcyMsAdId, 0.0, 10.0 );
  3101. cmDspSetDefaultDouble( ctx, &p->inst, kSusLvlAdId, 0.0, 0.8 );
  3102. cmDspSetDefaultDouble( ctx, &p->inst, kSusMsAdId, 0.0, 50.0);
  3103. cmDspSetDefaultDouble( ctx, &p->inst, kRlsMsAdId, 0.0, 20.0 );
  3104. cmDspSetDefaultDouble( ctx, &p->inst, kTScaleAdId, 0.0, 1.0 );
  3105. cmDspSetDefaultDouble( ctx, &p->inst, kAScaleAdId, 0.0, 1.0 );
  3106. cmDspSetDefaultBool( ctx, &p->inst, kGateAdId, false,false);
  3107. cmDspSetDefaultDouble( ctx, &p->inst, kRmsAdId, 0.0, 0.0);
  3108. cmDspSetDefaultDouble( ctx, &p->inst, kOutAdId, 0.0, cmDspDouble(&p->inst,kMinLvlAdId));
  3109. p->p = cmAdsrAlloc(ctx->cmProcCtx,NULL,false,0,0,0,0,0,0,0,0,0);
  3110. return &p->inst;
  3111. }
  3112. cmDspRC_t _cmDspAdsrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3113. {
  3114. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3115. cmAdsrFree(&p->p);
  3116. return kOkDspRC;
  3117. }
  3118. cmDspRC_t _cmDspAdsrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3119. {
  3120. cmDspRC_t rc = kOkDspRC;
  3121. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3122. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3123. {
  3124. bool trigFl= cmDspBool( inst, kTrigModeAdId );
  3125. cmReal_t minL = cmDspDouble( inst, kMinLvlAdId );
  3126. cmReal_t dlyMs = cmDspDouble( inst, kDlyMsAdId );
  3127. cmReal_t atkMs = cmDspDouble( inst, kAtkMsAdId );
  3128. cmReal_t atkL = cmDspDouble( inst, kAtkLvlAdId );
  3129. cmReal_t dcyMs = cmDspDouble( inst, kDcyMsAdId );
  3130. cmReal_t susMs = cmDspDouble( inst, kSusMsAdId );
  3131. cmReal_t susL = cmDspDouble( inst, kSusLvlAdId );
  3132. cmReal_t rlsMs = cmDspDouble( inst, kRlsMsAdId );
  3133. cmAdsrInit( p->p, cmDspSampleRate(ctx), trigFl, minL, dlyMs, atkMs, atkL, dcyMs, susMs, susL, rlsMs );
  3134. }
  3135. return rc;
  3136. }
  3137. cmDspRC_t _cmDspAdsrExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3138. {
  3139. cmDspRC_t rc = kOkDspRC;
  3140. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3141. bool gateFl = cmDspBool( inst, kGateAdId );
  3142. //double rms = cmDspDouble(inst,kRmsAdId);
  3143. double tscale = cmDspDouble(inst,kTScaleAdId);
  3144. double ascale = cmDspDouble(inst,kAScaleAdId);
  3145. //
  3146. // HACK HACK HACK HACK
  3147. // HACK HACK HACK HACK
  3148. // HACK HACK HACK HACK see the accompanying hack in cmProc3.c cmAdsrExec()
  3149. // HACK HACK HACK HACK
  3150. // HACK HACK HACK HACK
  3151. //
  3152. /*
  3153. double db = rms<0.00001 ? -100.0 : 20.0*log10(rms);
  3154. double dbMax = -15.0;
  3155. double dbMin = -58.0;
  3156. db = cmMin(dbMax,cmMax(dbMin,db));
  3157. double scale = (db - dbMin) / (dbMax-dbMin);
  3158. */
  3159. cmReal_t out = cmAdsrExec( p->p, cmDspSamplesPerCycle(ctx), gateFl, tscale, ascale );
  3160. rc = cmDspSetDouble( ctx, inst, kOutAdId, out );
  3161. return rc;
  3162. }
  3163. cmDspRC_t _cmDspAdsrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3164. {
  3165. cmDspRC_t rc = kOkDspRC;
  3166. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3167. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3168. return rc;
  3169. if( evt->dstVarId == kCmdAdId )
  3170. {
  3171. cmAdsrReport(p->p,ctx->rpt);
  3172. return rc;
  3173. }
  3174. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3175. switch( evt->dstVarId )
  3176. {
  3177. case kTrigModeAdId:
  3178. p->p->trigModeFl = cmDspBool(inst, kTrigModeAdId);
  3179. break;
  3180. case kMinLvlAdId:
  3181. cmAdsrSetLevel(p->p, v, kDlyAdsrId );
  3182. break;
  3183. case kDlyMsAdId:
  3184. cmAdsrSetTime(p->p, v, kDlyAdsrId );
  3185. break;
  3186. case kAtkMsAdId:
  3187. cmAdsrSetTime(p->p, v, kAtkAdsrId );
  3188. break;
  3189. case kAtkLvlAdId:
  3190. cmAdsrSetLevel(p->p, v, kAtkAdsrId );
  3191. break;
  3192. case kDcyMsAdId:
  3193. cmAdsrSetTime(p->p, v, kDcyAdsrId );
  3194. break;
  3195. case kSusMsAdId:
  3196. cmAdsrSetTime(p->p, v, kSusAdsrId );
  3197. break;
  3198. case kSusLvlAdId:
  3199. cmAdsrSetLevel(p->p, v, kSusAdsrId );
  3200. break;
  3201. case kRlsMsAdId:
  3202. cmAdsrSetTime(p->p, v, kRlsAdsrId );
  3203. break;
  3204. }
  3205. return rc;
  3206. }
  3207. struct cmDspClass_str* cmAdsrClassCons( cmDspCtx_t* ctx )
  3208. {
  3209. cmDspClassSetup(&_cmAdsrDC,ctx,"Adsr",
  3210. NULL,
  3211. _cmDspAdsrAlloc,
  3212. _cmDspAdsrFree,
  3213. _cmDspAdsrReset,
  3214. _cmDspAdsrExec,
  3215. _cmDspAdsrRecv,
  3216. NULL,NULL,
  3217. "ADSR Envelope Generator");
  3218. return &_cmAdsrDC;
  3219. }
  3220. //==========================================================================================================================================
  3221. enum
  3222. {
  3223. kBypassCmId,
  3224. kThreshDbCmId,
  3225. kRatioCmId,
  3226. kAtkMsCmId,
  3227. kRlsMsCmId,
  3228. kInGainCmId,
  3229. kOutGainCmId,
  3230. kWndMsCmId,
  3231. kMaxWndMsCmId,
  3232. kInCmId,
  3233. kOutCmId,
  3234. kEnvCmId
  3235. };
  3236. cmDspClass_t _cmCompressorDC;
  3237. typedef struct
  3238. {
  3239. cmDspInst_t inst;
  3240. cmCompressor* p;
  3241. } cmDspCompressor_t;
  3242. cmDspInst_t* _cmDspCompressorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3243. {
  3244. cmDspCompressor_t* p = cmDspInstAllocV(cmDspCompressor_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3245. 1, "bypass",kBypassCmId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable",
  3246. 1, "thr", kThreshDbCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Threshold in dB.",
  3247. 1, "ratio", kRatioCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Ratio numerator.",
  3248. 1, "atk", kAtkMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Attack milliseconds",
  3249. 1, "rls", kRlsMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Release milliseconds",
  3250. 1, "igain", kInGainCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain.",
  3251. 1, "ogain", kOutGainCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Makeup Gain",
  3252. 1, "wnd", kWndMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "RMS window milliseconds.",
  3253. 1, "maxwnd",kMaxWndMsCmId, 0, 0, kDoubleDsvFl | kOptArgDsvFl, "Max. RMS window milliseconds.",
  3254. 1, "in", kInCmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3255. 1, "out", kOutCmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3256. 1, "env", kEnvCmId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Envelope out",
  3257. 0 );
  3258. p->p = cmCompressorAlloc(ctx->cmProcCtx, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false );
  3259. cmDspSetDefaultBool( ctx, &p->inst, kBypassCmId, false, false );
  3260. cmDspSetDefaultDouble( ctx, &p->inst, kAtkMsCmId, 0.0, 20.0 );
  3261. cmDspSetDefaultDouble( ctx, &p->inst, kRlsMsCmId, 0.0, 20.0 );
  3262. cmDspSetDefaultDouble( ctx, &p->inst, kInGainCmId, 0.0, 1.0 );
  3263. cmDspSetDefaultDouble( ctx, &p->inst, kOutGainCmId, 0.0, 1.0 );
  3264. cmDspSetDefaultDouble( ctx, &p->inst, kWndMsCmId, 0.0, 200.0);
  3265. cmDspSetDefaultDouble( ctx, &p->inst, kMaxWndMsCmId, 0.0, 1000.0);
  3266. cmDspSetDefaultDouble( ctx, &p->inst, kEnvCmId, 0.0, 0.0 );
  3267. return &p->inst;
  3268. }
  3269. cmDspRC_t _cmDspCompressorFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3270. {
  3271. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3272. cmCompressorFree(&p->p);
  3273. return kOkDspRC;
  3274. }
  3275. cmDspRC_t _cmDspCompressorReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3276. {
  3277. cmDspRC_t rc = kOkDspRC;
  3278. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3279. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3280. {
  3281. cmDspZeroAudioBuf(ctx,inst,kOutCmId);
  3282. cmReal_t threshDb = cmDspDouble(inst, kThreshDbCmId );
  3283. cmReal_t ratio = cmDspDouble(inst, kRatioCmId );
  3284. cmReal_t atkMs = cmDspDouble(inst, kAtkMsCmId );
  3285. cmReal_t rlsMs = cmDspDouble(inst, kRlsMsCmId );
  3286. cmReal_t inGain = cmDspDouble(inst, kInGainCmId );
  3287. cmReal_t outGain = cmDspDouble(inst, kOutGainCmId );
  3288. cmReal_t wndMs = cmDspDouble(inst, kWndMsCmId );
  3289. cmReal_t maxWndMs = cmDspDouble(inst, kMaxWndMsCmId);
  3290. bool bypassFl = cmDspBool( inst, kBypassCmId );
  3291. cmCompressorInit(p->p,cmDspSampleRate(ctx),cmDspSamplesPerCycle(ctx), inGain, maxWndMs, wndMs, threshDb, ratio, atkMs, rlsMs, outGain, bypassFl );
  3292. }
  3293. return rc;
  3294. }
  3295. cmDspRC_t _cmDspCompressorExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3296. {
  3297. cmDspRC_t rc = kOkDspRC;
  3298. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3299. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutCmId, 0);
  3300. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInCmId,0);
  3301. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutCmId,0);
  3302. if( x != NULL )
  3303. {
  3304. cmCompressorExec(p->p,x,y,n);
  3305. rc = cmDspSetDouble( ctx, inst, kEnvCmId, p->p->gain );
  3306. }
  3307. return rc;
  3308. }
  3309. cmDspRC_t _cmDspCompressorRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3310. {
  3311. cmDspRC_t rc;
  3312. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3313. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3314. return rc;
  3315. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3316. switch( evt->dstVarId )
  3317. {
  3318. case kThreshDbCmId:
  3319. cmCompressorSetThreshDb(p->p,v);
  3320. break;
  3321. case kRatioCmId:
  3322. p->p->ratio_num = v;
  3323. break;
  3324. case kAtkMsCmId:
  3325. cmCompressorSetAttackMs(p->p,v);
  3326. break;
  3327. case kRlsMsCmId:
  3328. cmCompressorSetReleaseMs(p->p,v);
  3329. break;
  3330. case kInGainCmId:
  3331. p->p->inGain = v;
  3332. break;
  3333. case kOutGainCmId:
  3334. p->p->outGain = v;
  3335. break;
  3336. case kWndMsCmId:
  3337. cmCompressorSetRmsWndMs(p->p,v);
  3338. break;
  3339. case kBypassCmId:
  3340. p->p->bypassFl = cmDspBool(inst,kBypassCmId);
  3341. break;
  3342. }
  3343. return rc;
  3344. }
  3345. struct cmDspClass_str* cmCompressorClassCons( cmDspCtx_t* ctx )
  3346. {
  3347. cmDspClassSetup(&_cmCompressorDC,ctx,"Compressor",
  3348. NULL,
  3349. _cmDspCompressorAlloc,
  3350. _cmDspCompressorFree,
  3351. _cmDspCompressorReset,
  3352. _cmDspCompressorExec,
  3353. _cmDspCompressorRecv,
  3354. NULL,NULL,
  3355. "Compressor");
  3356. return &_cmCompressorDC;
  3357. }
  3358. //==========================================================================================================================================
  3359. enum
  3360. {
  3361. kBypassBqId,
  3362. kModeBqId,
  3363. kF0HzBqId,
  3364. kQBqId,
  3365. kGainDbBqId,
  3366. kInBqId,
  3367. kOutBqId
  3368. };
  3369. cmDspClass_t _cmBiQuadEqDC;
  3370. typedef struct
  3371. {
  3372. const cmChar_t* label;
  3373. unsigned mode;
  3374. unsigned symbol;
  3375. } cmDspBiQuadMap_t;
  3376. cmDspBiQuadMap_t _cmDspBiQuadMap[] =
  3377. {
  3378. {"LP", kLpfBqId, cmInvalidId },
  3379. {"HP", kHpFBqId, cmInvalidId },
  3380. {"BP", kBpfBqId, cmInvalidId },
  3381. {"Notch", kNotchBqId, cmInvalidId },
  3382. {"AP", kAllpassBqId, cmInvalidId },
  3383. {"Peak", kPeakBqId, cmInvalidId },
  3384. {"LSh", kLowShelfBqId, cmInvalidId },
  3385. {"HSh", kHighShelfBqId, cmInvalidId },
  3386. { NULL, cmInvalidId, cmInvalidId }
  3387. };
  3388. typedef struct
  3389. {
  3390. cmDspInst_t inst;
  3391. cmBiQuadEq* p;
  3392. } cmDspBiQuadEq_t;
  3393. cmDspInst_t* _cmDspBiQuadEqAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3394. {
  3395. cmDspBiQuadEq_t* p = cmDspInstAllocV(cmDspBiQuadEq_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3396. 1, "bypass",kBypassBqId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable.",
  3397. 1, "mode", kModeBqId, 0, 0, kInDsvFl | kSymDsvFl | kReqArgDsvFl, "Mode Symbol: LP|HP|BP|AP|Notch|Pk|LSh|HSh.",
  3398. 1, "f0", kF0HzBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Center or edge frequecy in Hz.",
  3399. 1, "Q", kQBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Q",
  3400. 1, "gain", kGainDbBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Gain Db (Pk,LSh,Hsh only)",
  3401. 1, "in", kInBqId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3402. 1, "out", kOutBqId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3403. 0 );
  3404. cmDspSetDefaultDouble( ctx, &p->inst, kGainDbBqId, 0.0, 1.0 );
  3405. p->p = cmBiQuadEqAlloc(ctx->cmProcCtx,NULL,0,0,0,0,0,false);
  3406. // register the filter mode symbols
  3407. unsigned i;
  3408. for(i=0; _cmDspBiQuadMap[i].label != NULL; ++i)
  3409. if( _cmDspBiQuadMap[i].symbol == cmInvalidId )
  3410. _cmDspBiQuadMap[i].symbol = cmSymTblRegisterStaticSymbol(ctx->stH,_cmDspBiQuadMap[i].label);
  3411. return &p->inst;
  3412. }
  3413. cmDspRC_t _cmDspBiQuadEqFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3414. {
  3415. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3416. cmBiQuadEqFree(&p->p);
  3417. return kOkDspRC;
  3418. }
  3419. unsigned _cmDspBiQuadEqModeId( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned modeSymId )
  3420. {
  3421. unsigned i;
  3422. for(i=0; _cmDspBiQuadMap[i].label!=NULL; ++i)
  3423. if( _cmDspBiQuadMap[i].symbol == modeSymId )
  3424. return _cmDspBiQuadMap[i].mode;
  3425. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The mode string '%s' is not valid.",cmStringNullGuard(cmSymTblLabel(ctx->stH,modeSymId)));
  3426. return cmInvalidId;
  3427. }
  3428. cmDspRC_t _cmDspBiQuadEqReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3429. {
  3430. cmDspRC_t rc = kOkDspRC;
  3431. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3432. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3433. {
  3434. cmDspZeroAudioBuf(ctx,inst,kOutBqId);
  3435. unsigned modeSymId = cmDspSymbol(inst, kModeBqId );
  3436. cmReal_t f0Hz = cmDspDouble(inst, kF0HzBqId );
  3437. cmReal_t Q = cmDspDouble(inst, kQBqId );
  3438. cmReal_t gainDb = cmDspDouble(inst, kGainDbBqId );
  3439. unsigned mode = _cmDspBiQuadEqModeId(ctx,inst,modeSymId );
  3440. bool bypassFl = cmDspBool(inst, kBypassBqId );
  3441. if( mode != cmInvalidId )
  3442. cmBiQuadEqInit(p->p, cmDspSampleRate(ctx), mode, f0Hz, Q, gainDb, bypassFl );
  3443. }
  3444. return rc;
  3445. }
  3446. cmDspRC_t _cmDspBiQuadEqExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3447. {
  3448. cmDspRC_t rc = kOkDspRC;
  3449. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3450. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutBqId, 0);
  3451. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInBqId,0);
  3452. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutBqId,0);
  3453. cmBiQuadEqExec(p->p,x,y,n);
  3454. return rc;
  3455. }
  3456. cmDspRC_t _cmDspBiQuadEqRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3457. {
  3458. cmDspRC_t rc;
  3459. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3460. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3461. return rc;
  3462. cmReal_t f0Hz = p->p->f0Hz;
  3463. cmReal_t Q = p->p->Q;
  3464. cmReal_t gainDb = p->p->gainDb;
  3465. unsigned mode = p->p->mode;
  3466. if( evt->dstVarId == kModeBqId )
  3467. {
  3468. if((mode = _cmDspBiQuadEqModeId(ctx,inst,cmDspSymbol(inst,kModeBqId) )) == cmInvalidId )
  3469. rc = kVarNotValidDspRC;
  3470. }
  3471. else
  3472. {
  3473. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3474. switch( evt->dstVarId )
  3475. {
  3476. case kF0HzBqId:
  3477. f0Hz = v;
  3478. break;
  3479. case kQBqId:
  3480. Q = v;
  3481. break;
  3482. case kGainDbBqId:
  3483. gainDb = v;
  3484. break;
  3485. case kBypassBqId:
  3486. p->p->bypassFl = cmDspBool(inst,kBypassBqId);
  3487. break;
  3488. }
  3489. }
  3490. cmBiQuadEqSet(p->p,mode,f0Hz,Q,gainDb);
  3491. return rc;
  3492. }
  3493. struct cmDspClass_str* cmBiQuadEqClassCons( cmDspCtx_t* ctx )
  3494. {
  3495. cmDspClassSetup(&_cmBiQuadEqDC,ctx,"BiQuadEq",
  3496. NULL,
  3497. _cmDspBiQuadEqAlloc,
  3498. _cmDspBiQuadEqFree,
  3499. _cmDspBiQuadEqReset,
  3500. _cmDspBiQuadEqExec,
  3501. _cmDspBiQuadEqRecv,
  3502. NULL,NULL,
  3503. "Bi-Quad EQ Filters");
  3504. return &_cmBiQuadEqDC;
  3505. }
  3506. //==========================================================================================================================================
  3507. enum
  3508. {
  3509. kBypassDsId,
  3510. kInGainDsId,
  3511. kSrateDsId,
  3512. kBitsDsId,
  3513. kRectDsId,
  3514. kFullDsId,
  3515. kClipDbDsId,
  3516. kOutGainDsId,
  3517. kInDsId,
  3518. kOutDsId
  3519. };
  3520. cmDspClass_t _cmDistDsDC;
  3521. typedef struct
  3522. {
  3523. cmDspInst_t inst;
  3524. cmDistDs* p;
  3525. } cmDspDistDs_t;
  3526. cmDspInst_t* _cmDspDistDsAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3527. {
  3528. cmDspDistDs_t* p = cmDspInstAllocV(cmDspDistDs_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3529. 1, "bypass", kBypassDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable.",
  3530. 1, "igain", kInGainDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain.",
  3531. 1, "srate", kSrateDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Down-sample rate.",
  3532. 1, "bits", kBitsDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Bits per sample",
  3533. 1, "rect", kRectDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Rectify flag",
  3534. 1, "full", kFullDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "1=Full 0=Half rectify",
  3535. 1, "clip", kClipDbDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Clip dB",
  3536. 1, "ogain", kOutGainDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain",
  3537. 1, "in", kInDsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3538. 1, "out", kOutDsId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3539. 0 );
  3540. cmDspSetDefaultBool( ctx, &p->inst, kBypassDsId, false, false );
  3541. cmDspSetDefaultDouble( ctx, &p->inst, kInGainDsId, 0.0, 1.0 );
  3542. cmDspSetDefaultDouble( ctx, &p->inst, kSrateDsId, 0.0, cmDspSampleRate(ctx));
  3543. cmDspSetDefaultDouble( ctx, &p->inst, kBitsDsId, 0.0, 24.0 );
  3544. cmDspSetDefaultBool( ctx, &p->inst, kRectDsId, false, false );
  3545. cmDspSetDefaultBool( ctx, &p->inst, kFullDsId, false, false );
  3546. cmDspSetDefaultDouble( ctx, &p->inst, kClipDbDsId, 0.0, 0.0 );
  3547. cmDspSetDefaultDouble( ctx, &p->inst, kOutGainDsId, 0.0, 1.0 );
  3548. p->p = cmDistDsAlloc(ctx->cmProcCtx, NULL, 0, 0, 0, 0, false, false, 0, 0, false );
  3549. return &p->inst;
  3550. }
  3551. cmDspRC_t _cmDspDistDsFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3552. {
  3553. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3554. cmDistDsFree(&p->p);
  3555. return kOkDspRC;
  3556. }
  3557. cmDspRC_t _cmDspDistDsReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3558. {
  3559. cmDspRC_t rc = kOkDspRC;
  3560. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3561. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3562. {
  3563. cmDspZeroAudioBuf(ctx,inst,kOutDsId);
  3564. bool bypassFl = cmDspBool( inst, kBypassDsId );
  3565. cmReal_t inGain = cmDspDouble( inst, kInGainDsId );
  3566. cmReal_t downSrate = cmDspDouble( inst, kSrateDsId );
  3567. cmReal_t bits = cmDspDouble( inst, kBitsDsId );
  3568. bool rectFl = cmDspBool( inst, kRectDsId );
  3569. bool fullFl = cmDspBool( inst, kFullDsId );
  3570. cmReal_t clipDb = cmDspDouble( inst, kClipDbDsId );
  3571. cmReal_t outGain = cmDspDouble( inst, kOutGainDsId );
  3572. cmDistDsInit(p->p, cmDspSampleRate(ctx), inGain, downSrate, bits, rectFl, fullFl, clipDb, outGain, bypassFl );
  3573. }
  3574. return rc;
  3575. }
  3576. cmDspRC_t _cmDspDistDsExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3577. {
  3578. cmDspRC_t rc = kOkDspRC;
  3579. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3580. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutDsId,0);
  3581. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutDsId,0);
  3582. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInDsId,0);
  3583. cmDistDsExec(p->p,x,y,n);
  3584. return rc;
  3585. }
  3586. cmDspRC_t _cmDspDistDsRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3587. {
  3588. cmDspRC_t rc = kOkDspRC;
  3589. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3590. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3591. {
  3592. switch( evt->dstVarId )
  3593. {
  3594. case kInGainDsId:
  3595. p->p->inGain = cmDspDouble(inst,kInGainDsId);
  3596. break;
  3597. case kSrateDsId:
  3598. p->p->downSrate = cmDspDouble(inst,kSrateDsId);
  3599. break;
  3600. case kBitsDsId:
  3601. p->p->bits = cmDspDouble(inst,kBitsDsId);
  3602. break;
  3603. case kRectDsId:
  3604. p->p->rectFl = cmDspBool(inst,kRectDsId);
  3605. break;
  3606. case kFullDsId:
  3607. p->p->fullFl = cmDspBool(inst,kFullDsId);
  3608. break;
  3609. case kClipDbDsId:
  3610. p->p->clipDb = cmDspDouble(inst,kClipDbDsId);
  3611. break;
  3612. case kOutGainDsId:
  3613. p->p->outGain = cmDspDouble(inst,kOutGainDsId);
  3614. break;
  3615. case kBypassDsId:
  3616. p->p->bypassFl = cmDspBool(inst,kBypassDsId);
  3617. break;
  3618. }
  3619. }
  3620. return rc;
  3621. }
  3622. struct cmDspClass_str* cmDistDsClassCons( cmDspCtx_t* ctx )
  3623. {
  3624. cmDspClassSetup(&_cmDistDsDC,ctx,"DistDs",
  3625. NULL,
  3626. _cmDspDistDsAlloc,
  3627. _cmDspDistDsFree,
  3628. _cmDspDistDsReset,
  3629. _cmDspDistDsExec,
  3630. _cmDspDistDsRecv,
  3631. NULL,NULL,
  3632. "Comb Filter");
  3633. return &_cmDistDsDC;
  3634. }
  3635. //==========================================================================================================================================
  3636. enum
  3637. {
  3638. kInDlId,
  3639. kOutDlId
  3640. };
  3641. cmDspClass_t _cmDbToLinDC;
  3642. typedef struct
  3643. {
  3644. cmDspInst_t inst;
  3645. } cmDspDbToLin_t;
  3646. cmDspInst_t* _cmDspDbToLinAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3647. {
  3648. cmDspDbToLin_t* p = cmDspInstAllocV(cmDspDbToLin_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3649. 1, "in", kInDlId, 0, 0, kInDsvFl | kDoubleDsvFl, "Input",
  3650. 1, "out", kOutDlId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  3651. 0 );
  3652. cmDspSetDefaultDouble( ctx, &p->inst, kInDlId, 0.0, -1000.0);
  3653. cmDspSetDefaultDouble( ctx, &p->inst, kOutDlId, 0.0, 0.0 );
  3654. return &p->inst;
  3655. }
  3656. cmDspRC_t _cmDspDbToLinReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3657. {
  3658. return cmDspApplyAllDefaults(ctx,inst);
  3659. }
  3660. cmDspRC_t _cmDspDbToLinRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3661. {
  3662. cmDspRC_t rc = kOkDspRC;
  3663. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3664. {
  3665. if( evt->dstVarId == kInDlId )
  3666. {
  3667. double db = cmMax(0.0,cmMin(100.0,cmDspDouble(inst,kInDlId)));
  3668. double lin = db==0 ? 0.0 : pow(10.0, (db-100.0)/20.0);
  3669. cmDspSetDouble(ctx,inst,kOutDlId,lin);
  3670. }
  3671. }
  3672. return rc;
  3673. }
  3674. struct cmDspClass_str* cmDbToLinClassCons( cmDspCtx_t* ctx )
  3675. {
  3676. cmDspClassSetup(&_cmDbToLinDC,ctx,"DbToLin",
  3677. NULL,
  3678. _cmDspDbToLinAlloc,
  3679. NULL,
  3680. _cmDspDbToLinReset,
  3681. NULL,
  3682. _cmDspDbToLinRecv,
  3683. NULL,NULL,
  3684. "dB to Linear converter");
  3685. return &_cmDbToLinDC;
  3686. }
  3687. //==========================================================================================================================================
  3688. // Pass any N of M inputs
  3689. enum
  3690. {
  3691. kInChCntNoId,
  3692. kOutChCntNoId,
  3693. kXfadeMsNoId,
  3694. kCmdNoId,
  3695. kSelIdxNoId,
  3696. kBaseGateNoId
  3697. };
  3698. cmDspClass_t _cmNofM_DC;
  3699. typedef struct
  3700. {
  3701. cmDspInst_t inst;
  3702. unsigned iChCnt;
  3703. unsigned oChCnt;
  3704. unsigned* map; // map[ oChCnt ]
  3705. cmXfader** xf; // xf[ oChCnt ];
  3706. unsigned cfgSymId;
  3707. unsigned onSymId;
  3708. unsigned offSymId;
  3709. bool verboseFl;
  3710. unsigned baseBaseInNoId; // first data input port id
  3711. unsigned baseBaseOutNoId; // first data output port id
  3712. unsigned baseInFloatNoId;
  3713. unsigned baseInBoolNoId;
  3714. unsigned baseInSymNoId;
  3715. unsigned baseInAudioNoId;
  3716. unsigned baseOutFloatNoId;
  3717. unsigned baseOutBoolNoId;
  3718. unsigned baseOutSymNoId;
  3719. unsigned baseOutAudioNoId;
  3720. bool printFl;
  3721. } cmDspNofM_t;
  3722. cmDspInst_t* _cmDspNofM_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3723. {
  3724. if( va_cnt < 2 )
  3725. {
  3726. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'NofM' constructor must given input and output channel counts.");
  3727. return NULL;
  3728. }
  3729. va_list vl1;
  3730. va_copy(vl1,vl);
  3731. int iChCnt = va_arg(vl,int);
  3732. int oChCnt = va_arg(vl,int);
  3733. if( oChCnt > iChCnt )
  3734. {
  3735. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'NofM' output count must be less than or equal to the input count.");
  3736. return NULL;
  3737. }
  3738. unsigned baseInFloatNoId = kBaseGateNoId + iChCnt;
  3739. unsigned baseInBoolNoId = baseInFloatNoId + iChCnt;
  3740. unsigned baseInSymNoId = baseInBoolNoId + iChCnt;
  3741. unsigned baseInAudioNoId = baseInSymNoId + iChCnt;
  3742. unsigned baseOutFloatNoId = baseInAudioNoId + iChCnt;
  3743. unsigned baseOutBoolNoId = baseOutFloatNoId + oChCnt;
  3744. unsigned baseOutSymNoId = baseOutBoolNoId + oChCnt;
  3745. unsigned baseOutAudioNoId = baseOutSymNoId + oChCnt;
  3746. unsigned i;
  3747. cmDspNofM_t* p = cmDspInstAllocV(cmDspNofM_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3748. 1, "ichs", kInChCntNoId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Input channel count.",
  3749. 1, "ochs", kOutChCntNoId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Output channel count.",
  3750. 1, "ms", kXfadeMsNoId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl,"Audio Cross-fade time in milliseconds.",
  3751. 1, "cmd", kCmdNoId, 0, 0, kSymDsvFl | kInDsvFl, "Command input.",
  3752. 1, "seli", kSelIdxNoId, 0, 0, kUIntDsvFl | kInDsvFl, "Enable gate at index.",
  3753. iChCnt, "sel", kBaseGateNoId, 0, 0, kBoolDsvFl | kInDsvFl, "Selector Gate inputs.",
  3754. iChCnt, "f-in", baseInFloatNoId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  3755. iChCnt, "b-in", baseInBoolNoId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  3756. iChCnt, "s-in", baseInSymNoId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  3757. iChCnt, "a-in", baseInAudioNoId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  3758. oChCnt, "f-out", baseOutFloatNoId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  3759. oChCnt, "b-out", baseOutBoolNoId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  3760. oChCnt, "s-out", baseOutSymNoId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  3761. oChCnt, "a-out", baseOutAudioNoId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  3762. 0 );
  3763. p->iChCnt = iChCnt;
  3764. p->oChCnt = oChCnt;
  3765. p->map = cmMemAllocZ(unsigned,oChCnt);
  3766. p->xf = cmMemAllocZ(cmXfader*,oChCnt);
  3767. p->cfgSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"cfg");
  3768. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  3769. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  3770. p->verboseFl = false;
  3771. p->baseBaseInNoId = baseInFloatNoId;
  3772. p->baseBaseOutNoId = baseOutFloatNoId;
  3773. p->baseInFloatNoId = baseInFloatNoId;
  3774. p->baseInBoolNoId = baseInBoolNoId;
  3775. p->baseInSymNoId = baseInSymNoId;
  3776. p->baseInAudioNoId = baseInAudioNoId;
  3777. p->baseOutFloatNoId = baseOutFloatNoId;
  3778. p->baseOutBoolNoId = baseOutBoolNoId;
  3779. p->baseOutSymNoId = baseOutSymNoId;
  3780. p->baseOutAudioNoId = baseOutAudioNoId;
  3781. for(i=0; i<oChCnt; ++i)
  3782. {
  3783. cmDspSetDefaultDouble( ctx, &p->inst, baseOutFloatNoId + i, 0.0, 0.0 );
  3784. cmDspSetDefaultBool( ctx, &p->inst, baseOutBoolNoId + i, false, false );
  3785. cmDspSetDefaultSymbol( ctx, &p->inst, baseOutSymNoId + i, cmInvalidId );
  3786. p->xf[i] = cmXfaderAlloc( ctx->cmProcCtx, NULL, 0, 0, 0 );
  3787. }
  3788. cmDspSetDefaultDouble( ctx, &p->inst, kXfadeMsNoId, 0.0, 15.0 );
  3789. return &p->inst;
  3790. }
  3791. cmDspRC_t _cmDspNofM_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3792. {
  3793. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3794. unsigned i;
  3795. for(i=0; i<p->oChCnt; ++i)
  3796. cmXfaderFree(&p->xf[i]);
  3797. cmMemFree(p->map);
  3798. cmMemFree(p->xf);
  3799. return kOkDspRC;
  3800. }
  3801. cmDspRC_t _cmDspNofM_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3802. {
  3803. cmDspRC_t rc = kOkDspRC;
  3804. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3805. unsigned i;
  3806. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3807. {
  3808. for(i=0; i<p->oChCnt; ++i)
  3809. {
  3810. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioNoId+i);
  3811. p->map[i] = cmInvalidIdx;
  3812. double xfadeMs = cmDspDouble(inst,kXfadeMsNoId);
  3813. cmXfaderInit(p->xf[i], cmDspSampleRate(ctx), p->iChCnt, xfadeMs );
  3814. }
  3815. }
  3816. return rc;
  3817. }
  3818. cmDspRC_t _cmDspNofM_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3819. {
  3820. cmDspRC_t rc = kOkDspRC;
  3821. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3822. unsigned i;
  3823. const cmSample_t* x[ p->iChCnt ];
  3824. for(i=0; i<p->iChCnt; ++i)
  3825. x[i] = cmDspAudioBuf(ctx,inst,p->baseInAudioNoId + i ,0);
  3826. // for each valid p->map[] element
  3827. for(i=0; i<p->oChCnt; ++i)
  3828. {
  3829. cmSample_t* y = cmDspAudioBuf(ctx,inst,p->baseOutAudioNoId+ i,0);
  3830. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->baseOutAudioNoId+i,0);
  3831. if( y != NULL )
  3832. {
  3833. y = cmVOS_Zero(y,n);
  3834. cmXfaderExecAudio(p->xf[i],n,NULL,p->iChCnt,x,y);
  3835. }
  3836. }
  3837. return rc;
  3838. }
  3839. cmDspRC_t _cmDspNofM_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3840. {
  3841. cmDspRC_t rc = kOkDspRC;
  3842. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3843. unsigned i,j;
  3844. assert( evt->dstVarId < p->baseBaseOutNoId );
  3845. // store the incoming value
  3846. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3847. return rc;
  3848. // if this is a fade time
  3849. if( evt->dstVarId == kXfadeMsNoId )
  3850. {
  3851. for(i=0; i<p->oChCnt; ++i)
  3852. cmXfaderSetXfadeTime( p->xf[i], cmDspDouble(inst,kXfadeMsNoId) );
  3853. return rc;
  3854. }
  3855. // if this is an index selector
  3856. if( kSelIdxNoId == evt->dstVarId )
  3857. {
  3858. unsigned idx;
  3859. if((idx = cmDspUInt(inst,kSelIdxNoId)) >= p->iChCnt )
  3860. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The selection index ('%i') is out of the channel range, (%i).",idx,p->iChCnt);
  3861. else
  3862. {
  3863. cmDspSetBool( ctx, inst, kBaseGateNoId+idx , true );
  3864. if( p->verboseFl )
  3865. cmRptPrintf(ctx->rpt,"nom seli:%i\n",idx);
  3866. }
  3867. return rc;
  3868. }
  3869. // NOTE: the internal state DOES NOT CHANGE until a message arrives
  3870. // on the 'cmd' port
  3871. // if anything arrives on the command port - then read the gate states and rebuild the map
  3872. if( evt->dstVarId == kCmdNoId )
  3873. {
  3874. unsigned cmdSymId = cmDspSymbol(inst,kCmdNoId);
  3875. // if cmdSymId == 'on' then turn on all selection gates
  3876. if(cmdSymId == p->onSymId )
  3877. {
  3878. for(i=0; i<p->oChCnt; ++i)
  3879. cmDspSetBool(ctx,inst,kBaseGateNoId+i,true);
  3880. }
  3881. else
  3882. // if cmdSymId == 'off' then turn off all selection gates
  3883. if( cmdSymId == p->offSymId )
  3884. {
  3885. if( p->verboseFl )
  3886. cmRptPrintf(ctx->rpt,"nom: off\n");
  3887. for(i=0; i<p->oChCnt; ++i)
  3888. cmDspSetBool(ctx,inst,kBaseGateNoId+i,false);
  3889. }
  3890. else
  3891. // cmdSymId == 'print' then print the in/out map[]
  3892. if( cmdSymId == cmSymTblId(ctx->stH,"print") )
  3893. {
  3894. for(i=0; i<p->oChCnt; ++i)
  3895. cmRptPrintf(ctx->rpt,"%i:%i ",i,p->map[i]);
  3896. cmRptPrintf(ctx->rpt,"\n");
  3897. cmRptPrintf(ctx->rpt,"%i usecs\n",ctx->execDurUsecs);
  3898. p->printFl = !p->printFl;
  3899. }
  3900. if( p->verboseFl )
  3901. cmRptPrintf(ctx->rpt,"Nom: %s ", inst->symId != cmInvalidId ? cmSymTblLabel(ctx->stH,inst->symId) : "");
  3902. // 5/26
  3903. if( p->iChCnt == p->oChCnt )
  3904. cmVOU_Fill(p->map,p->oChCnt,cmInvalidIdx);
  3905. // for each input
  3906. for(i=0,j=0; i<p->iChCnt; ++i)
  3907. {
  3908. // if this input is switched on
  3909. if( cmDspBool(inst,kBaseGateNoId+i) )
  3910. {
  3911. if( j >= p->oChCnt )
  3912. {
  3913. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"To many inputs have been turned on for %i outputs.",p->oChCnt);
  3914. break;
  3915. }
  3916. // assign input i to output j
  3917. p->map[j] = i;
  3918. // fade in ch i and fade out all others
  3919. cmXfaderSelectOne(p->xf[j],i);
  3920. ++j;
  3921. if( p->verboseFl )
  3922. cmRptPrintf(ctx->rpt,"%i ",i);
  3923. }
  3924. else // 5/26
  3925. {
  3926. if( p->iChCnt == p->oChCnt )
  3927. ++j;
  3928. }
  3929. }
  3930. // deselect all other output channels
  3931. //for(; j<p->oChCnt; ++j)
  3932. //{
  3933. // p->map[j] = cmInvalidIdx;
  3934. // cmXfaderAllOff(p->xf[j]);
  3935. //}
  3936. // 5/26
  3937. if( p->iChCnt == p->oChCnt )
  3938. {
  3939. for(j=0; j<p->oChCnt; ++j)
  3940. if( p->map[j] == cmInvalidIdx )
  3941. cmXfaderAllOff(p->xf[j]);
  3942. }
  3943. else
  3944. {
  3945. for(; j<p->oChCnt; ++j)
  3946. {
  3947. p->map[j] = cmInvalidIdx;
  3948. cmXfaderAllOff(p->xf[j]);
  3949. }
  3950. }
  3951. if( p->verboseFl )
  3952. cmRptPrintf(ctx->rpt,"\n");
  3953. // zero the audio buffers of unused output channels
  3954. for(i=0; i<p->oChCnt; ++i)
  3955. if( p->map[i] == cmInvalidIdx )
  3956. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioNoId+i);
  3957. }
  3958. // if this is an input data event
  3959. if( p->baseBaseInNoId <= evt->dstVarId && evt->dstVarId < p->baseBaseOutNoId )
  3960. {
  3961. // get the input channel this event occurred on
  3962. unsigned iChIdx = (evt->dstVarId - p->baseBaseInNoId) % p->iChCnt;
  3963. // is iChIdx mapped to an output ...
  3964. for(i=0; i<p->oChCnt; ++i)
  3965. if( p->map[i] == iChIdx )
  3966. break;
  3967. // ... no - nothing else to do
  3968. if( i==p->oChCnt )
  3969. return kOkDspRC;
  3970. // ... yes set the output ...
  3971. // double
  3972. if( p->baseInFloatNoId <= evt->dstVarId && evt->dstVarId < p->baseInFloatNoId + p->iChCnt )
  3973. {
  3974. cmDspSetDouble(ctx,inst,p->baseOutFloatNoId + i, cmDspDouble(inst,evt->dstVarId));
  3975. }
  3976. else
  3977. // bool
  3978. if( p->baseInBoolNoId <= evt->dstVarId && evt->dstVarId < p->baseInBoolNoId + p->iChCnt )
  3979. {
  3980. cmDspSetBool(ctx,inst,p->baseOutBoolNoId + i, cmDspBool(inst,evt->dstVarId));
  3981. if(p->printFl)
  3982. cmRptPrintf(ctx->rpt,"%i %i\n",p->baseOutBoolNoId + i, cmDspBool(inst,evt->dstVarId));
  3983. }
  3984. else
  3985. // symbol
  3986. if( p->baseInSymNoId <= evt->dstVarId && evt->dstVarId < p->baseInSymNoId + p->iChCnt )
  3987. cmDspSetSymbol(ctx,inst,p->baseOutSymNoId + i, cmDspSymbol(inst,evt->dstVarId));
  3988. }
  3989. return rc;
  3990. }
  3991. struct cmDspClass_str* cmNofMClassCons( cmDspCtx_t* ctx )
  3992. {
  3993. cmDspClassSetup(&_cmNofM_DC,ctx,"NofM",
  3994. NULL,
  3995. _cmDspNofM_Alloc,
  3996. _cmDspNofM_Free,
  3997. _cmDspNofM_Reset,
  3998. _cmDspNofM_Exec,
  3999. _cmDspNofM_Recv,
  4000. NULL,NULL,
  4001. "N of M Switch");
  4002. return &_cmNofM_DC;
  4003. }
  4004. //==========================================================================================================================================
  4005. enum
  4006. {
  4007. kInChCnt1oId,
  4008. kInChIdx1oId,
  4009. kOutFloat1oId,
  4010. kOutBool1oId,
  4011. kOutSym1oId,
  4012. kOutAudio1oId,
  4013. kBaseInFloat1oId
  4014. };
  4015. cmDspClass_t _cm1ofN_DC;
  4016. typedef struct
  4017. {
  4018. cmDspInst_t inst;
  4019. unsigned iChCnt;
  4020. unsigned oChCnt;
  4021. unsigned iChIdx;
  4022. unsigned baseBaseIn1oId; // first data input port id
  4023. unsigned baseInFloat1oId;
  4024. unsigned baseInBool1oId;
  4025. unsigned baseInSym1oId;
  4026. unsigned baseInAudio1oId;
  4027. unsigned baseOutFloat1oId;
  4028. unsigned baseOutBool1oId;
  4029. unsigned baseOutSym1oId;
  4030. unsigned baseOutAudio1oId;
  4031. } cmDsp1ofN_t;
  4032. cmDspInst_t* _cmDsp1ofN_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4033. {
  4034. if( va_cnt < 1 )
  4035. {
  4036. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The '1ofN' constructor must given input channel count.");
  4037. return NULL;
  4038. }
  4039. va_list vl1;
  4040. va_copy(vl1,vl);
  4041. int iChCnt = va_arg(vl,int);
  4042. unsigned baseInFloat1oId = kBaseInFloat1oId;
  4043. unsigned baseInBool1oId = baseInFloat1oId + iChCnt;
  4044. unsigned baseInSym1oId = baseInBool1oId + iChCnt;
  4045. unsigned baseInAudio1oId = baseInSym1oId + iChCnt;
  4046. cmDsp1ofN_t* p = cmDspInstAllocV(cmDsp1ofN_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4047. 1, "ichs", kInChCnt1oId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Input channel count.",
  4048. 1, "chidx", kInChIdx1oId, 0, 0, kUIntDsvFl | kReqArgDsvFl | kInDsvFl, "Input channel selector index.",
  4049. 1, "f-out", kOutFloat1oId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  4050. 1, "b-out", kOutBool1oId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  4051. 1, "s-out", kOutSym1oId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  4052. 1, "a-out", kOutAudio1oId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  4053. iChCnt, "f-in", baseInFloat1oId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  4054. iChCnt, "b-in", baseInBool1oId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  4055. iChCnt, "s-in", baseInSym1oId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  4056. iChCnt, "a-in", baseInAudio1oId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  4057. 0 );
  4058. p->iChCnt = iChCnt;
  4059. p->baseBaseIn1oId = kBaseInFloat1oId;
  4060. p->baseInFloat1oId = baseInFloat1oId;
  4061. p->baseInBool1oId = baseInBool1oId;
  4062. p->baseInSym1oId = baseInSym1oId;
  4063. p->baseInAudio1oId = baseInAudio1oId;
  4064. cmDspSetDefaultDouble( ctx, &p->inst, kOutFloat1oId, 0.0, 0.0 );
  4065. cmDspSetDefaultBool( ctx, &p->inst, kOutBool1oId, false, false );
  4066. cmDspSetDefaultSymbol( ctx, &p->inst, kOutSym1oId, cmInvalidId );
  4067. return &p->inst;
  4068. }
  4069. cmDspRC_t _cmDsp1ofN_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4070. {
  4071. return kOkDspRC;
  4072. }
  4073. cmDspRC_t _cmDsp1ofN_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4074. {
  4075. cmDspRC_t rc = kOkDspRC;
  4076. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4077. {
  4078. cmDspZeroAudioBuf(ctx,inst,kOutAudio1oId);
  4079. }
  4080. return rc;
  4081. }
  4082. cmDspRC_t _cmDsp1ofN_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4083. {
  4084. cmDspRC_t rc = kOkDspRC;
  4085. cmDsp1ofN_t* p = (cmDsp1ofN_t*)inst;
  4086. unsigned iChIdx = cmDspUInt(inst,kInChIdx1oId);
  4087. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAudio1oId,0);
  4088. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,p->baseInAudio1oId + iChIdx ,0);
  4089. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAudio1oId,0);
  4090. if( dp != NULL )
  4091. {
  4092. if( sp == NULL )
  4093. cmVOS_Zero(dp,n);
  4094. else
  4095. cmVOS_Copy(dp,n,sp);
  4096. }
  4097. return rc;
  4098. }
  4099. cmDspRC_t _cmDsp1ofN_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4100. {
  4101. cmDspRC_t rc = kOkDspRC;
  4102. cmDsp1ofN_t* p = (cmDsp1ofN_t*)inst;
  4103. // ignore out of range input channel
  4104. if( evt->dstVarId == kInChIdx1oId && cmDsvGetUInt(evt->valuePtr) >= p->iChCnt )
  4105. {
  4106. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The selector channel index %i is out of range.",cmDsvGetUInt(evt->valuePtr));
  4107. return kOkDspRC;
  4108. }
  4109. // store the incoming value
  4110. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4111. return rc;
  4112. // if this is an input data event
  4113. if( p->baseBaseIn1oId <= evt->dstVarId )
  4114. {
  4115. // get the input channel this event occurred on
  4116. unsigned iChIdx = (evt->dstVarId - p->baseBaseIn1oId) % p->iChCnt;
  4117. // if the event did not arrive on the selected input channel - there is nothing else to do
  4118. if( iChIdx != cmDspUInt(inst,kInChIdx1oId) )
  4119. return kOkDspRC;
  4120. // The event arrived on the input channel - send it out the output
  4121. // double
  4122. if( p->baseInFloat1oId <= evt->dstVarId && evt->dstVarId < p->baseInFloat1oId + p->iChCnt )
  4123. cmDspSetDouble(ctx,inst,kOutFloat1oId, cmDspDouble(inst,evt->dstVarId));
  4124. else
  4125. // bool
  4126. if( p->baseInBool1oId <= evt->dstVarId && evt->dstVarId < p->baseInBool1oId + p->iChCnt )
  4127. cmDspSetBool(ctx,inst,kOutBool1oId, cmDspBool(inst,evt->dstVarId));
  4128. else
  4129. // symbol
  4130. if( p->baseInSym1oId <= evt->dstVarId && evt->dstVarId < p->baseInSym1oId + p->iChCnt )
  4131. cmDspSetSymbol(ctx,inst,kOutSym1oId, cmDspSymbol(inst,evt->dstVarId));
  4132. }
  4133. return rc;
  4134. }
  4135. struct cmDspClass_str* cm1ofNClassCons( cmDspCtx_t* ctx )
  4136. {
  4137. cmDspClassSetup(&_cm1ofN_DC,ctx,"1ofN",
  4138. NULL,
  4139. _cmDsp1ofN_Alloc,
  4140. _cmDsp1ofN_Free,
  4141. _cmDsp1ofN_Reset,
  4142. _cmDsp1ofN_Exec,
  4143. _cmDsp1ofN_Recv,
  4144. NULL,NULL,
  4145. " 1 of N Switch");
  4146. return &_cm1ofN_DC;
  4147. }
  4148. //==========================================================================================================================================
  4149. // Send a 'true' out on the selected channel.
  4150. // Send a 'false' out on the deselected channel.
  4151. enum
  4152. {
  4153. kChCnt1uId,
  4154. kSel1uId,
  4155. kBaseOut1uId
  4156. };
  4157. cmDspClass_t _cm1Up_DC;
  4158. typedef struct
  4159. {
  4160. cmDspInst_t inst;
  4161. } cmDsp1Up_t;
  4162. cmDspInst_t* _cmDsp1Up_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4163. {
  4164. if( va_cnt < 1 )
  4165. {
  4166. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The '1Up' constructor must given output channel count.");
  4167. return NULL;
  4168. }
  4169. va_list vl1;
  4170. va_copy(vl1,vl);
  4171. int chCnt = va_arg(vl,int);
  4172. cmDsp1Up_t* p = cmDspInstAllocV(cmDsp1Up_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4173. 1, "chcnt", kChCnt1uId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Output channel count.",
  4174. 1, "sel", kSel1uId, 0, 0, kUIntDsvFl | kOptArgDsvFl | kInDsvFl, "Channel index selector.",
  4175. chCnt, "out", kBaseOut1uId, 0, 0, kBoolDsvFl | kOutDsvFl, "Gate outputs",
  4176. 0 );
  4177. unsigned i;
  4178. cmDspSetDefaultUInt( ctx, &p->inst, kSel1uId, 0.0, 0.0 );
  4179. for(i=0; i<chCnt; ++i)
  4180. cmDspSetDefaultBool( ctx, &p->inst, kBaseOut1uId + i, false, false );
  4181. return &p->inst;
  4182. }
  4183. cmDspRC_t _cmDsp1Up_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4184. {
  4185. cmDspRC_t rc = kOkDspRC;
  4186. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4187. {
  4188. unsigned chIdx = cmDspUInt(inst,kSel1uId);
  4189. unsigned chCnt = cmDspUInt(inst,kChCnt1uId);
  4190. unsigned i;
  4191. for(i=0; i<chCnt; ++i)
  4192. cmDspSetBool(ctx,inst,kBaseOut1uId+i, i == chIdx );
  4193. }
  4194. return rc;
  4195. }
  4196. cmDspRC_t _cmDsp1Up_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4197. {
  4198. cmDspRC_t rc = kOkDspRC;
  4199. if( evt->dstVarId == kSel1uId)
  4200. {
  4201. unsigned chIdx = cmDspUInt(inst,kSel1uId);
  4202. unsigned chCnt = cmDspUInt(inst,kChCnt1uId);
  4203. // turn off the previously selected channel
  4204. if( chIdx != cmInvalidIdx && chIdx < chCnt )
  4205. cmDspSetBool(ctx,inst,kBaseOut1uId+chIdx,false);
  4206. // set the new channel index
  4207. cmDspSetEvent(ctx,inst,evt);
  4208. // get the new channel index
  4209. chIdx = cmDspUInt(inst,kSel1uId);
  4210. // send the new channel index
  4211. if( chIdx != cmInvalidIdx && chIdx < chCnt )
  4212. cmDspSetBool(ctx,inst,kBaseOut1uId+chIdx,true);
  4213. }
  4214. return rc;
  4215. }
  4216. struct cmDspClass_str* cm1UpClassCons( cmDspCtx_t* ctx )
  4217. {
  4218. cmDspClassSetup(&_cm1Up_DC,ctx,"1Up",
  4219. NULL,
  4220. _cmDsp1Up_Alloc,
  4221. NULL,
  4222. _cmDsp1Up_Reset,
  4223. NULL,
  4224. _cmDsp1Up_Recv,
  4225. NULL,NULL,
  4226. "Set one input high and all others low.");
  4227. return &_cm1Up_DC;
  4228. }
  4229. //==========================================================================================================================================
  4230. // Convert a 'true'/'false' gate to an 'on'/'off' symbol
  4231. enum
  4232. {
  4233. kOnSymGsId,
  4234. kOffSymGsId,
  4235. kOnGsId,
  4236. kOffGsId,
  4237. kBothGsId,
  4238. kOutGsId
  4239. };
  4240. cmDspClass_t _cmGateToSym_DC;
  4241. typedef struct
  4242. {
  4243. cmDspInst_t inst;
  4244. } cmDspGateToSym_t;
  4245. cmDspInst_t* _cmDspGateToSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4246. {
  4247. cmDspGateToSym_t* p = cmDspInstAllocV(cmDspGateToSym_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4248. 1, "on_sym", kOnSymGsId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl,"'on' symbol id (default:'on')",
  4249. 1, "off_sym",kOffSymGsId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl,"'off' symbol id (default:'off')",
  4250. 1, "on", kOnGsId, 0, 0, kBoolDsvFl | kInDsvFl, "On - send out 'on' symbol when a 'true' is received.",
  4251. 1, "off", kOffGsId, 0, 0, kBoolDsvFl | kInDsvFl, "Off - send out 'off' symbol when a 'false' is received.",
  4252. 1, "both", kBothGsId, 0, 0, kBoolDsvFl | kInDsvFl, "Send 'on' on 'true' and 'off' on 'false'.",
  4253. 1, "out", kOutGsId, 0, 0, kSymDsvFl | kOutDsvFl, "Output",
  4254. 0 );
  4255. unsigned onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  4256. unsigned offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  4257. cmDspSetDefaultSymbol( ctx, &p->inst, kOnSymGsId, onSymId );
  4258. cmDspSetDefaultSymbol( ctx, &p->inst, kOffSymGsId, offSymId );
  4259. cmDspSetDefaultSymbol( ctx, &p->inst, kOutGsId, cmInvalidId );
  4260. return &p->inst;
  4261. }
  4262. cmDspRC_t _cmDspGateToSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4263. {
  4264. cmDspRC_t rc = kOkDspRC;
  4265. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4266. {
  4267. }
  4268. return rc;
  4269. }
  4270. cmDspRC_t _cmDspGateToSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4271. {
  4272. cmDspRC_t rc = kOkDspRC;
  4273. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  4274. {
  4275. unsigned onSymId = cmDspSymbol(inst,kOnSymGsId);
  4276. unsigned offSymId = cmDspSymbol(inst,kOffSymGsId);
  4277. switch( evt->dstVarId )
  4278. {
  4279. case kOnGsId:
  4280. if( cmDspBool(inst,kOnGsId) )
  4281. cmDspSetSymbol(ctx,inst,kOutGsId,onSymId);
  4282. break;
  4283. case kOffGsId:
  4284. if( cmDspBool(inst,kOffGsId) )
  4285. cmDspSetSymbol(ctx,inst,kOutGsId,offSymId);
  4286. break;
  4287. case kBothGsId:
  4288. cmDspSetSymbol(ctx,inst, kOutGsId, cmDspBool(inst,kBothGsId) ? onSymId : offSymId);
  4289. break;
  4290. }
  4291. }
  4292. return rc;
  4293. }
  4294. struct cmDspClass_str* cmGateToSymClassCons( cmDspCtx_t* ctx )
  4295. {
  4296. cmDspClassSetup(&_cmGateToSym_DC,ctx,"GateToSym",
  4297. NULL,
  4298. _cmDspGateToSym_Alloc,
  4299. NULL,
  4300. _cmDspGateToSym_Reset,
  4301. NULL,
  4302. _cmDspGateToSym_Recv,
  4303. NULL,NULL,
  4304. "Convert a 'true'/'false' gate to an 'on'/'off' symbol.");
  4305. return &_cmGateToSym_DC;
  4306. }
  4307. //==========================================================================================================================================
  4308. enum
  4309. {
  4310. kOutPtsId,
  4311. kBaseInPtsId
  4312. };
  4313. cmDspClass_t _cmPortToSym_DC;
  4314. typedef struct
  4315. {
  4316. cmDspInst_t inst;
  4317. unsigned* symIdArray;
  4318. unsigned symIdCnt;
  4319. unsigned baseOutPtsId;
  4320. } cmDspPortToSym_t;
  4321. cmDspInst_t* _cmDspPortToSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4322. {
  4323. va_list vl1;
  4324. va_copy(vl1,vl);
  4325. if( va_cnt < 1 )
  4326. {
  4327. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'PortToSym' constructor argument list must contain at least one symbol label.");
  4328. return NULL;
  4329. }
  4330. unsigned symCnt = va_cnt;
  4331. unsigned argCnt = 1 + 2*symCnt;
  4332. cmDspVarArg_t args[argCnt+1];
  4333. unsigned* symIdArray = cmMemAllocZ(unsigned,symCnt);
  4334. unsigned baseOutPtsId = kBaseInPtsId + symCnt;
  4335. // setup the output port arg recd
  4336. cmDspArgSetup(ctx,args,"out",cmInvalidId,kOutPtsId,0,0,kOutDsvFl | kSymDsvFl, "Output" );
  4337. unsigned i;
  4338. for(i=0; i<symCnt; ++i)
  4339. {
  4340. // get the symbol label
  4341. const cmChar_t* symLabel = va_arg(vl,const char*);
  4342. assert( symLabel != NULL );
  4343. // register the symbol
  4344. symIdArray[i] = cmSymTblRegisterSymbol(ctx->stH,symLabel);
  4345. cmDspArgSetup(ctx, args+kBaseInPtsId+i, symLabel, cmInvalidId, kBaseInPtsId+i, 0, 0, kInDsvFl | kTypeDsvMask, cmTsPrintfH(ctx->lhH,"%s Input.",symLabel) );
  4346. cmDspArgSetup(ctx, args+baseOutPtsId+i, symLabel, cmInvalidId, baseOutPtsId+i, 0, 0, kOutDsvFl | kSymDsvFl, cmTsPrintfH(ctx->lhH,"%s Output.",symLabel) );
  4347. }
  4348. cmDspArgSetupNull(args + argCnt);
  4349. cmDspPortToSym_t* p = cmDspInstAlloc(cmDspPortToSym_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl1);
  4350. p->symIdCnt = symCnt;
  4351. p->symIdArray = symIdArray;
  4352. p->baseOutPtsId = baseOutPtsId;
  4353. cmDspSetDefaultSymbol(ctx,&p->inst,kOutPtsId,cmInvalidId);
  4354. return &p->inst;
  4355. }
  4356. cmDspRC_t _cmDspPortToSym_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4357. {
  4358. cmDspPortToSym_t* p = (cmDspPortToSym_t*)inst;
  4359. cmMemFree(p->symIdArray);
  4360. return kOkDspRC;
  4361. }
  4362. cmDspRC_t _cmDspPortToSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4363. {
  4364. return cmDspApplyAllDefaults(ctx,inst);
  4365. }
  4366. cmDspRC_t _cmDspPortToSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4367. {
  4368. cmDspRC_t rc = kOkDspRC;
  4369. cmDspPortToSym_t* p = (cmDspPortToSym_t*)inst;
  4370. // if a msg of any type is recieved on an input port - send out the associated symbol
  4371. if( kBaseInPtsId <= evt->dstVarId && evt->dstVarId < kBaseInPtsId + p->symIdCnt )
  4372. {
  4373. unsigned idx = evt->dstVarId - kBaseInPtsId;
  4374. assert( idx < p->symIdCnt );
  4375. cmDspSetSymbol(ctx,inst,p->baseOutPtsId + idx, p->symIdArray[idx]);
  4376. return cmDspSetSymbol(ctx,inst,kOutPtsId,p->symIdArray[ evt->dstVarId - kBaseInPtsId ]);
  4377. }
  4378. return rc;
  4379. }
  4380. struct cmDspClass_str* cmPortToSymClassCons( cmDspCtx_t* ctx )
  4381. {
  4382. cmDspClassSetup(&_cmPortToSym_DC,ctx,"PortToSym",
  4383. NULL,
  4384. _cmDspPortToSym_Alloc,
  4385. _cmDspPortToSym_Free,
  4386. _cmDspPortToSym_Reset,
  4387. NULL,
  4388. _cmDspPortToSym_Recv,
  4389. NULL,NULL,
  4390. "If a message of any kind is received on a port then send the symbol associated with the port.");
  4391. return &_cmPortToSym_DC;
  4392. }
  4393. //==========================================================================================================================================
  4394. enum
  4395. {
  4396. kOutChCntRtId,
  4397. kOutChIdxRtId,
  4398. kInFloatRtId,
  4399. kInBoolRtId,
  4400. kInSymRtId,
  4401. kInAudioRtId,
  4402. kBaseOutFloatRtId
  4403. };
  4404. cmDspClass_t _cmRouter_DC;
  4405. typedef struct
  4406. {
  4407. cmDspInst_t inst;
  4408. unsigned oChCnt;
  4409. unsigned oChIdx;
  4410. unsigned baseBaseOutRtId; // first data input port id
  4411. unsigned baseInFloatRtId;
  4412. unsigned baseInBoolRtId;
  4413. unsigned baseInSymRtId;
  4414. unsigned baseInAudioRtId;
  4415. unsigned baseOutFloatRtId;
  4416. unsigned baseOutBoolRtId;
  4417. unsigned baseOutSymRtId;
  4418. unsigned baseOutAudioRtId;
  4419. } cmDspRouter_t;
  4420. cmDspInst_t* _cmDspRouter_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4421. {
  4422. if( va_cnt < 1 )
  4423. {
  4424. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Router' constructor must be given an output channel count.");
  4425. return NULL;
  4426. }
  4427. va_list vl1;
  4428. va_copy(vl1,vl);
  4429. int oChCnt = va_arg(vl,int);
  4430. unsigned baseOutFloatRtId = kBaseOutFloatRtId;
  4431. unsigned baseOutBoolRtId = baseOutFloatRtId + oChCnt;
  4432. unsigned baseOutSymRtId = baseOutBoolRtId + oChCnt;
  4433. unsigned baseOutAudioRtId = baseOutSymRtId + oChCnt;
  4434. cmDspRouter_t* p = cmDspInstAllocV(cmDspRouter_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4435. 1, "ochs", kOutChCntRtId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Output channel count.",
  4436. 1, "sel", kOutChIdxRtId, 0, 0, kUIntDsvFl | kReqArgDsvFl | kInDsvFl, "Output channel index selector.",
  4437. 1, "f-in", kInFloatRtId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  4438. 1, "b-in", kInBoolRtId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  4439. 1, "s-in", kInSymRtId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  4440. 1, "a-in", kInAudioRtId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  4441. oChCnt, "f-out", baseOutFloatRtId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  4442. oChCnt, "b-out", baseOutBoolRtId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  4443. oChCnt, "s-out", baseOutSymRtId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  4444. oChCnt, "a-out", baseOutAudioRtId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  4445. 0 );
  4446. p->oChCnt = oChCnt;
  4447. p->baseBaseOutRtId = kBaseOutFloatRtId;
  4448. p->baseOutFloatRtId = baseOutFloatRtId;
  4449. p->baseOutBoolRtId = baseOutBoolRtId;
  4450. p->baseOutSymRtId = baseOutSymRtId;
  4451. p->baseOutAudioRtId = baseOutAudioRtId;
  4452. unsigned i;
  4453. for(i=0; i<oChCnt; ++i)
  4454. {
  4455. cmDspSetDefaultDouble( ctx, &p->inst, baseOutFloatRtId+i, 0.0, 0.0 );
  4456. cmDspSetDefaultBool( ctx, &p->inst, baseOutBoolRtId+i, false, false );
  4457. cmDspSetDefaultSymbol( ctx, &p->inst, baseOutSymRtId+i, cmInvalidId );
  4458. }
  4459. return &p->inst;
  4460. }
  4461. cmDspRC_t _cmDspRouter_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4462. {
  4463. return kOkDspRC;
  4464. }
  4465. cmDspRC_t _cmDspRouter_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4466. {
  4467. cmDspRC_t rc = kOkDspRC;
  4468. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4469. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4470. {
  4471. unsigned i;
  4472. for(i=0; i<p->oChCnt; ++i)
  4473. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioRtId+i);
  4474. }
  4475. return rc;
  4476. }
  4477. cmDspRC_t _cmDspRouter_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4478. {
  4479. cmDspRC_t rc = kOkDspRC;
  4480. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4481. unsigned oChIdx = cmDspUInt(inst,kOutChIdxRtId);
  4482. cmSample_t* dp = cmDspAudioBuf(ctx,inst,p->baseOutAudioRtId + oChIdx,0);
  4483. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAudioRtId ,0);
  4484. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->baseOutAudioRtId,0);
  4485. if( dp != NULL )
  4486. {
  4487. if( sp == NULL )
  4488. cmVOS_Zero(dp,n);
  4489. else
  4490. cmVOS_Copy(dp,n,sp);
  4491. }
  4492. return rc;
  4493. }
  4494. cmDspRC_t _cmDspRouter_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4495. {
  4496. cmDspRC_t rc = kOkDspRC;
  4497. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4498. // ignore out of range output channel selection
  4499. if( evt->dstVarId == kOutChIdxRtId && cmDsvGetUInt(evt->valuePtr) >= p->oChCnt )
  4500. {
  4501. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The selector channel index %i is out of of range.",cmDsvGetUInt(evt->valuePtr));
  4502. return kOkDspRC;
  4503. }
  4504. // store the incoming value
  4505. if( evt->dstVarId < p->baseBaseOutRtId )
  4506. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4507. return rc;
  4508. unsigned chIdx = cmDspUInt(inst,kOutChIdxRtId);
  4509. switch( evt->dstVarId )
  4510. {
  4511. case kInFloatRtId:
  4512. cmDspSetDouble(ctx,inst,p->baseOutFloatRtId + chIdx, cmDspDouble(inst,kInFloatRtId) );
  4513. break;
  4514. case kInBoolRtId:
  4515. cmDspSetBool(ctx,inst,p->baseOutBoolRtId + chIdx, cmDspBool(inst,kInBoolRtId) );
  4516. break;
  4517. case kInSymRtId:
  4518. cmDspSetSymbol(ctx,inst,p->baseOutSymRtId + chIdx, cmDspSymbol(inst,kInSymRtId));
  4519. break;
  4520. }
  4521. return rc;
  4522. }
  4523. struct cmDspClass_str* cmRouterClassCons( cmDspCtx_t* ctx )
  4524. {
  4525. cmDspClassSetup(&_cmRouter_DC,ctx,"Router",
  4526. NULL,
  4527. _cmDspRouter_Alloc,
  4528. _cmDspRouter_Free,
  4529. _cmDspRouter_Reset,
  4530. _cmDspRouter_Exec,
  4531. _cmDspRouter_Recv,
  4532. NULL,NULL,
  4533. "1 to N Router");
  4534. return &_cmRouter_DC;
  4535. }
  4536. //==========================================================================================================================================
  4537. enum
  4538. {
  4539. kChCntAvId,
  4540. kTrigAvId,
  4541. kChIdxAvId,
  4542. kBaseInDisAvId,
  4543. };
  4544. cmDspClass_t _cmAvailCh_DC;
  4545. typedef struct
  4546. {
  4547. cmDspInst_t inst;
  4548. unsigned chCnt;
  4549. unsigned baseOutEnaAvId;
  4550. unsigned baseInDisAvId;
  4551. unsigned enableSymId;
  4552. unsigned disableSymId;
  4553. bool* stateArray;
  4554. } cmDspAvailCh_t;
  4555. cmDspInst_t* _cmDspAvailCh_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4556. {
  4557. if( va_cnt < 1 )
  4558. {
  4559. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AvailCh' constructor must be given an channel count.");
  4560. return NULL;
  4561. }
  4562. va_list vl1;
  4563. va_copy(vl1,vl);
  4564. int chCnt = va_arg(vl,int);
  4565. if( chCnt <= 0 )
  4566. {
  4567. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The 'AvailCh' constructor must be given a positive channel count.");
  4568. return NULL;
  4569. }
  4570. unsigned baseInDisAvId = kBaseInDisAvId;
  4571. unsigned baseOutEnaAvId = baseInDisAvId + chCnt;
  4572. cmDspAvailCh_t* p = cmDspInstAllocV(cmDspAvailCh_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4573. 1, "chs", kChCntAvId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count.",
  4574. 1, "trig", kTrigAvId, 0, 0, kTypeDsvMask | kInDsvFl, "Trigger the unit to select the next available channel.",
  4575. 1, "ch", kChIdxAvId, 0, 0, kUIntDsvFl | kReqArgDsvFl | kInDsvFl, "Currently selected channel.",
  4576. chCnt, "dis", baseInDisAvId, 0, 0, kTypeDsvMask | kInDsvFl, "Disable inputs.",
  4577. chCnt, "ena", baseOutEnaAvId, 0, 0, kSymDsvFl | kOutDsvFl, "'enable' outputs",
  4578. 0 );
  4579. p->chCnt = chCnt;
  4580. p->baseInDisAvId = baseInDisAvId;
  4581. p->baseOutEnaAvId = baseOutEnaAvId;
  4582. p->enableSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"enable");
  4583. p->disableSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"disable");
  4584. unsigned i;
  4585. for(i=0; i<chCnt; ++i)
  4586. cmDspSetDefaultSymbol( ctx, &p->inst, baseOutEnaAvId+i, p->disableSymId );
  4587. cmDspSetDefaultUInt( ctx, &p->inst, kChIdxAvId, 0, cmInvalidIdx );
  4588. return &p->inst;
  4589. }
  4590. cmDspRC_t _cmDspAvailCh_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4591. {
  4592. return kOkDspRC;
  4593. }
  4594. cmDspRC_t _cmDspAvailCh_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4595. {
  4596. return cmDspApplyAllDefaults(ctx,inst);
  4597. }
  4598. cmDspRC_t _cmDspAvailCh_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4599. {
  4600. cmDspRC_t rc = kOkDspRC;
  4601. cmDspAvailCh_t* p = (cmDspAvailCh_t*)inst;
  4602. // if this is a trigger
  4603. if( evt->dstVarId == kTrigAvId )
  4604. {
  4605. unsigned i;
  4606. for(i=0; i<p->chCnt; ++i)
  4607. if( cmDspSymbol(inst,p->baseOutEnaAvId+i) == p->disableSymId )
  4608. {
  4609. cmDspSetUInt(ctx,inst,kChIdxAvId,i);
  4610. cmDspSetSymbol(ctx,inst,p->baseOutEnaAvId,p->enableSymId);
  4611. }
  4612. return rc;
  4613. }
  4614. // if this is an incoming disable message.
  4615. if( p->baseInDisAvId <= evt->dstVarId && evt->dstVarId < p->baseInDisAvId+p->chCnt )
  4616. {
  4617. cmDspSetSymbol(ctx,inst,p->baseOutEnaAvId,p->disableSymId);
  4618. return rc;
  4619. }
  4620. return rc;
  4621. }
  4622. struct cmDspClass_str* cmAvailChClassCons( cmDspCtx_t* ctx )
  4623. {
  4624. cmDspClassSetup(&_cmAvailCh_DC,ctx,"AvailCh",
  4625. NULL,
  4626. _cmDspAvailCh_Alloc,
  4627. _cmDspAvailCh_Free,
  4628. _cmDspAvailCh_Reset,
  4629. NULL,
  4630. _cmDspAvailCh_Recv,
  4631. NULL,NULL,
  4632. "Enable the next availabled channel");
  4633. return &_cmAvailCh_DC;
  4634. }
  4635. //==========================================================================================================================================
  4636. enum
  4637. {
  4638. kGroupSymPrId,
  4639. kLabelPrId,
  4640. kCmdPrId,
  4641. kDonePrId,
  4642. kListPrId,
  4643. kSelPrId
  4644. };
  4645. cmDspClass_t _cmPreset_DC;
  4646. typedef struct
  4647. {
  4648. cmDspInst_t inst;
  4649. unsigned storeCmdSymId;
  4650. unsigned recallCmdSymId;
  4651. unsigned doneSymId;
  4652. cmJsonH_t jsH;
  4653. cmJsonNode_t* np;
  4654. } cmDspPreset_t;
  4655. cmDspRC_t _cmDspPresetUpdateList( cmDspCtx_t* ctx, cmDspPreset_t* p, unsigned groupSymId )
  4656. {
  4657. cmDspRC_t rc = kOkDspRC;
  4658. // initialize the JSON tree
  4659. if( cmJsonInitialize(&p->jsH,ctx->cmCtx) != kOkJsRC )
  4660. {
  4661. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"JSON preset list handle initialization failed.");
  4662. goto errLabel;
  4663. }
  4664. // create the JSON tree root container
  4665. if( cmJsonCreateObject(p->jsH,NULL) == NULL )
  4666. {
  4667. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"JSON preset list root object create failed.");
  4668. goto errLabel;
  4669. }
  4670. // if a valid preset group symbol was given
  4671. if( groupSymId != cmInvalidId )
  4672. {
  4673. // get the JSON list containing the preset labels and symId's for this preset group
  4674. if( cmDspSysPresetPresetJsonList(ctx->dspH, groupSymId, &p->jsH ) != kOkDspRC )
  4675. {
  4676. rc = cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Request for a preset list failed.");
  4677. goto errLabel;
  4678. }
  4679. // get a pointer to the JSON 'presetArray' array node
  4680. if(( p->np = cmJsonFindValue(p->jsH,"presetArray",NULL,kArrayTId)) == NULL )
  4681. {
  4682. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset list is empty or synatax is not recognized.");
  4683. goto errLabel;
  4684. }
  4685. // set the JSON list
  4686. if((rc = cmDspSetJson(ctx,&p->inst,kListPrId,p->np)) != kOkDspRC )
  4687. {
  4688. rc = cmDspInstErr(ctx,&p->inst,rc,"Preset list set failed.");
  4689. goto errLabel;
  4690. }
  4691. }
  4692. errLabel:
  4693. return rc;
  4694. }
  4695. cmDspRC_t _cmDspPresetDoRecall( cmDspCtx_t* ctx, cmDspPreset_t* p, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  4696. {
  4697. cmDspRC_t rc;
  4698. // recall the preset
  4699. if(( rc = cmDspSysPresetRecall(ctx->dspH, groupLabel, presetLabel )) != kOkDspRC )
  4700. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Preset recall failed for group:'%s' preset:'%s'.",cmStringNullGuard(groupLabel),cmStringNullGuard(presetLabel));
  4701. // send out a notification that a new preset has been loaded
  4702. return cmDspSetSymbol(ctx,&p->inst,kDonePrId,p->doneSymId);
  4703. }
  4704. // selIdx is base 1, not base 0, because it references the JSON tree rows where the
  4705. // first row contains the titles.
  4706. cmDspRC_t _cmDspPresetListSelectRecall( cmDspCtx_t* ctx, cmDspPreset_t* p, unsigned selIdx )
  4707. {
  4708. cmDspRC_t rc = kOkDspRC;
  4709. const cmChar_t* presetLabel;
  4710. const cmChar_t* groupLabel;
  4711. unsigned groupSymId;
  4712. unsigned presetSymId;
  4713. const cmJsonNode_t* rnp;
  4714. // validate the JSON tree
  4715. if( cmJsonIsValid(p->jsH) == false || p->np == NULL )
  4716. {
  4717. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. The preset JSON tree does not exist.");
  4718. goto errLabel;
  4719. }
  4720. // validate the group id
  4721. if( (groupSymId = cmDspSymbol(&p->inst,kGroupSymPrId)) == cmInvalidId )
  4722. {
  4723. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Preset recall failed. The preset group symbol has not been set.");
  4724. goto errLabel;
  4725. }
  4726. // validate the selection index
  4727. if( selIdx >= cmJsonChildCount(p->np) )
  4728. {
  4729. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Preset recall failed. The preset index: %i is out of range 0-%i", selIdx, cmJsonChildCount(p->np));
  4730. goto errLabel;
  4731. }
  4732. // get the preset element
  4733. if(( rnp = cmJsonArrayElementC(p->np, selIdx )) == NULL )
  4734. {
  4735. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. Unable to retrieve preset JSON element.");
  4736. goto errLabel;
  4737. }
  4738. // verify the JSON syntax
  4739. assert( rnp->typeId==kArrayTId && cmJsonChildCount(rnp)==2 && cmJsonArrayElementC(rnp,1)->typeId == kIntTId );
  4740. // get the preset symbol id
  4741. if( cmJsonUIntValue( cmJsonArrayElementC(rnp,1), &presetSymId ) != kOkJsRC )
  4742. {
  4743. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. Unable to retrieve preset symbol id.");
  4744. goto errLabel;
  4745. }
  4746. // convert symbols to strings
  4747. groupLabel = cmSymTblLabel(ctx->stH,groupSymId);
  4748. presetLabel = cmSymTblLabel(ctx->stH,presetSymId);
  4749. rc = _cmDspPresetDoRecall(ctx,p,groupLabel,presetLabel);
  4750. errLabel:
  4751. return rc;
  4752. }
  4753. cmDspInst_t* _cmDspPreset_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4754. {
  4755. cmDspPreset_t* p = cmDspInstAllocV(cmDspPreset_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4756. 1, "sym", kGroupSymPrId, 0, 0, kInDsvFl | kSymDsvFl | kReqArgDsvFl, "Preset group symbol.",
  4757. 1, "label", kLabelPrId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Preset label",
  4758. 1, "cmd", kCmdPrId, 0, 0, kInDsvFl | kSymDsvFl, "Command input",
  4759. 1, "done", kDonePrId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'done' symbol after preset recall.",
  4760. 1, "list", kListPrId, 0, 0, kInDsvFl | kJsonDsvFl, "Preset list as a JSON array.",
  4761. 1, "sel", kSelPrId, 0, 0, kInDsvFl | kUIntDsvFl, "Preset index selection index",
  4762. 0 );
  4763. p->jsH = cmJsonNullHandle;
  4764. p->np = NULL;
  4765. p->storeCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"store");
  4766. p->recallCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"recall");
  4767. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  4768. cmDspSetDefaultBool( ctx, &p->inst, kDonePrId,false,false);
  4769. cmDspSetDefaultSymbol( ctx, &p->inst, kGroupSymPrId, cmInvalidId);
  4770. cmDspSetDefaultStrcz( ctx, &p->inst, kLabelPrId, NULL,"");
  4771. unsigned height = 5;
  4772. cmDspUiMsgListCreate(ctx, &p->inst, height, kListPrId, kSelPrId );
  4773. return &p->inst;
  4774. }
  4775. cmDspRC_t _cmDspPreset_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4776. {
  4777. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4778. cmJsonFinalize(&p->jsH);
  4779. return kOkDspRC;
  4780. }
  4781. cmDspRC_t _cmDspPreset_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4782. {
  4783. cmDspRC_t rc;
  4784. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4785. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4786. {
  4787. _cmDspPresetUpdateList(ctx, p, cmDspSymbol(inst,kGroupSymPrId) );
  4788. }
  4789. return rc;
  4790. }
  4791. cmDspRC_t _cmDspPreset_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4792. {
  4793. cmDspRC_t rc = kOkDspRC;
  4794. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4795. switch( evt->dstVarId )
  4796. {
  4797. case kListPrId:
  4798. return rc; // we don't yet handle lists arriving by the input port
  4799. case kSelPrId:
  4800. {
  4801. // sel idx is base 1 because the first row in the msg list contains the titles
  4802. unsigned selIdx = cmDsvGetUInt(evt->valuePtr);
  4803. if((rc = _cmDspPresetListSelectRecall(ctx,p,selIdx)) != kOkDspRC)
  4804. return rc;
  4805. }
  4806. break;
  4807. }
  4808. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4809. return rc;
  4810. // if this is a store or recall command
  4811. if( evt->dstVarId == kCmdPrId )
  4812. {
  4813. unsigned cmdSymId = cmDspSymbol(inst,kCmdPrId);
  4814. if( cmdSymId == p->storeCmdSymId || cmdSymId==p->recallCmdSymId )
  4815. {
  4816. unsigned groupSymId;
  4817. const cmChar_t* groupLabel;
  4818. const cmChar_t* presetLabel;
  4819. // get the group symbol
  4820. if((groupSymId = cmDspSymbol(inst,kGroupSymPrId)) == cmInvalidId )
  4821. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset group symbol id is not set.");
  4822. // get the group label
  4823. if((groupLabel = cmSymTblLabel(ctx->stH,groupSymId)) == NULL )
  4824. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset group label was not found.");
  4825. // get the preset label
  4826. if(( presetLabel = cmDspStrcz(inst,kLabelPrId)) == NULL || strlen(presetLabel)==0 )
  4827. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset label was not set.");
  4828. // if this is a store command
  4829. if( cmdSymId == p->storeCmdSymId )
  4830. {
  4831. // create a new preset
  4832. if((rc = cmDspSysPresetCreate(ctx->dspH,groupLabel, presetLabel)) != kOkDspRC )
  4833. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Preset create failed for group:'%s' preset:'%s'.",cmStringNullGuard(groupLabel),cmStringNullGuard(presetLabel));
  4834. // update the list with the new preset
  4835. rc = _cmDspPresetUpdateList(ctx, p, groupSymId );
  4836. }
  4837. else // otherwise this must be a recall command
  4838. {
  4839. rc = _cmDspPresetDoRecall(ctx,p,groupLabel, presetLabel);
  4840. }
  4841. }
  4842. }
  4843. return rc;
  4844. }
  4845. struct cmDspClass_str* cmPresetClassCons( cmDspCtx_t* ctx )
  4846. {
  4847. cmDspClassSetup(&_cmPreset_DC,ctx,"Preset",
  4848. NULL,
  4849. _cmDspPreset_Alloc,
  4850. _cmDspPreset_Free,
  4851. _cmDspPreset_Reset,
  4852. NULL,
  4853. _cmDspPreset_Recv,
  4854. NULL,NULL,
  4855. "Preset Manager");
  4856. return &_cmPreset_DC;
  4857. }
  4858. //==========================================================================================================================================
  4859. enum
  4860. {
  4861. kAttrBcId,
  4862. kMsgBcId
  4863. };
  4864. cmDspClass_t _cmBcastSym_DC;
  4865. typedef struct
  4866. {
  4867. cmDspInst_t inst;
  4868. unsigned onSymId;
  4869. unsigned offSymId;
  4870. } cmDspBcastSym_t;
  4871. cmDspInst_t* _cmDspBcastSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4872. {
  4873. cmDspBcastSym_t* p = cmDspInstAllocV(cmDspBcastSym_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4874. 1, "attr", kAttrBcId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl, "Instance which have this attribute symbol will receive the message.",
  4875. 1, "msg", kMsgBcId, 0, 0, kTypeDsvMask | kInDsvFl, "Msg to broadcast.",
  4876. 0 );
  4877. cmDspSetDefaultSymbol( ctx, &p->inst, kAttrBcId, cmInvalidId );
  4878. return &p->inst;
  4879. }
  4880. cmDspRC_t _cmDspBcastSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4881. {
  4882. cmDspRC_t rc = kOkDspRC;
  4883. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4884. {
  4885. }
  4886. return rc;
  4887. }
  4888. cmDspRC_t _cmDspBcastSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4889. {
  4890. cmDspRC_t rc = kOkDspRC;
  4891. if( evt->dstVarId == kAttrBcId )
  4892. return cmDspSetEvent(ctx,inst,evt);
  4893. if( evt->dstVarId == kMsgBcId )
  4894. {
  4895. unsigned attrSymId = cmDspSymbol(inst,kAttrBcId);
  4896. if( cmDsvIsSymbol(evt->valuePtr) )
  4897. {
  4898. printf("bcast: %i %s\n",attrSymId,cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr)));
  4899. }
  4900. return cmDspSysBroadcastValue(ctx->dspH, attrSymId, evt->valuePtr );
  4901. }
  4902. return rc;
  4903. }
  4904. struct cmDspClass_str* cmBcastSymClassCons( cmDspCtx_t* ctx )
  4905. {
  4906. cmDspClassSetup(&_cmBcastSym_DC,ctx,"BcastSym",
  4907. NULL,
  4908. _cmDspBcastSym_Alloc,
  4909. NULL,
  4910. _cmDspBcastSym_Reset,
  4911. NULL,
  4912. _cmDspBcastSym_Recv,
  4913. NULL,NULL,
  4914. "Set one input high and all others low.");
  4915. return &_cmBcastSym_DC;
  4916. }
  4917. //==========================================================================================================================================
  4918. enum
  4919. {
  4920. kRsrcSlId,
  4921. kCmdSlId,
  4922. kTrigSlId,
  4923. kOutSlId,
  4924. };
  4925. cmDspClass_t _cmSegLineDC;
  4926. typedef struct
  4927. {
  4928. cmDspInst_t inst;
  4929. double* x; // x[n]
  4930. unsigned n;
  4931. unsigned i; // current segment
  4932. unsigned m; // cur cnt
  4933. } cmDspSegLine_t;
  4934. cmDspInst_t* _cmDspSegLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4935. {
  4936. cmDspSegLine_t* p = cmDspInstAllocV(cmDspSegLine_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4937. 1, "rsrc", kRsrcSlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Name of resource array.",
  4938. 1, "cmd", kCmdSlId, 0, 0, kInDsvFl | kSymDsvFl, "Command",
  4939. 1, "trig", kTrigSlId, 0, 0, kInDsvFl | kTypeDsvMask, "Trigger",
  4940. 1, "out", kOutSlId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  4941. 0 );
  4942. if( p == NULL )
  4943. return NULL;
  4944. // The array is expected to contain interleaved values:
  4945. // cnt_0, val_0, cnt_1, val_1 .... cnt_n val_n
  4946. // The 'cnt_x' values give the count of trigger values upon which the output will be 'val_x'.
  4947. if( cmDspRsrcDblArray(ctx->dspH,&p->n,&p->x,cmDspDefaultStrcz(&p->inst,kRsrcSlId),NULL) != kOkDspRC )
  4948. {
  4949. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'SegLine' constructor resource array could not be read.");
  4950. return NULL;
  4951. }
  4952. cmDspSetDefaultDouble( ctx, &p->inst, kOutSlId, 0.0, p->n >= 2 ? p->x[1] : 0.0 );
  4953. p->i = 0;
  4954. p->m = 0;
  4955. return &p->inst;
  4956. }
  4957. cmDspRC_t _cmDspSegLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4958. {
  4959. cmDspSegLine_t* p = (cmDspSegLine_t*)inst;
  4960. p->i = 0;
  4961. p->m = 0;
  4962. return cmDspApplyAllDefaults(ctx,inst);
  4963. }
  4964. cmDspRC_t _cmDspSegLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4965. {
  4966. cmDspRC_t rc = kOkDspRC;
  4967. cmDspSegLine_t* p = (cmDspSegLine_t*)inst;
  4968. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  4969. {
  4970. switch( evt->dstVarId )
  4971. {
  4972. case kTrigSlId:
  4973. {
  4974. double val = cmDspDouble(inst,kOutSlId);
  4975. if( p->i < p->n )
  4976. {
  4977. if( p->m >= p->x[p->i] )
  4978. p->i += 2;
  4979. if( p->i < p->n )
  4980. {
  4981. double x0 = p->x[p->i-2];
  4982. double y0 = p->x[p->i-1];
  4983. double x1 = p->x[p->i+0];
  4984. double y1 = p->x[p->i+1];
  4985. double dx = x1 - x0;
  4986. double dy = y1 - y0;
  4987. val = y0 + (p->m - x0) * dy / dx;
  4988. printf("i:%i m=%i x0:%f y0:%f x1:%f y1:%f : %f\n",p->i,p->m,x0,y0,x1,y1,val);
  4989. }
  4990. else
  4991. {
  4992. val = p->x[p->n-1];
  4993. }
  4994. ++p->m;
  4995. }
  4996. cmDspSetDouble(ctx,inst,kOutSlId,val);
  4997. }
  4998. break;
  4999. case kCmdSlId:
  5000. p->i = 0;
  5001. p->m = 0;
  5002. break;
  5003. }
  5004. }
  5005. return rc;
  5006. }
  5007. struct cmDspClass_str* cmSegLineClassCons( cmDspCtx_t* ctx )
  5008. {
  5009. cmDspClassSetup(&_cmSegLineDC,ctx,"SegLine",
  5010. NULL,
  5011. _cmDspSegLineAlloc,
  5012. NULL,
  5013. _cmDspSegLineReset,
  5014. NULL,
  5015. _cmDspSegLineRecv,
  5016. NULL,NULL,
  5017. "SegLine");
  5018. return &_cmSegLineDC;
  5019. }