libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmDspFx.c 201KB

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