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

cmDspFx.c 193KB

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