libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspKr.c 99KB

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