libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDspFx.c 192KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321
  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 stateBaseXfId;
  1143. unsigned gainBaseXfId;
  1144. unsigned chCnt;
  1145. bool* chGateV;
  1146. unsigned onSymId;
  1147. unsigned offSymId;
  1148. } cmDspXfader_t;
  1149. cmDspInst_t* _cmDspXfaderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1150. {
  1151. cmDspVarArg_t args[] =
  1152. {
  1153. { "chs", kChCntXfId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input and Output channel count"},
  1154. { "ms", kFadeTimeMsXfId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade time in milliseonds."},
  1155. { "mgate", kMstrGateXfId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Master gate - can be used to set all gates."},
  1156. { "on", kOnXfId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'on' when all ch's transition from off to on."},
  1157. { "off", kOffXfId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'off' when all ch's transition from on to off."},
  1158. };
  1159. if( va_cnt < 1 )
  1160. {
  1161. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Xfader object must be given a channel count argument.");
  1162. return NULL;
  1163. }
  1164. va_list vl1;
  1165. va_copy(vl1,vl);
  1166. unsigned chCnt = va_arg(vl,int);
  1167. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1168. unsigned argCnt = fixArgCnt + 5*chCnt;
  1169. unsigned inBaseXfId = kGateBaseXfId + chCnt;
  1170. unsigned outBaseXfId = inBaseXfId + chCnt;
  1171. unsigned stateBaseXfId = outBaseXfId + chCnt;
  1172. unsigned gainBaseXfId = stateBaseXfId + chCnt;
  1173. cmDspVarArg_t a[ argCnt+1 ];
  1174. // setup the input gate detectors and the output gain args
  1175. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1176. cmDspArgSetupN(ctx, a, argCnt, kGateBaseXfId, chCnt, "gate", kGateBaseXfId, 0, 0, kInDsvFl | kBoolDsvFl, "gate flags");
  1177. cmDspArgSetupN(ctx, a, argCnt, inBaseXfId, chCnt, "in", inBaseXfId, 0, 0, kInDsvFl | kAudioBufDsvFl, "audio input");
  1178. cmDspArgSetupN(ctx, a, argCnt, outBaseXfId, chCnt, "out", outBaseXfId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "audio output");
  1179. cmDspArgSetupN(ctx, a, argCnt, stateBaseXfId, chCnt, "state",stateBaseXfId, 0, 0, kOutDsvFl | kBoolDsvFl, "current fader state");
  1180. cmDspArgSetupN(ctx, a, argCnt, gainBaseXfId, chCnt, "gain", gainBaseXfId, 0, 0, kOutDsvFl | kDoubleDsvFl, "gain output");
  1181. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1182. cmDspXfader_t* p = cmDspInstAlloc(cmDspXfader_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1);
  1183. double fadeTimeMs = cmDspDouble(&p->inst, kFadeTimeMsXfId );
  1184. p->xfdp = cmXfaderAlloc(ctx->cmProcCtx,NULL,cmDspSampleRate(ctx), chCnt, fadeTimeMs);
  1185. p->inBaseXfId = inBaseXfId;
  1186. p->outBaseXfId = outBaseXfId;
  1187. p->stateBaseXfId = stateBaseXfId;
  1188. p->gainBaseXfId = gainBaseXfId;
  1189. p->chCnt = chCnt;
  1190. p->chGateV = cmMemAllocZ(bool,p->chCnt);
  1191. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  1192. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  1193. // set default values for the parameters that were not explicitely set in the va_arg list
  1194. cmDspSetDefaultDouble( ctx, &p->inst, kFadeTimeMsXfId, 0, 100 );
  1195. cmDspSetDefaultBool( ctx, &p->inst, kMstrGateXfId, false, false);
  1196. cmDspSetDefaultSymbol( ctx, &p->inst, kOnXfId, p->onSymId );
  1197. cmDspSetDefaultSymbol( ctx, &p->inst, kOffXfId, p->offSymId );
  1198. int i;
  1199. for(i=0; i<chCnt; ++i)
  1200. cmDspSetDefaultBool( ctx, &p->inst, stateBaseXfId+i, false, false );
  1201. return &p->inst;
  1202. }
  1203. cmDspRC_t _cmDspXfaderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1204. {
  1205. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1206. cmMemFree(p->chGateV);
  1207. cmXfaderFree(&p->xfdp);
  1208. return kOkDspRC;
  1209. }
  1210. cmDspRC_t _cmDspXfaderReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1211. {
  1212. cmDspRC_t rc = kOkDspRC;
  1213. rc = cmDspApplyAllDefaults(ctx,inst);
  1214. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1215. // TODO: zeroing of output audio buffers should be built into cmDspApplyAllDefaults().
  1216. unsigned i;
  1217. for(i=0; i<p->chCnt; ++i)
  1218. cmDspZeroAudioBuf(ctx,inst,p->outBaseXfId + i);
  1219. return rc;
  1220. }
  1221. cmDspRC_t _cmDspXfaderExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1222. {
  1223. cmDspRC_t rc = cmOkRC;
  1224. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1225. unsigned i;
  1226. // update the internal cross fader by providing it with new gate settings and generate new gain values
  1227. cmXfaderExec( p->xfdp, cmDspSamplesPerCycle(ctx), p->chGateV, p->chCnt );
  1228. for(i=0; i<p->chCnt; ++i)
  1229. {
  1230. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->outBaseXfId+i,0);
  1231. cmSample_t* op = cmDspAudioBuf(ctx,inst,p->outBaseXfId+i,0);
  1232. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,p->inBaseXfId+i,0);
  1233. cmSample_t gain = (cmSample_t)p->xfdp->chArray[i].gain;
  1234. if( op != NULL )
  1235. {
  1236. if( ip == NULL )
  1237. cmVOS_Zero(op,n);
  1238. else
  1239. cmVOS_MultVVS(op,n,ip,gain);
  1240. }
  1241. if( p->xfdp->chArray[i].onFl )
  1242. cmDspSetBool(ctx,inst,p->stateBaseXfId+i,true);
  1243. if( p->xfdp->chArray[i].offFl )
  1244. cmDspSetBool(ctx,inst,p->stateBaseXfId+i,false);
  1245. // send the gain output
  1246. cmDspSetDouble(ctx,inst,p->gainBaseXfId+i,gain);
  1247. }
  1248. if( p->xfdp->onFl )
  1249. cmDspSetSymbol(ctx,inst,kOnXfId,p->onSymId);
  1250. if( p->xfdp->offFl )
  1251. cmDspSetSymbol(ctx,inst,kOffXfId,p->offSymId);
  1252. return rc;
  1253. }
  1254. cmDspRC_t _cmDspXfaderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1255. {
  1256. cmDspRC_t rc;
  1257. cmDspXfader_t* p = (cmDspXfader_t*)inst;
  1258. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  1259. return rc;
  1260. switch( evt->dstVarId )
  1261. {
  1262. case kFadeTimeMsXfId:
  1263. // if this is an xfade time event then transfer the new xfade time to the xfade proc
  1264. cmXfaderSetXfadeTime(p->xfdp,cmDspDouble(inst,kFadeTimeMsXfId));
  1265. break;
  1266. case kMstrGateXfId:
  1267. {
  1268. bool fl = cmDspBool(inst,kMstrGateXfId);
  1269. printf("mstr:%i\n",fl);
  1270. unsigned i;
  1271. for(i=0; i<p->chCnt; ++i)
  1272. p->chGateV[i] = fl;
  1273. }
  1274. break;
  1275. }
  1276. // record gate changes into p->chGateV[] for later use in _cmDspXfaderExec().
  1277. if( kGateBaseXfId <= evt->dstVarId && evt->dstVarId < kGateBaseXfId + p->chCnt )
  1278. {
  1279. p->chGateV[ evt->dstVarId - kGateBaseXfId ] = cmDspBool( inst, evt->dstVarId );
  1280. }
  1281. return rc;
  1282. }
  1283. struct cmDspClass_str* cmXfaderClassCons( cmDspCtx_t* ctx )
  1284. {
  1285. cmDspClassSetup(&_cmXfaderDC,ctx,"Xfader",
  1286. NULL,
  1287. _cmDspXfaderAlloc,
  1288. _cmDspXfaderFree,
  1289. _cmDspXfaderReset,
  1290. _cmDspXfaderExec,
  1291. _cmDspXfaderRecv,
  1292. NULL,NULL,
  1293. "Cross fade gain generator.");
  1294. return &_cmXfaderDC;
  1295. }
  1296. //==========================================================================================================================================
  1297. enum
  1298. {
  1299. kFnCcId,
  1300. kSelCcId,
  1301. kDoneCcId,
  1302. kGainBaseCcId
  1303. };
  1304. cmDspClass_t _cmChCfgDC;
  1305. typedef struct
  1306. {
  1307. cmDspInst_t inst;
  1308. cmChCfg* ccp;
  1309. unsigned midiBaseCcId;
  1310. unsigned hzBaseCcId;
  1311. unsigned chBaseCcId;
  1312. unsigned nsflBaseCcId;
  1313. unsigned nshzBaseCcId;
  1314. unsigned printSymId;
  1315. unsigned writeSymId;
  1316. unsigned nsCmdSymId;
  1317. unsigned hzCmdSymId;
  1318. unsigned resetSymId;
  1319. } cmDspChCfg_t;
  1320. cmDspInst_t* _cmDspChCfgAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1321. {
  1322. cmDspVarArg_t args[] =
  1323. {
  1324. { "fn", kFnCcId, 0, 0, kStrzDsvFl| kReqArgDsvFl, "Channel configuration JSON file name."},
  1325. { "sel", kSelCcId, 0, 0, kInDsvFl | kSymDsvFl, "Action selector: print | write | ns | reset"},
  1326. { "done", kDoneCcId,0, 0, kOutDsvFl | kSymDsvFl, "Trigger following action."}
  1327. };
  1328. if( va_cnt < 1 )
  1329. {
  1330. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The channel configuration object must be given a file name argument.");
  1331. return NULL;
  1332. }
  1333. va_list vl1;
  1334. va_copy(vl1,vl);
  1335. const cmChar_t* chCfgFn = va_arg(vl,cmChar_t*);
  1336. cmChCfg* ccp = cmChCfgAlloc( ctx->cmProcCtx, NULL, ctx->cmCtx, chCfgFn );
  1337. if( ccp == NULL || ccp->chCnt==0 )
  1338. {
  1339. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The channel configuration object could not be initialized with the file name '%s'.",cmStringNullGuard(chCfgFn));
  1340. return NULL;
  1341. }
  1342. unsigned chCnt = ccp->chCnt;
  1343. unsigned nsChCnt = ccp->nsChCnt;
  1344. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1345. unsigned argCnt = fixArgCnt + 5*chCnt + nsChCnt;
  1346. unsigned midiBaseCcId = kGainBaseCcId + chCnt;
  1347. unsigned hzBaseCcId = midiBaseCcId + chCnt;
  1348. unsigned chBaseCcId = hzBaseCcId + chCnt;
  1349. unsigned nsflBaseCcId = chBaseCcId + chCnt;
  1350. unsigned nshzBaseCcId = nsflBaseCcId + chCnt;
  1351. cmDspChCfg_t* p = NULL;
  1352. cmDspVarArg_t a[ argCnt+1 ];
  1353. unsigned i,j;
  1354. // setup the input gate detectors and the output gain args
  1355. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1356. cmDspArgSetupN(ctx, a, argCnt, kGainBaseCcId, chCnt, "gain", kGainBaseCcId, 0, 0, kSendDfltDsvFl | kInDsvFl | kOutDsvFl | kDoubleDsvFl, "Gain input and output.");
  1357. cmDspArgSetupN(ctx, a, argCnt, midiBaseCcId, chCnt, "midi", midiBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kUIntDsvFl, "MIDI pitch output");
  1358. cmDspArgSetupN(ctx, a, argCnt, hzBaseCcId, chCnt, "hz", hzBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kDoubleDsvFl, "pitch output in Hz");
  1359. cmDspArgSetupN(ctx, a, argCnt, chBaseCcId, chCnt, "ch", chBaseCcId, 0, 0, kSendDfltDsvFl | kOutDsvFl | kUIntDsvFl , "Audio channel index");
  1360. cmDspArgSetupN(ctx, a, argCnt, nsflBaseCcId, chCnt, "nsfl", nsflBaseCcId, 0, 0, kOutDsvFl | kBoolDsvFl, "noise shaper enables");
  1361. cmDspArgSetupN(ctx, a, argCnt, nshzBaseCcId, nsChCnt, "nshz", nshzBaseCcId, 0, 0, kOutDsvFl | kDoubleDsvFl, "noise-shaper pitch output in Hz");
  1362. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1363. if((p = cmDspInstAlloc(cmDspChCfg_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1364. return NULL;
  1365. p->ccp = ccp;
  1366. p->midiBaseCcId = midiBaseCcId;
  1367. p->hzBaseCcId = hzBaseCcId;
  1368. p->chBaseCcId = chBaseCcId;
  1369. p->nsflBaseCcId = nsflBaseCcId;
  1370. p->nshzBaseCcId = nshzBaseCcId;
  1371. p->writeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"write");
  1372. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  1373. p->nsCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"ns");
  1374. p->hzCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"hz");
  1375. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"reset");
  1376. for(i=0,j=0; i<chCnt; ++i)
  1377. {
  1378. double hz = cmMidiToHz(ccp->chArray[i].midi);
  1379. cmDspSetDefaultDouble(ctx, &p->inst, kGainBaseCcId + i, 0.0, ccp->chArray[i].gain);
  1380. cmDspSetDefaultUInt( ctx, &p->inst, p->midiBaseCcId + i, 0, ccp->chArray[i].midi );
  1381. cmDspSetDefaultDouble(ctx, &p->inst, p->hzBaseCcId + i, 0.0, hz );
  1382. cmDspSetDefaultUInt( ctx, &p->inst, p->chBaseCcId + i, 0, ccp->chArray[i].ch );
  1383. cmDspSetDefaultBool( ctx, &p->inst, p->nsflBaseCcId+i, false, false );
  1384. if( ccp->chArray[i].nsFl )
  1385. {
  1386. cmDspSetDefaultDouble(ctx,&p->inst, p->nshzBaseCcId+j, 0.0, hz);
  1387. ++j;
  1388. }
  1389. }
  1390. cmDspSetDefaultSymbol(ctx, &p->inst, kDoneCcId, cmInvalidId );
  1391. cmDspSetDefaultSymbol(ctx, &p->inst, kSelCcId, cmInvalidId );
  1392. return &p->inst;
  1393. }
  1394. cmDspRC_t _cmDspChCfgFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1395. {
  1396. cmDspChCfg_t* p = (cmDspChCfg_t*)inst;
  1397. cmChCfgFree(&p->ccp);
  1398. return kOkDspRC;
  1399. }
  1400. cmDspRC_t _cmDspChCfgReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1401. {
  1402. cmDspRC_t rc = kOkDspRC;
  1403. rc = cmDspApplyAllDefaults(ctx,inst);
  1404. return rc;
  1405. }
  1406. cmDspRC_t _cmDspChCfgRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1407. {
  1408. cmDspRC_t rc;
  1409. cmDspChCfg_t* p = (cmDspChCfg_t*)inst;
  1410. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1411. {
  1412. if( evt->dstVarId == kSelCcId )
  1413. {
  1414. unsigned selId = cmDspSymbol(inst,kSelCcId);
  1415. if( selId == p->resetSymId )
  1416. {
  1417. _cmDspChCfgReset(ctx,inst,evt);
  1418. }
  1419. else
  1420. if( selId == p->hzCmdSymId )
  1421. {
  1422. unsigned i;
  1423. // snd the hz
  1424. for(i=0; i<p->ccp->chCnt; ++i)
  1425. cmDspSetDouble(ctx,inst,p->hzBaseCcId+i,cmDspDouble(inst,p->hzBaseCcId+i));
  1426. }
  1427. else
  1428. if( selId == p->nsCmdSymId )
  1429. {
  1430. cmRptPrintf(ctx->rpt,"ChCfg:NS\n");
  1431. unsigned i;
  1432. // send the ns flags
  1433. for(i=0; i<p->ccp->chCnt; ++i)
  1434. cmDspSetBool(ctx,inst,p->nsflBaseCcId+i,p->ccp->chArray[i].nsFl);
  1435. // snd the ns hz
  1436. for(i=0; i<p->ccp->nsChCnt; ++i)
  1437. cmDspSetDouble(ctx,inst,p->nshzBaseCcId+i,cmDspDouble(inst,p->nshzBaseCcId+i));
  1438. cmDspSetSymbol(ctx,inst,kDoneCcId,p->nsCmdSymId);
  1439. }
  1440. else
  1441. if( selId == p->printSymId )
  1442. {
  1443. cmRptPrintf(&ctx->cmCtx->rpt,"Channel Cfg Report\n");
  1444. cmChCfgPrint(p->ccp, ctx->rpt );
  1445. }
  1446. else
  1447. {
  1448. if( selId == p->writeSymId )
  1449. {
  1450. unsigned i;
  1451. cmRptPrintf(&ctx->cmCtx->rpt,"writing\n");
  1452. // copy the gain values into the internal chCfg object ...
  1453. for(i=0; i<p->ccp->chCnt; ++i)
  1454. p->ccp->chArray[i].gain = cmDspDouble(inst,kGainBaseCcId+i);
  1455. // ... and write the object
  1456. cmChCfgWrite(p->ccp);
  1457. }
  1458. }
  1459. }
  1460. }
  1461. return rc;
  1462. }
  1463. struct cmDspClass_str* cmChCfgClassCons( cmDspCtx_t* ctx )
  1464. {
  1465. cmDspClassSetup(&_cmChCfgDC,ctx,"ChCfg",
  1466. NULL,
  1467. _cmDspChCfgAlloc,
  1468. _cmDspChCfgFree,
  1469. _cmDspChCfgReset,
  1470. NULL,
  1471. _cmDspChCfgRecv,
  1472. NULL,NULL,
  1473. "PP Channel Configuration Object.");
  1474. return &_cmChCfgDC;
  1475. }
  1476. //==========================================================================================================================================
  1477. enum
  1478. {
  1479. kRsrcCdId,
  1480. kMaxTimeSpanCdId,
  1481. kMinNoteCntCdId,
  1482. kDetectCdId,
  1483. kCountCdId,
  1484. kGateBaseCdId
  1485. };
  1486. cmDspClass_t _cmChordDetectDC;
  1487. typedef struct
  1488. {
  1489. cmDspInst_t inst;
  1490. cmChordDetect* cdp;
  1491. unsigned rmsBaseCdId;
  1492. unsigned chCnt;
  1493. bool* chGateV; // chGateV[ chCnt ]
  1494. cmReal_t* chRmsV; // chRmsV[ chCnt ]
  1495. unsigned* chEnaV; // chEnaV[ chCnt ]
  1496. unsigned count;
  1497. } cmDspChordDetect_t;
  1498. cmDspInst_t* _cmDspChordDetectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1499. {
  1500. cmDspVarArg_t args[] =
  1501. {
  1502. { "rsrc", kRsrcCdId, 0, 0, kStrzDsvFl | kReqArgDsvFl, "Channel enable flag array."},
  1503. { "span", kMaxTimeSpanCdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Max. onset time span."},
  1504. { "notes", kMinNoteCntCdId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Min. note count per chord."},
  1505. { "detect", kDetectCdId, 0, 0, kOutDsvFl | kBoolDsvFl, "Chord detect flag."},
  1506. { "count", kCountCdId, 0, 0, kOutDsvFl | kUIntDsvFl, "Count of chords detected since last reset."}
  1507. };
  1508. if( va_cnt < 1 )
  1509. {
  1510. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector must be given a channel enable flags array resource argument .");
  1511. return NULL;
  1512. }
  1513. va_list vl1;
  1514. va_copy(vl1,vl);
  1515. const cmChar_t* rsrc = va_arg(vl,const cmChar_t*);
  1516. unsigned* enaV = NULL;
  1517. unsigned chCnt = 0;
  1518. if( cmDspRsrcUIntArray( ctx->dspH, &chCnt, &enaV, rsrc, NULL ) != kOkDspRC )
  1519. {
  1520. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(rsrc));
  1521. return NULL;
  1522. }
  1523. //cmRptPrintf(ctx->rpt,"cd %s chs:%i\n",rsrc,chCnt);
  1524. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1525. unsigned argCnt = fixArgCnt + 2*chCnt;
  1526. unsigned rmsBaseCdId = kGateBaseCdId + chCnt;
  1527. cmDspVarArg_t a[ argCnt+1 ];
  1528. unsigned i;
  1529. cmDspChordDetect_t* p;
  1530. // setup the input gate detectors and the output gain args
  1531. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1532. cmDspArgSetupN(ctx, a, argCnt, kGateBaseCdId, chCnt, "gate", kGateBaseCdId, 0, 0, kInDsvFl | kOutDsvFl | kBoolDsvFl, "Channel gate input and output.");
  1533. cmDspArgSetupN(ctx, a, argCnt, rmsBaseCdId, chCnt, "rms", rmsBaseCdId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl, "Channel RMS input and output");
  1534. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1535. if((p = cmDspInstAlloc(cmDspChordDetect_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1536. return NULL;
  1537. double dfltMaxTimeSpanMs = 50.0;
  1538. unsigned dfltMinNoteCnt = 2;
  1539. cmDspSetDefaultDouble( ctx, &p->inst, kMaxTimeSpanCdId, 0.0, dfltMaxTimeSpanMs );
  1540. cmDspSetDefaultUInt( ctx, &p->inst, kMinNoteCntCdId, 0, dfltMinNoteCnt );
  1541. cmDspSetDefaultBool( ctx, &p->inst, kDetectCdId, false, false );
  1542. cmDspSetDefaultUInt( ctx, &p->inst, kCountCdId, 0, 0 );
  1543. for(i=0; i<chCnt; ++i)
  1544. {
  1545. cmDspSetDefaultBool( ctx, &p->inst, kGateBaseCdId + i, false, false );
  1546. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseCdId + i, 0.0, 0.0 );
  1547. }
  1548. p->cdp = cmChordDetectAlloc( ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), chCnt, cmDspDouble(&p->inst,kMaxTimeSpanCdId), cmDspUInt(&p->inst,kMinNoteCntCdId) );
  1549. p->rmsBaseCdId = rmsBaseCdId;
  1550. p->chCnt = chCnt;
  1551. p->chGateV = cmMemAllocZ(bool, chCnt);
  1552. p->chRmsV = cmMemAllocZ(cmReal_t, chCnt);
  1553. p->chEnaV = enaV;
  1554. return &p->inst;
  1555. }
  1556. cmDspRC_t _cmDspChordDetectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1557. {
  1558. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1559. cmChordDetectFree(&p->cdp);
  1560. cmMemFree(p->chGateV);
  1561. cmMemFree(p->chRmsV);
  1562. return kOkDspRC;
  1563. }
  1564. cmDspRC_t _cmDspChordDetectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1565. {
  1566. cmDspRC_t rc = kOkDspRC;
  1567. rc = cmDspApplyAllDefaults(ctx,inst);
  1568. return rc;
  1569. }
  1570. cmDspRC_t _cmDspChordDetectExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1571. {
  1572. cmDspRC_t rc = kOkDspRC;
  1573. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1574. cmChordDetectExec(p->cdp, cmDspSamplesPerCycle(ctx), p->chGateV, p->chRmsV, p->chCnt );
  1575. if( p->cdp->detectFl )
  1576. {
  1577. unsigned i;
  1578. for(i=0; i<p->chCnt; ++i)
  1579. {
  1580. bool fl = p->cdp->chArray[i].chordFl;
  1581. cmDspSetBool( ctx, inst, kGateBaseCdId + i, fl );
  1582. cmDspSetDouble( ctx, inst, p->rmsBaseCdId + i, fl ? p->cdp->chArray[i].candRMS : 0 );
  1583. }
  1584. cmDspSetBool(ctx, inst, kDetectCdId, true);
  1585. cmDspSetUInt(ctx, inst, kCountCdId, cmDspUInt(inst,kCountCdId) + 1 );
  1586. }
  1587. cmVOB_Zero(p->chGateV,p->chCnt);
  1588. cmVOR_Zero(p->chRmsV,p->chCnt);
  1589. return rc;
  1590. }
  1591. cmDspRC_t _cmDspChordDetectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1592. {
  1593. cmDspRC_t rc = kOkDspRC;
  1594. cmDspChordDetect_t* p = (cmDspChordDetect_t*)inst;
  1595. if( kGateBaseCdId <= evt->dstVarId && evt->dstVarId < kGateBaseCdId + p->chCnt )
  1596. {
  1597. unsigned idx = evt->dstVarId - kGateBaseCdId;
  1598. if( p->chEnaV[idx] )
  1599. p->chGateV[ idx ] = cmDsvGetBool(evt->valuePtr);
  1600. //cmRptPrintf(ctx->rpt,"cd gate:%i e:%i v:%i\n",idx,p->chEnaV[idx],p->chGateV[idx]);
  1601. }
  1602. else
  1603. if( p->rmsBaseCdId <= evt->dstVarId && evt->dstVarId < p->rmsBaseCdId + p->chCnt )
  1604. {
  1605. unsigned idx = evt->dstVarId - p->rmsBaseCdId;
  1606. if( p->chEnaV[idx] )
  1607. p->chRmsV[ idx ] = cmDsvGetReal( evt->valuePtr );
  1608. }
  1609. else
  1610. {
  1611. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1612. {
  1613. switch( evt->dstVarId )
  1614. {
  1615. case kMaxTimeSpanCdId:
  1616. cmChordDetectSetSpanMs(p->cdp,cmDspDouble(inst,kMaxTimeSpanCdId));
  1617. break;
  1618. case kMinNoteCntCdId:
  1619. p->cdp->minNotesPerChord = cmDspUInt(inst,kMinNoteCntCdId);
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. return rc;
  1625. }
  1626. struct cmDspClass_str* cmChordDetectClassCons( cmDspCtx_t* ctx )
  1627. {
  1628. cmDspClassSetup(&_cmChordDetectDC,ctx,"ChordDetect",
  1629. NULL,
  1630. _cmDspChordDetectAlloc,
  1631. _cmDspChordDetectFree,
  1632. _cmDspChordDetectReset,
  1633. _cmDspChordDetectExec,
  1634. _cmDspChordDetectRecv,
  1635. NULL,NULL,
  1636. "Chord detector.");
  1637. return &_cmChordDetectDC;
  1638. }
  1639. //==========================================================================================================================================
  1640. enum
  1641. {
  1642. kTimeFaId,
  1643. kGateFaId,
  1644. kInFaId,
  1645. kGainFaId,
  1646. kOutFaId
  1647. };
  1648. cmDspClass_t _cmFaderDC;
  1649. typedef struct
  1650. {
  1651. cmDspInst_t inst;
  1652. cmFader* fdp;
  1653. } cmDspFader_t;
  1654. cmDspInst_t* _cmDspFaderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1655. {
  1656. cmDspVarArg_t args[] =
  1657. {
  1658. { "time", kTimeFaId, 0, 0, kDoubleDsvFl | kOptArgDsvFl, "Fade time in milliseconds."},
  1659. { "gate", kGateFaId, 0, 0, kInDsvFl | kBoolDsvFl, "Gate control signal."},
  1660. { "in", kInFaId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input."},
  1661. { "gain", kGainFaId, 0, 0, kOutDsvFl | kDoubleDsvFl, "gain output."},
  1662. { "out", kOutFaId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio out."},
  1663. { NULL, 0, 0, 0, 0, NULL }
  1664. };
  1665. cmDspFader_t* p;
  1666. if((p = cmDspInstAlloc(cmDspFader_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  1667. return NULL;
  1668. double dfltFadeTimeMs = 100.0;
  1669. cmDspSetDefaultDouble( ctx, &p->inst, kTimeFaId, 0.0, dfltFadeTimeMs );
  1670. p->fdp = cmFaderAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), dfltFadeTimeMs );
  1671. return &p->inst;
  1672. }
  1673. cmDspRC_t _cmDspFaderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1674. {
  1675. cmDspFader_t* p = (cmDspFader_t*)inst;
  1676. cmFaderFree(&p->fdp);
  1677. return kOkDspRC;
  1678. }
  1679. cmDspRC_t _cmDspFaderReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1680. {
  1681. cmDspRC_t rc = kOkDspRC;
  1682. cmDspFader_t* p = (cmDspFader_t*)inst;
  1683. rc = cmDspApplyAllDefaults(ctx,inst);
  1684. cmDspZeroAudioBuf(ctx,inst,kOutFaId);
  1685. cmFaderSetFadeTime(p->fdp,cmDspDouble(inst,kTimeFaId));
  1686. return rc;
  1687. }
  1688. cmDspRC_t _cmDspFaderExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1689. {
  1690. cmDspRC_t rc = kOkDspRC;
  1691. cmDspFader_t* p = (cmDspFader_t*)inst;
  1692. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutFaId,0);
  1693. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutFaId,0);
  1694. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInFaId,0);
  1695. cmFaderExec(p->fdp,n,cmDspBool(inst,kGateFaId),false,ip,op);
  1696. cmDspSetDouble(ctx,inst,kGainFaId,p->fdp->gain);
  1697. return rc;
  1698. }
  1699. cmDspRC_t _cmDspFaderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1700. {
  1701. cmDspRC_t rc = kOkDspRC;
  1702. cmDspFader_t* p = (cmDspFader_t*)inst;
  1703. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  1704. {
  1705. if( evt->dstVarId == kTimeFaId )
  1706. cmFaderSetFadeTime(p->fdp,cmDspDouble(inst,kTimeFaId));
  1707. }
  1708. return rc;
  1709. }
  1710. struct cmDspClass_str* cmFaderClassCons( cmDspCtx_t* ctx )
  1711. {
  1712. cmDspClassSetup(&_cmFaderDC,ctx,"Fader",
  1713. NULL,
  1714. _cmDspFaderAlloc,
  1715. _cmDspFaderFree,
  1716. _cmDspFaderReset,
  1717. _cmDspFaderExec,
  1718. _cmDspFaderRecv,
  1719. NULL,NULL,
  1720. "Audio fade in/out controller.");
  1721. return &_cmFaderDC;
  1722. }
  1723. //==========================================================================================================================================
  1724. enum
  1725. {
  1726. kChCntNsId,
  1727. kTrigNsId,
  1728. kDoneNsId,
  1729. kGateBaseNsId
  1730. };
  1731. enum
  1732. {
  1733. kGroupNonNsId,
  1734. kGroup0NsId,
  1735. kGroup1NsId
  1736. };
  1737. cmDspClass_t _cmNoteSelectDC;
  1738. typedef struct
  1739. {
  1740. cmDspInst_t inst;
  1741. unsigned chCnt;
  1742. unsigned rmsBaseNsId;
  1743. unsigned gate0BaseNsId;
  1744. unsigned gate1BaseNsId;
  1745. unsigned gate2BaseNsId;
  1746. unsigned gate3BaseNsId;
  1747. unsigned gate4BaseNsId;
  1748. bool* chGateV; // chGateV[chCnt]
  1749. cmReal_t* chRmsV; // chRmsV[ chCnt ];
  1750. unsigned* chGroupV; // chGroupV[ chCnt ] (0=non-chord 1=low/high 2=middle)
  1751. unsigned count;
  1752. unsigned doneSymId;
  1753. } cmDspNoteSelect_t;
  1754. cmDspInst_t* _cmDspNoteSelectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1755. {
  1756. cmDspVarArg_t args[] =
  1757. {
  1758. { "ch_cnt", kChCntNsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count."},
  1759. { "trig", kTrigNsId, 0, 0, kInDsvFl | kBoolDsvFl, "Trigger note selection."},
  1760. { "done", kDoneNsId, 0, 0, kOutDsvFl | kSymDsvFl, "Sends 'done' after new set of outputs have been sent."},
  1761. };
  1762. if( va_cnt < 1 )
  1763. {
  1764. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The note selector must be given a channel count argument .");
  1765. return NULL;
  1766. }
  1767. va_list vl1;
  1768. va_copy(vl1,vl);
  1769. unsigned CD0chanN = 0;
  1770. unsigned CD1chanN = 0;
  1771. unsigned* CD0chan = NULL;
  1772. unsigned* CD1chan = NULL;
  1773. const cmChar_t* CD0rsrc = "CD0chan";
  1774. const cmChar_t* CD1rsrc = "CD1chan";
  1775. if( cmDspRsrcUIntArray( ctx->dspH, &CD0chanN, &CD0chan, CD0rsrc, NULL ) != kOkDspRC )
  1776. {
  1777. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(CD0rsrc));
  1778. return NULL;
  1779. }
  1780. if( cmDspRsrcUIntArray( ctx->dspH, &CD1chanN, &CD1chan, CD1rsrc, NULL ) != kOkDspRC )
  1781. {
  1782. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The chord detector channel index resource '%s' could not be read.",cmStringNullGuard(CD1rsrc));
  1783. return NULL;
  1784. }
  1785. unsigned chCnt = va_arg(vl,unsigned);
  1786. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  1787. unsigned argCnt = fixArgCnt + 7*chCnt;
  1788. unsigned rmsBaseNsId = kGateBaseNsId + 1 * chCnt;
  1789. unsigned gate0BaseNsId = kGateBaseNsId + 2 * chCnt;
  1790. unsigned gate1BaseNsId = kGateBaseNsId + 3 * chCnt;
  1791. unsigned gate2BaseNsId = kGateBaseNsId + 4 * chCnt;
  1792. unsigned gate3BaseNsId = kGateBaseNsId + 5 * chCnt;
  1793. unsigned gate4BaseNsId = kGateBaseNsId + 6 * chCnt;
  1794. cmDspVarArg_t a[ argCnt+1 ];
  1795. unsigned i;
  1796. cmDspNoteSelect_t* p;
  1797. // setup the input gate detectors and the output gain args
  1798. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1799. cmDspArgSetupN(ctx, a, argCnt, kGateBaseNsId, chCnt, "gate", kGateBaseNsId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  1800. cmDspArgSetupN(ctx, a, argCnt, rmsBaseNsId, chCnt, "rms", rmsBaseNsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  1801. cmDspArgSetupN(ctx, a, argCnt, gate0BaseNsId, chCnt, "gate-0", gate0BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 0 output.");
  1802. cmDspArgSetupN(ctx, a, argCnt, gate1BaseNsId, chCnt, "gate-1", gate1BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 1 output.");
  1803. cmDspArgSetupN(ctx, a, argCnt, gate2BaseNsId, chCnt, "gate-2", gate2BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 2 output.");
  1804. cmDspArgSetupN(ctx, a, argCnt, gate3BaseNsId, chCnt, "gate-3", gate3BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 3 output.");
  1805. cmDspArgSetupN(ctx, a, argCnt, gate4BaseNsId, chCnt, "gate-4", gate4BaseNsId, 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 4 output.");
  1806. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  1807. if((p = cmDspInstAlloc(cmDspNoteSelect_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  1808. return NULL;
  1809. cmDspSetDefaultBool( ctx, &p->inst, kTrigNsId, false, false );
  1810. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneNsId, cmInvalidId );
  1811. p->rmsBaseNsId = rmsBaseNsId;
  1812. p->gate0BaseNsId = gate0BaseNsId;
  1813. p->gate1BaseNsId = gate1BaseNsId;
  1814. p->gate2BaseNsId = gate2BaseNsId;
  1815. p->gate3BaseNsId = gate3BaseNsId;
  1816. p->gate4BaseNsId = gate4BaseNsId;
  1817. p->chCnt = chCnt;
  1818. p->chGateV = cmMemAllocZ(bool,chCnt);
  1819. p->chRmsV = cmMemAllocZ(cmReal_t,chCnt);
  1820. p->chGroupV = cmMemAllocZ(unsigned,chCnt);
  1821. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  1822. for(i=0; i<CD0chanN; ++i)
  1823. {
  1824. if( CD0chan[i] >= chCnt )
  1825. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The chord detector resource array '%s' value %i is out of range %i.",cmStringNullGuard(CD0rsrc),CD0chan[i],chCnt);
  1826. else
  1827. p->chGroupV[ CD0chan[i] ] = kGroup0NsId;
  1828. }
  1829. for(i=0; i<CD1chanN; ++i)
  1830. {
  1831. if( CD1chan[i] >= chCnt )
  1832. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The chord detector resource array '%s' value %i is out of range %i.",cmStringNullGuard(CD1rsrc),CD1chan[i],chCnt);
  1833. else
  1834. p->chGroupV[ CD1chan[i] ] = kGroup1NsId;
  1835. }
  1836. for(i=0; i<chCnt; ++i)
  1837. {
  1838. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseNsId+i, 0.0, 0.0 );
  1839. cmDspSetDefaultBool( ctx, &p->inst, gate0BaseNsId+i, false, false );
  1840. cmDspSetDefaultBool( ctx, &p->inst, gate1BaseNsId+i, false, false );
  1841. cmDspSetDefaultBool( ctx, &p->inst, gate2BaseNsId+i, false, false );
  1842. cmDspSetDefaultBool( ctx, &p->inst, gate3BaseNsId+i, false, false );
  1843. // the non-chord channel selections should always be on
  1844. cmDspSetDefaultBool( ctx, &p->inst, gate4BaseNsId+i, false, p->chGroupV[i] == kGroupNonNsId );
  1845. }
  1846. return &p->inst;
  1847. }
  1848. cmDspRC_t _cmDspNoteSelectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1849. {
  1850. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1851. cmMemFree(p->chGateV);
  1852. cmMemFree(p->chRmsV);
  1853. cmMemFree(p->chGroupV);
  1854. return kOkDspRC;
  1855. }
  1856. cmDspRC_t _cmDspNoteSelectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1857. {
  1858. cmDspRC_t rc = kOkDspRC;
  1859. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1860. rc = cmDspApplyAllDefaults(ctx,inst);
  1861. cmVOR_Zero(p->chRmsV,p->chCnt);
  1862. return rc;
  1863. }
  1864. cmDspRC_t _cmDspNoteSelectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1865. {
  1866. cmDspRC_t rc = kOkDspRC;
  1867. cmDspNoteSelect_t* p = (cmDspNoteSelect_t*)inst;
  1868. // store incoming gate values
  1869. if( kGateBaseNsId <= evt->dstVarId && evt->dstVarId < kGateBaseNsId + p->chCnt )
  1870. p->chGateV[ evt->dstVarId - kGateBaseNsId ] = cmDsvGetBool(evt->valuePtr);
  1871. else
  1872. // store incoming RMS values
  1873. if( p->rmsBaseNsId <= evt->dstVarId && evt->dstVarId < p->rmsBaseNsId + p->chCnt )
  1874. p->chRmsV[ evt->dstVarId - p->rmsBaseNsId ] = cmDsvGetReal( evt->valuePtr );
  1875. else
  1876. {
  1877. // if a chord detection was triggered
  1878. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && evt->dstVarId == kTrigNsId )
  1879. {
  1880. unsigned i;
  1881. cmReal_t maxRms = 0;
  1882. unsigned maxIdx = cmInvalidIdx;
  1883. for(i=1; i<p->chCnt; ++i)
  1884. {
  1885. // if this channel had an onset and is a possible chord note and is the max RMS chord note
  1886. if( p->chGroupV[i] != kGroupNonNsId && p->chGateV[i] && (maxIdx==cmInvalidIdx || p->chRmsV[i] > maxRms) )
  1887. {
  1888. maxRms = p->chRmsV[i];
  1889. maxIdx = i;
  1890. }
  1891. }
  1892. for(i=0; i<p->chCnt; ++i)
  1893. {
  1894. bool fl = p->chGroupV[i] != kGroupNonNsId;
  1895. bool chosenFl = fl && i==maxIdx;
  1896. bool otherFl = fl && i!=maxIdx && p->chGateV[i];
  1897. bool cd0Fl = p->chGroupV[i]==kGroup0NsId && (!otherFl) && (!chosenFl);
  1898. bool cd1Fl = p->chGroupV[i]==kGroup1NsId && (!otherFl) && (!chosenFl);
  1899. // gate set 0: set output gate for max chord note
  1900. cmDspSetBool(ctx,inst,p->gate0BaseNsId+i, chosenFl );
  1901. // gate set 1: set output gate for non-max chord notes
  1902. cmDspSetBool(ctx,inst,p->gate1BaseNsId+i, otherFl );
  1903. // gate set 2: set output gate for non-chord notes
  1904. cmDspSetBool(ctx,inst,p->gate2BaseNsId+i, cd0Fl );
  1905. // gate set 3: set output gate for non-chord notes
  1906. cmDspSetBool(ctx,inst,p->gate3BaseNsId+i, cd1Fl);
  1907. // gate set 4: set output gate for non-chord notes
  1908. cmDspSetBool(ctx,inst,p->gate4BaseNsId+i, !fl );
  1909. }
  1910. // send the 'done' symbol to notify the gate receivers that the
  1911. // new set of gates is complete
  1912. cmDspSetSymbol(ctx,inst,kDoneNsId, p->doneSymId);
  1913. // zero the RMS vector
  1914. cmVOR_Zero(p->chRmsV,p->chCnt);
  1915. }
  1916. }
  1917. return rc;
  1918. }
  1919. struct cmDspClass_str* cmNoteSelectClassCons( cmDspCtx_t* ctx )
  1920. {
  1921. cmDspClassSetup(&_cmNoteSelectDC,ctx,"NoteSelect",
  1922. NULL,
  1923. _cmDspNoteSelectAlloc,
  1924. _cmDspNoteSelectFree,
  1925. _cmDspNoteSelectReset,
  1926. NULL,
  1927. _cmDspNoteSelectRecv,
  1928. NULL,NULL,
  1929. "Chord detector.");
  1930. return &_cmNoteSelectDC;
  1931. }
  1932. //==========================================================================================================================================
  1933. enum
  1934. {
  1935. kTrigNnId,
  1936. kDoneNnId,
  1937. kGateBaseNnId
  1938. };
  1939. cmDspClass_t _cmNetNoteSelectDC;
  1940. #define _cmNetNoteSelPortCnt (10)
  1941. typedef struct
  1942. {
  1943. cmDspInst_t inst;
  1944. unsigned chCnt;
  1945. unsigned rmsBaseNnId;
  1946. unsigned gateBaseNNId[ _cmNetNoteSelPortCnt ];
  1947. bool* chGateV; // chGateV[chCnt]
  1948. cmReal_t* chRmsV; // chRmsV[ chCnt ];
  1949. unsigned* portChCntV; // portChCntV[ 10 ]
  1950. unsigned* portBaseIdV; // portBaseIdV[ 10 ]
  1951. unsigned* chPortV; // chPortV[ chCnt ]
  1952. unsigned* chPortIdxV; // chPortIdxV[ chCnt ]
  1953. unsigned* ncPortV; // ncPortV[ chCnt ]
  1954. unsigned* ncPortIdxV; // ncPortIdxV[ chCnt ]
  1955. unsigned count;
  1956. unsigned doneSymId;
  1957. } cmDspNetNoteSelect_t;
  1958. cmDspInst_t* _cmDspNetNoteSelectAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1959. {
  1960. unsigned chCnt = 0;
  1961. const cmChar_t* label = NULL;
  1962. const cmChar_t* chPortRsrc = "nsChSelChV";
  1963. unsigned* chPortV = NULL;
  1964. const cmChar_t* chPortIdxRsrc = "nsChSelChIdxV";
  1965. unsigned* chPortIdxV = NULL;
  1966. const cmChar_t* ncPortRsrc = "nsNcSelChV";
  1967. unsigned* ncPortV = NULL;
  1968. const cmChar_t* ncPortIdxRsrc = "nsNcSelChIdxV";
  1969. unsigned* ncPortIdxV = NULL;
  1970. unsigned i,j,n;
  1971. cmDspVarArg_t args[] =
  1972. {
  1973. { "trig", kTrigNnId, 0, 0, kInDsvFl | kBoolDsvFl, "Trigger note selection."},
  1974. { "done", kDoneNnId, 0, 0, kOutDsvFl | kSymDsvFl, "Sends 'done' after new set of outputs have been sent."},
  1975. };
  1976. if( cmDspRsrcUIntArray( ctx->dspH, &chCnt, &chPortV, label = chPortRsrc, NULL ) != kOkDspRC )
  1977. {
  1978. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1979. return NULL;
  1980. }
  1981. if( cmDspRsrcUIntArray( ctx->dspH, &n, &chPortIdxV, label = chPortIdxRsrc, NULL ) != kOkDspRC )
  1982. {
  1983. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1984. return NULL;
  1985. }
  1986. assert(n == chCnt );
  1987. if( cmDspRsrcUIntArray( ctx->dspH, &n, &ncPortV, label = ncPortRsrc, NULL ) != kOkDspRC )
  1988. {
  1989. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1990. return NULL;
  1991. }
  1992. assert(n == chCnt );
  1993. if( cmDspRsrcUIntArray( ctx->dspH, &n, &ncPortIdxV, label = ncPortIdxRsrc, NULL ) != kOkDspRC )
  1994. {
  1995. cmDspClassErr(ctx,classPtr,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",label);
  1996. return NULL;
  1997. }
  1998. assert(n == chCnt );
  1999. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  2000. unsigned rmsBaseNnId = kGateBaseNnId + chCnt;
  2001. // get the count of ch's on each port
  2002. unsigned* portChCntV = cmLhAllocZ( ctx->lhH, unsigned, _cmNetNoteSelPortCnt );
  2003. unsigned* portBaseIdV = cmLhAllocZ( ctx->lhH, unsigned, _cmNetNoteSelPortCnt );
  2004. for(i=0; i<_cmNetNoteSelPortCnt; ++i)
  2005. {
  2006. // get the count of ch's in the ith gate output port
  2007. portChCntV[i] = cmVOU_Count( chPortV, chCnt, i ) + cmVOU_Count( ncPortV, chCnt, i );
  2008. // ports 1 and 6 are duplicates of ports 0 and 5
  2009. if( i == 1 || i == 6 )
  2010. portChCntV[i] = portChCntV[i-1];
  2011. // set the base port id for this port
  2012. if( i > 0 )
  2013. portBaseIdV[i] = portBaseIdV[i-1] + portChCntV[i-1];
  2014. else
  2015. portBaseIdV[ i ] = rmsBaseNnId + chCnt;
  2016. }
  2017. unsigned argCnt = fixArgCnt + (2*chCnt) + cmVOU_Sum(portChCntV,_cmNetNoteSelPortCnt );
  2018. cmDspVarArg_t a[ argCnt+1 ];
  2019. cmDspNetNoteSelect_t* p;
  2020. // setup the input gate detectors and the output gain args
  2021. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2022. cmDspArgSetupN(ctx, a, argCnt, kGateBaseNnId, chCnt, "gate", kGateBaseNnId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  2023. cmDspArgSetupN(ctx, a, argCnt, rmsBaseNnId, chCnt, "rms", rmsBaseNnId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  2024. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[0], portChCntV[0], "gate-0", portBaseIdV[0], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 0 output.");
  2025. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[1], portChCntV[1], "gate-1", portBaseIdV[1], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 1 output.");
  2026. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[2], portChCntV[2], "gate-2", portBaseIdV[2], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 2 output.");
  2027. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[3], portChCntV[3], "gate-3", portBaseIdV[3], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 3 output.");
  2028. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[4], portChCntV[4], "gate-4", portBaseIdV[4], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 4 output.");
  2029. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[5], portChCntV[5], "gate-5", portBaseIdV[5], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 5 output.");
  2030. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[6], portChCntV[6], "gate-6", portBaseIdV[6], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 6 output.");
  2031. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[7], portChCntV[7], "gate-7", portBaseIdV[7], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 7 output.");
  2032. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[8], portChCntV[8], "gate-8", portBaseIdV[8], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 8 output.");
  2033. cmDspArgSetupN(ctx, a, argCnt, portBaseIdV[9], portChCntV[9], "gate-9", portBaseIdV[9], 0, 0, kOutDsvFl | kBoolDsvFl, "Channel gate set 9 output.");
  2034. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2035. if((p = cmDspInstAlloc(cmDspNetNoteSelect_t,ctx,classPtr,a,instSymId,id,storeSymId,0,vl)) == NULL )
  2036. return NULL;
  2037. cmDspSetDefaultBool( ctx, &p->inst, kTrigNnId, false, false );
  2038. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneNnId, cmInvalidId );
  2039. p->rmsBaseNnId = rmsBaseNnId;
  2040. p->chCnt = chCnt;
  2041. p->chGateV = cmMemAllocZ(bool,chCnt);
  2042. p->chRmsV = cmMemAllocZ(cmReal_t,chCnt);
  2043. p->portChCntV = portChCntV;
  2044. p->portBaseIdV = portBaseIdV;
  2045. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2046. p->chPortV = chPortV;
  2047. p->chPortIdxV = chPortIdxV;
  2048. p->ncPortV = ncPortV;
  2049. p->ncPortIdxV = ncPortIdxV;
  2050. for(i=0; i<chCnt; ++i)
  2051. {
  2052. cmDspSetDefaultBool( ctx, &p->inst, kGateBaseNnId+i, false, false );
  2053. cmDspSetDefaultDouble(ctx, &p->inst, rmsBaseNnId+i, 0.0, 0.0 );
  2054. }
  2055. for(i=0; i<_cmNetNoteSelPortCnt; ++i)
  2056. for(j=0; j<p->portChCntV[i]; ++j)
  2057. cmDspSetDefaultBool( ctx, &p->inst, p->portBaseIdV[i]+j, false, false );
  2058. return &p->inst;
  2059. }
  2060. cmDspRC_t _cmDspNetNoteSelectFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2061. {
  2062. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2063. cmMemFree(p->chGateV);
  2064. cmMemFree(p->chRmsV);
  2065. return kOkDspRC;
  2066. }
  2067. cmDspRC_t _cmDspNetNoteSelectReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2068. {
  2069. cmDspRC_t rc = kOkDspRC;
  2070. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2071. rc = cmDspApplyAllDefaults(ctx,inst);
  2072. cmVOR_Zero(p->chRmsV,p->chCnt);
  2073. return rc;
  2074. }
  2075. cmDspRC_t _cmDspNetNoteSelectRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2076. {
  2077. cmDspRC_t rc = kOkDspRC;
  2078. cmDspNetNoteSelect_t* p = (cmDspNetNoteSelect_t*)inst;
  2079. // store incoming gate values
  2080. if( kGateBaseNnId <= evt->dstVarId && evt->dstVarId < kGateBaseNnId + p->chCnt )
  2081. {
  2082. p->chGateV[ evt->dstVarId - kGateBaseNnId ] = cmDsvGetBool(evt->valuePtr);
  2083. //unsigned idx = evt->dstVarId - kGateBaseNnId;
  2084. //cmRptPrintf(ctx->rpt,"ns gate:%i %i\n",idx, p->chGateV[ idx ]);
  2085. }
  2086. else
  2087. // store incoming RMS values
  2088. if( p->rmsBaseNnId <= evt->dstVarId && evt->dstVarId < p->rmsBaseNnId + p->chCnt )
  2089. {
  2090. p->chRmsV[ evt->dstVarId - p->rmsBaseNnId ] = cmDsvGetReal( evt->valuePtr );
  2091. }
  2092. else
  2093. {
  2094. // if a chord detection was triggered
  2095. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && evt->dstVarId == kTrigNnId )
  2096. {
  2097. unsigned i;
  2098. cmReal_t maxRms = 0;
  2099. unsigned maxIdx = cmInvalidIdx;
  2100. for(i=1; i<p->chCnt; ++i)
  2101. {
  2102. // if this channel had an onset and is a possible chord note and is the max RMS chord note
  2103. if( p->chGateV[i] && (maxIdx==cmInvalidIdx || p->chRmsV[i] > maxRms) )
  2104. {
  2105. maxRms = p->chRmsV[i];
  2106. maxIdx = i;
  2107. }
  2108. }
  2109. for(i=0; i<p->chCnt; ++i)
  2110. {
  2111. bool chosenFl = i==maxIdx;
  2112. bool otherFl = i!=maxIdx && p->chGateV[i];
  2113. bool nonFl = chosenFl==false && otherFl==false;
  2114. unsigned k;
  2115. // if this is a chord channel
  2116. if( p->chPortV[i] != cmInvalidIdx )
  2117. {
  2118. // get the port associated with this chord note
  2119. k = p->chPortV[i];
  2120. assert( k+1 < _cmNetNoteSelPortCnt );
  2121. assert( p->chPortIdxV[i] < p->portChCntV[k] && p->chPortIdxV[i] < p->portChCntV[k+1] );
  2122. // set the chosen and other gate outputs based on the state of
  2123. // chosenFl and otherFl
  2124. cmDspSetBool(ctx,inst,p->portBaseIdV[ k ] + p->chPortIdxV[i],chosenFl);
  2125. cmDspSetBool(ctx,inst,p->portBaseIdV[ k+1 ] + p->chPortIdxV[i],otherFl);
  2126. }
  2127. // all channels have a 'single' note channel
  2128. assert( p->ncPortV[i] != cmInvalidIdx );
  2129. k = p->ncPortV[i];
  2130. assert( k < _cmNetNoteSelPortCnt );
  2131. assert( p->ncPortIdxV[i] < p->portChCntV[k] );
  2132. cmDspSetBool(ctx,inst,p->portBaseIdV[k] + p->ncPortIdxV[i],nonFl);
  2133. }
  2134. // send the 'done' symbol to notify the gate receivers that the
  2135. // new set of gates is complete
  2136. cmDspSetSymbol(ctx,inst,kDoneNnId, p->doneSymId);
  2137. // zero the RMS vector
  2138. cmVOR_Zero(p->chRmsV,p->chCnt);
  2139. }
  2140. }
  2141. return rc;
  2142. }
  2143. struct cmDspClass_str* cmNetNoteSelectClassCons( cmDspCtx_t* ctx )
  2144. {
  2145. cmDspClassSetup(&_cmNetNoteSelectDC,ctx,"NetNoteSelect",
  2146. NULL,
  2147. _cmDspNetNoteSelectAlloc,
  2148. _cmDspNetNoteSelectFree,
  2149. _cmDspNetNoteSelectReset,
  2150. NULL,
  2151. _cmDspNetNoteSelectRecv,
  2152. NULL,NULL,
  2153. "Chord detector.");
  2154. return &_cmNetNoteSelectDC;
  2155. }
  2156. //==========================================================================================================================================
  2157. enum
  2158. {
  2159. kBypassCfId,
  2160. kMinHzCfId,
  2161. kFbFlCfId,
  2162. kHzCfId,
  2163. kAlphaCfId,
  2164. kInCfId,
  2165. kOutCfId
  2166. };
  2167. cmDspClass_t _cmCombFiltDC;
  2168. typedef struct
  2169. {
  2170. cmDspInst_t inst;
  2171. cmCombFilt* cfp;
  2172. } cmDspCombFilt_t;
  2173. cmDspInst_t* _cmDspCombFiltAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2174. {
  2175. cmDspVarArg_t args[] =
  2176. {
  2177. { "bypass",kBypassCfId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable flag." },
  2178. { "minhz", kMinHzCfId, 0, 0, kDoubleDsvFl | kReqArgDsvFl, "Minimum frequency limit."},
  2179. { "fb", kFbFlCfId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Configure the filter in feedback mode."},
  2180. { "hz", kHzCfId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Lowest comb frequency." },
  2181. { "alpha", kAlphaCfId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Filter coefficent."},
  2182. { "in", kInCfId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input."},
  2183. { "out", kOutCfId, 0, 1, kOutDsvFl| kAudioBufDsvFl, "Audio out."},
  2184. { NULL, 0, 0, 0, 0, NULL }
  2185. };
  2186. cmDspCombFilt_t* p;
  2187. if((p = cmDspInstAlloc(cmDspCombFilt_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  2188. return NULL;
  2189. p->cfp = cmCombFiltAlloc(ctx->cmProcCtx, NULL,
  2190. cmDspSampleRate(ctx),
  2191. cmDspBool(&p->inst,kFbFlCfId),
  2192. cmDspDouble(&p->inst,kMinHzCfId),
  2193. cmDspDouble(&p->inst,kAlphaCfId),
  2194. cmDspDouble(&p->inst,kHzCfId),
  2195. cmDspBool(&p->inst,kBypassCfId));
  2196. return &p->inst;
  2197. }
  2198. cmDspRC_t _cmDspCombFiltFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2199. {
  2200. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2201. cmCombFiltFree(&p->cfp);
  2202. return kOkDspRC;
  2203. }
  2204. cmDspRC_t _cmDspCombFiltReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2205. {
  2206. cmDspRC_t rc = kOkDspRC;
  2207. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2208. rc = cmDspApplyAllDefaults(ctx,inst);
  2209. cmDspZeroAudioBuf(ctx,inst,kOutCfId);
  2210. cmCombFiltInit(p->cfp,
  2211. cmDspSampleRate(ctx),
  2212. cmDspBool(inst,kFbFlCfId),
  2213. cmDspDouble(inst,kMinHzCfId),
  2214. cmDspDouble(inst,kAlphaCfId),
  2215. cmDspDouble(inst,kHzCfId),
  2216. cmDspBool(inst,kBypassCfId));
  2217. return rc;
  2218. }
  2219. cmDspRC_t _cmDspCombFiltExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2220. {
  2221. cmDspRC_t rc = kOkDspRC;
  2222. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2223. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutCfId,0);
  2224. cmSample_t* op = cmDspAudioBuf(ctx,inst,kOutCfId,0);
  2225. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kInCfId,0);
  2226. cmCombFiltExec(p->cfp,ip,op,n);
  2227. return rc;
  2228. }
  2229. cmDspRC_t _cmDspCombFiltRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2230. {
  2231. cmDspRC_t rc = kOkDspRC;
  2232. cmDspCombFilt_t* p = (cmDspCombFilt_t*)inst;
  2233. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2234. {
  2235. switch( evt->dstVarId )
  2236. {
  2237. case kHzCfId:
  2238. cmCombFiltSetHz(p->cfp,cmDspDouble(inst,evt->dstVarId));
  2239. //printf("%s hz:%f\n",cmSymTblLabel(ctx->stH,inst->symId),cmDspDouble(inst,evt->dstVarId));
  2240. break;
  2241. case kAlphaCfId:
  2242. cmCombFiltSetAlpha(p->cfp,cmDspDouble(inst,evt->dstVarId));
  2243. break;
  2244. case kBypassCfId:
  2245. p->cfp->bypassFl = cmDspBool(inst,evt->dstVarId);
  2246. break;
  2247. }
  2248. }
  2249. return rc;
  2250. }
  2251. struct cmDspClass_str* cmCombFiltClassCons( cmDspCtx_t* ctx )
  2252. {
  2253. cmDspClassSetup(&_cmCombFiltDC,ctx,"CombFilt",
  2254. NULL,
  2255. _cmDspCombFiltAlloc,
  2256. _cmDspCombFiltFree,
  2257. _cmDspCombFiltReset,
  2258. _cmDspCombFiltExec,
  2259. _cmDspCombFiltRecv,
  2260. NULL,NULL,
  2261. "Comb Filter");
  2262. return &_cmCombFiltDC;
  2263. }
  2264. //==========================================================================================================================================
  2265. enum
  2266. {
  2267. kPortCntSoId,
  2268. kOpSoId,
  2269. kOutSoId,
  2270. kBaseOpdSoId
  2271. };
  2272. cmDspClass_t _cmScalarOpDC;
  2273. struct cmDspScalarOp_str;
  2274. typedef cmDspRC_t (*_cmDspScalarOpFunc_t)(cmDspCtx_t* ctx, cmDspInst_t* inst );
  2275. typedef struct cmDspScalar_str
  2276. {
  2277. cmDspInst_t inst;
  2278. _cmDspScalarOpFunc_t func;
  2279. unsigned inPortCnt;
  2280. } cmDspScalarOp_t;
  2281. cmDspRC_t _cmDspScalarOpFuncMult(cmDspCtx_t* ctx, cmDspInst_t* inst )
  2282. {
  2283. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2284. double val = 1.0;
  2285. unsigned i;
  2286. for(i=0; i<p->inPortCnt; ++i)
  2287. val *= cmDspDouble( inst, kBaseOpdSoId+i );
  2288. cmDspSetDouble( ctx, inst, kOutSoId, val );
  2289. return kOkDspRC;
  2290. }
  2291. cmDspRC_t _cmDspScalarOpFuncAdd(cmDspCtx_t* ctx, cmDspInst_t* inst )
  2292. {
  2293. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2294. double val = 0;
  2295. unsigned i;
  2296. for(i=0; i<p->inPortCnt; ++i)
  2297. val += cmDspDouble( inst, kBaseOpdSoId+i );
  2298. cmDspSetDouble( ctx, inst, kOutSoId, val );
  2299. return kOkDspRC;
  2300. }
  2301. // 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>
  2302. cmDspInst_t* _cmDspScalarOpAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2303. {
  2304. cmDspVarArg_t args[] =
  2305. {
  2306. { "cnt", kPortCntSoId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input port count" },
  2307. { "op", kOpSoId, 0, 0, kStrzDsvFl | kReqArgDsvFl, "Operation symbol as a string."},
  2308. { "out", kOutSoId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Operation output."},
  2309. };
  2310. cmDspScalarOp_t* p;
  2311. if( va_cnt < 2 )
  2312. {
  2313. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ScalarOp' constructor must have a count of input ports and operation identifier string.");
  2314. return NULL;
  2315. }
  2316. va_list vl1;
  2317. va_copy(vl1,vl);
  2318. unsigned inPortCnt = va_arg(vl,unsigned);
  2319. const cmChar_t* opIdStr = va_arg(vl,const cmChar_t*);
  2320. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  2321. unsigned argCnt = fixArgCnt + inPortCnt;
  2322. cmDspVarArg_t a[ argCnt+1 ];
  2323. double dfltVal[ inPortCnt ];
  2324. unsigned i;
  2325. _cmDspScalarOpFunc_t fp = NULL;
  2326. // validate the count of input ports
  2327. if( inPortCnt == 0 )
  2328. {
  2329. cmDspClassErr(ctx,classPtr,kVarNotValidDspRC,"The 'ScalarOp' constructor input port argument must be non-zero.");
  2330. return NULL;
  2331. }
  2332. // locate the operation function
  2333. if( strcmp(opIdStr,"*") == 0 )
  2334. fp = _cmDspScalarOpFuncMult;
  2335. else
  2336. if( strcmp(opIdStr,"+") == 0 )
  2337. fp = _cmDspScalarOpFuncAdd;
  2338. // validate the operation function
  2339. if( fp == NULL )
  2340. {
  2341. cmDspClassErr(ctx,classPtr,kVarNotValidDspRC,"The 'ScalarOp' constructor operation string id '%s' did not match a known operation.",cmStringNullGuard(opIdStr));
  2342. return NULL;
  2343. }
  2344. // setup the fixed args
  2345. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2346. for(i=0; i<inPortCnt; ++i)
  2347. {
  2348. // get the operand label
  2349. const cmChar_t* label = va_arg(vl,const cmChar_t*);
  2350. // get the operand default value
  2351. dfltVal[i] = va_arg(vl,double);
  2352. // setup the arg recd
  2353. cmDspArgSetup(ctx,a + fixArgCnt + i, label, cmInvalidId, kBaseOpdSoId+i,0,0,kDoubleDsvFl|kInDsvFl,"Operand");
  2354. }
  2355. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2356. if((p = cmDspInstAlloc(cmDspScalarOp_t,ctx,classPtr,a,instSymId,id,storeSymId,2,vl1)) == NULL )
  2357. return NULL;
  2358. for(i=0; i<inPortCnt; ++i)
  2359. cmDspSetDefaultDouble(ctx,&p->inst,kBaseOpdSoId+i,0.0,dfltVal[i]);
  2360. p->inPortCnt = inPortCnt;
  2361. p->func = fp;
  2362. return &p->inst;
  2363. }
  2364. cmDspRC_t _cmDspScalarOpReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2365. {
  2366. cmDspRC_t rc = kOkDspRC;
  2367. rc = cmDspApplyAllDefaults(ctx,inst);
  2368. return rc;
  2369. }
  2370. cmDspRC_t _cmDspScalarOpRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2371. {
  2372. cmDspRC_t rc = kOkDspRC;
  2373. cmDspScalarOp_t* p = (cmDspScalarOp_t*)inst;
  2374. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2375. {
  2376. if( evt->dstVarId == kBaseOpdSoId )
  2377. p->func(ctx,inst);
  2378. }
  2379. return rc;
  2380. }
  2381. struct cmDspClass_str* cmScalarOpClassCons( cmDspCtx_t* ctx )
  2382. {
  2383. cmDspClassSetup(&_cmScalarOpDC,ctx,"ScalarOp",
  2384. NULL,
  2385. _cmDspScalarOpAlloc,
  2386. NULL,
  2387. _cmDspScalarOpReset,
  2388. NULL,
  2389. _cmDspScalarOpRecv,
  2390. NULL,NULL,
  2391. "Scalar Operations");
  2392. return &_cmScalarOpDC;
  2393. }
  2394. //==========================================================================================================================================
  2395. enum
  2396. {
  2397. kChCntGsId,
  2398. kGroupCntGsId,
  2399. kChsPerGroupGsId,
  2400. kBaseGateGsId,
  2401. };
  2402. cmDspClass_t _cmGroupSelDC;
  2403. typedef struct
  2404. {
  2405. cmDspInst_t inst;
  2406. unsigned chCnt;
  2407. unsigned groupCnt;
  2408. cmGroupSel* gsp;
  2409. unsigned baseRmsGsId;
  2410. unsigned baseOutGsId;
  2411. } cmDspGroupSel_t;
  2412. cmDspInst_t* _cmDspGroupSelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2413. {
  2414. cmDspVarArg_t args[] =
  2415. {
  2416. { "chCnt", kChCntGsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count." },
  2417. { "groupCnt", kGroupCntGsId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Group count." },
  2418. { "chsPerGroup", kChsPerGroupGsId, 0, 0, kUIntDsvFl | kInDsvFl | kReqArgDsvFl, "Channels per group." }
  2419. };
  2420. if( va_cnt < 2 )
  2421. {
  2422. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'GroupSel' constructor must have a channel and group count.");
  2423. return NULL;
  2424. }
  2425. va_list vl1;
  2426. va_copy(vl1,vl);
  2427. cmDspGroupSel_t* p;
  2428. unsigned i;
  2429. unsigned chCnt = va_arg(vl,unsigned);
  2430. unsigned groupCnt = va_arg(vl,unsigned);
  2431. unsigned outCnt = chCnt * groupCnt;
  2432. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  2433. unsigned baseRmsGsId = kBaseGateGsId + chCnt;
  2434. unsigned baseOutGsId = baseRmsGsId + chCnt;
  2435. unsigned argCnt = baseOutGsId + outCnt;
  2436. cmDspVarArg_t a[ argCnt + 1 ];
  2437. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  2438. cmDspArgSetupN(ctx, a, argCnt, kBaseGateGsId, chCnt, "gate", kBaseGateGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Channel gate input.");
  2439. cmDspArgSetupN(ctx, a, argCnt, baseRmsGsId, chCnt, "rms", baseRmsGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Channel RMS input");
  2440. for(i=0; i<groupCnt; ++i)
  2441. {
  2442. int labelCharCnt = 31;
  2443. char label[ labelCharCnt + 1 ];
  2444. snprintf(label,labelCharCnt,"gate-%i",i);
  2445. cmDspArgSetupN(ctx, a, argCnt, baseOutGsId + (i*chCnt), chCnt, label, baseOutGsId + (i*chCnt), 0, 0, kOutDsvFl | kBoolDsvFl, "Output gates");
  2446. }
  2447. cmDspArgSetupNull( a+argCnt); // set terminating arg. flag
  2448. if((p = cmDspInstAlloc(cmDspGroupSel_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  2449. return NULL;
  2450. p->chCnt = chCnt;
  2451. p->groupCnt = groupCnt;
  2452. p->gsp = cmGroupSelAlloc(ctx->cmProcCtx, NULL, 0, 0, 0 );
  2453. p->baseRmsGsId = baseRmsGsId;
  2454. p->baseOutGsId = baseOutGsId;
  2455. for(i=0; i<chCnt; ++i)
  2456. {
  2457. cmDspSetDefaultBool( ctx, &p->inst, kBaseGateGsId, false, false );
  2458. cmDspSetDefaultDouble(ctx,&p->inst, baseRmsGsId, 0.0, 0.0 );
  2459. }
  2460. for(i=0; i<outCnt; ++i)
  2461. cmDspSetDefaultBool( ctx, &p->inst, baseOutGsId, false, false );
  2462. return &p->inst;
  2463. }
  2464. cmDspRC_t _cmDspGroupSelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2465. {
  2466. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2467. cmGroupSelFree(&p->gsp);
  2468. return kOkDspRC;
  2469. }
  2470. cmDspRC_t _cmDspGroupSelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2471. {
  2472. cmDspRC_t rc = kOkDspRC;
  2473. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2474. rc = cmDspApplyAllDefaults(ctx,inst);
  2475. cmGroupSelInit(p->gsp,p->chCnt,p->groupCnt,cmDspUInt(&p->inst,kChsPerGroupGsId));
  2476. return rc;
  2477. }
  2478. cmDspRC_t _cmDspGroupSelExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2479. {
  2480. cmDspRC_t rc = kOkDspRC;
  2481. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2482. if( cmGroupSelExec(p->gsp) == cmOkRC && p->gsp->updateFl )
  2483. {
  2484. unsigned i,j;
  2485. for(i=0; i<p->groupCnt; ++i)
  2486. {
  2487. cmGroupSelGrp* gp = p->gsp->groupArray + i;
  2488. if( gp->releaseFl )
  2489. {
  2490. for(j=0; j<gp->chIdxCnt; ++j)
  2491. cmDspSetBool(ctx,inst,p->baseOutGsId + (i*p->chCnt) + gp->chIdxArray[j], false);
  2492. }
  2493. if( gp->createFl )
  2494. {
  2495. for(j=0; j<gp->chIdxCnt; ++j)
  2496. cmDspSetBool(ctx,inst,p->baseOutGsId + (i*p->chCnt) + gp->chIdxArray[j], true);
  2497. }
  2498. }
  2499. }
  2500. return rc;
  2501. }
  2502. cmDspRC_t _cmDspGroupSelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2503. {
  2504. cmDspRC_t rc = kOkDspRC;
  2505. cmDspGroupSel_t* p = (cmDspGroupSel_t*)inst;
  2506. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2507. {
  2508. if( evt->dstVarId == kChsPerGroupGsId )
  2509. p->gsp->chsPerGroup = cmDspUInt(inst, kChsPerGroupGsId );
  2510. else
  2511. if( kBaseGateGsId <= evt->dstVarId && evt->dstVarId < (kBaseGateGsId + p->chCnt) )
  2512. cmGroupSetChannelGate(p->gsp, evt->dstVarId - kBaseGateGsId, cmDspDouble(inst,evt->dstVarId));
  2513. else
  2514. if( p->baseRmsGsId <= evt->dstVarId && evt->dstVarId < (p->baseRmsGsId + p->chCnt) )
  2515. cmGroupSetChannelRMS(p->gsp, evt->dstVarId - p->baseRmsGsId, cmDspDouble(inst,evt->dstVarId));
  2516. }
  2517. return rc;
  2518. }
  2519. struct cmDspClass_str* cmGroupSelClassCons( cmDspCtx_t* ctx )
  2520. {
  2521. cmDspClassSetup(&_cmGroupSelDC,ctx,"GroupSel",
  2522. NULL,
  2523. _cmDspGroupSelAlloc,
  2524. _cmDspGroupSelFree,
  2525. _cmDspGroupSelReset,
  2526. _cmDspGroupSelExec,
  2527. _cmDspGroupSelRecv,
  2528. NULL,NULL,
  2529. "Group selector.");
  2530. return &_cmGroupSelDC;
  2531. }
  2532. //==========================================================================================================================================
  2533. enum
  2534. {
  2535. kInChCntNmId,
  2536. kOutChCntNmId,
  2537. kFadeTimeNmId,
  2538. kBaseGateNmId,
  2539. };
  2540. cmDspClass_t _cmAudioNofM_DC;
  2541. typedef struct
  2542. {
  2543. cmDspInst_t inst;
  2544. unsigned inChCnt;
  2545. unsigned outChCnt;
  2546. cmAudioNofM* nmp;
  2547. unsigned baseInNmId;
  2548. unsigned baseOutNmId;
  2549. unsigned baseGainNmId;
  2550. } cmDspAudioNofM_t;
  2551. cmDspInst_t* _cmDspAudioNofM_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2552. {
  2553. if( va_cnt < 2 )
  2554. {
  2555. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AudioNofM' constructor must given input and output channel counts.");
  2556. return NULL;
  2557. }
  2558. va_list vl1;
  2559. va_copy(vl1,vl);
  2560. int inChCnt = va_arg(vl,int);
  2561. int outChCnt = va_arg(vl,int);
  2562. unsigned baseInNmId = kBaseGateNmId + inChCnt;
  2563. unsigned baseOutNmId = baseInNmId + inChCnt;
  2564. unsigned baseGainNmId= baseOutNmId + outChCnt;
  2565. unsigned i;
  2566. cmDspAudioNofM_t* p = cmDspInstAllocV(cmDspAudioNofM_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2567. 1, "ichs", kInChCntNmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input channel count.",
  2568. 1, "ochs", kOutChCntNmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Output channel count.",
  2569. 1, "time", kFadeTimeNmId, 0, 0, kDoubleDsvFl | kOptArgDsvFl | kInDsvFl, "Fade time in milliseconds.",
  2570. inChCnt, "gate", kBaseGateNmId, 0, 0, kBoolDsvFl | kInDsvFl, "Gate inputs.",
  2571. inChCnt, "in", baseInNmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2572. outChCnt, "out", baseOutNmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2573. outChCnt, "gain", baseGainNmId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Gain output",
  2574. 0 );
  2575. cmDspSetDefaultDouble( ctx, &p->inst, kFadeTimeNmId, 0.0, 25.0 );
  2576. p->inChCnt = inChCnt;
  2577. p->outChCnt = outChCnt;
  2578. p->nmp = cmAudioNofMAlloc(ctx->cmProcCtx,NULL,0,0,0,0);
  2579. p->baseInNmId = baseInNmId;
  2580. p->baseOutNmId = baseOutNmId;
  2581. p->baseGainNmId = baseGainNmId;
  2582. for(i=0; i<outChCnt; ++i)
  2583. cmDspSetDefaultDouble( ctx, &p->inst, baseGainNmId + i, 0.0, 0.0 );
  2584. return &p->inst;
  2585. }
  2586. cmDspRC_t _cmDspAudioNofM_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2587. {
  2588. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2589. cmAudioNofMFree(&p->nmp);
  2590. return kOkDspRC;
  2591. }
  2592. cmDspRC_t _cmDspAudioNofM_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2593. {
  2594. cmDspRC_t rc = kOkDspRC;
  2595. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2596. unsigned i;
  2597. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  2598. {
  2599. for(i=0; i<p->outChCnt; ++i)
  2600. cmDspZeroAudioBuf(ctx,inst,p->baseOutNmId+i);
  2601. cmAudioNofMInit(p->nmp, cmDspSampleRate(ctx), p->inChCnt, p->outChCnt, cmDspDouble(&p->inst, kFadeTimeNmId ));
  2602. }
  2603. return rc;
  2604. }
  2605. cmDspRC_t _cmDspAudioNofM_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2606. {
  2607. cmDspRC_t rc = kOkDspRC;
  2608. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2609. unsigned i;
  2610. const cmSample_t* x[ p->inChCnt ];
  2611. cmSample_t* y[ p->outChCnt ];
  2612. unsigned n;
  2613. for(i=0; i<p->inChCnt; ++i)
  2614. {
  2615. if( i==0 )
  2616. n = cmDspAudioBufSmpCount(ctx,inst,p->baseInNmId+i,0);
  2617. else
  2618. { assert( n == cmDspAudioBufSmpCount(ctx,inst,p->baseInNmId+i,0)); }
  2619. x[i] = cmDspAudioBuf(ctx,inst,p->baseInNmId+i,0);
  2620. }
  2621. for(i=0; i<p->outChCnt; ++i)
  2622. {
  2623. y[i] = cmDspAudioBuf(ctx,inst,p->baseOutNmId+i,0);
  2624. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->baseOutNmId+i,0));
  2625. cmVOS_Zero(y[i],n);
  2626. }
  2627. cmAudioNofMExec(p->nmp,x,p->inChCnt,y,p->outChCnt,n);
  2628. for(i=0; i<p->outChCnt; ++i)
  2629. {
  2630. cmAudioNofM_In* ip = p->nmp->outArray[i].list;
  2631. double v = 0;
  2632. for(; ip != NULL; ip=ip->link)
  2633. v += ip->fader->gain;
  2634. cmDspSetDouble(ctx, inst,p->baseGainNmId + i,v );
  2635. }
  2636. return rc;
  2637. }
  2638. cmDspRC_t _cmDspAudioNofM_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2639. {
  2640. cmDspRC_t rc = kOkDspRC;
  2641. cmDspAudioNofM_t* p = (cmDspAudioNofM_t*)inst;
  2642. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2643. {
  2644. if( kBaseGateNmId <= evt->dstVarId && evt->dstVarId <= kBaseGateNmId + p->inChCnt )
  2645. cmAudioNofMSetChannelGate( p->nmp, evt->dstVarId - kBaseGateNmId, cmDspBool(inst,evt->dstVarId) );
  2646. }
  2647. return rc;
  2648. }
  2649. struct cmDspClass_str* cmAudioNofMClassCons( cmDspCtx_t* ctx )
  2650. {
  2651. cmDspClassSetup(&_cmAudioNofM_DC,ctx,"AudioNofM",
  2652. NULL,
  2653. _cmDspAudioNofM_Alloc,
  2654. _cmDspAudioNofM_Free,
  2655. _cmDspAudioNofM_Reset,
  2656. _cmDspAudioNofM_Exec,
  2657. _cmDspAudioNofM_Recv,
  2658. NULL,NULL,
  2659. "Audio N of M Switch");
  2660. return &_cmAudioNofM_DC;
  2661. }
  2662. //==========================================================================================================================================
  2663. enum
  2664. {
  2665. kInChCntRmId,
  2666. kBypassRmId,
  2667. kGainRmId,
  2668. kOutRmId,
  2669. kBaseInRmId
  2670. };
  2671. cmDspClass_t _cmRingModDC;
  2672. typedef struct
  2673. {
  2674. cmDspInst_t inst;
  2675. unsigned inChCnt;
  2676. } cmDspRingMod_t;
  2677. cmDspInst_t* _cmDspRingModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2678. {
  2679. if( va_cnt < 1 )
  2680. {
  2681. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RingMod' constructor must given an input channel counts.");
  2682. return NULL;
  2683. }
  2684. va_list vl1;
  2685. va_copy(vl1,vl);
  2686. int inChCnt = va_arg(vl,int);
  2687. cmDspRingMod_t* p = cmDspInstAllocV(cmDspRingMod_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2688. 1, "ichs", kInChCntRmId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Input channel count.",
  2689. 1, "bypass",kBypassRmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable",
  2690. 1, "gain", kGainRmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain (default:1.0)",
  2691. 1, "out", kOutRmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2692. inChCnt, "in", kBaseInRmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2693. 0 );
  2694. cmDspSetDefaultBool( ctx, &p->inst, kBypassRmId, false, false );
  2695. cmDspSetDefaultDouble( ctx, &p->inst, kGainRmId, 0.0, 1.0 );
  2696. p->inChCnt = inChCnt;
  2697. return &p->inst;
  2698. }
  2699. cmDspRC_t _cmDspRingModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2700. {
  2701. cmDspRC_t rc = kOkDspRC;
  2702. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  2703. {
  2704. cmDspZeroAudioBuf(ctx,inst,kOutRmId);
  2705. }
  2706. return rc;
  2707. }
  2708. cmDspRC_t _cmDspRingModExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2709. {
  2710. cmDspRC_t rc = kOkDspRC;
  2711. cmDspRingMod_t* p = (cmDspRingMod_t*)inst;
  2712. unsigned i,j;
  2713. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutRmId, 0);
  2714. const cmSample_t* x0 = cmDspAudioBuf(ctx,inst,kBaseInRmId,0);
  2715. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutRmId,0);
  2716. double gain = cmDspDouble(inst,kGainRmId);
  2717. bool bypassFl = cmDspBool(inst,kBypassRmId);
  2718. for(i=1; i<p->inChCnt; ++i)
  2719. {
  2720. assert( n == cmDspAudioBufSmpCount(ctx,inst,kBaseInRmId+i,0));
  2721. const cmSample_t* x1 = cmDspAudioBuf(ctx,inst,kBaseInRmId+i,0);
  2722. if( bypassFl )
  2723. {
  2724. for(j=0; j<n; ++j)
  2725. y[j] = x0[j] + x1[j];
  2726. }
  2727. else
  2728. {
  2729. for(j=0; j<n; ++j)
  2730. y[j] = x0[j] * x1[j] * gain;
  2731. }
  2732. x0 = y;
  2733. }
  2734. return rc;
  2735. }
  2736. cmDspRC_t _cmDspRingModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2737. {
  2738. return cmDspSetEvent(ctx,inst,evt);
  2739. }
  2740. struct cmDspClass_str* cmRingModClassCons( cmDspCtx_t* ctx )
  2741. {
  2742. cmDspClassSetup(&_cmRingModDC,ctx,"RingMod",
  2743. NULL,
  2744. _cmDspRingModAlloc,
  2745. NULL,
  2746. _cmDspRingModReset,
  2747. _cmDspRingModExec,
  2748. _cmDspRingModRecv,
  2749. NULL,NULL,
  2750. "Ring modulator");
  2751. return &_cmRingModDC;
  2752. }
  2753. //==========================================================================================================================================
  2754. enum
  2755. {
  2756. kMaxCntMdId,
  2757. kDelayMdId,
  2758. kInMdId,
  2759. kOutMdId,
  2760. };
  2761. cmDspClass_t _cmMsgDelayDC;
  2762. typedef struct cmDspMsgDelayEle_str
  2763. {
  2764. unsigned outTimeSmp;
  2765. cmDspValue_t value;
  2766. struct cmDspMsgDelayEle_str* link;
  2767. } cmDspMsgDelayEle_t;
  2768. typedef struct
  2769. {
  2770. cmDspInst_t inst;
  2771. unsigned maxCnt;
  2772. cmDspMsgDelayEle_t* array; // array[maxCnt];
  2773. cmDspMsgDelayEle_t* avail;
  2774. cmDspMsgDelayEle_t* active;
  2775. } cmDspMsgDelay_t;
  2776. cmDspInst_t* _cmDspMsgDelayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2777. {
  2778. cmDspMsgDelay_t* p = cmDspInstAllocV(cmDspMsgDelay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2779. 1, "maxcnt", kMaxCntMdId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Maximum count of elements in the delay",
  2780. 1, "delay", kDelayMdId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Delay time in millisecond.",
  2781. 1, "in", kInMdId, 0, 0, kInDsvFl | kTypeDsvMask, "Msg input",
  2782. 1, "out", kOutMdId, 0, 0, kOutDsvFl | kTypeDsvMask, "Msg input",
  2783. 0 );
  2784. if( p == NULL )
  2785. return NULL;
  2786. cmDspSetDefaultDouble( ctx, &p->inst, kDelayMdId, 0.0, 0.0 );
  2787. cmDspSetDefaultBool( ctx, &p->inst, kOutMdId, false, false );
  2788. p->maxCnt = cmDspUInt(&p->inst,kMaxCntMdId);
  2789. p->array = cmMemAllocZ(cmDspMsgDelayEle_t, p->maxCnt );
  2790. return &p->inst;
  2791. }
  2792. cmDspRC_t _cmDspMsgDelayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2793. {
  2794. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2795. cmMemFree(p->array);
  2796. return kOkDspRC;
  2797. }
  2798. // insert a value in the delay list
  2799. cmDspRC_t _cmDspMsgDelayInsert( cmDspCtx_t* ctx, cmDspMsgDelay_t* p, unsigned delayTimeSmp, const cmDspValue_t* valPtr )
  2800. {
  2801. cmDspRC_t rc = kOkDspRC;
  2802. // protect against pre-reset calls
  2803. if( p->avail == NULL && p->active == NULL )
  2804. return kOkDspRC;
  2805. // if there are no available delay elements
  2806. if( p->avail == NULL )
  2807. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The message delay has exhausted it's internal message store.");
  2808. // we only do the simplest kind of copying to avoid allocating memory
  2809. // TODO: fix this
  2810. if( cmDsvIsMtx(valPtr) || cmIsFlag(valPtr->flags,kProxyDsvFl))
  2811. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The message delay cannot yet store matrix or proxy types.");
  2812. // get a pointer to the next available element
  2813. cmDspMsgDelayEle_t* np = p->avail;
  2814. // remove the new ele from the avail list
  2815. p->avail = np->link;
  2816. // calc the new ele's exec time
  2817. np->outTimeSmp = ctx->cycleCnt * cmDspSamplesPerCycle(ctx) + delayTimeSmp;
  2818. // copy the msg value into the delay line element
  2819. // TODO: this should be a real copy that supports all types
  2820. np->value = *valPtr;
  2821. cmDspMsgDelayEle_t* ep = p->active;
  2822. cmDspMsgDelayEle_t* pp = NULL;
  2823. // if the active list is empty ...
  2824. if( ep == NULL )
  2825. {
  2826. // ... make the avail element the first on the list
  2827. p->active = np;
  2828. np->link = NULL;
  2829. }
  2830. else
  2831. {
  2832. // iterate through the list and find the active links which
  2833. // the new ele falls between based on its execution time
  2834. while(ep != NULL )
  2835. {
  2836. // ep's exec time is greater than the new ele's exec time
  2837. if( ep->outTimeSmp > np->outTimeSmp )
  2838. {
  2839. // insert the new ele in the active list before 'ep'
  2840. if( pp == NULL )
  2841. {
  2842. np->link = p->active;
  2843. p->active = np;
  2844. }
  2845. else
  2846. {
  2847. np->link = pp->link;
  2848. pp->link = np;
  2849. }
  2850. break;
  2851. }
  2852. pp = ep;
  2853. ep = ep->link;
  2854. }
  2855. // if the new element is last on the list
  2856. if( ep == NULL )
  2857. {
  2858. assert(pp != NULL && pp->link == NULL);
  2859. pp->link = np;
  2860. np->link = NULL;
  2861. }
  2862. }
  2863. return rc;
  2864. }
  2865. cmDspRC_t _cmDspMsgDelayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2866. {
  2867. cmDspRC_t rc = kOkDspRC;
  2868. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2869. if((rc = cmDspApplyDefault(ctx,inst,kDelayMdId)) == kOkDspRC )
  2870. {
  2871. unsigned i;
  2872. unsigned maxCnt = cmDspUInt(inst,kMaxCntMdId);
  2873. p->active = NULL;
  2874. p->avail = NULL;
  2875. // put all ele's on the available list
  2876. for(i=0; i<maxCnt; ++i)
  2877. {
  2878. p->array[i].link = p->avail;
  2879. p->avail = p->array + i;
  2880. }
  2881. }
  2882. return rc;
  2883. }
  2884. cmDspRC_t _cmDspMsgDelayExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* e )
  2885. {
  2886. cmDspRC_t rc = kOkDspRC;
  2887. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2888. unsigned framesPerCycle = cmDspSamplesPerCycle(ctx);
  2889. unsigned begTimeSmp = ctx->cycleCnt * framesPerCycle;
  2890. unsigned endTimeSmp = begTimeSmp + framesPerCycle;
  2891. while( p->active != NULL )
  2892. {
  2893. if( p->active->outTimeSmp >= endTimeSmp )
  2894. break;
  2895. cmDspMsgDelayEle_t* ep = p->active;
  2896. // remove the element from the active list and place it on the available list.
  2897. p->active = p->active->link; // advance the active list
  2898. ep->link = p->avail; // put the cur. element on the avail list
  2899. p->avail = ep; //
  2900. // output the element value
  2901. if((rc = cmDspValueSet(ctx,inst,kOutMdId,&ep->value,0)) != kOkDspRC )
  2902. return cmDspInstErr(ctx,inst,rc,"Message delay output failed.");
  2903. }
  2904. return rc;
  2905. }
  2906. cmDspRC_t _cmDspMsgDelayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2907. {
  2908. cmDspRC_t rc = kOkDspRC;
  2909. cmDspMsgDelay_t* p = (cmDspMsgDelay_t*)inst;
  2910. switch( evt->dstVarId )
  2911. {
  2912. case kDelayMdId:
  2913. rc = cmDspSetEvent(ctx,inst,evt);
  2914. break;
  2915. case kInMdId:
  2916. {
  2917. unsigned delayTimeSmp = floor(cmDspDouble(&p->inst,kDelayMdId) * cmDspSampleRate(ctx) / 1000.0);
  2918. rc = _cmDspMsgDelayInsert(ctx,p,delayTimeSmp,evt->valuePtr);
  2919. }
  2920. break;
  2921. }
  2922. return rc;
  2923. }
  2924. struct cmDspClass_str* cmMsgDelayClassCons( cmDspCtx_t* ctx )
  2925. {
  2926. cmDspClassSetup(&_cmMsgDelayDC,ctx,"MsgDelay",
  2927. NULL,
  2928. _cmDspMsgDelayAlloc,
  2929. _cmDspMsgDelayFree,
  2930. _cmDspMsgDelayReset,
  2931. _cmDspMsgDelayExec,
  2932. _cmDspMsgDelayRecv,
  2933. NULL,NULL,
  2934. "Message Delay");
  2935. return &_cmMsgDelayDC;
  2936. }
  2937. //==========================================================================================================================================
  2938. enum
  2939. {
  2940. kBegLnId,
  2941. kEndLnId,
  2942. kDurLnId,
  2943. kCmdLnId,
  2944. kOutLnId,
  2945. };
  2946. cmDspClass_t _cmLineDC;
  2947. typedef struct cmDspLineEle_str
  2948. {
  2949. unsigned outTimeSmp;
  2950. cmDspValue_t value;
  2951. struct cmDspLineEle_str* link;
  2952. } cmDspLineEle_t;
  2953. typedef struct
  2954. {
  2955. cmDspInst_t inst;
  2956. unsigned onSymId;
  2957. unsigned offSymId;
  2958. unsigned resetSymId;
  2959. unsigned curSmpIdx;
  2960. bool onFl;
  2961. } cmDspLine_t;
  2962. cmDspInst_t* _cmDspLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2963. {
  2964. cmDspLine_t* p = cmDspInstAllocV(cmDspLine_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2965. 1, "beg", kBegLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Begin value.",
  2966. 1, "end", kEndLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "End value.",
  2967. 1, "dur", kDurLnId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Duration (ms)",
  2968. 1, "cmd", kCmdLnId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on | off | reset",
  2969. 1, "out", kOutLnId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  2970. 0 );
  2971. if( p == NULL )
  2972. return NULL;
  2973. cmDspSetDefaultDouble( ctx, &p->inst, kOutLnId, 0.0, cmDspDefaultDouble(&p->inst,kBegLnId) );
  2974. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2975. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2976. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"reset");
  2977. return &p->inst;
  2978. }
  2979. cmDspRC_t _cmDspLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2980. {
  2981. return kOkDspRC;
  2982. }
  2983. cmDspRC_t _cmDspLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2984. {
  2985. cmDspLine_t* p = (cmDspLine_t*)inst;
  2986. p->curSmpIdx = 0;
  2987. p->onFl = false;
  2988. return cmDspApplyAllDefaults(ctx,inst);
  2989. }
  2990. cmDspRC_t _cmDspLineExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* e )
  2991. {
  2992. cmDspRC_t rc = kOkDspRC;
  2993. cmDspLine_t* p = (cmDspLine_t*)inst;
  2994. if( p->onFl == false )
  2995. return kOkDspRC;
  2996. double beg = cmDspDouble(inst,kBegLnId);
  2997. double end = cmDspDouble(inst,kEndLnId);
  2998. double ms = cmDspDouble(inst,kDurLnId);
  2999. double durSmpCnt = floor(ms * cmDspSampleRate(ctx) / 1000);
  3000. double out = beg + (end - beg) * p->curSmpIdx / durSmpCnt;
  3001. if( beg < end )
  3002. {
  3003. if( out >= end )
  3004. {
  3005. out = end;
  3006. p->onFl = false;
  3007. }
  3008. }
  3009. else
  3010. {
  3011. if( out <= end )
  3012. {
  3013. out = end;
  3014. p->onFl = false;
  3015. }
  3016. }
  3017. cmDspSetDouble(ctx,inst,kOutLnId,out);
  3018. p->curSmpIdx += cmDspSamplesPerCycle(ctx);
  3019. return rc;
  3020. }
  3021. cmDspRC_t _cmDspLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3022. {
  3023. cmDspRC_t rc = kOkDspRC;
  3024. cmDspLine_t* p = (cmDspLine_t*)inst;
  3025. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3026. {
  3027. switch( evt->dstVarId )
  3028. {
  3029. case kCmdLnId:
  3030. {
  3031. unsigned symId = cmDspSymbol(inst,kCmdLnId);
  3032. if( symId == p->onSymId )
  3033. p->onFl = true;
  3034. else
  3035. if( symId == p->offSymId )
  3036. p->onFl = false;
  3037. else
  3038. if( symId == p->resetSymId )
  3039. {
  3040. p->curSmpIdx = 0;
  3041. p->onFl = true;
  3042. }
  3043. }
  3044. break;
  3045. }
  3046. }
  3047. return rc;
  3048. }
  3049. struct cmDspClass_str* cmLineClassCons( cmDspCtx_t* ctx )
  3050. {
  3051. cmDspClassSetup(&_cmLineDC,ctx,"Line",
  3052. NULL,
  3053. _cmDspLineAlloc,
  3054. _cmDspLineFree,
  3055. _cmDspLineReset,
  3056. _cmDspLineExec,
  3057. _cmDspLineRecv,
  3058. NULL,NULL,
  3059. "Line");
  3060. return &_cmLineDC;
  3061. }
  3062. //==========================================================================================================================================
  3063. enum
  3064. {
  3065. kTrigModeAdId,
  3066. kMinLvlAdId,
  3067. kDlyMsAdId,
  3068. kAtkMsAdId,
  3069. kAtkLvlAdId,
  3070. kDcyMsAdId,
  3071. kSusLvlAdId,
  3072. kSusMsAdId,
  3073. kRlsMsAdId,
  3074. kTScaleAdId,
  3075. kAScaleAdId,
  3076. kGateAdId,
  3077. kRmsAdId,
  3078. kOutAdId,
  3079. kCmdAdId
  3080. };
  3081. cmDspClass_t _cmAdsrDC;
  3082. typedef struct
  3083. {
  3084. cmDspInst_t inst;
  3085. cmAdsr* p;
  3086. } cmDspAdsr_t;
  3087. cmDspInst_t* _cmDspAdsrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3088. {
  3089. cmDspAdsr_t* p = cmDspInstAllocV(cmDspAdsr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3090. 1, "trig", kTrigModeAdId, 0, 0, kBoolDsvFl | kInDsvFl | kOptArgDsvFl, "Trigger mode (offset ignored).",
  3091. 1, "min", kMinLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Minimum level (dflt:0.0).",
  3092. 1, "dly", kDlyMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Delay milliseconds.",
  3093. 1, "atk", kAtkMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Attack milliseconds.",
  3094. 1, "alvl", kAtkLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Attack Level.",
  3095. 1, "dcy", kDcyMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Decay milliseconds.",
  3096. 1, "sus", kSusLvlAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Sustain Level.",
  3097. 1, "hold", kSusMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Sustain Ms (trig mode only).",
  3098. 1, "rls", kRlsMsAdId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl, "Release milliseconds.",
  3099. 1, "tscale",kTScaleAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "Time scale.",
  3100. 1, "ascale",kAScaleAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "Amplitude scale.",
  3101. 1, "gate", kGateAdId, 0, 0, kBoolDsvFl | kInDsvFl, "Gate input.",
  3102. 1, "rms", kRmsAdId, 0, 0, kDoubleDsvFl | kInDsvFl, "RMS input.",
  3103. 1, "out", kOutAdId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Level output.",
  3104. 1, "cmd", kCmdAdId, 0, 0, kSymDsvFl | kInDsvFl, "Command input.",
  3105. 0 );
  3106. cmDspSetDefaultBool( ctx, &p->inst, kTrigModeAdId, false, false );
  3107. cmDspSetDefaultDouble( ctx, &p->inst, kMinLvlAdId, 0.0, 0.0 );
  3108. cmDspSetDefaultDouble( ctx, &p->inst, kDlyMsAdId, 0.0, 0.0 );
  3109. cmDspSetDefaultDouble( ctx, &p->inst, kAtkMsAdId, 0.0, 5.0 );
  3110. cmDspSetDefaultDouble( ctx, &p->inst, kAtkLvlAdId, 0.0, 1.0 );
  3111. cmDspSetDefaultDouble( ctx, &p->inst, kDcyMsAdId, 0.0, 10.0 );
  3112. cmDspSetDefaultDouble( ctx, &p->inst, kSusLvlAdId, 0.0, 0.8 );
  3113. cmDspSetDefaultDouble( ctx, &p->inst, kSusMsAdId, 0.0, 50.0);
  3114. cmDspSetDefaultDouble( ctx, &p->inst, kRlsMsAdId, 0.0, 20.0 );
  3115. cmDspSetDefaultDouble( ctx, &p->inst, kTScaleAdId, 0.0, 1.0 );
  3116. cmDspSetDefaultDouble( ctx, &p->inst, kAScaleAdId, 0.0, 1.0 );
  3117. cmDspSetDefaultBool( ctx, &p->inst, kGateAdId, false,false);
  3118. cmDspSetDefaultDouble( ctx, &p->inst, kRmsAdId, 0.0, 0.0);
  3119. cmDspSetDefaultDouble( ctx, &p->inst, kOutAdId, 0.0, cmDspDouble(&p->inst,kMinLvlAdId));
  3120. p->p = cmAdsrAlloc(ctx->cmProcCtx,NULL,false,0,0,0,0,0,0,0,0,0);
  3121. return &p->inst;
  3122. }
  3123. cmDspRC_t _cmDspAdsrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3124. {
  3125. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3126. cmAdsrFree(&p->p);
  3127. return kOkDspRC;
  3128. }
  3129. cmDspRC_t _cmDspAdsrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3130. {
  3131. cmDspRC_t rc = kOkDspRC;
  3132. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3133. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3134. {
  3135. bool trigFl= cmDspBool( inst, kTrigModeAdId );
  3136. cmReal_t minL = cmDspDouble( inst, kMinLvlAdId );
  3137. cmReal_t dlyMs = cmDspDouble( inst, kDlyMsAdId );
  3138. cmReal_t atkMs = cmDspDouble( inst, kAtkMsAdId );
  3139. cmReal_t atkL = cmDspDouble( inst, kAtkLvlAdId );
  3140. cmReal_t dcyMs = cmDspDouble( inst, kDcyMsAdId );
  3141. cmReal_t susMs = cmDspDouble( inst, kSusMsAdId );
  3142. cmReal_t susL = cmDspDouble( inst, kSusLvlAdId );
  3143. cmReal_t rlsMs = cmDspDouble( inst, kRlsMsAdId );
  3144. cmAdsrInit( p->p, cmDspSampleRate(ctx), trigFl, minL, dlyMs, atkMs, atkL, dcyMs, susMs, susL, rlsMs );
  3145. }
  3146. return rc;
  3147. }
  3148. cmDspRC_t _cmDspAdsrExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3149. {
  3150. cmDspRC_t rc = kOkDspRC;
  3151. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3152. bool gateFl = cmDspBool( inst, kGateAdId );
  3153. //double rms = cmDspDouble(inst,kRmsAdId);
  3154. double tscale = cmDspDouble(inst,kTScaleAdId);
  3155. double ascale = cmDspDouble(inst,kAScaleAdId);
  3156. //
  3157. // HACK HACK HACK HACK
  3158. // HACK HACK HACK HACK
  3159. // HACK HACK HACK HACK see the accompanying hack in cmProc3.c cmAdsrExec()
  3160. // HACK HACK HACK HACK
  3161. // HACK HACK HACK HACK
  3162. //
  3163. /*
  3164. double db = rms<0.00001 ? -100.0 : 20.0*log10(rms);
  3165. double dbMax = -15.0;
  3166. double dbMin = -58.0;
  3167. db = cmMin(dbMax,cmMax(dbMin,db));
  3168. double scale = (db - dbMin) / (dbMax-dbMin);
  3169. */
  3170. cmReal_t out = cmAdsrExec( p->p, cmDspSamplesPerCycle(ctx), gateFl, tscale, ascale );
  3171. rc = cmDspSetDouble( ctx, inst, kOutAdId, out );
  3172. return rc;
  3173. }
  3174. cmDspRC_t _cmDspAdsrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3175. {
  3176. cmDspRC_t rc = kOkDspRC;
  3177. cmDspAdsr_t* p = (cmDspAdsr_t*)inst;
  3178. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3179. return rc;
  3180. if( evt->dstVarId == kCmdAdId )
  3181. {
  3182. cmAdsrReport(p->p,ctx->rpt);
  3183. return rc;
  3184. }
  3185. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3186. switch( evt->dstVarId )
  3187. {
  3188. case kTrigModeAdId:
  3189. p->p->trigModeFl = cmDspBool(inst, kTrigModeAdId);
  3190. break;
  3191. case kMinLvlAdId:
  3192. cmAdsrSetLevel(p->p, v, kDlyAdsrId );
  3193. break;
  3194. case kDlyMsAdId:
  3195. cmAdsrSetTime(p->p, v, kDlyAdsrId );
  3196. break;
  3197. case kAtkMsAdId:
  3198. cmAdsrSetTime(p->p, v, kAtkAdsrId );
  3199. break;
  3200. case kAtkLvlAdId:
  3201. cmAdsrSetLevel(p->p, v, kAtkAdsrId );
  3202. break;
  3203. case kDcyMsAdId:
  3204. cmAdsrSetTime(p->p, v, kDcyAdsrId );
  3205. break;
  3206. case kSusMsAdId:
  3207. cmAdsrSetTime(p->p, v, kSusAdsrId );
  3208. break;
  3209. case kSusLvlAdId:
  3210. cmAdsrSetLevel(p->p, v, kSusAdsrId );
  3211. break;
  3212. case kRlsMsAdId:
  3213. cmAdsrSetTime(p->p, v, kRlsAdsrId );
  3214. break;
  3215. }
  3216. return rc;
  3217. }
  3218. struct cmDspClass_str* cmAdsrClassCons( cmDspCtx_t* ctx )
  3219. {
  3220. cmDspClassSetup(&_cmAdsrDC,ctx,"Adsr",
  3221. NULL,
  3222. _cmDspAdsrAlloc,
  3223. _cmDspAdsrFree,
  3224. _cmDspAdsrReset,
  3225. _cmDspAdsrExec,
  3226. _cmDspAdsrRecv,
  3227. NULL,NULL,
  3228. "ADSR Envelope Generator");
  3229. return &_cmAdsrDC;
  3230. }
  3231. //==========================================================================================================================================
  3232. enum
  3233. {
  3234. kBypassCmId,
  3235. kThreshDbCmId,
  3236. kRatioCmId,
  3237. kAtkMsCmId,
  3238. kRlsMsCmId,
  3239. kInGainCmId,
  3240. kOutGainCmId,
  3241. kWndMsCmId,
  3242. kMaxWndMsCmId,
  3243. kInCmId,
  3244. kOutCmId,
  3245. kEnvCmId
  3246. };
  3247. cmDspClass_t _cmCompressorDC;
  3248. typedef struct
  3249. {
  3250. cmDspInst_t inst;
  3251. cmCompressor* p;
  3252. } cmDspCompressor_t;
  3253. cmDspInst_t* _cmDspCompressorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3254. {
  3255. cmDspCompressor_t* p = cmDspInstAllocV(cmDspCompressor_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3256. 1, "bypass",kBypassCmId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable",
  3257. 1, "thr", kThreshDbCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Threshold in dB.",
  3258. 1, "ratio", kRatioCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Ratio numerator.",
  3259. 1, "atk", kAtkMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Attack milliseconds",
  3260. 1, "rls", kRlsMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Release milliseconds",
  3261. 1, "igain", kInGainCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain.",
  3262. 1, "ogain", kOutGainCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Makeup Gain",
  3263. 1, "wnd", kWndMsCmId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "RMS window milliseconds.",
  3264. 1, "maxwnd",kMaxWndMsCmId, 0, 0, kDoubleDsvFl | kOptArgDsvFl, "Max. RMS window milliseconds.",
  3265. 1, "in", kInCmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3266. 1, "out", kOutCmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3267. 1, "env", kEnvCmId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Envelope out",
  3268. 0 );
  3269. p->p = cmCompressorAlloc(ctx->cmProcCtx, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false );
  3270. cmDspSetDefaultBool( ctx, &p->inst, kBypassCmId, false, false );
  3271. cmDspSetDefaultDouble( ctx, &p->inst, kAtkMsCmId, 0.0, 20.0 );
  3272. cmDspSetDefaultDouble( ctx, &p->inst, kRlsMsCmId, 0.0, 20.0 );
  3273. cmDspSetDefaultDouble( ctx, &p->inst, kInGainCmId, 0.0, 1.0 );
  3274. cmDspSetDefaultDouble( ctx, &p->inst, kOutGainCmId, 0.0, 1.0 );
  3275. cmDspSetDefaultDouble( ctx, &p->inst, kWndMsCmId, 0.0, 200.0);
  3276. cmDspSetDefaultDouble( ctx, &p->inst, kMaxWndMsCmId, 0.0, 1000.0);
  3277. cmDspSetDefaultDouble( ctx, &p->inst, kEnvCmId, 0.0, 0.0 );
  3278. return &p->inst;
  3279. }
  3280. cmDspRC_t _cmDspCompressorFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3281. {
  3282. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3283. cmCompressorFree(&p->p);
  3284. return kOkDspRC;
  3285. }
  3286. cmDspRC_t _cmDspCompressorReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3287. {
  3288. cmDspRC_t rc = kOkDspRC;
  3289. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3290. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3291. {
  3292. cmDspZeroAudioBuf(ctx,inst,kOutCmId);
  3293. cmReal_t threshDb = cmDspDouble(inst, kThreshDbCmId );
  3294. cmReal_t ratio = cmDspDouble(inst, kRatioCmId );
  3295. cmReal_t atkMs = cmDspDouble(inst, kAtkMsCmId );
  3296. cmReal_t rlsMs = cmDspDouble(inst, kRlsMsCmId );
  3297. cmReal_t inGain = cmDspDouble(inst, kInGainCmId );
  3298. cmReal_t outGain = cmDspDouble(inst, kOutGainCmId );
  3299. cmReal_t wndMs = cmDspDouble(inst, kWndMsCmId );
  3300. cmReal_t maxWndMs = cmDspDouble(inst, kMaxWndMsCmId);
  3301. bool bypassFl = cmDspBool( inst, kBypassCmId );
  3302. cmCompressorInit(p->p,cmDspSampleRate(ctx),cmDspSamplesPerCycle(ctx), inGain, maxWndMs, wndMs, threshDb, ratio, atkMs, rlsMs, outGain, bypassFl );
  3303. }
  3304. return rc;
  3305. }
  3306. cmDspRC_t _cmDspCompressorExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3307. {
  3308. cmDspRC_t rc = kOkDspRC;
  3309. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3310. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutCmId, 0);
  3311. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInCmId,0);
  3312. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutCmId,0);
  3313. if( x != NULL )
  3314. {
  3315. cmCompressorExec(p->p,x,y,n);
  3316. rc = cmDspSetDouble( ctx, inst, kEnvCmId, p->p->gain );
  3317. }
  3318. return rc;
  3319. }
  3320. cmDspRC_t _cmDspCompressorRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3321. {
  3322. cmDspRC_t rc;
  3323. cmDspCompressor_t* p = (cmDspCompressor_t*)inst;
  3324. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3325. return rc;
  3326. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3327. switch( evt->dstVarId )
  3328. {
  3329. case kThreshDbCmId:
  3330. cmCompressorSetThreshDb(p->p,v);
  3331. break;
  3332. case kRatioCmId:
  3333. p->p->ratio_num = v;
  3334. break;
  3335. case kAtkMsCmId:
  3336. cmCompressorSetAttackMs(p->p,v);
  3337. break;
  3338. case kRlsMsCmId:
  3339. cmCompressorSetReleaseMs(p->p,v);
  3340. break;
  3341. case kInGainCmId:
  3342. p->p->inGain = v;
  3343. break;
  3344. case kOutGainCmId:
  3345. p->p->outGain = v;
  3346. break;
  3347. case kWndMsCmId:
  3348. cmCompressorSetRmsWndMs(p->p,v);
  3349. break;
  3350. case kBypassCmId:
  3351. p->p->bypassFl = cmDspBool(inst,kBypassCmId);
  3352. break;
  3353. }
  3354. return rc;
  3355. }
  3356. struct cmDspClass_str* cmCompressorClassCons( cmDspCtx_t* ctx )
  3357. {
  3358. cmDspClassSetup(&_cmCompressorDC,ctx,"Compressor",
  3359. NULL,
  3360. _cmDspCompressorAlloc,
  3361. _cmDspCompressorFree,
  3362. _cmDspCompressorReset,
  3363. _cmDspCompressorExec,
  3364. _cmDspCompressorRecv,
  3365. NULL,NULL,
  3366. "Compressor");
  3367. return &_cmCompressorDC;
  3368. }
  3369. //==========================================================================================================================================
  3370. enum
  3371. {
  3372. kBypassBqId,
  3373. kModeBqId,
  3374. kF0HzBqId,
  3375. kQBqId,
  3376. kGainDbBqId,
  3377. kInBqId,
  3378. kOutBqId
  3379. };
  3380. cmDspClass_t _cmBiQuadEqDC;
  3381. typedef struct
  3382. {
  3383. const cmChar_t* label;
  3384. unsigned mode;
  3385. unsigned symbol;
  3386. } cmDspBiQuadMap_t;
  3387. cmDspBiQuadMap_t _cmDspBiQuadMap[] =
  3388. {
  3389. {"LP", kLpfBqId, cmInvalidId },
  3390. {"HP", kHpFBqId, cmInvalidId },
  3391. {"BP", kBpfBqId, cmInvalidId },
  3392. {"Notch", kNotchBqId, cmInvalidId },
  3393. {"AP", kAllpassBqId, cmInvalidId },
  3394. {"Peak", kPeakBqId, cmInvalidId },
  3395. {"LSh", kLowShelfBqId, cmInvalidId },
  3396. {"HSh", kHighShelfBqId, cmInvalidId },
  3397. { NULL, cmInvalidId, cmInvalidId }
  3398. };
  3399. typedef struct
  3400. {
  3401. cmDspInst_t inst;
  3402. cmBiQuadEq* p;
  3403. } cmDspBiQuadEq_t;
  3404. cmDspInst_t* _cmDspBiQuadEqAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3405. {
  3406. cmDspBiQuadEq_t* p = cmDspInstAllocV(cmDspBiQuadEq_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3407. 1, "bypass",kBypassBqId, 0, 0, kInDsvFl | kBoolDsvFl | kReqArgDsvFl, "Bypass enable.",
  3408. 1, "mode", kModeBqId, 0, 0, kInDsvFl | kSymDsvFl | kReqArgDsvFl, "Mode Symbol: LP|HP|BP|AP|Notch|Pk|LSh|HSh.",
  3409. 1, "f0", kF0HzBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Center or edge frequecy in Hz.",
  3410. 1, "Q", kQBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Q",
  3411. 1, "gain", kGainDbBqId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Gain Db (Pk,LSh,Hsh only)",
  3412. 1, "in", kInBqId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3413. 1, "out", kOutBqId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3414. 0 );
  3415. cmDspSetDefaultDouble( ctx, &p->inst, kGainDbBqId, 0.0, 1.0 );
  3416. p->p = cmBiQuadEqAlloc(ctx->cmProcCtx,NULL,0,0,0,0,0,false);
  3417. // register the filter mode symbols
  3418. unsigned i;
  3419. for(i=0; _cmDspBiQuadMap[i].label != NULL; ++i)
  3420. if( _cmDspBiQuadMap[i].symbol == cmInvalidId )
  3421. _cmDspBiQuadMap[i].symbol = cmSymTblRegisterStaticSymbol(ctx->stH,_cmDspBiQuadMap[i].label);
  3422. return &p->inst;
  3423. }
  3424. cmDspRC_t _cmDspBiQuadEqFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3425. {
  3426. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3427. cmBiQuadEqFree(&p->p);
  3428. return kOkDspRC;
  3429. }
  3430. unsigned _cmDspBiQuadEqModeId( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned modeSymId )
  3431. {
  3432. unsigned i;
  3433. for(i=0; _cmDspBiQuadMap[i].label!=NULL; ++i)
  3434. if( _cmDspBiQuadMap[i].symbol == modeSymId )
  3435. return _cmDspBiQuadMap[i].mode;
  3436. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The mode string '%s' is not valid.",cmStringNullGuard(cmSymTblLabel(ctx->stH,modeSymId)));
  3437. return cmInvalidId;
  3438. }
  3439. cmDspRC_t _cmDspBiQuadEqReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3440. {
  3441. cmDspRC_t rc = kOkDspRC;
  3442. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3443. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3444. {
  3445. cmDspZeroAudioBuf(ctx,inst,kOutBqId);
  3446. unsigned modeSymId = cmDspSymbol(inst, kModeBqId );
  3447. cmReal_t f0Hz = cmDspDouble(inst, kF0HzBqId );
  3448. cmReal_t Q = cmDspDouble(inst, kQBqId );
  3449. cmReal_t gainDb = cmDspDouble(inst, kGainDbBqId );
  3450. unsigned mode = _cmDspBiQuadEqModeId(ctx,inst,modeSymId );
  3451. bool bypassFl = cmDspBool(inst, kBypassBqId );
  3452. if( mode != cmInvalidId )
  3453. cmBiQuadEqInit(p->p, cmDspSampleRate(ctx), mode, f0Hz, Q, gainDb, bypassFl );
  3454. }
  3455. return rc;
  3456. }
  3457. cmDspRC_t _cmDspBiQuadEqExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3458. {
  3459. cmDspRC_t rc = kOkDspRC;
  3460. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3461. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutBqId, 0);
  3462. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInBqId,0);
  3463. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutBqId,0);
  3464. cmBiQuadEqExec(p->p,x,y,n);
  3465. return rc;
  3466. }
  3467. cmDspRC_t _cmDspBiQuadEqRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3468. {
  3469. cmDspRC_t rc;
  3470. cmDspBiQuadEq_t* p = (cmDspBiQuadEq_t*)inst;
  3471. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3472. return rc;
  3473. cmReal_t f0Hz = p->p->f0Hz;
  3474. cmReal_t Q = p->p->Q;
  3475. cmReal_t gainDb = p->p->gainDb;
  3476. unsigned mode = p->p->mode;
  3477. if( evt->dstVarId == kModeBqId )
  3478. {
  3479. if((mode = _cmDspBiQuadEqModeId(ctx,inst,cmDspSymbol(inst,kModeBqId) )) == cmInvalidId )
  3480. rc = kVarNotValidDspRC;
  3481. }
  3482. else
  3483. {
  3484. cmReal_t v = cmDspDouble(inst,evt->dstVarId);
  3485. switch( evt->dstVarId )
  3486. {
  3487. case kF0HzBqId:
  3488. f0Hz = v;
  3489. break;
  3490. case kQBqId:
  3491. Q = v;
  3492. break;
  3493. case kGainDbBqId:
  3494. gainDb = v;
  3495. break;
  3496. case kBypassBqId:
  3497. p->p->bypassFl = cmDspBool(inst,kBypassBqId);
  3498. break;
  3499. }
  3500. }
  3501. cmBiQuadEqSet(p->p,mode,f0Hz,Q,gainDb);
  3502. return rc;
  3503. }
  3504. struct cmDspClass_str* cmBiQuadEqClassCons( cmDspCtx_t* ctx )
  3505. {
  3506. cmDspClassSetup(&_cmBiQuadEqDC,ctx,"BiQuadEq",
  3507. NULL,
  3508. _cmDspBiQuadEqAlloc,
  3509. _cmDspBiQuadEqFree,
  3510. _cmDspBiQuadEqReset,
  3511. _cmDspBiQuadEqExec,
  3512. _cmDspBiQuadEqRecv,
  3513. NULL,NULL,
  3514. "Bi-Quad EQ Filters");
  3515. return &_cmBiQuadEqDC;
  3516. }
  3517. //==========================================================================================================================================
  3518. enum
  3519. {
  3520. kBypassDsId,
  3521. kInGainDsId,
  3522. kSrateDsId,
  3523. kBitsDsId,
  3524. kRectDsId,
  3525. kFullDsId,
  3526. kClipDbDsId,
  3527. kOutGainDsId,
  3528. kInDsId,
  3529. kOutDsId
  3530. };
  3531. cmDspClass_t _cmDistDsDC;
  3532. typedef struct
  3533. {
  3534. cmDspInst_t inst;
  3535. cmDistDs* p;
  3536. } cmDspDistDs_t;
  3537. cmDspInst_t* _cmDspDistDsAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3538. {
  3539. cmDspDistDs_t* p = cmDspInstAllocV(cmDspDistDs_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3540. 1, "bypass", kBypassDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable.",
  3541. 1, "igain", kInGainDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain.",
  3542. 1, "srate", kSrateDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Down-sample rate.",
  3543. 1, "bits", kBitsDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Bits per sample",
  3544. 1, "rect", kRectDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Rectify flag",
  3545. 1, "full", kFullDsId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "1=Full 0=Half rectify",
  3546. 1, "clip", kClipDbDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Clip dB",
  3547. 1, "ogain", kOutGainDsId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain",
  3548. 1, "in", kInDsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3549. 1, "out", kOutDsId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3550. 0 );
  3551. cmDspSetDefaultBool( ctx, &p->inst, kBypassDsId, false, false );
  3552. cmDspSetDefaultDouble( ctx, &p->inst, kInGainDsId, 0.0, 1.0 );
  3553. cmDspSetDefaultDouble( ctx, &p->inst, kSrateDsId, 0.0, cmDspSampleRate(ctx));
  3554. cmDspSetDefaultDouble( ctx, &p->inst, kBitsDsId, 0.0, 24.0 );
  3555. cmDspSetDefaultBool( ctx, &p->inst, kRectDsId, false, false );
  3556. cmDspSetDefaultBool( ctx, &p->inst, kFullDsId, false, false );
  3557. cmDspSetDefaultDouble( ctx, &p->inst, kClipDbDsId, 0.0, 0.0 );
  3558. cmDspSetDefaultDouble( ctx, &p->inst, kOutGainDsId, 0.0, 1.0 );
  3559. p->p = cmDistDsAlloc(ctx->cmProcCtx, NULL, 0, 0, 0, 0, false, false, 0, 0, false );
  3560. return &p->inst;
  3561. }
  3562. cmDspRC_t _cmDspDistDsFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3563. {
  3564. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3565. cmDistDsFree(&p->p);
  3566. return kOkDspRC;
  3567. }
  3568. cmDspRC_t _cmDspDistDsReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3569. {
  3570. cmDspRC_t rc = kOkDspRC;
  3571. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3572. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3573. {
  3574. cmDspZeroAudioBuf(ctx,inst,kOutDsId);
  3575. bool bypassFl = cmDspBool( inst, kBypassDsId );
  3576. cmReal_t inGain = cmDspDouble( inst, kInGainDsId );
  3577. cmReal_t downSrate = cmDspDouble( inst, kSrateDsId );
  3578. cmReal_t bits = cmDspDouble( inst, kBitsDsId );
  3579. bool rectFl = cmDspBool( inst, kRectDsId );
  3580. bool fullFl = cmDspBool( inst, kFullDsId );
  3581. cmReal_t clipDb = cmDspDouble( inst, kClipDbDsId );
  3582. cmReal_t outGain = cmDspDouble( inst, kOutGainDsId );
  3583. cmDistDsInit(p->p, cmDspSampleRate(ctx), inGain, downSrate, bits, rectFl, fullFl, clipDb, outGain, bypassFl );
  3584. }
  3585. return rc;
  3586. }
  3587. cmDspRC_t _cmDspDistDsExec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3588. {
  3589. cmDspRC_t rc = kOkDspRC;
  3590. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3591. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutDsId,0);
  3592. cmSample_t* y = cmDspAudioBuf(ctx,inst,kOutDsId,0);
  3593. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInDsId,0);
  3594. cmDistDsExec(p->p,x,y,n);
  3595. return rc;
  3596. }
  3597. cmDspRC_t _cmDspDistDsRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3598. {
  3599. cmDspRC_t rc = kOkDspRC;
  3600. cmDspDistDs_t* p = (cmDspDistDs_t*)inst;
  3601. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3602. {
  3603. switch( evt->dstVarId )
  3604. {
  3605. case kInGainDsId:
  3606. p->p->inGain = cmDspDouble(inst,kInGainDsId);
  3607. break;
  3608. case kSrateDsId:
  3609. p->p->downSrate = cmDspDouble(inst,kSrateDsId);
  3610. break;
  3611. case kBitsDsId:
  3612. p->p->bits = cmDspDouble(inst,kBitsDsId);
  3613. break;
  3614. case kRectDsId:
  3615. p->p->rectFl = cmDspBool(inst,kRectDsId);
  3616. break;
  3617. case kFullDsId:
  3618. p->p->fullFl = cmDspBool(inst,kFullDsId);
  3619. break;
  3620. case kClipDbDsId:
  3621. p->p->clipDb = cmDspDouble(inst,kClipDbDsId);
  3622. break;
  3623. case kOutGainDsId:
  3624. p->p->outGain = cmDspDouble(inst,kOutGainDsId);
  3625. break;
  3626. case kBypassDsId:
  3627. p->p->bypassFl = cmDspBool(inst,kBypassDsId);
  3628. break;
  3629. }
  3630. }
  3631. return rc;
  3632. }
  3633. struct cmDspClass_str* cmDistDsClassCons( cmDspCtx_t* ctx )
  3634. {
  3635. cmDspClassSetup(&_cmDistDsDC,ctx,"DistDs",
  3636. NULL,
  3637. _cmDspDistDsAlloc,
  3638. _cmDspDistDsFree,
  3639. _cmDspDistDsReset,
  3640. _cmDspDistDsExec,
  3641. _cmDspDistDsRecv,
  3642. NULL,NULL,
  3643. "Comb Filter");
  3644. return &_cmDistDsDC;
  3645. }
  3646. //==========================================================================================================================================
  3647. enum
  3648. {
  3649. kInDlId,
  3650. kOutDlId
  3651. };
  3652. cmDspClass_t _cmDbToLinDC;
  3653. typedef struct
  3654. {
  3655. cmDspInst_t inst;
  3656. } cmDspDbToLin_t;
  3657. cmDspInst_t* _cmDspDbToLinAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3658. {
  3659. cmDspDbToLin_t* p = cmDspInstAllocV(cmDspDbToLin_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3660. 1, "in", kInDlId, 0, 0, kInDsvFl | kDoubleDsvFl, "Input",
  3661. 1, "out", kOutDlId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  3662. 0 );
  3663. cmDspSetDefaultDouble( ctx, &p->inst, kInDlId, 0.0, -1000.0);
  3664. cmDspSetDefaultDouble( ctx, &p->inst, kOutDlId, 0.0, 0.0 );
  3665. return &p->inst;
  3666. }
  3667. cmDspRC_t _cmDspDbToLinReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3668. {
  3669. return cmDspApplyAllDefaults(ctx,inst);
  3670. }
  3671. cmDspRC_t _cmDspDbToLinRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3672. {
  3673. cmDspRC_t rc = kOkDspRC;
  3674. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  3675. {
  3676. if( evt->dstVarId == kInDlId )
  3677. {
  3678. double db = cmMax(0.0,cmMin(100.0,cmDspDouble(inst,kInDlId)));
  3679. double lin = db==0 ? 0.0 : pow(10.0, (db-100.0)/20.0);
  3680. cmDspSetDouble(ctx,inst,kOutDlId,lin);
  3681. }
  3682. }
  3683. return rc;
  3684. }
  3685. struct cmDspClass_str* cmDbToLinClassCons( cmDspCtx_t* ctx )
  3686. {
  3687. cmDspClassSetup(&_cmDbToLinDC,ctx,"DbToLin",
  3688. NULL,
  3689. _cmDspDbToLinAlloc,
  3690. NULL,
  3691. _cmDspDbToLinReset,
  3692. NULL,
  3693. _cmDspDbToLinRecv,
  3694. NULL,NULL,
  3695. "dB to Linear converter");
  3696. return &_cmDbToLinDC;
  3697. }
  3698. //==========================================================================================================================================
  3699. // Pass any N of M inputs
  3700. enum
  3701. {
  3702. kInChCntNoId,
  3703. kOutChCntNoId,
  3704. kXfadeMsNoId,
  3705. kCmdNoId,
  3706. kSelIdxNoId,
  3707. kBaseGateNoId
  3708. };
  3709. cmDspClass_t _cmNofM_DC;
  3710. typedef struct
  3711. {
  3712. cmDspInst_t inst;
  3713. unsigned iChCnt;
  3714. unsigned oChCnt;
  3715. unsigned* map; // map[ oChCnt ]
  3716. cmXfader** xf; // xf[ oChCnt ];
  3717. unsigned cfgSymId;
  3718. unsigned onSymId;
  3719. unsigned offSymId;
  3720. bool verboseFl;
  3721. unsigned baseBaseInNoId; // first data input port id
  3722. unsigned baseBaseOutNoId; // first data output port id
  3723. unsigned baseInFloatNoId;
  3724. unsigned baseInBoolNoId;
  3725. unsigned baseInSymNoId;
  3726. unsigned baseInAudioNoId;
  3727. unsigned baseOutFloatNoId;
  3728. unsigned baseOutBoolNoId;
  3729. unsigned baseOutSymNoId;
  3730. unsigned baseOutAudioNoId;
  3731. bool printFl;
  3732. } cmDspNofM_t;
  3733. cmDspInst_t* _cmDspNofM_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3734. {
  3735. if( va_cnt < 2 )
  3736. {
  3737. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'NofM' constructor must given input and output channel counts.");
  3738. return NULL;
  3739. }
  3740. va_list vl1;
  3741. va_copy(vl1,vl);
  3742. int iChCnt = va_arg(vl,int);
  3743. int oChCnt = va_arg(vl,int);
  3744. if( oChCnt > iChCnt )
  3745. {
  3746. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'NofM' output count must be less than or equal to the input count.");
  3747. return NULL;
  3748. }
  3749. unsigned baseInFloatNoId = kBaseGateNoId + iChCnt;
  3750. unsigned baseInBoolNoId = baseInFloatNoId + iChCnt;
  3751. unsigned baseInSymNoId = baseInBoolNoId + iChCnt;
  3752. unsigned baseInAudioNoId = baseInSymNoId + iChCnt;
  3753. unsigned baseOutFloatNoId = baseInAudioNoId + iChCnt;
  3754. unsigned baseOutBoolNoId = baseOutFloatNoId + oChCnt;
  3755. unsigned baseOutSymNoId = baseOutBoolNoId + oChCnt;
  3756. unsigned baseOutAudioNoId = baseOutSymNoId + oChCnt;
  3757. unsigned i;
  3758. cmDspNofM_t* p = cmDspInstAllocV(cmDspNofM_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3759. 1, "ichs", kInChCntNoId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Input channel count.",
  3760. 1, "ochs", kOutChCntNoId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Output channel count.",
  3761. 1, "ms", kXfadeMsNoId, 0, 0, kDoubleDsvFl | kInDsvFl | kOptArgDsvFl,"Audio Cross-fade time in milliseconds.",
  3762. 1, "cmd", kCmdNoId, 0, 0, kSymDsvFl | kInDsvFl, "Command input.",
  3763. 1, "seli", kSelIdxNoId, 0, 0, kUIntDsvFl | kInDsvFl, "Enable gate at index.",
  3764. iChCnt, "sel", kBaseGateNoId, 0, 0, kBoolDsvFl | kInDsvFl, "Selector Gate inputs.",
  3765. iChCnt, "f-in", baseInFloatNoId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  3766. iChCnt, "b-in", baseInBoolNoId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  3767. iChCnt, "s-in", baseInSymNoId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  3768. iChCnt, "a-in", baseInAudioNoId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  3769. oChCnt, "f-out", baseOutFloatNoId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  3770. oChCnt, "b-out", baseOutBoolNoId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  3771. oChCnt, "s-out", baseOutSymNoId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  3772. oChCnt, "a-out", baseOutAudioNoId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  3773. 0 );
  3774. p->iChCnt = iChCnt;
  3775. p->oChCnt = oChCnt;
  3776. p->map = cmMemAllocZ(unsigned,oChCnt);
  3777. p->xf = cmMemAllocZ(cmXfader*,oChCnt);
  3778. p->cfgSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"cfg");
  3779. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  3780. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  3781. p->verboseFl = false;
  3782. p->baseBaseInNoId = baseInFloatNoId;
  3783. p->baseBaseOutNoId = baseOutFloatNoId;
  3784. p->baseInFloatNoId = baseInFloatNoId;
  3785. p->baseInBoolNoId = baseInBoolNoId;
  3786. p->baseInSymNoId = baseInSymNoId;
  3787. p->baseInAudioNoId = baseInAudioNoId;
  3788. p->baseOutFloatNoId = baseOutFloatNoId;
  3789. p->baseOutBoolNoId = baseOutBoolNoId;
  3790. p->baseOutSymNoId = baseOutSymNoId;
  3791. p->baseOutAudioNoId = baseOutAudioNoId;
  3792. for(i=0; i<oChCnt; ++i)
  3793. {
  3794. cmDspSetDefaultDouble( ctx, &p->inst, baseOutFloatNoId + i, 0.0, 0.0 );
  3795. cmDspSetDefaultBool( ctx, &p->inst, baseOutBoolNoId + i, false, false );
  3796. cmDspSetDefaultSymbol( ctx, &p->inst, baseOutSymNoId + i, cmInvalidId );
  3797. p->xf[i] = cmXfaderAlloc( ctx->cmProcCtx, NULL, 0, 0, 0 );
  3798. }
  3799. cmDspSetDefaultDouble( ctx, &p->inst, kXfadeMsNoId, 0.0, 15.0 );
  3800. return &p->inst;
  3801. }
  3802. cmDspRC_t _cmDspNofM_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3803. {
  3804. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3805. unsigned i;
  3806. for(i=0; i<p->oChCnt; ++i)
  3807. cmXfaderFree(&p->xf[i]);
  3808. cmMemFree(p->map);
  3809. cmMemFree(p->xf);
  3810. return kOkDspRC;
  3811. }
  3812. cmDspRC_t _cmDspNofM_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3813. {
  3814. cmDspRC_t rc = kOkDspRC;
  3815. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3816. unsigned i;
  3817. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3818. {
  3819. for(i=0; i<p->oChCnt; ++i)
  3820. {
  3821. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioNoId+i);
  3822. p->map[i] = cmInvalidIdx;
  3823. double xfadeMs = cmDspDouble(inst,kXfadeMsNoId);
  3824. cmXfaderInit(p->xf[i], cmDspSampleRate(ctx), p->iChCnt, xfadeMs );
  3825. }
  3826. }
  3827. return rc;
  3828. }
  3829. cmDspRC_t _cmDspNofM_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3830. {
  3831. cmDspRC_t rc = kOkDspRC;
  3832. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3833. unsigned i;
  3834. const cmSample_t* x[ p->iChCnt ];
  3835. for(i=0; i<p->iChCnt; ++i)
  3836. x[i] = cmDspAudioBuf(ctx,inst,p->baseInAudioNoId + i ,0);
  3837. // for each valid p->map[] element
  3838. for(i=0; i<p->oChCnt; ++i)
  3839. {
  3840. cmSample_t* y = cmDspAudioBuf(ctx,inst,p->baseOutAudioNoId+ i,0);
  3841. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->baseOutAudioNoId+i,0);
  3842. if( y != NULL )
  3843. {
  3844. y = cmVOS_Zero(y,n);
  3845. cmXfaderExecAudio(p->xf[i],n,NULL,p->iChCnt,x,y);
  3846. }
  3847. }
  3848. return rc;
  3849. }
  3850. cmDspRC_t _cmDspNofM_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3851. {
  3852. cmDspRC_t rc = kOkDspRC;
  3853. cmDspNofM_t* p = (cmDspNofM_t*)inst;
  3854. unsigned i,j;
  3855. assert( evt->dstVarId < p->baseBaseOutNoId );
  3856. // store the incoming value
  3857. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3858. return rc;
  3859. // if this is a fade time
  3860. if( evt->dstVarId == kXfadeMsNoId )
  3861. {
  3862. for(i=0; i<p->oChCnt; ++i)
  3863. cmXfaderSetXfadeTime( p->xf[i], cmDspDouble(inst,kXfadeMsNoId) );
  3864. return rc;
  3865. }
  3866. // if this is an index selector
  3867. if( kSelIdxNoId == evt->dstVarId )
  3868. {
  3869. unsigned idx;
  3870. if((idx = cmDspUInt(inst,kSelIdxNoId)) >= p->iChCnt )
  3871. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The selection index ('%i') is out of the channel range, (%i).",idx,p->iChCnt);
  3872. else
  3873. {
  3874. cmDspSetBool( ctx, inst, kBaseGateNoId+idx , true );
  3875. if( p->verboseFl )
  3876. cmRptPrintf(ctx->rpt,"nom seli:%i\n",idx);
  3877. }
  3878. return rc;
  3879. }
  3880. // NOTE: the internal state DOES NOT CHANGE until a message arrives
  3881. // on the 'cmd' port
  3882. // if anything arrives on the command port - then read the gate states and rebuild the map
  3883. if( evt->dstVarId == kCmdNoId )
  3884. {
  3885. unsigned cmdSymId = cmDspSymbol(inst,kCmdNoId);
  3886. // if cmdSymId == 'on' then turn on all selection gates
  3887. if(cmdSymId == p->onSymId )
  3888. {
  3889. for(i=0; i<p->oChCnt; ++i)
  3890. cmDspSetBool(ctx,inst,kBaseGateNoId+i,true);
  3891. }
  3892. else
  3893. // if cmdSymId == 'off' then turn off all selection gates
  3894. if( cmdSymId == p->offSymId )
  3895. {
  3896. if( p->verboseFl )
  3897. cmRptPrintf(ctx->rpt,"nom: off\n");
  3898. for(i=0; i<p->oChCnt; ++i)
  3899. cmDspSetBool(ctx,inst,kBaseGateNoId+i,false);
  3900. }
  3901. else
  3902. // cmdSymId == 'print' then print the in/out map[]
  3903. if( cmdSymId == cmSymTblId(ctx->stH,"print") )
  3904. {
  3905. for(i=0; i<p->oChCnt; ++i)
  3906. cmRptPrintf(ctx->rpt,"%i:%i ",i,p->map[i]);
  3907. cmRptPrintf(ctx->rpt,"\n");
  3908. cmRptPrintf(ctx->rpt,"%i usecs\n",ctx->execDurUsecs);
  3909. p->printFl = !p->printFl;
  3910. }
  3911. if( p->verboseFl )
  3912. cmRptPrintf(ctx->rpt,"Nom: %s ", inst->symId != cmInvalidId ? cmSymTblLabel(ctx->stH,inst->symId) : "");
  3913. // 5/26
  3914. if( p->iChCnt == p->oChCnt )
  3915. cmVOU_Fill(p->map,p->oChCnt,cmInvalidIdx);
  3916. // for each input
  3917. for(i=0,j=0; i<p->iChCnt; ++i)
  3918. {
  3919. // if this input is switched on
  3920. if( cmDspBool(inst,kBaseGateNoId+i) )
  3921. {
  3922. if( j >= p->oChCnt )
  3923. {
  3924. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"To many inputs have been turned on for %i outputs.",p->oChCnt);
  3925. break;
  3926. }
  3927. // assign input i to output j
  3928. p->map[j] = i;
  3929. // fade in ch i and fade out all others
  3930. cmXfaderSelectOne(p->xf[j],i);
  3931. ++j;
  3932. if( p->verboseFl )
  3933. cmRptPrintf(ctx->rpt,"%i ",i);
  3934. }
  3935. else // 5/26
  3936. {
  3937. if( p->iChCnt == p->oChCnt )
  3938. ++j;
  3939. }
  3940. }
  3941. // deselect all other output channels
  3942. //for(; j<p->oChCnt; ++j)
  3943. //{
  3944. // p->map[j] = cmInvalidIdx;
  3945. // cmXfaderAllOff(p->xf[j]);
  3946. //}
  3947. // 5/26
  3948. if( p->iChCnt == p->oChCnt )
  3949. {
  3950. for(j=0; j<p->oChCnt; ++j)
  3951. if( p->map[j] == cmInvalidIdx )
  3952. cmXfaderAllOff(p->xf[j]);
  3953. }
  3954. else
  3955. {
  3956. for(; j<p->oChCnt; ++j)
  3957. {
  3958. p->map[j] = cmInvalidIdx;
  3959. cmXfaderAllOff(p->xf[j]);
  3960. }
  3961. }
  3962. if( p->verboseFl )
  3963. cmRptPrintf(ctx->rpt,"\n");
  3964. // zero the audio buffers of unused output channels
  3965. for(i=0; i<p->oChCnt; ++i)
  3966. if( p->map[i] == cmInvalidIdx )
  3967. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioNoId+i);
  3968. }
  3969. // if this is an input data event
  3970. if( p->baseBaseInNoId <= evt->dstVarId && evt->dstVarId < p->baseBaseOutNoId )
  3971. {
  3972. // get the input channel this event occurred on
  3973. unsigned iChIdx = (evt->dstVarId - p->baseBaseInNoId) % p->iChCnt;
  3974. // is iChIdx mapped to an output ...
  3975. for(i=0; i<p->oChCnt; ++i)
  3976. if( p->map[i] == iChIdx )
  3977. break;
  3978. // ... no - nothing else to do
  3979. if( i==p->oChCnt )
  3980. return kOkDspRC;
  3981. // ... yes set the output ...
  3982. // double
  3983. if( p->baseInFloatNoId <= evt->dstVarId && evt->dstVarId < p->baseInFloatNoId + p->iChCnt )
  3984. {
  3985. cmDspSetDouble(ctx,inst,p->baseOutFloatNoId + i, cmDspDouble(inst,evt->dstVarId));
  3986. }
  3987. else
  3988. // bool
  3989. if( p->baseInBoolNoId <= evt->dstVarId && evt->dstVarId < p->baseInBoolNoId + p->iChCnt )
  3990. {
  3991. cmDspSetBool(ctx,inst,p->baseOutBoolNoId + i, cmDspBool(inst,evt->dstVarId));
  3992. if(p->printFl)
  3993. cmRptPrintf(ctx->rpt,"%i %i\n",p->baseOutBoolNoId + i, cmDspBool(inst,evt->dstVarId));
  3994. }
  3995. else
  3996. // symbol
  3997. if( p->baseInSymNoId <= evt->dstVarId && evt->dstVarId < p->baseInSymNoId + p->iChCnt )
  3998. cmDspSetSymbol(ctx,inst,p->baseOutSymNoId + i, cmDspSymbol(inst,evt->dstVarId));
  3999. }
  4000. return rc;
  4001. }
  4002. struct cmDspClass_str* cmNofMClassCons( cmDspCtx_t* ctx )
  4003. {
  4004. cmDspClassSetup(&_cmNofM_DC,ctx,"NofM",
  4005. NULL,
  4006. _cmDspNofM_Alloc,
  4007. _cmDspNofM_Free,
  4008. _cmDspNofM_Reset,
  4009. _cmDspNofM_Exec,
  4010. _cmDspNofM_Recv,
  4011. NULL,NULL,
  4012. "N of M Switch");
  4013. return &_cmNofM_DC;
  4014. }
  4015. //==========================================================================================================================================
  4016. enum
  4017. {
  4018. kInChCnt1oId,
  4019. kInChIdx1oId,
  4020. kOutFloat1oId,
  4021. kOutBool1oId,
  4022. kOutSym1oId,
  4023. kOutAudio1oId,
  4024. kBaseInFloat1oId
  4025. };
  4026. cmDspClass_t _cm1ofN_DC;
  4027. typedef struct
  4028. {
  4029. cmDspInst_t inst;
  4030. unsigned iChCnt;
  4031. unsigned oChCnt;
  4032. unsigned iChIdx;
  4033. unsigned baseBaseIn1oId; // first data input port id
  4034. unsigned baseInFloat1oId;
  4035. unsigned baseInBool1oId;
  4036. unsigned baseInSym1oId;
  4037. unsigned baseInAudio1oId;
  4038. unsigned baseOutFloat1oId;
  4039. unsigned baseOutBool1oId;
  4040. unsigned baseOutSym1oId;
  4041. unsigned baseOutAudio1oId;
  4042. } cmDsp1ofN_t;
  4043. cmDspInst_t* _cmDsp1ofN_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4044. {
  4045. if( va_cnt < 1 )
  4046. {
  4047. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The '1ofN' constructor must given input channel count.");
  4048. return NULL;
  4049. }
  4050. va_list vl1;
  4051. va_copy(vl1,vl);
  4052. int iChCnt = va_arg(vl,int);
  4053. unsigned baseInFloat1oId = kBaseInFloat1oId;
  4054. unsigned baseInBool1oId = baseInFloat1oId + iChCnt;
  4055. unsigned baseInSym1oId = baseInBool1oId + iChCnt;
  4056. unsigned baseInAudio1oId = baseInSym1oId + iChCnt;
  4057. cmDsp1ofN_t* p = cmDspInstAllocV(cmDsp1ofN_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4058. 1, "ichs", kInChCnt1oId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Input channel count.",
  4059. 1, "chidx", kInChIdx1oId, 0, 0, kUIntDsvFl | kReqArgDsvFl | kInDsvFl, "Input channel selector index.",
  4060. 1, "f-out", kOutFloat1oId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  4061. 1, "b-out", kOutBool1oId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  4062. 1, "s-out", kOutSym1oId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  4063. 1, "a-out", kOutAudio1oId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  4064. iChCnt, "f-in", baseInFloat1oId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  4065. iChCnt, "b-in", baseInBool1oId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  4066. iChCnt, "s-in", baseInSym1oId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  4067. iChCnt, "a-in", baseInAudio1oId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  4068. 0 );
  4069. p->iChCnt = iChCnt;
  4070. p->baseBaseIn1oId = kBaseInFloat1oId;
  4071. p->baseInFloat1oId = baseInFloat1oId;
  4072. p->baseInBool1oId = baseInBool1oId;
  4073. p->baseInSym1oId = baseInSym1oId;
  4074. p->baseInAudio1oId = baseInAudio1oId;
  4075. cmDspSetDefaultDouble( ctx, &p->inst, kOutFloat1oId, 0.0, 0.0 );
  4076. cmDspSetDefaultBool( ctx, &p->inst, kOutBool1oId, false, false );
  4077. cmDspSetDefaultSymbol( ctx, &p->inst, kOutSym1oId, cmInvalidId );
  4078. return &p->inst;
  4079. }
  4080. cmDspRC_t _cmDsp1ofN_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4081. {
  4082. return kOkDspRC;
  4083. }
  4084. cmDspRC_t _cmDsp1ofN_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4085. {
  4086. cmDspRC_t rc = kOkDspRC;
  4087. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4088. {
  4089. cmDspZeroAudioBuf(ctx,inst,kOutAudio1oId);
  4090. }
  4091. return rc;
  4092. }
  4093. cmDspRC_t _cmDsp1ofN_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4094. {
  4095. cmDspRC_t rc = kOkDspRC;
  4096. cmDsp1ofN_t* p = (cmDsp1ofN_t*)inst;
  4097. unsigned iChIdx = cmDspUInt(inst,kInChIdx1oId);
  4098. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAudio1oId,0);
  4099. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,p->baseInAudio1oId + iChIdx ,0);
  4100. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAudio1oId,0);
  4101. if( dp != NULL )
  4102. {
  4103. if( sp == NULL )
  4104. cmVOS_Zero(dp,n);
  4105. else
  4106. cmVOS_Copy(dp,n,sp);
  4107. }
  4108. return rc;
  4109. }
  4110. cmDspRC_t _cmDsp1ofN_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4111. {
  4112. cmDspRC_t rc = kOkDspRC;
  4113. cmDsp1ofN_t* p = (cmDsp1ofN_t*)inst;
  4114. // ignore out of range input channel
  4115. if( evt->dstVarId == kInChIdx1oId && cmDsvGetUInt(evt->valuePtr) >= p->iChCnt )
  4116. {
  4117. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The selector channel index %i is out of range.",cmDsvGetUInt(evt->valuePtr));
  4118. return kOkDspRC;
  4119. }
  4120. // store the incoming value
  4121. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4122. return rc;
  4123. // if this is an input data event
  4124. if( p->baseBaseIn1oId <= evt->dstVarId )
  4125. {
  4126. // get the input channel this event occurred on
  4127. unsigned iChIdx = (evt->dstVarId - p->baseBaseIn1oId) % p->iChCnt;
  4128. // if the event did not arrive on the selected input channel - there is nothing else to do
  4129. if( iChIdx != cmDspUInt(inst,kInChIdx1oId) )
  4130. return kOkDspRC;
  4131. // The event arrived on the input channel - send it out the output
  4132. // double
  4133. if( p->baseInFloat1oId <= evt->dstVarId && evt->dstVarId < p->baseInFloat1oId + p->iChCnt )
  4134. cmDspSetDouble(ctx,inst,kOutFloat1oId, cmDspDouble(inst,evt->dstVarId));
  4135. else
  4136. // bool
  4137. if( p->baseInBool1oId <= evt->dstVarId && evt->dstVarId < p->baseInBool1oId + p->iChCnt )
  4138. cmDspSetBool(ctx,inst,kOutBool1oId, cmDspBool(inst,evt->dstVarId));
  4139. else
  4140. // symbol
  4141. if( p->baseInSym1oId <= evt->dstVarId && evt->dstVarId < p->baseInSym1oId + p->iChCnt )
  4142. cmDspSetSymbol(ctx,inst,kOutSym1oId, cmDspSymbol(inst,evt->dstVarId));
  4143. }
  4144. return rc;
  4145. }
  4146. struct cmDspClass_str* cm1ofNClassCons( cmDspCtx_t* ctx )
  4147. {
  4148. cmDspClassSetup(&_cm1ofN_DC,ctx,"1ofN",
  4149. NULL,
  4150. _cmDsp1ofN_Alloc,
  4151. _cmDsp1ofN_Free,
  4152. _cmDsp1ofN_Reset,
  4153. _cmDsp1ofN_Exec,
  4154. _cmDsp1ofN_Recv,
  4155. NULL,NULL,
  4156. " 1 of N Switch");
  4157. return &_cm1ofN_DC;
  4158. }
  4159. //==========================================================================================================================================
  4160. // Send a 'true' out on the selected channel.
  4161. // Send a 'false' out on the deselected channel.
  4162. enum
  4163. {
  4164. kChCnt1uId,
  4165. kSel1uId,
  4166. kBaseOut1uId
  4167. };
  4168. cmDspClass_t _cm1Up_DC;
  4169. typedef struct
  4170. {
  4171. cmDspInst_t inst;
  4172. } cmDsp1Up_t;
  4173. cmDspInst_t* _cmDsp1Up_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4174. {
  4175. if( va_cnt < 1 )
  4176. {
  4177. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The '1Up' constructor must given output channel count.");
  4178. return NULL;
  4179. }
  4180. va_list vl1;
  4181. va_copy(vl1,vl);
  4182. int chCnt = va_arg(vl,int);
  4183. cmDsp1Up_t* p = cmDspInstAllocV(cmDsp1Up_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4184. 1, "chcnt", kChCnt1uId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Output channel count.",
  4185. 1, "sel", kSel1uId, 0, 0, kUIntDsvFl | kOptArgDsvFl | kInDsvFl, "Channel index selector.",
  4186. chCnt, "out", kBaseOut1uId, 0, 0, kBoolDsvFl | kOutDsvFl, "Gate outputs",
  4187. 0 );
  4188. unsigned i;
  4189. cmDspSetDefaultUInt( ctx, &p->inst, kSel1uId, 0.0, 0.0 );
  4190. for(i=0; i<chCnt; ++i)
  4191. cmDspSetDefaultBool( ctx, &p->inst, kBaseOut1uId + i, false, false );
  4192. return &p->inst;
  4193. }
  4194. cmDspRC_t _cmDsp1Up_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4195. {
  4196. cmDspRC_t rc = kOkDspRC;
  4197. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4198. {
  4199. unsigned chIdx = cmDspUInt(inst,kSel1uId);
  4200. unsigned chCnt = cmDspUInt(inst,kChCnt1uId);
  4201. unsigned i;
  4202. for(i=0; i<chCnt; ++i)
  4203. cmDspSetBool(ctx,inst,kBaseOut1uId+i, i == chIdx );
  4204. }
  4205. return rc;
  4206. }
  4207. cmDspRC_t _cmDsp1Up_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4208. {
  4209. cmDspRC_t rc = kOkDspRC;
  4210. if( evt->dstVarId == kSel1uId)
  4211. {
  4212. unsigned chIdx = cmDspUInt(inst,kSel1uId);
  4213. unsigned chCnt = cmDspUInt(inst,kChCnt1uId);
  4214. // turn off the previously selected channel
  4215. if( chIdx != cmInvalidIdx && chIdx < chCnt )
  4216. cmDspSetBool(ctx,inst,kBaseOut1uId+chIdx,false);
  4217. // set the new channel index
  4218. cmDspSetEvent(ctx,inst,evt);
  4219. // get the new channel index
  4220. chIdx = cmDspUInt(inst,kSel1uId);
  4221. // send the new channel index
  4222. if( chIdx != cmInvalidIdx && chIdx < chCnt )
  4223. cmDspSetBool(ctx,inst,kBaseOut1uId+chIdx,true);
  4224. }
  4225. return rc;
  4226. }
  4227. struct cmDspClass_str* cm1UpClassCons( cmDspCtx_t* ctx )
  4228. {
  4229. cmDspClassSetup(&_cm1Up_DC,ctx,"1Up",
  4230. NULL,
  4231. _cmDsp1Up_Alloc,
  4232. NULL,
  4233. _cmDsp1Up_Reset,
  4234. NULL,
  4235. _cmDsp1Up_Recv,
  4236. NULL,NULL,
  4237. "Set one input high and all others low.");
  4238. return &_cm1Up_DC;
  4239. }
  4240. //==========================================================================================================================================
  4241. // Convert a 'true'/'false' gate to an 'on'/'off' symbol
  4242. enum
  4243. {
  4244. kOnSymGsId,
  4245. kOffSymGsId,
  4246. kOnGsId,
  4247. kOffGsId,
  4248. kBothGsId,
  4249. kOutGsId
  4250. };
  4251. cmDspClass_t _cmGateToSym_DC;
  4252. typedef struct
  4253. {
  4254. cmDspInst_t inst;
  4255. } cmDspGateToSym_t;
  4256. cmDspInst_t* _cmDspGateToSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4257. {
  4258. cmDspGateToSym_t* p = cmDspInstAllocV(cmDspGateToSym_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4259. 1, "on_sym", kOnSymGsId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl,"'on' symbol id (default:'on')",
  4260. 1, "off_sym",kOffSymGsId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl,"'off' symbol id (default:'off')",
  4261. 1, "on", kOnGsId, 0, 0, kBoolDsvFl | kInDsvFl, "On - send out 'on' symbol when a 'true' is received.",
  4262. 1, "off", kOffGsId, 0, 0, kBoolDsvFl | kInDsvFl, "Off - send out 'off' symbol when a 'false' is received.",
  4263. 1, "both", kBothGsId, 0, 0, kBoolDsvFl | kInDsvFl, "Send 'on' on 'true' and 'off' on 'false'.",
  4264. 1, "out", kOutGsId, 0, 0, kSymDsvFl | kOutDsvFl, "Output",
  4265. 0 );
  4266. unsigned onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  4267. unsigned offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  4268. cmDspSetDefaultSymbol( ctx, &p->inst, kOnSymGsId, onSymId );
  4269. cmDspSetDefaultSymbol( ctx, &p->inst, kOffSymGsId, offSymId );
  4270. cmDspSetDefaultSymbol( ctx, &p->inst, kOutGsId, cmInvalidId );
  4271. return &p->inst;
  4272. }
  4273. cmDspRC_t _cmDspGateToSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4274. {
  4275. cmDspRC_t rc = kOkDspRC;
  4276. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4277. {
  4278. }
  4279. return rc;
  4280. }
  4281. cmDspRC_t _cmDspGateToSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4282. {
  4283. cmDspRC_t rc = kOkDspRC;
  4284. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  4285. {
  4286. unsigned onSymId = cmDspSymbol(inst,kOnSymGsId);
  4287. unsigned offSymId = cmDspSymbol(inst,kOffSymGsId);
  4288. switch( evt->dstVarId )
  4289. {
  4290. case kOnGsId:
  4291. if( cmDspBool(inst,kOnGsId) )
  4292. cmDspSetSymbol(ctx,inst,kOutGsId,onSymId);
  4293. break;
  4294. case kOffGsId:
  4295. if( cmDspBool(inst,kOffGsId) )
  4296. cmDspSetSymbol(ctx,inst,kOutGsId,offSymId);
  4297. break;
  4298. case kBothGsId:
  4299. cmDspSetSymbol(ctx,inst, kOutGsId, cmDspBool(inst,kBothGsId) ? onSymId : offSymId);
  4300. break;
  4301. }
  4302. }
  4303. return rc;
  4304. }
  4305. struct cmDspClass_str* cmGateToSymClassCons( cmDspCtx_t* ctx )
  4306. {
  4307. cmDspClassSetup(&_cmGateToSym_DC,ctx,"GateToSym",
  4308. NULL,
  4309. _cmDspGateToSym_Alloc,
  4310. NULL,
  4311. _cmDspGateToSym_Reset,
  4312. NULL,
  4313. _cmDspGateToSym_Recv,
  4314. NULL,NULL,
  4315. "Convert a 'true'/'false' gate to an 'on'/'off' symbol.");
  4316. return &_cmGateToSym_DC;
  4317. }
  4318. //==========================================================================================================================================
  4319. enum
  4320. {
  4321. kOutPtsId,
  4322. kBaseInPtsId
  4323. };
  4324. cmDspClass_t _cmPortToSym_DC;
  4325. typedef struct
  4326. {
  4327. cmDspInst_t inst;
  4328. unsigned* symIdArray;
  4329. unsigned symIdCnt;
  4330. unsigned baseOutPtsId;
  4331. } cmDspPortToSym_t;
  4332. cmDspInst_t* _cmDspPortToSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4333. {
  4334. va_list vl1;
  4335. va_copy(vl1,vl);
  4336. if( va_cnt < 1 )
  4337. {
  4338. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'PortToSym' constructor argument list must contain at least one symbol label.");
  4339. return NULL;
  4340. }
  4341. unsigned symCnt = va_cnt;
  4342. unsigned argCnt = 1 + 2*symCnt;
  4343. cmDspVarArg_t args[argCnt+1];
  4344. unsigned* symIdArray = cmMemAllocZ(unsigned,symCnt);
  4345. unsigned baseOutPtsId = kBaseInPtsId + symCnt;
  4346. // setup the output port arg recd
  4347. cmDspArgSetup(ctx,args,"out",cmInvalidId,kOutPtsId,0,0,kOutDsvFl | kSymDsvFl, "Output" );
  4348. unsigned i;
  4349. for(i=0; i<symCnt; ++i)
  4350. {
  4351. // get the symbol label
  4352. const cmChar_t* symLabel = va_arg(vl,const char*);
  4353. assert( symLabel != NULL );
  4354. // register the symbol
  4355. symIdArray[i] = cmSymTblRegisterSymbol(ctx->stH,symLabel);
  4356. cmDspArgSetup(ctx, args+kBaseInPtsId+i, symLabel, cmInvalidId, kBaseInPtsId+i, 0, 0, kInDsvFl | kTypeDsvMask, cmTsPrintfH(ctx->lhH,"%s Input.",symLabel) );
  4357. cmDspArgSetup(ctx, args+baseOutPtsId+i, symLabel, cmInvalidId, baseOutPtsId+i, 0, 0, kOutDsvFl | kSymDsvFl, cmTsPrintfH(ctx->lhH,"%s Output.",symLabel) );
  4358. }
  4359. cmDspArgSetupNull(args + argCnt);
  4360. cmDspPortToSym_t* p = cmDspInstAlloc(cmDspPortToSym_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl1);
  4361. p->symIdCnt = symCnt;
  4362. p->symIdArray = symIdArray;
  4363. p->baseOutPtsId = baseOutPtsId;
  4364. cmDspSetDefaultSymbol(ctx,&p->inst,kOutPtsId,cmInvalidId);
  4365. return &p->inst;
  4366. }
  4367. cmDspRC_t _cmDspPortToSym_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4368. {
  4369. cmDspPortToSym_t* p = (cmDspPortToSym_t*)inst;
  4370. cmMemFree(p->symIdArray);
  4371. return kOkDspRC;
  4372. }
  4373. cmDspRC_t _cmDspPortToSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4374. {
  4375. return cmDspApplyAllDefaults(ctx,inst);
  4376. }
  4377. cmDspRC_t _cmDspPortToSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4378. {
  4379. cmDspRC_t rc = kOkDspRC;
  4380. cmDspPortToSym_t* p = (cmDspPortToSym_t*)inst;
  4381. // if a msg of any type is recieved on an input port - send out the associated symbol
  4382. if( kBaseInPtsId <= evt->dstVarId && evt->dstVarId < kBaseInPtsId + p->symIdCnt )
  4383. {
  4384. unsigned idx = evt->dstVarId - kBaseInPtsId;
  4385. assert( idx < p->symIdCnt );
  4386. cmDspSetSymbol(ctx,inst,p->baseOutPtsId + idx, p->symIdArray[idx]);
  4387. return cmDspSetSymbol(ctx,inst,kOutPtsId,p->symIdArray[ evt->dstVarId - kBaseInPtsId ]);
  4388. }
  4389. return rc;
  4390. }
  4391. struct cmDspClass_str* cmPortToSymClassCons( cmDspCtx_t* ctx )
  4392. {
  4393. cmDspClassSetup(&_cmPortToSym_DC,ctx,"PortToSym",
  4394. NULL,
  4395. _cmDspPortToSym_Alloc,
  4396. _cmDspPortToSym_Free,
  4397. _cmDspPortToSym_Reset,
  4398. NULL,
  4399. _cmDspPortToSym_Recv,
  4400. NULL,NULL,
  4401. "If a message of any kind is received on a port then send the symbol associated with the port.");
  4402. return &_cmPortToSym_DC;
  4403. }
  4404. //==========================================================================================================================================
  4405. enum
  4406. {
  4407. kOutChCntRtId,
  4408. kOutChIdxRtId,
  4409. kInFloatRtId,
  4410. kInBoolRtId,
  4411. kInSymRtId,
  4412. kInAudioRtId,
  4413. kBaseOutFloatRtId
  4414. };
  4415. cmDspClass_t _cmRouter_DC;
  4416. typedef struct
  4417. {
  4418. cmDspInst_t inst;
  4419. unsigned oChCnt;
  4420. unsigned oChIdx;
  4421. unsigned baseBaseOutRtId; // first data input port id
  4422. unsigned baseInFloatRtId;
  4423. unsigned baseInBoolRtId;
  4424. unsigned baseInSymRtId;
  4425. unsigned baseInAudioRtId;
  4426. unsigned baseOutFloatRtId;
  4427. unsigned baseOutBoolRtId;
  4428. unsigned baseOutSymRtId;
  4429. unsigned baseOutAudioRtId;
  4430. } cmDspRouter_t;
  4431. cmDspInst_t* _cmDspRouter_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4432. {
  4433. if( va_cnt < 1 )
  4434. {
  4435. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Router' constructor must be given an output channel count.");
  4436. return NULL;
  4437. }
  4438. va_list vl1;
  4439. va_copy(vl1,vl);
  4440. int oChCnt = va_arg(vl,int);
  4441. unsigned baseOutFloatRtId = kBaseOutFloatRtId;
  4442. unsigned baseOutBoolRtId = baseOutFloatRtId + oChCnt;
  4443. unsigned baseOutSymRtId = baseOutBoolRtId + oChCnt;
  4444. unsigned baseOutAudioRtId = baseOutSymRtId + oChCnt;
  4445. cmDspRouter_t* p = cmDspInstAllocV(cmDspRouter_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4446. 1, "ochs", kOutChCntRtId, 0, 0, kUIntDsvFl | kReqArgDsvFl,"Output channel count.",
  4447. 1, "sel", kOutChIdxRtId, 0, 0, kUIntDsvFl | kReqArgDsvFl | kInDsvFl, "Output channel index selector.",
  4448. 1, "f-in", kInFloatRtId, 0, 0, kDoubleDsvFl | kInDsvFl, "Float input",
  4449. 1, "b-in", kInBoolRtId, 0, 0, kBoolDsvFl | kInDsvFl, "Bool input",
  4450. 1, "s-in", kInSymRtId, 0, 0, kSymDsvFl | kInDsvFl, "Symbol input",
  4451. 1, "a-in", kInAudioRtId, 0, 0, kAudioBufDsvFl | kInDsvFl, "Audio input",
  4452. oChCnt, "f-out", baseOutFloatRtId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Float output",
  4453. oChCnt, "b-out", baseOutBoolRtId, 0, 0, kBoolDsvFl | kOutDsvFl, "Bool output",
  4454. oChCnt, "s-out", baseOutSymRtId, 0, 0, kSymDsvFl | kOutDsvFl, "Symbol output",
  4455. oChCnt, "a-out", baseOutAudioRtId, 0, 1, kAudioBufDsvFl | kOutDsvFl, "Audio output",
  4456. 0 );
  4457. p->oChCnt = oChCnt;
  4458. p->baseBaseOutRtId = kBaseOutFloatRtId;
  4459. p->baseOutFloatRtId = baseOutFloatRtId;
  4460. p->baseOutBoolRtId = baseOutBoolRtId;
  4461. p->baseOutSymRtId = baseOutSymRtId;
  4462. p->baseOutAudioRtId = baseOutAudioRtId;
  4463. unsigned i;
  4464. for(i=0; i<oChCnt; ++i)
  4465. {
  4466. cmDspSetDefaultDouble( ctx, &p->inst, baseOutFloatRtId+i, 0.0, 0.0 );
  4467. cmDspSetDefaultBool( ctx, &p->inst, baseOutBoolRtId+i, false, false );
  4468. cmDspSetDefaultSymbol( ctx, &p->inst, baseOutSymRtId+i, cmInvalidId );
  4469. }
  4470. return &p->inst;
  4471. }
  4472. cmDspRC_t _cmDspRouter_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4473. {
  4474. return kOkDspRC;
  4475. }
  4476. cmDspRC_t _cmDspRouter_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4477. {
  4478. cmDspRC_t rc = kOkDspRC;
  4479. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4480. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4481. {
  4482. unsigned i;
  4483. for(i=0; i<p->oChCnt; ++i)
  4484. cmDspZeroAudioBuf(ctx,inst,p->baseOutAudioRtId+i);
  4485. }
  4486. return rc;
  4487. }
  4488. cmDspRC_t _cmDspRouter_Exec( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4489. {
  4490. cmDspRC_t rc = kOkDspRC;
  4491. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4492. unsigned oChIdx = cmDspUInt(inst,kOutChIdxRtId);
  4493. cmSample_t* dp = cmDspAudioBuf(ctx,inst,p->baseOutAudioRtId + oChIdx,0);
  4494. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAudioRtId ,0);
  4495. unsigned n = cmDspAudioBufSmpCount(ctx,inst,p->baseOutAudioRtId,0);
  4496. if( dp != NULL )
  4497. {
  4498. if( sp == NULL )
  4499. cmVOS_Zero(dp,n);
  4500. else
  4501. cmVOS_Copy(dp,n,sp);
  4502. }
  4503. return rc;
  4504. }
  4505. cmDspRC_t _cmDspRouter_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4506. {
  4507. cmDspRC_t rc = kOkDspRC;
  4508. cmDspRouter_t* p = (cmDspRouter_t*)inst;
  4509. // ignore out of range output channel selection
  4510. if( evt->dstVarId == kOutChIdxRtId && cmDsvGetUInt(evt->valuePtr) >= p->oChCnt )
  4511. {
  4512. cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The selector channel index %i is out of of range.",cmDsvGetUInt(evt->valuePtr));
  4513. return kOkDspRC;
  4514. }
  4515. // store the incoming value
  4516. if( evt->dstVarId < p->baseBaseOutRtId )
  4517. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4518. return rc;
  4519. unsigned chIdx = cmDspUInt(inst,kOutChIdxRtId);
  4520. switch( evt->dstVarId )
  4521. {
  4522. case kInFloatRtId:
  4523. cmDspSetDouble(ctx,inst,p->baseOutFloatRtId + chIdx, cmDspDouble(inst,kInFloatRtId) );
  4524. break;
  4525. case kInBoolRtId:
  4526. cmDspSetBool(ctx,inst,p->baseOutBoolRtId + chIdx, cmDspBool(inst,kInBoolRtId) );
  4527. break;
  4528. case kInSymRtId:
  4529. cmDspSetSymbol(ctx,inst,p->baseOutSymRtId + chIdx, cmDspSymbol(inst,kInSymRtId));
  4530. break;
  4531. }
  4532. return rc;
  4533. }
  4534. struct cmDspClass_str* cmRouterClassCons( cmDspCtx_t* ctx )
  4535. {
  4536. cmDspClassSetup(&_cmRouter_DC,ctx,"Router",
  4537. NULL,
  4538. _cmDspRouter_Alloc,
  4539. _cmDspRouter_Free,
  4540. _cmDspRouter_Reset,
  4541. _cmDspRouter_Exec,
  4542. _cmDspRouter_Recv,
  4543. NULL,NULL,
  4544. "1 to N Router");
  4545. return &_cmRouter_DC;
  4546. }
  4547. //==========================================================================================================================================
  4548. // Purpose: AvailCh can be used to implement a channel switching circuit.
  4549. //
  4550. // Inputs:
  4551. // chs - The count of channels. Constructor only argument.
  4552. // trig - Any input causes the next available channel, i, to be enabled.
  4553. // gate[i] transmits 'true'. In 'exclusive (0) mode all active
  4554. // channels are then requested to shutdown by transmitting 'false' on
  4555. // gate[] - only the new channel will be active. In 'multi' (1) mode
  4556. // no signal is sent out the gate[].
  4557. // dis[chCnt] - Recieves a gate signal from an external object which indicates
  4558. // when a channel is no longer active. When a 'false' is received on dis[i]
  4559. // the channel i is marked as available. In 'multi' mode 'false' is
  4560. // then transmitted on gate[i].
  4561. // Outputs:
  4562. // gate[chCnt] - 'true' is transmitted when a channel is made active (see trig)
  4563. // 'false' is transmitted to notify the channel that it should shutdown.
  4564. // The channel is not considered actually shutdown until dis[i]
  4565. // recieves a 'false'.
  4566. // Notes:
  4567. // The gate[] output is designed to work with the gate[] input of Xfader. When
  4568. // availCh.gate[] goes high Xfader fades in, when availCh.gate[] goes low
  4569. // Xfader fades out. The dis[] channel is designed to connect from Xfader.state[].
  4570. // When Xfader.state[] goes low, when a fade-out is complete, the connected AvailCh
  4571. // is marked as available.
  4572. enum
  4573. {
  4574. kChCntAvId,
  4575. kModeAvId,
  4576. kTrigAvId,
  4577. kChIdxAvId,
  4578. kBaseDisInAvId,
  4579. kExclusiveModeAvId=0,
  4580. kMultiModeAvId=1
  4581. };
  4582. cmDspClass_t _cmAvailCh_DC;
  4583. typedef struct
  4584. {
  4585. cmDspInst_t inst;
  4586. unsigned chCnt;
  4587. unsigned baseDisInAvId;
  4588. unsigned baseGateOutAvId;
  4589. bool* stateArray;
  4590. } cmDspAvailCh_t;
  4591. cmDspInst_t* _cmDspAvailCh_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4592. {
  4593. if( va_cnt < 1 )
  4594. {
  4595. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AvailCh' constructor must be given an channel count.");
  4596. return NULL;
  4597. }
  4598. va_list vl1;
  4599. va_copy(vl1,vl);
  4600. int chCnt = va_arg(vl,int);
  4601. if( chCnt <= 0 )
  4602. {
  4603. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The 'AvailCh' constructor must be given a positive channel count.");
  4604. return NULL;
  4605. }
  4606. unsigned baseDisInAvId = kBaseDisInAvId;
  4607. unsigned baseGateOutAvId = baseDisInAvId + chCnt;
  4608. cmDspAvailCh_t* p = cmDspInstAllocV(cmDspAvailCh_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  4609. 1, "chs", kChCntAvId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Channel count.",
  4610. 1, "mode", kModeAvId, 0, 0, kUIntDsvFl | kInDsvFl, "Mode: 0=exclusive (dflt) 1=multi",
  4611. 1, "trig", kTrigAvId, 0, 0, kTypeDsvMask | kInDsvFl, "Trigger the unit to select the next available channel.",
  4612. 1, "ch", kChIdxAvId, 0, 0, kUIntDsvFl | kOutDsvFl, "Currently selected channel.",
  4613. chCnt, "dis", baseDisInAvId, 0, 0, kBoolDsvFl | kInDsvFl, "Disable channel gate",
  4614. chCnt, "gate", baseGateOutAvId, 0, 0, kBoolDsvFl | kOutDsvFl, "Active channel gate",
  4615. 0 );
  4616. p->chCnt = chCnt;
  4617. p->baseDisInAvId = baseDisInAvId;
  4618. p->baseGateOutAvId = baseGateOutAvId;
  4619. unsigned i;
  4620. for(i=0; i<chCnt; ++i)
  4621. {
  4622. cmDspSetDefaultBool( ctx, &p->inst, baseDisInAvId+i, false, false );
  4623. cmDspSetDefaultBool( ctx, &p->inst, baseGateOutAvId+i, false, false );
  4624. }
  4625. cmDspSetDefaultUInt( ctx, &p->inst, kModeAvId, 0, kExclusiveModeAvId );
  4626. cmDspSetDefaultUInt( ctx, &p->inst, kChIdxAvId, 0, cmInvalidIdx );
  4627. return &p->inst;
  4628. }
  4629. cmDspRC_t _cmDspAvailCh_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4630. {
  4631. return kOkDspRC;
  4632. }
  4633. cmDspRC_t _cmDspAvailCh_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4634. {
  4635. return cmDspApplyAllDefaults(ctx,inst);
  4636. }
  4637. cmDspRC_t _cmDspAvailCh_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4638. {
  4639. cmDspRC_t rc = kOkDspRC;
  4640. cmDspAvailCh_t* p = (cmDspAvailCh_t*)inst;
  4641. bool exclModeFl = cmDspUInt(inst, kModeAvId ) == kExclusiveModeAvId;
  4642. // if this is a trigger
  4643. if( evt->dstVarId == kTrigAvId )
  4644. {
  4645. unsigned i;
  4646. bool fl = true;
  4647. for(i=0; i<p->chCnt; ++i)
  4648. {
  4649. // the actual channel's active state is held in the 'dis' variable.
  4650. bool activeFl = cmDspBool(inst,p->baseDisInAvId+i);
  4651. // if ch[i] is the first avail inactive channel
  4652. if( fl && !activeFl )
  4653. {
  4654. cmDspSetUInt(ctx,inst,kChIdxAvId,i);
  4655. cmDspSetBool(ctx, inst, p->baseDisInAvId + i, true);
  4656. cmDspSetBool(ctx, inst, p->baseGateOutAvId + i, true);
  4657. fl = false;
  4658. }
  4659. // if ch[i] is active - then request that it shutdown
  4660. if( activeFl && exclModeFl)
  4661. cmDspSetBool(ctx, inst, p->baseGateOutAvId + i, false);
  4662. }
  4663. return rc;
  4664. }
  4665. // if this is an incoming disable message.
  4666. if( p->baseDisInAvId <= evt->dstVarId && evt->dstVarId < p->baseDisInAvId+p->chCnt && cmDsvGetBool(evt->valuePtr) == false)
  4667. {
  4668. cmDspSetEvent(ctx,inst,evt);
  4669. if( !exclModeFl )
  4670. cmDspSetBool(ctx, inst, p->baseGateOutAvId + (evt->dstVarId - p->baseDisInAvId), false);
  4671. }
  4672. return rc;
  4673. }
  4674. struct cmDspClass_str* cmAvailChClassCons( cmDspCtx_t* ctx )
  4675. {
  4676. cmDspClassSetup(&_cmAvailCh_DC,ctx,"AvailCh",
  4677. NULL,
  4678. _cmDspAvailCh_Alloc,
  4679. _cmDspAvailCh_Free,
  4680. _cmDspAvailCh_Reset,
  4681. NULL,
  4682. _cmDspAvailCh_Recv,
  4683. NULL,NULL,
  4684. "Enable the next availabled channel");
  4685. return &_cmAvailCh_DC;
  4686. }
  4687. //==========================================================================================================================================
  4688. enum
  4689. {
  4690. kGroupSymPrId,
  4691. kLabelPrId,
  4692. kCmdPrId,
  4693. kDonePrId,
  4694. kListPrId,
  4695. kSelPrId
  4696. };
  4697. cmDspClass_t _cmPreset_DC;
  4698. typedef struct
  4699. {
  4700. cmDspInst_t inst;
  4701. unsigned storeCmdSymId;
  4702. unsigned recallCmdSymId;
  4703. unsigned doneSymId;
  4704. cmJsonH_t jsH;
  4705. cmJsonNode_t* np;
  4706. } cmDspPreset_t;
  4707. cmDspRC_t _cmDspPresetUpdateList( cmDspCtx_t* ctx, cmDspPreset_t* p, unsigned groupSymId )
  4708. {
  4709. cmDspRC_t rc = kOkDspRC;
  4710. // initialize the JSON tree
  4711. if( cmJsonInitialize(&p->jsH,ctx->cmCtx) != kOkJsRC )
  4712. {
  4713. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"JSON preset list handle initialization failed.");
  4714. goto errLabel;
  4715. }
  4716. // create the JSON tree root container
  4717. if( cmJsonCreateObject(p->jsH,NULL) == NULL )
  4718. {
  4719. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"JSON preset list root object create failed.");
  4720. goto errLabel;
  4721. }
  4722. // if a valid preset group symbol was given
  4723. if( groupSymId != cmInvalidId )
  4724. {
  4725. // get the JSON list containing the preset labels and symId's for this preset group
  4726. if( cmDspSysPresetPresetJsonList(ctx->dspH, groupSymId, &p->jsH ) != kOkDspRC )
  4727. {
  4728. rc = cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Request for a preset list failed.");
  4729. goto errLabel;
  4730. }
  4731. // get a pointer to the JSON 'presetArray' array node
  4732. if(( p->np = cmJsonFindValue(p->jsH,"presetArray",NULL,kArrayTId)) == NULL )
  4733. {
  4734. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset list is empty or synatax is not recognized.");
  4735. goto errLabel;
  4736. }
  4737. // set the JSON list
  4738. if((rc = cmDspSetJson(ctx,&p->inst,kListPrId,p->np)) != kOkDspRC )
  4739. {
  4740. rc = cmDspInstErr(ctx,&p->inst,rc,"Preset list set failed.");
  4741. goto errLabel;
  4742. }
  4743. }
  4744. errLabel:
  4745. return rc;
  4746. }
  4747. cmDspRC_t _cmDspPresetDoRecall( cmDspCtx_t* ctx, cmDspPreset_t* p, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  4748. {
  4749. cmDspRC_t rc;
  4750. // recall the preset
  4751. if(( rc = cmDspSysPresetRecall(ctx->dspH, groupLabel, presetLabel )) != kOkDspRC )
  4752. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Preset recall failed for group:'%s' preset:'%s'.",cmStringNullGuard(groupLabel),cmStringNullGuard(presetLabel));
  4753. // send out a notification that a new preset has been loaded
  4754. return cmDspSetSymbol(ctx,&p->inst,kDonePrId,p->doneSymId);
  4755. }
  4756. // selIdx is base 1, not base 0, because it references the JSON tree rows where the
  4757. // first row contains the titles.
  4758. cmDspRC_t _cmDspPresetListSelectRecall( cmDspCtx_t* ctx, cmDspPreset_t* p, unsigned selIdx )
  4759. {
  4760. cmDspRC_t rc = kOkDspRC;
  4761. const cmChar_t* presetLabel;
  4762. const cmChar_t* groupLabel;
  4763. unsigned groupSymId;
  4764. unsigned presetSymId;
  4765. const cmJsonNode_t* rnp;
  4766. // validate the JSON tree
  4767. if( cmJsonIsValid(p->jsH) == false || p->np == NULL )
  4768. {
  4769. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. The preset JSON tree does not exist.");
  4770. goto errLabel;
  4771. }
  4772. // validate the group id
  4773. if( (groupSymId = cmDspSymbol(&p->inst,kGroupSymPrId)) == cmInvalidId )
  4774. {
  4775. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Preset recall failed. The preset group symbol has not been set.");
  4776. goto errLabel;
  4777. }
  4778. // validate the selection index
  4779. if( selIdx >= cmJsonChildCount(p->np) )
  4780. {
  4781. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Preset recall failed. The preset index: %i is out of range 0-%i", selIdx, cmJsonChildCount(p->np));
  4782. goto errLabel;
  4783. }
  4784. // get the preset element
  4785. if(( rnp = cmJsonArrayElementC(p->np, selIdx )) == NULL )
  4786. {
  4787. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. Unable to retrieve preset JSON element.");
  4788. goto errLabel;
  4789. }
  4790. // verify the JSON syntax
  4791. assert( rnp->typeId==kArrayTId && cmJsonChildCount(rnp)==2 && cmJsonArrayElementC(rnp,1)->typeId == kIntTId );
  4792. // get the preset symbol id
  4793. if( cmJsonUIntValue( cmJsonArrayElementC(rnp,1), &presetSymId ) != kOkJsRC )
  4794. {
  4795. rc = cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"Preset recall failed. Unable to retrieve preset symbol id.");
  4796. goto errLabel;
  4797. }
  4798. // convert symbols to strings
  4799. groupLabel = cmSymTblLabel(ctx->stH,groupSymId);
  4800. presetLabel = cmSymTblLabel(ctx->stH,presetSymId);
  4801. rc = _cmDspPresetDoRecall(ctx,p,groupLabel,presetLabel);
  4802. errLabel:
  4803. return rc;
  4804. }
  4805. cmDspInst_t* _cmDspPreset_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4806. {
  4807. cmDspPreset_t* p = cmDspInstAllocV(cmDspPreset_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4808. 1, "sym", kGroupSymPrId, 0, 0, kInDsvFl | kSymDsvFl | kReqArgDsvFl, "Preset group symbol.",
  4809. 1, "label", kLabelPrId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Preset label",
  4810. 1, "cmd", kCmdPrId, 0, 0, kInDsvFl | kSymDsvFl, "Command input",
  4811. 1, "done", kDonePrId, 0, 0, kOutDsvFl | kSymDsvFl, "Send 'done' symbol after preset recall.",
  4812. 1, "list", kListPrId, 0, 0, kInDsvFl | kJsonDsvFl, "Preset list as a JSON array.",
  4813. 1, "sel", kSelPrId, 0, 0, kInDsvFl | kUIntDsvFl, "Preset index selection index",
  4814. 0 );
  4815. p->jsH = cmJsonNullHandle;
  4816. p->np = NULL;
  4817. p->storeCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"store");
  4818. p->recallCmdSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"recall");
  4819. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  4820. cmDspSetDefaultBool( ctx, &p->inst, kDonePrId,false,false);
  4821. cmDspSetDefaultSymbol( ctx, &p->inst, kGroupSymPrId, cmInvalidId);
  4822. cmDspSetDefaultStrcz( ctx, &p->inst, kLabelPrId, NULL,"");
  4823. unsigned height = 5;
  4824. cmDspUiMsgListCreate(ctx, &p->inst, height, kListPrId, kSelPrId );
  4825. return &p->inst;
  4826. }
  4827. cmDspRC_t _cmDspPreset_Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4828. {
  4829. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4830. cmJsonFinalize(&p->jsH);
  4831. return kOkDspRC;
  4832. }
  4833. cmDspRC_t _cmDspPreset_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4834. {
  4835. cmDspRC_t rc;
  4836. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4837. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4838. {
  4839. _cmDspPresetUpdateList(ctx, p, cmDspSymbol(inst,kGroupSymPrId) );
  4840. }
  4841. return rc;
  4842. }
  4843. cmDspRC_t _cmDspPreset_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4844. {
  4845. cmDspRC_t rc = kOkDspRC;
  4846. cmDspPreset_t* p = (cmDspPreset_t*)inst;
  4847. switch( evt->dstVarId )
  4848. {
  4849. case kListPrId:
  4850. return rc; // we don't yet handle lists arriving by the input port
  4851. case kSelPrId:
  4852. {
  4853. // sel idx is base 1 because the first row in the msg list contains the titles
  4854. unsigned selIdx = cmDsvGetUInt(evt->valuePtr);
  4855. if((rc = _cmDspPresetListSelectRecall(ctx,p,selIdx)) != kOkDspRC)
  4856. return rc;
  4857. }
  4858. break;
  4859. }
  4860. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  4861. return rc;
  4862. // if this is a store or recall command
  4863. if( evt->dstVarId == kCmdPrId )
  4864. {
  4865. unsigned cmdSymId = cmDspSymbol(inst,kCmdPrId);
  4866. if( cmdSymId == p->storeCmdSymId || cmdSymId==p->recallCmdSymId )
  4867. {
  4868. unsigned groupSymId;
  4869. const cmChar_t* groupLabel;
  4870. const cmChar_t* presetLabel;
  4871. // get the group symbol
  4872. if((groupSymId = cmDspSymbol(inst,kGroupSymPrId)) == cmInvalidId )
  4873. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset group symbol id is not set.");
  4874. // get the group label
  4875. if((groupLabel = cmSymTblLabel(ctx->stH,groupSymId)) == NULL )
  4876. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset group label was not found.");
  4877. // get the preset label
  4878. if(( presetLabel = cmDspStrcz(inst,kLabelPrId)) == NULL || strlen(presetLabel)==0 )
  4879. return cmDspInstErr(ctx,inst,kVarNotValidDspRC,"The preset label was not set.");
  4880. // if this is a store command
  4881. if( cmdSymId == p->storeCmdSymId )
  4882. {
  4883. // create a new preset
  4884. if((rc = cmDspSysPresetCreate(ctx->dspH,groupLabel, presetLabel)) != kOkDspRC )
  4885. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Preset create failed for group:'%s' preset:'%s'.",cmStringNullGuard(groupLabel),cmStringNullGuard(presetLabel));
  4886. // update the list with the new preset
  4887. rc = _cmDspPresetUpdateList(ctx, p, groupSymId );
  4888. }
  4889. else // otherwise this must be a recall command
  4890. {
  4891. rc = _cmDspPresetDoRecall(ctx,p,groupLabel, presetLabel);
  4892. }
  4893. }
  4894. }
  4895. return rc;
  4896. }
  4897. struct cmDspClass_str* cmPresetClassCons( cmDspCtx_t* ctx )
  4898. {
  4899. cmDspClassSetup(&_cmPreset_DC,ctx,"Preset",
  4900. NULL,
  4901. _cmDspPreset_Alloc,
  4902. _cmDspPreset_Free,
  4903. _cmDspPreset_Reset,
  4904. NULL,
  4905. _cmDspPreset_Recv,
  4906. NULL,NULL,
  4907. "Preset Manager");
  4908. return &_cmPreset_DC;
  4909. }
  4910. //==========================================================================================================================================
  4911. enum
  4912. {
  4913. kAttrBcId,
  4914. kMsgBcId
  4915. };
  4916. cmDspClass_t _cmBcastSym_DC;
  4917. typedef struct
  4918. {
  4919. cmDspInst_t inst;
  4920. unsigned onSymId;
  4921. unsigned offSymId;
  4922. } cmDspBcastSym_t;
  4923. cmDspInst_t* _cmDspBcastSym_Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4924. {
  4925. cmDspBcastSym_t* p = cmDspInstAllocV(cmDspBcastSym_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4926. 1, "attr", kAttrBcId, 0, 0, kSymDsvFl | kInDsvFl | kOptArgDsvFl, "Instance which have this attribute symbol will receive the message.",
  4927. 1, "msg", kMsgBcId, 0, 0, kTypeDsvMask | kInDsvFl, "Msg to broadcast.",
  4928. 0 );
  4929. cmDspSetDefaultSymbol( ctx, &p->inst, kAttrBcId, cmInvalidId );
  4930. return &p->inst;
  4931. }
  4932. cmDspRC_t _cmDspBcastSym_Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4933. {
  4934. cmDspRC_t rc = kOkDspRC;
  4935. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  4936. {
  4937. }
  4938. return rc;
  4939. }
  4940. cmDspRC_t _cmDspBcastSym_Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4941. {
  4942. cmDspRC_t rc = kOkDspRC;
  4943. if( evt->dstVarId == kAttrBcId )
  4944. return cmDspSetEvent(ctx,inst,evt);
  4945. if( evt->dstVarId == kMsgBcId )
  4946. {
  4947. unsigned attrSymId = cmDspSymbol(inst,kAttrBcId);
  4948. if( cmDsvIsSymbol(evt->valuePtr) )
  4949. {
  4950. printf("bcast: %i %s\n",attrSymId,cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr)));
  4951. }
  4952. return cmDspSysBroadcastValue(ctx->dspH, attrSymId, evt->valuePtr );
  4953. }
  4954. return rc;
  4955. }
  4956. struct cmDspClass_str* cmBcastSymClassCons( cmDspCtx_t* ctx )
  4957. {
  4958. cmDspClassSetup(&_cmBcastSym_DC,ctx,"BcastSym",
  4959. NULL,
  4960. _cmDspBcastSym_Alloc,
  4961. NULL,
  4962. _cmDspBcastSym_Reset,
  4963. NULL,
  4964. _cmDspBcastSym_Recv,
  4965. NULL,NULL,
  4966. "Set one input high and all others low.");
  4967. return &_cmBcastSym_DC;
  4968. }
  4969. //==========================================================================================================================================
  4970. enum
  4971. {
  4972. kRsrcSlId,
  4973. kCmdSlId,
  4974. kTrigSlId,
  4975. kOutSlId,
  4976. };
  4977. cmDspClass_t _cmSegLineDC;
  4978. typedef struct
  4979. {
  4980. cmDspInst_t inst;
  4981. double* x; // x[n]
  4982. unsigned n;
  4983. unsigned i; // current segment
  4984. unsigned m; // cur cnt
  4985. } cmDspSegLine_t;
  4986. cmDspInst_t* _cmDspSegLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4987. {
  4988. cmDspSegLine_t* p = cmDspInstAllocV(cmDspSegLine_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  4989. 1, "rsrc", kRsrcSlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Name of resource array.",
  4990. 1, "cmd", kCmdSlId, 0, 0, kInDsvFl | kSymDsvFl, "Command",
  4991. 1, "trig", kTrigSlId, 0, 0, kInDsvFl | kTypeDsvMask, "Trigger",
  4992. 1, "out", kOutSlId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output",
  4993. 0 );
  4994. if( p == NULL )
  4995. return NULL;
  4996. // The array is expected to contain interleaved values:
  4997. // cnt_0, val_0, cnt_1, val_1 .... cnt_n val_n
  4998. // The 'cnt_x' values give the count of trigger values upon which the output will be 'val_x'.
  4999. if( cmDspRsrcDblArray(ctx->dspH,&p->n,&p->x,cmDspDefaultStrcz(&p->inst,kRsrcSlId),NULL) != kOkDspRC )
  5000. {
  5001. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'SegLine' constructor resource array could not be read.");
  5002. return NULL;
  5003. }
  5004. cmDspSetDefaultDouble( ctx, &p->inst, kOutSlId, 0.0, p->n >= 2 ? p->x[1] : 0.0 );
  5005. p->i = 0;
  5006. p->m = 0;
  5007. return &p->inst;
  5008. }
  5009. cmDspRC_t _cmDspSegLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  5010. {
  5011. cmDspSegLine_t* p = (cmDspSegLine_t*)inst;
  5012. p->i = 0;
  5013. p->m = 0;
  5014. return cmDspApplyAllDefaults(ctx,inst);
  5015. }
  5016. cmDspRC_t _cmDspSegLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  5017. {
  5018. cmDspRC_t rc = kOkDspRC;
  5019. cmDspSegLine_t* p = (cmDspSegLine_t*)inst;
  5020. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  5021. {
  5022. switch( evt->dstVarId )
  5023. {
  5024. case kTrigSlId:
  5025. {
  5026. double val = cmDspDouble(inst,kOutSlId);
  5027. if( p->i < p->n )
  5028. {
  5029. if( p->m >= p->x[p->i] )
  5030. p->i += 2;
  5031. if( p->i < p->n )
  5032. {
  5033. double x0 = p->x[p->i-2];
  5034. double y0 = p->x[p->i-1];
  5035. double x1 = p->x[p->i+0];
  5036. double y1 = p->x[p->i+1];
  5037. double dx = x1 - x0;
  5038. double dy = y1 - y0;
  5039. val = y0 + (p->m - x0) * dy / dx;
  5040. printf("i:%i m=%i x0:%f y0:%f x1:%f y1:%f : %f\n",p->i,p->m,x0,y0,x1,y1,val);
  5041. }
  5042. else
  5043. {
  5044. val = p->x[p->n-1];
  5045. }
  5046. ++p->m;
  5047. }
  5048. cmDspSetDouble(ctx,inst,kOutSlId,val);
  5049. }
  5050. break;
  5051. case kCmdSlId:
  5052. p->i = 0;
  5053. p->m = 0;
  5054. break;
  5055. }
  5056. }
  5057. return rc;
  5058. }
  5059. struct cmDspClass_str* cmSegLineClassCons( cmDspCtx_t* ctx )
  5060. {
  5061. cmDspClassSetup(&_cmSegLineDC,ctx,"SegLine",
  5062. NULL,
  5063. _cmDspSegLineAlloc,
  5064. NULL,
  5065. _cmDspSegLineReset,
  5066. NULL,
  5067. _cmDspSegLineRecv,
  5068. NULL,NULL,
  5069. "SegLine");
  5070. return &_cmSegLineDC;
  5071. }