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.

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