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 57KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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. kMidiFnTlId,
  231. kBegAudSmpIdxTlId,
  232. kEndAudSmpIdxTlId,
  233. kBegMidiSmpIdxTlId,
  234. kEndMidiSmpIdxTlId
  235. };
  236. cmDspClass_t _cmTimeLineDC;
  237. typedef struct
  238. {
  239. cmDspInst_t inst;
  240. cmTlH_t tlH;
  241. unsigned afIdx;
  242. } cmDspTimeLine_t;
  243. cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  244. {
  245. cmDspVarArg_t args[] =
  246. {
  247. { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
  248. { "path", kPrefixPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line data file prefix path" },
  249. { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
  250. { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
  251. { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
  252. { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
  253. { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
  254. { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
  255. { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
  256. { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
  257. { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
  258. { NULL, 0, 0, 0, 0 }
  259. };
  260. cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  261. cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
  262. cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
  263. cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
  264. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
  265. cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
  266. cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
  267. cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
  268. cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
  269. // create the UI control
  270. cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kPrefixPathTlId,kSelTlId,kCursTlId);
  271. p->tlH = cmTimeLineNullHandle;
  272. return &p->inst;
  273. }
  274. cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  275. {
  276. cmDspRC_t rc = kOkDspRC;
  277. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  278. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  279. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
  280. return rc;
  281. }
  282. cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  283. {
  284. cmDspRC_t rc = kOkDspRC;
  285. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  286. cmDspApplyAllDefaults(ctx,inst);
  287. const cmChar_t* tlFn;
  288. const cmChar_t* tlPrePath = cmDspStrcz(inst,kPrefixPathTlId);
  289. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  290. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn, tlPrePath ) != kOkTlRC )
  291. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  292. return rc;
  293. }
  294. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  295. {
  296. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  297. switch( evt->dstVarId )
  298. {
  299. case kPrefixPathTlId:
  300. cmDspSetEvent(ctx,inst,evt);
  301. break;
  302. case kCursTlId:
  303. cmDspSetEvent(ctx,inst,evt);
  304. break;
  305. case kResetTlId:
  306. case kSelTlId:
  307. {
  308. unsigned markerId;
  309. cmDspSetEvent(ctx,inst,evt);
  310. // get the id of the selected marker
  311. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  312. {
  313. // get the marker object
  314. cmTlObj_t* op;
  315. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  316. {
  317. assert(op->typeId == kMarkerTlId);
  318. p->afIdx = op->begSmpIdx;
  319. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  320. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  321. // locate the audio file assoc'd with the marker
  322. cmTlAudioFile_t* afp;
  323. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  324. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  325. // locate the midi file assoc'd with the marker
  326. cmTlMidiFile_t* mfp;
  327. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  328. {
  329. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  330. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  331. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  332. }
  333. }
  334. }
  335. }
  336. break;
  337. default:
  338. {assert(0);}
  339. }
  340. return kOkDspRC;
  341. }
  342. struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx )
  343. {
  344. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  345. NULL,
  346. _cmDspTimeLineAlloc,
  347. _cmDspTimeLineFree,
  348. _cmDspTimeLineReset,
  349. NULL,
  350. _cmDspTimeLineRecv,
  351. NULL,NULL,
  352. "Time Line control.");
  353. return &_cmTimeLineDC;
  354. }
  355. //==========================================================================================================================================
  356. // Score UI Object
  357. enum
  358. {
  359. kFnScId,
  360. kSelScId,
  361. kSendScId,
  362. kStatusScId,
  363. kD0ScId,
  364. kD1ScId,
  365. kSmpIdxScId,
  366. kLocIdxScId,
  367. kEvtIdxScId,
  368. kDynScId,
  369. kValTypeScId,
  370. kValueScId
  371. };
  372. cmDspClass_t _cmScoreDC;
  373. typedef struct
  374. {
  375. cmDspInst_t inst;
  376. cmScH_t scH;
  377. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  378. } cmDspScore_t;
  379. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  380. {
  381. cmDspVarArg_t args[] =
  382. {
  383. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  384. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  385. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  386. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  387. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  388. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  389. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  390. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  391. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  392. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  393. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  394. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  395. { NULL, 0, 0, 0, 0 }
  396. };
  397. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  398. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  399. // create the UI control
  400. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId);
  401. p->scH = cmScNullHandle;
  402. return &p->inst;
  403. }
  404. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  405. {
  406. cmDspRC_t rc = kOkDspRC;
  407. cmDspScore_t* p = (cmDspScore_t*)inst;
  408. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  409. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  410. return rc;
  411. }
  412. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  413. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  414. {
  415. cmDspInst_t* inst = (cmDspInst_t*)arg;
  416. cmDspScore_t* p = (cmDspScore_t*)inst;
  417. cmScMsg_t m;
  418. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  419. {
  420. switch( m.typeId )
  421. {
  422. case kDynMsgScId:
  423. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  424. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  425. break;
  426. case kVarMsgScId:
  427. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  428. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  429. break;
  430. default:
  431. { assert(0); }
  432. }
  433. }
  434. }
  435. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  436. {
  437. cmDspRC_t rc = kOkDspRC;
  438. cmDspScore_t* p = (cmDspScore_t*)inst;
  439. const cmChar_t* tlFn = NULL;
  440. unsigned* dynRefArray = NULL;
  441. unsigned dynRefCnt = 0;
  442. cmDspApplyAllDefaults(ctx,inst);
  443. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  444. {
  445. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  446. goto errLabel;
  447. }
  448. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  449. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, cmSymTblNullHandle ) != kOkTlRC )
  450. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  451. errLabel:
  452. return rc;
  453. }
  454. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  455. {
  456. cmDspScore_t* p = (cmDspScore_t*)inst;
  457. if( evt->dstVarId == kSendScId )
  458. {
  459. unsigned selIdx;
  460. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  461. {
  462. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  463. cmScoreClearPerfInfo(p->scH);
  464. }
  465. return kOkDspRC;
  466. }
  467. cmDspSetEvent(ctx,inst,evt);
  468. switch( evt->dstVarId )
  469. {
  470. case kSelScId:
  471. cmScoreClearPerfInfo(p->scH);
  472. break;
  473. case kStatusScId:
  474. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  475. break;
  476. case kLocIdxScId:
  477. {
  478. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  479. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  480. // this call may result in callbacks to _cmDspScoreCb()
  481. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  482. }
  483. break;
  484. }
  485. return kOkDspRC;
  486. }
  487. struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
  488. {
  489. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  490. NULL,
  491. _cmDspScoreAlloc,
  492. _cmDspScoreFree,
  493. _cmDspScoreReset,
  494. NULL,
  495. _cmDspScoreRecv,
  496. NULL,NULL,
  497. "Score control.");
  498. return &_cmScoreDC;
  499. }
  500. //==========================================================================================================================================
  501. // MIDI File Player
  502. enum
  503. {
  504. kFnMfId,
  505. kSelMfId,
  506. kBsiMfId,
  507. kEsiMfId,
  508. kStatusMfId,
  509. kD0MfId,
  510. kD1MfId,
  511. kSmpIdxMfId
  512. };
  513. cmDspClass_t _cmMidiFilePlayDC;
  514. typedef struct
  515. {
  516. cmDspInst_t inst;
  517. cmMidiFileH_t mfH;
  518. unsigned curMsgIdx; // current midi file msg index
  519. int csi; // current sample index
  520. int bsi; // starting sample index
  521. int esi; // ending sample index
  522. unsigned startSymId;
  523. unsigned stopSymId;
  524. unsigned contSymId;
  525. bool errFl;
  526. } cmDspMidiFilePlay_t;
  527. /*
  528. 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  529. These indexes are relative to the start of the file.
  530. When the player recieves a 'start' msg it sets the current sample index
  531. 'si' to 'bsi' and begins scanning for the next note to play.
  532. On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  533. si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  534. */
  535. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  536. {
  537. cmDspVarArg_t args[] =
  538. {
  539. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  540. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  541. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  542. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  543. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  544. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  545. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  546. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  547. { NULL, 0, 0, 0, 0 }
  548. };
  549. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  550. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  551. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  552. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  553. p->mfH = cmMidiFileNullHandle;
  554. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  555. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  556. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  557. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  558. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  559. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  560. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  561. return &p->inst;
  562. }
  563. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  564. {
  565. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  566. if( cmMidiFileClose(&p->mfH) )
  567. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  568. return kOkDspRC;
  569. }
  570. // return the index of the msg following smpIdx
  571. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  572. {
  573. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  574. if( cmMidiFileIsValid(p->mfH) == false )
  575. {
  576. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  577. return cmInvalidIdx;
  578. }
  579. unsigned i;
  580. unsigned n = cmMidiFileMsgCount(p->mfH);
  581. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  582. for(i=0; i<n; ++i)
  583. if( a[i]->dtick > smpIdx )
  584. break;
  585. return i==n ? cmInvalidIdx : i;
  586. }
  587. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  588. {
  589. cmDspRC_t rc = kOkDspRC;
  590. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  591. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  592. p->errFl = false;
  593. if( fn==NULL || strlen(fn)==0 )
  594. return rc;
  595. if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
  596. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  597. else
  598. {
  599. p->curMsgIdx = 0;
  600. p->bsi = cmDspInt(inst,kBsiMfId);
  601. p->esi = cmDspInt(inst,kEsiMfId);
  602. p->csi = 0;
  603. // force the first msg to occurr one quarter note into the file
  604. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  605. // convert midi msg times to absolute time in samples
  606. cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  607. }
  608. return rc;
  609. }
  610. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  611. {
  612. cmDspApplyAllDefaults(ctx,inst);
  613. return _cmDspMidiFilePlayOpen(ctx,inst);
  614. }
  615. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  616. {
  617. cmDspRC_t rc = kOkDspRC;
  618. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  619. unsigned sPc = cmDspSamplesPerCycle(ctx);
  620. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  621. {
  622. if( cmMidiFileIsValid(p->mfH) == false )
  623. {
  624. if( p->errFl==false )
  625. {
  626. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  627. p->errFl = true;
  628. }
  629. return rc;
  630. }
  631. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  632. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  633. for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
  634. {
  635. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  636. switch( mp->status )
  637. {
  638. case kNoteOnMdId:
  639. case kCtlMdId:
  640. cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
  641. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  642. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  643. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  644. break;
  645. }
  646. }
  647. }
  648. p->csi += sPc;
  649. return rc;
  650. }
  651. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  652. {
  653. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  654. cmDspSetEvent(ctx,inst,evt);
  655. switch(evt->dstVarId)
  656. {
  657. case kFnMfId:
  658. _cmDspMidiFilePlayOpen(ctx, inst );
  659. break;
  660. case kSelMfId:
  661. {
  662. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  663. {
  664. p->csi = cmDspInt(inst,kBsiMfId);
  665. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  666. }
  667. break;
  668. }
  669. }
  670. return kOkDspRC;
  671. }
  672. struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  673. {
  674. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  675. NULL,
  676. _cmDspMidiFilePlayAlloc,
  677. _cmDspMidiFilePlayFree,
  678. _cmDspMidiFilePlayReset,
  679. _cmDspMidiFilePlayExec,
  680. _cmDspMidiFilePlayRecv,
  681. NULL,NULL,
  682. "Time tagged text file.");
  683. return &_cmMidiFilePlayDC;
  684. }
  685. //==========================================================================================================================================
  686. enum
  687. {
  688. kFnSfId,
  689. kBufCntSfId,
  690. kMinLkAhdSfId,
  691. kMaxWndCntSfId,
  692. kMinVelSfId,
  693. kIndexSfId,
  694. kStatusSfId,
  695. kD0SfId,
  696. kD1SfId,
  697. kSmpIdxSfId,
  698. kCmdSfId,
  699. kOutSfId,
  700. kDynSfId,
  701. kEvenSfId,
  702. kTempoSfId,
  703. kCostSfId,
  704. kSymSfId
  705. };
  706. cmDspClass_t _cmScFolDC;
  707. struct cmDspScFol_str;
  708. typedef struct
  709. {
  710. cmDspCtx_t* ctx;
  711. struct cmDspScFol_str* sfp;
  712. } cmDspScFolCbArg_t;
  713. typedef struct cmDspScFol_str
  714. {
  715. cmDspInst_t inst;
  716. cmScMatcher* sfp;
  717. cmScMeas* smp;
  718. cmScH_t scH;
  719. cmDspScFolCbArg_t arg;
  720. unsigned printSymId;
  721. unsigned quietSymId;
  722. } cmDspScFol_t;
  723. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  724. {
  725. cmDspVarArg_t args[] =
  726. {
  727. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  728. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Event buffer element count." },
  729. { "lkahd", kMinLkAhdSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum window look-ahead."},
  730. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl, "Maximum window length."},
  731. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum velocity."},
  732. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  733. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  734. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  735. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  736. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  737. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  738. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Current score location index."},
  739. { "dyn", kDynSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Dynamic value."},
  740. { "even", kEvenSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Evenness value."},
  741. { "tempo", kTempoSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Tempo value."},
  742. { "cost", kCostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Match cost value."},
  743. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  744. { NULL, 0, 0, 0, 0, NULL }
  745. };
  746. cmDspScFol_t* p;
  747. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  748. return NULL;
  749. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  750. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  751. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  752. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  753. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  754. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  755. cmDspSetDefaultUInt( ctx, &p->inst, kMinLkAhdSfId, 0, 3);
  756. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  757. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  758. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  759. cmDspSetDefaultDouble( ctx, &p->inst, kDynSfId, 0, 0);
  760. cmDspSetDefaultDouble( ctx, &p->inst, kEvenSfId, 0, 0);
  761. cmDspSetDefaultDouble( ctx, &p->inst, kTempoSfId, 0, 0);
  762. cmDspSetDefaultDouble( ctx, &p->inst, kCostSfId, 0, 0);
  763. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  764. return &p->inst;
  765. }
  766. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  767. {
  768. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  769. cmScMatcherFree(&p->sfp);
  770. cmScMeasFree(&p->smp);
  771. cmScoreFinalize(&p->scH);
  772. return kOkDspRC;
  773. }
  774. // This is a callback function from cmScMatcherExec() which is called when
  775. // this cmDspFol object receives a new score location index.
  776. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  777. {
  778. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  779. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  780. {
  781. cmDspInst_t* inst = &(ap->sfp->inst);
  782. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  783. unsigned i;
  784. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  785. if(ap->sfp->smp->set[i].value != DBL_MAX )
  786. {
  787. switch( ap->sfp->smp->set[i].sp->varId )
  788. {
  789. case kEvenVarScId:
  790. cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  791. break;
  792. case kDynVarScId:
  793. cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  794. break;
  795. case kTempoVarScId:
  796. cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  797. break;
  798. default:
  799. { assert(0); }
  800. }
  801. cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  802. // Set the values in the global variable storage
  803. cmDspValue_t vv,cv;
  804. unsigned j;
  805. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  806. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  807. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  808. {
  809. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  810. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  811. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  812. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  813. }
  814. }
  815. /*
  816. // trigger 'section' starts
  817. for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  818. {
  819. const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  820. if( locPtr->begSectPtr != NULL )
  821. cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  822. }
  823. */
  824. }
  825. }
  826. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  827. {
  828. cmDspRC_t rc = kOkDspRC;
  829. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  830. const cmChar_t* fn;
  831. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  832. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  833. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  834. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  835. if( cmScoreIsValid(p->scH) )
  836. {
  837. unsigned* dynRefArray = NULL;
  838. unsigned dynRefCnt = 0;
  839. // initialize the cmScMatcher
  840. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  841. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  842. // read the dynamics reference array
  843. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  844. {
  845. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  846. goto errLabel;
  847. }
  848. // initialize the cmScMeas object.
  849. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  850. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  851. }
  852. errLabel:
  853. return rc;
  854. }
  855. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  856. {
  857. cmDspRC_t rc;
  858. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  859. return rc;
  860. return _cmDspScFolOpenScore(ctx,inst);
  861. }
  862. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  863. {
  864. cmDspRC_t rc = kOkDspRC;
  865. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  866. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  867. {
  868. switch( evt->dstVarId )
  869. {
  870. case kIndexSfId:
  871. if( cmScoreIsValid(p->scH) )
  872. {
  873. if( cmScMeasReset( p->smp ) != cmOkRC )
  874. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  875. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  876. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  877. }
  878. break;
  879. case kStatusSfId:
  880. if( cmScoreIsValid(p->scH))
  881. {
  882. unsigned scLocIdx = cmInvalidIdx;
  883. // setup the cmScMeas() callback arg.
  884. p->arg.ctx = ctx;
  885. p->arg.sfp = p;
  886. p->sfp->cbArg = &p->arg;
  887. // this call may result in a callback to _cmScFolMatcherCb()
  888. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  889. if( scLocIdx != cmInvalidIdx )
  890. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  891. }
  892. break;
  893. case kFnSfId:
  894. _cmDspScFolOpenScore(ctx,inst);
  895. break;
  896. case kCmdSfId:
  897. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  898. p->sfp->printFl = true;
  899. else
  900. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  901. p->sfp->printFl = false;
  902. break;
  903. }
  904. }
  905. return rc;
  906. }
  907. struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
  908. {
  909. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  910. NULL,
  911. _cmDspScFolAlloc,
  912. _cmDspScFolFree,
  913. _cmDspScFolReset,
  914. NULL,
  915. _cmDspScFolRecv,
  916. NULL,NULL,
  917. "Score Follower");
  918. return &_cmScFolDC;
  919. }
  920. //==========================================================================================================================================
  921. enum
  922. {
  923. kScLocIdxMdId,
  924. kResetIdxMdId,
  925. kCmdMdId
  926. };
  927. cmDspClass_t _cmModulatorDC;
  928. typedef struct
  929. {
  930. cmDspInst_t inst;
  931. cmScModulator* mp;
  932. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  933. cmChar_t* fn;
  934. cmChar_t* modLabel;
  935. unsigned onSymId;
  936. unsigned offSymId;
  937. } cmDspScMod_t;
  938. void _cmDspScModCb( void* arg, unsigned varSymId, double value )
  939. {
  940. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  941. cmDspVar_t* varPtr;
  942. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  943. return;
  944. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  945. }
  946. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  947. {
  948. va_list vl1;
  949. va_copy(vl1,vl);
  950. cmDspVarArg_t args[] =
  951. {
  952. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  953. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  954. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  955. { NULL, 0, 0, 0, 0 }
  956. };
  957. // validate the argument count
  958. if( va_cnt != 2 )
  959. {
  960. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  961. return NULL;
  962. }
  963. // read the modulator file and label strings
  964. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  965. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  966. va_end(vl1);
  967. // validate the file
  968. if( fn==NULL || cmFsIsFile(fn)==false )
  969. {
  970. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  971. return NULL;
  972. }
  973. // allocate the internal modulator object
  974. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  975. if(mp == NULL )
  976. {
  977. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  978. return NULL;
  979. }
  980. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  981. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  982. cmDspVarArg_t a[ argCnt+1 ];
  983. unsigned i;
  984. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  985. for(i=fixArgCnt; i<argCnt; ++i)
  986. {
  987. unsigned varIdx = i - fixArgCnt;
  988. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  989. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  990. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  991. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  992. }
  993. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  994. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  995. p->fn = cmMemAllocStr(fn);
  996. p->modLabel = cmMemAllocStr(modLabel);
  997. p->mp = mp;
  998. p->onSymId = cmSymTblId(ctx->stH,"on");
  999. p->offSymId = cmSymTblId(ctx->stH,"off");
  1000. mp->cbArg = p; // set the modulator callback arg
  1001. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1002. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1003. return &p->inst;
  1004. }
  1005. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1006. {
  1007. cmDspRC_t rc = kOkDspRC;
  1008. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1009. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1010. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1011. cmMemFree(p->fn);
  1012. cmMemFree(p->modLabel);
  1013. return rc;
  1014. }
  1015. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1016. {
  1017. cmDspRC_t rc = kOkDspRC;
  1018. cmDspApplyAllDefaults(ctx,inst);
  1019. return rc;
  1020. }
  1021. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1022. {
  1023. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1024. cmDspSetEvent(ctx,inst,evt);
  1025. switch( evt->dstVarId )
  1026. {
  1027. case kResetIdxMdId:
  1028. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1029. break;
  1030. case kCmdMdId:
  1031. {
  1032. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1033. if( symId == p->onSymId )
  1034. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1035. }
  1036. break;
  1037. }
  1038. return kOkDspRC;
  1039. }
  1040. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1041. {
  1042. cmDspRC_t rc = kOkDspRC;
  1043. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1044. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1045. {
  1046. p->tmp_ctx = ctx;
  1047. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1048. }
  1049. return rc;
  1050. }
  1051. struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
  1052. {
  1053. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1054. NULL,
  1055. _cmDspScModAlloc,
  1056. _cmDspScModFree,
  1057. _cmDspScModReset,
  1058. _cmDspScModExec,
  1059. _cmDspScModRecv,
  1060. NULL,NULL,
  1061. "Score Driven Variable Modulator.");
  1062. return &_cmModulatorDC;
  1063. }
  1064. //==========================================================================================================================================
  1065. enum
  1066. {
  1067. kInChCntGsId,
  1068. kOutGroupCntGsId,
  1069. kGroupSelIdxGsId,
  1070. kBaseInFloatGsId
  1071. };
  1072. cmDspClass_t _cmGSwitchDC;
  1073. typedef struct
  1074. {
  1075. cmDspInst_t inst;
  1076. unsigned iChCnt;
  1077. unsigned oGroupCnt;
  1078. unsigned baseInFloatGsId;
  1079. unsigned baseInSymGsId;
  1080. unsigned baseInBoolGsId;
  1081. unsigned baseOutFloatGsId;
  1082. unsigned baseOutSymGsId;
  1083. unsigned baseOutBoolGsId;
  1084. } cmDspGSwitch_t;
  1085. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1086. {
  1087. va_list vl1;
  1088. va_copy(vl1,vl);
  1089. cmDspVarArg_t args[] =
  1090. {
  1091. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1092. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1093. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1094. { NULL, 0, 0, 0, 0 }
  1095. };
  1096. // validate the argument count
  1097. if( va_cnt != 2 )
  1098. {
  1099. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1100. return NULL;
  1101. }
  1102. // read the input ch and output group count
  1103. unsigned iChCnt = va_arg(vl1,unsigned);
  1104. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1105. va_end(vl1);
  1106. // validate the channel counts
  1107. if( iChCnt == 0 || oGroupCnt==0 )
  1108. {
  1109. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1110. return NULL;
  1111. }
  1112. unsigned typeCnt = 3; // i.e. float,sym,bool
  1113. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1114. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1115. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1116. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1117. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1118. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1119. unsigned fixArgCnt = 3;
  1120. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1121. unsigned argCnt = fixArgCnt + varArgCnt;
  1122. cmDspVarArg_t a[ argCnt+1 ];
  1123. unsigned i;
  1124. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1125. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1126. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1127. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1128. unsigned labelCharCnt = 63;
  1129. cmChar_t label[labelCharCnt+1];
  1130. label[labelCharCnt] = 0;
  1131. unsigned gsid = baseOutFloatGsId;
  1132. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1133. {
  1134. snprintf(label,labelCharCnt,"f-out-%i",i);
  1135. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1136. }
  1137. gsid = baseOutSymGsId;
  1138. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1139. {
  1140. snprintf(label,labelCharCnt,"s-out-%i",i);
  1141. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1142. }
  1143. gsid = baseOutBoolGsId;
  1144. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1145. {
  1146. snprintf(label,labelCharCnt,"b-out-%i",i);
  1147. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1148. }
  1149. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1150. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1151. p->iChCnt = iChCnt;
  1152. p->oGroupCnt = oGroupCnt;
  1153. p->baseInFloatGsId = baseInFloatGsId;
  1154. p->baseInSymGsId = baseInSymGsId;
  1155. p->baseInBoolGsId = baseInBoolGsId;
  1156. p->baseOutFloatGsId = baseOutFloatGsId;
  1157. p->baseOutSymGsId = baseOutSymGsId;
  1158. p->baseOutBoolGsId = baseOutBoolGsId;
  1159. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1160. return &p->inst;
  1161. }
  1162. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1163. {
  1164. cmDspRC_t rc = kOkDspRC;
  1165. cmDspApplyAllDefaults(ctx,inst);
  1166. return rc;
  1167. }
  1168. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1169. {
  1170. cmDspRC_t rc = kOkDspRC;
  1171. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1172. // if this is the group selector
  1173. if( evt->dstVarId == kGroupSelIdxGsId )
  1174. {
  1175. unsigned idx;
  1176. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1177. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1178. else
  1179. cmDspSetEvent(ctx,inst,evt);
  1180. return rc;
  1181. }
  1182. // get the group selector
  1183. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1184. assert( groupIdx < p->oGroupCnt);
  1185. // if this is a float input
  1186. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1187. {
  1188. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1189. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1190. return rc;
  1191. }
  1192. // if this is a symbol input
  1193. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1194. {
  1195. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1196. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1197. return rc;
  1198. }
  1199. // if this is a bool input
  1200. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1201. {
  1202. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1203. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1204. return rc;
  1205. }
  1206. return rc;
  1207. }
  1208. struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1209. {
  1210. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1211. NULL,
  1212. _cmDspGSwitchAlloc,
  1213. NULL,
  1214. _cmDspGSwitchReset,
  1215. NULL,
  1216. _cmDspGSwitchRecv,
  1217. NULL,NULL,
  1218. "Ganged switch.");
  1219. return &_cmGSwitchDC;
  1220. }
  1221. //==========================================================================================================================================
  1222. enum
  1223. {
  1224. kMinInSrId,
  1225. kMaxInSrId,
  1226. kMinOutSrId,
  1227. kMaxOutSrId,
  1228. kValInSrId,
  1229. kValOutSrId,
  1230. };
  1231. cmDspClass_t _cmScaleRangeDC;
  1232. typedef struct
  1233. {
  1234. cmDspInst_t inst;
  1235. } cmDspScaleRange_t;
  1236. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1237. {
  1238. va_list vl1;
  1239. va_copy(vl1,vl);
  1240. cmDspVarArg_t args[] =
  1241. {
  1242. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1243. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1244. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1245. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1246. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1247. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1248. { NULL, 0, 0, 0, 0 }
  1249. };
  1250. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1251. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1252. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1253. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1254. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1255. return &p->inst;
  1256. }
  1257. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1258. {
  1259. cmDspRC_t rc = kOkDspRC;
  1260. cmDspApplyAllDefaults(ctx,inst);
  1261. return rc;
  1262. }
  1263. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1264. {
  1265. cmDspRC_t rc = kOkDspRC;
  1266. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1267. cmDspSetEvent(ctx,inst,evt);
  1268. if( evt->dstVarId == kValInSrId )
  1269. {
  1270. double val = cmDspDouble(inst,kValInSrId);
  1271. double min_in = cmDspDouble(inst,kMinInSrId);
  1272. double max_in = cmDspDouble(inst,kMaxInSrId);
  1273. double min_out = cmDspDouble(inst,kMinOutSrId);
  1274. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1275. double x = cmMax(min_in,cmMin(max_in,val));
  1276. x = (x - min_in)/(max_in - min_in);
  1277. x = min_out + x * (max_out - min_out);
  1278. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1279. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1280. }
  1281. return rc;
  1282. }
  1283. struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1284. {
  1285. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1286. NULL,
  1287. _cmDspScaleRangeAlloc,
  1288. NULL,
  1289. _cmDspScaleRangeReset,
  1290. NULL,
  1291. _cmDspScaleRangeRecv,
  1292. NULL,NULL,
  1293. "Scale a value inside an input range to a value in the output range.");
  1294. return &_cmScaleRangeDC;
  1295. }
  1296. //==========================================================================================================================================
  1297. enum
  1298. {
  1299. kCntAmId,
  1300. kSflocAmId,
  1301. kLocAmId,
  1302. kTypeAmId,
  1303. kValueAmId,
  1304. kCstAmId,
  1305. kCmdAmId,
  1306. kScLocAmId,
  1307. kEvenAmId,
  1308. kDynAmId,
  1309. kTempoAmId,
  1310. kCostAmId
  1311. };
  1312. cmDspClass_t _cmActiveMeasDC;
  1313. typedef struct
  1314. {
  1315. unsigned loc;
  1316. unsigned type;
  1317. double value;
  1318. double cost;
  1319. } cmDspActiveMeasRecd_t;
  1320. int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1321. {
  1322. return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1323. }
  1324. typedef struct
  1325. {
  1326. cmDspInst_t inst;
  1327. unsigned addSymId;
  1328. unsigned clearSymId;
  1329. unsigned printSymId;
  1330. unsigned rewindSymId;
  1331. cmDspActiveMeasRecd_t* array; // array[cnt]
  1332. unsigned cnt;
  1333. unsigned nextEmptyIdx;
  1334. unsigned nextFullIdx;
  1335. } cmDspActiveMeas_t;
  1336. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1337. {
  1338. cmDspVarArg_t args[] =
  1339. {
  1340. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1341. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1342. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1343. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type." },
  1344. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1345. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1346. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1347. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1348. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1349. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1350. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1351. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1352. { NULL, 0, 0, 0, 0 }
  1353. };
  1354. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1355. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1356. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1357. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1358. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1359. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,100);
  1360. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1361. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1362. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1363. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1364. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1365. return &p->inst;
  1366. }
  1367. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1368. {
  1369. unsigned i;
  1370. for(i=0; i<p->nextEmptyIdx; ++i)
  1371. {
  1372. const cmChar_t* label = "<null>";
  1373. switch( p->array[i].type )
  1374. {
  1375. case kEvenVarScId: label="even "; break;
  1376. case kDynVarScId: label="dyn "; break;
  1377. case kTempoVarScId: label="tempo"; break;
  1378. default:
  1379. { assert(0); }
  1380. }
  1381. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",p->array[i].loc,label,p->array[i].value,p->array[i].cost);
  1382. }
  1383. return kOkDspRC;
  1384. }
  1385. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1386. {
  1387. p->nextEmptyIdx = 0;
  1388. p->nextFullIdx = cmInvalidIdx;
  1389. return kOkDspRC;
  1390. }
  1391. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1392. {
  1393. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1394. _cmDspActiveMeasClear(ctx,p);
  1395. cmMemPtrFree(&p->array);
  1396. return kOkDspRC;
  1397. }
  1398. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1399. {
  1400. cmDspRC_t rc = kOkDspRC;
  1401. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1402. cmDspApplyAllDefaults(ctx,inst);
  1403. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1404. _cmDspActiveMeasFree(ctx,inst,evt);
  1405. p->array = cmMemAllocZ(cmDspActiveMeasRecd_t,cnt);
  1406. p->cnt = cnt;
  1407. return rc;
  1408. }
  1409. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1410. {
  1411. cmDspRC_t rc = kOkDspRC;
  1412. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1413. cmDspSetEvent(ctx,inst,evt);
  1414. switch( evt->dstVarId )
  1415. {
  1416. case kSflocAmId:
  1417. if( p->nextFullIdx != cmInvalidIdx )
  1418. {
  1419. // get the recv'd score location
  1420. unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1421. unsigned prvLoc = cmInvalidIdx;
  1422. // for each remaining avail record
  1423. for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1424. {
  1425. cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1426. // if this records score location is after the recv'd score loc then we're done
  1427. if( r->loc > sflocIdx )
  1428. break;
  1429. // deterimine the records type
  1430. unsigned varId = cmInvalidId;
  1431. switch( r->type )
  1432. {
  1433. case kEvenVarScId: varId = kEvenAmId; break;
  1434. case kDynVarScId: varId = kDynAmId; break;
  1435. case kTempoVarScId: varId = kTempoAmId; break;
  1436. default:
  1437. { assert(0); }
  1438. }
  1439. // if this score location has not yet been sent then send it now
  1440. if( prvLoc != r->loc )
  1441. cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1442. // transmit the records value and cost
  1443. cmDspSetDouble(ctx,inst,varId,r->value);
  1444. cmDspSetDouble(ctx,inst,kCostAmId,r->value);
  1445. prvLoc = r->loc;
  1446. }
  1447. }
  1448. break;
  1449. case kCmdAmId:
  1450. {
  1451. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1452. if( cmdSymId == p->addSymId )
  1453. {
  1454. if( p->nextEmptyIdx >= p->cnt )
  1455. cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1456. else
  1457. {
  1458. cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1459. r->loc = cmDspUInt( inst,kLocAmId);
  1460. r->type = cmDspUInt( inst,kTypeAmId);
  1461. r->value = cmDspDouble(inst,kValueAmId);
  1462. r->cost = cmDspDouble(inst,kCstAmId);
  1463. p->nextEmptyIdx += 1;
  1464. qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1465. if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1466. p->nextFullIdx = 0;
  1467. }
  1468. }
  1469. if( cmdSymId == p->clearSymId )
  1470. rc = _cmDspActiveMeasClear(ctx,p);
  1471. else
  1472. if( cmdSymId == p->printSymId )
  1473. rc = _cmDspActiveMeasPrint(ctx,p);
  1474. else
  1475. if(cmdSymId == p->rewindSymId )
  1476. p->nextFullIdx = 0;
  1477. }
  1478. break;
  1479. }
  1480. return rc;
  1481. }
  1482. struct cmDspClass_str* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1483. {
  1484. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1485. NULL,
  1486. _cmDspActiveMeasAlloc,
  1487. _cmDspActiveMeasFree,
  1488. _cmDspActiveMeasReset,
  1489. NULL,
  1490. _cmDspActiveMeasRecv,
  1491. NULL,NULL,
  1492. "Scale a value inside an input range to a value in the output range.");
  1493. return &_cmActiveMeasDC;
  1494. }