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

cmDspFx.c 186KB

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