libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmDspFx.c 199KB

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