libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmDspKr.c 119KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973
  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. kPostMdId
  1010. };
  1011. cmDspClass_t _cmModulatorDC;
  1012. typedef struct
  1013. {
  1014. cmDspInst_t inst;
  1015. cmScModulator* mp;
  1016. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  1017. cmChar_t* fn;
  1018. cmChar_t* modLabel;
  1019. unsigned onSymId;
  1020. unsigned offSymId;
  1021. unsigned postSymId;
  1022. unsigned dumpSymId;
  1023. } cmDspScMod_t;
  1024. void _cmDspScModCb( void* arg, unsigned varSymId, double value, bool postFl )
  1025. {
  1026. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  1027. cmDspVar_t* varPtr;
  1028. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  1029. return;
  1030. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  1031. if( postFl )
  1032. cmDspSetSymbol(p->tmp_ctx,&p->inst,kPostMdId,p->postSymId);
  1033. }
  1034. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1035. {
  1036. va_list vl1;
  1037. va_copy(vl1,vl);
  1038. cmDspVarArg_t args[] =
  1039. {
  1040. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  1041. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  1042. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  1043. { "post", kPostMdId, 0,0, kOutDsvFl | kSymDsvFl, "Sends 'post' symbol after a message transmission if the 'post' flag is set in scMod."},
  1044. { NULL, 0, 0, 0, 0 }
  1045. };
  1046. // validate the argument count
  1047. if( va_cnt != 2 )
  1048. {
  1049. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  1050. return NULL;
  1051. }
  1052. // read the modulator file and label strings
  1053. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  1054. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  1055. va_end(vl1);
  1056. // validate the file
  1057. if( fn==NULL || cmFsIsFile(fn)==false )
  1058. {
  1059. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  1060. return NULL;
  1061. }
  1062. // allocate the internal modulator object
  1063. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  1064. if(mp == NULL )
  1065. {
  1066. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  1067. return NULL;
  1068. }
  1069. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  1070. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  1071. cmDspVarArg_t a[ argCnt+1 ];
  1072. unsigned i;
  1073. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1074. for(i=fixArgCnt; i<argCnt; ++i)
  1075. {
  1076. unsigned varIdx = i - fixArgCnt;
  1077. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  1078. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  1079. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  1080. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  1081. }
  1082. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1083. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1084. p->fn = cmMemAllocStr(fn);
  1085. p->modLabel = cmMemAllocStr(modLabel);
  1086. p->mp = mp;
  1087. p->onSymId = cmSymTblId(ctx->stH,"on");
  1088. p->offSymId = cmSymTblId(ctx->stH,"off");
  1089. p->postSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"post");
  1090. p->dumpSymId = cmSymTblId(ctx->stH,"dump");
  1091. mp->cbArg = p; // set the modulator callback arg
  1092. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1093. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1094. return &p->inst;
  1095. }
  1096. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1097. {
  1098. cmDspRC_t rc = kOkDspRC;
  1099. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1100. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1101. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1102. cmMemFree(p->fn);
  1103. cmMemFree(p->modLabel);
  1104. return rc;
  1105. }
  1106. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1107. {
  1108. cmDspRC_t rc = kOkDspRC;
  1109. cmDspApplyAllDefaults(ctx,inst);
  1110. return rc;
  1111. }
  1112. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1113. {
  1114. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1115. cmDspSetEvent(ctx,inst,evt);
  1116. switch( evt->dstVarId )
  1117. {
  1118. case kResetIdxMdId:
  1119. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1120. break;
  1121. case kCmdMdId:
  1122. {
  1123. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1124. if( symId == p->onSymId )
  1125. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1126. if( symId == p->dumpSymId )
  1127. cmScModulatorDump(p->mp);
  1128. }
  1129. break;
  1130. }
  1131. return kOkDspRC;
  1132. }
  1133. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1134. {
  1135. cmDspRC_t rc = kOkDspRC;
  1136. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1137. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1138. {
  1139. p->tmp_ctx = ctx;
  1140. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1141. }
  1142. return rc;
  1143. }
  1144. cmDspClass_t* cmScModClassCons( cmDspCtx_t* ctx )
  1145. {
  1146. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1147. NULL,
  1148. _cmDspScModAlloc,
  1149. _cmDspScModFree,
  1150. _cmDspScModReset,
  1151. _cmDspScModExec,
  1152. _cmDspScModRecv,
  1153. NULL,NULL,
  1154. "Score Driven Variable Modulator.");
  1155. return &_cmModulatorDC;
  1156. }
  1157. //------------------------------------------------------------------------------------------------------------
  1158. //)
  1159. //( { label:cmDspGSwitch file_desc:"Route all inputs to one of a group of outputs." kw:[sunit] }
  1160. enum
  1161. {
  1162. kInChCntGsId,
  1163. kOutGroupCntGsId,
  1164. kGroupSelIdxGsId,
  1165. kBaseInFloatGsId
  1166. };
  1167. cmDspClass_t _cmGSwitchDC;
  1168. typedef struct
  1169. {
  1170. cmDspInst_t inst;
  1171. unsigned iChCnt;
  1172. unsigned oGroupCnt;
  1173. unsigned baseInFloatGsId;
  1174. unsigned baseInSymGsId;
  1175. unsigned baseInBoolGsId;
  1176. unsigned baseOutFloatGsId;
  1177. unsigned baseOutSymGsId;
  1178. unsigned baseOutBoolGsId;
  1179. } cmDspGSwitch_t;
  1180. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1181. {
  1182. va_list vl1;
  1183. va_copy(vl1,vl);
  1184. cmDspVarArg_t args[] =
  1185. {
  1186. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1187. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1188. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1189. { NULL, 0, 0, 0, 0 }
  1190. };
  1191. // validate the argument count
  1192. if( va_cnt != 2 )
  1193. {
  1194. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1195. return NULL;
  1196. }
  1197. // read the input ch and output group count
  1198. unsigned iChCnt = va_arg(vl1,unsigned);
  1199. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1200. va_end(vl1);
  1201. // validate the channel counts
  1202. if( iChCnt == 0 || oGroupCnt==0 )
  1203. {
  1204. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1205. return NULL;
  1206. }
  1207. unsigned typeCnt = 3; // i.e. float,sym,bool
  1208. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1209. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1210. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1211. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1212. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1213. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1214. unsigned fixArgCnt = 3;
  1215. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1216. unsigned argCnt = fixArgCnt + varArgCnt;
  1217. cmDspVarArg_t a[ argCnt+1 ];
  1218. unsigned i;
  1219. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1220. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1221. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1222. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1223. unsigned labelCharCnt = 63;
  1224. cmChar_t label[labelCharCnt+1];
  1225. label[labelCharCnt] = 0;
  1226. unsigned gsid = baseOutFloatGsId;
  1227. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1228. {
  1229. snprintf(label,labelCharCnt,"f-out-%i",i);
  1230. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1231. }
  1232. gsid = baseOutSymGsId;
  1233. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1234. {
  1235. snprintf(label,labelCharCnt,"s-out-%i",i);
  1236. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1237. }
  1238. gsid = baseOutBoolGsId;
  1239. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1240. {
  1241. snprintf(label,labelCharCnt,"b-out-%i",i);
  1242. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1243. }
  1244. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1245. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1246. p->iChCnt = iChCnt;
  1247. p->oGroupCnt = oGroupCnt;
  1248. p->baseInFloatGsId = baseInFloatGsId;
  1249. p->baseInSymGsId = baseInSymGsId;
  1250. p->baseInBoolGsId = baseInBoolGsId;
  1251. p->baseOutFloatGsId = baseOutFloatGsId;
  1252. p->baseOutSymGsId = baseOutSymGsId;
  1253. p->baseOutBoolGsId = baseOutBoolGsId;
  1254. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1255. return &p->inst;
  1256. }
  1257. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1258. {
  1259. cmDspRC_t rc = kOkDspRC;
  1260. cmDspApplyAllDefaults(ctx,inst);
  1261. return rc;
  1262. }
  1263. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1264. {
  1265. cmDspRC_t rc = kOkDspRC;
  1266. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1267. // if this is the group selector
  1268. if( evt->dstVarId == kGroupSelIdxGsId )
  1269. {
  1270. unsigned idx;
  1271. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1272. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1273. else
  1274. cmDspSetEvent(ctx,inst,evt);
  1275. return rc;
  1276. }
  1277. // get the group selector
  1278. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1279. assert( groupIdx < p->oGroupCnt);
  1280. // if this is a float input
  1281. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1282. {
  1283. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1284. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1285. return rc;
  1286. }
  1287. // if this is a symbol input
  1288. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1289. {
  1290. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1291. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1292. return rc;
  1293. }
  1294. // if this is a bool input
  1295. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1296. {
  1297. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1298. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1299. return rc;
  1300. }
  1301. return rc;
  1302. }
  1303. cmDspClass_t* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1304. {
  1305. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1306. NULL,
  1307. _cmDspGSwitchAlloc,
  1308. NULL,
  1309. _cmDspGSwitchReset,
  1310. NULL,
  1311. _cmDspGSwitchRecv,
  1312. NULL,NULL,
  1313. "Ganged switch.");
  1314. return &_cmGSwitchDC;
  1315. }
  1316. //------------------------------------------------------------------------------------------------------------
  1317. //)
  1318. //( { label:cmDspScaleRange file_desc:"Offset and scale a scalar value." kw:[sunit] }
  1319. enum
  1320. {
  1321. kMinInSrId,
  1322. kMaxInSrId,
  1323. kMinOutSrId,
  1324. kMaxOutSrId,
  1325. kValInSrId,
  1326. kValOutSrId,
  1327. };
  1328. cmDspClass_t _cmScaleRangeDC;
  1329. typedef struct
  1330. {
  1331. cmDspInst_t inst;
  1332. } cmDspScaleRange_t;
  1333. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1334. {
  1335. va_list vl1;
  1336. va_copy(vl1,vl);
  1337. cmDspVarArg_t args[] =
  1338. {
  1339. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1340. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1341. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1342. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1343. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1344. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1345. { NULL, 0, 0, 0, 0 }
  1346. };
  1347. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1348. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1349. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1350. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1351. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1352. return &p->inst;
  1353. }
  1354. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1355. {
  1356. cmDspRC_t rc = kOkDspRC;
  1357. cmDspApplyAllDefaults(ctx,inst);
  1358. return rc;
  1359. }
  1360. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1361. {
  1362. cmDspRC_t rc = kOkDspRC;
  1363. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1364. cmDspSetEvent(ctx,inst,evt);
  1365. if( evt->dstVarId == kValInSrId )
  1366. {
  1367. double val = cmDspDouble(inst,kValInSrId);
  1368. double min_in = cmDspDouble(inst,kMinInSrId);
  1369. double max_in = cmDspDouble(inst,kMaxInSrId);
  1370. double min_out = cmDspDouble(inst,kMinOutSrId);
  1371. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1372. double x = cmMax(min_in,cmMin(max_in,val));
  1373. x = (x - min_in)/(max_in - min_in);
  1374. x = min_out + x * (max_out - min_out);
  1375. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1376. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1377. }
  1378. return rc;
  1379. }
  1380. cmDspClass_t* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1381. {
  1382. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1383. NULL,
  1384. _cmDspScaleRangeAlloc,
  1385. NULL,
  1386. _cmDspScaleRangeReset,
  1387. NULL,
  1388. _cmDspScaleRangeRecv,
  1389. NULL,NULL,
  1390. "Scale a value inside an input range to a value in the output range.");
  1391. return &_cmScaleRangeDC;
  1392. }
  1393. //------------------------------------------------------------------------------------------------------------
  1394. //)
  1395. //( { label:cmDspActiveMeas file_desc:"Issue stored parameter values at specified score locations." kw:[sunit] }
  1396. enum
  1397. {
  1398. kCntAmId,
  1399. kSflocAmId,
  1400. kLocAmId,
  1401. kTypeAmId,
  1402. kValueAmId,
  1403. kCstAmId,
  1404. kCmdAmId,
  1405. kScLocAmId,
  1406. kEvenAmId,
  1407. kDynAmId,
  1408. kTempoAmId,
  1409. kCostAmId
  1410. };
  1411. cmDspClass_t _cmActiveMeasDC;
  1412. typedef struct cmDspAmRecd_str
  1413. {
  1414. unsigned loc;
  1415. unsigned type;
  1416. double value;
  1417. double cost;
  1418. struct cmDspAmRecd_str* link;
  1419. } cmDspAmRecd_t;
  1420. //int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1421. //{
  1422. // return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1423. //}
  1424. typedef struct
  1425. {
  1426. cmDspInst_t inst;
  1427. unsigned addSymId;
  1428. unsigned clearSymId;
  1429. unsigned printSymId;
  1430. unsigned rewindSymId;
  1431. cmDspAmRecd_t* array; // array[cnt]
  1432. unsigned cnt;
  1433. cmDspAmRecd_t* list; // first recd in list sorted on 'loc'.
  1434. cmDspAmRecd_t* avail; // next empty recd
  1435. cmDspAmRecd_t* sent; // last recd sent
  1436. } cmDspActiveMeas_t;
  1437. void _cmDspAmAllocList( cmDspActiveMeas_t* p, unsigned cnt )
  1438. {
  1439. assert(p->array == NULL );
  1440. cmDspAmRecd_t* r = cmMemAllocZ(cmDspAmRecd_t,cnt);
  1441. p->cnt = cnt;
  1442. p->array = r;
  1443. p->list = NULL;
  1444. p->avail = r;
  1445. p->sent = NULL;
  1446. }
  1447. cmDspRC_t _cmDspActiveMeasAdd( cmDspCtx_t* ctx, cmDspActiveMeas_t* p, unsigned loc, unsigned type, double value, double cost)
  1448. {
  1449. assert( type != kInvalidVarScId );
  1450. cmDspAmRecd_t* rp = p->list;
  1451. cmDspAmRecd_t* pp = NULL;
  1452. // search for the location to add the new record
  1453. for(; rp!=NULL; rp=rp->link)
  1454. {
  1455. // if this loc and type already exists then replace the value and cost fields
  1456. if( rp->loc==loc && rp->type==type )
  1457. goto foundLabel;
  1458. // if this loc should be inserted before rp
  1459. if( loc < rp->loc )
  1460. break;
  1461. pp = rp;
  1462. }
  1463. // if the pre-allocated list is full
  1464. if( p->avail >= p->array+p->cnt )
  1465. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"Unable to store new measurement record. All preallocated active measurement slots are in use.");
  1466. // if prepending to the list
  1467. if( pp == NULL )
  1468. {
  1469. rp = p->avail;
  1470. rp->link = p->list;
  1471. p->list = rp;
  1472. }
  1473. else
  1474. {
  1475. // if appending to the list after pp
  1476. if( rp == NULL )
  1477. {
  1478. // nothing to do
  1479. }
  1480. else // if inserting between pp and rp
  1481. {
  1482. p->avail->link = rp;
  1483. }
  1484. rp = p->avail;
  1485. pp->link = rp;
  1486. }
  1487. p->avail += 1;
  1488. foundLabel:
  1489. rp->loc = loc;
  1490. rp->type = type;
  1491. rp->value = value;
  1492. rp->cost = cost;
  1493. return kOkDspRC;
  1494. }
  1495. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1496. {
  1497. cmDspVarArg_t args[] =
  1498. {
  1499. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1500. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1501. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1502. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type. (even,dyn,...)" },
  1503. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1504. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1505. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1506. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1507. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1508. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1509. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1510. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1511. { NULL, 0, 0, 0, 0 }
  1512. };
  1513. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1514. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1515. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1516. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1517. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1518. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,256);
  1519. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1520. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1521. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1522. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1523. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1524. return &p->inst;
  1525. }
  1526. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1527. {
  1528. cmDspAmRecd_t* rp = p->list;
  1529. for(; rp!=NULL; rp=rp->link)
  1530. {
  1531. const cmChar_t* label = "<null>";
  1532. switch( rp->type )
  1533. {
  1534. case kEvenVarScId: label="even "; break;
  1535. case kDynVarScId: label="dyn "; break;
  1536. case kTempoVarScId: label="tempo"; break;
  1537. default:
  1538. { assert(0); }
  1539. }
  1540. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",rp->loc,label,rp->value,rp->cost);
  1541. }
  1542. return kOkDspRC;
  1543. }
  1544. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1545. {
  1546. memset(p->array,0,sizeof(p->array[0])*p->cnt);
  1547. p->avail = p->array;
  1548. p->list = NULL;
  1549. p->avail = p->array;
  1550. return kOkDspRC;
  1551. }
  1552. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1553. {
  1554. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1555. cmMemPtrFree(&p->array);
  1556. return kOkDspRC;
  1557. }
  1558. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1559. {
  1560. cmDspRC_t rc = kOkDspRC;
  1561. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1562. cmDspApplyAllDefaults(ctx,inst);
  1563. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1564. _cmDspActiveMeasFree(ctx,inst,evt);
  1565. _cmDspAmAllocList(p,cnt);
  1566. return rc;
  1567. }
  1568. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1569. {
  1570. cmDspRC_t rc = kOkDspRC;
  1571. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1572. cmDspSetEvent(ctx,inst,evt);
  1573. switch( evt->dstVarId )
  1574. {
  1575. case kSflocAmId:
  1576. {
  1577. unsigned sfloc = cmDspUInt(inst,kSflocAmId); // get the recv'd score location
  1578. cmDspAmRecd_t* rp = p->sent==NULL ? p->list : p->sent->link; // get the next recd to send
  1579. bool fl = false;
  1580. for(; rp!=NULL; rp=rp->link)
  1581. if( rp->loc <= sfloc )
  1582. {
  1583. // deterimine the records type
  1584. unsigned varId = cmInvalidId;
  1585. switch( rp->type )
  1586. {
  1587. case kEvenVarScId: varId = kEvenAmId; break;
  1588. case kDynVarScId: varId = kDynAmId; break;
  1589. case kTempoVarScId: varId = kTempoAmId; break;
  1590. default:
  1591. { assert(0); }
  1592. }
  1593. // Sending the location triggers the avail-ch to switch - so the location should only
  1594. // be sent once.
  1595. if( !fl )
  1596. {
  1597. cmDspSetUInt(ctx,inst,kScLocAmId,rp->loc);
  1598. fl = true;
  1599. }
  1600. // transmit the records value and cost
  1601. cmDspSetDouble(ctx,inst,varId,rp->value);
  1602. cmDspSetDouble(ctx,inst,kCostAmId,rp->cost);
  1603. p->sent = rp;
  1604. }
  1605. }
  1606. break;
  1607. case kCmdAmId:
  1608. {
  1609. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1610. if( cmdSymId == p->addSymId )
  1611. rc = _cmDspActiveMeasAdd(ctx,p,cmDspUInt(inst,kLocAmId),cmDspUInt(inst,kTypeAmId),cmDspDouble(inst,kValueAmId),cmDspDouble(inst,kCstAmId));
  1612. else
  1613. if( cmdSymId == p->clearSymId )
  1614. rc = _cmDspActiveMeasClear(ctx,p);
  1615. else
  1616. if( cmdSymId == p->printSymId )
  1617. rc = _cmDspActiveMeasPrint(ctx,p);
  1618. else
  1619. if(cmdSymId == p->rewindSymId )
  1620. p->sent = NULL;
  1621. }
  1622. break;
  1623. }
  1624. // switch( evt->dstVarId )
  1625. // {
  1626. // case kSflocAmId:
  1627. // if( p->nextFullIdx != cmInvalidIdx )
  1628. // {
  1629. // // get the recv'd score location
  1630. // unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1631. //
  1632. // unsigned prvLoc = cmInvalidIdx;
  1633. //
  1634. // // for each remaining avail record
  1635. // for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1636. // {
  1637. // cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1638. //
  1639. // // if this records score location is after the recv'd score loc then we're done
  1640. // if( r->loc > sflocIdx )
  1641. // break;
  1642. //
  1643. // // deterimine the records type
  1644. // unsigned varId = cmInvalidId;
  1645. // switch( r->type )
  1646. // {
  1647. // case kEvenVarScId: varId = kEvenAmId; break;
  1648. // case kDynVarScId: varId = kDynAmId; break;
  1649. // case kTempoVarScId: varId = kTempoAmId; break;
  1650. // default:
  1651. // { assert(0); }
  1652. // }
  1653. //
  1654. // // if this score location has not yet been sent then send it now
  1655. // if( prvLoc != r->loc )
  1656. // cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1657. //
  1658. // // transmit the records value and cost
  1659. // cmDspSetDouble(ctx,inst,varId,r->value);
  1660. // cmDspSetDouble(ctx,inst,kCostAmId,r->cost);
  1661. //
  1662. // prvLoc = r->loc;
  1663. // }
  1664. //
  1665. //
  1666. // }
  1667. // break;
  1668. //
  1669. // case kCmdAmId:
  1670. // {
  1671. // unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1672. //
  1673. // if( cmdSymId == p->addSymId )
  1674. // {
  1675. // if( p->nextEmptyIdx >= p->cnt )
  1676. // cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1677. // else
  1678. // {
  1679. // cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1680. // r->loc = cmDspUInt( inst,kLocAmId);
  1681. // r->type = cmDspUInt( inst,kTypeAmId);
  1682. // r->value = cmDspDouble(inst,kValueAmId);
  1683. // r->cost = cmDspDouble(inst,kCstAmId);
  1684. // p->nextEmptyIdx += 1;
  1685. //
  1686. // qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1687. //
  1688. // if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1689. // p->nextFullIdx = 0;
  1690. //
  1691. // }
  1692. // }
  1693. //
  1694. // if( cmdSymId == p->clearSymId )
  1695. // rc = _cmDspActiveMeasClear(ctx,p);
  1696. // else
  1697. // if( cmdSymId == p->printSymId )
  1698. // rc = _cmDspActiveMeasPrint(ctx,p);
  1699. // else
  1700. // if(cmdSymId == p->rewindSymId )
  1701. // p->nextFullIdx = 0;
  1702. // }
  1703. // break;
  1704. //
  1705. // }
  1706. return rc;
  1707. }
  1708. cmDspClass_t* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1709. {
  1710. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1711. NULL,
  1712. _cmDspActiveMeasAlloc,
  1713. _cmDspActiveMeasFree,
  1714. _cmDspActiveMeasReset,
  1715. NULL,
  1716. _cmDspActiveMeasRecv,
  1717. NULL,NULL,
  1718. "Scale a value inside an input range to a value in the output range.");
  1719. return &_cmActiveMeasDC;
  1720. }
  1721. //------------------------------------------------------------------------------------------------------------
  1722. //)
  1723. //( { label:cmDspAmSync file_desc:"Calculate MIDI to Audio latency offsets." kw:[sunit] }
  1724. //
  1725. //
  1726. // Usage:
  1727. // 1) In the program resource file setup a list of sync points.
  1728. // 'asmp' refers to a sample offset into the audio file 'af'
  1729. // which should match to the midi event index 'mid' in the
  1730. // midi file 'mf'.
  1731. //
  1732. // amSync :
  1733. // [
  1734. // { af:"af-16" asmp:34735276 mf:"mf-10" mid:350 }
  1735. // { af:"af-16" asmp:71802194 mf:"mf-10" mid:787 }
  1736. // ]
  1737. //
  1738. // 2) Feed the 'fidx' output from a wave table loaded with 'af' into the 'asmp' input port of this amSync object.
  1739. // Feed the 'id' output from the MIDI file player loaded with 'mf' into the 'mid' input port of this amSync object.
  1740. //
  1741. // 3) Run the players.
  1742. // 4) When the run is complete send any message to the 'sel' port of this amSync object.
  1743. // The 'frm:' field of the printed output gives the difference in samples between
  1744. // MIDI and audio sync points.
  1745. //
  1746. // If the value is positive then the MIDI point is after the Audio point.
  1747. // If the value is negative then the MIDI point is before the audio point.
  1748. //
  1749. enum
  1750. {
  1751. kSelAmId,
  1752. kAFnAmId,
  1753. kASmpAmId,
  1754. kMFnAmId,
  1755. kMIdAmId,
  1756. };
  1757. enum
  1758. {
  1759. kAfnAmFl = 0x01,
  1760. kMfnAmFl = 0x02,
  1761. kAsmpAmFl = 0x04,
  1762. kMidAmFl = 0x08,
  1763. };
  1764. cmDspClass_t _cmAmSyncDC;
  1765. typedef struct cmDspAmSyncEntry_str
  1766. {
  1767. const cmChar_t* afn; // audio file name
  1768. const cmChar_t* mfn; // midi file name
  1769. unsigned asmp; // Audio sample index to sync to MIDI event
  1770. unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
  1771. int afi; // closest DSP system cycle index to the reference audio sample index (asmp).
  1772. int mfi; // DSP system cycle on which the reference MIDI event (mid) arrived.
  1773. unsigned state; // as incoming msg match this record the state is updated with kXXXAmFl flags
  1774. } cmDspAmSyncEntry_t;
  1775. typedef struct
  1776. {
  1777. cmDspInst_t inst;
  1778. cmDspAmSyncEntry_t* array;
  1779. unsigned arrayCnt;
  1780. cmDspAmSyncEntry_t* acur;
  1781. cmDspAmSyncEntry_t* mcur;
  1782. } cmDspAmSync_t;
  1783. cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1784. {
  1785. cmDspVarArg_t args[] =
  1786. {
  1787. { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
  1788. { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
  1789. { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
  1790. { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
  1791. { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
  1792. { NULL, 0, 0, 0, 0 }
  1793. };
  1794. cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1795. return &p->inst;
  1796. }
  1797. cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1798. {
  1799. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1800. cmMemFree(p->array);
  1801. return kOkDspRC;
  1802. }
  1803. cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1804. {
  1805. cmDspRC_t rc = kOkDspRC;
  1806. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1807. cmDspApplyAllDefaults(ctx,inst);
  1808. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  1809. cmJsonNode_t* np;
  1810. const cmChar_t* errLabelPtr;
  1811. unsigned i;
  1812. cmJsRC_t jsRC;
  1813. if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
  1814. {
  1815. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
  1816. goto errLabel;
  1817. }
  1818. p->arrayCnt = cmJsonChildCount(np);
  1819. p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
  1820. for(i=0; i<p->arrayCnt; ++i)
  1821. {
  1822. cmJsonNode_t* anp = cmJsonArrayElement(np,i);
  1823. cmDspAmSyncEntry_t* r = p->array + i;
  1824. if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
  1825. "af", kStringTId,&r->afn,
  1826. "asmp",kIntTId, &r->asmp,
  1827. "mf", kStringTId,&r->mfn,
  1828. "mid", kIntTId, &r->mid,
  1829. NULL)) != kOkJsRC )
  1830. {
  1831. if( jsRC == kNodeNotFoundJsRC )
  1832. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
  1833. else
  1834. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
  1835. break;
  1836. }
  1837. r->afi = cmInvalidIdx;
  1838. r->mfi = cmInvalidIdx;
  1839. r->state = 0;
  1840. }
  1841. errLabel:
  1842. return rc;
  1843. }
  1844. cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1845. {
  1846. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1847. unsigned i;
  1848. if( evt->dstVarId != kSelAmId )
  1849. cmDspSetEvent(ctx,inst,evt);
  1850. switch(evt->dstVarId)
  1851. {
  1852. case kSelAmId:
  1853. {
  1854. double srate = cmDspSysSampleRate(ctx->dspH);
  1855. int fpc = cmDspSamplesPerCycle(ctx);
  1856. for(i=0; i<p->arrayCnt; ++i)
  1857. {
  1858. cmDspAmSyncEntry_t* r = p->array + i;
  1859. int dframes = r->mfi-r->afi;
  1860. cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
  1861. r->state,
  1862. r->afn,
  1863. r->asmp,
  1864. r->afi,
  1865. r->mfn,
  1866. r->mid,
  1867. r->mfi,
  1868. dframes,
  1869. dframes*fpc,dframes*fpc/srate);
  1870. r->afi = cmInvalidIdx;
  1871. r->mfi = cmInvalidIdx;
  1872. r->state = 0;
  1873. }
  1874. p->acur = NULL;
  1875. p->mcur = NULL;
  1876. }
  1877. break;
  1878. case kAFnAmId:
  1879. {
  1880. // an audio file name just arrived - set p->acur to point to it
  1881. const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
  1882. for(i=0; i<p->arrayCnt; ++i)
  1883. if( strcmp(fn,p->array[i].afn) == 0 )
  1884. {
  1885. p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
  1886. p->acur = p->array + i;
  1887. }
  1888. }
  1889. break;
  1890. case kMFnAmId:
  1891. {
  1892. // a midi file name just arrived - set p->mcur to point to it
  1893. const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
  1894. for(i=0; i<p->arrayCnt; ++i)
  1895. if( strcmp(fn,p->array[i].mfn) == 0 )
  1896. {
  1897. p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
  1898. p->mcur = p->array + i;
  1899. }
  1900. }
  1901. break;
  1902. case kASmpAmId:
  1903. {
  1904. // a audio file sample index has just arrived
  1905. int v = cmDspInt(inst,kASmpAmId);
  1906. // if a valid audio file has been set
  1907. if( p->acur != NULL )
  1908. for(i=0; i<p->arrayCnt; ++i)
  1909. {
  1910. // if the audio sync point is before or on the new audio file sample index then
  1911. // this is the closest audio file index to the audio sync point - record the
  1912. // associated cycleCnt
  1913. cmDspAmSyncEntry_t* r = p->array + i;
  1914. if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
  1915. {
  1916. r->afi = ctx->cycleCnt;
  1917. r->state = cmSetFlag(r->state,kAsmpAmFl);
  1918. break;
  1919. }
  1920. }
  1921. }
  1922. break;
  1923. case kMIdAmId:
  1924. {
  1925. // a new MIDI event was received
  1926. int v = cmDspInt(inst,kMIdAmId);
  1927. if( p->mcur != NULL )
  1928. for(i=0; i<p->arrayCnt; ++i)
  1929. {
  1930. // if the new MIDI event matched the MIDI sync point then record the
  1931. // current cycleCnt.
  1932. cmDspAmSyncEntry_t* r = p->array + i;
  1933. if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
  1934. {
  1935. r->mfi = ctx->cycleCnt;
  1936. r->state = cmSetFlag(r->state,kMidAmFl);
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. break;
  1942. }
  1943. return kOkDspRC;
  1944. }
  1945. cmDspClass_t* cmAmSyncClassCons( cmDspCtx_t* ctx )
  1946. {
  1947. cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
  1948. NULL,
  1949. _cmDspAmSyncAlloc,
  1950. _cmDspAmSyncFree,
  1951. _cmDspAmSyncReset,
  1952. NULL,
  1953. _cmDspAmSyncRecv,
  1954. NULL,NULL,
  1955. "Audio - MIDI Sync Object.");
  1956. return &_cmAmSyncDC;
  1957. }
  1958. //------------------------------------------------------------------------------------------------------------
  1959. //)
  1960. //( { label:cmDspNanoMap file_desc:"Control a MIDI synth." kw:[sunit] }
  1961. enum
  1962. {
  1963. kPgmNmId,
  1964. kStatusNmId,
  1965. kD0NmId,
  1966. kD1NmId,
  1967. kThruNmId
  1968. };
  1969. cmDspClass_t _cmNanoMapDC;
  1970. typedef struct
  1971. {
  1972. cmDspInst_t inst;
  1973. } cmDspNanoMap_t;
  1974. cmDspRC_t _cmDspNanoMapSend( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned st, unsigned d0, unsigned d1 )
  1975. {
  1976. cmDspSetUInt(ctx,inst,kD1NmId,d1);
  1977. cmDspSetUInt(ctx,inst,kD0NmId,d0);
  1978. cmDspSetUInt(ctx,inst,kStatusNmId,st);
  1979. return kOkDspRC;
  1980. }
  1981. void _cmDspNanoMapPgm( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned pgm )
  1982. {
  1983. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  1984. unsigned i;
  1985. for(i=0; i<kMidiChCnt; ++i)
  1986. {
  1987. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,121,0); // reset all controllers
  1988. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,123,0); // turn all notes off
  1989. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,0,0); // switch to bank 0
  1990. _cmDspNanoMapSend(ctx,inst,kPgmMdId+i,pgm,0); // send pgm change
  1991. cmSleepMs(15);
  1992. }
  1993. }
  1994. cmDspInst_t* _cmDspNanoMapAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1995. {
  1996. cmDspVarArg_t args[] =
  1997. {
  1998. { "pgm", kPgmNmId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reprogram all channels to this pgm." },
  1999. { "status", kStatusNmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  2000. { "d0", kD0NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  2001. { "d1", kD1NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  2002. { "thru", kThruNmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable pass through."},
  2003. { NULL, 0, 0, 0, 0 }
  2004. };
  2005. cmDspNanoMap_t* p = cmDspInstAlloc(cmDspNanoMap_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2006. cmDspSetDefaultUInt(ctx,&p->inst, kPgmNmId, 0, 0 );
  2007. return &p->inst;
  2008. }
  2009. cmDspRC_t _cmDspNanoMapReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2010. {
  2011. cmDspRC_t rc = kOkDspRC;
  2012. cmDspApplyAllDefaults(ctx,inst);
  2013. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2014. return rc;
  2015. }
  2016. cmDspRC_t _cmDspNanoMapRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2017. {
  2018. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  2019. switch( evt->dstVarId )
  2020. {
  2021. case kPgmNmId:
  2022. cmDspSetEvent(ctx,inst,evt);
  2023. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2024. break;
  2025. case kStatusNmId:
  2026. {
  2027. unsigned status = cmDsvGetUInt(evt->valuePtr);
  2028. unsigned stat_no_ch = status & 0xf0;
  2029. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  2030. {
  2031. //unsigned d0 = cmDspUInt(inst,kD0NmId);
  2032. unsigned ch = 0; //d0 % 8;
  2033. status = (status & 0xf0) + ch;
  2034. cmDspSetUInt(ctx,inst,kStatusNmId,status);
  2035. }
  2036. }
  2037. break;
  2038. default:
  2039. cmDspSetEvent(ctx,inst,evt);
  2040. break;
  2041. }
  2042. return kOkDspRC;
  2043. }
  2044. cmDspClass_t* cmNanoMapClassCons( cmDspCtx_t* ctx )
  2045. {
  2046. cmDspClassSetup(&_cmNanoMapDC,ctx,"NanoMap",
  2047. NULL,
  2048. _cmDspNanoMapAlloc,
  2049. NULL,
  2050. _cmDspNanoMapReset,
  2051. NULL,
  2052. _cmDspNanoMapRecv,
  2053. NULL,
  2054. NULL,
  2055. "Nanosynth Mapper");
  2056. return &_cmNanoMapDC;
  2057. }
  2058. //------------------------------------------------------------------------------------------------------------
  2059. //)
  2060. //( { label:cmDspRecdPlay file_desc:"Record audio segments from a live perfromance and play them back at a later time" kw:[sunit] }
  2061. enum
  2062. {
  2063. kChCntPrId,
  2064. kFnPrId,
  2065. kSecsPrId,
  2066. kMaxLaSecsPrId,
  2067. kCurLaSecsPrId,
  2068. kFadeRatePrId,
  2069. kScInitLocIdxPrId,
  2070. kScLocIdxPrId,
  2071. kCmdPrId,
  2072. kInAudioBasePrId
  2073. };
  2074. cmDspClass_t _cmRecdPlayDC;
  2075. typedef struct
  2076. {
  2077. cmDspInst_t inst;
  2078. cmRecdPlay* rcdply;
  2079. cmScH_t scH;
  2080. unsigned onSymId;
  2081. unsigned offSymId;
  2082. unsigned audioOutBaseId;
  2083. unsigned chCnt;
  2084. //unsigned scLocIdx;
  2085. } cmDspRecdPlay_t;
  2086. cmDspRC_t _cmDspRecdPlayParseRsrc( cmDspCtx_t* ctx, cmDspInst_t* inst, cmRecdPlay* rcdply )
  2087. {
  2088. cmDspRC_t rc = kOkDspRC;
  2089. const cmChar_t* path = NULL;
  2090. // read the 'recdplay' audio file path
  2091. if( cmDspRsrcString( ctx->dspH, &path, "recdPlayPath", NULL ) != kOkDspRC )
  2092. {
  2093. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The 'recdPlayPath' resource string was not found.");
  2094. }
  2095. if( path == NULL )
  2096. path = "";
  2097. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2098. cmJsonNode_t* jnp = cmJsonFindValue(jsH,"recdPlay",NULL, kArrayTId);
  2099. if( jnp == NULL || cmJsonIsArray(jnp)==false )
  2100. {
  2101. // this is really a warning - the object does not require preloaded segments.
  2102. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"Warning: The 'recdPlay' resource used to define pre-loaded segments was not found.");
  2103. return kOkDspRC;
  2104. }
  2105. unsigned n = cmJsonChildCount(jnp);
  2106. unsigned i;
  2107. // for each 'recdplay' segment record
  2108. for(i=0; i<n && rc==kOkDspRC; ++i)
  2109. {
  2110. cmJsonNode_t* cnp = cmJsonArrayElement(jnp,i);
  2111. const cmChar_t* label = NULL;
  2112. unsigned segSymId = cmInvalidId;
  2113. const cmChar_t* errLabel = NULL;
  2114. const cmChar_t* fn = NULL;
  2115. // read the ith segment record
  2116. if( cmJsonMemberValues(cnp,&errLabel,
  2117. "label", kStringTId, &label,
  2118. "file", kStringTId, &fn,
  2119. NULL) != kOkJsRC )
  2120. {
  2121. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The record at index %i in the 'recdPlay' pre-loaded segment list could not be parsed.",i);
  2122. goto errLabel;
  2123. }
  2124. // find or generate the symbol id for the segment label symbol
  2125. if((segSymId = cmSymTblRegisterSymbol(ctx->stH,label)) == cmInvalidId )
  2126. {
  2127. rc = cmDspInstErr(ctx,inst,kSymNotFoundDspRC,"The 'recdPlay' pre-load segment symbol '%s' could not be found or registered.",cmStringNullGuard(label));
  2128. goto errLabel;
  2129. }
  2130. // create the full path name for the segment audio file
  2131. if((fn = cmFsMakeFn( path, fn, NULL, NULL )) == NULL )
  2132. {
  2133. rc = cmDspInstErr(ctx,inst,kFileSysFailDspRC,"The 'recdPlay' file name '%s/%s' could not be generated.",cmStringNullGuard(path),cmStringNullGuard(fn));
  2134. goto errLabel;
  2135. }
  2136. // pre-load the segment
  2137. if( cmRecdPlayInsertRecord(rcdply,segSymId,fn) != cmOkRC )
  2138. rc = cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The 'recdPlay' segment label:'%s' file:'%s' could not be loaded.",cmStringNullGuard(label),cmStringNullGuard(fn));
  2139. cmFsFreeFn(fn);
  2140. }
  2141. errLabel:
  2142. return rc;
  2143. }
  2144. cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2145. {
  2146. cmDspRC_t rc =kOkDspRC;
  2147. const cmChar_t* fn;
  2148. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2149. //p->scLocIdx = 0;
  2150. if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
  2151. return cmDspInstErr(ctx,inst, kInvalidArgDspRC, "No score file name supplied.");
  2152. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  2153. return cmDspInstErr(ctx,inst, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  2154. if( cmScoreIsValid(p->scH) )
  2155. {
  2156. unsigned i;
  2157. unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
  2158. double initFragSecs = cmDspDouble(inst,kSecsPrId);
  2159. double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
  2160. double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
  2161. printf("2 max la secs:%f\n",cmDspDouble(inst,kMaxLaSecsPrId));
  2162. if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
  2163. return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
  2164. for(i=0; i<markerCnt; ++i)
  2165. cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
  2166. if((rc = _cmDspRecdPlayParseRsrc(ctx,inst,p->rcdply)) != kOkDspRC )
  2167. rc = cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The 'recdplay' segment pre-load failed.");
  2168. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2169. }
  2170. return rc;
  2171. }
  2172. cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2173. {
  2174. if( va_cnt < 1 )
  2175. {
  2176. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
  2177. return NULL;
  2178. }
  2179. va_list vl1;
  2180. va_copy(vl1,vl);
  2181. int chCnt = va_arg(vl,int);
  2182. unsigned audioOutBase = kInAudioBasePrId + chCnt;
  2183. cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2184. 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
  2185. 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
  2186. 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
  2187. 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum look-ahead buffer in seconds.",
  2188. 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Current look-head buffer in seconds.",
  2189. 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade rate in dB per second.",
  2190. 1, "initIdx",kScInitLocIdxPrId,0,0,kInDsvFl | kUIntDsvFl, "Score search start location.",
  2191. 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
  2192. 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
  2193. chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2194. chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2195. 0 );
  2196. va_end(vl1);
  2197. p->onSymId = cmSymTblId(ctx->stH,"on");
  2198. p->offSymId = cmSymTblId(ctx->stH,"off");
  2199. p->audioOutBaseId = audioOutBase;
  2200. p->chCnt = chCnt;
  2201. //p->scLocIdx = 0;
  2202. printf("0 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2203. cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
  2204. cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
  2205. cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
  2206. cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
  2207. cmDspSetDefaultUInt( ctx,&p->inst, kScInitLocIdxPrId,0,0);
  2208. printf("1 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2209. return &p->inst;
  2210. }
  2211. cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2212. {
  2213. cmDspRC_t rc = kOkDspRC;
  2214. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2215. cmRecdPlayFree(&p->rcdply);
  2216. cmScoreFinalize(&p->scH);
  2217. return rc;
  2218. }
  2219. cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2220. {
  2221. cmDspRC_t rc;
  2222. if((rc = _cmDspRecdPlayOpenScore(ctx,inst)) == kOkDspRC )
  2223. cmDspApplyAllDefaults(ctx,inst);
  2224. return rc;
  2225. }
  2226. cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2227. {
  2228. cmDspRC_t rc = kOkDspRC;
  2229. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2230. const cmSample_t* x[ p->chCnt ];
  2231. cmSample_t* y[ p->chCnt ];
  2232. unsigned n = 0;
  2233. unsigned i;
  2234. unsigned actChCnt = 0;
  2235. for(i=0; i<p->chCnt; ++i)
  2236. {
  2237. if( cmDspIsAudioInputConnected(ctx,inst,kInAudioBasePrId+i) == false )
  2238. {
  2239. x[i] = NULL;
  2240. y[i] = NULL;
  2241. }
  2242. else
  2243. {
  2244. if( i==0 )
  2245. n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
  2246. else
  2247. {
  2248. assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0));
  2249. }
  2250. x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
  2251. if( x[i] != NULL )
  2252. {
  2253. y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
  2254. if( y[i] != NULL )
  2255. {
  2256. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
  2257. cmVOS_Zero(y[i],n);
  2258. actChCnt += 1;
  2259. }
  2260. }
  2261. }
  2262. }
  2263. cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
  2264. return rc;
  2265. }
  2266. cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2267. {
  2268. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2269. if( p->rcdply == NULL )
  2270. return kOkDspRC;
  2271. cmDspSetEvent(ctx,inst,evt);
  2272. switch( evt->dstVarId )
  2273. {
  2274. case kCmdPrId:
  2275. if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
  2276. {
  2277. printf("rewind\n");
  2278. cmRecdPlayRewind(p->rcdply);
  2279. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2280. }
  2281. else
  2282. if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
  2283. {
  2284. }
  2285. break;
  2286. case kCurLaSecsPrId:
  2287. cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
  2288. break;
  2289. case kScInitLocIdxPrId:
  2290. printf("init-idx:%i\n",cmDspUInt(inst,kScInitLocIdxPrId));
  2291. break;
  2292. case kScLocIdxPrId:
  2293. {
  2294. unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId);
  2295. if( endScLocIdx < cmDspUInt(inst,kScInitLocIdxPrId) )
  2296. break;
  2297. cmScoreLoc_t* loc = cmScoreLoc(p->scH, endScLocIdx );
  2298. if( loc == NULL )
  2299. break;
  2300. cmScoreMarker_t* mp = loc->markList;
  2301. for(; mp!=NULL; mp=mp->link)
  2302. switch( mp->markTypeId )
  2303. {
  2304. case kRecdBegScMId:
  2305. printf("recd-beg %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2306. cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
  2307. break;
  2308. case kRecdEndScMId:
  2309. printf("recd-end %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2310. cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
  2311. break;
  2312. case kPlayBegScMId:
  2313. printf("play-beg\n");
  2314. cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
  2315. break;
  2316. case kPlayEndScMId:
  2317. printf("play-end\n");
  2318. cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
  2319. break;
  2320. case kFadeScMId:
  2321. printf("fade-beg\n");
  2322. cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
  2323. break;
  2324. default:
  2325. break;
  2326. }
  2327. //p->scLocIdx = endScLocIdx+1;
  2328. }
  2329. break;
  2330. }
  2331. return kOkDspRC;
  2332. }
  2333. cmDspClass_t* cmRecdPlayClassCons( cmDspCtx_t* ctx )
  2334. {
  2335. cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
  2336. NULL,
  2337. _cmDspRecdPlayAlloc,
  2338. _cmDspRecdPlayFree,
  2339. _cmDspRecdPlayReset,
  2340. _cmDspRecdPlayExec,
  2341. _cmDspRecdPlayRecv,
  2342. NULL,
  2343. NULL,
  2344. "Score controlled live recorder/player");
  2345. return &_cmRecdPlayDC;
  2346. }
  2347. //------------------------------------------------------------------------------------------------------------
  2348. //)
  2349. //( { label:cmDspGoertzel file_desc:"Goertzel tone detection filter" kw:[sunit] }
  2350. enum
  2351. {
  2352. kHopFactGrId,
  2353. kInGrId,
  2354. kHzBaseGrId,
  2355. };
  2356. cmDspClass_t _cmGoertzelDC;
  2357. typedef struct
  2358. {
  2359. cmDspInst_t inst;
  2360. cmGoertzel* g;
  2361. double outPhs;
  2362. unsigned outBaseGrId;
  2363. unsigned chCnt;
  2364. } cmDspGoertzel_t;
  2365. cmDspInst_t* _cmDspGoertzelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2366. {
  2367. if( va_cnt !=3 )
  2368. {
  2369. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Goertzel' constructor must have two arguments: a channel count and frequency array.");
  2370. return NULL;
  2371. }
  2372. va_list vl1;
  2373. va_copy(vl1,vl);
  2374. unsigned hopFact = va_arg(vl,unsigned);
  2375. int chCnt = va_arg(vl,int);
  2376. double* hzV = va_arg(vl,double*);
  2377. unsigned outBaseGrId = kHzBaseGrId + chCnt;
  2378. unsigned i;
  2379. cmDspGoertzel_t* p = cmDspInstAllocV(cmDspGoertzel_t,ctx,classPtr,instSymId,id,storeSymId,1,vl1,
  2380. 1, "hop", kHopFactGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hop factor",
  2381. 1, "in", kInGrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2382. chCnt, "hz", kHzBaseGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hz input.",
  2383. chCnt, "out", outBaseGrId, 0,1, kOutDsvFl | kDoubleDsvFl, "Detector output",
  2384. 0 );
  2385. va_end(vl1);
  2386. p->outBaseGrId = outBaseGrId;
  2387. p->chCnt = chCnt;
  2388. p->g = cmGoertzelAlloc(ctx->cmProcCtx, NULL, 0, NULL, 0,0,0,0 );
  2389. cmDspSetDefaultUInt(ctx,&p->inst, kHopFactGrId, 0, cmMax(hopFact,1));
  2390. for(i=0; i<chCnt; ++i)
  2391. cmDspSetDefaultDouble(ctx,&p->inst, kHzBaseGrId+i, 0.0, hzV[i] );
  2392. return &p->inst;
  2393. }
  2394. cmDspRC_t _cmDspGoertzelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2395. {
  2396. cmDspRC_t rc = kOkDspRC;
  2397. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2398. unsigned hopFact = cmDspUInt(inst,kHopFactGrId);
  2399. unsigned procSmpCnt = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2400. unsigned wndSmpCnt = procSmpCnt * hopFact;
  2401. double fcHzV[ p->chCnt ];
  2402. unsigned i;
  2403. for(i=0; i<p->chCnt; ++i)
  2404. {
  2405. double hz;
  2406. if( p->g->ch == NULL || p->g->ch[i].hz == 0 )
  2407. hz = cmDspDouble(inst,kHzBaseGrId);
  2408. else
  2409. hz = p->g->ch[i].hz;
  2410. fcHzV[i] = hz;
  2411. }
  2412. if( cmGoertzelInit(p->g,cmDspSysSampleRate(ctx->dspH),fcHzV,p->chCnt,procSmpCnt,procSmpCnt,wndSmpCnt) != cmOkRC )
  2413. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal Goertzel detector.");
  2414. return rc;
  2415. }
  2416. cmDspRC_t _cmDspGoertzelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2417. {
  2418. cmDspRC_t rc = kOkDspRC;
  2419. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2420. cmGoertzelFree(&p->g);
  2421. return rc;
  2422. }
  2423. cmDspRC_t _cmDspGoertzelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2424. {
  2425. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2426. cmDspApplyAllDefaults(ctx,inst);
  2427. p->outPhs = 0;
  2428. return _cmDspGoertzelSetup(ctx, inst );
  2429. }
  2430. cmDspRC_t _cmDspGoertzelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2431. {
  2432. cmDspRC_t rc = kOkDspRC;
  2433. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2434. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInGrId,0);
  2435. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2436. double outMs = 50.0;
  2437. double outPhsMax = outMs * cmDspSysSampleRate(ctx->dspH) / 1000.0;
  2438. double outV[ p->chCnt ];
  2439. unsigned i;
  2440. if( x != NULL )
  2441. {
  2442. cmGoertzelExec(p->g,x,n,outV,p->chCnt);
  2443. p->outPhs += n;
  2444. if( p->outPhs > outPhsMax )
  2445. {
  2446. while( p->outPhs > outPhsMax )
  2447. p->outPhs -= outPhsMax;
  2448. for(i=0; i<p->chCnt; ++i)
  2449. {
  2450. cmDspSetDouble(ctx,inst,p->outBaseGrId+i,outV[i]);
  2451. //printf("%f ",outV[i]);
  2452. }
  2453. //printf("\n");
  2454. }
  2455. }
  2456. return rc;
  2457. }
  2458. cmDspRC_t _cmDspGoertzelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2459. {
  2460. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2461. cmDspSetEvent(ctx,inst,evt);
  2462. if( kHzBaseGrId <= evt->dstVarId && evt->dstVarId < kHzBaseGrId+p->chCnt )
  2463. cmGoertzelSetFcHz(p->g, evt->dstVarId - kHzBaseGrId, cmDspDouble(inst,evt->dstVarId));
  2464. else
  2465. {
  2466. if( evt->dstVarId==kHopFactGrId )
  2467. {
  2468. _cmDspGoertzelSetup(ctx,inst);
  2469. }
  2470. }
  2471. return kOkDspRC;
  2472. }
  2473. cmDspClass_t* cmGoertzelClassCons( cmDspCtx_t* ctx )
  2474. {
  2475. cmDspClassSetup(&_cmGoertzelDC,ctx,"Goertzel",
  2476. NULL,
  2477. _cmDspGoertzelAlloc,
  2478. _cmDspGoertzelFree,
  2479. _cmDspGoertzelReset,
  2480. _cmDspGoertzelExec,
  2481. _cmDspGoertzelRecv,
  2482. NULL,
  2483. NULL,
  2484. "Goertzel Tone Detector Filter");
  2485. return &_cmGoertzelDC;
  2486. }
  2487. //------------------------------------------------------------------------------------------------------------
  2488. //)
  2489. //( { label:cmDspSyncRecd file_desc:"Time align a MIDI and associated audio recording" kw:[sunit] }
  2490. enum
  2491. {
  2492. kRecdDirSrId,
  2493. kSrFnSrId,
  2494. kAfSrId,
  2495. kBitsSrId,
  2496. kCmdSrId,
  2497. kStatusSrId,
  2498. kD0SrId,
  2499. kD1SrId,
  2500. kSecSrId,
  2501. kNSecSrId,
  2502. kAinBaseSrId
  2503. };
  2504. cmDspClass_t _cmSyncRecdDC;
  2505. typedef struct
  2506. {
  2507. cmDspInst_t inst;
  2508. unsigned chCnt;
  2509. cmTimeSpec_t ats;
  2510. cmSyncRecdH_t srH;
  2511. unsigned openSymId;
  2512. unsigned closeSymId;
  2513. const cmChar_t* aFn;
  2514. const cmChar_t* srFn;
  2515. unsigned smpIdx;
  2516. } cmDspSyncRecd_t;
  2517. cmDspRC_t _cmDspSyncRecdCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2518. {
  2519. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2520. const cmChar_t* aFn = cmDspStrcz(inst,kAfSrId);
  2521. const cmChar_t* srFn = cmDspStrcz(inst,kSrFnSrId);
  2522. const cmChar_t* dir = cmDspStrcz(inst,kRecdDirSrId);
  2523. if( !cmFsIsDir(dir) )
  2524. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' is not a valid directory.",cmStringNullGuard(dir));
  2525. cmMemPtrFree(&p->aFn);
  2526. if( cmFsGenFn(dir,aFn,"aiff",&p->aFn) != kOkFsRC )
  2527. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Audio file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(aFn));
  2528. cmMemPtrFree(&p->srFn);
  2529. if( cmFsGenFn(dir,srFn,"sr",&p->srFn) != kOkFsRC )
  2530. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Sync-recd file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(srFn));
  2531. unsigned bits = cmDspUInt(inst,kBitsSrId);
  2532. if( cmSyncRecdCreate( ctx->cmCtx, &p->srH, p->srFn, p->aFn, cmDspSampleRate(ctx), p->chCnt, bits ) != kOkSyRC )
  2533. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd file create failed for '%s'.",p->srFn);
  2534. p->smpIdx = 0;
  2535. return kOkDspRC;
  2536. }
  2537. cmDspInst_t* _cmDspSyncRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2538. {
  2539. cmDspSyncRecd_t* p = cmDspInstAllocV(cmDspSyncRecd_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2540. 1, "dir", kRecdDirSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Recording directory.",
  2541. 1, "srfn", kSrFnSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "SyncRecd file prefix.",
  2542. 1, "afn", kAfSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file prefix.",
  2543. 1, "bits", kBitsSrId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Audio file bits per sample.",
  2544. 1, "cmd", kCmdSrId, 0,0, kInDsvFl | kSymDsvFl, "Command: open | close",
  2545. 1, "status", kStatusSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI status",
  2546. 1, "d0", kD0SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d0",
  2547. 1, "d1", kD1SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d1",
  2548. 1, "sec", kSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Seconds",
  2549. 1, "nsec", kNSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Nanoseconds",
  2550. 2, "ain", kAinBaseSrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio Input",
  2551. 0 );
  2552. p->chCnt = 2;
  2553. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  2554. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  2555. cmDspSetDefaultUInt(ctx,&p->inst,kBitsSrId,0,16);
  2556. return &p->inst;
  2557. }
  2558. cmDspRC_t _cmDspSyncRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2559. {
  2560. cmDspRC_t rc = kOkDspRC;
  2561. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2562. cmMemPtrFree(&p->aFn);
  2563. cmMemPtrFree(&p->srFn);
  2564. cmSyncRecdFinal(&p->srH);
  2565. return rc;
  2566. }
  2567. cmDspRC_t _cmDspSyncRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2568. {
  2569. cmDspRC_t rc = kOkDspRC;
  2570. cmDspApplyAllDefaults(ctx,inst);
  2571. return rc;
  2572. }
  2573. cmDspRC_t _cmDspSyncRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2574. {
  2575. cmDspRC_t rc = kOkDspRC;
  2576. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2577. const cmSample_t* x[ p->chCnt ];
  2578. unsigned n = 0;
  2579. unsigned i;
  2580. //const cmTimeSpec_t* ts = &ctx->ctx->oTimeStamp;
  2581. //printf("SR: %ld %ld\n",ts->tv_sec,ts->tv_nsec);
  2582. p->ats = ctx->ctx->iTimeStamp;
  2583. for(i=0; i<p->chCnt; ++i)
  2584. {
  2585. if( i==0 )
  2586. n = cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0);
  2587. else
  2588. { assert( n == cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0)); }
  2589. x[i] = cmDspAudioBuf(ctx,inst,kAinBaseSrId+i,0);
  2590. }
  2591. if( n>0 && cmSyncRecdIsValid(p->srH ) )
  2592. if( cmSyncRecdAudioWrite( p->srH, &ctx->ctx->iTimeStamp, p->smpIdx, x, p->chCnt, n ) != kOkSyRC )
  2593. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd audio update failed.");
  2594. p->smpIdx += n;
  2595. return rc;
  2596. }
  2597. cmDspRC_t _cmDspSyncRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2598. {
  2599. cmDspRC_t rc = kOkDspRC;
  2600. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2601. cmDspSetEvent(ctx,inst,evt);
  2602. switch( evt->dstVarId )
  2603. {
  2604. case kStatusSrId:
  2605. if(cmMidiIsChStatus( cmDspUInt(inst,kStatusSrId) ) )
  2606. {
  2607. cmTimeSpec_t ts;
  2608. ts.tv_sec = cmDspUInt(inst,kSecSrId);
  2609. ts.tv_nsec = cmDspUInt(inst,kNSecSrId);
  2610. //printf("%i %i\n",cmDspUInt(inst,kD1SrId),cmTimeElapsedMicros(&ts,&p->ats));
  2611. if( cmSyncRecdIsValid(p->srH ) )
  2612. if( cmSyncRecdMidiWrite(p->srH, &ts, cmDspUInt(inst,kStatusSrId), cmDspUInt(inst,kD0SrId), cmDspUInt(inst,kD1SrId) ) != kOkSyRC )
  2613. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd MIDI update failed.");
  2614. }
  2615. break;
  2616. case kCmdSrId:
  2617. {
  2618. unsigned cmdId = cmDspSymbol(inst,kCmdSrId);
  2619. if( cmdId == p->openSymId )
  2620. rc = _cmDspSyncRecdCreateFile(ctx,inst);
  2621. else
  2622. if( cmdId == p->closeSymId && cmSyncRecdIsValid(p->srH))
  2623. {
  2624. cmSyncRecdFinal(&p->srH);
  2625. //cmSyncRecdTest(ctx->cmCtx);
  2626. }
  2627. }
  2628. break;
  2629. }
  2630. return rc;
  2631. }
  2632. cmDspClass_t* cmSyncRecdClassCons( cmDspCtx_t* ctx )
  2633. {
  2634. cmDspClassSetup(&_cmSyncRecdDC,ctx,"SyncRecd",
  2635. NULL,
  2636. _cmDspSyncRecdAlloc,
  2637. _cmDspSyncRecdFree,
  2638. _cmDspSyncRecdReset,
  2639. _cmDspSyncRecdExec,
  2640. _cmDspSyncRecdRecv,
  2641. NULL,
  2642. NULL,
  2643. "Synchronized Audio and MIDI recorder.");
  2644. return &_cmSyncRecdDC;
  2645. }
  2646. //------------------------------------------------------------------------------------------------------------
  2647. //)
  2648. //( { label:cmDspTakeSeqBldr file_desc:"User interface unit for creating a single sequence from multiple, score aligned, MIDI fragments." kw:[sunit] }
  2649. enum
  2650. {
  2651. kFnTsbId,
  2652. kBldrTsbId,
  2653. kSelTsbId,
  2654. kRefreshTsbId,
  2655. kSendTsbId
  2656. };
  2657. cmDspClass_t _cmTakeSeqBldrDC;
  2658. typedef struct
  2659. {
  2660. cmDspInst_t inst;
  2661. cmTakeSeqBldrH_t h;
  2662. bool errFl;
  2663. } cmDspTakeSeqBldr_t;
  2664. cmDspInst_t* _cmDspTakeSeqBldrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2665. {
  2666. cmDspTakeSeqBldr_t* p = cmDspInstAllocV(cmDspTakeSeqBldr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2667. 1, "fn", kFnTsbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score Tracking file.",
  2668. 1, "bldr", kBldrTsbId, 0, 0, kOutDsvFl | kPtrDsvFl, "Bldr Ref",
  2669. 1, "sel", kSelTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Selected score element location index.",
  2670. 1, "refresh",kRefreshTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Refresh",
  2671. 1, "send", kSendTsbId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element location.",
  2672. 0 );
  2673. p->errFl = false;
  2674. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsbId, 0, 0);
  2675. if( cmTakeSeqBldrAlloc(ctx->cmCtx, &p->h ) != kOkTsbRC )
  2676. cmErrMsg(&p->inst.classPtr->err, kSubSysFailDspRC, "Allocate TaskSeqBldr object.");
  2677. else
  2678. {
  2679. cmDspUiTakeSeqBldrCreate(ctx,&p->inst,kFnTsbId,kBldrTsbId,kSelTsbId,kRefreshTsbId);
  2680. }
  2681. return &p->inst;
  2682. }
  2683. cmDspRC_t _cmDspTakeSeqBldrSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2684. {
  2685. cmDspRC_t rc = kOkDspRC;
  2686. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2687. if( cmTakeSeqBldrInitialize(p->h, cmDspStrcz(inst,kFnTsbId) ) != kOkTsbRC )
  2688. {
  2689. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal TakeSeqBldr object with %s.",cmStringNullGuard(cmDspStrcz(inst,kFnTsbId)));
  2690. }
  2691. else
  2692. {
  2693. cmDspSetPtr(ctx,inst,kBldrTsbId,p->h.h);
  2694. p->errFl = false;
  2695. }
  2696. return rc;
  2697. }
  2698. cmDspRC_t _cmDspTakeSeqBldrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2699. {
  2700. cmDspRC_t rc = kOkDspRC;
  2701. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2702. cmTakeSeqBldrFree(&p->h);
  2703. return rc;
  2704. }
  2705. cmDspRC_t _cmDspTakeSeqBldrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2706. {
  2707. //cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2708. cmDspApplyAllDefaults(ctx,inst);
  2709. return _cmDspTakeSeqBldrSetup(ctx, inst );
  2710. }
  2711. cmDspRC_t _cmDspTakeSeqBldrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2712. {
  2713. cmDspRC_t rc = kOkDspRC;
  2714. return rc;
  2715. }
  2716. cmDspRC_t _cmDspTakeSeqBldrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2717. {
  2718. // not matter what arrives at the 'send' input ....
  2719. if( evt->dstVarId == kSendTsbId )
  2720. {
  2721. // send the last score loc
  2722. unsigned selIdx;
  2723. if((selIdx = cmDspUInt(inst,kSelTsbId)) != cmInvalidIdx )
  2724. cmDspSetUInt(ctx,inst,kSelTsbId, selIdx );
  2725. return kOkDspRC;
  2726. }
  2727. cmDspSetEvent(ctx,inst,evt);
  2728. switch(evt->dstVarId)
  2729. {
  2730. case kFnTsbId:
  2731. _cmDspMidiFilePlayOpen(ctx, inst );
  2732. break;
  2733. }
  2734. return kOkDspRC;
  2735. }
  2736. cmDspClass_t* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx )
  2737. {
  2738. cmDspClassSetup(&_cmTakeSeqBldrDC,ctx,"TakeSeqBldr",
  2739. NULL,
  2740. _cmDspTakeSeqBldrAlloc,
  2741. _cmDspTakeSeqBldrFree,
  2742. _cmDspTakeSeqBldrReset,
  2743. _cmDspTakeSeqBldrExec,
  2744. _cmDspTakeSeqBldrRecv,
  2745. NULL,
  2746. NULL,
  2747. "TakeSeqBldr");
  2748. return &_cmTakeSeqBldrDC;
  2749. }
  2750. //------------------------------------------------------------------------------------------------------------
  2751. //)
  2752. //( { label:cmDspTakeSeqRend file_desc:"User interface unit for graphically rendering the MIDI sequences created by cmDspTakeSeqBldr." kw:[sunit] }
  2753. enum
  2754. {
  2755. kBldrTsrId,
  2756. kRefreshTsrId,
  2757. kCmdTsrId,
  2758. kSelTsrId,
  2759. kStatusTsrId,
  2760. kD0TsrId,
  2761. kD1TsrId,
  2762. kSmpIdxTsrId
  2763. };
  2764. cmDspClass_t _cmTakeSeqRendDC;
  2765. typedef struct
  2766. {
  2767. cmDspInst_t inst;
  2768. cmTakeSeqBldrH_t h;
  2769. unsigned startSymId;
  2770. unsigned stopSymId;
  2771. unsigned contSymId;
  2772. unsigned onSymId;
  2773. unsigned offSymId;
  2774. bool errFl;
  2775. } cmDspTakeSeqRend_t;
  2776. cmDspInst_t* _cmDspTakeSeqRendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2777. {
  2778. cmDspTakeSeqRend_t* p = cmDspInstAllocV(cmDspTakeSeqRend_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2779. 1, "bldr", kBldrTsrId, 0, 0, kInDsvFl | kPtrDsvFl | kOptArgDsvFl, "Take Sequene Builder Ref",
  2780. 1, "refresh",kRefreshTsrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Refresh",
  2781. 1, "cmd", kCmdTsrId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" ,
  2782. 1, "sel", kSelTsrId, 0, 0, kInDsvFl | kUIntDsvFl, "Selected score element location index input.",
  2783. 1, "status", kStatusTsrId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output",
  2784. 1, "d0", kD0TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" ,
  2785. 1, "d1", kD1TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1",
  2786. 1, "smpidx", kSmpIdxTsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index.",
  2787. 0 );
  2788. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  2789. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  2790. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  2791. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2792. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2793. p->errFl = false;
  2794. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsrId, 0, 0);
  2795. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdTsrId, p->stopSymId);
  2796. cmDspSetDefaultInt( ctx, &p->inst, kSmpIdxTsrId, 0, 0);
  2797. cmDspSetDefaultUInt( ctx, &p->inst, kStatusTsrId, 0, 0);
  2798. cmDspSetDefaultUInt( ctx, &p->inst, kD0TsrId, 0, 0);
  2799. cmDspSetDefaultUInt( ctx, &p->inst, kD1TsrId, 0, 0);
  2800. cmDspUiTakeSeqRendCreate(ctx,&p->inst,kBldrTsrId,kRefreshTsrId,kSelTsrId);
  2801. return &p->inst;
  2802. }
  2803. cmDspRC_t _cmDspTakeSeqRendFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2804. {
  2805. cmDspRC_t rc = kOkDspRC;
  2806. return rc;
  2807. }
  2808. cmDspRC_t _cmDspTakeSeqRendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2809. {
  2810. //cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2811. cmDspApplyAllDefaults(ctx,inst);
  2812. return kOkDspRC;
  2813. }
  2814. typedef struct
  2815. {
  2816. cmDspCtx_t* ctx;
  2817. cmDspInst_t* inst;
  2818. } _cmDspTakeSeqRendCbArg_t;
  2819. // Called from cmDspTakeSeqRendExec() -> cmTakeSeqRendPlayExec() to
  2820. // deliver MIDI messages which need to be transmitted.
  2821. void _cmDspTakeSeqRendMidiCb( void* arg, const cmTksbEvent_t* e )
  2822. {
  2823. _cmDspTakeSeqRendCbArg_t* a = (_cmDspTakeSeqRendCbArg_t*)arg;
  2824. switch( e->status )
  2825. {
  2826. case kNoteOffMdId:
  2827. case kNoteOnMdId:
  2828. case kCtlMdId:
  2829. //if( !cmMidiIsPedal(e->status,e->d0))
  2830. {
  2831. cmDspSetUInt(a->ctx,a->inst, kSmpIdxTsrId, e->smpIdx);
  2832. cmDspSetUInt(a->ctx,a->inst, kD1TsrId, e->d1);
  2833. cmDspSetUInt(a->ctx,a->inst, kD0TsrId, e->d0);
  2834. cmDspSetUInt(a->ctx,a->inst, kStatusTsrId, e->status);
  2835. }
  2836. break;
  2837. }
  2838. }
  2839. void _cmDspTakeSeqRendPedalsUp( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2840. {
  2841. _cmDspTakeSeqRendCbArg_t a;
  2842. a.ctx = ctx;
  2843. a.inst = inst;
  2844. cmTksbEvent_t e[] =
  2845. {
  2846. { 0, kCtlMdId, kSustainCtlMdId, 0 },
  2847. { 0, kCtlMdId, kPortamentoCtlMdId,0 },
  2848. { 0, kCtlMdId, kSostenutoCtlMdId, 0 },
  2849. { 0, kCtlMdId, kSoftPedalCtlMdId, 0 },
  2850. { 0, kCtlMdId, kLegatoCtlMdId, 0 }
  2851. };
  2852. unsigned n = sizeof(e)/sizeof(e[0]);
  2853. unsigned i;
  2854. for(i=0; i<n; ++i)
  2855. _cmDspTakeSeqRendMidiCb(&a,e+i);
  2856. }
  2857. cmDspRC_t _cmDspTakeSeqRendExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2858. {
  2859. cmDspRC_t rc = kOkDspRC;
  2860. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2861. if( cmDspSymbol(inst,kCmdTsrId) != p->stopSymId )
  2862. {
  2863. if( cmTakeSeqBldrIsValid(p->h) == false )
  2864. {
  2865. if( p->errFl==false )
  2866. {
  2867. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The Take Sequence Builder not been given a valid file.");
  2868. p->errFl = true;
  2869. }
  2870. return rc;
  2871. }
  2872. unsigned sPc = cmDspSamplesPerCycle(ctx);
  2873. _cmDspTakeSeqRendCbArg_t arg;
  2874. arg.inst = inst;
  2875. arg.ctx = ctx;
  2876. // This call may result in multiple callbacks
  2877. // to _cmDspTakeSeqRendMidiCb() from within the function.
  2878. cmTakeSeqBldrPlayExec(p->h,sPc,_cmDspTakeSeqRendMidiCb, &arg );
  2879. }
  2880. return rc;
  2881. }
  2882. cmDspRC_t _cmDspTakeSeqRendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2883. {
  2884. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2885. printf("Recv:%s\n",cmStringNullGuard(cmDspVarLabel(ctx, inst, evt->dstVarId)) );
  2886. cmDspSetEvent(ctx,inst,evt);
  2887. switch(evt->dstVarId)
  2888. {
  2889. case kBldrTsrId:
  2890. p->h.h = cmDspPtr(inst,kBldrTsrId);
  2891. break;
  2892. case kCmdTsrId:
  2893. {
  2894. unsigned symId = cmDspSymbol(inst,kCmdTsrId);
  2895. if( symId == p->onSymId )
  2896. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->startSymId );
  2897. else
  2898. if( symId == p->offSymId )
  2899. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->stopSymId );
  2900. if( cmTakeSeqBldrIsValid(p->h) && cmDspSymbol(inst,kCmdTsrId)==p->startSymId )
  2901. {
  2902. _cmDspTakeSeqRendPedalsUp( ctx, inst );
  2903. cmTakeSeqBldrPlaySeekLoc(p->h, cmInvalidId );
  2904. }
  2905. }
  2906. break;
  2907. case kSelTsrId:
  2908. {
  2909. // seek the playback position to the scLocIdx.
  2910. unsigned scLocIdx = cmDspUInt(inst,kSelTsrId);
  2911. if( cmTakeSeqBldrIsValid(p->h) && cmTakeSeqBldrPlaySeekLoc(p->h, scLocIdx ) != kOkTsbRC )
  2912. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Take Sequence Bldr Seek failed on score location index %i.", scLocIdx);
  2913. }
  2914. break;
  2915. }
  2916. return kOkDspRC;
  2917. }
  2918. cmDspClass_t* cmTakeSeqRendClassCons( cmDspCtx_t* ctx )
  2919. {
  2920. cmDspClassSetup(&_cmTakeSeqRendDC,ctx,"TakeSeqRend",
  2921. NULL,
  2922. _cmDspTakeSeqRendAlloc,
  2923. _cmDspTakeSeqRendFree,
  2924. _cmDspTakeSeqRendReset,
  2925. _cmDspTakeSeqRendExec,
  2926. _cmDspTakeSeqRendRecv,
  2927. NULL,
  2928. NULL,
  2929. "TakeSeqRend");
  2930. return &_cmTakeSeqRendDC;
  2931. }
  2932. //------------------------------------------------------------------------------------------------------------
  2933. //)
  2934. //( { label:cmDspReflectCalc file_desc:"Estimate the time-of-flight of from an acoustic signal from a speaker to a microphone." kw:[sunit] }
  2935. enum
  2936. {
  2937. kLfsrN_RcId,
  2938. kMlsCoeff0RcId,
  2939. kMlsCoeff1RcId,
  2940. kSmpPerChipRcId,
  2941. kRcosBetaRcId,
  2942. kRcosOS_RcId,
  2943. kCarrierHzRcId,
  2944. kAtkDcyMsRcId,
  2945. kPhatAlphaRcId,
  2946. kPhatMultRcId,
  2947. kInRcId,
  2948. kOutRcId,
  2949. };
  2950. cmDspClass_t _cmReflectCalcDC;
  2951. typedef struct
  2952. {
  2953. cmDspInst_t inst;
  2954. cmReflectCalc_t* r;
  2955. } cmDspReflectCalc_t;
  2956. cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2957. {
  2958. // if( va_cnt !=3 )
  2959. // {
  2960. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array.");
  2961. // return NULL;
  2962. // }
  2963. cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2964. 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length",
  2965. 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  2966. 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  2967. 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.",
  2968. 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta",
  2969. 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.",
  2970. 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.",
  2971. 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.",
  2972. 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.",
  2973. 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.",
  2974. 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input",
  2975. 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  2976. 0 );
  2977. p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 );
  2978. cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8);
  2979. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e);
  2980. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96);
  2981. cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64);
  2982. cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5);
  2983. cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4);
  2984. cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0);
  2985. cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0);
  2986. cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5);
  2987. cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 1);
  2988. return &p->inst;
  2989. }
  2990. cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2991. {
  2992. cmDspRC_t rc = kOkDspRC;
  2993. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2994. cmGoldSigArg_t gsa;
  2995. gsa.chN = 1;
  2996. gsa.srate = cmDspSampleRate(ctx);
  2997. gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId);
  2998. gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId);
  2999. gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId);
  3000. gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId);
  3001. gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId);
  3002. gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId);
  3003. gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId);
  3004. gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId);
  3005. double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId);
  3006. unsigned phatMult = cmDspUInt(inst,kPhatMultRcId);
  3007. if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC )
  3008. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector.");
  3009. return rc;
  3010. }
  3011. cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3012. {
  3013. cmDspRC_t rc = kOkDspRC;
  3014. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3015. cmReflectCalcFree(&p->r);
  3016. return rc;
  3017. }
  3018. cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3019. {
  3020. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3021. cmDspApplyAllDefaults(ctx,inst);
  3022. return _cmDspReflectCalcSetup(ctx, inst );
  3023. }
  3024. cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3025. {
  3026. cmDspRC_t rc = kOkDspRC;
  3027. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3028. const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0);
  3029. unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0);
  3030. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0);
  3031. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0);
  3032. if( xV != NULL && yV != NULL )
  3033. {
  3034. assert( xN == yN );
  3035. cmReflectCalcExec(p->r,xV,yV,xN);
  3036. }
  3037. return rc;
  3038. }
  3039. cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3040. {
  3041. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3042. cmDspSetEvent(ctx,inst,evt);
  3043. return kOkDspRC;
  3044. }
  3045. cmDspClass_t* cmReflectCalcClassCons( cmDspCtx_t* ctx )
  3046. {
  3047. cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc",
  3048. NULL,
  3049. _cmDspReflectCalcAlloc,
  3050. _cmDspReflectCalcFree,
  3051. _cmDspReflectCalcReset,
  3052. _cmDspReflectCalcExec,
  3053. _cmDspReflectCalcRecv,
  3054. NULL,
  3055. NULL,
  3056. "Reflecttion time calculator");
  3057. return &_cmReflectCalcDC;
  3058. }
  3059. //------------------------------------------------------------------------------------------------------------
  3060. //)
  3061. //( { label:cmDspEchoCancel file_desc:"Normalized least mean squares echo canceller." kw:[sunit] }
  3062. enum
  3063. {
  3064. kMuEcId,
  3065. kImpRespN_EcId,
  3066. kDelayN_EcId,
  3067. kBypassEcId,
  3068. kUnfiltInEcId,
  3069. kFiltInEcId,
  3070. kOutEcId
  3071. };
  3072. cmDspClass_t _cmEchoCancelDC;
  3073. typedef struct
  3074. {
  3075. cmDspInst_t inst;
  3076. cmNlmsEc_t* r;
  3077. } cmDspEchoCancel_t;
  3078. cmDspInst_t* _cmDspEchoCancelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3079. {
  3080. // if( va_cnt !=3 )
  3081. // {
  3082. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'EchoCancel' constructor must have two arguments: a channel count and frequency array.");
  3083. // return NULL;
  3084. // }
  3085. cmDspEchoCancel_t* p = cmDspInstAllocV(cmDspEchoCancel_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3086. 1, "mu", kMuEcId, 0,0, kInDsvFl | kDoubleDsvFl, "NLSM mu coefficient.",
  3087. 1, "irN", kImpRespN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Filter impulse response length in samples.",
  3088. 1, "delayN", kDelayN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Fixed feedback delay in samples.",
  3089. 1, "bypass", kBypassEcId, 0,0, kInDsvFl | kBoolDsvFl, "Bypass enable flag.",
  3090. 1, "uf_in", kUnfiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Unfiltered audio input",
  3091. 1, "f_in", kFiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Filtered audio input",
  3092. 1, "out", kOutEcId, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3093. 0 );
  3094. p->r = cmNlmsEcAlloc(ctx->cmProcCtx, NULL, 0, 0,0,0 );
  3095. cmDspSetDefaultDouble( ctx, &p->inst, kMuEcId, 0, 0.1);
  3096. cmDspSetDefaultUInt( ctx, &p->inst, kImpRespN_EcId, 0, 2048);
  3097. cmDspSetDefaultUInt( ctx, &p->inst, kDelayN_EcId, 0, 1765);
  3098. cmDspSetDefaultBool( ctx, &p->inst, kBypassEcId, 0, false);
  3099. return &p->inst;
  3100. }
  3101. cmDspRC_t _cmDspEchoCancelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3102. {
  3103. cmDspRC_t rc = kOkDspRC;
  3104. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3105. double mu = cmDspDouble(inst,kMuEcId);
  3106. unsigned hN = cmDspUInt(inst,kImpRespN_EcId);
  3107. unsigned delayN = cmDspUInt(inst,kDelayN_EcId);
  3108. if( cmNlmsEcInit(p->r,cmDspSampleRate(ctx),mu,hN,delayN) != cmOkRC )
  3109. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal echo canceller.");
  3110. return rc;
  3111. }
  3112. cmDspRC_t _cmDspEchoCancelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3113. {
  3114. cmDspRC_t rc = kOkDspRC;
  3115. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3116. cmNlmsEcWrite(p->r, "/Users/kevin/temp/kc");
  3117. cmNlmsEcFree(&p->r);
  3118. return rc;
  3119. }
  3120. cmDspRC_t _cmDspEchoCancelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3121. {
  3122. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3123. cmDspApplyAllDefaults(ctx,inst);
  3124. return _cmDspEchoCancelSetup(ctx, inst );
  3125. }
  3126. cmDspRC_t _cmDspEchoCancelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3127. {
  3128. cmDspRC_t rc = kOkDspRC;
  3129. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3130. bool bypassFl = true; //cmDspBool(inst,kBypassEcId);
  3131. const cmSample_t* fV = cmDspAudioBuf(ctx,inst,kFiltInEcId,0);
  3132. unsigned fN = cmDspAudioBufSmpCount(ctx,inst,kFiltInEcId,0);
  3133. const cmSample_t* uV = cmDspAudioBuf(ctx,inst,kUnfiltInEcId,0);
  3134. unsigned uN = cmDspAudioBufSmpCount(ctx,inst,kUnfiltInEcId,0);
  3135. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutEcId,0);
  3136. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutEcId,0);
  3137. assert( fN==uN && fN==yN );
  3138. if( bypassFl )
  3139. {
  3140. cmVOS_Copy(yV,yN,uV);
  3141. }
  3142. else
  3143. {
  3144. if( fV !=NULL && uV != NULL && yV != NULL )
  3145. {
  3146. assert( uN == yN && fN == yN );
  3147. cmNlmsEcExec(p->r,uV,fV,yV,yN);
  3148. }
  3149. }
  3150. return rc;
  3151. }
  3152. cmDspRC_t _cmDspEchoCancelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3153. {
  3154. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3155. cmDspSetEvent(ctx,inst,evt);
  3156. if( p->r != NULL )
  3157. {
  3158. switch(evt->dstVarId)
  3159. {
  3160. case kMuEcId:
  3161. cmNlmsEcSetMu( p->r, cmDspReal(inst,kMuEcId));
  3162. break;
  3163. case kImpRespN_EcId:
  3164. cmNlmsEcSetIrN( p->r, cmDspUInt(inst,kImpRespN_EcId));
  3165. break;
  3166. case kDelayN_EcId:
  3167. cmNlmsEcSetDelayN( p->r, cmDspUInt(inst,kDelayN_EcId));
  3168. break;
  3169. case kBypassEcId:
  3170. printf("EC bypass:%i\n",cmDspBool(inst,kBypassEcId));
  3171. break;
  3172. }
  3173. }
  3174. printf("mu:%f dN:%i hN:%i\n",p->r->mu,p->r->delayN,p->r->hN);
  3175. return kOkDspRC;
  3176. }
  3177. cmDspClass_t* cmEchoCancelClassCons( cmDspCtx_t* ctx )
  3178. {
  3179. cmDspClassSetup(&_cmEchoCancelDC,ctx,"EchoCancel",
  3180. NULL,
  3181. _cmDspEchoCancelAlloc,
  3182. _cmDspEchoCancelFree,
  3183. _cmDspEchoCancelReset,
  3184. _cmDspEchoCancelExec,
  3185. _cmDspEchoCancelRecv,
  3186. NULL,
  3187. NULL,
  3188. "Echo canceller");
  3189. return &_cmEchoCancelDC;
  3190. }
  3191. //)