12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmComplexTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmFile.h"
- #include "cmSymTbl.h"
- #include "cmJson.h"
- #include "cmText.h"
- #include "cmPrefs.h"
- #include "cmDspValue.h"
- #include "cmMsgProtocol.h"
- #include "cmThread.h"
- #include "cmUdpPort.h"
- #include "cmUdpNet.h"
- #include "cmAudioSys.h"
- #include "cmDspCtx.h"
- #include "cmDspClass.h"
- #include "cmDspStore.h"
- #include "cmDspUi.h"
- #include "cmDspSys.h"
- #include "cmMath.h"
-
-
- #include "cmAudioFile.h"
- #include "cmFileSys.h"
- #include "cmProcObj.h"
- #include "cmProcTemplateMain.h"
- #include "cmProc.h"
- #include "cmMidi.h"
- #include "cmProc2.h"
- #include "cmVectOpsTemplateMain.h"
-
- #include "cmAudioFile.h"
- #include "cmMidiFile.h"
- #include "cmTimeLine.h"
- #include "cmScore.h"
- #include "cmProc4.h"
-
- enum
- {
- kWndSmpCntKrId,
- kHopFactKrId,
- kModeKrId,
- kThreshKrId,
- kLwrSlopeKrId,
- kUprSlopeKrId,
- kOffsetKrId,
- kInvertKrId,
- kBypassKrId,
- kWetKrId,
- kAudioInKrId,
- kAudioOutKrId
- };
-
- typedef struct
- {
- cmDspInst_t inst;
- cmCtx* ctx;
- cmSpecDist_t* sdp;
- } cmDspKr_t;
-
- cmDspClass_t _cmKrDC;
-
-
- //==========================================================================================================================================
-
- cmDspInst_t* _cmDspKrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "wndn", kWndSmpCntKrId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
- { "hopf", kHopFactKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
- { "mode", kModeKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Mode 0=bypass 1=basic 2=spec cnt 3=amp env" },
- { "thrh", kThreshKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
- { "lwrs", kLwrSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
- { "uprs", kUprSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
- { "offs", kOffsetKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset"},
- { "invt", kInvertKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Invert"},
- { "bypass", kBypassKrId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
- { "wet", kWetKrId, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
- { "in", kAudioInKrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
- { "out", kAudioOutKrId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspKr_t* p = cmDspInstAlloc(cmDspKr_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
- unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKrId);
- unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
-
- cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKrId, defWndSmpCnt, wndSmpCnt );
- cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKrId, 0, 4 );
- cmDspSetDefaultUInt( ctx,&p->inst, kModeKrId, 0, kBasicModeSdId );
- cmDspSetDefaultDouble( ctx,&p->inst, kThreshKrId, 0, 60.0 );
- cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKrId, 0, 2.0 );
- cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKrId, 0, 0.0 );
- cmDspSetDefaultDouble( ctx,&p->inst, kOffsetKrId, 0, 30.0);
- cmDspSetDefaultUInt( ctx,&p->inst, kInvertKrId, 0, 0 );
- cmDspSetDefaultUInt( ctx,&p->inst, kBypassKrId, 0, 0 );
- cmDspSetDefaultSample( ctx,&p->inst, kWetKrId, 0, 1.0);
-
- //_cmDspKrCmInit(ctx,p); // initialize the cm library
-
- p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspKrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspKr_t* p = (cmDspKr_t*)inst;
-
- cmSpecDistFree(&p->sdp);
-
- cmCtxFree(&p->ctx);
- //_cmDspKrCmFinal(ctx,p); // finalize the cm library
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspKrSetup(cmDspCtx_t* ctx, cmDspKr_t* p )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKrId);
- unsigned hopFact = cmDspUInt(&p->inst,kHopFactKrId);
- unsigned olaWndTypeId =kHannWndId;
-
- cmSpecDistFree(&p->sdp);
-
- p->sdp = cmSpecDistAlloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
-
- assert(p->sdp != NULL );
-
- if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKrId)) != kOkDspRC )
- return rc;
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspKrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspKr_t* p = (cmDspKr_t*)inst;
- cmDspRC_t rc;
-
- if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
- return rc;
-
- return _cmDspKrSetup(ctx,p);
- }
-
- cmDspRC_t _cmDspKrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspKr_t* p = (cmDspKr_t*)inst;
- cmDspRC_t rc = kOkDspRC;
-
- unsigned iChIdx = 0;
- const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKrId,iChIdx);
- unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKrId);
-
- // if no connected
- if( iSmpCnt == 0 )
- return rc;
-
- unsigned oChIdx = 0;
- cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKrId,oChIdx);
- unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKrId);
- const cmSample_t* sp;
-
- cmSample_t wet = cmDspSample(inst,kWetKrId);
-
- cmSpecDistExec(p->sdp,ip,iSmpCnt);
-
- if((sp = cmSpecDistOut(p->sdp)) != NULL )
- {
- cmVOS_MultVVS(op,oSmpCnt,sp,wet);
- }
-
- if( wet<1.0 )
- cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
-
- return rc;
- }
-
- cmDspRC_t _cmDspKrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspKr_t* p = (cmDspKr_t*)inst;
- cmDspRC_t rc = kOkDspRC;
-
-
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kWndSmpCntKrId:
- case kHopFactKrId:
- _cmDspKrSetup(ctx,p);
-
- // THIS IS A HACK
- // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
- // SHOULD BE ISOLATED IN cmSpecDist() AND THE
- // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
- // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
- // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
- // MINIMAL AUDIO INTERUPTION.
-
- p->sdp->mode = cmDspUInt(inst,kModeKrId);
- p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
- p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
- p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
- p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
- p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
-
- printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
- break;
-
- case kModeKrId:
- p->sdp->mode = cmDspUInt(inst,kModeKrId);
- printf("mode:%i\n",p->sdp->mode);
- break;
-
- case kThreshKrId:
- p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
- //printf("thr:p:%p sdp:%p %f\n",p,p->sdp,p->sdp->thresh);
- break;
-
- case kUprSlopeKrId:
- p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
- //printf("upr slope:%f\n",p->sdp->uprSlope);
- break;
-
- case kLwrSlopeKrId:
- p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
- //printf("upr slope:%f\n",p->sdp->lwrSlope);
- break;
-
- case kOffsetKrId:
- p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
- break;
-
- case kInvertKrId:
- p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
- break;
-
- case kWetKrId:
- break;
-
- default:
- { assert(0); }
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmKrClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmKrDC,ctx,"Kr",
- NULL,
- _cmDspKrAlloc,
- _cmDspKrFree,
- _cmDspKrReset,
- _cmDspKrExec,
- _cmDspKrRecv,
- NULL,NULL,
- "Fourier based non-linear transformer.");
-
- return &_cmKrDC;
- }
-
-
- //==========================================================================================================================================
- // Time Line UI Object
-
- enum
- {
- kTlFileTlId,
- kPrefixPathTlId,
- kSelTlId,
- kCursTlId,
- kResetTlId,
- kAudFnTlId,
- kAudLblTlId,
- kMidiFnTlId,
- kMidiLblTlId,
- kBegAudSmpIdxTlId,
- kEndAudSmpIdxTlId,
- kBegMidiSmpIdxTlId,
- kEndMidiSmpIdxTlId
- };
-
- cmDspClass_t _cmTimeLineDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmTlH_t tlH;
- unsigned afIdx;
- } cmDspTimeLine_t;
-
- cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
- { "path", kPrefixPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line data file prefix path" },
- { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
- { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
- { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
- { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
- { "albl", kAudLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select Audio file time line label."},
- { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
- { "mlbl", kMidiLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select MIDI file time line label."},
- { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
- { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
- { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
- { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
- cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
- cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
- cmDspSetDefaultStrcz(ctx, &p->inst, kAudLblTlId, NULL, "");
- cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
- cmDspSetDefaultStrcz(ctx, &p->inst, kMidiLblTlId, NULL, "");
- cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
- cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
- cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
- cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
-
- // create the UI control
- cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kPrefixPathTlId,kSelTlId,kCursTlId);
-
- p->tlH = cmTimeLineNullHandle;
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
-
- if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
- return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- const cmChar_t* tlFn;
-
- const cmChar_t* tlPrePath = cmDspStrcz(inst,kPrefixPathTlId);
-
- if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
- if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn, tlPrePath ) != kOkTlRC )
- rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
-
- return rc;
- }
-
- cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
-
- switch( evt->dstVarId )
- {
- case kPrefixPathTlId:
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- case kCursTlId:
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- case kResetTlId:
- case kSelTlId:
- {
- unsigned markerId;
- cmDspSetEvent(ctx,inst,evt);
-
- // get the id of the selected marker
- if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
- {
- // get the marker object
- cmTlObj_t* op;
- if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
- {
- assert(op->typeId == kMarkerTlId);
-
- p->afIdx = op->begSmpIdx;
-
- cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
- cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
-
- // locate the audio file assoc'd with the marker
- cmTlAudioFile_t* afp;
- if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
- {
- cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
- cmDspSetStrcz(ctx, inst, kAudLblTlId, afp->obj.name );
- }
-
- // locate the midi file assoc'd with the marker
- cmTlMidiFile_t* mfp;
- if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
- {
- cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
- cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
-
- cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
- cmDspSetStrcz(ctx, inst, kMidiLblTlId,mfp->obj.name );
-
- }
- }
-
- }
-
- }
-
- break;
-
- default:
- {assert(0);}
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
- NULL,
- _cmDspTimeLineAlloc,
- _cmDspTimeLineFree,
- _cmDspTimeLineReset,
- NULL,
- _cmDspTimeLineRecv,
- NULL,NULL,
- "Time Line control.");
-
- return &_cmTimeLineDC;
- }
-
- //==========================================================================================================================================
- // Score UI Object
-
- enum
- {
- kFnScId,
- kSelScId,
- kSendScId,
- kStatusScId,
- kD0ScId,
- kD1ScId,
- kSmpIdxScId,
- kLocIdxScId,
- kEvtIdxScId,
- kDynScId,
- kValTypeScId,
- kValueScId
- };
-
- cmDspClass_t _cmScoreDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmScH_t scH;
- cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
- } cmDspScore_t;
-
- cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
- { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
- { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
- { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
- { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
- { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
- { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
- { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
- { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
- { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
- { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
- { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
-
- // create the UI control
- cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId);
-
- p->scH = cmScNullHandle;
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScore_t* p = (cmDspScore_t*)inst;
-
- if( cmScoreFinalize(&p->scH) != kOkTlRC )
- return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
-
- return rc;
- }
-
- // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
- void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
- {
- cmDspInst_t* inst = (cmDspInst_t*)arg;
- cmDspScore_t* p = (cmDspScore_t*)inst;
- cmScMsg_t m;
- if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
- {
- switch( m.typeId )
- {
- case kDynMsgScId:
- cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
- cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
- break;
-
- case kVarMsgScId:
- cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
- cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
- break;
-
- default:
- { assert(0); }
- }
- }
- }
-
- cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScore_t* p = (cmDspScore_t*)inst;
- const cmChar_t* tlFn = NULL;
- unsigned* dynRefArray = NULL;
- unsigned dynRefCnt = 0;
-
- cmDspApplyAllDefaults(ctx,inst);
-
-
- if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
- {
- rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
- goto errLabel;
- }
-
- if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
- {
- if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, ctx->stH ) != kOkTlRC )
- rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
- //else
- // cmScorePrintLoc(p->scH);
- }
- errLabel:
- return rc;
- }
-
- cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspScore_t* p = (cmDspScore_t*)inst;
-
- if( evt->dstVarId == kSendScId )
- {
- unsigned selIdx;
- if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
- {
- cmDspSetUInt(ctx,inst,kSelScId, selIdx );
- cmScoreClearPerfInfo(p->scH);
- }
- return kOkDspRC;
- }
-
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kSelScId:
- cmScoreClearPerfInfo(p->scH);
- break;
-
- case kStatusScId:
- //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
- break;
-
- case kLocIdxScId:
- {
- assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
- p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
-
- // this call may result in callbacks to _cmDspScoreCb()
- cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
- }
- break;
-
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmScoreDC,ctx,"Score",
- NULL,
- _cmDspScoreAlloc,
- _cmDspScoreFree,
- _cmDspScoreReset,
- NULL,
- _cmDspScoreRecv,
- NULL,NULL,
- "Score control.");
-
- return &_cmScoreDC;
- }
-
- //==========================================================================================================================================
- // MIDI File Player
-
- enum
- {
- kFnMfId,
- kSelMfId,
- kBsiMfId,
- kEsiMfId,
- kStatusMfId,
- kD0MfId,
- kD1MfId,
- kSmpIdxMfId,
- kIdMfId
- };
-
- cmDspClass_t _cmMidiFilePlayDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmMidiFileH_t mfH;
- unsigned curMsgIdx; // current midi file msg index
- int csi; // current sample index
- int bsi; // starting sample index
- int esi; // ending sample index
- unsigned startSymId;
- unsigned stopSymId;
- unsigned contSymId;
- bool errFl;
- } cmDspMidiFilePlay_t;
-
- /*
- 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
- These indexes are relative to the start of the file.
- When the player recieves a 'start' msg it sets the current sample index
- 'si' to 'bsi' and begins scanning for the next note to play.
- On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
- si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
- */
-
- cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
- { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
- { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
- { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
- { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
- { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
- { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
- { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
- { "id", kIdMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI file msg unique id."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
- p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
- p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
- p->mfH = cmMidiFileNullHandle;
-
- cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
- cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
- cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
- cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
- if( cmMidiFileClose(&p->mfH) )
- return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
- return kOkDspRC;
- }
-
- // return the index of the msg following smpIdx
- unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
- {
- cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
-
- if( cmMidiFileIsValid(p->mfH) == false )
- {
- cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
- return cmInvalidIdx;
- }
-
- unsigned i;
- unsigned n = cmMidiFileMsgCount(p->mfH);
- const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
-
- for(i=0; i<n; ++i)
- if( a[i]->dtick > smpIdx )
- break;
-
- return i==n ? cmInvalidIdx : i;
- }
-
- cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspRC_t rc = kOkDspRC;
- const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
- cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
-
- p->errFl = false;
-
- if( fn==NULL || strlen(fn)==0 )
- return rc;
-
- if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
- rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
- else
- {
- p->curMsgIdx = 0;
- p->bsi = cmDspInt(inst,kBsiMfId);
- p->esi = cmDspInt(inst,kEsiMfId);
- p->csi = 0;
-
- // force the first msg to occurr one quarter note into the file
- cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
-
- // convert midi msg times to absolute time in samples
- cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
-
- }
- return rc;
- }
-
- cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
-
- return _cmDspMidiFilePlayOpen(ctx,inst);
- }
-
- cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
- unsigned sPc = cmDspSamplesPerCycle(ctx);
-
-
- if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
- {
- if( cmMidiFileIsValid(p->mfH) == false )
- {
- if( p->errFl==false )
- {
- rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
- p->errFl = true;
- }
- return rc;
- }
-
- const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
- unsigned msgN = cmMidiFileMsgCount(p->mfH);
-
- for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
- {
- const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
- switch( mp->status )
- {
- case kNoteOffMdId:
- case kNoteOnMdId:
- case kCtlMdId:
- cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
- cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
- cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
- cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
- cmDspSetUInt(ctx,inst, kIdMfId, mp->uid);
- break;
- }
- }
- }
-
- p->csi += sPc;
-
- return rc;
- }
-
- cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
-
- cmDspSetEvent(ctx,inst,evt);
-
- switch(evt->dstVarId)
- {
- case kFnMfId:
- _cmDspMidiFilePlayOpen(ctx, inst );
- break;
-
- case kSelMfId:
- {
- if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
- {
- p->csi = cmDspInt(inst,kBsiMfId);
- p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
- }
- break;
- }
-
- }
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
- NULL,
- _cmDspMidiFilePlayAlloc,
- _cmDspMidiFilePlayFree,
- _cmDspMidiFilePlayReset,
- _cmDspMidiFilePlayExec,
- _cmDspMidiFilePlayRecv,
- NULL,NULL,
- "MIDI file player.");
-
- return &_cmMidiFilePlayDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kFnSfId,
- kBufCntSfId,
- kMinLkAhdSfId,
- kMaxWndCntSfId,
- kMinVelSfId,
- kIndexSfId,
- kStatusSfId,
- kD0SfId,
- kD1SfId,
- kSmpIdxSfId,
- kCmdSfId,
- kOutSfId,
- kRecentSfId,
- kDynSfId,
- kEvenSfId,
- kTempoSfId,
- kCostSfId,
- kSymSfId
- };
-
- cmDspClass_t _cmScFolDC;
- struct cmDspScFol_str;
-
- typedef struct
- {
- cmDspCtx_t* ctx;
- struct cmDspScFol_str* sfp;
- } cmDspScFolCbArg_t;
-
- typedef struct cmDspScFol_str
- {
- cmDspInst_t inst;
- cmScMatcher* sfp;
- cmScMeas* smp;
- cmScH_t scH;
- cmDspScFolCbArg_t arg;
- unsigned printSymId;
- unsigned quietSymId;
- unsigned maxScLocIdx;
- } cmDspScFol_t;
-
- cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
- { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Event buffer element count." },
- { "lkahd", kMinLkAhdSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum window look-ahead."},
- { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl, "Maximum window length."},
- { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum velocity."},
- { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
- { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
- { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
- { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
- { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
- { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
- { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Maximum score location index."},
- { "recent",kRecentSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Most recent score location index."},
- { "dyn", kDynSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Dynamic value."},
- { "even", kEvenSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Evenness value."},
- { "tempo", kTempoSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Tempo value."},
- { "cost", kCostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Match cost value."},
- { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
- { NULL, 0, 0, 0, 0, NULL }
- };
-
- cmDspScFol_t* p;
-
- if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
- return NULL;
-
- p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
- p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
- p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
- p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
- p->maxScLocIdx= cmInvalidIdx;
-
- cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
- cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
- cmDspSetDefaultUInt( ctx, &p->inst, kMinLkAhdSfId, 0, 3);
- cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
- cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kRecentSfId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kDynSfId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kEvenSfId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kTempoSfId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kCostSfId, 0, 0);
-
- cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspScFol_t* p = (cmDspScFol_t*)inst;
- cmScMatcherFree(&p->sfp);
- cmScMeasFree(&p->smp);
- cmScoreFinalize(&p->scH);
- return kOkDspRC;
- }
-
- // This is a callback function from cmScMatcherExec() which is called when
- // this cmDspFol object receives a new score location index.
- void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
- {
- cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
-
-
- if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
- {
- cmDspInst_t* inst = &(ap->sfp->inst);
-
- // send 'set' values that were calculated on the previous call to cmScMeasExec()
- unsigned i;
- for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
- if(ap->sfp->smp->set[i].value != DBL_MAX )
- {
-
- switch( ap->sfp->smp->set[i].sp->varId )
- {
- case kEvenVarScId:
- cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
- break;
-
- case kDynVarScId:
- cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
- break;
-
- case kTempoVarScId:
- cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
- break;
-
- default:
- { assert(0); }
- }
-
- cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
-
-
- // Set the values in the global variable storage
- cmDspValue_t vv,cv;
- unsigned j;
- cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
- cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
-
- for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
- {
- cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
- cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
-
- cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
- cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
- }
-
-
- }
-
- /*
- // trigger 'section' starts
- for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
- {
- const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
- if( locPtr->begSectPtr != NULL )
- cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
- }
- */
- }
- }
-
-
- cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScFol_t* p = (cmDspScFol_t*)inst;
- const cmChar_t* fn;
-
- if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
- return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
-
- if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
- return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
-
- if( cmScoreIsValid(p->scH) )
- {
- unsigned* dynRefArray = NULL;
- unsigned dynRefCnt = 0;
-
- // initialize the cmScMatcher
- if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
- rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
-
- // read the dynamics reference array
- if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
- {
- rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
- goto errLabel;
- }
-
- // initialize the cmScMeas object.
- if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
- rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
-
- }
-
- errLabel:
- return rc;
- }
-
- cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc;
- if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
- return rc;
-
- return _cmDspScFolOpenScore(ctx,inst);
- }
-
-
- cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScFol_t* p = (cmDspScFol_t*)inst;
-
- if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
- {
- switch( evt->dstVarId )
- {
- case kIndexSfId:
- if( cmScoreIsValid(p->scH) )
- {
- p->maxScLocIdx = cmInvalidIdx;
-
- if( cmScMeasReset( p->smp ) != cmOkRC )
- cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
-
- if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
- cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
- }
- break;
-
- case kStatusSfId:
- if( cmScoreIsValid(p->scH))
- {
- unsigned scLocIdx = cmInvalidIdx;
-
- // setup the cmScMeas() callback arg.
- p->arg.ctx = ctx;
- p->arg.sfp = p;
- p->sfp->cbArg = &p->arg;
-
- // this call may result in a callback to _cmScFolMatcherCb()
- if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
- if( scLocIdx != cmInvalidIdx )
- {
- // It is possible that the internal score follower may go backwards.
- // In this case it will report a given score location multiple times or out of time order.
- // The 'out' port will only be updated under the circumstances that no later
- // score location has been seen - so the last output from 'out' always reports
- // the furthest possible progress in the score. THe 'recent' output simply reports
- // the most recent output from the internal score follower which may include
- // previously reported or out of order score locations.
- cmDspSetUInt(ctx,inst,kRecentSfId,scLocIdx);
-
- if( p->maxScLocIdx==cmInvalidIdx || p->maxScLocIdx < scLocIdx )
- {
- p->maxScLocIdx = scLocIdx;
- cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
- }
- }
- }
- break;
-
- case kFnSfId:
- _cmDspScFolOpenScore(ctx,inst);
- break;
-
- case kCmdSfId:
- if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
- p->sfp->printFl = true;
- else
- if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
- p->sfp->printFl = false;
-
- break;
- }
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
- NULL,
- _cmDspScFolAlloc,
- _cmDspScFolFree,
- _cmDspScFolReset,
- NULL,
- _cmDspScFolRecv,
- NULL,NULL,
- "Score Follower");
-
- return &_cmScFolDC;
- }
-
- //==========================================================================================================================================
-
- enum
- {
- kScLocIdxMdId,
- kResetIdxMdId,
- kCmdMdId,
- kPostMdId
- };
-
- cmDspClass_t _cmModulatorDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmScModulator* mp;
- cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
- cmChar_t* fn;
- cmChar_t* modLabel;
- unsigned onSymId;
- unsigned offSymId;
- unsigned postSymId;
- } cmDspScMod_t;
-
- void _cmDspScModCb( void* arg, unsigned varSymId, double value, bool postFl )
- {
- cmDspScMod_t* p = (cmDspScMod_t*)arg;
-
- cmDspVar_t* varPtr;
- if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
- return;
-
- cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
-
- if( postFl )
- cmDspSetSymbol(p->tmp_ctx,&p->inst,kPostMdId,p->postSymId);
-
- }
-
- cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl1;
- va_copy(vl1,vl);
-
- cmDspVarArg_t args[] =
- {
- { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
- { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
- { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
- { "post", kPostMdId, 0,0, kOutDsvFl | kSymDsvFl, "Sends 'post' symbol after a message transmission if the 'post' flag is set in scMod."},
- { NULL, 0, 0, 0, 0 }
- };
-
- // validate the argument count
- if( va_cnt != 2 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
- return NULL;
- }
-
- // read the modulator file and label strings
- const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
- const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
-
- va_end(vl1);
-
- // validate the file
- if( fn==NULL || cmFsIsFile(fn)==false )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
- return NULL;
- }
-
- // allocate the internal modulator object
- cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
-
- if(mp == NULL )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
- return NULL;
- }
- unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
- unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
- cmDspVarArg_t a[ argCnt+1 ];
- unsigned i;
-
- cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
-
- for(i=fixArgCnt; i<argCnt; ++i)
- {
- unsigned varIdx = i - fixArgCnt;
- const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
- const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
- const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
-
- cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
- }
- cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
-
- cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
-
-
- p->fn = cmMemAllocStr(fn);
- p->modLabel = cmMemAllocStr(modLabel);
- p->mp = mp;
- p->onSymId = cmSymTblId(ctx->stH,"on");
- p->offSymId = cmSymTblId(ctx->stH,"off");
- p->postSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"post");
-
- mp->cbArg = p; // set the modulator callback arg
-
-
-
- cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
- cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
- return &p->inst;
- }
-
- cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScMod_t* p = (cmDspScMod_t*)inst;
-
-
- if( cmScModulatorFree(&p->mp) != kOkTlRC )
- return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
-
- cmMemFree(p->fn);
- cmMemFree(p->modLabel);
- return rc;
- }
-
-
- cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- return rc;
- }
-
- cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspScMod_t* p = (cmDspScMod_t*)inst;
-
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kResetIdxMdId:
- cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
- break;
-
- case kCmdMdId:
- {
- unsigned symId = cmDspSymbol(inst,kCmdMdId);
- if( symId == p->onSymId )
- cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
- }
- break;
-
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspScMod_t* p = (cmDspScMod_t*)inst;
-
- if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
- {
- p->tmp_ctx = ctx;
- cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
- NULL,
- _cmDspScModAlloc,
- _cmDspScModFree,
- _cmDspScModReset,
- _cmDspScModExec,
- _cmDspScModRecv,
- NULL,NULL,
- "Score Driven Variable Modulator.");
-
- return &_cmModulatorDC;
- }
-
- //==========================================================================================================================================
-
- enum
- {
- kInChCntGsId,
- kOutGroupCntGsId,
- kGroupSelIdxGsId,
- kBaseInFloatGsId
- };
-
- cmDspClass_t _cmGSwitchDC;
-
- typedef struct
- {
- cmDspInst_t inst;
-
- unsigned iChCnt;
- unsigned oGroupCnt;
-
- unsigned baseInFloatGsId;
- unsigned baseInSymGsId;
- unsigned baseInBoolGsId;
-
- unsigned baseOutFloatGsId;
- unsigned baseOutSymGsId;
- unsigned baseOutBoolGsId;
-
- } cmDspGSwitch_t;
-
-
- cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl1;
- va_copy(vl1,vl);
-
- cmDspVarArg_t args[] =
- {
- { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
- { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
- { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
- { NULL, 0, 0, 0, 0 }
- };
-
- // validate the argument count
- if( va_cnt != 2 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
- return NULL;
- }
-
- // read the input ch and output group count
- unsigned iChCnt = va_arg(vl1,unsigned);
- unsigned oGroupCnt = va_arg(vl1,unsigned);
-
- va_end(vl1);
-
- // validate the channel counts
- if( iChCnt == 0 || oGroupCnt==0 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
- return NULL;
- }
-
- unsigned typeCnt = 3; // i.e. float,sym,bool
- unsigned baseInFloatGsId = kBaseInFloatGsId;
- unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
- unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
- unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
- unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
- unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
-
- unsigned fixArgCnt = 3;
- unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
- unsigned argCnt = fixArgCnt + varArgCnt;
- cmDspVarArg_t a[ argCnt+1 ];
- unsigned i;
-
- cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
- cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
- cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
- cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
-
- unsigned labelCharCnt = 63;
- cmChar_t label[labelCharCnt+1];
- label[labelCharCnt] = 0;
-
- unsigned gsid = baseOutFloatGsId;
- for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
- {
- snprintf(label,labelCharCnt,"f-out-%i",i);
- cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
- }
-
- gsid = baseOutSymGsId;
- for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
- {
- snprintf(label,labelCharCnt,"s-out-%i",i);
- cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
- }
-
- gsid = baseOutBoolGsId;
- for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
- {
- snprintf(label,labelCharCnt,"b-out-%i",i);
- cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
- }
-
- cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
-
- cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
-
- p->iChCnt = iChCnt;
- p->oGroupCnt = oGroupCnt;
- p->baseInFloatGsId = baseInFloatGsId;
- p->baseInSymGsId = baseInSymGsId;
- p->baseInBoolGsId = baseInBoolGsId;
- p->baseOutFloatGsId = baseOutFloatGsId;
- p->baseOutSymGsId = baseOutSymGsId;
- p->baseOutBoolGsId = baseOutBoolGsId;
-
- cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- return rc;
- }
-
- cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
-
- // if this is the group selector
- if( evt->dstVarId == kGroupSelIdxGsId )
- {
- unsigned idx;
- if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
- cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
- else
- cmDspSetEvent(ctx,inst,evt);
- return rc;
- }
-
- // get the group selector
- unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
- assert( groupIdx < p->oGroupCnt);
-
-
- // if this is a float input
- if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
- {
- unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
- cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
- return rc;
- }
-
- // if this is a symbol input
- if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
- {
- unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
- cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
- return rc;
- }
-
- // if this is a bool input
- if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
- {
- unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
- cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
- return rc;
- }
-
- return rc;
- }
-
-
- struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
- NULL,
- _cmDspGSwitchAlloc,
- NULL,
- _cmDspGSwitchReset,
- NULL,
- _cmDspGSwitchRecv,
- NULL,NULL,
- "Ganged switch.");
-
- return &_cmGSwitchDC;
- }
-
-
- //==========================================================================================================================================
-
- enum
- {
- kMinInSrId,
- kMaxInSrId,
- kMinOutSrId,
- kMaxOutSrId,
- kValInSrId,
- kValOutSrId,
- };
-
- cmDspClass_t _cmScaleRangeDC;
-
- typedef struct
- {
- cmDspInst_t inst;
-
- } cmDspScaleRange_t;
-
-
- cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl1;
- va_copy(vl1,vl);
-
- cmDspVarArg_t args[] =
- {
- { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
- { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
- { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
- { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
- { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
- { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
- cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
-
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- return rc;
- }
-
- cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
-
- cmDspSetEvent(ctx,inst,evt);
-
- if( evt->dstVarId == kValInSrId )
- {
- double val = cmDspDouble(inst,kValInSrId);
- double min_in = cmDspDouble(inst,kMinInSrId);
- double max_in = cmDspDouble(inst,kMaxInSrId);
- double min_out = cmDspDouble(inst,kMinOutSrId);
- double max_out = cmDspDouble(inst,kMaxOutSrId);
-
- double x = cmMax(min_in,cmMin(max_in,val));
-
- x = (x - min_in)/(max_in - min_in);
-
- x = min_out + x * (max_out - min_out);
-
- cmDspSetDouble(ctx,inst,kValOutSrId, x );
- //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
- }
- return rc;
- }
-
-
- struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
- NULL,
- _cmDspScaleRangeAlloc,
- NULL,
- _cmDspScaleRangeReset,
- NULL,
- _cmDspScaleRangeRecv,
- NULL,NULL,
- "Scale a value inside an input range to a value in the output range.");
-
- return &_cmScaleRangeDC;
- }
-
-
- //==========================================================================================================================================
-
- enum
- {
- kCntAmId,
- kSflocAmId,
- kLocAmId,
- kTypeAmId,
- kValueAmId,
- kCstAmId,
- kCmdAmId,
- kScLocAmId,
- kEvenAmId,
- kDynAmId,
- kTempoAmId,
- kCostAmId
- };
-
- cmDspClass_t _cmActiveMeasDC;
-
- typedef struct
- {
- unsigned loc;
- unsigned type;
- double value;
- double cost;
- } cmDspActiveMeasRecd_t;
-
- int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
- {
- return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
- }
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned addSymId;
- unsigned clearSymId;
- unsigned printSymId;
- unsigned rewindSymId;
- cmDspActiveMeasRecd_t* array; // array[cnt]
- unsigned cnt;
- unsigned nextEmptyIdx;
- unsigned nextFullIdx;
- } cmDspActiveMeas_t;
-
- cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
-
- cmDspVarArg_t args[] =
- {
- { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
- { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
- { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
- { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type." },
- { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
- { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
- { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
- { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
- { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
- { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
- { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
- { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
- { NULL, 0, 0, 0, 0 }
- };
-
-
-
- cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
- p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
- p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
- p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
-
- cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,100);
- cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
- cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
-
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
- {
- unsigned i;
- for(i=0; i<p->nextEmptyIdx; ++i)
- {
- const cmChar_t* label = "<null>";
- switch( p->array[i].type )
- {
- case kEvenVarScId: label="even "; break;
- case kDynVarScId: label="dyn "; break;
- case kTempoVarScId: label="tempo"; break;
- default:
- { assert(0); }
- }
-
- cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",p->array[i].loc,label,p->array[i].value,p->array[i].cost);
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
- {
- p->nextEmptyIdx = 0;
- p->nextFullIdx = cmInvalidIdx;
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
- _cmDspActiveMeasClear(ctx,p);
- cmMemPtrFree(&p->array);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
- _cmDspActiveMeasFree(ctx,inst,evt);
- p->array = cmMemAllocZ(cmDspActiveMeasRecd_t,cnt);
- p->cnt = cnt;
- return rc;
- }
-
- cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kSflocAmId:
- if( p->nextFullIdx != cmInvalidIdx )
- {
- // get the recv'd score location
- unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
-
- unsigned prvLoc = cmInvalidIdx;
-
- // for each remaining avail record
- for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
- {
- cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
-
- // if this records score location is after the recv'd score loc then we're done
- if( r->loc > sflocIdx )
- break;
-
- // deterimine the records type
- unsigned varId = cmInvalidId;
- switch( r->type )
- {
- case kEvenVarScId: varId = kEvenAmId; break;
- case kDynVarScId: varId = kDynAmId; break;
- case kTempoVarScId: varId = kTempoAmId; break;
- default:
- { assert(0); }
- }
-
- // if this score location has not yet been sent then send it now
- if( prvLoc != r->loc )
- cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
-
- // transmit the records value and cost
- cmDspSetDouble(ctx,inst,varId,r->value);
- cmDspSetDouble(ctx,inst,kCostAmId,r->cost);
-
- prvLoc = r->loc;
- }
-
-
- }
- break;
-
- case kCmdAmId:
- {
- unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
-
- if( cmdSymId == p->addSymId )
- {
- if( p->nextEmptyIdx >= p->cnt )
- cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
- else
- {
- cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
- r->loc = cmDspUInt( inst,kLocAmId);
- r->type = cmDspUInt( inst,kTypeAmId);
- r->value = cmDspDouble(inst,kValueAmId);
- r->cost = cmDspDouble(inst,kCstAmId);
- p->nextEmptyIdx += 1;
-
- qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
-
- if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
- p->nextFullIdx = 0;
-
- }
- }
-
- if( cmdSymId == p->clearSymId )
- rc = _cmDspActiveMeasClear(ctx,p);
- else
- if( cmdSymId == p->printSymId )
- rc = _cmDspActiveMeasPrint(ctx,p);
- else
- if(cmdSymId == p->rewindSymId )
- p->nextFullIdx = 0;
- }
- break;
-
- }
- return rc;
- }
-
-
- struct cmDspClass_str* cmActiveMeasClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
- NULL,
- _cmDspActiveMeasAlloc,
- _cmDspActiveMeasFree,
- _cmDspActiveMeasReset,
- NULL,
- _cmDspActiveMeasRecv,
- NULL,NULL,
- "Scale a value inside an input range to a value in the output range.");
-
- return &_cmActiveMeasDC;
- }
-
- //==========================================================================================================================================
- // Audio MIDI Sync
- /*
- Usage:
- 1) In the program resource file setup a list of sync points.
- 'asmp' refers to a sample offset into the audio file 'af'
- which should match to the midi event index 'mid' in the
- midi file 'mf'.
-
- amSync :
- [
- { af:"af-16" asmp:34735276 mf:"mf-10" mid:350 }
- { af:"af-16" asmp:71802194 mf:"mf-10" mid:787 }
- ]
-
- 2) Feed the 'fidx' output from a wave table loaded with 'af' into the 'asmp' input port of this amSync object.
- Feed the 'id' output from the MIDI file player loaded with 'mf' into the 'mid' input port of this amSync object.
-
- 3) Run the players.
- 4) When the run is complete send any message to the 'sel' port of this amSync object.
- The 'frm:' field of the printed output gives the difference in samples between
- MIDI and audio sync points.
-
- If the value is positive then the MIDI point is after the Audio point.
- If the value is negative then the MIDI point is before the audio point.
-
- */
-
- enum
- {
- kSelAmId,
- kAFnAmId,
- kASmpAmId,
- kMFnAmId,
- kMIdAmId,
- };
-
- enum
- {
- kAfnAmFl = 0x01,
- kMfnAmFl = 0x02,
- kAsmpAmFl = 0x04,
- kMidAmFl = 0x08,
- };
-
- cmDspClass_t _cmAmSyncDC;
-
- typedef struct cmDspAmSyncEntry_str
- {
- const cmChar_t* afn; // audio file name
- const cmChar_t* mfn; // midi file name
- unsigned asmp; // Audio sample index to sync to MIDI event
- unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
- int afi; // closest DSP system cycle index to the reference audio sample index (asmp).
- int mfi; // DSP system cycle on which the reference MIDI event (mid) arrived.
- unsigned state; // as incoming msg match this record the state is updated with kXXXAmFl flags
- } cmDspAmSyncEntry_t;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmDspAmSyncEntry_t* array;
- unsigned arrayCnt;
- cmDspAmSyncEntry_t* acur;
- cmDspAmSyncEntry_t* mcur;
- } cmDspAmSync_t;
-
- cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
- { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
- { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
- { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
- { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
-
- cmMemFree(p->array);
-
- return kOkDspRC;
- }
-
-
- cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
- cmJsonNode_t* np;
- const cmChar_t* errLabelPtr;
- unsigned i;
- cmJsRC_t jsRC;
-
- if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
- {
- rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
- goto errLabel;
- }
-
- p->arrayCnt = cmJsonChildCount(np);
-
- p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
-
- for(i=0; i<p->arrayCnt; ++i)
- {
- cmJsonNode_t* anp = cmJsonArrayElement(np,i);
-
- cmDspAmSyncEntry_t* r = p->array + i;
- if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
- "af", kStringTId,&r->afn,
- "asmp",kIntTId, &r->asmp,
- "mf", kStringTId,&r->mfn,
- "mid", kIntTId, &r->mid,
- NULL)) != kOkJsRC )
- {
- if( jsRC == kNodeNotFoundJsRC )
- rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
- else
- rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
- break;
- }
- r->afi = cmInvalidIdx;
- r->mfi = cmInvalidIdx;
- r->state = 0;
- }
-
- errLabel:
- return rc;
- }
-
-
- cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
- unsigned i;
-
- if( evt->dstVarId != kSelAmId )
- cmDspSetEvent(ctx,inst,evt);
-
- switch(evt->dstVarId)
- {
- case kSelAmId:
- {
- double srate = cmDspSysSampleRate(ctx->dspH);
- int fpc = cmDspSamplesPerCycle(ctx);
-
- for(i=0; i<p->arrayCnt; ++i)
- {
- cmDspAmSyncEntry_t* r = p->array + i;
-
- int dframes = r->mfi-r->afi;
- cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
- r->state,
- r->afn,
- r->asmp,
- r->afi,
- r->mfn,
- r->mid,
- r->mfi,
- dframes,
- dframes*fpc,dframes*fpc/srate);
-
- r->afi = cmInvalidIdx;
- r->mfi = cmInvalidIdx;
- r->state = 0;
-
- }
- p->acur = NULL;
- p->mcur = NULL;
- }
- break;
-
- case kAFnAmId:
- {
- // an audio file name just arrived - set p->acur to point to it
- const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
-
- for(i=0; i<p->arrayCnt; ++i)
- if( strcmp(fn,p->array[i].afn) == 0 )
- {
- p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
- p->acur = p->array + i;
- }
- }
- break;
-
- case kMFnAmId:
- {
- // a midi file name just arrived - set p->mcur to point to it
- const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
- for(i=0; i<p->arrayCnt; ++i)
- if( strcmp(fn,p->array[i].mfn) == 0 )
- {
- p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
- p->mcur = p->array + i;
- }
- }
- break;
-
- case kASmpAmId:
- {
- // a audio file sample index has just arrived
- int v = cmDspInt(inst,kASmpAmId);
-
- // if a valid audio file has been set
- if( p->acur != NULL )
- for(i=0; i<p->arrayCnt; ++i)
- {
- // if the audio sync point is before or on the new audio file sample index then
- // this is the closest audio file index to the audio sync point - record the
- // associated cycleCnt
- cmDspAmSyncEntry_t* r = p->array + i;
- if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
- {
- r->afi = ctx->cycleCnt;
- r->state = cmSetFlag(r->state,kAsmpAmFl);
- break;
- }
- }
- }
- break;
-
- case kMIdAmId:
- {
- // a new MIDI event was received
- int v = cmDspInt(inst,kMIdAmId);
- if( p->mcur != NULL )
- for(i=0; i<p->arrayCnt; ++i)
- {
- // if the new MIDI event matched the MIDI sync point then record the
- // current cycleCnt.
- cmDspAmSyncEntry_t* r = p->array + i;
- if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
- {
- r->mfi = ctx->cycleCnt;
- r->state = cmSetFlag(r->state,kMidAmFl);
- break;
- }
- }
-
- }
- break;
-
- }
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmAmSyncClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
- NULL,
- _cmDspAmSyncAlloc,
- _cmDspAmSyncFree,
- _cmDspAmSyncReset,
- NULL,
- _cmDspAmSyncRecv,
- NULL,NULL,
- "Audio - MIDI Sync Object.");
-
- return &_cmAmSyncDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kPgmNmId,
- kStatusNmId,
- kD0NmId,
- kD1NmId,
- kThruNmId
- };
-
- cmDspClass_t _cmNanoMapDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspNanoMap_t;
-
- cmDspRC_t _cmDspNanoMapSend( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned st, unsigned d0, unsigned d1 )
- {
- cmDspSetUInt(ctx,inst,kD1NmId,d1);
- cmDspSetUInt(ctx,inst,kD0NmId,d0);
- cmDspSetUInt(ctx,inst,kStatusNmId,st);
- return kOkDspRC;
- }
-
- void _cmDspNanoMapPgm( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned pgm )
- {
- unsigned i;
-
- for(i=0; i<kMidiChCnt; ++i)
- {
- _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,121,0); // reset all controllers
- _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,123,0); // turn all notes off
- _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,0,0); // switch to bank 0
- _cmDspNanoMapSend(ctx,inst,kPgmMdId+i,pgm,0); // send pgm change
- cmSleepMs(15);
- }
-
- }
-
- cmDspInst_t* _cmDspNanoMapAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "pgm", kPgmNmId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reprogram all channels to this pgm." },
- { "status", kStatusNmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
- { "d0", kD0NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
- { "d1", kD1NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
- { "thru", kThruNmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable pass through."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspNanoMap_t* p = cmDspInstAlloc(cmDspNanoMap_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt(ctx,&p->inst, kPgmNmId, 0, 0 );
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspNanoMapReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
-
- return rc;
- }
-
- cmDspRC_t _cmDspNanoMapRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
-
- switch( evt->dstVarId )
- {
- case kPgmNmId:
- cmDspSetEvent(ctx,inst,evt);
- _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
- break;
-
- case kStatusNmId:
- {
- unsigned status = cmDsvGetUInt(evt->valuePtr);
- if( (status & 0xf0) == kNoteOnMdId )
- {
- unsigned d0 = cmDspUInt(inst,kD0NmId);
- unsigned ch = d0 % 8;
- status = (status & 0xf0) + ch;
- cmDspSetUInt(ctx,inst,kStatusNmId,status);
- }
- }
- break;
-
-
- default:
- cmDspSetEvent(ctx,inst,evt);
- break;
- }
-
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmNanoMapClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmNanoMapDC,ctx,"NanoMap",
- NULL,
- _cmDspNanoMapAlloc,
- NULL,
- _cmDspNanoMapReset,
- NULL,
- _cmDspNanoMapRecv,
- NULL,
- NULL,
- "Nanosynth Mapper");
-
- return &_cmNanoMapDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kChCntPrId,
- kFnPrId,
- kSecsPrId,
- kMaxLaSecsPrId,
- kCurLaSecsPrId,
- kFadeRatePrId,
- kScLocIdxPrId,
- kCmdPrId,
- kInAudioBasePrId
- };
-
- cmDspClass_t _cmRecdPlayDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmRecdPlay* rcdply;
- cmScH_t scH;
- unsigned onSymId;
- unsigned offSymId;
- unsigned audioOutBaseId;
- unsigned chCnt;
- unsigned scLocIdx;
- } cmDspRecdPlay_t;
-
- cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspRC_t rc =kOkDspRC;
- const cmChar_t* fn;
-
- cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
-
- p->scLocIdx = 0;
-
-
- if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
- return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
-
- if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
- return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
-
- if( cmScoreIsValid(p->scH) )
- {
- unsigned i;
- unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
- double initFragSecs = cmDspDouble(inst,kSecsPrId);
- double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
- double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
-
- if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
- return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
-
- for(i=0; i<markerCnt; ++i)
- cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
-
- }
-
- return rc;
- }
-
-
- cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
-
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
- return NULL;
- }
-
- va_list vl1;
- va_copy(vl1,vl);
-
- int chCnt = va_arg(vl,int);
- unsigned audioOutBase = kInAudioBasePrId + chCnt;
-
- cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
- 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
- 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
- 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
- 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl, "Maximum look-ahead buffer in seconds.",
- 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl, "Current look-head buffer in seconds.",
- 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl, "Fade rate in dB per second.",
- 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
- 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
- chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
- chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
- 0 );
-
- va_end(vl1);
-
- p->onSymId = cmSymTblId(ctx->stH,"on");
- p->offSymId = cmSymTblId(ctx->stH,"off");
- p->audioOutBaseId = audioOutBase;
- p->chCnt = chCnt;
- p->scLocIdx = 0;
-
- cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
- cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
- cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
- cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
-
- cmRecdPlayFree(&p->rcdply);
-
- cmScoreFinalize(&p->scH);
- return rc;
- }
-
- cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
-
- return _cmDspRecdPlayOpenScore(ctx,inst);
- }
-
- cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
-
- const cmSample_t* x[ p->chCnt ];
- cmSample_t* y[ p->chCnt ];
- unsigned n;
- unsigned i;
- unsigned actChCnt = 0;
-
- for(i=0; i<p->chCnt; ++i)
- {
- if( i==0 )
- n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
- else
- { assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0)); }
-
- x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
-
- if( x[i] != NULL )
- {
- y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
-
- if( y[i] != NULL )
- {
- assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
-
- cmVOS_Zero(y[i],n);
-
- actChCnt += 1;
- }
-
- }
- }
-
- cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
-
- return rc;
- }
-
- cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
-
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kCmdPrId:
- if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
- {
- printf("rewind\n");
- cmRecdPlayRewind(p->rcdply);
- p->scLocIdx = 0;
- }
- else
- if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
- {
- }
-
- break;
-
- case kCurLaSecsPrId:
- cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
- break;
-
- case kScLocIdxPrId:
- {
- unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId) ;
-
- for(; p->scLocIdx<=endScLocIdx; p->scLocIdx+=1)
- {
- cmScoreLoc_t* loc = cmScoreLoc(p->scH, p->scLocIdx );
- cmScoreMarker_t* mp = loc->markList;
-
- for(; mp!=NULL; mp=mp->link)
- switch( mp->markTypeId )
- {
- case kRecdBegScMId:
- printf("recd-beg\n");
- cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
- break;
-
- case kRecdEndScMId:
- printf("recd-end\n");
- cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
- break;
-
- case kPlayBegScMId:
- printf("play-beg\n");
- cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
- break;
-
- case kPlayEndScMId:
- printf("play-end\n");
- cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
- break;
-
- case kFadeScMId:
- printf("fade-beg\n");
- cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
- break;
-
- default:
- break;
- }
- }
-
- p->scLocIdx = endScLocIdx+1;
- }
- break;
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmRecdPlayClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
- NULL,
- _cmDspRecdPlayAlloc,
- _cmDspRecdPlayFree,
- _cmDspRecdPlayReset,
- _cmDspRecdPlayExec,
- _cmDspRecdPlayRecv,
- NULL,
- NULL,
- "Score controlled live recorder/player");
-
- return &_cmRecdPlayDC;
- }
|