libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmDspKr.c 93KB

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