libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmDspKr.c 64KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  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. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, cmSymTblNullHandle ) != kOkTlRC )
  460. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  461. errLabel:
  462. return rc;
  463. }
  464. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  465. {
  466. cmDspScore_t* p = (cmDspScore_t*)inst;
  467. if( evt->dstVarId == kSendScId )
  468. {
  469. unsigned selIdx;
  470. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  471. {
  472. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  473. cmScoreClearPerfInfo(p->scH);
  474. }
  475. return kOkDspRC;
  476. }
  477. cmDspSetEvent(ctx,inst,evt);
  478. switch( evt->dstVarId )
  479. {
  480. case kSelScId:
  481. cmScoreClearPerfInfo(p->scH);
  482. break;
  483. case kStatusScId:
  484. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  485. break;
  486. case kLocIdxScId:
  487. {
  488. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  489. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  490. // this call may result in callbacks to _cmDspScoreCb()
  491. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  492. }
  493. break;
  494. }
  495. return kOkDspRC;
  496. }
  497. struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
  498. {
  499. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  500. NULL,
  501. _cmDspScoreAlloc,
  502. _cmDspScoreFree,
  503. _cmDspScoreReset,
  504. NULL,
  505. _cmDspScoreRecv,
  506. NULL,NULL,
  507. "Score control.");
  508. return &_cmScoreDC;
  509. }
  510. //==========================================================================================================================================
  511. // MIDI File Player
  512. enum
  513. {
  514. kFnMfId,
  515. kSelMfId,
  516. kBsiMfId,
  517. kEsiMfId,
  518. kStatusMfId,
  519. kD0MfId,
  520. kD1MfId,
  521. kSmpIdxMfId,
  522. kIdMfId
  523. };
  524. cmDspClass_t _cmMidiFilePlayDC;
  525. typedef struct
  526. {
  527. cmDspInst_t inst;
  528. cmMidiFileH_t mfH;
  529. unsigned curMsgIdx; // current midi file msg index
  530. int csi; // current sample index
  531. int bsi; // starting sample index
  532. int esi; // ending sample index
  533. unsigned startSymId;
  534. unsigned stopSymId;
  535. unsigned contSymId;
  536. bool errFl;
  537. } cmDspMidiFilePlay_t;
  538. /*
  539. 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  540. These indexes are relative to the start of the file.
  541. When the player recieves a 'start' msg it sets the current sample index
  542. 'si' to 'bsi' and begins scanning for the next note to play.
  543. On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  544. si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  545. */
  546. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  547. {
  548. cmDspVarArg_t args[] =
  549. {
  550. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  551. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  552. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  553. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  554. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  555. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  556. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  557. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  558. { "id", kIdMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI file msg unique id."},
  559. { NULL, 0, 0, 0, 0 }
  560. };
  561. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  562. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  563. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  564. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  565. p->mfH = cmMidiFileNullHandle;
  566. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  567. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  568. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  569. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  570. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  571. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  572. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  573. return &p->inst;
  574. }
  575. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  576. {
  577. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  578. if( cmMidiFileClose(&p->mfH) )
  579. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  580. return kOkDspRC;
  581. }
  582. // return the index of the msg following smpIdx
  583. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  584. {
  585. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  586. if( cmMidiFileIsValid(p->mfH) == false )
  587. {
  588. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  589. return cmInvalidIdx;
  590. }
  591. unsigned i;
  592. unsigned n = cmMidiFileMsgCount(p->mfH);
  593. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  594. for(i=0; i<n; ++i)
  595. if( a[i]->dtick > smpIdx )
  596. break;
  597. return i==n ? cmInvalidIdx : i;
  598. }
  599. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  600. {
  601. cmDspRC_t rc = kOkDspRC;
  602. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  603. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  604. p->errFl = false;
  605. if( fn==NULL || strlen(fn)==0 )
  606. return rc;
  607. if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
  608. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  609. else
  610. {
  611. p->curMsgIdx = 0;
  612. p->bsi = cmDspInt(inst,kBsiMfId);
  613. p->esi = cmDspInt(inst,kEsiMfId);
  614. p->csi = 0;
  615. // force the first msg to occurr one quarter note into the file
  616. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  617. // convert midi msg times to absolute time in samples
  618. cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  619. }
  620. return rc;
  621. }
  622. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  623. {
  624. cmDspApplyAllDefaults(ctx,inst);
  625. return _cmDspMidiFilePlayOpen(ctx,inst);
  626. }
  627. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  628. {
  629. cmDspRC_t rc = kOkDspRC;
  630. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  631. unsigned sPc = cmDspSamplesPerCycle(ctx);
  632. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  633. {
  634. if( cmMidiFileIsValid(p->mfH) == false )
  635. {
  636. if( p->errFl==false )
  637. {
  638. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  639. p->errFl = true;
  640. }
  641. return rc;
  642. }
  643. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  644. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  645. for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
  646. {
  647. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  648. switch( mp->status )
  649. {
  650. case kNoteOnMdId:
  651. case kCtlMdId:
  652. cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
  653. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  654. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  655. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  656. cmDspSetUInt(ctx,inst, kIdMfId, mp->uid);
  657. break;
  658. }
  659. }
  660. }
  661. p->csi += sPc;
  662. return rc;
  663. }
  664. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  665. {
  666. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  667. cmDspSetEvent(ctx,inst,evt);
  668. switch(evt->dstVarId)
  669. {
  670. case kFnMfId:
  671. _cmDspMidiFilePlayOpen(ctx, inst );
  672. break;
  673. case kSelMfId:
  674. {
  675. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  676. {
  677. p->csi = cmDspInt(inst,kBsiMfId);
  678. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  679. }
  680. break;
  681. }
  682. }
  683. return kOkDspRC;
  684. }
  685. struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  686. {
  687. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  688. NULL,
  689. _cmDspMidiFilePlayAlloc,
  690. _cmDspMidiFilePlayFree,
  691. _cmDspMidiFilePlayReset,
  692. _cmDspMidiFilePlayExec,
  693. _cmDspMidiFilePlayRecv,
  694. NULL,NULL,
  695. "MIDI file player.");
  696. return &_cmMidiFilePlayDC;
  697. }
  698. //==========================================================================================================================================
  699. enum
  700. {
  701. kFnSfId,
  702. kBufCntSfId,
  703. kMinLkAhdSfId,
  704. kMaxWndCntSfId,
  705. kMinVelSfId,
  706. kIndexSfId,
  707. kStatusSfId,
  708. kD0SfId,
  709. kD1SfId,
  710. kSmpIdxSfId,
  711. kCmdSfId,
  712. kOutSfId,
  713. kDynSfId,
  714. kEvenSfId,
  715. kTempoSfId,
  716. kCostSfId,
  717. kSymSfId
  718. };
  719. cmDspClass_t _cmScFolDC;
  720. struct cmDspScFol_str;
  721. typedef struct
  722. {
  723. cmDspCtx_t* ctx;
  724. struct cmDspScFol_str* sfp;
  725. } cmDspScFolCbArg_t;
  726. typedef struct cmDspScFol_str
  727. {
  728. cmDspInst_t inst;
  729. cmScMatcher* sfp;
  730. cmScMeas* smp;
  731. cmScH_t scH;
  732. cmDspScFolCbArg_t arg;
  733. unsigned printSymId;
  734. unsigned quietSymId;
  735. } cmDspScFol_t;
  736. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  737. {
  738. cmDspVarArg_t args[] =
  739. {
  740. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  741. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Event buffer element count." },
  742. { "lkahd", kMinLkAhdSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum window look-ahead."},
  743. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl, "Maximum window length."},
  744. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum velocity."},
  745. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  746. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  747. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  748. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  749. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  750. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  751. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Current score location index."},
  752. { "dyn", kDynSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Dynamic value."},
  753. { "even", kEvenSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Evenness value."},
  754. { "tempo", kTempoSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Tempo value."},
  755. { "cost", kCostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Match cost value."},
  756. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  757. { NULL, 0, 0, 0, 0, NULL }
  758. };
  759. cmDspScFol_t* p;
  760. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  761. return NULL;
  762. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  763. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  764. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  765. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  766. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  767. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  768. cmDspSetDefaultUInt( ctx, &p->inst, kMinLkAhdSfId, 0, 3);
  769. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  770. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  771. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  772. cmDspSetDefaultDouble( ctx, &p->inst, kDynSfId, 0, 0);
  773. cmDspSetDefaultDouble( ctx, &p->inst, kEvenSfId, 0, 0);
  774. cmDspSetDefaultDouble( ctx, &p->inst, kTempoSfId, 0, 0);
  775. cmDspSetDefaultDouble( ctx, &p->inst, kCostSfId, 0, 0);
  776. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  777. return &p->inst;
  778. }
  779. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  780. {
  781. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  782. cmScMatcherFree(&p->sfp);
  783. cmScMeasFree(&p->smp);
  784. cmScoreFinalize(&p->scH);
  785. return kOkDspRC;
  786. }
  787. // This is a callback function from cmScMatcherExec() which is called when
  788. // this cmDspFol object receives a new score location index.
  789. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  790. {
  791. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  792. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  793. {
  794. cmDspInst_t* inst = &(ap->sfp->inst);
  795. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  796. unsigned i;
  797. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  798. if(ap->sfp->smp->set[i].value != DBL_MAX )
  799. {
  800. switch( ap->sfp->smp->set[i].sp->varId )
  801. {
  802. case kEvenVarScId:
  803. cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  804. break;
  805. case kDynVarScId:
  806. cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  807. break;
  808. case kTempoVarScId:
  809. cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  810. break;
  811. default:
  812. { assert(0); }
  813. }
  814. cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  815. // Set the values in the global variable storage
  816. cmDspValue_t vv,cv;
  817. unsigned j;
  818. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  819. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  820. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  821. {
  822. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  823. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  824. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  825. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  826. }
  827. }
  828. /*
  829. // trigger 'section' starts
  830. for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  831. {
  832. const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  833. if( locPtr->begSectPtr != NULL )
  834. cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  835. }
  836. */
  837. }
  838. }
  839. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  840. {
  841. cmDspRC_t rc = kOkDspRC;
  842. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  843. const cmChar_t* fn;
  844. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  845. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  846. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  847. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  848. if( cmScoreIsValid(p->scH) )
  849. {
  850. unsigned* dynRefArray = NULL;
  851. unsigned dynRefCnt = 0;
  852. // initialize the cmScMatcher
  853. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  854. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  855. // read the dynamics reference array
  856. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  857. {
  858. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  859. goto errLabel;
  860. }
  861. // initialize the cmScMeas object.
  862. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  863. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  864. }
  865. errLabel:
  866. return rc;
  867. }
  868. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  869. {
  870. cmDspRC_t rc;
  871. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  872. return rc;
  873. return _cmDspScFolOpenScore(ctx,inst);
  874. }
  875. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  876. {
  877. cmDspRC_t rc = kOkDspRC;
  878. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  879. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  880. {
  881. switch( evt->dstVarId )
  882. {
  883. case kIndexSfId:
  884. if( cmScoreIsValid(p->scH) )
  885. {
  886. if( cmScMeasReset( p->smp ) != cmOkRC )
  887. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  888. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  889. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  890. }
  891. break;
  892. case kStatusSfId:
  893. if( cmScoreIsValid(p->scH))
  894. {
  895. unsigned scLocIdx = cmInvalidIdx;
  896. // setup the cmScMeas() callback arg.
  897. p->arg.ctx = ctx;
  898. p->arg.sfp = p;
  899. p->sfp->cbArg = &p->arg;
  900. // this call may result in a callback to _cmScFolMatcherCb()
  901. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  902. if( scLocIdx != cmInvalidIdx )
  903. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  904. }
  905. break;
  906. case kFnSfId:
  907. _cmDspScFolOpenScore(ctx,inst);
  908. break;
  909. case kCmdSfId:
  910. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  911. p->sfp->printFl = true;
  912. else
  913. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  914. p->sfp->printFl = false;
  915. break;
  916. }
  917. }
  918. return rc;
  919. }
  920. struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
  921. {
  922. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  923. NULL,
  924. _cmDspScFolAlloc,
  925. _cmDspScFolFree,
  926. _cmDspScFolReset,
  927. NULL,
  928. _cmDspScFolRecv,
  929. NULL,NULL,
  930. "Score Follower");
  931. return &_cmScFolDC;
  932. }
  933. //==========================================================================================================================================
  934. enum
  935. {
  936. kScLocIdxMdId,
  937. kResetIdxMdId,
  938. kCmdMdId
  939. };
  940. cmDspClass_t _cmModulatorDC;
  941. typedef struct
  942. {
  943. cmDspInst_t inst;
  944. cmScModulator* mp;
  945. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  946. cmChar_t* fn;
  947. cmChar_t* modLabel;
  948. unsigned onSymId;
  949. unsigned offSymId;
  950. } cmDspScMod_t;
  951. void _cmDspScModCb( void* arg, unsigned varSymId, double value )
  952. {
  953. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  954. cmDspVar_t* varPtr;
  955. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  956. return;
  957. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  958. }
  959. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  960. {
  961. va_list vl1;
  962. va_copy(vl1,vl);
  963. cmDspVarArg_t args[] =
  964. {
  965. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  966. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  967. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  968. { NULL, 0, 0, 0, 0 }
  969. };
  970. // validate the argument count
  971. if( va_cnt != 2 )
  972. {
  973. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  974. return NULL;
  975. }
  976. // read the modulator file and label strings
  977. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  978. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  979. va_end(vl1);
  980. // validate the file
  981. if( fn==NULL || cmFsIsFile(fn)==false )
  982. {
  983. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  984. return NULL;
  985. }
  986. // allocate the internal modulator object
  987. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  988. if(mp == NULL )
  989. {
  990. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  991. return NULL;
  992. }
  993. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  994. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  995. cmDspVarArg_t a[ argCnt+1 ];
  996. unsigned i;
  997. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  998. for(i=fixArgCnt; i<argCnt; ++i)
  999. {
  1000. unsigned varIdx = i - fixArgCnt;
  1001. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  1002. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  1003. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  1004. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  1005. }
  1006. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1007. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1008. p->fn = cmMemAllocStr(fn);
  1009. p->modLabel = cmMemAllocStr(modLabel);
  1010. p->mp = mp;
  1011. p->onSymId = cmSymTblId(ctx->stH,"on");
  1012. p->offSymId = cmSymTblId(ctx->stH,"off");
  1013. mp->cbArg = p; // set the modulator callback arg
  1014. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1015. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1016. return &p->inst;
  1017. }
  1018. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1019. {
  1020. cmDspRC_t rc = kOkDspRC;
  1021. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1022. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1023. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1024. cmMemFree(p->fn);
  1025. cmMemFree(p->modLabel);
  1026. return rc;
  1027. }
  1028. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1029. {
  1030. cmDspRC_t rc = kOkDspRC;
  1031. cmDspApplyAllDefaults(ctx,inst);
  1032. return rc;
  1033. }
  1034. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1035. {
  1036. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1037. cmDspSetEvent(ctx,inst,evt);
  1038. switch( evt->dstVarId )
  1039. {
  1040. case kResetIdxMdId:
  1041. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1042. break;
  1043. case kCmdMdId:
  1044. {
  1045. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1046. if( symId == p->onSymId )
  1047. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1048. }
  1049. break;
  1050. }
  1051. return kOkDspRC;
  1052. }
  1053. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1054. {
  1055. cmDspRC_t rc = kOkDspRC;
  1056. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1057. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1058. {
  1059. p->tmp_ctx = ctx;
  1060. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1061. }
  1062. return rc;
  1063. }
  1064. struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
  1065. {
  1066. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1067. NULL,
  1068. _cmDspScModAlloc,
  1069. _cmDspScModFree,
  1070. _cmDspScModReset,
  1071. _cmDspScModExec,
  1072. _cmDspScModRecv,
  1073. NULL,NULL,
  1074. "Score Driven Variable Modulator.");
  1075. return &_cmModulatorDC;
  1076. }
  1077. //==========================================================================================================================================
  1078. enum
  1079. {
  1080. kInChCntGsId,
  1081. kOutGroupCntGsId,
  1082. kGroupSelIdxGsId,
  1083. kBaseInFloatGsId
  1084. };
  1085. cmDspClass_t _cmGSwitchDC;
  1086. typedef struct
  1087. {
  1088. cmDspInst_t inst;
  1089. unsigned iChCnt;
  1090. unsigned oGroupCnt;
  1091. unsigned baseInFloatGsId;
  1092. unsigned baseInSymGsId;
  1093. unsigned baseInBoolGsId;
  1094. unsigned baseOutFloatGsId;
  1095. unsigned baseOutSymGsId;
  1096. unsigned baseOutBoolGsId;
  1097. } cmDspGSwitch_t;
  1098. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1099. {
  1100. va_list vl1;
  1101. va_copy(vl1,vl);
  1102. cmDspVarArg_t args[] =
  1103. {
  1104. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1105. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1106. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1107. { NULL, 0, 0, 0, 0 }
  1108. };
  1109. // validate the argument count
  1110. if( va_cnt != 2 )
  1111. {
  1112. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1113. return NULL;
  1114. }
  1115. // read the input ch and output group count
  1116. unsigned iChCnt = va_arg(vl1,unsigned);
  1117. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1118. va_end(vl1);
  1119. // validate the channel counts
  1120. if( iChCnt == 0 || oGroupCnt==0 )
  1121. {
  1122. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1123. return NULL;
  1124. }
  1125. unsigned typeCnt = 3; // i.e. float,sym,bool
  1126. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1127. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1128. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1129. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1130. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1131. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1132. unsigned fixArgCnt = 3;
  1133. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1134. unsigned argCnt = fixArgCnt + varArgCnt;
  1135. cmDspVarArg_t a[ argCnt+1 ];
  1136. unsigned i;
  1137. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1138. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1139. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1140. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1141. unsigned labelCharCnt = 63;
  1142. cmChar_t label[labelCharCnt+1];
  1143. label[labelCharCnt] = 0;
  1144. unsigned gsid = baseOutFloatGsId;
  1145. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1146. {
  1147. snprintf(label,labelCharCnt,"f-out-%i",i);
  1148. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1149. }
  1150. gsid = baseOutSymGsId;
  1151. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1152. {
  1153. snprintf(label,labelCharCnt,"s-out-%i",i);
  1154. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1155. }
  1156. gsid = baseOutBoolGsId;
  1157. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1158. {
  1159. snprintf(label,labelCharCnt,"b-out-%i",i);
  1160. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1161. }
  1162. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1163. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1164. p->iChCnt = iChCnt;
  1165. p->oGroupCnt = oGroupCnt;
  1166. p->baseInFloatGsId = baseInFloatGsId;
  1167. p->baseInSymGsId = baseInSymGsId;
  1168. p->baseInBoolGsId = baseInBoolGsId;
  1169. p->baseOutFloatGsId = baseOutFloatGsId;
  1170. p->baseOutSymGsId = baseOutSymGsId;
  1171. p->baseOutBoolGsId = baseOutBoolGsId;
  1172. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1173. return &p->inst;
  1174. }
  1175. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1176. {
  1177. cmDspRC_t rc = kOkDspRC;
  1178. cmDspApplyAllDefaults(ctx,inst);
  1179. return rc;
  1180. }
  1181. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1182. {
  1183. cmDspRC_t rc = kOkDspRC;
  1184. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1185. // if this is the group selector
  1186. if( evt->dstVarId == kGroupSelIdxGsId )
  1187. {
  1188. unsigned idx;
  1189. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1190. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1191. else
  1192. cmDspSetEvent(ctx,inst,evt);
  1193. return rc;
  1194. }
  1195. // get the group selector
  1196. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1197. assert( groupIdx < p->oGroupCnt);
  1198. // if this is a float input
  1199. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1200. {
  1201. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1202. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1203. return rc;
  1204. }
  1205. // if this is a symbol input
  1206. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1207. {
  1208. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1209. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1210. return rc;
  1211. }
  1212. // if this is a bool input
  1213. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1214. {
  1215. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1216. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1217. return rc;
  1218. }
  1219. return rc;
  1220. }
  1221. struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1222. {
  1223. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1224. NULL,
  1225. _cmDspGSwitchAlloc,
  1226. NULL,
  1227. _cmDspGSwitchReset,
  1228. NULL,
  1229. _cmDspGSwitchRecv,
  1230. NULL,NULL,
  1231. "Ganged switch.");
  1232. return &_cmGSwitchDC;
  1233. }
  1234. //==========================================================================================================================================
  1235. enum
  1236. {
  1237. kMinInSrId,
  1238. kMaxInSrId,
  1239. kMinOutSrId,
  1240. kMaxOutSrId,
  1241. kValInSrId,
  1242. kValOutSrId,
  1243. };
  1244. cmDspClass_t _cmScaleRangeDC;
  1245. typedef struct
  1246. {
  1247. cmDspInst_t inst;
  1248. } cmDspScaleRange_t;
  1249. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1250. {
  1251. va_list vl1;
  1252. va_copy(vl1,vl);
  1253. cmDspVarArg_t args[] =
  1254. {
  1255. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1256. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1257. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1258. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1259. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1260. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1261. { NULL, 0, 0, 0, 0 }
  1262. };
  1263. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1264. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1265. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1266. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1267. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1268. return &p->inst;
  1269. }
  1270. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1271. {
  1272. cmDspRC_t rc = kOkDspRC;
  1273. cmDspApplyAllDefaults(ctx,inst);
  1274. return rc;
  1275. }
  1276. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1277. {
  1278. cmDspRC_t rc = kOkDspRC;
  1279. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1280. cmDspSetEvent(ctx,inst,evt);
  1281. if( evt->dstVarId == kValInSrId )
  1282. {
  1283. double val = cmDspDouble(inst,kValInSrId);
  1284. double min_in = cmDspDouble(inst,kMinInSrId);
  1285. double max_in = cmDspDouble(inst,kMaxInSrId);
  1286. double min_out = cmDspDouble(inst,kMinOutSrId);
  1287. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1288. double x = cmMax(min_in,cmMin(max_in,val));
  1289. x = (x - min_in)/(max_in - min_in);
  1290. x = min_out + x * (max_out - min_out);
  1291. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1292. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1293. }
  1294. return rc;
  1295. }
  1296. struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1297. {
  1298. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1299. NULL,
  1300. _cmDspScaleRangeAlloc,
  1301. NULL,
  1302. _cmDspScaleRangeReset,
  1303. NULL,
  1304. _cmDspScaleRangeRecv,
  1305. NULL,NULL,
  1306. "Scale a value inside an input range to a value in the output range.");
  1307. return &_cmScaleRangeDC;
  1308. }
  1309. //==========================================================================================================================================
  1310. enum
  1311. {
  1312. kCntAmId,
  1313. kSflocAmId,
  1314. kLocAmId,
  1315. kTypeAmId,
  1316. kValueAmId,
  1317. kCstAmId,
  1318. kCmdAmId,
  1319. kScLocAmId,
  1320. kEvenAmId,
  1321. kDynAmId,
  1322. kTempoAmId,
  1323. kCostAmId
  1324. };
  1325. cmDspClass_t _cmActiveMeasDC;
  1326. typedef struct
  1327. {
  1328. unsigned loc;
  1329. unsigned type;
  1330. double value;
  1331. double cost;
  1332. } cmDspActiveMeasRecd_t;
  1333. int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1334. {
  1335. return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1336. }
  1337. typedef struct
  1338. {
  1339. cmDspInst_t inst;
  1340. unsigned addSymId;
  1341. unsigned clearSymId;
  1342. unsigned printSymId;
  1343. unsigned rewindSymId;
  1344. cmDspActiveMeasRecd_t* array; // array[cnt]
  1345. unsigned cnt;
  1346. unsigned nextEmptyIdx;
  1347. unsigned nextFullIdx;
  1348. } cmDspActiveMeas_t;
  1349. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1350. {
  1351. cmDspVarArg_t args[] =
  1352. {
  1353. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1354. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1355. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1356. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type." },
  1357. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1358. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1359. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1360. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1361. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1362. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1363. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1364. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1365. { NULL, 0, 0, 0, 0 }
  1366. };
  1367. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1368. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1369. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1370. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1371. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1372. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,100);
  1373. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1374. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1375. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1376. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1377. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1378. return &p->inst;
  1379. }
  1380. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1381. {
  1382. unsigned i;
  1383. for(i=0; i<p->nextEmptyIdx; ++i)
  1384. {
  1385. const cmChar_t* label = "<null>";
  1386. switch( p->array[i].type )
  1387. {
  1388. case kEvenVarScId: label="even "; break;
  1389. case kDynVarScId: label="dyn "; break;
  1390. case kTempoVarScId: label="tempo"; break;
  1391. default:
  1392. { assert(0); }
  1393. }
  1394. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",p->array[i].loc,label,p->array[i].value,p->array[i].cost);
  1395. }
  1396. return kOkDspRC;
  1397. }
  1398. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1399. {
  1400. p->nextEmptyIdx = 0;
  1401. p->nextFullIdx = cmInvalidIdx;
  1402. return kOkDspRC;
  1403. }
  1404. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1405. {
  1406. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1407. _cmDspActiveMeasClear(ctx,p);
  1408. cmMemPtrFree(&p->array);
  1409. return kOkDspRC;
  1410. }
  1411. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1412. {
  1413. cmDspRC_t rc = kOkDspRC;
  1414. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1415. cmDspApplyAllDefaults(ctx,inst);
  1416. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1417. _cmDspActiveMeasFree(ctx,inst,evt);
  1418. p->array = cmMemAllocZ(cmDspActiveMeasRecd_t,cnt);
  1419. p->cnt = cnt;
  1420. return rc;
  1421. }
  1422. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1423. {
  1424. cmDspRC_t rc = kOkDspRC;
  1425. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1426. cmDspSetEvent(ctx,inst,evt);
  1427. switch( evt->dstVarId )
  1428. {
  1429. case kSflocAmId:
  1430. if( p->nextFullIdx != cmInvalidIdx )
  1431. {
  1432. // get the recv'd score location
  1433. unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1434. unsigned prvLoc = cmInvalidIdx;
  1435. // for each remaining avail record
  1436. for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1437. {
  1438. cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1439. // if this records score location is after the recv'd score loc then we're done
  1440. if( r->loc > sflocIdx )
  1441. break;
  1442. // deterimine the records type
  1443. unsigned varId = cmInvalidId;
  1444. switch( r->type )
  1445. {
  1446. case kEvenVarScId: varId = kEvenAmId; break;
  1447. case kDynVarScId: varId = kDynAmId; break;
  1448. case kTempoVarScId: varId = kTempoAmId; break;
  1449. default:
  1450. { assert(0); }
  1451. }
  1452. // if this score location has not yet been sent then send it now
  1453. if( prvLoc != r->loc )
  1454. cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1455. // transmit the records value and cost
  1456. cmDspSetDouble(ctx,inst,varId,r->value);
  1457. cmDspSetDouble(ctx,inst,kCostAmId,r->value);
  1458. prvLoc = r->loc;
  1459. }
  1460. }
  1461. break;
  1462. case kCmdAmId:
  1463. {
  1464. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1465. if( cmdSymId == p->addSymId )
  1466. {
  1467. if( p->nextEmptyIdx >= p->cnt )
  1468. cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1469. else
  1470. {
  1471. cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1472. r->loc = cmDspUInt( inst,kLocAmId);
  1473. r->type = cmDspUInt( inst,kTypeAmId);
  1474. r->value = cmDspDouble(inst,kValueAmId);
  1475. r->cost = cmDspDouble(inst,kCstAmId);
  1476. p->nextEmptyIdx += 1;
  1477. qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1478. if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1479. p->nextFullIdx = 0;
  1480. }
  1481. }
  1482. if( cmdSymId == p->clearSymId )
  1483. rc = _cmDspActiveMeasClear(ctx,p);
  1484. else
  1485. if( cmdSymId == p->printSymId )
  1486. rc = _cmDspActiveMeasPrint(ctx,p);
  1487. else
  1488. if(cmdSymId == p->rewindSymId )
  1489. p->nextFullIdx = 0;
  1490. }
  1491. break;
  1492. }
  1493. return rc;
  1494. }
  1495. struct cmDspClass_str* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1496. {
  1497. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1498. NULL,
  1499. _cmDspActiveMeasAlloc,
  1500. _cmDspActiveMeasFree,
  1501. _cmDspActiveMeasReset,
  1502. NULL,
  1503. _cmDspActiveMeasRecv,
  1504. NULL,NULL,
  1505. "Scale a value inside an input range to a value in the output range.");
  1506. return &_cmActiveMeasDC;
  1507. }
  1508. //==========================================================================================================================================
  1509. // Audio MIDI Sync
  1510. enum
  1511. {
  1512. kSelAmId,
  1513. kAFnAmId,
  1514. kASmpAmId,
  1515. kMFnAmId,
  1516. kMIdAmId,
  1517. };
  1518. enum
  1519. {
  1520. kAfnAmFl = 0x01,
  1521. kMfnAmFl = 0x02,
  1522. kAsmpAmFl = 0x04,
  1523. kMidAmFl = 0x08,
  1524. };
  1525. cmDspClass_t _cmAmSyncDC;
  1526. typedef struct cmDspAmSyncEntry_str
  1527. {
  1528. const cmChar_t* afn;
  1529. const cmChar_t* mfn;
  1530. unsigned asmp; // Audio sample index to sync to MIDI event
  1531. unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
  1532. int afi;
  1533. int mfi;
  1534. unsigned state;
  1535. } cmDspAmSyncEntry_t;
  1536. typedef struct
  1537. {
  1538. cmDspInst_t inst;
  1539. cmDspAmSyncEntry_t* array;
  1540. unsigned arrayCnt;
  1541. cmDspAmSyncEntry_t* acur;
  1542. cmDspAmSyncEntry_t* mcur;
  1543. } cmDspAmSync_t;
  1544. cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1545. {
  1546. cmDspVarArg_t args[] =
  1547. {
  1548. { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
  1549. { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
  1550. { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
  1551. { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
  1552. { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
  1553. { NULL, 0, 0, 0, 0 }
  1554. };
  1555. cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1556. return &p->inst;
  1557. }
  1558. cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1559. {
  1560. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1561. cmMemFree(p->array);
  1562. return kOkDspRC;
  1563. }
  1564. cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1565. {
  1566. cmDspRC_t rc = kOkDspRC;
  1567. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1568. cmDspApplyAllDefaults(ctx,inst);
  1569. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  1570. cmJsonNode_t* np;
  1571. const cmChar_t* errLabelPtr;
  1572. unsigned i;
  1573. cmJsRC_t jsRC;
  1574. if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
  1575. {
  1576. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
  1577. goto errLabel;
  1578. }
  1579. p->arrayCnt = cmJsonChildCount(np);
  1580. p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
  1581. for(i=0; i<p->arrayCnt; ++i)
  1582. {
  1583. cmJsonNode_t* anp = cmJsonArrayElement(np,i);
  1584. cmDspAmSyncEntry_t* r = p->array + i;
  1585. if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
  1586. "af", kStringTId,&r->afn,
  1587. "asmp",kIntTId, &r->asmp,
  1588. "mf", kStringTId,&r->mfn,
  1589. "mid", kIntTId, &r->mid,
  1590. NULL)) != kOkJsRC )
  1591. {
  1592. if( jsRC == kNodeNotFoundJsRC )
  1593. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
  1594. else
  1595. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
  1596. break;
  1597. }
  1598. r->afi = cmInvalidIdx;
  1599. r->mfi = cmInvalidIdx;
  1600. r->state = 0;
  1601. }
  1602. errLabel:
  1603. return rc;
  1604. }
  1605. cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1606. {
  1607. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1608. unsigned i;
  1609. if( evt->dstVarId != kSelAmId )
  1610. cmDspSetEvent(ctx,inst,evt);
  1611. switch(evt->dstVarId)
  1612. {
  1613. case kSelAmId:
  1614. {
  1615. double srate = cmDspSysSampleRate(ctx->dspH);
  1616. int fpc = cmDspSamplesPerCycle(ctx);
  1617. for(i=0; i<p->arrayCnt; ++i)
  1618. {
  1619. cmDspAmSyncEntry_t* r = p->array + i;
  1620. int dframes = r->mfi-r->afi;
  1621. cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
  1622. r->state,r->afn,r->asmp,r->afi,r->mfn,r->mid,r->mfi,dframes,dframes*fpc,dframes*fpc/srate);
  1623. r->afi = cmInvalidIdx;
  1624. r->mfi = cmInvalidIdx;
  1625. r->state = 0;
  1626. }
  1627. p->acur = NULL;
  1628. p->mcur = NULL;
  1629. }
  1630. break;
  1631. case kAFnAmId:
  1632. {
  1633. const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
  1634. for(i=0; i<p->arrayCnt; ++i)
  1635. if( strcmp(fn,p->array[i].afn) == 0 )
  1636. {
  1637. p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
  1638. p->acur = p->array + i;
  1639. }
  1640. }
  1641. break;
  1642. case kMFnAmId:
  1643. {
  1644. const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
  1645. for(i=0; i<p->arrayCnt; ++i)
  1646. if( strcmp(fn,p->array[i].mfn) == 0 )
  1647. {
  1648. p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
  1649. p->mcur = p->array + i;
  1650. }
  1651. }
  1652. break;
  1653. case kASmpAmId:
  1654. {
  1655. int v = cmDspInt(inst,kASmpAmId);
  1656. if( p->acur != NULL )
  1657. for(i=0; i<p->arrayCnt; ++i)
  1658. {
  1659. cmDspAmSyncEntry_t* r = p->array + i;
  1660. if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
  1661. {
  1662. r->afi = ctx->cycleCnt;
  1663. r->state = cmSetFlag(r->state,kAsmpAmFl);
  1664. break;
  1665. }
  1666. }
  1667. }
  1668. break;
  1669. case kMIdAmId:
  1670. {
  1671. int v = cmDspInt(inst,kMIdAmId);
  1672. if( p->mcur != NULL )
  1673. for(i=0; i<p->arrayCnt; ++i)
  1674. {
  1675. cmDspAmSyncEntry_t* r = p->array + i;
  1676. if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
  1677. {
  1678. r->mfi = ctx->cycleCnt;
  1679. r->state = cmSetFlag(r->state,kMidAmFl);
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. break;
  1685. }
  1686. return kOkDspRC;
  1687. }
  1688. struct cmDspClass_str* cmAmSyncClassCons( cmDspCtx_t* ctx )
  1689. {
  1690. cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
  1691. NULL,
  1692. _cmDspAmSyncAlloc,
  1693. _cmDspAmSyncFree,
  1694. _cmDspAmSyncReset,
  1695. NULL,
  1696. _cmDspAmSyncRecv,
  1697. NULL,NULL,
  1698. "Audio - MIDI Sync Object.");
  1699. return &_cmAmSyncDC;
  1700. }