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 186KB

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