libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmDspKr.c 126KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222
  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 "cmFile.h"
  12. #include "cmSymTbl.h"
  13. #include "cmJson.h"
  14. #include "cmText.h"
  15. #include "cmPrefs.h"
  16. #include "cmDspValue.h"
  17. #include "cmMsgProtocol.h"
  18. #include "cmThread.h"
  19. #include "cmUdpPort.h"
  20. #include "cmUdpNet.h"
  21. //( { file_desc:"'snap' audio effects performance analysis units." kw:[snap]}
  22. #include "cmTime.h"
  23. #include "cmAudioSys.h"
  24. #include "cmDspCtx.h"
  25. #include "cmDspClass.h"
  26. #include "cmDspStore.h"
  27. #include "cmDspUi.h"
  28. #include "cmDspSys.h"
  29. #include "cmMath.h"
  30. #include "cmAudioFile.h"
  31. #include "cmFileSys.h"
  32. #include "cmProcObj.h"
  33. #include "cmProcTemplateMain.h"
  34. #include "cmProc.h"
  35. #include "cmMidi.h"
  36. #include "cmProc2.h"
  37. #include "cmVectOpsTemplateMain.h"
  38. #include "cmAudioFile.h"
  39. #include "cmMidiFile.h"
  40. #include "cmTimeLine.h"
  41. #include "cmScore.h"
  42. #include "cmProc4.h"
  43. #include "cmProc5.h"
  44. #include "cmSyncRecd.h"
  45. #include "cmTakeSeqBldr.h"
  46. //------------------------------------------------------------------------------------------------------------
  47. //)
  48. //( { label:cmDspKr file_desc:"Spectral non-linear distortion effect." kw:[sunit] }
  49. enum
  50. {
  51. kWndSmpCntKrId,
  52. kHopFactKrId,
  53. kModeKrId,
  54. kThreshKrId,
  55. kLwrSlopeKrId,
  56. kUprSlopeKrId,
  57. kOffsetKrId,
  58. kInvertKrId,
  59. kBypassKrId,
  60. kWetKrId,
  61. kAudioInKrId,
  62. kAudioOutKrId
  63. };
  64. typedef struct
  65. {
  66. cmDspInst_t inst;
  67. cmCtx* ctx;
  68. cmSpecDist_t* sdp;
  69. } cmDspKr_t;
  70. cmDspClass_t _cmKrDC;
  71. cmDspInst_t* _cmDspKrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  72. {
  73. cmDspVarArg_t args[] =
  74. {
  75. { "wndn", kWndSmpCntKrId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  76. { "hopf", kHopFactKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  77. { "mode", kModeKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Mode 0=bypass 1=basic 2=spec cnt 3=amp env" },
  78. { "thrh", kThreshKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  79. { "lwrs", kLwrSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  80. { "uprs", kUprSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  81. { "offs", kOffsetKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset"},
  82. { "invt", kInvertKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Invert"},
  83. { "bypass", kBypassKrId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  84. { "wet", kWetKrId, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
  85. { "in", kAudioInKrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  86. { "out", kAudioOutKrId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  87. { NULL, 0, 0, 0, 0 }
  88. };
  89. cmDspKr_t* p = cmDspInstAlloc(cmDspKr_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  90. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKrId);
  91. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  92. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKrId, defWndSmpCnt, wndSmpCnt );
  93. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKrId, 0, 4 );
  94. cmDspSetDefaultUInt( ctx,&p->inst, kModeKrId, 0, kBasicModeSdId );
  95. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKrId, 0, 60.0 );
  96. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKrId, 0, 2.0 );
  97. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKrId, 0, 0.0 );
  98. cmDspSetDefaultDouble( ctx,&p->inst, kOffsetKrId, 0, 30.0);
  99. cmDspSetDefaultUInt( ctx,&p->inst, kInvertKrId, 0, 0 );
  100. cmDspSetDefaultUInt( ctx,&p->inst, kBypassKrId, 0, 0 );
  101. cmDspSetDefaultSample( ctx,&p->inst, kWetKrId, 0, 1.0);
  102. //_cmDspKrCmInit(ctx,p); // initialize the cm library
  103. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  104. return &p->inst;
  105. }
  106. cmDspRC_t _cmDspKrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  107. {
  108. cmDspRC_t rc = kOkDspRC;
  109. cmDspKr_t* p = (cmDspKr_t*)inst;
  110. cmSpecDistFree(&p->sdp);
  111. cmCtxFree(&p->ctx);
  112. //_cmDspKrCmFinal(ctx,p); // finalize the cm library
  113. return rc;
  114. }
  115. cmDspRC_t _cmDspKrSetup(cmDspCtx_t* ctx, cmDspKr_t* p )
  116. {
  117. cmDspRC_t rc = kOkDspRC;
  118. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKrId);
  119. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKrId);
  120. unsigned olaWndTypeId =kHannWndId;
  121. cmSpecDistFree(&p->sdp);
  122. p->sdp = cmSpecDistAlloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  123. assert(p->sdp != NULL );
  124. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKrId)) != kOkDspRC )
  125. return rc;
  126. return rc;
  127. }
  128. cmDspRC_t _cmDspKrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  129. {
  130. cmDspKr_t* p = (cmDspKr_t*)inst;
  131. cmDspRC_t rc;
  132. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  133. return rc;
  134. return _cmDspKrSetup(ctx,p);
  135. }
  136. cmDspRC_t _cmDspKrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  137. {
  138. cmDspKr_t* p = (cmDspKr_t*)inst;
  139. cmDspRC_t rc = kOkDspRC;
  140. unsigned iChIdx = 0;
  141. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKrId,iChIdx);
  142. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKrId);
  143. // if no connected
  144. if( iSmpCnt == 0 )
  145. return rc;
  146. unsigned oChIdx = 0;
  147. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKrId,oChIdx);
  148. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKrId);
  149. const cmSample_t* sp;
  150. cmSample_t wet = cmDspSample(inst,kWetKrId);
  151. cmSpecDistExec(p->sdp,ip,iSmpCnt);
  152. if((sp = cmSpecDistOut(p->sdp)) != NULL )
  153. {
  154. cmVOS_MultVVS(op,oSmpCnt,sp,wet);
  155. }
  156. if( wet<1.0 )
  157. cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
  158. return rc;
  159. }
  160. cmDspRC_t _cmDspKrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  161. {
  162. cmDspKr_t* p = (cmDspKr_t*)inst;
  163. cmDspRC_t rc = kOkDspRC;
  164. cmDspSetEvent(ctx,inst,evt);
  165. switch( evt->dstVarId )
  166. {
  167. case kWndSmpCntKrId:
  168. case kHopFactKrId:
  169. _cmDspKrSetup(ctx,p);
  170. // THIS IS A HACK
  171. // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
  172. // SHOULD BE ISOLATED IN cmSpecDist() AND THE
  173. // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
  174. // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
  175. // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
  176. // MINIMAL AUDIO INTERUPTION.
  177. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  178. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  179. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  180. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  181. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  182. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  183. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  184. break;
  185. case kModeKrId:
  186. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  187. printf("mode:%i\n",p->sdp->mode);
  188. break;
  189. case kThreshKrId:
  190. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  191. //printf("thr:p:%p sdp:%p %f\n",p,p->sdp,p->sdp->thresh);
  192. break;
  193. case kUprSlopeKrId:
  194. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  195. //printf("upr slope:%f\n",p->sdp->uprSlope);
  196. break;
  197. case kLwrSlopeKrId:
  198. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  199. //printf("upr slope:%f\n",p->sdp->lwrSlope);
  200. break;
  201. case kOffsetKrId:
  202. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  203. break;
  204. case kInvertKrId:
  205. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  206. break;
  207. case kWetKrId:
  208. break;
  209. default:
  210. { assert(0); }
  211. }
  212. return rc;
  213. }
  214. cmDspClass_t* cmKrClassCons( cmDspCtx_t* ctx )
  215. {
  216. cmDspClassSetup(&_cmKrDC,ctx,"Kr",
  217. NULL,
  218. _cmDspKrAlloc,
  219. _cmDspKrFree,
  220. _cmDspKrReset,
  221. _cmDspKrExec,
  222. _cmDspKrRecv,
  223. NULL,NULL,
  224. "Fourier based non-linear transformer.");
  225. return &_cmKrDC;
  226. }
  227. //------------------------------------------------------------------------------------------------------------
  228. //)
  229. //( { label:cmDspKr2 file_desc:"Spectral non-linear distortion effect." kw:[sunit] }
  230. enum
  231. {
  232. kWndSmpCntKr2Id,
  233. kHopFactKr2Id,
  234. kCeilKr2Id,
  235. kExpoKr2Id,
  236. kThreshKr2Id,
  237. kLwrSlopeKr2Id,
  238. kUprSlopeKr2Id,
  239. kMixKr2Id,
  240. kWetKr2Id,
  241. kAudioInKr2Id,
  242. kAudioOutKr2Id
  243. };
  244. typedef struct
  245. {
  246. cmDspInst_t inst;
  247. cmCtx* ctx;
  248. cmSpecDist2_t* sdp;
  249. } cmDspKr2_t;
  250. cmDspClass_t _cmKr2DC;
  251. cmDspInst_t* _cmDspKr2Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  252. {
  253. cmDspVarArg_t args[] =
  254. {
  255. { "wndn", kWndSmpCntKr2Id, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  256. { "hopf", kHopFactKr2Id, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  257. { "ceil", kCeilKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Ceiling" },
  258. { "expo", kExpoKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Exponent" },
  259. { "thrh", kThreshKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  260. { "lwrs", kLwrSlopeKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  261. { "uprs", kUprSlopeKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  262. { "mix", kMixKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Mix"},
  263. { "wet", kWetKr2Id, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
  264. { "in", kAudioInKr2Id, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  265. { "out", kAudioOutKr2Id, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  266. { NULL, 0, 0, 0, 0 }
  267. };
  268. cmDspKr2_t* p = cmDspInstAlloc(cmDspKr2_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  269. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKr2Id);
  270. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  271. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKr2Id, defWndSmpCnt, wndSmpCnt );
  272. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKr2Id, 0, 4 );
  273. cmDspSetDefaultDouble( ctx,&p->inst, kCeilKr2Id, 0, 20.0 );
  274. cmDspSetDefaultDouble( ctx,&p->inst, kExpoKr2Id, 0, 2.0 );
  275. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKr2Id, 0, 60.0 );
  276. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKr2Id, 0, 2.0 );
  277. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKr2Id, 0, 0.0 );
  278. cmDspSetDefaultDouble( ctx,&p->inst, kMixKr2Id, 0, 0.0 );
  279. cmDspSetDefaultSample( ctx,&p->inst, kWetKr2Id, 0, 1.0);
  280. //_cmDspKr2CmInit(ctx,p); // initialize the cm library
  281. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  282. return &p->inst;
  283. }
  284. cmDspRC_t _cmDspKr2Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  285. {
  286. cmDspRC_t rc = kOkDspRC;
  287. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  288. cmSpecDist2Free(&p->sdp);
  289. cmCtxFree(&p->ctx);
  290. //_cmDspKr2CmFinal(ctx,p); // finalize the cm library
  291. return rc;
  292. }
  293. cmDspRC_t _cmDspKr2Setup(cmDspCtx_t* ctx, cmDspKr2_t* p )
  294. {
  295. cmDspRC_t rc = kOkDspRC;
  296. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKr2Id);
  297. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKr2Id);
  298. unsigned olaWndTypeId =kHannWndId;
  299. cmSpecDist2Free(&p->sdp);
  300. p->sdp = cmSpecDist2Alloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  301. assert(p->sdp != NULL );
  302. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKr2Id)) != kOkDspRC )
  303. return rc;
  304. return rc;
  305. }
  306. cmDspRC_t _cmDspKr2Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  307. {
  308. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  309. cmDspRC_t rc;
  310. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  311. return rc;
  312. return _cmDspKr2Setup(ctx,p);
  313. }
  314. cmDspRC_t _cmDspKr2Exec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  315. {
  316. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  317. cmDspRC_t rc = kOkDspRC;
  318. unsigned iChIdx = 0;
  319. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKr2Id,iChIdx);
  320. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKr2Id);
  321. // if no connected
  322. if( iSmpCnt == 0 )
  323. return rc;
  324. unsigned oChIdx = 0;
  325. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKr2Id,oChIdx);
  326. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKr2Id);
  327. const cmSample_t* sp;
  328. cmSample_t wet = cmDspSample(inst,kWetKr2Id);
  329. cmSpecDist2Exec(p->sdp,ip,iSmpCnt);
  330. if((sp = cmSpecDist2Out(p->sdp)) != NULL )
  331. {
  332. cmVOS_MultVVS(op,oSmpCnt,sp,wet);
  333. }
  334. if( wet<1.0 )
  335. cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
  336. return rc;
  337. }
  338. cmDspRC_t _cmDspKr2Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  339. {
  340. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  341. cmDspRC_t rc = kOkDspRC;
  342. cmDspSetEvent(ctx,inst,evt);
  343. switch( evt->dstVarId )
  344. {
  345. case kWndSmpCntKr2Id:
  346. case kHopFactKr2Id:
  347. _cmDspKr2Setup(ctx,p);
  348. // THIS IS A HACK
  349. // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
  350. // SHOULD BE ISOLATED IN cmSpecDist() AND THE
  351. // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
  352. // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
  353. // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
  354. // MINIMAL AUDIO INTERUPTION.
  355. p->sdp->ceiling = cmDspDouble(inst,kCeilKr2Id);
  356. p->sdp->expo = cmDspDouble(inst,kExpoKr2Id);
  357. p->sdp->thresh = cmDspDouble(inst,kThreshKr2Id);
  358. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKr2Id);
  359. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKr2Id);
  360. p->sdp->mix = cmDspDouble(inst,kMixKr2Id);
  361. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  362. break;
  363. case kCeilKr2Id:
  364. p->sdp->ceiling = cmDspDouble(inst,kCeilKr2Id);
  365. break;
  366. case kExpoKr2Id:
  367. p->sdp->expo = cmDspDouble(inst,kExpoKr2Id);
  368. break;
  369. case kThreshKr2Id:
  370. p->sdp->thresh = cmDspDouble(inst,kThreshKr2Id);
  371. break;
  372. case kUprSlopeKr2Id:
  373. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKr2Id);
  374. break;
  375. case kLwrSlopeKr2Id:
  376. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKr2Id);
  377. break;
  378. case kMixKr2Id:
  379. p->sdp->mix = cmDspDouble(inst,kMixKr2Id);
  380. break;
  381. case kWetKr2Id:
  382. break;
  383. default:
  384. { assert(0); }
  385. }
  386. cmSpecDist2Report(p->sdp);
  387. return rc;
  388. }
  389. cmDspClass_t* cmKr2ClassCons( cmDspCtx_t* ctx )
  390. {
  391. cmDspClassSetup(&_cmKr2DC,ctx,"Kr2",
  392. NULL,
  393. _cmDspKr2Alloc,
  394. _cmDspKr2Free,
  395. _cmDspKr2Reset,
  396. _cmDspKr2Exec,
  397. _cmDspKr2Recv,
  398. NULL,NULL,
  399. "Fourier based non-linear transformer two.");
  400. return &_cmKr2DC;
  401. }
  402. //------------------------------------------------------------------------------------------------------------
  403. //)
  404. //( { label:cmDspTimeLine file_desc:"Time line user interface unit." kw:[sunit] }
  405. enum
  406. {
  407. kTlFileTlId,
  408. kPrefixPathTlId,
  409. kSelTlId,
  410. kMeasTlId,
  411. kCursTlId,
  412. kResetTlId,
  413. kAudFnTlId,
  414. kAudLblTlId,
  415. kMidiFnTlId,
  416. kMidiLblTlId,
  417. kBegAudSmpIdxTlId,
  418. kEndAudSmpIdxTlId,
  419. kBegMidiSmpIdxTlId,
  420. kEndMidiSmpIdxTlId,
  421. };
  422. cmDspClass_t _cmTimeLineDC;
  423. typedef struct
  424. {
  425. cmDspInst_t inst;
  426. cmTlH_t tlH;
  427. unsigned afIdx;
  428. } cmDspTimeLine_t;
  429. cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  430. {
  431. cmDspVarArg_t args[] =
  432. {
  433. { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
  434. { "path", kPrefixPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line data file prefix path" },
  435. { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
  436. { "meas", kMeasTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Select a bar marker and generate a 'sel' output."},
  437. { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
  438. { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
  439. { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
  440. { "albl", kAudLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select Audio file time line label."},
  441. { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
  442. { "mlbl", kMidiLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select MIDI file time line label."},
  443. { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
  444. { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
  445. { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
  446. { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
  447. { NULL, 0, 0, 0, 0 }
  448. };
  449. cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  450. cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
  451. cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
  452. cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
  453. cmDspSetDefaultStrcz(ctx, &p->inst, kAudLblTlId, NULL, "");
  454. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
  455. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiLblTlId, NULL, "");
  456. cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
  457. cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
  458. cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
  459. cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
  460. // create the UI control
  461. cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kPrefixPathTlId,kSelTlId,kMeasTlId,kCursTlId);
  462. p->tlH = cmTimeLineNullHandle;
  463. return &p->inst;
  464. }
  465. cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  466. {
  467. cmDspRC_t rc = kOkDspRC;
  468. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  469. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  470. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
  471. return rc;
  472. }
  473. cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  474. {
  475. cmDspRC_t rc = kOkDspRC;
  476. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  477. cmDspApplyAllDefaults(ctx,inst);
  478. const cmChar_t* tlFn;
  479. const cmChar_t* tlPrePath = cmDspStrcz(inst,kPrefixPathTlId);
  480. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  481. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn, tlPrePath ) != kOkTlRC )
  482. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  483. return rc;
  484. }
  485. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  486. {
  487. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  488. switch( evt->dstVarId )
  489. {
  490. case kPrefixPathTlId:
  491. cmDspSetEvent(ctx,inst,evt);
  492. break;
  493. case kCursTlId:
  494. cmDspSetEvent(ctx,inst,evt);
  495. break;
  496. case kResetTlId:
  497. case kSelTlId:
  498. {
  499. unsigned markerId;
  500. cmDspSetEvent(ctx,inst,evt);
  501. // get the id of the selected marker
  502. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  503. {
  504. // get the marker object
  505. cmTlObj_t* op;
  506. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  507. {
  508. assert(op->typeId == kMarkerTlId || op->typeId == kMidiEvtTlId );
  509. p->afIdx = op->begSmpIdx;
  510. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  511. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  512. // locate the audio file assoc'd with the marker
  513. cmTlAudioFile_t* afp;
  514. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  515. {
  516. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  517. cmDspSetStrcz(ctx, inst, kAudLblTlId, afp->obj.name );
  518. }
  519. // locate the midi file assoc'd with the marker
  520. cmTlMidiFile_t* mfp;
  521. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  522. {
  523. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  524. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  525. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  526. cmDspSetStrcz(ctx, inst, kMidiLblTlId,mfp->obj.name );
  527. }
  528. }
  529. }
  530. }
  531. break;
  532. case kMeasTlId:
  533. cmDspSetEvent(ctx,inst,evt);
  534. break;
  535. default:
  536. {assert(0);}
  537. }
  538. return kOkDspRC;
  539. }
  540. cmDspClass_t* cmTimeLineClassCons( cmDspCtx_t* ctx )
  541. {
  542. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  543. NULL,
  544. _cmDspTimeLineAlloc,
  545. _cmDspTimeLineFree,
  546. _cmDspTimeLineReset,
  547. NULL,
  548. _cmDspTimeLineRecv,
  549. NULL,NULL,
  550. "Time Line control.");
  551. return &_cmTimeLineDC;
  552. }
  553. //------------------------------------------------------------------------------------------------------------
  554. //)
  555. //( { label:cmDspScore file_desc:"Musical score user interface unit." kw:[sunit] }
  556. enum
  557. {
  558. kFnScId,
  559. kSelScId,
  560. kSendScId,
  561. kStatusScId,
  562. kD0ScId,
  563. kD1ScId,
  564. kSmpIdxScId,
  565. kLocIdxScId,
  566. kCmdScId,
  567. kEvtIdxScId,
  568. kDynScId,
  569. kValTypeScId,
  570. kValueScId,
  571. kMeasScId,
  572. };
  573. cmDspClass_t _cmScoreDC;
  574. typedef struct
  575. {
  576. cmDspInst_t inst;
  577. cmScH_t scH;
  578. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  579. unsigned printSymId;
  580. } cmDspScore_t;
  581. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  582. {
  583. cmDspVarArg_t args[] =
  584. {
  585. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  586. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  587. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  588. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  589. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  590. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  591. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  592. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  593. { "cmd", kCmdScId, 0, 0, kInDsvFl | kSymDsvFl, "cmd: dump "},
  594. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  595. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  596. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  597. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  598. { "meas", kMeasScId, 0, 0, kInDsvFl | kUIntDsvFl, "Trigger this measures location to emit from 'sel'."},
  599. { NULL, 0, 0, 0, 0 }
  600. };
  601. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  602. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  603. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  604. // create the UI control
  605. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId,kMeasScId);
  606. p->scH = cmScNullHandle;
  607. return &p->inst;
  608. }
  609. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  610. {
  611. cmDspRC_t rc = kOkDspRC;
  612. cmDspScore_t* p = (cmDspScore_t*)inst;
  613. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  614. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  615. return rc;
  616. }
  617. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  618. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  619. {
  620. cmDspInst_t* inst = (cmDspInst_t*)arg;
  621. cmDspScore_t* p = (cmDspScore_t*)inst;
  622. cmScMsg_t m;
  623. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  624. {
  625. switch( m.typeId )
  626. {
  627. case kDynMsgScId:
  628. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  629. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  630. break;
  631. case kVarMsgScId:
  632. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  633. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  634. break;
  635. default:
  636. { assert(0); }
  637. }
  638. }
  639. }
  640. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  641. {
  642. cmDspRC_t rc = kOkDspRC;
  643. cmDspScore_t* p = (cmDspScore_t*)inst;
  644. const cmChar_t* tlFn = NULL;
  645. unsigned* dynRefArray = NULL;
  646. unsigned dynRefCnt = 0;
  647. cmDspApplyAllDefaults(ctx,inst);
  648. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  649. {
  650. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  651. goto errLabel;
  652. }
  653. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  654. {
  655. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, ctx->stH ) != kOkTlRC )
  656. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  657. //else
  658. // cmScorePrintLoc(p->scH);
  659. }
  660. errLabel:
  661. return rc;
  662. }
  663. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  664. {
  665. cmDspScore_t* p = (cmDspScore_t*)inst;
  666. if( evt->dstVarId == kSendScId )
  667. {
  668. unsigned selIdx;
  669. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  670. {
  671. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  672. cmScoreClearPerfInfo(p->scH);
  673. }
  674. return kOkDspRC;
  675. }
  676. cmDspSetEvent(ctx,inst,evt);
  677. switch( evt->dstVarId )
  678. {
  679. case kSelScId:
  680. cmScoreClearPerfInfo(p->scH);
  681. break;
  682. case kStatusScId:
  683. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  684. break;
  685. case kLocIdxScId:
  686. {
  687. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  688. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  689. // this call may result in callbacks to _cmDspScoreCb()
  690. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  691. }
  692. break;
  693. case kCmdScId:
  694. if( cmDspSymbol(inst,kCmdScId) == p->printSymId )
  695. cmScorePrintLoc(p->scH);
  696. break;
  697. case kMeasScId:
  698. break;
  699. }
  700. return kOkDspRC;
  701. }
  702. cmDspClass_t* cmScoreClassCons( cmDspCtx_t* ctx )
  703. {
  704. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  705. NULL,
  706. _cmDspScoreAlloc,
  707. _cmDspScoreFree,
  708. _cmDspScoreReset,
  709. NULL,
  710. _cmDspScoreRecv,
  711. NULL,NULL,
  712. "Score control.");
  713. return &_cmScoreDC;
  714. }
  715. //------------------------------------------------------------------------------------------------------------
  716. //)
  717. //( { label:cmDspMidiFilePlay file_desc:"MIDI file player." kw:[sunit] }
  718. enum
  719. {
  720. kFnMfId,
  721. kSelMfId,
  722. kBsiMfId,
  723. kEsiMfId,
  724. kStatusMfId,
  725. kD0MfId,
  726. kD1MfId,
  727. kSmpIdxMfId,
  728. kIdMfId
  729. };
  730. cmDspClass_t _cmMidiFilePlayDC;
  731. typedef struct
  732. {
  733. cmDspInst_t inst;
  734. cmMidiFileH_t mfH;
  735. unsigned curMsgIdx; // current midi file msg index
  736. int csi; // current sample index
  737. int bsi; // starting sample index
  738. int esi; // ending sample index
  739. unsigned startSymId;
  740. unsigned stopSymId;
  741. unsigned contSymId;
  742. bool errFl;
  743. } cmDspMidiFilePlay_t;
  744. // 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  745. // These indexes are relative to the start of the file.
  746. // When the player recieves a 'start' msg it sets the current sample index
  747. // 'si' to 'bsi' and begins scanning for the next note to play.
  748. // On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  749. // si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  750. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  751. {
  752. cmDspVarArg_t args[] =
  753. {
  754. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  755. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  756. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  757. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  758. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  759. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  760. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  761. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  762. { "id", kIdMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI file msg unique id."},
  763. { NULL, 0, 0, 0, 0 }
  764. };
  765. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  766. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  767. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  768. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  769. p->mfH = cmMidiFileNullHandle;
  770. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  771. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  772. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  773. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  774. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  775. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  776. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  777. return &p->inst;
  778. }
  779. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  780. {
  781. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  782. if( cmMidiFileClose(&p->mfH) )
  783. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  784. return kOkDspRC;
  785. }
  786. // return the index of the msg following smpIdx
  787. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  788. {
  789. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  790. if( cmMidiFileIsValid(p->mfH) == false )
  791. {
  792. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  793. return cmInvalidIdx;
  794. }
  795. unsigned i;
  796. unsigned n = cmMidiFileMsgCount(p->mfH);
  797. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  798. for(i=0; i<n; ++i)
  799. if( (a[i]->amicro * cmDspSampleRate(ctx) / 1000000.0) >= smpIdx )
  800. break;
  801. return i==n ? cmInvalidIdx : i;
  802. }
  803. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  804. {
  805. cmDspRC_t rc = kOkDspRC;
  806. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  807. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  808. p->errFl = false;
  809. if( fn==NULL || strlen(fn)==0 )
  810. return rc;
  811. if( cmMidiFileOpen( ctx->cmCtx, &p->mfH, fn ) != kOkFileRC )
  812. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  813. else
  814. {
  815. p->curMsgIdx = 0;
  816. p->bsi = cmDspInt(inst,kBsiMfId);
  817. p->esi = cmDspInt(inst,kEsiMfId);
  818. p->csi = 0;
  819. // force the first msg to occurr one quarter note into the file
  820. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  821. // convert midi msg times to absolute time in samples
  822. //cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  823. }
  824. return rc;
  825. }
  826. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  827. {
  828. cmDspApplyAllDefaults(ctx,inst);
  829. return _cmDspMidiFilePlayOpen(ctx,inst);
  830. }
  831. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  832. {
  833. cmDspRC_t rc = kOkDspRC;
  834. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  835. unsigned sPc = cmDspSamplesPerCycle(ctx);
  836. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  837. {
  838. if( cmMidiFileIsValid(p->mfH) == false )
  839. {
  840. if( p->errFl==false )
  841. {
  842. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  843. p->errFl = true;
  844. }
  845. return rc;
  846. }
  847. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  848. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  849. do
  850. {
  851. if( p->curMsgIdx >= msgN )
  852. break;
  853. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  854. // convert the absolute time in microseconds to samples
  855. unsigned curMsgTimeSmp = round(mp->amicro * cmDspSampleRate(ctx) / 1000000.0);
  856. // if this midi event falls inside this execution window
  857. if( p->csi > curMsgTimeSmp || curMsgTimeSmp >= (p->csi + sPc))
  858. break;
  859. switch( mp->status )
  860. {
  861. case kNoteOffMdId:
  862. case kNoteOnMdId:
  863. case kCtlMdId:
  864. cmDspSetUInt(ctx,inst, kSmpIdxMfId, curMsgTimeSmp);
  865. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  866. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  867. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  868. cmDspSetUInt(ctx,inst, kIdMfId, mp->uid);
  869. break;
  870. }
  871. p->curMsgIdx += 1;
  872. }while(1);
  873. }
  874. p->csi += sPc;
  875. return rc;
  876. }
  877. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  878. {
  879. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  880. cmDspSetEvent(ctx,inst,evt);
  881. switch(evt->dstVarId)
  882. {
  883. case kFnMfId:
  884. _cmDspMidiFilePlayOpen(ctx, inst );
  885. break;
  886. case kSelMfId:
  887. {
  888. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  889. {
  890. p->csi = cmDspInt(inst,kBsiMfId);
  891. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  892. }
  893. break;
  894. }
  895. }
  896. return kOkDspRC;
  897. }
  898. cmDspClass_t* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  899. {
  900. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  901. NULL,
  902. _cmDspMidiFilePlayAlloc,
  903. _cmDspMidiFilePlayFree,
  904. _cmDspMidiFilePlayReset,
  905. _cmDspMidiFilePlayExec,
  906. _cmDspMidiFilePlayRecv,
  907. NULL,NULL,
  908. "MIDI file player.");
  909. return &_cmMidiFilePlayDC;
  910. }
  911. //------------------------------------------------------------------------------------------------------------
  912. //)
  913. //( { label:cmDspScFol file_desc:"MIDI performance score follower." kw:[sunit] }
  914. enum
  915. {
  916. kFnSfId,
  917. kBufCntSfId,
  918. kMaxWndCntSfId,
  919. kMinVelSfId,
  920. kMeasflSfId,
  921. kIndexSfId,
  922. kMuidSfId,
  923. kStatusSfId,
  924. kD0SfId,
  925. kD1SfId,
  926. kSmpIdxSfId,
  927. kCmdSfId,
  928. kOutSfId,
  929. kRecentSfId,
  930. kVlocSfId,
  931. kVtypSfId,
  932. kVvalSfId,
  933. kVcostSfId,
  934. kSymSfId
  935. };
  936. cmDspClass_t _cmScFolDC;
  937. struct cmDspScFol_str;
  938. typedef struct
  939. {
  940. cmDspCtx_t* ctx;
  941. struct cmDspScFol_str* sfp;
  942. } cmDspScFolCbArg_t;
  943. typedef struct cmDspScFol_str
  944. {
  945. cmDspInst_t inst;
  946. cmScMatcher* sfp;
  947. cmScMeas* smp;
  948. cmScH_t scH;
  949. cmDspScFolCbArg_t arg;
  950. unsigned printSymId;
  951. unsigned quietSymId;
  952. unsigned maxScLocIdx;
  953. bool liveFl;
  954. } cmDspScFol_t;
  955. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  956. {
  957. cmDspVarArg_t args[] =
  958. {
  959. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  960. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Event buffer element count." },
  961. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Maximum window length."},
  962. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Minimum velocity."},
  963. { "measfl",kMeasflSfId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable measurements"},
  964. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  965. { "muid", kMuidSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI msg file unique id"},
  966. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  967. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  968. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  969. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  970. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  971. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Maximum score location index."},
  972. { "recent",kRecentSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Most recent score location index."},
  973. { "vloc", kVlocSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Score location at which the variable value becomes active."},
  974. { "vtyp", kVtypSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Variable type: 0=even=kEvenVarScId 1=dyn=kDynVarScId 2=tempo=kTempoVarScId."},
  975. { "vval", kVvalSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable value."},
  976. { "vcost", kVcostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable match cost value."},
  977. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  978. { NULL, 0, 0, 0, 0, NULL }
  979. };
  980. cmDspScFol_t* p;
  981. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  982. return NULL;
  983. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  984. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  985. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  986. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  987. p->maxScLocIdx= cmInvalidIdx;
  988. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  989. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  990. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  991. cmDspSetDefaultBool( ctx, &p->inst, kMeasflSfId, 0, 0);
  992. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  993. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  994. cmDspSetDefaultUInt( ctx, &p->inst, kRecentSfId, 0, 0);
  995. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  996. return &p->inst;
  997. }
  998. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  999. {
  1000. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1001. cmScMatcherFree(&p->sfp);
  1002. cmScMeasFree(&p->smp);
  1003. cmScoreFinalize(&p->scH);
  1004. return kOkDspRC;
  1005. }
  1006. // This is a callback function from cmScMatcherExec() which is called when
  1007. // this cmDspFol object receives a new score location index.
  1008. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  1009. {
  1010. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  1011. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  1012. {
  1013. cmDspInst_t* inst = &(ap->sfp->inst);
  1014. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  1015. unsigned i;
  1016. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  1017. if(ap->sfp->smp->set[i].value != DBL_MAX )
  1018. {
  1019. // switch( ap->sfp->smp->set[i].sp->varId )
  1020. // {
  1021. // case kEvenVarScId:
  1022. // cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  1023. // break;
  1024. //
  1025. // case kDynVarScId:
  1026. // cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  1027. // break;
  1028. //
  1029. // case kTempoVarScId:
  1030. // cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  1031. // break;
  1032. //
  1033. // default:
  1034. // { assert(0); }
  1035. // }
  1036. //
  1037. // cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  1038. // Set the values in the global variable storage
  1039. cmDspValue_t vv,cv;
  1040. unsigned j;
  1041. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  1042. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  1043. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  1044. {
  1045. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  1046. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  1047. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  1048. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  1049. if( cmDspBool(inst,kMeasflSfId) )
  1050. {
  1051. cmDspSetUInt( ap->ctx, inst, kVlocSfId, ap->sfp->smp->set[i].sp->sectArray[j]->locPtr->index);
  1052. cmDspSetDouble( ap->ctx, inst, kVvalSfId, ap->sfp->smp->set[i].value);
  1053. cmDspSetDouble( ap->ctx, inst, kVcostSfId, ap->sfp->smp->set[i].match_cost);
  1054. cmDspSetUInt( ap->ctx, inst, kVtypSfId, ap->sfp->smp->set[i].sp->varId);
  1055. }
  1056. }
  1057. }
  1058. // // trigger 'section' starts
  1059. // for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  1060. // {
  1061. // const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  1062. // if( locPtr->begSectPtr != NULL )
  1063. // cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  1064. // }
  1065. }
  1066. }
  1067. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  1068. {
  1069. cmDspRC_t rc = kOkDspRC;
  1070. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1071. const cmChar_t* fn;
  1072. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  1073. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  1074. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  1075. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  1076. if( cmScoreIsValid(p->scH) )
  1077. {
  1078. unsigned* dynRefArray = NULL;
  1079. unsigned dynRefCnt = 0;
  1080. // initialize the cmScMatcher
  1081. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  1082. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  1083. // read the dynamics reference array
  1084. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  1085. {
  1086. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  1087. goto errLabel;
  1088. }
  1089. // initialize the cmScMeas object.
  1090. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  1091. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  1092. }
  1093. errLabel:
  1094. return rc;
  1095. }
  1096. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1097. {
  1098. cmDspRC_t rc;
  1099. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  1100. return rc;
  1101. return _cmDspScFolOpenScore(ctx,inst);
  1102. }
  1103. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1104. {
  1105. cmDspRC_t rc = kOkDspRC;
  1106. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1107. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  1108. {
  1109. switch( evt->dstVarId )
  1110. {
  1111. case kIndexSfId:
  1112. if( cmScoreIsValid(p->scH) )
  1113. {
  1114. p->maxScLocIdx = cmInvalidIdx;
  1115. if( cmScMeasReset( p->smp ) != cmOkRC )
  1116. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  1117. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  1118. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  1119. }
  1120. break;
  1121. case kStatusSfId:
  1122. if( cmScoreIsValid(p->scH))
  1123. {
  1124. unsigned scLocIdx = cmInvalidIdx;
  1125. // setup the cmScMeas() callback arg.
  1126. p->arg.ctx = ctx;
  1127. p->arg.sfp = p;
  1128. p->sfp->cbArg = &p->arg;
  1129. // this call may result in a callback to _cmScFolMatcherCb()
  1130. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kMuidSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  1131. if( scLocIdx != cmInvalidIdx )
  1132. {
  1133. // It is possible that the internal score follower may go backwards.
  1134. // In this case it will report a given score location multiple times or out of time order.
  1135. // The 'out' port will only be updated under the circumstances that no later
  1136. // score location has been seen - so the last output from 'out' always reports
  1137. // the furthest possible progress in the score. THe 'recent' output simply reports
  1138. // the most recent output from the internal score follower which may include
  1139. // previously reported or out of order score locations.
  1140. cmDspSetUInt(ctx,inst,kRecentSfId,scLocIdx);
  1141. if( p->maxScLocIdx==cmInvalidIdx || p->maxScLocIdx < scLocIdx )
  1142. {
  1143. p->maxScLocIdx = scLocIdx;
  1144. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  1145. }
  1146. }
  1147. }
  1148. break;
  1149. case kFnSfId:
  1150. _cmDspScFolOpenScore(ctx,inst);
  1151. break;
  1152. case kCmdSfId:
  1153. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  1154. p->sfp->printFl = true;
  1155. else
  1156. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  1157. p->sfp->printFl = false;
  1158. break;
  1159. }
  1160. }
  1161. return rc;
  1162. }
  1163. cmDspClass_t* cmScFolClassCons( cmDspCtx_t* ctx )
  1164. {
  1165. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  1166. NULL,
  1167. _cmDspScFolAlloc,
  1168. _cmDspScFolFree,
  1169. _cmDspScFolReset,
  1170. NULL,
  1171. _cmDspScFolRecv,
  1172. NULL,NULL,
  1173. "Score Follower");
  1174. return &_cmScFolDC;
  1175. }
  1176. //------------------------------------------------------------------------------------------------------------
  1177. //)
  1178. //( { label:cmDspScMod file_desc:"Score driven parameter automation." kw:[sunit] }
  1179. enum
  1180. {
  1181. kScLocIdxMdId,
  1182. kResetIdxMdId,
  1183. kCmdMdId,
  1184. kSelMdId,
  1185. kPostMdId
  1186. };
  1187. cmDspClass_t _cmModulatorDC;
  1188. typedef struct
  1189. {
  1190. cmDspInst_t inst;
  1191. cmScModulator* mp;
  1192. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  1193. cmChar_t* fn;
  1194. cmChar_t* modLabel;
  1195. unsigned onSymId;
  1196. unsigned offSymId;
  1197. unsigned postSymId;
  1198. unsigned dumpSymId;
  1199. } cmDspScMod_t;
  1200. void _cmDspScModCb( void* arg, unsigned varSymId, double value, bool postFl )
  1201. {
  1202. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  1203. cmDspVar_t* varPtr;
  1204. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  1205. return;
  1206. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  1207. if( postFl )
  1208. cmDspSetSymbol(p->tmp_ctx,&p->inst,kPostMdId,p->postSymId);
  1209. }
  1210. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1211. {
  1212. va_list vl1;
  1213. va_copy(vl1,vl);
  1214. cmDspVarArg_t args[] =
  1215. {
  1216. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  1217. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  1218. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  1219. { "sel", kSelMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Set the next active entry group name."},
  1220. { "post", kPostMdId, 0,0, kOutDsvFl | kSymDsvFl, "Sends 'post' symbol after a message transmission if the 'post' flag is set in scMod."},
  1221. { NULL, 0, 0, 0, 0 }
  1222. };
  1223. // validate the argument count
  1224. if( va_cnt != 2 )
  1225. {
  1226. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  1227. return NULL;
  1228. }
  1229. // read the modulator file and label strings
  1230. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  1231. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  1232. va_end(vl1);
  1233. // validate the file
  1234. if( fn==NULL || cmFsIsFile(fn)==false )
  1235. {
  1236. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  1237. return NULL;
  1238. }
  1239. // allocate the internal modulator object
  1240. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  1241. if(mp == NULL )
  1242. {
  1243. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  1244. return NULL;
  1245. }
  1246. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  1247. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  1248. cmDspVarArg_t a[ argCnt+1 ];
  1249. unsigned i;
  1250. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1251. for(i=fixArgCnt; i<argCnt; ++i)
  1252. {
  1253. unsigned varIdx = i - fixArgCnt;
  1254. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  1255. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  1256. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  1257. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  1258. }
  1259. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1260. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1261. p->fn = cmMemAllocStr(fn);
  1262. p->modLabel = cmMemAllocStr(modLabel);
  1263. p->mp = mp;
  1264. p->onSymId = cmSymTblId(ctx->stH,"on");
  1265. p->offSymId = cmSymTblId(ctx->stH,"off");
  1266. p->postSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"post");
  1267. p->dumpSymId = cmSymTblId(ctx->stH,"dump");
  1268. mp->cbArg = p; // set the modulator callback arg
  1269. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1270. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1271. cmDspSetDefaultSymbol(ctx,&p->inst,kSelMdId,cmInvalidId);
  1272. return &p->inst;
  1273. }
  1274. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1275. {
  1276. cmDspRC_t rc = kOkDspRC;
  1277. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1278. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1279. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1280. cmMemFree(p->fn);
  1281. cmMemFree(p->modLabel);
  1282. return rc;
  1283. }
  1284. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1285. {
  1286. cmDspRC_t rc = kOkDspRC;
  1287. cmDspApplyAllDefaults(ctx,inst);
  1288. return rc;
  1289. }
  1290. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1291. {
  1292. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1293. cmDspSetEvent(ctx,inst,evt);
  1294. switch( evt->dstVarId )
  1295. {
  1296. case kResetIdxMdId:
  1297. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1298. break;
  1299. case kCmdMdId:
  1300. {
  1301. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1302. if( symId == p->onSymId )
  1303. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId), cmDspSymbol(inst,kSelMdId));
  1304. if( symId == p->dumpSymId )
  1305. cmScModulatorDump(p->mp);
  1306. }
  1307. break;
  1308. }
  1309. return kOkDspRC;
  1310. }
  1311. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1312. {
  1313. cmDspRC_t rc = kOkDspRC;
  1314. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1315. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1316. {
  1317. p->tmp_ctx = ctx;
  1318. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1319. }
  1320. return rc;
  1321. }
  1322. cmDspClass_t* cmScModClassCons( cmDspCtx_t* ctx )
  1323. {
  1324. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1325. NULL,
  1326. _cmDspScModAlloc,
  1327. _cmDspScModFree,
  1328. _cmDspScModReset,
  1329. _cmDspScModExec,
  1330. _cmDspScModRecv,
  1331. NULL,NULL,
  1332. "Score Driven Variable Modulator.");
  1333. return &_cmModulatorDC;
  1334. }
  1335. //------------------------------------------------------------------------------------------------------------
  1336. //)
  1337. //( { label:cmDspGSwitch file_desc:"Route all inputs to one of a group of outputs." kw:[sunit] }
  1338. enum
  1339. {
  1340. kInChCntGsId,
  1341. kOutGroupCntGsId,
  1342. kGroupSelIdxGsId,
  1343. kBaseInFloatGsId
  1344. };
  1345. cmDspClass_t _cmGSwitchDC;
  1346. typedef struct
  1347. {
  1348. cmDspInst_t inst;
  1349. unsigned iChCnt;
  1350. unsigned oGroupCnt;
  1351. unsigned baseInFloatGsId;
  1352. unsigned baseInSymGsId;
  1353. unsigned baseInBoolGsId;
  1354. unsigned baseOutFloatGsId;
  1355. unsigned baseOutSymGsId;
  1356. unsigned baseOutBoolGsId;
  1357. } cmDspGSwitch_t;
  1358. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1359. {
  1360. va_list vl1;
  1361. va_copy(vl1,vl);
  1362. cmDspVarArg_t args[] =
  1363. {
  1364. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1365. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1366. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1367. { NULL, 0, 0, 0, 0 }
  1368. };
  1369. // validate the argument count
  1370. if( va_cnt != 2 )
  1371. {
  1372. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1373. return NULL;
  1374. }
  1375. // read the input ch and output group count
  1376. unsigned iChCnt = va_arg(vl1,unsigned);
  1377. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1378. va_end(vl1);
  1379. // validate the channel counts
  1380. if( iChCnt == 0 || oGroupCnt==0 )
  1381. {
  1382. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1383. return NULL;
  1384. }
  1385. unsigned typeCnt = 3; // i.e. float,sym,bool
  1386. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1387. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1388. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1389. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1390. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1391. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1392. unsigned fixArgCnt = 3;
  1393. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1394. unsigned argCnt = fixArgCnt + varArgCnt;
  1395. cmDspVarArg_t a[ argCnt+1 ];
  1396. unsigned i;
  1397. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1398. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1399. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1400. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1401. unsigned labelCharCnt = 63;
  1402. cmChar_t label[labelCharCnt+1];
  1403. label[labelCharCnt] = 0;
  1404. unsigned gsid = baseOutFloatGsId;
  1405. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1406. {
  1407. snprintf(label,labelCharCnt,"f-out-%i",i);
  1408. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1409. }
  1410. gsid = baseOutSymGsId;
  1411. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1412. {
  1413. snprintf(label,labelCharCnt,"s-out-%i",i);
  1414. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1415. }
  1416. gsid = baseOutBoolGsId;
  1417. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1418. {
  1419. snprintf(label,labelCharCnt,"b-out-%i",i);
  1420. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1421. }
  1422. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1423. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1424. p->iChCnt = iChCnt;
  1425. p->oGroupCnt = oGroupCnt;
  1426. p->baseInFloatGsId = baseInFloatGsId;
  1427. p->baseInSymGsId = baseInSymGsId;
  1428. p->baseInBoolGsId = baseInBoolGsId;
  1429. p->baseOutFloatGsId = baseOutFloatGsId;
  1430. p->baseOutSymGsId = baseOutSymGsId;
  1431. p->baseOutBoolGsId = baseOutBoolGsId;
  1432. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1433. return &p->inst;
  1434. }
  1435. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1436. {
  1437. cmDspRC_t rc = kOkDspRC;
  1438. cmDspApplyAllDefaults(ctx,inst);
  1439. return rc;
  1440. }
  1441. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1442. {
  1443. cmDspRC_t rc = kOkDspRC;
  1444. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1445. // if this is the group selector
  1446. if( evt->dstVarId == kGroupSelIdxGsId )
  1447. {
  1448. unsigned idx;
  1449. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1450. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1451. else
  1452. cmDspSetEvent(ctx,inst,evt);
  1453. return rc;
  1454. }
  1455. // get the group selector
  1456. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1457. assert( groupIdx < p->oGroupCnt);
  1458. // if this is a float input
  1459. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1460. {
  1461. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1462. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1463. return rc;
  1464. }
  1465. // if this is a symbol input
  1466. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1467. {
  1468. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1469. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1470. return rc;
  1471. }
  1472. // if this is a bool input
  1473. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1474. {
  1475. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1476. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1477. return rc;
  1478. }
  1479. return rc;
  1480. }
  1481. cmDspClass_t* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1482. {
  1483. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1484. NULL,
  1485. _cmDspGSwitchAlloc,
  1486. NULL,
  1487. _cmDspGSwitchReset,
  1488. NULL,
  1489. _cmDspGSwitchRecv,
  1490. NULL,NULL,
  1491. "Ganged switch.");
  1492. return &_cmGSwitchDC;
  1493. }
  1494. //------------------------------------------------------------------------------------------------------------
  1495. //)
  1496. //( { label:cmDspScaleRange file_desc:"Offset and scale a scalar value." kw:[sunit] }
  1497. enum
  1498. {
  1499. kMinInSrId,
  1500. kMaxInSrId,
  1501. kMinOutSrId,
  1502. kMaxOutSrId,
  1503. kValInSrId,
  1504. kValOutSrId,
  1505. };
  1506. cmDspClass_t _cmScaleRangeDC;
  1507. typedef struct
  1508. {
  1509. cmDspInst_t inst;
  1510. } cmDspScaleRange_t;
  1511. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1512. {
  1513. va_list vl1;
  1514. va_copy(vl1,vl);
  1515. cmDspVarArg_t args[] =
  1516. {
  1517. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1518. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1519. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1520. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1521. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1522. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1523. { NULL, 0, 0, 0, 0 }
  1524. };
  1525. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1526. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1527. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1528. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1529. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1530. return &p->inst;
  1531. }
  1532. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1533. {
  1534. cmDspRC_t rc = kOkDspRC;
  1535. cmDspApplyAllDefaults(ctx,inst);
  1536. return rc;
  1537. }
  1538. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1539. {
  1540. cmDspRC_t rc = kOkDspRC;
  1541. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1542. cmDspSetEvent(ctx,inst,evt);
  1543. if( evt->dstVarId == kValInSrId )
  1544. {
  1545. double val = cmDspDouble(inst,kValInSrId);
  1546. double min_in = cmDspDouble(inst,kMinInSrId);
  1547. double max_in = cmDspDouble(inst,kMaxInSrId);
  1548. double min_out = cmDspDouble(inst,kMinOutSrId);
  1549. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1550. double x = cmMax(min_in,cmMin(max_in,val));
  1551. x = (x - min_in)/(max_in - min_in);
  1552. x = min_out + x * (max_out - min_out);
  1553. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1554. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1555. }
  1556. return rc;
  1557. }
  1558. cmDspClass_t* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1559. {
  1560. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1561. NULL,
  1562. _cmDspScaleRangeAlloc,
  1563. NULL,
  1564. _cmDspScaleRangeReset,
  1565. NULL,
  1566. _cmDspScaleRangeRecv,
  1567. NULL,NULL,
  1568. "Scale a value inside an input range to a value in the output range.");
  1569. return &_cmScaleRangeDC;
  1570. }
  1571. //------------------------------------------------------------------------------------------------------------
  1572. //)
  1573. //( { label:cmDspActiveMeas file_desc:"Issue stored parameter values at specified score locations." kw:[sunit] }
  1574. enum
  1575. {
  1576. kCntAmId,
  1577. kSflocAmId,
  1578. kLocAmId,
  1579. kTypeAmId,
  1580. kValueAmId,
  1581. kCstAmId,
  1582. kCmdAmId,
  1583. kScLocAmId,
  1584. kEvenAmId,
  1585. kDynAmId,
  1586. kTempoAmId,
  1587. kCostAmId
  1588. };
  1589. cmDspClass_t _cmActiveMeasDC;
  1590. typedef struct cmDspAmRecd_str
  1591. {
  1592. unsigned loc;
  1593. unsigned type;
  1594. double value;
  1595. double cost;
  1596. struct cmDspAmRecd_str* link;
  1597. } cmDspAmRecd_t;
  1598. //int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1599. //{
  1600. // return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1601. //}
  1602. typedef struct
  1603. {
  1604. cmDspInst_t inst;
  1605. unsigned addSymId;
  1606. unsigned clearSymId;
  1607. unsigned printSymId;
  1608. unsigned rewindSymId;
  1609. cmDspAmRecd_t* array; // array[cnt]
  1610. unsigned cnt;
  1611. cmDspAmRecd_t* list; // first recd in list sorted on 'loc'.
  1612. cmDspAmRecd_t* avail; // next empty recd
  1613. cmDspAmRecd_t* sent; // last recd sent
  1614. } cmDspActiveMeas_t;
  1615. void _cmDspAmAllocList( cmDspActiveMeas_t* p, unsigned cnt )
  1616. {
  1617. assert(p->array == NULL );
  1618. cmDspAmRecd_t* r = cmMemAllocZ(cmDspAmRecd_t,cnt);
  1619. p->cnt = cnt;
  1620. p->array = r;
  1621. p->list = NULL;
  1622. p->avail = r;
  1623. p->sent = NULL;
  1624. }
  1625. cmDspRC_t _cmDspActiveMeasAdd( cmDspCtx_t* ctx, cmDspActiveMeas_t* p, unsigned loc, unsigned type, double value, double cost)
  1626. {
  1627. assert( type != kInvalidVarScId );
  1628. cmDspAmRecd_t* rp = p->list;
  1629. cmDspAmRecd_t* pp = NULL;
  1630. // search for the location to add the new record
  1631. for(; rp!=NULL; rp=rp->link)
  1632. {
  1633. // if this loc and type already exists then replace the value and cost fields
  1634. if( rp->loc==loc && rp->type==type )
  1635. goto foundLabel;
  1636. // if this loc should be inserted before rp
  1637. if( loc < rp->loc )
  1638. break;
  1639. pp = rp;
  1640. }
  1641. // if the pre-allocated list is full
  1642. if( p->avail >= p->array+p->cnt )
  1643. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"Unable to store new measurement record. All preallocated active measurement slots are in use.");
  1644. // if prepending to the list
  1645. if( pp == NULL )
  1646. {
  1647. rp = p->avail;
  1648. rp->link = p->list;
  1649. p->list = rp;
  1650. }
  1651. else
  1652. {
  1653. // if appending to the list after pp
  1654. if( rp == NULL )
  1655. {
  1656. // nothing to do
  1657. }
  1658. else // if inserting between pp and rp
  1659. {
  1660. p->avail->link = rp;
  1661. }
  1662. rp = p->avail;
  1663. pp->link = rp;
  1664. }
  1665. p->avail += 1;
  1666. foundLabel:
  1667. rp->loc = loc;
  1668. rp->type = type;
  1669. rp->value = value;
  1670. rp->cost = cost;
  1671. return kOkDspRC;
  1672. }
  1673. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1674. {
  1675. cmDspVarArg_t args[] =
  1676. {
  1677. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1678. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1679. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1680. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type. (even,dyn,...)" },
  1681. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1682. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1683. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1684. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1685. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1686. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1687. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1688. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1689. { NULL, 0, 0, 0, 0 }
  1690. };
  1691. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1692. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1693. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1694. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1695. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1696. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,256);
  1697. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1698. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1699. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1700. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1701. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1702. return &p->inst;
  1703. }
  1704. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1705. {
  1706. cmDspAmRecd_t* rp = p->list;
  1707. for(; rp!=NULL; rp=rp->link)
  1708. {
  1709. const cmChar_t* label = "<null>";
  1710. switch( rp->type )
  1711. {
  1712. case kEvenVarScId: label="even "; break;
  1713. case kDynVarScId: label="dyn "; break;
  1714. case kTempoVarScId: label="tempo"; break;
  1715. default:
  1716. { assert(0); }
  1717. }
  1718. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",rp->loc,label,rp->value,rp->cost);
  1719. }
  1720. return kOkDspRC;
  1721. }
  1722. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1723. {
  1724. memset(p->array,0,sizeof(p->array[0])*p->cnt);
  1725. p->avail = p->array;
  1726. p->list = NULL;
  1727. p->avail = p->array;
  1728. return kOkDspRC;
  1729. }
  1730. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1731. {
  1732. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1733. cmMemPtrFree(&p->array);
  1734. return kOkDspRC;
  1735. }
  1736. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1737. {
  1738. cmDspRC_t rc = kOkDspRC;
  1739. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1740. cmDspApplyAllDefaults(ctx,inst);
  1741. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1742. _cmDspActiveMeasFree(ctx,inst,evt);
  1743. _cmDspAmAllocList(p,cnt);
  1744. return rc;
  1745. }
  1746. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1747. {
  1748. cmDspRC_t rc = kOkDspRC;
  1749. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1750. cmDspSetEvent(ctx,inst,evt);
  1751. switch( evt->dstVarId )
  1752. {
  1753. case kSflocAmId:
  1754. {
  1755. unsigned sfloc = cmDspUInt(inst,kSflocAmId); // get the recv'd score location
  1756. cmDspAmRecd_t* rp = p->sent==NULL ? p->list : p->sent->link; // get the next recd to send
  1757. bool fl = false;
  1758. for(; rp!=NULL; rp=rp->link)
  1759. if( rp->loc <= sfloc )
  1760. {
  1761. // deterimine the records type
  1762. unsigned varId = cmInvalidId;
  1763. switch( rp->type )
  1764. {
  1765. case kEvenVarScId: varId = kEvenAmId; break;
  1766. case kDynVarScId: varId = kDynAmId; break;
  1767. case kTempoVarScId: varId = kTempoAmId; break;
  1768. default:
  1769. { assert(0); }
  1770. }
  1771. // Sending the location triggers the avail-ch to switch - so the location should only
  1772. // be sent once.
  1773. if( !fl )
  1774. {
  1775. cmDspSetUInt(ctx,inst,kScLocAmId,rp->loc);
  1776. fl = true;
  1777. }
  1778. // transmit the records value and cost
  1779. cmDspSetDouble(ctx,inst,varId,rp->value);
  1780. cmDspSetDouble(ctx,inst,kCostAmId,rp->cost);
  1781. p->sent = rp;
  1782. }
  1783. }
  1784. break;
  1785. case kCmdAmId:
  1786. {
  1787. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1788. if( cmdSymId == p->addSymId )
  1789. rc = _cmDspActiveMeasAdd(ctx,p,cmDspUInt(inst,kLocAmId),cmDspUInt(inst,kTypeAmId),cmDspDouble(inst,kValueAmId),cmDspDouble(inst,kCstAmId));
  1790. else
  1791. if( cmdSymId == p->clearSymId )
  1792. rc = _cmDspActiveMeasClear(ctx,p);
  1793. else
  1794. if( cmdSymId == p->printSymId )
  1795. rc = _cmDspActiveMeasPrint(ctx,p);
  1796. else
  1797. if(cmdSymId == p->rewindSymId )
  1798. p->sent = NULL;
  1799. }
  1800. break;
  1801. }
  1802. // switch( evt->dstVarId )
  1803. // {
  1804. // case kSflocAmId:
  1805. // if( p->nextFullIdx != cmInvalidIdx )
  1806. // {
  1807. // // get the recv'd score location
  1808. // unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1809. //
  1810. // unsigned prvLoc = cmInvalidIdx;
  1811. //
  1812. // // for each remaining avail record
  1813. // for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1814. // {
  1815. // cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1816. //
  1817. // // if this records score location is after the recv'd score loc then we're done
  1818. // if( r->loc > sflocIdx )
  1819. // break;
  1820. //
  1821. // // deterimine the records type
  1822. // unsigned varId = cmInvalidId;
  1823. // switch( r->type )
  1824. // {
  1825. // case kEvenVarScId: varId = kEvenAmId; break;
  1826. // case kDynVarScId: varId = kDynAmId; break;
  1827. // case kTempoVarScId: varId = kTempoAmId; break;
  1828. // default:
  1829. // { assert(0); }
  1830. // }
  1831. //
  1832. // // if this score location has not yet been sent then send it now
  1833. // if( prvLoc != r->loc )
  1834. // cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1835. //
  1836. // // transmit the records value and cost
  1837. // cmDspSetDouble(ctx,inst,varId,r->value);
  1838. // cmDspSetDouble(ctx,inst,kCostAmId,r->cost);
  1839. //
  1840. // prvLoc = r->loc;
  1841. // }
  1842. //
  1843. //
  1844. // }
  1845. // break;
  1846. //
  1847. // case kCmdAmId:
  1848. // {
  1849. // unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1850. //
  1851. // if( cmdSymId == p->addSymId )
  1852. // {
  1853. // if( p->nextEmptyIdx >= p->cnt )
  1854. // cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1855. // else
  1856. // {
  1857. // cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1858. // r->loc = cmDspUInt( inst,kLocAmId);
  1859. // r->type = cmDspUInt( inst,kTypeAmId);
  1860. // r->value = cmDspDouble(inst,kValueAmId);
  1861. // r->cost = cmDspDouble(inst,kCstAmId);
  1862. // p->nextEmptyIdx += 1;
  1863. //
  1864. // qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1865. //
  1866. // if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1867. // p->nextFullIdx = 0;
  1868. //
  1869. // }
  1870. // }
  1871. //
  1872. // if( cmdSymId == p->clearSymId )
  1873. // rc = _cmDspActiveMeasClear(ctx,p);
  1874. // else
  1875. // if( cmdSymId == p->printSymId )
  1876. // rc = _cmDspActiveMeasPrint(ctx,p);
  1877. // else
  1878. // if(cmdSymId == p->rewindSymId )
  1879. // p->nextFullIdx = 0;
  1880. // }
  1881. // break;
  1882. //
  1883. // }
  1884. return rc;
  1885. }
  1886. cmDspClass_t* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1887. {
  1888. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1889. NULL,
  1890. _cmDspActiveMeasAlloc,
  1891. _cmDspActiveMeasFree,
  1892. _cmDspActiveMeasReset,
  1893. NULL,
  1894. _cmDspActiveMeasRecv,
  1895. NULL,NULL,
  1896. "Scale a value inside an input range to a value in the output range.");
  1897. return &_cmActiveMeasDC;
  1898. }
  1899. //------------------------------------------------------------------------------------------------------------
  1900. //)
  1901. //( { label:cmDspAmSync file_desc:"Calculate MIDI to Audio latency offsets." kw:[sunit] }
  1902. //
  1903. //
  1904. // Usage:
  1905. // 1) In the program resource file setup a list of sync points.
  1906. // 'asmp' refers to a sample offset into the audio file 'af'
  1907. // which should match to the midi event index 'mid' in the
  1908. // midi file 'mf'.
  1909. //
  1910. // amSync :
  1911. // [
  1912. // { af:"af-16" asmp:34735276 mf:"mf-10" mid:350 }
  1913. // { af:"af-16" asmp:71802194 mf:"mf-10" mid:787 }
  1914. // ]
  1915. //
  1916. // 2) Feed the 'fidx' output from a wave table loaded with 'af' into the 'asmp' input port of this amSync object.
  1917. // Feed the 'id' output from the MIDI file player loaded with 'mf' into the 'mid' input port of this amSync object.
  1918. //
  1919. // 3) Run the players.
  1920. // 4) When the run is complete send any message to the 'sel' port of this amSync object.
  1921. // The 'frm:' field of the printed output gives the difference in samples between
  1922. // MIDI and audio sync points.
  1923. //
  1924. // If the value is positive then the MIDI point is after the Audio point.
  1925. // If the value is negative then the MIDI point is before the audio point.
  1926. //
  1927. enum
  1928. {
  1929. kSelAmId,
  1930. kAFnAmId,
  1931. kASmpAmId,
  1932. kMFnAmId,
  1933. kMIdAmId,
  1934. };
  1935. enum
  1936. {
  1937. kAfnAmFl = 0x01,
  1938. kMfnAmFl = 0x02,
  1939. kAsmpAmFl = 0x04,
  1940. kMidAmFl = 0x08,
  1941. };
  1942. cmDspClass_t _cmAmSyncDC;
  1943. typedef struct cmDspAmSyncEntry_str
  1944. {
  1945. const cmChar_t* afn; // audio file name
  1946. const cmChar_t* mfn; // midi file name
  1947. unsigned asmp; // Audio sample index to sync to MIDI event
  1948. unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
  1949. int afi; // closest DSP system cycle index to the reference audio sample index (asmp).
  1950. int mfi; // DSP system cycle on which the reference MIDI event (mid) arrived.
  1951. unsigned state; // as incoming msg match this record the state is updated with kXXXAmFl flags
  1952. } cmDspAmSyncEntry_t;
  1953. typedef struct
  1954. {
  1955. cmDspInst_t inst;
  1956. cmDspAmSyncEntry_t* array;
  1957. unsigned arrayCnt;
  1958. cmDspAmSyncEntry_t* acur;
  1959. cmDspAmSyncEntry_t* mcur;
  1960. } cmDspAmSync_t;
  1961. cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1962. {
  1963. cmDspVarArg_t args[] =
  1964. {
  1965. { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
  1966. { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
  1967. { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
  1968. { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
  1969. { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
  1970. { NULL, 0, 0, 0, 0 }
  1971. };
  1972. cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1973. return &p->inst;
  1974. }
  1975. cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1976. {
  1977. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1978. cmMemFree(p->array);
  1979. return kOkDspRC;
  1980. }
  1981. cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1982. {
  1983. cmDspRC_t rc = kOkDspRC;
  1984. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1985. cmDspApplyAllDefaults(ctx,inst);
  1986. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  1987. cmJsonNode_t* np;
  1988. const cmChar_t* errLabelPtr;
  1989. unsigned i;
  1990. cmJsRC_t jsRC;
  1991. if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
  1992. {
  1993. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
  1994. goto errLabel;
  1995. }
  1996. p->arrayCnt = cmJsonChildCount(np);
  1997. p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
  1998. for(i=0; i<p->arrayCnt; ++i)
  1999. {
  2000. cmJsonNode_t* anp = cmJsonArrayElement(np,i);
  2001. cmDspAmSyncEntry_t* r = p->array + i;
  2002. if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
  2003. "af", kStringTId,&r->afn,
  2004. "asmp",kIntTId, &r->asmp,
  2005. "mf", kStringTId,&r->mfn,
  2006. "mid", kIntTId, &r->mid,
  2007. NULL)) != kOkJsRC )
  2008. {
  2009. if( jsRC == kNodeNotFoundJsRC )
  2010. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
  2011. else
  2012. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
  2013. break;
  2014. }
  2015. r->afi = cmInvalidIdx;
  2016. r->mfi = cmInvalidIdx;
  2017. r->state = 0;
  2018. }
  2019. errLabel:
  2020. return rc;
  2021. }
  2022. cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2023. {
  2024. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  2025. unsigned i;
  2026. if( evt->dstVarId != kSelAmId )
  2027. cmDspSetEvent(ctx,inst,evt);
  2028. switch(evt->dstVarId)
  2029. {
  2030. case kSelAmId:
  2031. {
  2032. double srate = cmDspSysSampleRate(ctx->dspH);
  2033. int fpc = cmDspSamplesPerCycle(ctx);
  2034. for(i=0; i<p->arrayCnt; ++i)
  2035. {
  2036. cmDspAmSyncEntry_t* r = p->array + i;
  2037. int dframes = r->mfi-r->afi;
  2038. cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
  2039. r->state,
  2040. r->afn,
  2041. r->asmp,
  2042. r->afi,
  2043. r->mfn,
  2044. r->mid,
  2045. r->mfi,
  2046. dframes,
  2047. dframes*fpc,dframes*fpc/srate);
  2048. r->afi = cmInvalidIdx;
  2049. r->mfi = cmInvalidIdx;
  2050. r->state = 0;
  2051. }
  2052. p->acur = NULL;
  2053. p->mcur = NULL;
  2054. }
  2055. break;
  2056. case kAFnAmId:
  2057. {
  2058. // an audio file name just arrived - set p->acur to point to it
  2059. const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
  2060. for(i=0; i<p->arrayCnt; ++i)
  2061. if( strcmp(fn,p->array[i].afn) == 0 )
  2062. {
  2063. p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
  2064. p->acur = p->array + i;
  2065. }
  2066. }
  2067. break;
  2068. case kMFnAmId:
  2069. {
  2070. // a midi file name just arrived - set p->mcur to point to it
  2071. const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
  2072. for(i=0; i<p->arrayCnt; ++i)
  2073. if( strcmp(fn,p->array[i].mfn) == 0 )
  2074. {
  2075. p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
  2076. p->mcur = p->array + i;
  2077. }
  2078. }
  2079. break;
  2080. case kASmpAmId:
  2081. {
  2082. // a audio file sample index has just arrived
  2083. int v = cmDspInt(inst,kASmpAmId);
  2084. // if a valid audio file has been set
  2085. if( p->acur != NULL )
  2086. for(i=0; i<p->arrayCnt; ++i)
  2087. {
  2088. // if the audio sync point is before or on the new audio file sample index then
  2089. // this is the closest audio file index to the audio sync point - record the
  2090. // associated cycleCnt
  2091. cmDspAmSyncEntry_t* r = p->array + i;
  2092. if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
  2093. {
  2094. r->afi = ctx->cycleCnt;
  2095. r->state = cmSetFlag(r->state,kAsmpAmFl);
  2096. break;
  2097. }
  2098. }
  2099. }
  2100. break;
  2101. case kMIdAmId:
  2102. {
  2103. // a new MIDI event was received
  2104. int v = cmDspInt(inst,kMIdAmId);
  2105. if( p->mcur != NULL )
  2106. for(i=0; i<p->arrayCnt; ++i)
  2107. {
  2108. // if the new MIDI event matched the MIDI sync point then record the
  2109. // current cycleCnt.
  2110. cmDspAmSyncEntry_t* r = p->array + i;
  2111. if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
  2112. {
  2113. r->mfi = ctx->cycleCnt;
  2114. r->state = cmSetFlag(r->state,kMidAmFl);
  2115. break;
  2116. }
  2117. }
  2118. }
  2119. break;
  2120. }
  2121. return kOkDspRC;
  2122. }
  2123. cmDspClass_t* cmAmSyncClassCons( cmDspCtx_t* ctx )
  2124. {
  2125. cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
  2126. NULL,
  2127. _cmDspAmSyncAlloc,
  2128. _cmDspAmSyncFree,
  2129. _cmDspAmSyncReset,
  2130. NULL,
  2131. _cmDspAmSyncRecv,
  2132. NULL,NULL,
  2133. "Audio - MIDI Sync Object.");
  2134. return &_cmAmSyncDC;
  2135. }
  2136. //------------------------------------------------------------------------------------------------------------
  2137. //)
  2138. //( { label:cmDspNanoMap file_desc:"Control a MIDI synth." kw:[sunit] }
  2139. enum
  2140. {
  2141. kPgmNmId,
  2142. kStatusNmId,
  2143. kD0NmId,
  2144. kD1NmId,
  2145. kThruNmId
  2146. };
  2147. cmDspClass_t _cmNanoMapDC;
  2148. typedef struct
  2149. {
  2150. cmDspInst_t inst;
  2151. } cmDspNanoMap_t;
  2152. cmDspRC_t _cmDspNanoMapSend( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned st, unsigned d0, unsigned d1 )
  2153. {
  2154. cmDspSetUInt(ctx,inst,kD1NmId,d1);
  2155. cmDspSetUInt(ctx,inst,kD0NmId,d0);
  2156. cmDspSetUInt(ctx,inst,kStatusNmId,st);
  2157. return kOkDspRC;
  2158. }
  2159. void _cmDspNanoMapPgm( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned pgm )
  2160. {
  2161. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  2162. unsigned i;
  2163. for(i=0; i<kMidiChCnt; ++i)
  2164. {
  2165. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,121,0); // reset all controllers
  2166. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,123,0); // turn all notes off
  2167. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,0,0); // switch to bank 0
  2168. _cmDspNanoMapSend(ctx,inst,kPgmMdId+i,pgm,0); // send pgm change
  2169. cmSleepMs(15);
  2170. }
  2171. }
  2172. cmDspInst_t* _cmDspNanoMapAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2173. {
  2174. cmDspVarArg_t args[] =
  2175. {
  2176. { "pgm", kPgmNmId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reprogram all channels to this pgm." },
  2177. { "status", kStatusNmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  2178. { "d0", kD0NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  2179. { "d1", kD1NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  2180. { "thru", kThruNmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable pass through."},
  2181. { NULL, 0, 0, 0, 0 }
  2182. };
  2183. cmDspNanoMap_t* p = cmDspInstAlloc(cmDspNanoMap_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2184. cmDspSetDefaultUInt(ctx,&p->inst, kPgmNmId, 0, 0 );
  2185. return &p->inst;
  2186. }
  2187. cmDspRC_t _cmDspNanoMapReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2188. {
  2189. cmDspRC_t rc = kOkDspRC;
  2190. cmDspApplyAllDefaults(ctx,inst);
  2191. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2192. return rc;
  2193. }
  2194. cmDspRC_t _cmDspNanoMapRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2195. {
  2196. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  2197. switch( evt->dstVarId )
  2198. {
  2199. case kPgmNmId:
  2200. cmDspSetEvent(ctx,inst,evt);
  2201. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2202. break;
  2203. case kStatusNmId:
  2204. {
  2205. unsigned status = cmDsvGetUInt(evt->valuePtr);
  2206. unsigned stat_no_ch = status & 0xf0;
  2207. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  2208. {
  2209. //unsigned d0 = cmDspUInt(inst,kD0NmId);
  2210. unsigned ch = 0; //d0 % 8;
  2211. status = (status & 0xf0) + ch;
  2212. cmDspSetUInt(ctx,inst,kStatusNmId,status);
  2213. }
  2214. }
  2215. break;
  2216. default:
  2217. cmDspSetEvent(ctx,inst,evt);
  2218. break;
  2219. }
  2220. return kOkDspRC;
  2221. }
  2222. cmDspClass_t* cmNanoMapClassCons( cmDspCtx_t* ctx )
  2223. {
  2224. cmDspClassSetup(&_cmNanoMapDC,ctx,"NanoMap",
  2225. NULL,
  2226. _cmDspNanoMapAlloc,
  2227. NULL,
  2228. _cmDspNanoMapReset,
  2229. NULL,
  2230. _cmDspNanoMapRecv,
  2231. NULL,
  2232. NULL,
  2233. "Nanosynth Mapper");
  2234. return &_cmNanoMapDC;
  2235. }
  2236. //------------------------------------------------------------------------------------------------------------
  2237. //)
  2238. //( { label:cmDspRecdPlay file_desc:"Record audio segments from a live perfromance and play them back at a later time" kw:[sunit] }
  2239. enum
  2240. {
  2241. kChCntPrId,
  2242. kFnPrId,
  2243. kSecsPrId,
  2244. kMaxLaSecsPrId,
  2245. kCurLaSecsPrId,
  2246. kFadeRatePrId,
  2247. kScInitLocIdxPrId,
  2248. kScLocIdxPrId,
  2249. kCmdPrId,
  2250. kInAudioBasePrId
  2251. };
  2252. cmDspClass_t _cmRecdPlayDC;
  2253. typedef struct
  2254. {
  2255. cmDspInst_t inst;
  2256. cmRecdPlay* rcdply;
  2257. cmScH_t scH;
  2258. unsigned onSymId;
  2259. unsigned offSymId;
  2260. unsigned audioOutBaseId;
  2261. unsigned chCnt;
  2262. //unsigned scLocIdx;
  2263. } cmDspRecdPlay_t;
  2264. cmDspRC_t _cmDspRecdPlayParseRsrc( cmDspCtx_t* ctx, cmDspInst_t* inst, cmRecdPlay* rcdply )
  2265. {
  2266. cmDspRC_t rc = kOkDspRC;
  2267. const cmChar_t* path = NULL;
  2268. // read the 'recdplay' audio file path
  2269. if( cmDspRsrcString( ctx->dspH, &path, "recdPlayPath", NULL ) != kOkDspRC )
  2270. {
  2271. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The 'recdPlayPath' resource string was not found.");
  2272. }
  2273. if( path == NULL )
  2274. path = "";
  2275. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2276. cmJsonNode_t* jnp = cmJsonFindValue(jsH,"recdPlay",NULL, kArrayTId);
  2277. if( jnp == NULL || cmJsonIsArray(jnp)==false )
  2278. {
  2279. // this is really a warning - the object does not require preloaded segments.
  2280. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"Warning: The 'recdPlay' resource used to define pre-loaded segments was not found.");
  2281. return kOkDspRC;
  2282. }
  2283. unsigned n = cmJsonChildCount(jnp);
  2284. unsigned i;
  2285. // for each 'recdplay' segment record
  2286. for(i=0; i<n && rc==kOkDspRC; ++i)
  2287. {
  2288. cmJsonNode_t* cnp = cmJsonArrayElement(jnp,i);
  2289. const cmChar_t* label = NULL;
  2290. unsigned segSymId = cmInvalidId;
  2291. const cmChar_t* errLabel = NULL;
  2292. const cmChar_t* fn = NULL;
  2293. // read the ith segment record
  2294. if( cmJsonMemberValues(cnp,&errLabel,
  2295. "label", kStringTId, &label,
  2296. "file", kStringTId, &fn,
  2297. NULL) != kOkJsRC )
  2298. {
  2299. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The record at index %i in the 'recdPlay' pre-loaded segment list could not be parsed.",i);
  2300. goto errLabel;
  2301. }
  2302. // find or generate the symbol id for the segment label symbol
  2303. if((segSymId = cmSymTblRegisterSymbol(ctx->stH,label)) == cmInvalidId )
  2304. {
  2305. rc = cmDspInstErr(ctx,inst,kSymNotFoundDspRC,"The 'recdPlay' pre-load segment symbol '%s' could not be found or registered.",cmStringNullGuard(label));
  2306. goto errLabel;
  2307. }
  2308. // create the full path name for the segment audio file
  2309. if((fn = cmFsMakeFn( path, fn, NULL, NULL )) == NULL )
  2310. {
  2311. rc = cmDspInstErr(ctx,inst,kFileSysFailDspRC,"The 'recdPlay' file name '%s/%s' could not be generated.",cmStringNullGuard(path),cmStringNullGuard(fn));
  2312. goto errLabel;
  2313. }
  2314. // pre-load the segment
  2315. if( cmRecdPlayInsertRecord(rcdply,segSymId,fn) != cmOkRC )
  2316. rc = cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The 'recdPlay' segment label:'%s' file:'%s' could not be loaded.",cmStringNullGuard(label),cmStringNullGuard(fn));
  2317. cmFsFreeFn(fn);
  2318. }
  2319. errLabel:
  2320. return rc;
  2321. }
  2322. cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2323. {
  2324. cmDspRC_t rc =kOkDspRC;
  2325. const cmChar_t* fn;
  2326. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2327. //p->scLocIdx = 0;
  2328. if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
  2329. return cmDspInstErr(ctx,inst, kInvalidArgDspRC, "No score file name supplied.");
  2330. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  2331. return cmDspInstErr(ctx,inst, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  2332. if( cmScoreIsValid(p->scH) )
  2333. {
  2334. unsigned i;
  2335. unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
  2336. double initFragSecs = cmDspDouble(inst,kSecsPrId);
  2337. double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
  2338. double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
  2339. printf("2 max la secs:%f\n",cmDspDouble(inst,kMaxLaSecsPrId));
  2340. if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
  2341. return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
  2342. for(i=0; i<markerCnt; ++i)
  2343. cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
  2344. if((rc = _cmDspRecdPlayParseRsrc(ctx,inst,p->rcdply)) != kOkDspRC )
  2345. rc = cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The 'recdplay' segment pre-load failed.");
  2346. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2347. }
  2348. return rc;
  2349. }
  2350. cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2351. {
  2352. if( va_cnt < 1 )
  2353. {
  2354. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
  2355. return NULL;
  2356. }
  2357. va_list vl1;
  2358. va_copy(vl1,vl);
  2359. int chCnt = va_arg(vl,int);
  2360. unsigned audioOutBase = kInAudioBasePrId + chCnt;
  2361. cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2362. 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
  2363. 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
  2364. 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
  2365. 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum look-ahead buffer in seconds.",
  2366. 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Current look-head buffer in seconds.",
  2367. 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade rate in dB per second.",
  2368. 1, "initIdx",kScInitLocIdxPrId,0,0,kInDsvFl | kUIntDsvFl, "Score search start location.",
  2369. 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
  2370. 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
  2371. chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2372. chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2373. 0 );
  2374. va_end(vl1);
  2375. p->onSymId = cmSymTblId(ctx->stH,"on");
  2376. p->offSymId = cmSymTblId(ctx->stH,"off");
  2377. p->audioOutBaseId = audioOutBase;
  2378. p->chCnt = chCnt;
  2379. //p->scLocIdx = 0;
  2380. printf("0 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2381. cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
  2382. cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
  2383. cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
  2384. cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
  2385. cmDspSetDefaultUInt( ctx,&p->inst, kScInitLocIdxPrId,0,0);
  2386. printf("1 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2387. return &p->inst;
  2388. }
  2389. cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2390. {
  2391. cmDspRC_t rc = kOkDspRC;
  2392. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2393. cmRecdPlayFree(&p->rcdply);
  2394. cmScoreFinalize(&p->scH);
  2395. return rc;
  2396. }
  2397. cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2398. {
  2399. cmDspRC_t rc;
  2400. if((rc = _cmDspRecdPlayOpenScore(ctx,inst)) == kOkDspRC )
  2401. cmDspApplyAllDefaults(ctx,inst);
  2402. return rc;
  2403. }
  2404. cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2405. {
  2406. cmDspRC_t rc = kOkDspRC;
  2407. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2408. const cmSample_t* x[ p->chCnt ];
  2409. cmSample_t* y[ p->chCnt ];
  2410. unsigned n = 0;
  2411. unsigned i;
  2412. unsigned actChCnt = 0;
  2413. for(i=0; i<p->chCnt; ++i)
  2414. {
  2415. if( cmDspIsAudioInputConnected(ctx,inst,kInAudioBasePrId+i) == false )
  2416. {
  2417. x[i] = NULL;
  2418. y[i] = NULL;
  2419. }
  2420. else
  2421. {
  2422. if( i==0 )
  2423. n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
  2424. else
  2425. {
  2426. assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0));
  2427. }
  2428. x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
  2429. if( x[i] != NULL )
  2430. {
  2431. y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
  2432. if( y[i] != NULL )
  2433. {
  2434. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
  2435. cmVOS_Zero(y[i],n);
  2436. actChCnt += 1;
  2437. }
  2438. }
  2439. }
  2440. }
  2441. cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
  2442. return rc;
  2443. }
  2444. cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2445. {
  2446. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2447. if( p->rcdply == NULL )
  2448. return kOkDspRC;
  2449. cmDspSetEvent(ctx,inst,evt);
  2450. switch( evt->dstVarId )
  2451. {
  2452. case kCmdPrId:
  2453. if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
  2454. {
  2455. printf("rewind\n");
  2456. cmRecdPlayRewind(p->rcdply);
  2457. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2458. }
  2459. else
  2460. if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
  2461. {
  2462. }
  2463. break;
  2464. case kCurLaSecsPrId:
  2465. cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
  2466. break;
  2467. case kScInitLocIdxPrId:
  2468. printf("init-idx:%i\n",cmDspUInt(inst,kScInitLocIdxPrId));
  2469. break;
  2470. case kScLocIdxPrId:
  2471. {
  2472. unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId);
  2473. if( endScLocIdx < cmDspUInt(inst,kScInitLocIdxPrId) )
  2474. break;
  2475. cmScoreLoc_t* loc = cmScoreLoc(p->scH, endScLocIdx );
  2476. if( loc == NULL )
  2477. break;
  2478. cmScoreMarker_t* mp = loc->markList;
  2479. for(; mp!=NULL; mp=mp->link)
  2480. switch( mp->markTypeId )
  2481. {
  2482. case kRecdBegScMId:
  2483. printf("recd-beg %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2484. cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
  2485. break;
  2486. case kRecdEndScMId:
  2487. printf("recd-end %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2488. cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
  2489. break;
  2490. case kPlayBegScMId:
  2491. printf("play-beg\n");
  2492. cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
  2493. break;
  2494. case kPlayEndScMId:
  2495. printf("play-end\n");
  2496. cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
  2497. break;
  2498. case kFadeScMId:
  2499. printf("fade-beg\n");
  2500. cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
  2501. break;
  2502. default:
  2503. break;
  2504. }
  2505. //p->scLocIdx = endScLocIdx+1;
  2506. }
  2507. break;
  2508. }
  2509. return kOkDspRC;
  2510. }
  2511. cmDspClass_t* cmRecdPlayClassCons( cmDspCtx_t* ctx )
  2512. {
  2513. cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
  2514. NULL,
  2515. _cmDspRecdPlayAlloc,
  2516. _cmDspRecdPlayFree,
  2517. _cmDspRecdPlayReset,
  2518. _cmDspRecdPlayExec,
  2519. _cmDspRecdPlayRecv,
  2520. NULL,
  2521. NULL,
  2522. "Score controlled live recorder/player");
  2523. return &_cmRecdPlayDC;
  2524. }
  2525. //------------------------------------------------------------------------------------------------------------
  2526. //)
  2527. //( { label:cmDspGoertzel file_desc:"Goertzel tone detection filter" kw:[sunit] }
  2528. enum
  2529. {
  2530. kHopFactGrId,
  2531. kInGrId,
  2532. kHzBaseGrId,
  2533. };
  2534. cmDspClass_t _cmGoertzelDC;
  2535. typedef struct
  2536. {
  2537. cmDspInst_t inst;
  2538. cmGoertzel* g;
  2539. double outPhs;
  2540. unsigned outBaseGrId;
  2541. unsigned chCnt;
  2542. } cmDspGoertzel_t;
  2543. cmDspInst_t* _cmDspGoertzelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2544. {
  2545. if( va_cnt !=3 )
  2546. {
  2547. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Goertzel' constructor must have two arguments: a channel count and frequency array.");
  2548. return NULL;
  2549. }
  2550. va_list vl1;
  2551. va_copy(vl1,vl);
  2552. unsigned hopFact = va_arg(vl,unsigned);
  2553. int chCnt = va_arg(vl,int);
  2554. double* hzV = va_arg(vl,double*);
  2555. unsigned outBaseGrId = kHzBaseGrId + chCnt;
  2556. unsigned i;
  2557. cmDspGoertzel_t* p = cmDspInstAllocV(cmDspGoertzel_t,ctx,classPtr,instSymId,id,storeSymId,1,vl1,
  2558. 1, "hop", kHopFactGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hop factor",
  2559. 1, "in", kInGrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2560. chCnt, "hz", kHzBaseGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hz input.",
  2561. chCnt, "out", outBaseGrId, 0,1, kOutDsvFl | kDoubleDsvFl, "Detector output",
  2562. 0 );
  2563. va_end(vl1);
  2564. p->outBaseGrId = outBaseGrId;
  2565. p->chCnt = chCnt;
  2566. p->g = cmGoertzelAlloc(ctx->cmProcCtx, NULL, 0, NULL, 0,0,0,0 );
  2567. cmDspSetDefaultUInt(ctx,&p->inst, kHopFactGrId, 0, cmMax(hopFact,1));
  2568. for(i=0; i<chCnt; ++i)
  2569. cmDspSetDefaultDouble(ctx,&p->inst, kHzBaseGrId+i, 0.0, hzV[i] );
  2570. return &p->inst;
  2571. }
  2572. cmDspRC_t _cmDspGoertzelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2573. {
  2574. cmDspRC_t rc = kOkDspRC;
  2575. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2576. unsigned hopFact = cmDspUInt(inst,kHopFactGrId);
  2577. unsigned procSmpCnt = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2578. unsigned wndSmpCnt = procSmpCnt * hopFact;
  2579. double fcHzV[ p->chCnt ];
  2580. unsigned i;
  2581. for(i=0; i<p->chCnt; ++i)
  2582. {
  2583. double hz;
  2584. if( p->g->ch == NULL || p->g->ch[i].hz == 0 )
  2585. hz = cmDspDouble(inst,kHzBaseGrId);
  2586. else
  2587. hz = p->g->ch[i].hz;
  2588. fcHzV[i] = hz;
  2589. }
  2590. if( cmGoertzelInit(p->g,cmDspSysSampleRate(ctx->dspH),fcHzV,p->chCnt,procSmpCnt,procSmpCnt,wndSmpCnt) != cmOkRC )
  2591. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal Goertzel detector.");
  2592. return rc;
  2593. }
  2594. cmDspRC_t _cmDspGoertzelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2595. {
  2596. cmDspRC_t rc = kOkDspRC;
  2597. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2598. cmGoertzelFree(&p->g);
  2599. return rc;
  2600. }
  2601. cmDspRC_t _cmDspGoertzelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2602. {
  2603. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2604. cmDspApplyAllDefaults(ctx,inst);
  2605. p->outPhs = 0;
  2606. return _cmDspGoertzelSetup(ctx, inst );
  2607. }
  2608. cmDspRC_t _cmDspGoertzelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2609. {
  2610. cmDspRC_t rc = kOkDspRC;
  2611. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2612. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInGrId,0);
  2613. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2614. double outMs = 50.0;
  2615. double outPhsMax = outMs * cmDspSysSampleRate(ctx->dspH) / 1000.0;
  2616. double outV[ p->chCnt ];
  2617. unsigned i;
  2618. if( x != NULL )
  2619. {
  2620. cmGoertzelExec(p->g,x,n,outV,p->chCnt);
  2621. p->outPhs += n;
  2622. if( p->outPhs > outPhsMax )
  2623. {
  2624. while( p->outPhs > outPhsMax )
  2625. p->outPhs -= outPhsMax;
  2626. for(i=0; i<p->chCnt; ++i)
  2627. {
  2628. cmDspSetDouble(ctx,inst,p->outBaseGrId+i,outV[i]);
  2629. //printf("%f ",outV[i]);
  2630. }
  2631. //printf("\n");
  2632. }
  2633. }
  2634. return rc;
  2635. }
  2636. cmDspRC_t _cmDspGoertzelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2637. {
  2638. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2639. cmDspSetEvent(ctx,inst,evt);
  2640. if( kHzBaseGrId <= evt->dstVarId && evt->dstVarId < kHzBaseGrId+p->chCnt )
  2641. cmGoertzelSetFcHz(p->g, evt->dstVarId - kHzBaseGrId, cmDspDouble(inst,evt->dstVarId));
  2642. else
  2643. {
  2644. if( evt->dstVarId==kHopFactGrId )
  2645. {
  2646. _cmDspGoertzelSetup(ctx,inst);
  2647. }
  2648. }
  2649. return kOkDspRC;
  2650. }
  2651. cmDspClass_t* cmGoertzelClassCons( cmDspCtx_t* ctx )
  2652. {
  2653. cmDspClassSetup(&_cmGoertzelDC,ctx,"Goertzel",
  2654. NULL,
  2655. _cmDspGoertzelAlloc,
  2656. _cmDspGoertzelFree,
  2657. _cmDspGoertzelReset,
  2658. _cmDspGoertzelExec,
  2659. _cmDspGoertzelRecv,
  2660. NULL,
  2661. NULL,
  2662. "Goertzel Tone Detector Filter");
  2663. return &_cmGoertzelDC;
  2664. }
  2665. //------------------------------------------------------------------------------------------------------------
  2666. //)
  2667. //( { label:cmDspSyncRecd file_desc:"Time align a MIDI and associated audio recording" kw:[sunit] }
  2668. enum
  2669. {
  2670. kRecdDirSrId,
  2671. kSrFnSrId,
  2672. kAfSrId,
  2673. kBitsSrId,
  2674. kCmdSrId,
  2675. kStatusSrId,
  2676. kD0SrId,
  2677. kD1SrId,
  2678. kSecSrId,
  2679. kNSecSrId,
  2680. kAinBaseSrId
  2681. };
  2682. cmDspClass_t _cmSyncRecdDC;
  2683. typedef struct
  2684. {
  2685. cmDspInst_t inst;
  2686. unsigned chCnt;
  2687. cmTimeSpec_t ats;
  2688. cmSyncRecdH_t srH;
  2689. unsigned openSymId;
  2690. unsigned closeSymId;
  2691. const cmChar_t* aFn;
  2692. const cmChar_t* srFn;
  2693. unsigned smpIdx;
  2694. } cmDspSyncRecd_t;
  2695. cmDspRC_t _cmDspSyncRecdCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2696. {
  2697. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2698. const cmChar_t* aFn = cmDspStrcz(inst,kAfSrId);
  2699. const cmChar_t* srFn = cmDspStrcz(inst,kSrFnSrId);
  2700. const cmChar_t* dir = cmDspStrcz(inst,kRecdDirSrId);
  2701. if( !cmFsIsDir(dir) )
  2702. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' is not a valid directory.",cmStringNullGuard(dir));
  2703. cmMemPtrFree(&p->aFn);
  2704. if( cmFsGenFn(dir,aFn,"aiff",&p->aFn) != kOkFsRC )
  2705. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Audio file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(aFn));
  2706. cmMemPtrFree(&p->srFn);
  2707. if( cmFsGenFn(dir,srFn,"sr",&p->srFn) != kOkFsRC )
  2708. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Sync-recd file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(srFn));
  2709. unsigned bits = cmDspUInt(inst,kBitsSrId);
  2710. if( cmSyncRecdCreate( ctx->cmCtx, &p->srH, p->srFn, p->aFn, cmDspSampleRate(ctx), p->chCnt, bits ) != kOkSyRC )
  2711. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd file create failed for '%s'.",p->srFn);
  2712. p->smpIdx = 0;
  2713. return kOkDspRC;
  2714. }
  2715. cmDspInst_t* _cmDspSyncRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2716. {
  2717. cmDspSyncRecd_t* p = cmDspInstAllocV(cmDspSyncRecd_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2718. 1, "dir", kRecdDirSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Recording directory.",
  2719. 1, "srfn", kSrFnSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "SyncRecd file prefix.",
  2720. 1, "afn", kAfSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file prefix.",
  2721. 1, "bits", kBitsSrId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Audio file bits per sample.",
  2722. 1, "cmd", kCmdSrId, 0,0, kInDsvFl | kSymDsvFl, "Command: open | close",
  2723. 1, "status", kStatusSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI status",
  2724. 1, "d0", kD0SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d0",
  2725. 1, "d1", kD1SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d1",
  2726. 1, "sec", kSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Seconds",
  2727. 1, "nsec", kNSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Nanoseconds",
  2728. 2, "ain", kAinBaseSrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio Input",
  2729. 0 );
  2730. p->chCnt = 2;
  2731. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  2732. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  2733. cmDspSetDefaultUInt(ctx,&p->inst,kBitsSrId,0,16);
  2734. return &p->inst;
  2735. }
  2736. cmDspRC_t _cmDspSyncRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2737. {
  2738. cmDspRC_t rc = kOkDspRC;
  2739. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2740. cmMemPtrFree(&p->aFn);
  2741. cmMemPtrFree(&p->srFn);
  2742. cmSyncRecdFinal(&p->srH);
  2743. return rc;
  2744. }
  2745. cmDspRC_t _cmDspSyncRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2746. {
  2747. cmDspRC_t rc = kOkDspRC;
  2748. cmDspApplyAllDefaults(ctx,inst);
  2749. return rc;
  2750. }
  2751. cmDspRC_t _cmDspSyncRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2752. {
  2753. cmDspRC_t rc = kOkDspRC;
  2754. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2755. const cmSample_t* x[ p->chCnt ];
  2756. unsigned n = 0;
  2757. unsigned i;
  2758. //const cmTimeSpec_t* ts = &ctx->ctx->oTimeStamp;
  2759. //printf("SR: %ld %ld\n",ts->tv_sec,ts->tv_nsec);
  2760. p->ats = ctx->ctx->iTimeStamp;
  2761. for(i=0; i<p->chCnt; ++i)
  2762. {
  2763. if( i==0 )
  2764. n = cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0);
  2765. else
  2766. { assert( n == cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0)); }
  2767. x[i] = cmDspAudioBuf(ctx,inst,kAinBaseSrId+i,0);
  2768. }
  2769. if( n>0 && cmSyncRecdIsValid(p->srH ) )
  2770. if( cmSyncRecdAudioWrite( p->srH, &ctx->ctx->iTimeStamp, p->smpIdx, x, p->chCnt, n ) != kOkSyRC )
  2771. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd audio update failed.");
  2772. p->smpIdx += n;
  2773. return rc;
  2774. }
  2775. cmDspRC_t _cmDspSyncRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2776. {
  2777. cmDspRC_t rc = kOkDspRC;
  2778. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2779. cmDspSetEvent(ctx,inst,evt);
  2780. switch( evt->dstVarId )
  2781. {
  2782. case kStatusSrId:
  2783. if(cmMidiIsChStatus( cmDspUInt(inst,kStatusSrId) ) )
  2784. {
  2785. cmTimeSpec_t ts;
  2786. ts.tv_sec = cmDspUInt(inst,kSecSrId);
  2787. ts.tv_nsec = cmDspUInt(inst,kNSecSrId);
  2788. //printf("%i %i\n",cmDspUInt(inst,kD1SrId),cmTimeElapsedMicros(&ts,&p->ats));
  2789. if( cmSyncRecdIsValid(p->srH ) )
  2790. if( cmSyncRecdMidiWrite(p->srH, &ts, cmDspUInt(inst,kStatusSrId), cmDspUInt(inst,kD0SrId), cmDspUInt(inst,kD1SrId) ) != kOkSyRC )
  2791. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd MIDI update failed.");
  2792. }
  2793. break;
  2794. case kCmdSrId:
  2795. {
  2796. unsigned cmdId = cmDspSymbol(inst,kCmdSrId);
  2797. if( cmdId == p->openSymId )
  2798. rc = _cmDspSyncRecdCreateFile(ctx,inst);
  2799. else
  2800. if( cmdId == p->closeSymId && cmSyncRecdIsValid(p->srH))
  2801. {
  2802. cmSyncRecdFinal(&p->srH);
  2803. //cmSyncRecdTest(ctx->cmCtx);
  2804. }
  2805. }
  2806. break;
  2807. }
  2808. return rc;
  2809. }
  2810. cmDspClass_t* cmSyncRecdClassCons( cmDspCtx_t* ctx )
  2811. {
  2812. cmDspClassSetup(&_cmSyncRecdDC,ctx,"SyncRecd",
  2813. NULL,
  2814. _cmDspSyncRecdAlloc,
  2815. _cmDspSyncRecdFree,
  2816. _cmDspSyncRecdReset,
  2817. _cmDspSyncRecdExec,
  2818. _cmDspSyncRecdRecv,
  2819. NULL,
  2820. NULL,
  2821. "Synchronized Audio and MIDI recorder.");
  2822. return &_cmSyncRecdDC;
  2823. }
  2824. //------------------------------------------------------------------------------------------------------------
  2825. //)
  2826. //( { label:cmDspTakeSeqBldr file_desc:"User interface unit for creating a single sequence from multiple, score aligned, MIDI fragments." kw:[sunit] }
  2827. enum
  2828. {
  2829. kFnTsbId,
  2830. kBldrTsbId,
  2831. kSelTsbId,
  2832. kRefreshTsbId,
  2833. kSendTsbId
  2834. };
  2835. cmDspClass_t _cmTakeSeqBldrDC;
  2836. typedef struct
  2837. {
  2838. cmDspInst_t inst;
  2839. cmTakeSeqBldrH_t h;
  2840. bool errFl;
  2841. } cmDspTakeSeqBldr_t;
  2842. cmDspInst_t* _cmDspTakeSeqBldrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2843. {
  2844. cmDspTakeSeqBldr_t* p = cmDspInstAllocV(cmDspTakeSeqBldr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2845. 1, "fn", kFnTsbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score Tracking file.",
  2846. 1, "bldr", kBldrTsbId, 0, 0, kOutDsvFl | kPtrDsvFl, "Bldr Ref",
  2847. 1, "sel", kSelTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Selected score element location index.",
  2848. 1, "refresh",kRefreshTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Refresh",
  2849. 1, "send", kSendTsbId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element location.",
  2850. 0 );
  2851. p->errFl = false;
  2852. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsbId, 0, 0);
  2853. if( cmTakeSeqBldrAlloc(ctx->cmCtx, &p->h ) != kOkTsbRC )
  2854. cmErrMsg(&p->inst.classPtr->err, kSubSysFailDspRC, "Allocate TaskSeqBldr object.");
  2855. else
  2856. {
  2857. cmDspUiTakeSeqBldrCreate(ctx,&p->inst,kFnTsbId,kBldrTsbId,kSelTsbId,kRefreshTsbId);
  2858. }
  2859. return &p->inst;
  2860. }
  2861. cmDspRC_t _cmDspTakeSeqBldrSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2862. {
  2863. cmDspRC_t rc = kOkDspRC;
  2864. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2865. if( cmTakeSeqBldrInitialize(p->h, cmDspStrcz(inst,kFnTsbId) ) != kOkTsbRC )
  2866. {
  2867. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal TakeSeqBldr object with %s.",cmStringNullGuard(cmDspStrcz(inst,kFnTsbId)));
  2868. }
  2869. else
  2870. {
  2871. cmDspSetPtr(ctx,inst,kBldrTsbId,p->h.h);
  2872. p->errFl = false;
  2873. }
  2874. return rc;
  2875. }
  2876. cmDspRC_t _cmDspTakeSeqBldrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2877. {
  2878. cmDspRC_t rc = kOkDspRC;
  2879. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2880. cmTakeSeqBldrFree(&p->h);
  2881. return rc;
  2882. }
  2883. cmDspRC_t _cmDspTakeSeqBldrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2884. {
  2885. //cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2886. cmDspApplyAllDefaults(ctx,inst);
  2887. return _cmDspTakeSeqBldrSetup(ctx, inst );
  2888. }
  2889. cmDspRC_t _cmDspTakeSeqBldrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2890. {
  2891. cmDspRC_t rc = kOkDspRC;
  2892. return rc;
  2893. }
  2894. cmDspRC_t _cmDspTakeSeqBldrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2895. {
  2896. // not matter what arrives at the 'send' input ....
  2897. if( evt->dstVarId == kSendTsbId )
  2898. {
  2899. // send the last score loc
  2900. unsigned selIdx;
  2901. if((selIdx = cmDspUInt(inst,kSelTsbId)) != cmInvalidIdx )
  2902. cmDspSetUInt(ctx,inst,kSelTsbId, selIdx );
  2903. return kOkDspRC;
  2904. }
  2905. cmDspSetEvent(ctx,inst,evt);
  2906. switch(evt->dstVarId)
  2907. {
  2908. case kFnTsbId:
  2909. _cmDspMidiFilePlayOpen(ctx, inst );
  2910. break;
  2911. }
  2912. return kOkDspRC;
  2913. }
  2914. cmDspClass_t* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx )
  2915. {
  2916. cmDspClassSetup(&_cmTakeSeqBldrDC,ctx,"TakeSeqBldr",
  2917. NULL,
  2918. _cmDspTakeSeqBldrAlloc,
  2919. _cmDspTakeSeqBldrFree,
  2920. _cmDspTakeSeqBldrReset,
  2921. _cmDspTakeSeqBldrExec,
  2922. _cmDspTakeSeqBldrRecv,
  2923. NULL,
  2924. NULL,
  2925. "TakeSeqBldr");
  2926. return &_cmTakeSeqBldrDC;
  2927. }
  2928. //------------------------------------------------------------------------------------------------------------
  2929. //)
  2930. //( { label:cmDspTakeSeqRend file_desc:"User interface unit for graphically rendering the MIDI sequences created by cmDspTakeSeqBldr." kw:[sunit] }
  2931. enum
  2932. {
  2933. kBldrTsrId,
  2934. kRefreshTsrId,
  2935. kCmdTsrId,
  2936. kSelTsrId,
  2937. kStatusTsrId,
  2938. kD0TsrId,
  2939. kD1TsrId,
  2940. kSmpIdxTsrId
  2941. };
  2942. cmDspClass_t _cmTakeSeqRendDC;
  2943. typedef struct
  2944. {
  2945. cmDspInst_t inst;
  2946. cmTakeSeqBldrH_t h;
  2947. unsigned startSymId;
  2948. unsigned stopSymId;
  2949. unsigned contSymId;
  2950. unsigned onSymId;
  2951. unsigned offSymId;
  2952. bool errFl;
  2953. } cmDspTakeSeqRend_t;
  2954. cmDspInst_t* _cmDspTakeSeqRendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2955. {
  2956. cmDspTakeSeqRend_t* p = cmDspInstAllocV(cmDspTakeSeqRend_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2957. 1, "bldr", kBldrTsrId, 0, 0, kInDsvFl | kPtrDsvFl | kOptArgDsvFl, "Take Sequene Builder Ref",
  2958. 1, "refresh",kRefreshTsrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Refresh",
  2959. 1, "cmd", kCmdTsrId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" ,
  2960. 1, "sel", kSelTsrId, 0, 0, kInDsvFl | kUIntDsvFl, "Selected score element location index input.",
  2961. 1, "status", kStatusTsrId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output",
  2962. 1, "d0", kD0TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" ,
  2963. 1, "d1", kD1TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1",
  2964. 1, "smpidx", kSmpIdxTsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index.",
  2965. 0 );
  2966. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  2967. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  2968. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  2969. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2970. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2971. p->errFl = false;
  2972. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsrId, 0, 0);
  2973. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdTsrId, p->stopSymId);
  2974. cmDspSetDefaultInt( ctx, &p->inst, kSmpIdxTsrId, 0, 0);
  2975. cmDspSetDefaultUInt( ctx, &p->inst, kStatusTsrId, 0, 0);
  2976. cmDspSetDefaultUInt( ctx, &p->inst, kD0TsrId, 0, 0);
  2977. cmDspSetDefaultUInt( ctx, &p->inst, kD1TsrId, 0, 0);
  2978. cmDspUiTakeSeqRendCreate(ctx,&p->inst,kBldrTsrId,kRefreshTsrId,kSelTsrId);
  2979. return &p->inst;
  2980. }
  2981. cmDspRC_t _cmDspTakeSeqRendFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2982. {
  2983. cmDspRC_t rc = kOkDspRC;
  2984. return rc;
  2985. }
  2986. cmDspRC_t _cmDspTakeSeqRendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2987. {
  2988. //cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2989. cmDspApplyAllDefaults(ctx,inst);
  2990. return kOkDspRC;
  2991. }
  2992. typedef struct
  2993. {
  2994. cmDspCtx_t* ctx;
  2995. cmDspInst_t* inst;
  2996. } _cmDspTakeSeqRendCbArg_t;
  2997. // Called from cmDspTakeSeqRendExec() -> cmTakeSeqRendPlayExec() to
  2998. // deliver MIDI messages which need to be transmitted.
  2999. void _cmDspTakeSeqRendMidiCb( void* arg, const cmTksbEvent_t* e )
  3000. {
  3001. _cmDspTakeSeqRendCbArg_t* a = (_cmDspTakeSeqRendCbArg_t*)arg;
  3002. switch( e->status )
  3003. {
  3004. case kNoteOffMdId:
  3005. case kNoteOnMdId:
  3006. case kCtlMdId:
  3007. //if( !cmMidiIsPedal(e->status,e->d0))
  3008. {
  3009. cmDspSetUInt(a->ctx,a->inst, kSmpIdxTsrId, e->smpIdx);
  3010. cmDspSetUInt(a->ctx,a->inst, kD1TsrId, e->d1);
  3011. cmDspSetUInt(a->ctx,a->inst, kD0TsrId, e->d0);
  3012. cmDspSetUInt(a->ctx,a->inst, kStatusTsrId, e->status);
  3013. }
  3014. break;
  3015. }
  3016. }
  3017. void _cmDspTakeSeqRendPedalsUp( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3018. {
  3019. _cmDspTakeSeqRendCbArg_t a;
  3020. a.ctx = ctx;
  3021. a.inst = inst;
  3022. cmTksbEvent_t e[] =
  3023. {
  3024. { 0, kCtlMdId, kSustainCtlMdId, 0 },
  3025. { 0, kCtlMdId, kPortamentoCtlMdId,0 },
  3026. { 0, kCtlMdId, kSostenutoCtlMdId, 0 },
  3027. { 0, kCtlMdId, kSoftPedalCtlMdId, 0 },
  3028. { 0, kCtlMdId, kLegatoCtlMdId, 0 }
  3029. };
  3030. unsigned n = sizeof(e)/sizeof(e[0]);
  3031. unsigned i;
  3032. for(i=0; i<n; ++i)
  3033. _cmDspTakeSeqRendMidiCb(&a,e+i);
  3034. }
  3035. cmDspRC_t _cmDspTakeSeqRendExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3036. {
  3037. cmDspRC_t rc = kOkDspRC;
  3038. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3039. if( cmDspSymbol(inst,kCmdTsrId) != p->stopSymId )
  3040. {
  3041. if( cmTakeSeqBldrIsValid(p->h) == false )
  3042. {
  3043. if( p->errFl==false )
  3044. {
  3045. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The Take Sequence Builder not been given a valid file.");
  3046. p->errFl = true;
  3047. }
  3048. return rc;
  3049. }
  3050. unsigned sPc = cmDspSamplesPerCycle(ctx);
  3051. _cmDspTakeSeqRendCbArg_t arg;
  3052. arg.inst = inst;
  3053. arg.ctx = ctx;
  3054. // This call may result in multiple callbacks
  3055. // to _cmDspTakeSeqRendMidiCb() from within the function.
  3056. cmTakeSeqBldrPlayExec(p->h,sPc,_cmDspTakeSeqRendMidiCb, &arg );
  3057. }
  3058. return rc;
  3059. }
  3060. cmDspRC_t _cmDspTakeSeqRendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3061. {
  3062. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3063. printf("Recv:%s\n",cmStringNullGuard(cmDspVarLabel(ctx, inst, evt->dstVarId)) );
  3064. cmDspSetEvent(ctx,inst,evt);
  3065. switch(evt->dstVarId)
  3066. {
  3067. case kBldrTsrId:
  3068. p->h.h = cmDspPtr(inst,kBldrTsrId);
  3069. break;
  3070. case kCmdTsrId:
  3071. {
  3072. unsigned symId = cmDspSymbol(inst,kCmdTsrId);
  3073. if( symId == p->onSymId )
  3074. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->startSymId );
  3075. else
  3076. if( symId == p->offSymId )
  3077. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->stopSymId );
  3078. if( cmTakeSeqBldrIsValid(p->h) && cmDspSymbol(inst,kCmdTsrId)==p->startSymId )
  3079. {
  3080. _cmDspTakeSeqRendPedalsUp( ctx, inst );
  3081. cmTakeSeqBldrPlaySeekLoc(p->h, cmInvalidId );
  3082. }
  3083. }
  3084. break;
  3085. case kSelTsrId:
  3086. {
  3087. // seek the playback position to the scLocIdx.
  3088. unsigned scLocIdx = cmDspUInt(inst,kSelTsrId);
  3089. if( cmTakeSeqBldrIsValid(p->h) && cmTakeSeqBldrPlaySeekLoc(p->h, scLocIdx ) != kOkTsbRC )
  3090. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Take Sequence Bldr Seek failed on score location index %i.", scLocIdx);
  3091. }
  3092. break;
  3093. }
  3094. return kOkDspRC;
  3095. }
  3096. cmDspClass_t* cmTakeSeqRendClassCons( cmDspCtx_t* ctx )
  3097. {
  3098. cmDspClassSetup(&_cmTakeSeqRendDC,ctx,"TakeSeqRend",
  3099. NULL,
  3100. _cmDspTakeSeqRendAlloc,
  3101. _cmDspTakeSeqRendFree,
  3102. _cmDspTakeSeqRendReset,
  3103. _cmDspTakeSeqRendExec,
  3104. _cmDspTakeSeqRendRecv,
  3105. NULL,
  3106. NULL,
  3107. "TakeSeqRend");
  3108. return &_cmTakeSeqRendDC;
  3109. }
  3110. //------------------------------------------------------------------------------------------------------------
  3111. //)
  3112. //( { label:cmDspReflectCalc file_desc:"Estimate the time-of-flight of from an acoustic signal from a speaker to a microphone." kw:[sunit] }
  3113. enum
  3114. {
  3115. kLfsrN_RcId,
  3116. kMlsCoeff0RcId,
  3117. kMlsCoeff1RcId,
  3118. kSmpPerChipRcId,
  3119. kRcosBetaRcId,
  3120. kRcosOS_RcId,
  3121. kCarrierHzRcId,
  3122. kAtkDcyMsRcId,
  3123. kPhatAlphaRcId,
  3124. kPhatMultRcId,
  3125. kInRcId,
  3126. kOutRcId,
  3127. };
  3128. cmDspClass_t _cmReflectCalcDC;
  3129. typedef struct
  3130. {
  3131. cmDspInst_t inst;
  3132. cmReflectCalc_t* r;
  3133. } cmDspReflectCalc_t;
  3134. cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3135. {
  3136. // if( va_cnt !=3 )
  3137. // {
  3138. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array.");
  3139. // return NULL;
  3140. // }
  3141. cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3142. 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length",
  3143. 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3144. 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3145. 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.",
  3146. 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta",
  3147. 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.",
  3148. 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.",
  3149. 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.",
  3150. 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.",
  3151. 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.",
  3152. 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input",
  3153. 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  3154. 0 );
  3155. p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 );
  3156. cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8);
  3157. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e);
  3158. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96);
  3159. cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64);
  3160. cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5);
  3161. cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4);
  3162. cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0);
  3163. cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0);
  3164. cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5);
  3165. cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 1);
  3166. return &p->inst;
  3167. }
  3168. cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3169. {
  3170. cmDspRC_t rc = kOkDspRC;
  3171. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3172. cmGoldSigArg_t gsa;
  3173. gsa.chN = 1;
  3174. gsa.srate = cmDspSampleRate(ctx);
  3175. gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId);
  3176. gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId);
  3177. gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId);
  3178. gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId);
  3179. gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId);
  3180. gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId);
  3181. gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId);
  3182. gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId);
  3183. double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId);
  3184. unsigned phatMult = cmDspUInt(inst,kPhatMultRcId);
  3185. if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC )
  3186. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector.");
  3187. return rc;
  3188. }
  3189. cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3190. {
  3191. cmDspRC_t rc = kOkDspRC;
  3192. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3193. cmReflectCalcFree(&p->r);
  3194. return rc;
  3195. }
  3196. cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3197. {
  3198. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3199. cmDspApplyAllDefaults(ctx,inst);
  3200. return _cmDspReflectCalcSetup(ctx, inst );
  3201. }
  3202. cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3203. {
  3204. cmDspRC_t rc = kOkDspRC;
  3205. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3206. const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0);
  3207. unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0);
  3208. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0);
  3209. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0);
  3210. if( xV != NULL && yV != NULL )
  3211. {
  3212. assert( xN == yN );
  3213. cmReflectCalcExec(p->r,xV,yV,xN);
  3214. }
  3215. return rc;
  3216. }
  3217. cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3218. {
  3219. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3220. cmDspSetEvent(ctx,inst,evt);
  3221. return kOkDspRC;
  3222. }
  3223. cmDspClass_t* cmReflectCalcClassCons( cmDspCtx_t* ctx )
  3224. {
  3225. cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc",
  3226. NULL,
  3227. _cmDspReflectCalcAlloc,
  3228. _cmDspReflectCalcFree,
  3229. _cmDspReflectCalcReset,
  3230. _cmDspReflectCalcExec,
  3231. _cmDspReflectCalcRecv,
  3232. NULL,
  3233. NULL,
  3234. "Reflecttion time calculator");
  3235. return &_cmReflectCalcDC;
  3236. }
  3237. //------------------------------------------------------------------------------------------------------------
  3238. //)
  3239. //( { label:cmDspEchoCancel file_desc:"Normalized least mean squares echo canceller." kw:[sunit] }
  3240. enum
  3241. {
  3242. kMuEcId,
  3243. kImpRespN_EcId,
  3244. kDelayN_EcId,
  3245. kBypassEcId,
  3246. kUnfiltInEcId,
  3247. kFiltInEcId,
  3248. kOutEcId
  3249. };
  3250. cmDspClass_t _cmEchoCancelDC;
  3251. typedef struct
  3252. {
  3253. cmDspInst_t inst;
  3254. cmNlmsEc_t* r;
  3255. } cmDspEchoCancel_t;
  3256. cmDspInst_t* _cmDspEchoCancelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3257. {
  3258. // if( va_cnt !=3 )
  3259. // {
  3260. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'EchoCancel' constructor must have two arguments: a channel count and frequency array.");
  3261. // return NULL;
  3262. // }
  3263. cmDspEchoCancel_t* p = cmDspInstAllocV(cmDspEchoCancel_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3264. 1, "mu", kMuEcId, 0,0, kInDsvFl | kDoubleDsvFl, "NLSM mu coefficient.",
  3265. 1, "irN", kImpRespN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Filter impulse response length in samples.",
  3266. 1, "delayN", kDelayN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Fixed feedback delay in samples.",
  3267. 1, "bypass", kBypassEcId, 0,0, kInDsvFl | kBoolDsvFl, "Bypass enable flag.",
  3268. 1, "uf_in", kUnfiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Unfiltered audio input",
  3269. 1, "f_in", kFiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Filtered audio input",
  3270. 1, "out", kOutEcId, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3271. 0 );
  3272. p->r = cmNlmsEcAlloc(ctx->cmProcCtx, NULL, 0, 0,0,0 );
  3273. cmDspSetDefaultDouble( ctx, &p->inst, kMuEcId, 0, 0.1);
  3274. cmDspSetDefaultUInt( ctx, &p->inst, kImpRespN_EcId, 0, 2048);
  3275. cmDspSetDefaultUInt( ctx, &p->inst, kDelayN_EcId, 0, 1765);
  3276. cmDspSetDefaultBool( ctx, &p->inst, kBypassEcId, 0, false);
  3277. return &p->inst;
  3278. }
  3279. cmDspRC_t _cmDspEchoCancelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3280. {
  3281. cmDspRC_t rc = kOkDspRC;
  3282. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3283. double mu = cmDspDouble(inst,kMuEcId);
  3284. unsigned hN = cmDspUInt(inst,kImpRespN_EcId);
  3285. unsigned delayN = cmDspUInt(inst,kDelayN_EcId);
  3286. if( cmNlmsEcInit(p->r,cmDspSampleRate(ctx),mu,hN,delayN) != cmOkRC )
  3287. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal echo canceller.");
  3288. return rc;
  3289. }
  3290. cmDspRC_t _cmDspEchoCancelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3291. {
  3292. cmDspRC_t rc = kOkDspRC;
  3293. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3294. cmNlmsEcWrite(p->r, "/Users/kevin/temp/kc");
  3295. cmNlmsEcFree(&p->r);
  3296. return rc;
  3297. }
  3298. cmDspRC_t _cmDspEchoCancelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3299. {
  3300. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3301. cmDspApplyAllDefaults(ctx,inst);
  3302. return _cmDspEchoCancelSetup(ctx, inst );
  3303. }
  3304. cmDspRC_t _cmDspEchoCancelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3305. {
  3306. cmDspRC_t rc = kOkDspRC;
  3307. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3308. bool bypassFl = true; //cmDspBool(inst,kBypassEcId);
  3309. const cmSample_t* fV = cmDspAudioBuf(ctx,inst,kFiltInEcId,0);
  3310. unsigned fN = cmDspAudioBufSmpCount(ctx,inst,kFiltInEcId,0);
  3311. const cmSample_t* uV = cmDspAudioBuf(ctx,inst,kUnfiltInEcId,0);
  3312. unsigned uN = cmDspAudioBufSmpCount(ctx,inst,kUnfiltInEcId,0);
  3313. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutEcId,0);
  3314. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutEcId,0);
  3315. assert( fN==uN && fN==yN );
  3316. if( bypassFl )
  3317. {
  3318. cmVOS_Copy(yV,yN,uV);
  3319. }
  3320. else
  3321. {
  3322. if( fV !=NULL && uV != NULL && yV != NULL )
  3323. {
  3324. assert( uN == yN && fN == yN );
  3325. cmNlmsEcExec(p->r,uV,fV,yV,yN);
  3326. }
  3327. }
  3328. return rc;
  3329. }
  3330. cmDspRC_t _cmDspEchoCancelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3331. {
  3332. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3333. cmDspSetEvent(ctx,inst,evt);
  3334. if( p->r != NULL )
  3335. {
  3336. switch(evt->dstVarId)
  3337. {
  3338. case kMuEcId:
  3339. cmNlmsEcSetMu( p->r, cmDspReal(inst,kMuEcId));
  3340. break;
  3341. case kImpRespN_EcId:
  3342. cmNlmsEcSetIrN( p->r, cmDspUInt(inst,kImpRespN_EcId));
  3343. break;
  3344. case kDelayN_EcId:
  3345. cmNlmsEcSetDelayN( p->r, cmDspUInt(inst,kDelayN_EcId));
  3346. break;
  3347. case kBypassEcId:
  3348. printf("EC bypass:%i\n",cmDspBool(inst,kBypassEcId));
  3349. break;
  3350. }
  3351. }
  3352. printf("mu:%f dN:%i hN:%i\n",p->r->mu,p->r->delayN,p->r->hN);
  3353. return kOkDspRC;
  3354. }
  3355. cmDspClass_t* cmEchoCancelClassCons( cmDspCtx_t* ctx )
  3356. {
  3357. cmDspClassSetup(&_cmEchoCancelDC,ctx,"EchoCancel",
  3358. NULL,
  3359. _cmDspEchoCancelAlloc,
  3360. _cmDspEchoCancelFree,
  3361. _cmDspEchoCancelReset,
  3362. _cmDspEchoCancelExec,
  3363. _cmDspEchoCancelRecv,
  3364. NULL,
  3365. NULL,
  3366. "Echo canceller");
  3367. return &_cmEchoCancelDC;
  3368. }
  3369. //)