libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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