libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmDspKr.c 119KB

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