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

cmDspFx.c 193KB

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