libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmDspKr.c 116KB

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