libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspKr.c 83KB

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