libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDspKr.c 117KB

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