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

cmDspKr.c 96KB

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