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

cmDspFx.c 202KB

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