libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmDspKr.c 47KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. kAudPathTlId,
  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", kAudPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio 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,kAudPathTlId,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. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  289. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn ) != kOkTlRC )
  290. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  291. return rc;
  292. }
  293. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  294. {
  295. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  296. switch( evt->dstVarId )
  297. {
  298. case kCursTlId:
  299. cmDspSetEvent(ctx,inst,evt);
  300. break;
  301. case kResetTlId:
  302. case kSelTlId:
  303. {
  304. unsigned markerId;
  305. cmDspSetEvent(ctx,inst,evt);
  306. // get the id of the selected marker
  307. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  308. {
  309. // get the marker object
  310. cmTlObj_t* op;
  311. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  312. {
  313. assert(op->typeId == kMarkerTlId);
  314. p->afIdx = op->begSmpIdx;
  315. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  316. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  317. // locate the audio file assoc'd with the marker
  318. cmTlAudioFile_t* afp;
  319. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  320. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  321. // locate the midi file assoc'd with the marker
  322. cmTlMidiFile_t* mfp;
  323. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  324. {
  325. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  326. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  327. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  328. }
  329. }
  330. }
  331. }
  332. break;
  333. default:
  334. {assert(0);}
  335. }
  336. return kOkDspRC;
  337. }
  338. struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx )
  339. {
  340. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  341. NULL,
  342. _cmDspTimeLineAlloc,
  343. _cmDspTimeLineFree,
  344. _cmDspTimeLineReset,
  345. NULL,
  346. _cmDspTimeLineRecv,
  347. NULL,NULL,
  348. "Time Line control.");
  349. return &_cmTimeLineDC;
  350. }
  351. //==========================================================================================================================================
  352. // Score UI Object
  353. enum
  354. {
  355. kFnScId,
  356. kSelScId,
  357. kSendScId,
  358. kStatusScId,
  359. kD0ScId,
  360. kD1ScId,
  361. kSmpIdxScId,
  362. kLocIdxScId,
  363. kEvtIdxScId,
  364. kDynScId,
  365. kValTypeScId,
  366. kValueScId
  367. };
  368. cmDspClass_t _cmScoreDC;
  369. typedef struct
  370. {
  371. cmDspInst_t inst;
  372. cmScH_t scH;
  373. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  374. } cmDspScore_t;
  375. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  376. {
  377. cmDspVarArg_t args[] =
  378. {
  379. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  380. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  381. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  382. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  383. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  384. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  385. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  386. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  387. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  388. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  389. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  390. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  391. { NULL, 0, 0, 0, 0 }
  392. };
  393. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  394. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  395. // create the UI control
  396. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId);
  397. p->scH = cmScNullHandle;
  398. return &p->inst;
  399. }
  400. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  401. {
  402. cmDspRC_t rc = kOkDspRC;
  403. cmDspScore_t* p = (cmDspScore_t*)inst;
  404. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  405. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  406. return rc;
  407. }
  408. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  409. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  410. {
  411. cmDspInst_t* inst = (cmDspInst_t*)arg;
  412. cmDspScore_t* p = (cmDspScore_t*)inst;
  413. cmScMsg_t m;
  414. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  415. {
  416. switch( m.typeId )
  417. {
  418. case kDynMsgScId:
  419. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  420. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  421. break;
  422. case kVarMsgScId:
  423. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  424. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  425. break;
  426. default:
  427. { assert(0); }
  428. }
  429. }
  430. }
  431. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  432. {
  433. cmDspRC_t rc = kOkDspRC;
  434. cmDspScore_t* p = (cmDspScore_t*)inst;
  435. const cmChar_t* tlFn = NULL;
  436. unsigned* dynRefArray = NULL;
  437. unsigned dynRefCnt = 0;
  438. cmDspApplyAllDefaults(ctx,inst);
  439. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  440. {
  441. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  442. goto errLabel;
  443. }
  444. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  445. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, cmSymTblNullHandle ) != kOkTlRC )
  446. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  447. errLabel:
  448. return rc;
  449. }
  450. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  451. {
  452. cmDspScore_t* p = (cmDspScore_t*)inst;
  453. if( evt->dstVarId == kSendScId )
  454. {
  455. unsigned selIdx;
  456. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  457. {
  458. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  459. cmScoreClearPerfInfo(p->scH);
  460. }
  461. return kOkDspRC;
  462. }
  463. cmDspSetEvent(ctx,inst,evt);
  464. switch( evt->dstVarId )
  465. {
  466. case kSelScId:
  467. cmScoreClearPerfInfo(p->scH);
  468. break;
  469. case kStatusScId:
  470. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  471. break;
  472. case kLocIdxScId:
  473. {
  474. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  475. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  476. // this call may result in callbacks to _cmDspScoreCb()
  477. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  478. }
  479. break;
  480. }
  481. return kOkDspRC;
  482. }
  483. struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
  484. {
  485. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  486. NULL,
  487. _cmDspScoreAlloc,
  488. _cmDspScoreFree,
  489. _cmDspScoreReset,
  490. NULL,
  491. _cmDspScoreRecv,
  492. NULL,NULL,
  493. "Score control.");
  494. return &_cmScoreDC;
  495. }
  496. //==========================================================================================================================================
  497. // MIDI File Player
  498. enum
  499. {
  500. kFnMfId,
  501. kSelMfId,
  502. kBsiMfId,
  503. kEsiMfId,
  504. kStatusMfId,
  505. kD0MfId,
  506. kD1MfId,
  507. kSmpIdxMfId
  508. };
  509. cmDspClass_t _cmMidiFilePlayDC;
  510. typedef struct
  511. {
  512. cmDspInst_t inst;
  513. cmMidiFileH_t mfH;
  514. unsigned curMsgIdx; // current midi file msg index
  515. int csi; // current sample index
  516. int bsi; // starting sample index
  517. int esi; // ending sample index
  518. unsigned startSymId;
  519. unsigned stopSymId;
  520. unsigned contSymId;
  521. bool errFl;
  522. } cmDspMidiFilePlay_t;
  523. /*
  524. 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  525. These indexes are relative to the start of the file.
  526. When the player recieves a 'start' msg it sets the current sample index
  527. 'si' to 'bsi' and begins scanning for the next note to play.
  528. On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  529. si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  530. */
  531. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  532. {
  533. cmDspVarArg_t args[] =
  534. {
  535. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  536. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  537. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  538. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  539. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  540. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  541. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  542. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  543. { NULL, 0, 0, 0, 0 }
  544. };
  545. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  546. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  547. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  548. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  549. p->mfH = cmMidiFileNullHandle;
  550. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  551. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  552. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  553. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  554. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  555. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  556. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  557. return &p->inst;
  558. }
  559. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  560. {
  561. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  562. if( cmMidiFileClose(&p->mfH) )
  563. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  564. return kOkDspRC;
  565. }
  566. // return the index of the msg following smpIdx
  567. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  568. {
  569. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  570. if( cmMidiFileIsValid(p->mfH) == false )
  571. {
  572. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  573. return cmInvalidIdx;
  574. }
  575. unsigned i;
  576. unsigned n = cmMidiFileMsgCount(p->mfH);
  577. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  578. for(i=0; i<n; ++i)
  579. if( a[i]->dtick > smpIdx )
  580. break;
  581. return i==n ? cmInvalidIdx : i;
  582. }
  583. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  584. {
  585. cmDspRC_t rc = kOkDspRC;
  586. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  587. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  588. p->errFl = false;
  589. if( fn==NULL || strlen(fn)==0 )
  590. return rc;
  591. if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
  592. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  593. else
  594. {
  595. p->curMsgIdx = 0;
  596. p->bsi = cmDspInt(inst,kBsiMfId);
  597. p->esi = cmDspInt(inst,kEsiMfId);
  598. p->csi = 0;
  599. // force the first msg to occurr one quarter note into the file
  600. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  601. // convert midi msg times to absolute time in samples
  602. cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  603. }
  604. return rc;
  605. }
  606. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  607. {
  608. cmDspApplyAllDefaults(ctx,inst);
  609. return _cmDspMidiFilePlayOpen(ctx,inst);
  610. }
  611. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  612. {
  613. cmDspRC_t rc = kOkDspRC;
  614. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  615. unsigned sPc = cmDspSamplesPerCycle(ctx);
  616. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  617. {
  618. if( cmMidiFileIsValid(p->mfH) == false )
  619. {
  620. if( p->errFl==false )
  621. {
  622. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  623. p->errFl = true;
  624. }
  625. return rc;
  626. }
  627. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  628. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  629. for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
  630. {
  631. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  632. switch( mp->status )
  633. {
  634. case kNoteOnMdId:
  635. case kCtlMdId:
  636. cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
  637. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  638. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  639. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  640. break;
  641. }
  642. }
  643. }
  644. p->csi += sPc;
  645. return rc;
  646. }
  647. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  648. {
  649. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  650. cmDspSetEvent(ctx,inst,evt);
  651. switch(evt->dstVarId)
  652. {
  653. case kFnMfId:
  654. _cmDspMidiFilePlayOpen(ctx, inst );
  655. break;
  656. case kSelMfId:
  657. {
  658. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  659. {
  660. p->csi = cmDspInt(inst,kBsiMfId);
  661. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  662. }
  663. break;
  664. }
  665. }
  666. return kOkDspRC;
  667. }
  668. struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  669. {
  670. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  671. NULL,
  672. _cmDspMidiFilePlayAlloc,
  673. _cmDspMidiFilePlayFree,
  674. _cmDspMidiFilePlayReset,
  675. _cmDspMidiFilePlayExec,
  676. _cmDspMidiFilePlayRecv,
  677. NULL,NULL,
  678. "Time tagged text file.");
  679. return &_cmMidiFilePlayDC;
  680. }
  681. //==========================================================================================================================================
  682. enum
  683. {
  684. kFnSfId,
  685. kBufCntSfId,
  686. kMinLkAhdSfId,
  687. kMaxWndCntSfId,
  688. kMinVelSfId,
  689. kIndexSfId,
  690. kStatusSfId,
  691. kD0SfId,
  692. kD1SfId,
  693. kSmpIdxSfId,
  694. kCmdSfId,
  695. kOutSfId,
  696. kDynSfId,
  697. kEvenSfId,
  698. kTempoSfId,
  699. kCostSfId,
  700. kSymSfId
  701. };
  702. cmDspClass_t _cmScFolDC;
  703. struct cmDspScFol_str;
  704. typedef struct
  705. {
  706. cmDspCtx_t* ctx;
  707. struct cmDspScFol_str* sfp;
  708. } cmDspScFolCbArg_t;
  709. typedef struct cmDspScFol_str
  710. {
  711. cmDspInst_t inst;
  712. cmScMatcher* sfp;
  713. cmScMeas* smp;
  714. cmScH_t scH;
  715. cmDspScFolCbArg_t arg;
  716. unsigned printSymId;
  717. unsigned quietSymId;
  718. } cmDspScFol_t;
  719. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  720. {
  721. cmDspVarArg_t args[] =
  722. {
  723. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  724. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Event buffer element count." },
  725. { "lkahd", kMinLkAhdSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum window look-ahead."},
  726. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl, "Maximum window length."},
  727. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum velocity."},
  728. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  729. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  730. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  731. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  732. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  733. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  734. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Current score index."},
  735. { "dyn", kDynSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Dynamic value."},
  736. { "even", kEvenSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Evenness value."},
  737. { "tempo", kTempoSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Tempo value."},
  738. { "cost", kCostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Match cost value."},
  739. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  740. { NULL, 0, 0, 0, 0, NULL }
  741. };
  742. cmDspScFol_t* p;
  743. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  744. return NULL;
  745. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  746. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  747. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  748. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  749. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  750. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  751. cmDspSetDefaultUInt( ctx, &p->inst, kMinLkAhdSfId, 0, 3);
  752. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  753. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  754. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  755. cmDspSetDefaultDouble( ctx, &p->inst, kDynSfId, 0, 0);
  756. cmDspSetDefaultDouble( ctx, &p->inst, kEvenSfId, 0, 0);
  757. cmDspSetDefaultDouble( ctx, &p->inst, kTempoSfId, 0, 0);
  758. cmDspSetDefaultDouble( ctx, &p->inst, kCostSfId, 0, 0);
  759. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  760. return &p->inst;
  761. }
  762. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  763. {
  764. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  765. cmScMatcherFree(&p->sfp);
  766. cmScMeasFree(&p->smp);
  767. cmScoreFinalize(&p->scH);
  768. return kOkDspRC;
  769. }
  770. // This is a callback function from cmScMatcherExec() which is called when
  771. // this cmDspFol object receives a new score location index.
  772. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  773. {
  774. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  775. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  776. {
  777. cmDspInst_t* inst = &(ap->sfp->inst);
  778. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  779. unsigned i;
  780. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  781. {
  782. switch( ap->sfp->smp->set[i].sp->varId )
  783. {
  784. case kEvenVarScId:
  785. cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  786. break;
  787. case kDynVarScId:
  788. cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  789. break;
  790. case kTempoVarScId:
  791. cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  792. break;
  793. default:
  794. { assert(0); }
  795. }
  796. cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  797. // Set the values in the global variable storage
  798. cmDspValue_t vv,cv;
  799. unsigned j;
  800. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  801. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  802. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  803. {
  804. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  805. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  806. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  807. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  808. }
  809. }
  810. /*
  811. // trigger 'section' starts
  812. for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  813. {
  814. const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  815. if( locPtr->begSectPtr != NULL )
  816. cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  817. }
  818. */
  819. }
  820. }
  821. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  822. {
  823. cmDspRC_t rc = kOkDspRC;
  824. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  825. const cmChar_t* fn;
  826. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  827. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  828. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  829. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  830. if( cmScoreIsValid(p->scH) )
  831. {
  832. unsigned* dynRefArray = NULL;
  833. unsigned dynRefCnt = 0;
  834. // initialize the cmScMatcher
  835. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  836. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  837. // read the dynamics reference array
  838. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  839. {
  840. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  841. goto errLabel;
  842. }
  843. // initialize the cmScMeas object.
  844. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  845. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  846. }
  847. errLabel:
  848. return rc;
  849. }
  850. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  851. {
  852. cmDspRC_t rc;
  853. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  854. return rc;
  855. return _cmDspScFolOpenScore(ctx,inst);
  856. }
  857. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  858. {
  859. cmDspRC_t rc = kOkDspRC;
  860. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  861. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  862. {
  863. switch( evt->dstVarId )
  864. {
  865. case kIndexSfId:
  866. if( cmScoreIsValid(p->scH) )
  867. {
  868. if( cmScMeasReset( p->smp ) != cmOkRC )
  869. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  870. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  871. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  872. }
  873. break;
  874. case kStatusSfId:
  875. if( cmScoreIsValid(p->scH))
  876. {
  877. unsigned scLocIdx = cmInvalidIdx;
  878. // setup the cmScMeas() callback arg.
  879. p->arg.ctx = ctx;
  880. p->arg.sfp = p;
  881. p->sfp->cbArg = &p->arg;
  882. // this call may result in a callback to _cmScFolMatcherCb()
  883. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  884. if( scLocIdx != cmInvalidIdx )
  885. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  886. }
  887. break;
  888. case kFnSfId:
  889. _cmDspScFolOpenScore(ctx,inst);
  890. break;
  891. case kCmdSfId:
  892. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  893. p->sfp->printFl = true;
  894. else
  895. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  896. p->sfp->printFl = false;
  897. break;
  898. }
  899. }
  900. return rc;
  901. }
  902. struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
  903. {
  904. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  905. NULL,
  906. _cmDspScFolAlloc,
  907. _cmDspScFolFree,
  908. _cmDspScFolReset,
  909. NULL,
  910. _cmDspScFolRecv,
  911. NULL,NULL,
  912. "Score Follower");
  913. return &_cmScFolDC;
  914. }
  915. //==========================================================================================================================================
  916. enum
  917. {
  918. kScLocIdxMdId,
  919. kResetIdxMdId,
  920. kCmdMdId
  921. };
  922. cmDspClass_t _cmModulatorDC;
  923. typedef struct
  924. {
  925. cmDspInst_t inst;
  926. cmScModulator* mp;
  927. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  928. cmChar_t* fn;
  929. cmChar_t* modLabel;
  930. unsigned onSymId;
  931. unsigned offSymId;
  932. } cmDspScMod_t;
  933. void _cmDspScModCb( void* arg, unsigned varSymId, double value )
  934. {
  935. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  936. cmDspVar_t* varPtr;
  937. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  938. return;
  939. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  940. }
  941. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  942. {
  943. va_list vl1;
  944. va_copy(vl1,vl);
  945. cmDspVarArg_t args[] =
  946. {
  947. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  948. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  949. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  950. { NULL, 0, 0, 0, 0 }
  951. };
  952. // validate the argument count
  953. if( va_cnt != 2 )
  954. {
  955. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  956. return NULL;
  957. }
  958. // read the modulator file and label strings
  959. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  960. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  961. va_end(vl1);
  962. // validate the file
  963. if( fn==NULL || cmFsIsFile(fn)==false )
  964. {
  965. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  966. return NULL;
  967. }
  968. // allocate the internal modulator object
  969. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  970. if(mp == NULL )
  971. {
  972. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  973. return NULL;
  974. }
  975. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  976. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  977. cmDspVarArg_t a[ argCnt+1 ];
  978. unsigned i;
  979. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  980. for(i=fixArgCnt; i<argCnt; ++i)
  981. {
  982. unsigned varIdx = i - fixArgCnt;
  983. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  984. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  985. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  986. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  987. }
  988. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  989. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  990. p->fn = cmMemAllocStr(fn);
  991. p->modLabel = cmMemAllocStr(modLabel);
  992. p->mp = mp;
  993. p->onSymId = cmSymTblId(ctx->stH,"on");
  994. p->offSymId = cmSymTblId(ctx->stH,"off");
  995. mp->cbArg = p; // set the modulator callback arg
  996. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  997. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  998. return &p->inst;
  999. }
  1000. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1001. {
  1002. cmDspRC_t rc = kOkDspRC;
  1003. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1004. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1005. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1006. cmMemFree(p->fn);
  1007. cmMemFree(p->modLabel);
  1008. return rc;
  1009. }
  1010. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1011. {
  1012. cmDspRC_t rc = kOkDspRC;
  1013. cmDspApplyAllDefaults(ctx,inst);
  1014. return rc;
  1015. }
  1016. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1017. {
  1018. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1019. cmDspSetEvent(ctx,inst,evt);
  1020. switch( evt->dstVarId )
  1021. {
  1022. case kResetIdxMdId:
  1023. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1024. break;
  1025. case kCmdMdId:
  1026. {
  1027. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1028. if( symId == p->onSymId )
  1029. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1030. }
  1031. break;
  1032. }
  1033. return kOkDspRC;
  1034. }
  1035. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1036. {
  1037. cmDspRC_t rc = kOkDspRC;
  1038. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1039. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1040. {
  1041. p->tmp_ctx = ctx;
  1042. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1043. }
  1044. return rc;
  1045. }
  1046. struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
  1047. {
  1048. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1049. NULL,
  1050. _cmDspScModAlloc,
  1051. _cmDspScModFree,
  1052. _cmDspScModReset,
  1053. _cmDspScModExec,
  1054. _cmDspScModRecv,
  1055. NULL,NULL,
  1056. "Score Driven Variable Modulator.");
  1057. return &_cmModulatorDC;
  1058. }
  1059. //==========================================================================================================================================
  1060. enum
  1061. {
  1062. kInChCntGsId,
  1063. kOutGroupCntGsId,
  1064. kGroupSelIdxGsId,
  1065. kBaseInFloatGsId
  1066. };
  1067. cmDspClass_t _cmGSwitchDC;
  1068. typedef struct
  1069. {
  1070. cmDspInst_t inst;
  1071. unsigned iChCnt;
  1072. unsigned oGroupCnt;
  1073. unsigned baseInFloatGsId;
  1074. unsigned baseInSymGsId;
  1075. unsigned baseInBoolGsId;
  1076. unsigned baseOutFloatGsId;
  1077. unsigned baseOutSymGsId;
  1078. unsigned baseOutBoolGsId;
  1079. } cmDspGSwitch_t;
  1080. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1081. {
  1082. va_list vl1;
  1083. va_copy(vl1,vl);
  1084. cmDspVarArg_t args[] =
  1085. {
  1086. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1087. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1088. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1089. { NULL, 0, 0, 0, 0 }
  1090. };
  1091. // validate the argument count
  1092. if( va_cnt != 2 )
  1093. {
  1094. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1095. return NULL;
  1096. }
  1097. // read the input ch and output group count
  1098. unsigned iChCnt = va_arg(vl1,unsigned);
  1099. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1100. va_end(vl1);
  1101. // validate the channel counts
  1102. if( iChCnt == 0 || oGroupCnt==0 )
  1103. {
  1104. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1105. return NULL;
  1106. }
  1107. unsigned typeCnt = 3; // i.e. float,sym,bool
  1108. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1109. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1110. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1111. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1112. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1113. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1114. unsigned fixArgCnt = 3;
  1115. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1116. unsigned argCnt = fixArgCnt + varArgCnt;
  1117. cmDspVarArg_t a[ argCnt+1 ];
  1118. unsigned i;
  1119. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1120. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1121. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1122. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1123. unsigned labelCharCnt = 63;
  1124. cmChar_t label[labelCharCnt+1];
  1125. label[labelCharCnt] = 0;
  1126. unsigned gsid = baseOutFloatGsId;
  1127. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1128. {
  1129. snprintf(label,labelCharCnt,"f-out-%i",i);
  1130. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1131. }
  1132. gsid = baseOutSymGsId;
  1133. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1134. {
  1135. snprintf(label,labelCharCnt,"s-out-%i",i);
  1136. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1137. }
  1138. gsid = baseOutBoolGsId;
  1139. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1140. {
  1141. snprintf(label,labelCharCnt,"b-out-%i",i);
  1142. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1143. }
  1144. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1145. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1146. p->iChCnt = iChCnt;
  1147. p->oGroupCnt = oGroupCnt;
  1148. p->baseInFloatGsId = baseInFloatGsId;
  1149. p->baseInSymGsId = baseInSymGsId;
  1150. p->baseInBoolGsId = baseInBoolGsId;
  1151. p->baseOutFloatGsId = baseOutFloatGsId;
  1152. p->baseOutSymGsId = baseOutSymGsId;
  1153. p->baseOutBoolGsId = baseOutBoolGsId;
  1154. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1155. return &p->inst;
  1156. }
  1157. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1158. {
  1159. cmDspRC_t rc = kOkDspRC;
  1160. cmDspApplyAllDefaults(ctx,inst);
  1161. return rc;
  1162. }
  1163. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1164. {
  1165. cmDspRC_t rc = kOkDspRC;
  1166. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1167. // if this is the group selector
  1168. if( evt->dstVarId == kGroupSelIdxGsId )
  1169. {
  1170. unsigned idx;
  1171. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1172. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1173. else
  1174. cmDspSetEvent(ctx,inst,evt);
  1175. return rc;
  1176. }
  1177. // get the group selector
  1178. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1179. assert( groupIdx < p->oGroupCnt);
  1180. // if this is a float input
  1181. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1182. {
  1183. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1184. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1185. return rc;
  1186. }
  1187. // if this is a symbol input
  1188. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1189. {
  1190. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1191. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1192. return rc;
  1193. }
  1194. // if this is a bool input
  1195. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1196. {
  1197. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1198. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1199. return rc;
  1200. }
  1201. return rc;
  1202. }
  1203. struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1204. {
  1205. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1206. NULL,
  1207. _cmDspGSwitchAlloc,
  1208. NULL,
  1209. _cmDspGSwitchReset,
  1210. NULL,
  1211. _cmDspGSwitchRecv,
  1212. NULL,NULL,
  1213. "Ganged switch.");
  1214. return &_cmGSwitchDC;
  1215. }