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

cmDspKr.c 94KB

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