1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmTime.h"
- #include "cmAudioPort.h"
- #include "cmAudioNrtDev.h"
- #include "cmAudioPortFile.h"
- #include "cmApBuf.h"
- #include "cmJson.h"
- #include "cmThread.h"
- #include "cmUdpPort.h"
- #include "cmUdpNet.h"
- #include "cmRtSysMsg.h"
- #include "cmRtNet.h"
- #include "cmRtSys.h"
- #include "cmMidi.h"
- #include "cmMidiPort.h"
-
- #include "cmMath.h"
-
- typedef enum
- {
- kNoCmdId,
- kEnableCbCmdId,
- kDisableCbCmdId
- } kRtCmdId_t;
-
- cmRtSysH_t cmRtSysNullHandle = cmSTATIC_NULL_HANDLE;
-
- struct cmRt_str;
-
- typedef struct
- {
- struct cmRt_str* p; // pointer to the real-time system instance which owns this sub-system
- cmRtSysSubSys_t ss; // sub-system configuration record
- cmRtSysCtx_t ctx; // DSP context
- cmRtSysStatus_t status; // current runtime status of this sub-system
- cmThreadH_t threadH; // real-time system thread
- cmTsMp1cH_t htdQueueH; // host-to-dsp thread safe msg queue
- cmThreadMutexH_t engMutexH; // thread mutex and condition variable
- cmRtNetH_t netH;
- bool runFl; // false during finalization otherwise true
- bool statusFl; // true if regular status notifications should be sent
- bool syncInputFl;
-
- kRtCmdId_t cmdId; // written by app thread, read by rt thread
- unsigned cbEnableFl; // written by rt thread, read by app thread
-
- bool noBlockEnaFl; //
- unsigned noBlockSleepMs;
-
- double* iMeterArray; //
- double* oMeterArray; //
-
- unsigned statusUpdateSmpCnt; // transmit a state update msg every statusUpdateSmpCnt samples
- unsigned statusUpdateSmpIdx; // state update phase
-
- } _cmRtCfg_t;
-
- typedef struct cmRt_str
- {
- cmErr_t err;
- cmCtx_t* ctx;
- _cmRtCfg_t* ssArray;
- unsigned ssCnt;
- unsigned waitRtSubIdx; // index of the next sub-system to try with cmRtSysIsMsgWaiting().
- cmTsMp1cH_t dthQueH;
- bool initFl; // true if the real-time system is initialized
-
- cmTsQueueCb_t clientCbFunc; // These fields are only used during configuration.
- void* clientCbArg; // See cmRtBeginCfg() and cmRtCfg().
-
- } cmRt_t;
-
-
- cmRt_t* _cmRtHandleToPtr( cmRtSysH_t h )
- {
- cmRt_t* p = (cmRt_t*)h.h;
- assert(p != NULL);
- return p;
- }
-
- cmRtRC_t _cmRtError( cmRt_t* p, cmRtRC_t rc, const char* fmt, ... )
- {
- va_list vl;
- va_start(vl,fmt);
- cmErrVMsg(&p->err,rc,fmt,vl);
- va_end(vl);
- return rc;
- }
-
- // Wrapper function to put msgs into thread safe queues and handle related errors.
- cmRtRC_t _cmRtEnqueueMsg( cmRt_t* p, cmTsMp1cH_t qH, const void* msgDataPtrArray[], unsigned msgCntArray[], unsigned segCnt, const char* queueLabel )
- {
- cmRtRC_t rc = kOkRtRC;
-
- switch( cmTsMp1cEnqueueSegMsg(qH, msgDataPtrArray, msgCntArray, segCnt) )
- {
- case kOkThRC:
- break;
-
- case kBufFullThRC:
- {
- unsigned i;
- unsigned byteCnt = 0;
- for(i=0; i<segCnt; ++i)
- byteCnt += msgCntArray[i];
-
- rc = _cmRtError(p,kMsgEnqueueFailRtRC,"The %s queue was unable to load a msg containing %i bytes. The queue is currently allocated %i bytes and has %i bytes available.",queueLabel,byteCnt,cmTsMp1cAllocByteCount(qH),cmTsMp1cAvailByteCount(qH));
- }
- break;
-
- default:
- rc = _cmRtError(p,kMsgEnqueueFailRtRC,"A %s msg. enqueue failed.",queueLabel);
- }
-
- return rc;
- }
-
- // This is the function pointed to by ctx->dspToHostFunc.
- // It is called by the DSP proces to pass msgs to the host.
- // therefore it is always called from inside of _cmRtDspExecCallback().
- cmRtRC_t _cmRtDspToHostMsgCallback(struct cmRtSysCtx_str* ctx, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt)
- {
- cmRt_t* p = (cmRt_t*)ctx->reserved;
- assert( ctx->rtSubIdx < p->ssCnt );
- return _cmRtEnqueueMsg(p,p->dthQueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"DSP-to-Host");
- }
-
- cmRtRC_t _cmRtSysDspToHostSegMsg( cmRt_t* p, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt)
- {
- return _cmRtEnqueueMsg(p,p->dthQueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"DSP-to-Host");
- }
-
- cmRtRC_t cmRtSysDspToHostSegMsg( cmRtSysH_t h, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt)
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- return _cmRtSysDspToHostSegMsg(p,msgDataPtrArray,msgByteCntArray,msgSegCnt);
- }
-
- cmRtRC_t cmRtSysDspToHost( cmRtSysH_t h, const void* msgDataPtr, unsigned msgByteCnt)
- {
- const void* msgDataArray[] = { msgDataPtr };
- unsigned msgByteCntArray[] = { msgByteCnt };
- return cmRtSysDspToHostSegMsg(h,msgDataArray,msgByteCntArray,1);
- }
-
- cmRtRC_t _cmRtParseNonSubSysMsg( cmRt_t* p, const void* msg, unsigned msgByteCnt )
- {
- cmRtRC_t rc = kOkRtRC;
- cmRtSysMstr_t* m = (cmRtSysMstr_t*)msg;
- /*
- unsigned devIdx = cmRtSysUiInstIdToDevIndex(h->instId);
- unsigned chIdx = cmRtSysUiInstIdToChIndex(h->instId);
- unsigned inFl = cmRtSysUiInstIdToInFlag(h->instId);
- unsigned ctlId = cmRtSysUiInstIdToCtlId(h->instId);
- */
-
- // if the valuu associated with this msg is a mtx then set
- // its mtx data area pointer to just after the msg header.
- //if( cmDsvIsMtx(&h->value) )
- // h->value.u.m.u.vp = ((char*)msg) + sizeof(cmDspUiHdr_t);
-
- unsigned flags = m->inFl ? kInApFl : kOutApFl;
-
- switch( m->ctlId )
- {
-
- case kSliderUiRtId: // slider
- cmApBufSetGain(m->devIdx,m->chIdx, flags, m->value);
- break;
-
- case kMeterUiRtId: // meter
- break;
-
- case kMuteUiRtId: // mute
- flags += m->value == 0 ? kEnableApFl : 0;
- cmApBufEnableChannel(m->devIdx,m->chIdx,flags);
- break;
-
- case kToneUiRtId: // tone
- flags += m->value > 0 ? kEnableApFl : 0;
- cmApBufEnableTone(m->devIdx,m->chIdx,flags);
- break;
-
- case kPassUiRtId: // pass
- flags += m->value > 0 ? kEnableApFl : 0;
- cmApBufEnablePass(m->devIdx,m->chIdx,flags);
- break;
-
- default:
- { assert(0); }
- }
-
- return rc;
- }
-
- // Process a UI msg sent from the host to the real-time system
- cmRtRC_t _cmRtHandleNonSubSysMsg( cmRt_t* p, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt )
- {
- cmRtRC_t rc = kOkRtRC;
-
- // if the message is contained in a single segment it can be dispatched immediately ...
- if( msgSegCnt == 1 )
- rc = _cmRtParseNonSubSysMsg(p,msgDataPtrArray[0],msgByteCntArray[0]);
- else
- {
- // ... otherwise deserialize the message into contiguous memory ....
- unsigned byteCnt = 0;
- unsigned i;
-
- for(i=0; i<msgSegCnt; ++i)
- byteCnt += msgByteCntArray[i];
-
- char buf[ byteCnt ];
- char* b = buf;
- for(i=0; i<msgSegCnt; ++i)
- {
- memcpy(b, msgDataPtrArray[i], msgByteCntArray[i] );
- b += msgByteCntArray[i];
- }
- // ... and then dispatch it
- rc = _cmRtParseNonSubSysMsg(p,buf,byteCnt);
-
- }
-
- return rc;
- }
-
-
- cmRtRC_t _cmRtSendStateStatusToHost( _cmRtCfg_t* cp )
- {
- cmRtRC_t rc = kOkRtRC;
-
- cp->status.hdr.rtSubIdx = cp->ctx.rtSubIdx;
- cp->status.hdr.selId = kStatusSelRtId;
-
- cmApBufGetStatus( cp->ss.args.inDevIdx, kInApFl, cp->iMeterArray, cp->status.iMeterCnt, &cp->status.overflowCnt );
- cmApBufGetStatus( cp->ss.args.outDevIdx, kOutApFl, cp->oMeterArray, cp->status.oMeterCnt, &cp->status.underflowCnt );
-
- unsigned iMeterByteCnt = sizeof(cp->iMeterArray[0]) * cp->status.iMeterCnt;
- unsigned oMeterByteCnt = sizeof(cp->oMeterArray[0]) * cp->status.oMeterCnt;
- const void* msgDataPtrArray[] = { &cp->status, cp->iMeterArray, cp->oMeterArray };
- unsigned msgByteCntArray[] = { sizeof(cp->status), iMeterByteCnt, oMeterByteCnt };
- unsigned segCnt = sizeof(msgByteCntArray)/sizeof(unsigned);
-
- _cmRtSysDspToHostSegMsg(cp->p,msgDataPtrArray,msgByteCntArray, segCnt );
-
- return rc;
- }
-
- // This function is called, within the real-time thread,
- // with _cmRtRecd.engMutexH locked, to deliver
- // messages to the real-time DSP processes via cp->ss.cbFunc()
- cmRtRC_t _cmRtDeliverMsgsWithLock( _cmRtCfg_t* cp )
- {
- int i;
- cmRtRC_t rc = kOkThRC;
-
- // as long msg's are in the queue incoming msg queue
- for(i=0; rc == kOkThRC; ++i)
- {
- // if a msg is waiting transmit it via cp->ss.cbFunc()
- if((rc = cmTsMp1cDequeueMsg(cp->htdQueueH,NULL,0)) == kOkThRC)
- ++cp->status.msgCbCnt;
- }
-
- return rc;
- }
-
- // This funciton is _cmRtDspExecCallback()->cmRtNetReceive() in the
- // real-time thread to deliver msg's to the DSP process.
- void _cmRtSysNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
- {
- _cmRtCfg_t* cp = (_cmRtCfg_t*)cbArg;
-
- if( cp->cbEnableFl )
- {
- cmRtSysH_t h;
- h.h = cp->p;
- cmRtSysDeliverMsg(h,data,dataByteCnt,cmInvalidId);
- }
- }
-
- // The DSP execution callback happens through this function.
- // This function is only called from inside _cmRtThreadCallback()
- // with the engine mutex locked.
- void _cmRtDspExecCallback( _cmRtCfg_t* cp )
- {
-
- // Fill iChArray[] and oChArray[] with pointers to the incoming and outgoing sample buffers.
- // Notes:
- // 1) Buffers associated with disabled input/output channels will be set to NULL in iChArray[]/oChArray[].
- // 2) Buffers associated with channels marked for pass-through will be set to NULL in oChArray[].
- // 3) All samples returned in oChArray[] buffers will be set to zero.
- if( cp->noBlockEnaFl == false )
- cmApBufGetIO(cp->ss.args.inDevIdx, cp->ctx.iChArray, cp->ctx.iChCnt, &cp->ctx.iTimeStamp,
- cp->ss.args.outDevIdx, cp->ctx.oChArray, cp->ctx.oChCnt, &cp->ctx.oTimeStamp );
-
-
- // calling this function results in callbacks to _cmRtSysNetRecv()
- // which in turn calls cmRtSysDeliverMsg() which queues any incoming messages
- // which are then transferred to the DSP processes by the the call to
- // _cmRtDeliverMsgWithLock() below.
- if( cmRtNetIsValid(cp->netH) )
- if( cmRtNetReceive(cp->netH) != kOkNetRC )
- _cmRtError(cp->p,kNetErrRtRC,"Network receive failed.");
-
- // NOTE: BY DEQUEUEING MSGS FIRST AND THEN SERVICING THE NETWORK
- // WE COULD ELIMINATE QUEUEING NETWORK MESSAGES - THEY COULD BE
- // SEND DIRECTLY THROUGH TO THE DSP PROCESSES
-
- // if there are msgs waiting to be sent to the DSP process send them.
- if( cp->cbEnableFl )
- if( cmTsMp1cMsgWaiting(cp->htdQueueH) )
- _cmRtDeliverMsgsWithLock(cp);
-
-
- // call the application provided DSP process
- if( cp->cbEnableFl )
- {
- cp->ctx.audioRateFl = true;
- cp->ss.cbFunc( &cp->ctx, 0, NULL );
- cp->ctx.audioRateFl = false;
- }
-
- // Notice client callback enable/disable
- // requests from the client thread
- switch( cp->cmdId )
- {
- case kNoCmdId:
- break;
-
- case kDisableCbCmdId:
- if( cp->cbEnableFl )
- cmThUIntDecr(&cp->cbEnableFl,1);
- break;
-
- case kEnableCbCmdId:
- if( cp->cbEnableFl==0)
- cmThUIntIncr(&cp->cbEnableFl,1);
- break;
- }
-
- // advance the audio buffer
- if( cp->noBlockEnaFl == false )
- {
- cmApBufAdvance( cp->ss.args.outDevIdx, kOutApFl );
- cmApBufAdvance( cp->ss.args.inDevIdx, kInApFl );
- }
-
- // handle periodic status messages to the host
- if( (cp->statusUpdateSmpIdx += cp->ss.args.dspFramesPerCycle) >= cp->statusUpdateSmpCnt )
- {
- cp->statusUpdateSmpIdx -= cp->statusUpdateSmpCnt;
-
- if( cp->statusFl )
- _cmRtSendStateStatusToHost(cp);
- }
-
- }
-
- // Returns true if audio buffer is has waiting incoming samples and
- // available outgoing space.
- bool _cmRtBufIsReady( const _cmRtCfg_t* cp )
- {
- // if there neither the input or output device is valid
- if( cp->ss.args.inDevIdx==cmInvalidIdx && cp->ss.args.outDevIdx == cmInvalidIdx )
- return false;
-
- bool ibFl = cmApBufIsDeviceReady(cp->ss.args.inDevIdx, kInApFl);
- bool obFl = cmApBufIsDeviceReady(cp->ss.args.outDevIdx, kOutApFl);
- bool iFl = (cp->ss.args.inDevIdx == cmInvalidIdx) || ibFl;
- bool oFl = (cp->ss.args.outDevIdx == cmInvalidIdx) || obFl;
-
- //printf("br: %i %i %i %i\n",ibFl,obFl,iFl,oFl);
-
- return iFl && oFl;
- }
-
-
-
-
- // This is the main real-time system loop (and thread callback function).
- // It blocks by waiting on a cond. var (which simultaneously unlocks a mutex).
- // With the mutex unlocked messages can pass directly to the DSP process
- // via calls to cmRtDeliverMsg().
- // When the audio buffers need to be serviced the audio device callback
- // signals the cond. var. which results in this thread waking up (and
- // simultaneously locking the mutex) as soon as the mutex is available.
- bool _cmRtThreadCallback(void* arg)
- {
- cmRtRC_t rc;
- _cmRtCfg_t* cp = (_cmRtCfg_t*)arg;
- bool noBlockFl = false;
-
- // lock the cmRtSys mutex
- if((rc = cmThreadMutexLock(cp->engMutexH)) != kOkRtRC )
- {
- _cmRtError(cp->p,rc,"The cmRtSys thread mutex lock failed.");
- return false;
- }
-
- // runFl is always set except during finalization
- while( cp->runFl )
- {
-
-
- // if the buffer is NOT ready or the cmRtSys is disabled
- if(_cmRtBufIsReady(cp) == false || cp->cbEnableFl==false )
- {
- // block on the cond var and unlock the mutex
- if( noBlockFl )
- cmSleepMs(cp->noBlockSleepMs);
- else
- {
- if( cmThreadMutexWaitOnCondVar(cp->engMutexH,false) != kOkRtRC )
- {
- cmThreadMutexUnlock(cp->engMutexH);
- _cmRtError(cp->p,rc,"The cmRtSys cond. var. wait failed.");
- return false;
- }
- }
-
- //
- // the cond var was signaled and the mutex is now locked
- //
- ++cp->status.wakeupCnt;
- }
-
- noBlockFl = cp->noBlockEnaFl;
-
- // be sure we are still enabled and the buffer is still ready
- while( cp->runFl && _cmRtBufIsReady(cp) )
- {
- ++cp->status.audioCbCnt;
-
- // make the cmRtSys callback
- _cmRtDspExecCallback( cp );
-
- // update the signal time
- cp->ctx.begSmpIdx += cp->ss.args.dspFramesPerCycle;
- }
-
- }
-
- // unlock the mutex
- cmThreadMutexUnlock(cp->engMutexH);
-
- return true;
- }
-
- void _cmRtGenSignal( cmApAudioPacket_t* outPktArray, unsigned outPktCnt, bool sineFl )
- {
- static unsigned rtPhase = 0;
-
- //fill output with noise
- unsigned i = 0,j =0, k = 0, phs = 0;
- for(; i<outPktCnt; ++i)
- {
- cmApAudioPacket_t* a = outPktArray + i;
- cmApSample_t* dp = (cmApSample_t*)a->audioBytesPtr;
-
- phs = a->audioFramesCnt;
-
- if( sineFl )
- {
- for(j=0; j<a->audioFramesCnt; ++j)
- {
- cmApSample_t v = (cmApSample_t)(0.7 * sin(2*M_PI/44100.0 * rtPhase + j ));
-
- for(k=0; k<a->chCnt; ++k,++dp)
- *dp = v;
- }
- }
- else
- {
- for(j=0; j<a->audioFramesCnt*a->chCnt; ++j,++dp)
- *dp = (cmApSample_t)(rand() - (RAND_MAX/2))/(RAND_MAX/2);
- }
- }
-
- rtPhase += phs;
-
- }
-
- // This is the audio port callback function.
- //
- // _cmRtSysAudioUpdate() assumes that at most two audio device threads
- // (input and output) may call it. cmApBufUpdate() is safe under these conditions
- // since the input and output buffers are updated separately.
- // p->syncInputFl is used to allow either the input or output thread to signal
- // the condition variable. This flag is necessary to prevent both threads from simultaneously
- // attempting to signal the condition variable (which will lock the system).
- //
- // If more than two audio device threads call the function then this function is not safe.
- void _cmRtSysAudioUpdate( cmApAudioPacket_t* inPktArray, unsigned inPktCnt, cmApAudioPacket_t* outPktArray, unsigned outPktCnt )
- {
- _cmRtCfg_t* cp = (_cmRtCfg_t*)(inPktArray!=NULL ? inPktArray[0].userCbPtr : outPktArray[0].userCbPtr);
-
- ++cp->status.updateCnt;
-
- if( cp->runFl )
- {
-
- // transfer incoming/outgoing samples from/to the audio device
- cmApBufUpdate(inPktArray,inPktCnt,outPktArray,outPktCnt);
-
- // generate a test signal
- //_cmRtGenSignal( cmApAudioPacket_t* outPktArray, unsigned outPktCnt, bool sineFl );
- //return;
-
- bool testBufFl = (cp->syncInputFl==true && inPktCnt>0) || (cp->syncInputFl==false && outPktCnt>0);
-
- //printf("%i %i %i %i\n",testBufFl,cp->syncInputFl,inPktCnt,outPktCnt);
-
- // if the input/output buffer contain samples to be processed then signal the condition variable
- // - this will cause the real-time system thread to unblock and the used defined DSP process will be called.
- if( testBufFl && _cmRtBufIsReady(cp) )
- {
- if( cmThreadMutexSignalCondVar(cp->engMutexH) != kOkThRC )
- _cmRtError(cp->p,kMutexErrRtRC,"CmRtSys signal cond. var. failed.");
-
- }
-
- if( cp->noBlockEnaFl )
- {
- cmApBufGetIO(cp->ss.args.inDevIdx, cp->ctx.iChArray, cp->ctx.iChCnt, &cp->ctx.iTimeStamp,
- cp->ss.args.outDevIdx, cp->ctx.oChArray, cp->ctx.oChCnt, &cp->ctx.oTimeStamp );
-
- cmApBufAdvance( cp->ss.args.outDevIdx, kOutApFl );
- cmApBufAdvance( cp->ss.args.inDevIdx, kInApFl );
-
- }
-
- }
-
- }
-
- // Called when MIDI messages arrive from external MIDI ports.
- void _cmRtSysMidiCallback( const cmMidiPacket_t* pktArray, unsigned pktCnt )
- {
- unsigned i;
- for(i=0; i<pktCnt; ++i)
- {
- const cmMidiPacket_t* pkt = pktArray + i;
- _cmRtCfg_t* cp = (_cmRtCfg_t*)(pkt->cbDataPtr);
-
- if( !cp->runFl )
- continue;
-
- cmRtSysH_t asH;
- asH.h = cp->p;
-
- cmRtSysMidi_t m;
- m.hdr.rtSubIdx = cp->ctx.rtSubIdx;
- m.hdr.selId = kMidiMsgArraySelRtId;
- m.devIdx = pkt->devIdx;
- m.portIdx = pkt->portIdx;
- m.msgCnt = pkt->msgCnt;
-
-
- /*
- unsigned selId = kMidiMsgArraySelRtId;
- const void* msgPtrArray[] = { &cp->ctx.rtSubIdx, &selId, &pkt->devIdx, &pkt->portIdx, &pkt->msgCnt, pkt->msgArray };
- unsigned msgByteCntArray[] = { sizeof(cp->ctx.rtSubIdx), sizeof(selId), sizeof(pkt->devIdx), sizeof(pkt->portIdx), sizeof(pkt->msgCnt), pkt->msgCnt*sizeof(cmMidiMsg) };
- unsigned msgSegCnt = sizeof(msgByteCntArray)/sizeof(unsigned);
- */
-
- const void* msgPtrArray[] = { &m, pkt->msgArray };
- unsigned msgByteCntArray[] = { sizeof(m), pkt->msgCnt*sizeof(cmMidiMsg) };
- unsigned msgSegCnt = sizeof(msgByteCntArray)/sizeof(unsigned);
-
- cmRtSysDeliverSegMsg(asH,msgPtrArray,msgByteCntArray,msgSegCnt,cmInvalidId);
- }
-
- }
-
-
-
- cmRtRC_t cmRtSysAllocate( cmRtSysH_t* hp, cmCtx_t* ctx )
- {
- cmRtRC_t rc;
-
- if((rc = cmRtSysFree(hp)) != kOkRtRC )
- return rc;
-
- cmRt_t* p = cmMemAllocZ( cmRt_t, 1 );
-
- cmErrSetup(&p->err,&ctx->rpt,"Real-Time System");
- p->ctx = ctx;
-
- hp->h = p;
-
- return rc;
- }
-
- cmRtRC_t cmRtSysFree( cmRtSysH_t* hp )
- {
- cmRtRC_t rc;
-
- if( hp == NULL || hp->h == NULL )
- return kOkRtRC;
-
- if((rc = cmRtSysFinalize(*hp)) != kOkRtRC )
- return rc;
-
- cmRt_t* p = _cmRtHandleToPtr(*hp);
-
- cmMemFree(p);
-
- hp->h = NULL;
-
- return rc;
- }
-
- cmRtRC_t _cmRtSysEnable( cmRt_t* p, bool enableFl )
- {
- cmRtRC_t rc = kOkRtRC;
-
- unsigned i;
- unsigned n;
- unsigned tickMs = 20;
- unsigned timeOutMs = 10000;
-
- for(i=0; i<p->ssCnt; ++i)
- {
- _cmRtCfg_t* cp = p->ssArray + i;
-
- cmApBufOnPortEnable(cp->ss.args.inDevIdx,enableFl);
- cmApBufOnPortEnable(cp->ss.args.outDevIdx,enableFl);
-
- if( enableFl )
- {
- cp->cmdId = kNoCmdId;
- cmThUIntIncr(&cp->cmdId,kEnableCbCmdId);
-
- for(n=0; n<timeOutMs && cp->cbEnableFl==false; n+=tickMs )
- cmSleepMs(tickMs);
-
- cmThUIntDecr(&cp->cmdId,kEnableCbCmdId);
-
- }
- else
- {
- cp->cmdId = kNoCmdId;
- cmThUIntIncr(&cp->cmdId,kDisableCbCmdId);
-
- // wait for the rt thread to return from a client callbacks
- for(n=0; n<timeOutMs && cp->cbEnableFl; n+=tickMs )
- cmSleepMs(tickMs);
-
- cmThUIntDecr(&cp->cmdId,kDisableCbCmdId);
-
- }
-
- if( n >= timeOutMs )
- rc = cmErrMsg(&p->err,kTimeOutErrRtRC,"RT System %s timed out after %i milliseconds.",enableFl?"enable":"disable",timeOutMs);
-
- }
-
- // enable network sync mode
- if( enableFl)
- for(i=0; i<p->ssCnt; ++i)
- {
- _cmRtCfg_t* cp = p->ssArray + i;
- if( cmRtNetIsValid(cp->netH) )
- if( cmRtNetDoSync(cp->netH) != kOkNetRC )
- rc = cmErrMsg(&p->err,kNetErrRtRC,"Network Mgr. failed on entering sync mode.");
- }
-
- return rc;
- }
-
- cmRtRC_t _cmRtSysFinalize( cmRt_t* p )
- {
- cmRtRC_t rc = kOkRtRC;
- unsigned i;
-
- // mark the real-time system as NOT initialized
- p->initFl = false;
-
- // be sure all audio callbacks are disabled before continuing.
- if((rc = _cmRtSysEnable(p,false)) != kOkRtRC )
- return _cmRtError(p,rc,"real-time system finalize failed because device halting failed.");
-
- // stop the audio devices
- for(i=0; i<p->ssCnt; ++i)
- {
- _cmRtCfg_t* cp = p->ssArray + i;
-
- // stop the input device
- if((rc = cmApDeviceStop( cp->ss.args.inDevIdx )) != kOkRtRC )
- return _cmRtError(p,kAudioDevStopFailRtRC,"The audio input device stop failed.");
-
- // stop the output device
- if((rc = cmApDeviceStop( cp->ss.args.outDevIdx )) != kOkRtRC )
- return _cmRtError(p,kAudioDevStopFailRtRC,"The audio output device stop failed.");
- }
-
-
- for(i=0; i<p->ssCnt; ++i)
- {
- _cmRtCfg_t* cp = p->ssArray + i;
-
- if( cmThreadIsValid( cp->threadH ))
- {
- // inform the thread that it should exit
- cp->runFl = false;
- cp->statusFl = false;
-
-
- // signal the cond var to cause the thread to run
- if((rc = cmThreadMutexSignalCondVar(cp->engMutexH)) != kOkThRC )
- _cmRtError(p,kMutexErrRtRC,"Finalize signal cond. var. failed.");
-
- // wait to take control of the mutex - this will occur when the thread function exits
- if((rc = cmThreadMutexLock(cp->engMutexH)) != kOkThRC )
- _cmRtError(p,kMutexErrRtRC,"Finalize lock failed.");
-
- // unlock the mutex because it is no longer needed and must be unlocked to be destroyed
- if((rc = cmThreadMutexUnlock(cp->engMutexH)) != kOkThRC )
- _cmRtError(p,kMutexErrRtRC,"Finalize unlock failed.");
-
- // destroy the thread
- if((rc = cmThreadDestroy( &cp->threadH )) != kOkThRC )
- _cmRtError(p,kThreadErrRtRC,"Thread destroy failed.");
-
- }
-
- // destroy the mutex
- if( cmThreadMutexIsValid(cp->engMutexH) )
- if((rc = cmThreadMutexDestroy( &cp->engMutexH )) != kOkThRC )
- _cmRtError(p,kMutexErrRtRC,"Mutex destroy failed.");
-
- // release the network mgr
- if( cmRtNetFree(&cp->netH) != kOkNetRC )
- _cmRtError(p,kNetErrRtRC,"Network Mrr. release failed.");
-
- // remove the MIDI callback
- if( cmMpIsInitialized() && cmMpUsesCallback(-1,-1, _cmRtSysMidiCallback, cp) )
- if( cmMpRemoveCallback( -1, -1, _cmRtSysMidiCallback, cp ) != kOkMpRC )
- _cmRtError(p,kMidiSysFailRtRC,"MIDI callback removal failed.");
-
- // destroy the host-to-dsp msg queue
- if( cmTsMp1cIsValid(cp->htdQueueH ) )
- if((rc = cmTsMp1cDestroy( &cp->htdQueueH )) != kOkThRC )
- _cmRtError(p,kTsQueueErrRtRC,"Host-to-DSP msg queue destroy failed.");
-
- // destroy the dsp-to-host msg queue
- if( cmTsMp1cIsValid(p->dthQueH) )
- if((rc = cmTsMp1cDestroy( &p->dthQueH )) != kOkThRC )
- _cmRtError(p,kTsQueueErrRtRC,"DSP-to-Host msg queue destroy failed.");
-
-
- cmMemPtrFree(&cp->ctx.iChArray);
- cmMemPtrFree(&cp->ctx.oChArray);
- cp->ctx.iChCnt = 0;
- cp->ctx.oChCnt = 0;
-
- cmMemPtrFree(&cp->iMeterArray);
- cmMemPtrFree(&cp->oMeterArray);
- cp->status.iMeterCnt = 0;
- cp->status.oMeterCnt = 0;
-
- }
-
-
- cmMemPtrFree(&p->ssArray);
- p->ssCnt = 0;
-
- return rc;
- }
-
- // A given device may be used as an input device exactly once and an
- // output device exactly once. When the input to a given device is used
- // by one sub-system and the output is used by another then both sub-systems
- // must use the same srate,devFramesPerCycle, audioBufCnt and dspFramesPerCycle.
- cmRtRC_t _cmRtSysValidate( cmRt_t* p )
- {
- unsigned i,j,k;
- for(i=0; i<2; ++i)
- {
- // examine input devices - then output devices
- bool inputFl = i==0;
- bool outputFl = !inputFl;
-
- for(j=0; j<p->ssCnt; ++j)
- {
- cmRtSysArgs_t* s0 = &p->ssArray[j].ss.args;
- unsigned devIdx = inputFl ? s0->inDevIdx : s0->outDevIdx;
-
- for(k=0; k<p->ssCnt && devIdx != cmInvalidIdx; ++k)
- if( k != j )
- {
- cmRtSysArgs_t* s1 = &p->ssArray[k].ss.args;
-
- // if the device was used as input or output multple times then signal an error
- if( (inputFl && (s1->inDevIdx == devIdx) && s1->inDevIdx != cmInvalidIdx) || (outputFl && (s1->outDevIdx == devIdx) && s1->outDevIdx != cmInvalidIdx) )
- return cmErrMsg(&p->err,kInvalidArgRtRC,"The device %i was used as an %s by multiple sub-systems.", devIdx, inputFl ? "input" : "output");
-
- // if this device is being used by another subsystem ...
- if( (inputFl && (s1->outDevIdx == devIdx) && s1->inDevIdx != cmInvalidIdx) || (outputFl && (s1->outDevIdx == devIdx) && s1->outDevIdx != cmInvalidIdx ) )
- {
- // ... then some of its buffer spec's must match
- if( s0->srate != s1->srate || s0->audioBufCnt != s1->audioBufCnt || s0->dspFramesPerCycle != s1->dspFramesPerCycle || s0->devFramesPerCycle != s1->devFramesPerCycle )
- return cmErrMsg(&p->err,kInvalidArgRtRC,"The device %i is used by different sub-system with different audio buffer parameters.",devIdx);
- }
- }
- }
- }
-
- return kOkRtRC;
- }
-
- cmRtRC_t cmRtSysBeginCfg( cmRtSysH_t h, cmTsQueueCb_t clientCbFunc, void* clientCbArg, unsigned meterMs, unsigned ssCnt )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- cmRtRC_t rc;
-
- // always finalize before iniitalize
- if((rc = cmRtSysFinalize(h)) != kOkRtRC )
- return rc;
-
- p->ssArray = cmMemAllocZ( _cmRtCfg_t, ssCnt );
- p->ssCnt = ssCnt;
- p->clientCbFunc = clientCbFunc;
- p->clientCbArg = clientCbArg;
-
- return rc;
- }
-
- cmRtRC_t cmRtSysCfg( cmRtSysH_t h, const cmRtSysSubSys_t* ss, unsigned rtSubIdx )
- {
- cmRtRC_t rc;
- unsigned j;
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- assert( rtSubIdx < p->ssCnt);
-
- _cmRtCfg_t* cp = p->ssArray + rtSubIdx;;
-
- cp->p = p;
- cp->ss = *ss; // copy the cfg into the internal real-time system state
- cp->runFl = false;
- cp->statusFl = false;
- cp->ctx.reserved = p;
- cp->ctx.rtSubIdx = rtSubIdx;
- cp->ctx.ss = &cp->ss;
- cp->ctx.begSmpIdx = 0;
- cp->ctx.dspToHostFunc = _cmRtDspToHostMsgCallback;
-
- // validate the input device index
- if( ss->args.inDevIdx != cmInvalidIdx && ss->args.inDevIdx >= cmApDeviceCount() )
- {
- rc = _cmRtError(p,kAudioDevSetupErrRtRC,"The audio input device index %i is invalid.",ss->args.inDevIdx);
- goto errLabel;
- }
-
- // validate the output device index
- if( ss->args.outDevIdx != cmInvalidIdx && ss->args.outDevIdx >= cmApDeviceCount() )
- {
- rc = _cmRtError(p,kAudioDevSetupErrRtRC,"The audio output device index %i is invalid.",ss->args.outDevIdx);
- goto errLabel;
- }
-
- // setup the input device
- if( ss->args.inDevIdx != cmInvalidIdx )
- if((rc = cmApDeviceSetup( ss->args.inDevIdx, ss->args.srate, ss->args.devFramesPerCycle, _cmRtSysAudioUpdate, cp )) != kOkRtRC )
- {
- rc = _cmRtError(p,kAudioDevSetupErrRtRC,"Audio input device setup failed.");
- goto errLabel;
- }
-
- // setup the output device
- if( ss->args.outDevIdx != ss->args.inDevIdx && ss->args.outDevIdx != cmInvalidIdx )
- if((rc = cmApDeviceSetup( ss->args.outDevIdx, ss->args.srate, ss->args.devFramesPerCycle, _cmRtSysAudioUpdate, cp )) != kOkRtRC )
- {
- rc = _cmRtError(p,kAudioDevSetupErrRtRC,"Audio output device setup failed.");
- goto errLabel;
- }
-
- // setup the input device buffer
- if( ss->args.inDevIdx != cmInvalidIdx )
- if((rc = cmApBufSetup( ss->args.inDevIdx, ss->args.srate, ss->args.dspFramesPerCycle, ss->args.audioBufCnt, cmApDeviceChannelCount(ss->args.inDevIdx, true), ss->args.devFramesPerCycle, cmApDeviceChannelCount(ss->args.inDevIdx, false), ss->args.devFramesPerCycle )) != kOkRtRC )
- {
- rc = _cmRtError(p,kAudioBufSetupErrRtRC,"Audio buffer input setup failed.");
- goto errLabel;
- }
-
- cmApBufEnableMeter(ss->args.inDevIdx, -1, kInApFl | kEnableApFl );
- cmApBufEnableMeter(ss->args.outDevIdx,-1, kOutApFl | kEnableApFl );
-
- // setup the input audio buffer ptr array - used to send input audio to the DSP system in _cmRtDspExecCallback()
- if((cp->ctx.iChCnt = cmApDeviceChannelCount(ss->args.inDevIdx, true)) != 0 )
- cp->ctx.iChArray = cmMemAllocZ( cmSample_t*, cp->ctx.iChCnt );
-
- // setup the output device buffer
- if( ss->args.outDevIdx != ss->args.inDevIdx )
- if((rc = cmApBufSetup( ss->args.outDevIdx, ss->args.srate, ss->args.dspFramesPerCycle, ss->args.audioBufCnt, cmApDeviceChannelCount(ss->args.outDevIdx, true), ss->args.devFramesPerCycle, cmApDeviceChannelCount(ss->args.outDevIdx, false), ss->args.devFramesPerCycle )) != kOkRtRC )
- return _cmRtError(p,kAudioBufSetupErrRtRC,"Audio buffer ouput device setup failed.");
-
- // setup the output audio buffer ptr array - used to recv output audio from the DSP system in _cmRtDspExecCallback()
- if((cp->ctx.oChCnt = cmApDeviceChannelCount(ss->args.outDevIdx, false)) != 0 )
- cp->ctx.oChArray = cmMemAllocZ( cmSample_t*, cp->ctx.oChCnt );
-
- // determine the sync source
- cp->syncInputFl = ss->args.syncInputFl;
-
- // if sync'ing to an unavailable device then sync to the available device
- if( ss->args.syncInputFl && cp->ctx.iChCnt == 0 )
- cp->syncInputFl = false;
-
- if( ss->args.syncInputFl==false && cp->ctx.oChCnt == 0 )
- cp->syncInputFl = true;
-
- // setup the status record
- cp->status.hdr.rtSubIdx = cp->ctx.rtSubIdx;
- cp->status.iDevIdx = ss->args.inDevIdx;
- cp->status.oDevIdx = ss->args.outDevIdx;
- cp->status.iMeterCnt = cp->ctx.iChCnt;
- cp->status.oMeterCnt = cp->ctx.oChCnt;
- cp->iMeterArray = cmMemAllocZ( double, cp->status.iMeterCnt );
- cp->oMeterArray = cmMemAllocZ( double, cp->status.oMeterCnt );
- cp->noBlockEnaFl = false;
-
- // create the real-time system thread
- if((rc = cmThreadCreate( &cp->threadH, _cmRtThreadCallback, cp, ss->args.rpt )) != kOkThRC )
- {
- rc = _cmRtError(p,kThreadErrRtRC,"Thread create failed.");
- goto errLabel;
- }
-
- // create the real-time system mutex
- if((rc = cmThreadMutexCreate( &cp->engMutexH, ss->args.rpt )) != kOkThRC )
- {
- rc = _cmRtError(p,kMutexErrRtRC,"Thread mutex create failed.");
- goto errLabel;
- }
-
- // create the host-to-dsp thread safe msg queue
- if((rc = cmTsMp1cCreate( &cp->htdQueueH, ss->args.msgQueueByteCnt, ss->cbFunc, &cp->ctx, ss->args.rpt )) != kOkThRC )
- {
- rc = _cmRtError(p,kTsQueueErrRtRC,"Host-to-DSP msg queue create failed.");
- goto errLabel;
- }
-
- // create the dsp-to-host thread safe msg queue
- if( cmTsMp1cIsValid( p->dthQueH ) == false )
- {
- if((rc = cmTsMp1cCreate( &p->dthQueH, ss->args.msgQueueByteCnt, p->clientCbFunc, p->clientCbArg, ss->args.rpt )) != kOkThRC )
- {
- rc = _cmRtError(p,kTsQueueErrRtRC,"DSP-to-Host msg queue create failed.");
- goto errLabel;
- }
- }
-
- // install an external MIDI port callback handler for incoming MIDI messages
- if( cmMpIsInitialized() )
- if( cmMpInstallCallback( -1, -1, _cmRtSysMidiCallback, cp ) != kOkMpRC )
- {
- rc = _cmRtError(p,kMidiSysFailRtRC,"MIDI system callback installation failed.");
- goto errLabel;
- }
-
- // setup the sub-system status notification
- cp->statusUpdateSmpCnt = floor(cmApBufMeterMs() * cp->ss.args.srate / 1000.0 );
- cp->statusUpdateSmpIdx = 0;
-
-
- // allocate the network mgr
- if( cmRtNetAlloc(p->ctx,&cp->netH, _cmRtSysNetRecv, cp ) != kOkNetRC )
- {
- rc = _cmRtError(p,kNetErrRtRC,"Network allocation failed.");
- goto errLabel;
- }
-
- if( cmRtNetInitialize( cp->netH, ss->bcastAddr, ss->localNodeLabel, ss->localIpAddr, ss->localIpPort) != kOkNetRC )
- {
- rc = _cmRtError(p,kNetErrRtRC,"Network node initialization failed on label:%s addr:%s port:%i.",cmStringNullGuard(ss->localNodeLabel),cmStringNullGuard(ss->localIpAddr),ss->localIpPort);
- goto errLabel;
- }
-
-
- // register the local endpoints
- for(j=0; j<ss->endptCnt; ++j)
- {
- cmRtSysNetEndpt_t* ep = ss->endptArray + j;
- if( cmRtNetRegisterEndPoint( cp->netH, cp->ctx.rtSubIdx, ep->label, ep->id ) != kOkNetRC )
- {
- rc = _cmRtError(p,kNetErrRtRC,"Network end point allocation failed on label:%s id:%i.",cmStringNullGuard(ep->label),ep->id);
- goto errLabel;
- }
- }
-
- errLabel:
- if( rc != kOkRtRC )
- _cmRtSysFinalize(p);
-
- return rc;
- }
-
- cmRtRC_t cmRtSysEndCfg( cmRtSysH_t h )
- {
- cmRtRC_t rc;
- cmRt_t* p = _cmRtHandleToPtr(h);
- unsigned i;
-
- if((rc = _cmRtSysValidate(p)) != kOkRtRC )
- goto errLabel;
-
-
- for(i=0; i<p->ssCnt; ++i)
- {
- _cmRtCfg_t* cp = p->ssArray + i;
-
- cp->runFl = true;
-
- // start the real-time system thread
- if( cmThreadPause( cp->threadH, 0 ) != kOkThRC )
- {
- rc = _cmRtError(p,kThreadErrRtRC,"Thread start failed.");
- goto errLabel;
- }
-
- // start the input device
- if((rc = cmApDeviceStart( cp->ss.args.inDevIdx )) != kOkRtRC )
- return _cmRtError(p,kAudioDevStartFailRtRC,"The audio input device start failed.");
-
- // start the output device
- if( cmApDeviceStart( cp->ss.args.outDevIdx ) != kOkRtRC )
- return _cmRtError(p,kAudioDevStartFailRtRC,"The audio ouput device start failed.");
- }
-
- p->initFl = true;
-
- errLabel:
- if( rc != kOkRtRC )
- _cmRtSysFinalize(p);
-
- return rc;
- }
-
-
- cmRtRC_t cmRtSysFinalize(cmRtSysH_t h )
- {
- cmRtRC_t rc = kOkRtRC;
-
- if( cmRtSysHandleIsValid(h) == false )
- return rc;
-
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- rc = _cmRtSysFinalize(p);
-
- h.h = NULL;
-
- return rc;
- }
-
-
- bool cmRtSysIsInitialized( cmRtSysH_t h )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- return p->initFl;
- }
-
- cmRtRC_t _cmRtSysVerifyInit( cmRt_t* p, bool errFl )
- {
- if( p->initFl == false )
- {
-
- // if the last msg generated was also a not init msg then don't
- // generate another message - just return the error
- if( errFl )
- if( cmErrLastRC(&p->err) != kNotInitRtRC )
- cmErrMsg(&p->err,kNotInitRtRC,"The real-time system is not initialized.");
-
- return kNotInitRtRC;
- }
-
- return kOkRtRC;
- }
-
-
- bool cmRtSysIsEnabled( cmRtSysH_t h )
- {
- if( cmRtSysIsInitialized(h) == false )
- return false;
-
- cmRt_t* p = _cmRtHandleToPtr(h);
- unsigned i;
- for(i=0; i<p->ssCnt; ++i)
- if( p->ssArray[i].cbEnableFl )
- return true;
-
- return false;
- }
-
-
- cmRtRC_t cmRtSysEnable( cmRtSysH_t h, bool enableFl )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- return _cmRtSysEnable(p,enableFl);
- }
-
- cmRtRC_t cmRtSysDeliverSegMsg( cmRtSysH_t h, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt, unsigned srcNetNodeId )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- cmRtRC_t rc;
-
- // the system must be initialized to use this function
- if((rc = _cmRtSysVerifyInit(p,true)) != kOkRtRC )
- return rc;
-
- if( msgSegCnt == 0 )
- return kOkRtRC;
-
- // BUG BUG BUG - there is no reason that both the rtSubIdx and the selId must
- // be in the first segment but it would be nice.
- assert( msgByteCntArray[0] >= 2*sizeof(unsigned) || (msgSegCnt>1 && msgByteCntArray[0]==sizeof(unsigned) && msgByteCntArray[1]>=sizeof(unsigned)) );
-
- // The audio sub-system index is always the first field of the msg
- // and the msg selector id is always the second field
-
- unsigned* array = (unsigned*)msgDataPtrArray[0];
- unsigned rtSubIdx = array[0];
- unsigned selId = array[1];
-
- if( selId == kUiMstrSelRtId )
- return _cmRtHandleNonSubSysMsg( p, msgDataPtrArray, msgByteCntArray, msgSegCnt );
-
- /*
- if( selId == kNetSyncSelRtId )
- {
- assert( msgSegCnt==1);
- assert( rtSubIdx < p->ssCnt );
- p->ssArray[rtSubIdx].ctx.srcNetNodeId = srcNetNodeId;
- p->ssArray[rtSubIdx].ss.cbFunc(&p->ssArray[rtSubIdx].ctx,msgByteCntArray[0],msgDataPtrArray[0]);
- return kOkRtRC;
- }
- */
-
- // if rtSubIdx == kInvalidIdx then send the msg to all sub-systems
- // otherwise send it to the specified sub-system.
-
- unsigned i = 0;
- unsigned n = 1;
-
- if( rtSubIdx == cmInvalidIdx )
- n = p->ssCnt;
-
- for(; i<n; ++i)
- {
- unsigned j = rtSubIdx==cmInvalidIdx ? i : rtSubIdx;
-
- if((rc = _cmRtEnqueueMsg(p,p->ssArray[j].htdQueueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"Host-to-DSP")) != kOkRtRC )
- break;
- }
-
- return rc;
- }
-
- cmRtRC_t cmRtSysDeliverMsg( cmRtSysH_t h, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
- {
- const void* msgDataPtrArray[] = { msgPtr };
- unsigned msgByteCntArray[] = { msgByteCnt };
- return cmRtSysDeliverSegMsg(h,msgDataPtrArray,msgByteCntArray,1,srcNetNodeId);
- }
-
- cmRtRC_t cmRtSysDeliverIdMsg( cmRtSysH_t h, unsigned rtSubIdx, unsigned id, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
- {
- cmRtRC_t rc;
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- // the system must be initialized to use this function
- if((rc = _cmRtSysVerifyInit(p,true)) != kOkRtRC )
- return rc;
-
- const void* msgDataPtrArray[] = { &rtSubIdx, &id, msgPtr };
- unsigned msgByteCntArray[] = { sizeof(rtSubIdx), sizeof(id), msgByteCnt };
- return cmRtSysDeliverSegMsg(h,msgDataPtrArray,msgByteCntArray,3,srcNetNodeId);
- }
-
- unsigned cmRtSysIsMsgWaiting( cmRtSysH_t h )
- {
- cmRtRC_t rc;
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- // the system must be initialized to use this function
- if((rc = _cmRtSysVerifyInit(p,false)) != kOkRtRC )
- return 0;
-
- unsigned n = 0;
- unsigned retByteCnt;
-
- for(n=0; n < p->ssCnt; ++n )
- {
- if( (retByteCnt = cmTsMp1cDequeueMsgByteCount(p->dthQueH)) > 0 )
- return retByteCnt;
-
- p->waitRtSubIdx = (p->waitRtSubIdx + 1) % p->ssCnt;
- }
-
- return 0;
- }
-
- cmRtRC_t cmRtSysReceiveMsg( cmRtSysH_t h, void* msgDataPtr, unsigned msgByteCnt )
- {
- cmRtRC_t rc;
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- // the system must be initialized to use this function
- if((rc = _cmRtSysVerifyInit(p,true)) != kOkRtRC )
- return rc;
-
- //switch( cmTsMp1cDequeueMsg(p->ssArray[p->waitRtSubIdx].dthQueueH,msgDataPtr,msgByteCnt) )
- switch( cmTsMp1cDequeueMsg(p->dthQueH,msgDataPtr,msgByteCnt) )
- {
- case kOkThRC:
- p->waitRtSubIdx = (p->waitRtSubIdx + 1) % p->ssCnt;
- return kOkRtRC;
-
- case kBufTooSmallThRC:
- return kBufTooSmallRtRC;
-
- case kBufEmptyThRC:
- return kNoMsgWaitingRtRC;
- }
-
- return _cmRtError(p,kTsQueueErrRtRC,"A deque operation failed on the DSP-to-Host message queue.");
- }
-
-
- void cmRtSysStatus( cmRtSysH_t h, unsigned rtSubIdx, cmRtSysStatus_t* statusPtr )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- // the system must be initialized to use this function
- if( _cmRtSysVerifyInit(p,true) != kOkRtRC )
- return;
-
- if( rtSubIdx < p->ssCnt )
- *statusPtr = p->ssArray[rtSubIdx].status;
- }
-
- void cmRtSysStatusNotifyEnable( cmRtSysH_t h, unsigned rtSubIdx, bool enableFl )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- // the system must be initialized to use this function
- if( _cmRtSysVerifyInit(p,true) != kOkRtRC )
- return;
-
- unsigned i = rtSubIdx == cmInvalidIdx ? 0 : rtSubIdx;
- unsigned n = rtSubIdx == cmInvalidIdx ? p->ssCnt : rtSubIdx+1;
- for(; i<n; ++i)
- p->ssArray[i].statusFl = enableFl;
- }
-
- bool cmRtSysHandleIsValid( cmRtSysH_t h )
- { return h.h != NULL; }
-
- cmRtSysCtx_t* cmRtSysContext( cmRtSysH_t h, unsigned rtSubIdx )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- if( _cmRtSysVerifyInit(p,true) != kOkRtRC )
- return NULL;
-
- if( rtSubIdx >= p->ssCnt )
- return NULL;
-
- return &p->ssArray[rtSubIdx].ctx;
- }
-
- cmRtRC_t cmRtSysEnableNoBlockMode( cmRtSysH_t h, unsigned rtSubIdx, bool enaFl, unsigned noBlockSleepMs )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- cmRtRC_t rc = kOkRtRC;
-
- if((rc = _cmRtSysVerifyInit(p,true)) != kOkRtRC )
- return rc;
-
- if( rtSubIdx >= p->ssCnt )
- return cmErrMsg(&p->err,kInvalidArgRtRC,"Invalid 'rtSubIdx'. Enable non-block mode failed.");
-
-
- p->ssArray[rtSubIdx].noBlockSleepMs = noBlockSleepMs;
- p->ssArray[rtSubIdx].noBlockEnaFl = enaFl;
-
- return kOkRtRC;
- }
-
-
- unsigned cmRtSysSubSystemCount( cmRtSysH_t h )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- if( _cmRtSysVerifyInit(p,true) != kOkRtRC )
- return 0;
-
- return p->ssCnt;
- }
-
-
- bool cmRtSysNetIsInitialized( cmRtSysH_t h )
- {
- cmRt_t* p = _cmRtHandleToPtr(h);
- unsigned i = 0;
- for(; i<p->ssCnt; ++i)
- if( cmRtNetIsInitialized(p->ssArray[i].netH) )
- return true;
- return false;
- }
-
-
- cmRtRC_t cmRtSysNetDoSync( cmRtSysH_t h )
- {
- cmRtRC_t rc = kOkRtRC;
- cmRt_t* p = _cmRtHandleToPtr(h);
- unsigned i = 0;
- for(; i<p->ssCnt; ++i)
- if( cmRtNetIsInitialized(p->ssArray[i].netH) )
- cmRtNetDoSync(p->ssArray[i].netH);
-
- return rc;
- }
-
- cmRtRC_t cmRtSysNetReport( cmRtSysH_t h )
- {
- cmRtRC_t rc = kOkRtRC;
- cmRt_t* p = _cmRtHandleToPtr(h);
- unsigned i = 0;
- for(; i<p->ssCnt; ++i)
- {
- cmRptPrintf(p->err.rpt,"Sub-system:%i\n",i);
- if( cmRtNetIsValid(p->ssArray[i].netH))
- cmRtNetDoSync(p->ssArray[i].netH);
- }
- return rc;
-
- }
-
- cmRtRC_t cmRtSysNetGetHandle( cmRtSysH_t h, unsigned rtSubIdx, cmRtNetH_t* hp )
- {
- cmRtRC_t rc = kOkRtRC;
- cmRt_t* p = _cmRtHandleToPtr(h);
-
- assert( rtSubIdx < p->ssCnt );
-
- if( rtSubIdx < p->ssCnt )
- {
- *hp = p->ssArray[rtSubIdx].netH;
- return rc;
- }
-
- return cmErrMsg(&p->err,kInvalidArgRtRC,"The rtSubIdx %i is out of range %i.",rtSubIdx,p->ssCnt);
- }
-
-
-
- //===========================================================================================================================
- //
- // cmRtTest()
- //
-
- /// [cmRtSysTest]
-
- typedef struct
- {
- double hz; // current synth frq
- long phs; // current synth phase
- double srate; // audio sample rate
- unsigned cbCnt; // DSP cycle count
- bool synthFl; // true=synth false=pass through
- } _cmRtTestCbRecd;
-
- typedef struct
- {
- unsigned rtSubIdx; // rtSubIdx must always be the first field in the msg
- unsigned id; // 0 = set DSP Hz, 1 = report cbCount to host
- double hz;
- unsigned uint;
- } _cmRtTestMsg;
-
-
- long _cmRtSynthSine( _cmRtTestCbRecd* r, cmApSample_t* p, unsigned chCnt, unsigned frmCnt )
- {
- long ph = 0;
- unsigned i;
-
-
- for(i=0; i<chCnt; ++i)
- {
- unsigned j;
- cmApSample_t* op = p + i;
-
- ph = r->phs;
- for(j=0; j<frmCnt; j++, op+=chCnt, ph++)
- *op = (cmApSample_t)(0.9 * sin( 2.0 * M_PI * r->hz * ph / r->srate ));
- }
-
- return ph;
- }
-
- unsigned _cmRtTestChIdx = 0;
-
- cmRC_t _cmRtTestCb( void* cbPtr, unsigned msgByteCnt, const void* msgDataPtr )
- {
- cmRC_t rc = cmOkRC;
- cmRtSysCtx_t* ctx = (cmRtSysCtx_t*)cbPtr;
- cmRtSysSubSys_t* ss = ctx->ss;
- _cmRtTestCbRecd* r = (_cmRtTestCbRecd*)ss->cbDataPtr;
-
- // update the calback counter
- ++r->cbCnt;
-
- // if this is an audio update request
- if( msgByteCnt == 0 )
- {
- unsigned i;
- if( r->synthFl )
- {
- long phs = 0;
- if(0)
- {
- for(i=0; i<ctx->oChCnt; ++i)
- if( ctx->oChArray[i] != NULL )
- phs = _cmRtSynthSine(r, ctx->oChArray[i], 1, ss->args.dspFramesPerCycle );
- }
- else
- {
- if( _cmRtTestChIdx < ctx->oChCnt )
- phs = _cmRtSynthSine(r, ctx->oChArray[_cmRtTestChIdx], 1, ss->args.dspFramesPerCycle );
- }
-
- r->phs = phs;
- }
- else
- {
- // BUG BUG BUG - this assumes that the input and output channels are the same.
- unsigned chCnt = cmMin(ctx->oChCnt,ctx->iChCnt);
- for(i=0; i<chCnt; ++i)
- memcpy(ctx->oChArray[i],ctx->iChArray[i],sizeof(cmSample_t)*ss->args.dspFramesPerCycle);
- }
-
- }
- else // ... otherwise it is a msg for the DSP process from the host
- {
- _cmRtTestMsg* msg = (_cmRtTestMsg*)msgDataPtr;
-
- msg->rtSubIdx = ctx->rtSubIdx;
-
- switch(msg->id)
- {
- case 0:
- r->hz = msg->hz;
- break;
-
- case 1:
- msg->uint = r->cbCnt;
- msgByteCnt = sizeof(_cmRtTestMsg);
- rc = ctx->dspToHostFunc(ctx,(const void **)&msg,&msgByteCnt,1);
- break;
- }
-
- }
-
- return rc;
- }
-
- // print the usage message for cmAudioPortTest.c
- void _cmRtPrintUsage( cmRpt_t* rpt )
- {
- char msg[] =
- "cmRtSysTest() command switches:\n"
- "-r <srate> -c <chcnt> -b <bufcnt> -f <frmcnt> -i <idevidx> -o <odevidx> -m <msgqsize> -d <dspsize> -t -p -h \n"
- "\n"
- "-r <srate> = sample rate (48000)\n"
- "-c <chcnt> = audio channels (2)\n"
- "-b <bufcnt> = count of buffers (3)\n"
- "-f <frmcnt> = count of samples per buffer (512)\n"
- "-i <idevidx> = input device index (0)\n"
- "-o <odevidx> = output device index (2)\n"
- "-m <msgqsize> = message queue byte count (1024)\n"
- "-d <dspsize> = samples per DSP frame (64)\n"
- "-s = true: sync to input port false: sync to output port\n"
- "-t = copy input to output otherwise synthesize a 1000 Hz sine (false)\n"
- "-p = report but don't start audio devices\n"
- "-h = print this usage message\n";
-
- cmRptPrintf(rpt,"%s",msg);
- }
-
- // Get a command line option.
- int _cmRtGetOpt( int argc, const char* argv[], const char* label, int defaultVal, bool boolFl )
- {
- int i = 0;
- for(; i<argc; ++i)
- if( strcmp(label,argv[i]) == 0 )
- {
- if(boolFl)
- return 1;
-
- if( i == (argc-1) )
- return defaultVal;
-
- return atoi(argv[i+1]);
- }
-
- return defaultVal;
- }
-
- bool _cmRtGetBoolOpt( int argc, const char* argv[], const char* label, bool defaultVal )
- { return _cmRtGetOpt(argc,argv,label,defaultVal?1:0,true)!=0; }
-
- int _cmRtGetIntOpt( int argc, const char* argv[], const char* label, int defaultVal )
- { return _cmRtGetOpt(argc,argv,label,defaultVal,false); }
-
-
- void cmRtSysTest( cmCtx_t* ctx, int argc, const char* argv[] )
- {
- cmRtSysSubSys_t ss;
- cmRtSysH_t h = cmRtSysNullHandle;
- cmRtSysStatus_t status;
- _cmRtTestCbRecd cbRecd = {1000.0,0,48000.0,0};
- cmRpt_t* rpt = &ctx->rpt;
- memset(&status,0,sizeof(status));
-
-
- unsigned meterMs = 50;
- unsigned ssCnt = 1;
- unsigned rtSubIdx = 0;
-
- if(_cmRtGetBoolOpt(argc,argv,"-h",false))
- _cmRtPrintUsage(rpt);
-
- cbRecd.srate = _cmRtGetIntOpt(argc,argv,"-r",48000);
- cbRecd.synthFl = _cmRtGetBoolOpt(argc,argv,"-t",false)==false;
-
- ss.args.rpt = rpt;
- ss.args.inDevIdx = _cmRtGetIntOpt( argc,argv,"-i",0);
- ss.args.outDevIdx = _cmRtGetIntOpt( argc,argv,"-o",2);
- ss.args.syncInputFl = _cmRtGetBoolOpt(argc,argv,"-s",true);
- ss.args.msgQueueByteCnt = _cmRtGetIntOpt( argc,argv,"-m",8192);
- ss.args.devFramesPerCycle = _cmRtGetIntOpt( argc,argv,"-f",512);
- ss.args.dspFramesPerCycle = _cmRtGetIntOpt( argc,argv,"-d",64);;
- ss.args.audioBufCnt = _cmRtGetIntOpt( argc,argv,"-b",3);
- ss.args.srate = cbRecd.srate;
- ss.cbFunc = _cmRtTestCb; // set the DSP entry function
- ss.cbDataPtr = &cbRecd; // set the DSP function argument record
-
- cmRptPrintf(rpt,"in:%i out:%i syncFl:%i que:%i fpc:%i dsp:%i bufs:%i sr:%f\n",ss.args.inDevIdx,ss.args.outDevIdx,ss.args.syncInputFl,
- ss.args.msgQueueByteCnt,ss.args.devFramesPerCycle,ss.args.dspFramesPerCycle,ss.args.audioBufCnt,ss.args.srate);
-
- if( cmApNrtAllocate(rpt) != kOkApRC )
- goto errLabel;
-
- if( cmApFileAllocate(rpt) != kOkApRC )
- goto errLabel;
-
- // initialize the audio device system
- if( cmApInitialize(rpt) != kOkApRC )
- goto errLabel;
-
- cmApReport(rpt);
-
- // initialize the audio buffer
- if( cmApBufInitialize( cmApDeviceCount(), meterMs ) != kOkApRC )
- goto errLabel;
-
- // initialize the real-time system
- if( cmRtSysAllocate(&h,ctx) != kOkRtRC )
- goto errLabel;
-
- if( cmRtSysBeginCfg(h,NULL,NULL,meterMs,ssCnt) != kOkRtRC )
- goto errLabel;
-
- if( cmRtSysCfg(h,&ss,rtSubIdx) != kOkRtRC )
- goto errLabel;
-
- if( cmRtSysEndCfg(h) != kOkRtRC )
- goto errLabel;
-
- // start the real-time system
- cmRtSysEnable(h,true);
-
- char c = 0;
- printf("q=quit a-g=note n=ch r=rqst s=status\n");
-
- // simulate a host event loop
- while(c != 'q')
- {
- _cmRtTestMsg msg = {0,0,0,0};
- bool fl = true;
-
- // wait here for a key press
- c =(char)fgetc(stdin);
- fflush(stdin);
-
-
- switch(c)
- {
- case 'c': msg.hz = cmMidiToHz(60); break;
- case 'd': msg.hz = cmMidiToHz(62); break;
- case 'e': msg.hz = cmMidiToHz(64); break;
- case 'f': msg.hz = cmMidiToHz(65); break;
- case 'g': msg.hz = cmMidiToHz(67); break;
- case 'a': msg.hz = cmMidiToHz(69); break;
- case 'b': msg.hz = cmMidiToHz(71); break;
-
- case 'r': msg.id = 1; break; // request DSP process to send a callback count
-
- case 'n': ++_cmRtTestChIdx; printf("ch:%i\n",_cmRtTestChIdx); break;
-
- case 's':
- // report the real-time system status
- cmRtSysStatus(h,0,&status);
- printf("phs:%li cb count:%i (upd:%i wake:%i acb:%i msgs:%i)\n",cbRecd.phs, cbRecd.cbCnt, status.updateCnt, status.wakeupCnt, status.audioCbCnt, status.msgCbCnt);
- //printf("%f \n",status.oMeterArray[0]);
- fl = false;
- break;
-
- default:
- fl=false;
-
- }
-
- if( fl )
- {
- // transmit a command to the DSP process
- cmRtSysDeliverMsg(h,&msg, sizeof(msg), cmInvalidId);
- }
-
-
- // check if messages are waiting to be delivered from the DSP process
- unsigned msgByteCnt;
- if((msgByteCnt = cmRtSysIsMsgWaiting(h)) > 0 )
- {
- char buf[ msgByteCnt ];
-
- // rcv a msg from the DSP process
- if( cmRtSysReceiveMsg(h,buf,msgByteCnt) == kOkRtRC )
- {
- _cmRtTestMsg* msg = (_cmRtTestMsg*)buf;
- switch(msg->id)
- {
- case 1:
- printf("RCV: Callback count:%i\n",msg->uint);
- break;
- }
-
- }
- }
-
- // report the audio buffer status
- //cmApBufReport(ss.args.rpt);
- }
-
- // stop the real-time system
- cmRtSysEnable(h,false);
-
-
- goto exitLabel;
-
- errLabel:
- printf("REAL-TIME SYSTEM TEST ERROR\n");
-
- exitLabel:
-
- cmRtSysFree(&h);
- cmApFinalize();
- cmApFileFree();
- cmApNrtFree();
- cmApBufFinalize();
-
- }
-
- /// [cmRtSysTest]
|