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

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