libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmAudioSys.c 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmTime.h"
  10. #include "cmAudioPort.h"
  11. #include "cmAudioNrtDev.h"
  12. #include "cmAudioPortFile.h"
  13. #include "cmApBuf.h"
  14. #include "cmJson.h"
  15. #include "cmThread.h"
  16. #include "cmUdpPort.h"
  17. #include "cmUdpNet.h"
  18. #include "cmSerialPort.h"
  19. #include "cmAudioSysMsg.h"
  20. #include "cmAudioSys.h"
  21. #include "cmMidi.h"
  22. #include "cmMidiPort.h"
  23. #include "cmMath.h"
  24. cmAudioSysH_t cmAudioSysNullHandle = { NULL };
  25. struct cmAs_str;
  26. typedef struct
  27. {
  28. struct cmAs_str* p; // pointer to the audio system instance which owns this sub-system
  29. cmAudioSysSubSys_t ss; // sub-system configuration record
  30. cmAudioSysCtx_t ctx; // DSP context
  31. cmAudioSysStatus_t status; // current runtime status of this sub-system
  32. cmThreadH_t threadH; // audio system thread
  33. cmTsMp1cH_t htdQueueH; // host-to-dsp thread safe msg queue
  34. cmThreadMutexH_t engMutexH; // thread mutex and condition variable
  35. cmUdpNetH_t netH;
  36. cmSeH_t serialPortH;
  37. bool enableFl; // application controlled pause flag
  38. bool runFl; // false during finalization otherwise true
  39. bool statusFl; // true if regular status notifications should be sent
  40. unsigned audCbLock; //
  41. bool syncInputFl;
  42. double* iMeterArray; //
  43. double* oMeterArray; //
  44. unsigned statusUpdateSmpCnt; // transmit a state update msg every statusUpdateSmpCnt samples
  45. unsigned statusUpdateSmpIdx; // state update phase
  46. } _cmAsCfg_t;
  47. typedef struct cmAs_str
  48. {
  49. cmErr_t err;
  50. _cmAsCfg_t* ssArray;
  51. unsigned ssCnt;
  52. unsigned waitAsSubIdx; // index of the next sub-system to try with cmAudioSysIsMsgWaiting().
  53. cmTsMp1cH_t dthQueH;
  54. bool initFl; // true if the audio system is initialized
  55. } cmAs_t;
  56. cmAs_t* _cmAsHandleToPtr( cmAudioSysH_t h )
  57. {
  58. cmAs_t* p = (cmAs_t*)h.h;
  59. assert(p != NULL);
  60. return p;
  61. }
  62. cmAsRC_t _cmAsError( cmAs_t* p, cmAsRC_t rc, const char* fmt, ... )
  63. {
  64. va_list vl;
  65. va_start(vl,fmt);
  66. cmErrVMsg(&p->err,rc,fmt,vl);
  67. va_end(vl);
  68. return rc;
  69. }
  70. // Wrapper function to put msgs into thread safe queues and handle related errors.
  71. cmAsRC_t _cmAsEnqueueMsg( cmAs_t* p, cmTsMp1cH_t qH, const void* msgDataPtrArray[], unsigned msgCntArray[], unsigned segCnt, const char* queueLabel )
  72. {
  73. cmAsRC_t rc = kOkAsRC;
  74. switch( cmTsMp1cEnqueueSegMsg(qH, msgDataPtrArray, msgCntArray, segCnt) )
  75. {
  76. case kOkThRC:
  77. break;
  78. case kBufFullThRC:
  79. {
  80. unsigned i;
  81. unsigned byteCnt = 0;
  82. for(i=0; i<segCnt; ++i)
  83. byteCnt += msgCntArray[i];
  84. rc = _cmAsError(p,kMsgEnqueueFailAsRC,"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));
  85. }
  86. break;
  87. default:
  88. rc = _cmAsError(p,kMsgEnqueueFailAsRC,"A %s msg. enqueue failed.",queueLabel);
  89. }
  90. return rc;
  91. }
  92. // This is the function pointed to by ctx->dspToHostFunc.
  93. // It is called by the DSP proces to pass msgs to the host.
  94. // therefore it is always called from inside of _cmAsDspExecCallback().
  95. cmAsRC_t _cmAsDspToHostMsgCallback(struct cmAudioSysCtx_str* ctx, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt)
  96. {
  97. cmAs_t* p = (cmAs_t*)ctx->reserved;
  98. assert( ctx->asSubIdx < p->ssCnt );
  99. //return _cmAsEnqueueMsg(p,p->ssArray[ctx->asSubIdx].dthQueueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"DSP-to-Host");
  100. return _cmAsEnqueueMsg(p,p->dthQueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"DSP-to-Host");
  101. }
  102. cmAsRC_t _cmAsHostInitNotify( cmAs_t* p )
  103. {
  104. cmAsRC_t rc = kOkAsRC;
  105. unsigned i;
  106. for(i=0; i<p->ssCnt; ++i)
  107. {
  108. cmAudioSysSsInitMsg_t m;
  109. _cmAsCfg_t* cp = p->ssArray + i;
  110. const char* inDevLabel = cp->ss.args.inDevIdx == cmInvalidIdx ? "" : cmApDeviceLabel( cp->ss.args.inDevIdx );
  111. const char* outDevLabel = cp->ss.args.outDevIdx == cmInvalidIdx ? "" : cmApDeviceLabel( cp->ss.args.outDevIdx );
  112. m.asSubIdx = i;
  113. m.selId = kSsInitSelAsId;
  114. m.asSubCnt = p->ssCnt;
  115. m.inDevIdx = cp->ss.args.inDevIdx;
  116. m.outDevIdx = cp->ss.args.outDevIdx;
  117. m.dspFramesPerCycle = cp->ss.args.dspFramesPerCycle;
  118. m.srate = cp->ss.args.srate;
  119. m.inChCnt = cp->status.iMeterCnt;
  120. m.outChCnt = cp->status.oMeterCnt;
  121. unsigned segCnt = 3;
  122. const void* msgDataPtrArray[] = { &m, inDevLabel, outDevLabel };
  123. unsigned msgByteCntArray[] = { sizeof(m), strlen(cmStringNullGuard(inDevLabel))+1, strlen(cmStringNullGuard(outDevLabel))+1 };
  124. assert( sizeof(msgDataPtrArray)/sizeof(void*) == segCnt);
  125. assert( sizeof(msgByteCntArray)/sizeof(unsigned) == segCnt);
  126. if((rc = _cmAsDspToHostMsgCallback(&cp->ctx, msgDataPtrArray, msgByteCntArray, segCnt)) != kOkAsRC )
  127. return rc;
  128. }
  129. return rc;
  130. }
  131. cmAsRC_t _cmAsParseNonSubSysMsg( cmAs_t* p, const void* msg, unsigned msgByteCnt )
  132. {
  133. cmAsRC_t rc = kOkAsRC;
  134. cmAudioSysMstr_t* h = (cmAudioSysMstr_t*)msg;
  135. unsigned devIdx = cmAudioSysUiInstIdToDevIndex(h->instId);
  136. unsigned chIdx = cmAudioSysUiInstIdToChIndex(h->instId);
  137. unsigned inFl = cmAudioSysUiInstIdToInFlag(h->instId);
  138. unsigned ctlId = cmAudioSysUiInstIdToCtlId(h->instId);
  139. // if the valuu associated with this msg is a mtx then set
  140. // its mtx data area pointer to just after the msg header.
  141. //if( cmDsvIsMtx(&h->value) )
  142. // h->value.u.m.u.vp = ((char*)msg) + sizeof(cmDspUiHdr_t);
  143. unsigned flags = inFl ? kInApFl : kOutApFl;
  144. switch( ctlId )
  145. {
  146. case kSliderUiAsId: // slider
  147. cmApBufSetGain(devIdx,chIdx, flags, h->value);
  148. break;
  149. case kMeterUiAsId: // meter
  150. break;
  151. case kMuteUiAsId: // mute
  152. flags += h->value == 0 ? kEnableApFl : 0;
  153. cmApBufEnableChannel(devIdx,chIdx,flags);
  154. break;
  155. case kToneUiAsId: // tone
  156. flags += h->value > 0 ? kEnableApFl : 0;
  157. cmApBufEnableTone(devIdx,chIdx,flags);
  158. break;
  159. case kPassUiAsId: // pass
  160. flags += h->value > 0 ? kEnableApFl : 0;
  161. cmApBufEnablePass(devIdx,chIdx,flags);
  162. break;
  163. default:
  164. { assert(0); }
  165. }
  166. return rc;
  167. }
  168. // Process a UI msg sent from the host to the audio system
  169. cmAsRC_t _cmAsHandleNonSubSysMsg( cmAs_t* p, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt )
  170. {
  171. cmAsRC_t rc = kOkAsRC;
  172. // if the message is contained in a single segment it can be dispatched immediately ...
  173. if( msgSegCnt == 1 )
  174. rc = _cmAsParseNonSubSysMsg(p,msgDataPtrArray[0],msgByteCntArray[0]);
  175. else
  176. {
  177. // ... otherwise deserialize the message into contiguous memory ....
  178. unsigned byteCnt = 0;
  179. unsigned i;
  180. for(i=0; i<msgSegCnt; ++i)
  181. byteCnt += msgByteCntArray[i];
  182. char buf[ byteCnt ];
  183. char* b = buf;
  184. for(i=0; i<msgSegCnt; ++i)
  185. {
  186. memcpy(b, msgDataPtrArray[i], msgByteCntArray[i] );
  187. b += msgByteCntArray[i];
  188. }
  189. // ... and then dispatch it
  190. rc = _cmAsParseNonSubSysMsg(p,buf,byteCnt);
  191. }
  192. return rc;
  193. }
  194. cmAsRC_t _cmAsSendStateStatusToHost( _cmAsCfg_t* cp )
  195. {
  196. cmAsRC_t rc = kOkAsRC;
  197. unsigned hdr[] = { cp->ctx.asSubIdx, kStatusSelAsId };
  198. cmApBufGetStatus( cp->ss.args.inDevIdx, kInApFl, cp->iMeterArray, cp->status.iMeterCnt, &cp->status.overflowCnt );
  199. cmApBufGetStatus( cp->ss.args.outDevIdx, kOutApFl, cp->oMeterArray, cp->status.oMeterCnt, &cp->status.underflowCnt );
  200. unsigned iMeterByteCnt = sizeof(cp->iMeterArray[0]) * cp->status.iMeterCnt;
  201. unsigned oMeterByteCnt = sizeof(cp->oMeterArray[0]) * cp->status.oMeterCnt;
  202. const void* msgDataPtrArray[] = { &hdr, &cp->status, cp->iMeterArray, cp->oMeterArray };
  203. unsigned msgByteCntArray[] = { sizeof(hdr), sizeof(cp->status), iMeterByteCnt, oMeterByteCnt };
  204. unsigned segCnt = sizeof(msgByteCntArray)/sizeof(unsigned);
  205. _cmAsDspToHostMsgCallback(&cp->ctx,msgDataPtrArray,msgByteCntArray, segCnt );
  206. return rc;
  207. }
  208. // The DSP execution callback happens through this function.
  209. // This function is only called from inside _cmAsThreadCallback() with the engine mutex locked.
  210. void _cmAsDspExecCallback( _cmAsCfg_t* cp )
  211. {
  212. /*
  213. unsigned i;
  214. // get pointers to a set of audio out buffers - pointers to disabled channels will be set to NULL
  215. cmApBufGet( cp->ss.args.outDevIdx, kOutApFl, cp->ctx.oChArray, cp->ctx.oChCnt );
  216. cmApBufGet( cp->ss.args.inDevIdx, kInApFl, cp->ctx.iChArray, cp->ctx.iChCnt );
  217. // zero the output buffers on all enabled channels
  218. for(i=0; i<cp->ctx.oChCnt; ++i)
  219. if( cp->ctx.oChArray[i] != NULL )
  220. memset( cp->ctx.oChArray[i], 0, cp->ss.args.dspFramesPerCycle * sizeof(cmSample_t));
  221. */
  222. // Fill iChArray[] and oChArray[] with pointers to the incoming and outgoing sample buffers.
  223. // Notes:
  224. // 1) Buffers associated with disabled input/output channels will be set to NULL in iChArray[]/oChArray[].
  225. // 2) Buffers associated with channels marked for pass-through will be set to NULL in oChArray[].
  226. // 3) All samples returned in oChArray[] buffers will be set to zero.
  227. cmApBufGetIO(cp->ss.args.inDevIdx, cp->ctx.iChArray, cp->ctx.iChCnt, &cp->ctx.iTimeStamp,
  228. cp->ss.args.outDevIdx, cp->ctx.oChArray, cp->ctx.oChCnt, &cp->ctx.oTimeStamp );
  229. // call the application provided DSP process
  230. cp->ctx.audioRateFl = true;
  231. cp->ss.cbFunc( &cp->ctx, 0, NULL );
  232. cp->ctx.audioRateFl = false;
  233. // advance the audio buffer
  234. cmApBufAdvance( cp->ss.args.outDevIdx, kOutApFl );
  235. cmApBufAdvance( cp->ss.args.inDevIdx, kInApFl );
  236. // handle periodic status messages to the host
  237. if( (cp->statusUpdateSmpIdx += cp->ss.args.dspFramesPerCycle) >= cp->statusUpdateSmpCnt )
  238. {
  239. cp->statusUpdateSmpIdx -= cp->statusUpdateSmpCnt;
  240. if( cp->statusFl )
  241. _cmAsSendStateStatusToHost(cp);
  242. }
  243. }
  244. // Returns true if audio buffer is has waiting incoming samples and
  245. // available outgoing space.
  246. bool _cmAsBufIsReady( const _cmAsCfg_t* cp )
  247. {
  248. // if there neither the input or output device is valid
  249. if( cp->ss.args.inDevIdx==cmInvalidIdx && cp->ss.args.outDevIdx == cmInvalidIdx )
  250. return false;
  251. bool ibFl = cmApBufIsDeviceReady(cp->ss.args.inDevIdx, kInApFl);
  252. bool obFl = cmApBufIsDeviceReady(cp->ss.args.outDevIdx, kOutApFl);
  253. bool iFl = (cp->ss.args.inDevIdx == cmInvalidIdx) || ibFl;
  254. bool oFl = (cp->ss.args.outDevIdx == cmInvalidIdx) || obFl;
  255. //printf("br: %i %i %i %i\n",ibFl,obFl,iFl,oFl);
  256. return iFl && oFl;
  257. }
  258. // This is only called with _cmAsRecd.engMutexH locked
  259. cmAsRC_t _cmAsDeliverMsgsWithLock( _cmAsCfg_t* cp )
  260. {
  261. int i;
  262. cmAsRC_t rc = kOkThRC;
  263. // as long as their may be a msg wating in the incoming msg queue
  264. for(i=0; rc == kOkThRC; ++i)
  265. {
  266. // if a msg is waiting transmit it via cfg->cbFunc()
  267. if((rc = cmTsMp1cDequeueMsg(cp->htdQueueH,NULL,0)) == kOkThRC)
  268. ++cp->status.msgCbCnt;
  269. }
  270. return rc;
  271. }
  272. // This is the main audio system loop (and thread callback function).
  273. // It blocks by waiting on a cond. var (which simultaneously unlocks a mutex).
  274. // With the mutex unlocked messages can pass directly to the DSP process
  275. // via calls to cmAsDeliverMsg().
  276. // When the audio buffers need to be serviced the audio device callback
  277. // signals the cond. var. which results in this thread waking up (and
  278. // simultaneously locking the mutex) as soon as the mutex is available.
  279. bool _cmAsThreadCallback(void* arg)
  280. {
  281. cmAsRC_t rc;
  282. _cmAsCfg_t* cp = (_cmAsCfg_t*)arg;
  283. // lock the cmAudioSys mutex
  284. if((rc = cmThreadMutexLock(cp->engMutexH)) != kOkAsRC )
  285. {
  286. _cmAsError(cp->p,rc,"The cmAudioSys thread mutex lock failed.");
  287. return false;
  288. }
  289. // runFl is always set except during finalization
  290. while( cp->runFl )
  291. {
  292. // if the buffer is NOT ready or the cmAudioSys is disabled
  293. if(_cmAsBufIsReady(cp) == false || cp->enableFl==false )
  294. {
  295. // block on the cond var and unlock the mutex
  296. if( cmThreadMutexWaitOnCondVar(cp->engMutexH,false) != kOkAsRC )
  297. {
  298. cmThreadMutexUnlock(cp->engMutexH);
  299. _cmAsError(cp->p,rc,"The cmAudioSys cond. var. wait failed.");
  300. return false;
  301. }
  302. //
  303. // the cond var was signaled and the mutex is now locked
  304. //
  305. ++cp->status.wakeupCnt;
  306. }
  307. // be sure we are still enabled and the buffer is still ready
  308. if( cp->enableFl && cp->runFl )
  309. {
  310. while( _cmAsBufIsReady(cp) )
  311. {
  312. ++cp->status.audioCbCnt;
  313. // calling this function results in callbacks to cmAudDsp.c:_cmAdUdpNetCallback()
  314. // which in turn calls cmAudioSysDeliverMsg() which queues any incoming messages
  315. // which are then transferred to the DSP processes by the the call to
  316. // _cmAsDeliverMsgWithLock() below.
  317. cmUdpNetReceive(cp->netH,NULL);
  318. // if there are msgs waiting to be sent to the DSP process send them.
  319. if( cmTsMp1cMsgWaiting(cp->htdQueueH) )
  320. _cmAsDeliverMsgsWithLock(cp);
  321. // make the cmAudioSys callback
  322. _cmAsDspExecCallback( cp );
  323. // update the signal time
  324. cp->ctx.begSmpIdx += cp->ss.args.dspFramesPerCycle;
  325. }
  326. }
  327. }
  328. // unlock the mutex
  329. cmThreadMutexUnlock(cp->engMutexH);
  330. return true;
  331. }
  332. // This is the audio port callback function.
  333. //
  334. // _cmAudioSysAudioUpdate() assumes that at most two audio device threads (input and output) may call it.
  335. // cmApBufUpdate() is safe under these conditions since the input and output buffers are updated separately.
  336. // p->audCbLock is used to allow either the input or output thread to signal
  337. // the condition variable. This flag is necessary to prevent both threads from simultaneously
  338. // attempting to signal the condition variable (which will lock the system).
  339. //
  340. // If more than two audio device threads call the function then this function is not safe.
  341. unsigned phase = 0;
  342. void _cmAudioSysAudioUpdate( cmApAudioPacket_t* inPktArray, unsigned inPktCnt, cmApAudioPacket_t* outPktArray, unsigned outPktCnt )
  343. {
  344. _cmAsCfg_t* cp = (_cmAsCfg_t*)(inPktArray!=NULL ? inPktArray[0].userCbPtr : outPktArray[0].userCbPtr);
  345. ++cp->status.updateCnt;
  346. if( cp->runFl )
  347. {
  348. // transfer incoming/outgoing samples from/to the audio device
  349. cmApBufUpdate(inPktArray,inPktCnt,outPktArray,outPktCnt);
  350. /*
  351. //fill output with noise
  352. unsigned i = 0,j =0, k = 0, phs = 0;
  353. for(; i<outPktCnt; ++i)
  354. {
  355. cmApAudioPacket_t* a = outPktArray + i;
  356. cmApSample_t* dp = (cmApSample_t*)a->audioBytesPtr;
  357. phs = a->audioFramesCnt;
  358. for(j=0; j<a->audioFramesCnt; ++j)
  359. {
  360. cmApSample_t v = (cmApSample_t)(0.7 * sin(2*M_PI/44100.0 * phase + j ));
  361. for(k=0; k<a->chCnt; ++k,++dp)
  362. *dp = v;
  363. }
  364. //for(j=0; j<a->audioFramesCnt*a->chCnt; ++j,++dp)
  365. // *dp = (cmApSample_t)(rand() - (RAND_MAX/2))/(RAND_MAX/2);
  366. }
  367. phase += phs;
  368. return;
  369. */
  370. //++p->audCbLock;
  371. bool testBufFl = (cp->syncInputFl==true && inPktCnt>0) || (cp->syncInputFl==false && outPktCnt>0);
  372. //printf("%i %i %i %i\n",testBufFl,cp->syncInputFl,inPktCnt,outPktCnt);
  373. // if the input/output buffer contain samples to be processed then signal the condition variable
  374. // - this will cause the audio system thread to unblock and the used defined DSP process will be called.
  375. if( testBufFl && _cmAsBufIsReady(cp) )
  376. {
  377. if( cmThreadMutexSignalCondVar(cp->engMutexH) != kOkThRC )
  378. _cmAsError(cp->p,kMutexErrAsRC,"CmAudioSys signal cond. var. failed.");
  379. }
  380. //--p->audCbLock;
  381. }
  382. }
  383. // Called when MIDI messages arrive from external MIDI ports.
  384. void _cmAudioSysMidiCallback( const cmMidiPacket_t* pktArray, unsigned pktCnt )
  385. {
  386. unsigned i;
  387. for(i=0; i<pktCnt; ++i)
  388. {
  389. const cmMidiPacket_t* pkt = pktArray + i;
  390. _cmAsCfg_t* cp = (_cmAsCfg_t*)(pkt->cbDataPtr);
  391. if( !cp->runFl )
  392. continue;
  393. cmAudioSysH_t asH;
  394. asH.h = cp->p;
  395. unsigned selId = kMidiMsgArraySelAsId;
  396. const void* msgPtrArray[] = { &cp->ctx.asSubIdx, &selId, &pkt->devIdx, &pkt->portIdx, &pkt->msgCnt, pkt->msgArray };
  397. unsigned msgByteCntArray[] = { sizeof(cp->ctx.asSubIdx), sizeof(selId), sizeof(pkt->devIdx), sizeof(pkt->portIdx), sizeof(pkt->msgCnt), pkt->msgCnt*sizeof(cmMidiMsg) };
  398. unsigned msgSegCnt = sizeof(msgByteCntArray)/sizeof(unsigned);
  399. cmAudioSysDeliverSegMsg(asH,msgPtrArray,msgByteCntArray,msgSegCnt,cmInvalidId);
  400. }
  401. }
  402. void _cmAudioSysSerialPortCallback( void* cbArg, const void* byteA, unsigned byteN )
  403. {
  404. //_cmAsCfg_t* p (_cmAsCfg_t*)cbArg;
  405. // TODO: handle serial receive
  406. /*
  407. int i;
  408. for(i=0; i<byteN; ++i)
  409. {
  410. printf("%02x ",((const uint8_t*)byteA)[i]);
  411. fflush(stdout);
  412. }
  413. */
  414. }
  415. cmAsRC_t cmAudioSysAllocate( cmAudioSysH_t* hp, cmRpt_t* rpt, const cmAudioSysCfg_t* cfg )
  416. {
  417. cmAsRC_t rc;
  418. if((rc = cmAudioSysFree(hp)) != kOkAsRC )
  419. return rc;
  420. cmAs_t* p = cmMemAllocZ( cmAs_t, 1 );
  421. cmErrSetup(&p->err,rpt,"Audio System");
  422. hp->h = p;
  423. if( cfg != NULL )
  424. if((rc = cmAudioSysInitialize( *hp, cfg )) != kOkAsRC )
  425. cmAudioSysFree(hp);
  426. return rc;
  427. }
  428. cmAsRC_t cmAudioSysFree( cmAudioSysH_t* hp )
  429. {
  430. cmAsRC_t rc;
  431. if( hp == NULL || hp->h == NULL )
  432. return kOkAsRC;
  433. if((rc = cmAudioSysFinalize(*hp)) != kOkAsRC )
  434. return rc;
  435. cmAs_t* p = _cmAsHandleToPtr(*hp);
  436. cmMemFree(p);
  437. hp->h = NULL;
  438. return rc;
  439. }
  440. cmAsRC_t _cmAudioSysEnable( cmAs_t* p, bool enableFl )
  441. {
  442. cmAsRC_t rc;
  443. unsigned i;
  444. for(i=0; i<p->ssCnt; ++i)
  445. {
  446. _cmAsCfg_t* cp = p->ssArray + i;
  447. cmApBufOnPortEnable(cp->ss.args.inDevIdx,enableFl);
  448. cmApBufOnPortEnable(cp->ss.args.outDevIdx,enableFl);
  449. if( enableFl )
  450. {
  451. //cmApBufPrimeOutput( cp->ss.args.outDevIdx, 2 );
  452. // start the input device
  453. if((rc = cmApDeviceStart( cp->ss.args.inDevIdx )) != kOkAsRC )
  454. return _cmAsError(p,kAudioDevStartFailAsRC,"The audio input device start failed.");
  455. // start the output device
  456. if( cmApDeviceStart( cp->ss.args.outDevIdx ) != kOkAsRC )
  457. return _cmAsError(p,kAudioDevStartFailAsRC,"The audio ouput device start failed.");
  458. }
  459. else
  460. {
  461. // stop the input device
  462. if((rc = cmApDeviceStop( cp->ss.args.inDevIdx )) != kOkAsRC )
  463. return _cmAsError(p,kAudioDevStopFailAsRC,"The audio input device stop failed.");
  464. // stop the output device
  465. if((rc = cmApDeviceStop( cp->ss.args.outDevIdx )) != kOkAsRC )
  466. return _cmAsError(p,kAudioDevStopFailAsRC,"The audio output device stop failed.");
  467. }
  468. cp->enableFl = enableFl;
  469. }
  470. return kOkAsRC;
  471. }
  472. cmAsRC_t _cmAudioSysFinalize( cmAs_t* p )
  473. {
  474. cmAsRC_t rc = kOkAsRC;
  475. unsigned i;
  476. // mark the audio system as NOT initialized
  477. p->initFl = false;
  478. // be sure all audio callbacks are disabled before continuing.
  479. if((rc = _cmAudioSysEnable(p,false)) != kOkAsRC )
  480. return _cmAsError(p,rc,"Audio system finalize failed because device halting failed.");
  481. for(i=0; i<p->ssCnt; ++i)
  482. {
  483. _cmAsCfg_t* cp = p->ssArray + i;
  484. if( cmThreadIsValid( cp->threadH ))
  485. {
  486. // inform the thread that it should exit
  487. cp->enableFl = false;
  488. cp->runFl = false;
  489. cp->statusFl = false;
  490. // WARNING: be sure that the audio thread cannot simultaneously signal the
  491. // cond variable from _cmAsAudioUpdate() otherwise the system may crash
  492. while( cp->audCbLock != 0 )
  493. { cmSleepUs(100000); }
  494. // signal the cond var to cause the thread to run
  495. if((rc = cmThreadMutexSignalCondVar(cp->engMutexH)) != kOkThRC )
  496. _cmAsError(p,kMutexErrAsRC,"Finalize signal cond. var. failed.");
  497. // wait to take control of the mutex - this will occur when the thread function exits
  498. if((rc = cmThreadMutexLock(cp->engMutexH)) != kOkThRC )
  499. _cmAsError(p,kMutexErrAsRC,"Finalize lock failed.");
  500. // unlock the mutex because it is no longer needed and must be unlocked to be destroyed
  501. if((rc = cmThreadMutexUnlock(cp->engMutexH)) != kOkThRC )
  502. _cmAsError(p,kMutexErrAsRC,"Finalize unlock failed.");
  503. // destroy the thread
  504. if((rc = cmThreadDestroy( &cp->threadH )) != kOkThRC )
  505. _cmAsError(p,kThreadErrAsRC,"Thread destroy failed.");
  506. }
  507. // destroy the mutex
  508. if( cmThreadMutexIsValid(cp->engMutexH) )
  509. if((rc = cmThreadMutexDestroy( &cp->engMutexH )) != kOkThRC )
  510. _cmAsError(p,kMutexErrAsRC,"Mutex destroy failed.");
  511. // remove the MIDI callback
  512. if( cmMpIsInitialized() && cmMpUsesCallback(-1,-1, _cmAudioSysMidiCallback, cp) )
  513. if( cmMpRemoveCallback( -1, -1, _cmAudioSysMidiCallback, cp ) != kOkMpRC )
  514. _cmAsError(p,kMidiSysFailAsRC,"MIDI callback removal failed.");
  515. // destroy the host-to-dsp msg queue
  516. if( cmTsMp1cIsValid(cp->htdQueueH ) )
  517. if((rc = cmTsMp1cDestroy( &cp->htdQueueH )) != kOkThRC )
  518. _cmAsError(p,kTsQueueErrAsRC,"Host-to-DSP msg queue destroy failed.");
  519. // destroy the dsp-to-host msg queue
  520. if( cmTsMp1cIsValid(p->dthQueH) )
  521. if((rc = cmTsMp1cDestroy( &p->dthQueH )) != kOkThRC )
  522. _cmAsError(p,kTsQueueErrAsRC,"DSP-to-Host msg queue destroy failed.");
  523. cmMemPtrFree(&cp->ctx.iChArray);
  524. cmMemPtrFree(&cp->ctx.oChArray);
  525. cp->ctx.iChCnt = 0;
  526. cp->ctx.oChCnt = 0;
  527. cmMemPtrFree(&cp->iMeterArray);
  528. cmMemPtrFree(&cp->oMeterArray);
  529. cp->status.iMeterCnt = 0;
  530. cp->status.oMeterCnt = 0;
  531. }
  532. cmMemPtrFree(&p->ssArray);
  533. p->ssCnt = 0;
  534. return rc;
  535. }
  536. // A given device may be used as an input device exactly once and an output device exactly once.
  537. // When the input to a given device is used by one sub-system and the output is used by another
  538. // then both sub-systems must use the same srate,devFramesPerCycle, audioBufCnt and dspFramesPerCycle.
  539. cmAsRC_t _cmAsSysValidate( cmErr_t* err, const cmAudioSysCfg_t* cfg )
  540. {
  541. unsigned i,j,k;
  542. for(i=0; i<2; ++i)
  543. {
  544. // examine input devices - then output devices
  545. bool inputFl = i==0;
  546. bool outputFl = !inputFl;
  547. for(j=0; j<cfg->ssCnt; ++j)
  548. {
  549. cmAudioSysArgs_t* s0 = &cfg->ssArray[j].args;
  550. unsigned devIdx = inputFl ? s0->inDevIdx : s0->outDevIdx;
  551. for(k=0; k<cfg->ssCnt && devIdx != cmInvalidIdx; ++k)
  552. if( k != j )
  553. {
  554. cmAudioSysArgs_t* s1 = &cfg->ssArray[k].args;
  555. // if the device was used as input or output multple times then signal an error
  556. if( (inputFl && (s1->inDevIdx == devIdx) && s1->inDevIdx != cmInvalidIdx) || (outputFl && (s1->outDevIdx == devIdx) && s1->outDevIdx != cmInvalidIdx) )
  557. return cmErrMsg(err,kInvalidArgAsRC,"The device %i was used as an %s by multiple sub-systems.", devIdx, inputFl ? "input" : "output");
  558. // if this device is being used by another subsystem ...
  559. if( (inputFl && (s1->outDevIdx == devIdx) && s1->inDevIdx != cmInvalidIdx) || (outputFl && (s1->outDevIdx == devIdx) && s1->outDevIdx != cmInvalidIdx ) )
  560. {
  561. // ... then some of its buffer spec's must match
  562. if( s0->srate != s1->srate || s0->audioBufCnt != s1->audioBufCnt || s0->dspFramesPerCycle != s1->dspFramesPerCycle || s0->devFramesPerCycle != s1->devFramesPerCycle )
  563. return cmErrMsg(err,kInvalidArgAsRC,"The device %i is used by different sub-system with different audio buffer parameters.",devIdx);
  564. }
  565. }
  566. }
  567. }
  568. return kOkAsRC;
  569. }
  570. cmAsRC_t cmAudioSysInitialize( cmAudioSysH_t h, const cmAudioSysCfg_t* cfg )
  571. {
  572. cmAsRC_t rc;
  573. unsigned i;
  574. cmAs_t* p = _cmAsHandleToPtr(h);
  575. // validate the device setup
  576. if((rc =_cmAsSysValidate(&p->err, cfg )) != kOkAsRC )
  577. return rc;
  578. // always finalize before iniitalize
  579. if((rc = cmAudioSysFinalize(h)) != kOkAsRC )
  580. return rc;
  581. // create the audio file devices
  582. /*
  583. for(i=0; i<cfg->afpCnt; ++i)
  584. {
  585. const cmAudioSysFilePort_t* afp = cfg->afpArray + i;
  586. cmApFileDeviceCreate( afp->devLabel, afp->inAudioFn, afp->outAudioFn, afp->oBits, afp->oChCnt );
  587. }
  588. */
  589. p->ssArray = cmMemAllocZ( _cmAsCfg_t, cfg->ssCnt );
  590. p->ssCnt = cfg->ssCnt;
  591. for(i=0; i<p->ssCnt; ++i)
  592. {
  593. _cmAsCfg_t* cp = p->ssArray + i;
  594. const cmAudioSysSubSys_t* ss = cfg->ssArray + i;
  595. cp->p = p;
  596. cp->ss = *ss; // copy the cfg into the internal audio system state
  597. cp->runFl = false;
  598. cp->enableFl = false;
  599. cp->statusFl = false;
  600. cp->ctx.reserved = p;
  601. cp->ctx.asSubIdx = i;
  602. cp->ctx.ss = &cp->ss;
  603. cp->ctx.begSmpIdx = 0;
  604. cp->ctx.dspToHostFunc = _cmAsDspToHostMsgCallback;
  605. // validate the input device index
  606. if( ss->args.inDevIdx != cmInvalidIdx && ss->args.inDevIdx >= cmApDeviceCount() )
  607. {
  608. rc = _cmAsError(p,kAudioDevSetupErrAsRC,"The audio input device index %i is invalid.",ss->args.inDevIdx);
  609. goto errLabel;
  610. }
  611. // validate the output device index
  612. if( ss->args.outDevIdx != cmInvalidIdx && ss->args.outDevIdx >= cmApDeviceCount() )
  613. {
  614. rc = _cmAsError(p,kAudioDevSetupErrAsRC,"The audio output device index %i is invalid.",ss->args.outDevIdx);
  615. goto errLabel;
  616. }
  617. // setup the input device
  618. if( ss->args.inDevIdx != cmInvalidIdx )
  619. if((rc = cmApDeviceSetup( ss->args.inDevIdx, ss->args.srate, ss->args.devFramesPerCycle, _cmAudioSysAudioUpdate, cp )) != kOkAsRC )
  620. {
  621. rc = _cmAsError(p,kAudioDevSetupErrAsRC,"Audio input device setup failed.");
  622. goto errLabel;
  623. }
  624. // setup the output device
  625. if( ss->args.outDevIdx != ss->args.inDevIdx && ss->args.outDevIdx != cmInvalidIdx )
  626. if((rc = cmApDeviceSetup( ss->args.outDevIdx, ss->args.srate, ss->args.devFramesPerCycle, _cmAudioSysAudioUpdate, cp )) != kOkAsRC )
  627. {
  628. rc = _cmAsError(p,kAudioDevSetupErrAsRC,"Audio output device setup failed.");
  629. goto errLabel;
  630. }
  631. // setup the input device buffer
  632. if( ss->args.inDevIdx != cmInvalidIdx )
  633. 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, ss->args.srateMult )) != kOkAsRC )
  634. {
  635. rc = _cmAsError(p,kAudioBufSetupErrAsRC,"Audio buffer input setup failed.");
  636. goto errLabel;
  637. }
  638. cmApBufEnableMeter(ss->args.inDevIdx, -1, kInApFl | kEnableApFl );
  639. cmApBufEnableMeter(ss->args.outDevIdx,-1, kOutApFl | kEnableApFl );
  640. // setup the input audio buffer ptr array - used to send input audio to the DSP system in _cmAsDspExecCallback()
  641. if((cp->ctx.iChCnt = cmApDeviceChannelCount(ss->args.inDevIdx, true)) != 0 )
  642. cp->ctx.iChArray = cmMemAllocZ( cmSample_t*, cp->ctx.iChCnt );
  643. // setup the output device buffer
  644. if( ss->args.outDevIdx != ss->args.inDevIdx )
  645. 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, ss->args.srateMult )) != kOkAsRC )
  646. return _cmAsError(p,kAudioBufSetupErrAsRC,"Audio buffer ouput device setup failed.");
  647. // setup the output audio buffer ptr array - used to recv output audio from the DSP system in _cmAsDspExecCallback()
  648. if((cp->ctx.oChCnt = cmApDeviceChannelCount(ss->args.outDevIdx, false)) != 0 )
  649. cp->ctx.oChArray = cmMemAllocZ( cmSample_t*, cp->ctx.oChCnt );
  650. // determine the sync source
  651. cp->syncInputFl = ss->args.syncInputFl;
  652. // if sync'ing to an unavailable device then sync to the available device
  653. if( ss->args.syncInputFl && cp->ctx.iChCnt == 0 )
  654. cp->syncInputFl = false;
  655. if( ss->args.syncInputFl==false && cp->ctx.oChCnt == 0 )
  656. cp->syncInputFl = true;
  657. // setup the status record
  658. cp->status.asSubIdx = cp->ctx.asSubIdx;
  659. cp->status.iDevIdx = ss->args.inDevIdx;
  660. cp->status.oDevIdx = ss->args.outDevIdx;
  661. cp->status.iMeterCnt = cp->ctx.iChCnt;
  662. cp->status.oMeterCnt = cp->ctx.oChCnt;
  663. cp->iMeterArray = cmMemAllocZ( double, cp->status.iMeterCnt );
  664. cp->oMeterArray = cmMemAllocZ( double, cp->status.oMeterCnt );
  665. cp->netH = cfg->netH;
  666. cp->serialPortH = cfg->serialPortH;
  667. // create the audio System thread
  668. if((rc = cmThreadCreate( &cp->threadH, _cmAsThreadCallback, cp, ss->args.rpt )) != kOkThRC )
  669. {
  670. rc = _cmAsError(p,kThreadErrAsRC,"Thread create failed.");
  671. goto errLabel;
  672. }
  673. // create the audio System mutex
  674. if((rc = cmThreadMutexCreate( &cp->engMutexH, ss->args.rpt )) != kOkThRC )
  675. {
  676. rc = _cmAsError(p,kMutexErrAsRC,"Thread mutex create failed.");
  677. goto errLabel;
  678. }
  679. // create the host-to-dsp thread safe msg queue
  680. if((rc = cmTsMp1cCreate( &cp->htdQueueH, ss->args.msgQueueByteCnt, ss->cbFunc, &cp->ctx, ss->args.rpt )) != kOkThRC )
  681. {
  682. rc = _cmAsError(p,kTsQueueErrAsRC,"Host-to-DSP msg queue create failed.");
  683. goto errLabel;
  684. }
  685. // create the dsp-to-host thread safe msg queue
  686. if( cmTsMp1cIsValid( p->dthQueH ) == false )
  687. {
  688. if((rc = cmTsMp1cCreate( &p->dthQueH, ss->args.msgQueueByteCnt, cfg->clientCbFunc, cfg->clientCbData, ss->args.rpt )) != kOkThRC )
  689. {
  690. rc = _cmAsError(p,kTsQueueErrAsRC,"DSP-to-Host msg queue create failed.");
  691. goto errLabel;
  692. }
  693. }
  694. //cp->dthQueueH = p->dthQueH;
  695. // install an external MIDI port callback handler for incoming MIDI messages
  696. if( cmMpIsInitialized() )
  697. if( cmMpInstallCallback( -1, -1, _cmAudioSysMidiCallback, cp ) != kOkMpRC )
  698. {
  699. rc = _cmAsError(p,kMidiSysFailAsRC,"MIDI system callback installation failed.");
  700. goto errLabel;
  701. }
  702. // install the serial port
  703. if( cmSeIsOpen(cp->serialPortH) )
  704. {
  705. if( cmSeSetCallback(cp->serialPortH, _cmAudioSysSerialPortCallback, cp ) != kOkSeRC )
  706. {
  707. rc = _cmAsError(p,kSerialPortFailAsRC,"Serial port callback installation failed.");
  708. goto errLabel;
  709. }
  710. }
  711. // setup the sub-system status notification
  712. cp->statusUpdateSmpCnt = floor(cmApBufMeterMs() * cp->ss.args.srate / 1000.0 );
  713. cp->statusUpdateSmpIdx = 0;
  714. cp->runFl = true;
  715. // start the audio System thread
  716. if( cmThreadPause( cp->threadH, 0 ) != kOkThRC )
  717. {
  718. rc = _cmAsError(p,kThreadErrAsRC,"Thread start failed.");
  719. goto errLabel;
  720. }
  721. if( cmSeIsOpen(cp->serialPortH) )
  722. {
  723. if( cmSeStart( cp->serialPortH ) != kOkSeRC )
  724. {
  725. rc = _cmAsError(p,kSerialPortFailAsRC,"Serial port start failed.");
  726. goto errLabel;
  727. }
  728. }
  729. }
  730. _cmAsHostInitNotify(p);
  731. p->initFl = true;
  732. errLabel:
  733. if( rc != kOkAsRC )
  734. _cmAudioSysFinalize(p);
  735. return rc;
  736. }
  737. cmAsRC_t cmAudioSysFinalize(cmAudioSysH_t h )
  738. {
  739. cmAsRC_t rc = kOkAsRC;
  740. if( cmAudioSysHandleIsValid(h) == false )
  741. return rc;
  742. cmAs_t* p = _cmAsHandleToPtr(h);
  743. rc = _cmAudioSysFinalize(p);
  744. h.h = NULL;
  745. return rc;
  746. }
  747. bool cmAudioSysIsInitialized( cmAudioSysH_t h )
  748. {
  749. cmAs_t* p = _cmAsHandleToPtr(h);
  750. return p->initFl;
  751. }
  752. cmAsRC_t _cmAudioSysVerifyInit( cmAs_t* p )
  753. {
  754. if( p->initFl == false )
  755. {
  756. // if the last msg generated was also a not init msg then don't
  757. // generate another message - just return the error
  758. if( cmErrLastRC(&p->err) != kNotInitAsRC )
  759. cmErrMsg(&p->err,kNotInitAsRC,"The audio system is not initialized.");
  760. return kNotInitAsRC;
  761. }
  762. return kOkAsRC;
  763. }
  764. bool cmAudioSysIsEnabled( cmAudioSysH_t h )
  765. {
  766. if( cmAudioSysIsInitialized(h) == false )
  767. return false;
  768. cmAs_t* p = _cmAsHandleToPtr(h);
  769. unsigned i;
  770. for(i=0; i<p->ssCnt; ++i)
  771. if( p->ssArray[i].enableFl )
  772. return true;
  773. return false;
  774. }
  775. cmAsRC_t cmAudioSysEnable( cmAudioSysH_t h, bool enableFl )
  776. {
  777. cmAs_t* p = _cmAsHandleToPtr(h);
  778. return _cmAudioSysEnable(p,enableFl);
  779. }
  780. cmAsRC_t cmAudioSysDeliverSegMsg( cmAudioSysH_t h, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt, unsigned srcNetNodeId )
  781. {
  782. cmAs_t* p = _cmAsHandleToPtr(h);
  783. cmAsRC_t rc;
  784. // the system must be initialized to use this function
  785. if((rc = _cmAudioSysVerifyInit(p)) != kOkAsRC )
  786. return rc;
  787. if( msgSegCnt == 0 )
  788. return kOkAsRC;
  789. // BUG BUG BUG - there is no reason that both the asSubIdx and the selId must
  790. // be in the first segment but it would be nice.
  791. assert( msgByteCntArray[0] >= 2*sizeof(unsigned) || (msgSegCnt>1 && msgByteCntArray[0]==sizeof(unsigned) && msgByteCntArray[1]>=sizeof(unsigned)) );
  792. // The audio sub-system index is always the first field of the msg
  793. // and the msg selector id is always the second field
  794. unsigned* array = (unsigned*)msgDataPtrArray[0];
  795. unsigned asSubIdx = array[0];
  796. unsigned selId = array[1];
  797. if( selId == kUiMstrSelAsId )
  798. return _cmAsHandleNonSubSysMsg( p, msgDataPtrArray, msgByteCntArray, msgSegCnt );
  799. if( selId == kNetSyncSelAsId )
  800. {
  801. assert( msgSegCnt==1);
  802. assert( asSubIdx < p->ssCnt );
  803. p->ssArray[asSubIdx].ctx.srcNetNodeId = srcNetNodeId;
  804. p->ssArray[asSubIdx].ss.cbFunc(&p->ssArray[asSubIdx].ctx,msgByteCntArray[0],msgDataPtrArray[0]);
  805. return kOkAsRC;
  806. }
  807. return _cmAsEnqueueMsg(p,p->ssArray[asSubIdx].htdQueueH,msgDataPtrArray,msgByteCntArray,msgSegCnt,"Host-to-DSP");
  808. }
  809. cmAsRC_t cmAudioSysDeliverMsg( cmAudioSysH_t h, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
  810. {
  811. const void* msgDataPtrArray[] = { msgPtr };
  812. unsigned msgByteCntArray[] = { msgByteCnt };
  813. return cmAudioSysDeliverSegMsg(h,msgDataPtrArray,msgByteCntArray,1,srcNetNodeId);
  814. }
  815. cmAsRC_t cmAudioSysDeliverIdMsg( cmAudioSysH_t h, unsigned asSubIdx, unsigned id, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
  816. {
  817. cmAsRC_t rc;
  818. cmAs_t* p = _cmAsHandleToPtr(h);
  819. // the system must be initialized to use this function
  820. if((rc = _cmAudioSysVerifyInit(p)) != kOkAsRC )
  821. return rc;
  822. const void* msgDataPtrArray[] = { &asSubIdx, &id, msgPtr };
  823. unsigned msgByteCntArray[] = { sizeof(asSubIdx), sizeof(id), msgByteCnt };
  824. return cmAudioSysDeliverSegMsg(h,msgDataPtrArray,msgByteCntArray,3,srcNetNodeId);
  825. }
  826. unsigned cmAudioSysIsMsgWaiting( cmAudioSysH_t h )
  827. {
  828. cmAsRC_t rc;
  829. cmAs_t* p = _cmAsHandleToPtr(h);
  830. // the system must be initialized to use this function
  831. if((rc = _cmAudioSysVerifyInit(p)) != kOkAsRC )
  832. return 0;
  833. unsigned n = 0;
  834. unsigned retByteCnt;
  835. for(n=0; n < p->ssCnt; ++n )
  836. {
  837. //if( (retByteCnt = cmTsMp1cDequeueMsgByteCount(p->ssArray[p->waitAsSubIdx].dthQueueH)) > 0 )
  838. if( (retByteCnt = cmTsMp1cDequeueMsgByteCount(p->dthQueH)) > 0 )
  839. return retByteCnt;
  840. p->waitAsSubIdx = (p->waitAsSubIdx + 1) % p->ssCnt;
  841. }
  842. return 0;
  843. }
  844. cmAsRC_t cmAudioSysReceiveMsg( cmAudioSysH_t h, void* msgDataPtr, unsigned msgByteCnt )
  845. {
  846. cmAsRC_t rc;
  847. cmAs_t* p = _cmAsHandleToPtr(h);
  848. // the system must be initialized to use this function
  849. if((rc = _cmAudioSysVerifyInit(p)) != kOkAsRC )
  850. return rc;
  851. //switch( cmTsMp1cDequeueMsg(p->ssArray[p->waitAsSubIdx].dthQueueH,msgDataPtr,msgByteCnt) )
  852. switch( cmTsMp1cDequeueMsg(p->dthQueH,msgDataPtr,msgByteCnt) )
  853. {
  854. case kOkThRC:
  855. p->waitAsSubIdx = (p->waitAsSubIdx + 1) % p->ssCnt;
  856. return kOkAsRC;
  857. case kBufTooSmallThRC:
  858. return kBufTooSmallAsRC;
  859. case kBufEmptyThRC:
  860. return kNoMsgWaitingAsRC;
  861. }
  862. return _cmAsError(p,kTsQueueErrAsRC,"A deque operation failed on the DSP-to-Host message queue.");
  863. }
  864. void cmAudioSysStatus( cmAudioSysH_t h, unsigned asSubIdx, cmAudioSysStatus_t* statusPtr )
  865. {
  866. cmAs_t* p = _cmAsHandleToPtr(h);
  867. // the system must be initialized to use this function
  868. if( _cmAudioSysVerifyInit(p) != kOkAsRC )
  869. return;
  870. if( asSubIdx < p->ssCnt )
  871. *statusPtr = p->ssArray[asSubIdx].status;
  872. }
  873. void cmAudioSysStatusNotifyEnable( cmAudioSysH_t h, unsigned asSubIdx, bool enableFl )
  874. {
  875. cmAs_t* p = _cmAsHandleToPtr(h);
  876. // the system must be initialized to use this function
  877. if( _cmAudioSysVerifyInit(p) != kOkAsRC )
  878. return;
  879. unsigned i = asSubIdx == cmInvalidIdx ? 0 : asSubIdx;
  880. unsigned n = asSubIdx == cmInvalidIdx ? p->ssCnt : asSubIdx+1;
  881. for(; i<n; ++i)
  882. p->ssArray[i].statusFl = enableFl;
  883. }
  884. bool cmAudioSysHandleIsValid( cmAudioSysH_t h )
  885. { return h.h != NULL; }
  886. cmAudioSysCtx_t* cmAudioSysContext( cmAudioSysH_t h, unsigned asSubIdx )
  887. {
  888. cmAs_t* p = _cmAsHandleToPtr(h);
  889. if( _cmAudioSysVerifyInit(p) != kOkAsRC )
  890. return NULL;
  891. return &p->ssArray[asSubIdx].ctx;
  892. }
  893. unsigned cmAudioSysSubSystemCount( cmAudioSysH_t h )
  894. {
  895. cmAs_t* p = _cmAsHandleToPtr(h);
  896. if( _cmAudioSysVerifyInit(p) != kOkAsRC )
  897. return 0;
  898. return p->ssCnt;
  899. }
  900. //===========================================================================================================================
  901. //
  902. // cmAsTest()
  903. //
  904. /// [cmAudioSysTest]
  905. typedef struct
  906. {
  907. double hz; // current synth frq
  908. long phs; // current synth phase
  909. double srate; // audio sample rate
  910. unsigned cbCnt; // DSP cycle count
  911. bool synthFl; // true=synth false=pass through
  912. } _cmAsTestCbRecd;
  913. typedef struct
  914. {
  915. unsigned asSubIdx; // asSubIdx must always be the first field in the msg
  916. unsigned id; // 0 = set DSP Hz, 1 = report cbCount to host
  917. double hz;
  918. unsigned uint;
  919. } _cmAsTestMsg;
  920. long _cmAsSynthSine( _cmAsTestCbRecd* r, cmApSample_t* p, unsigned chCnt, unsigned frmCnt )
  921. {
  922. long ph = 0;
  923. unsigned i;
  924. for(i=0; i<chCnt; ++i)
  925. {
  926. unsigned j;
  927. cmApSample_t* op = p + i;
  928. ph = r->phs;
  929. for(j=0; j<frmCnt; j++, op+=chCnt, ph++)
  930. *op = (cmApSample_t)(0.9 * sin( 2.0 * M_PI * r->hz * ph / r->srate ));
  931. }
  932. return ph;
  933. }
  934. unsigned _cmAsTestChIdx = 0;
  935. cmRC_t _cmAsTestCb( void* cbPtr, unsigned msgByteCnt, const void* msgDataPtr )
  936. {
  937. cmRC_t rc = cmOkRC;
  938. cmAudioSysCtx_t* ctx = (cmAudioSysCtx_t*)cbPtr;
  939. cmAudioSysSubSys_t* ss = ctx->ss;
  940. _cmAsTestCbRecd* r = (_cmAsTestCbRecd*)ss->cbDataPtr;
  941. // update the calback counter
  942. ++r->cbCnt;
  943. // if this is an audio update request
  944. if( msgByteCnt == 0 )
  945. {
  946. unsigned i;
  947. if( r->synthFl )
  948. {
  949. long phs = 0;
  950. if(0)
  951. {
  952. for(i=0; i<ctx->oChCnt; ++i)
  953. if( ctx->oChArray[i] != NULL )
  954. phs = _cmAsSynthSine(r, ctx->oChArray[i], 1, ss->args.dspFramesPerCycle );
  955. }
  956. else
  957. {
  958. if( _cmAsTestChIdx < ctx->oChCnt )
  959. phs = _cmAsSynthSine(r, ctx->oChArray[_cmAsTestChIdx], 1, ss->args.dspFramesPerCycle );
  960. }
  961. r->phs = phs;
  962. }
  963. else
  964. {
  965. // BUG BUG BUG - this assumes that the input and output channels are the same.
  966. unsigned chCnt = cmMin(ctx->oChCnt,ctx->iChCnt);
  967. for(i=0; i<chCnt; ++i)
  968. memcpy(ctx->oChArray[i],ctx->iChArray[i],sizeof(cmSample_t)*ss->args.dspFramesPerCycle);
  969. }
  970. }
  971. else // ... otherwise it is a msg for the DSP process from the host
  972. {
  973. _cmAsTestMsg* msg = (_cmAsTestMsg*)msgDataPtr;
  974. msg->asSubIdx = ctx->asSubIdx;
  975. switch(msg->id)
  976. {
  977. case 0:
  978. r->hz = msg->hz;
  979. break;
  980. case 1:
  981. msg->uint = r->cbCnt;
  982. msgByteCnt = sizeof(_cmAsTestMsg);
  983. rc = ctx->dspToHostFunc(ctx,(const void **)&msg,&msgByteCnt,1);
  984. break;
  985. }
  986. }
  987. return rc;
  988. }
  989. // print the usage message for cmAudioPortTest.c
  990. void _cmAsPrintUsage( cmRpt_t* rpt )
  991. {
  992. char msg[] =
  993. "cmAudioSysTest() command switches:\n"
  994. "-r <srate> -c <chcnt> -b <bufcnt> -f <frmcnt> -i <idevidx> -o <odevidx> -m <msgqsize> -d <dspsize> -t -p -h \n"
  995. "\n"
  996. "-r <srate> = sample rate (48000)\n"
  997. "-c <chcnt> = audio channels (2)\n"
  998. "-b <bufcnt> = count of buffers (3)\n"
  999. "-f <frmcnt> = count of samples per buffer (512)\n"
  1000. "-i <idevidx> = input device index (0)\n"
  1001. "-o <odevidx> = output device index (2)\n"
  1002. "-m <msgqsize> = message queue byte count (1024)\n"
  1003. "-d <dspsize> = samples per DSP frame (64)\n"
  1004. "-s = true: sync to input port false: sync to output port\n"
  1005. "-t = copy input to output otherwise synthesize a 1000 Hz sine (false)\n"
  1006. "-p = report but don't start audio devices\n"
  1007. "-h = print this usage message\n";
  1008. cmRptPrintf(rpt,"%s",msg);
  1009. }
  1010. // Get a command line option.
  1011. int _cmAsGetOpt( int argc, const char* argv[], const char* label, int defaultVal, bool boolFl )
  1012. {
  1013. int i = 0;
  1014. for(; i<argc; ++i)
  1015. if( strcmp(label,argv[i]) == 0 )
  1016. {
  1017. if(boolFl)
  1018. return 1;
  1019. if( i == (argc-1) )
  1020. return defaultVal;
  1021. return atoi(argv[i+1]);
  1022. }
  1023. return defaultVal;
  1024. }
  1025. bool _cmAsGetBoolOpt( int argc, const char* argv[], const char* label, bool defaultVal )
  1026. { return _cmAsGetOpt(argc,argv,label,defaultVal?1:0,true)!=0; }
  1027. int _cmAsGetIntOpt( int argc, const char* argv[], const char* label, int defaultVal )
  1028. { return _cmAsGetOpt(argc,argv,label,defaultVal,false); }
  1029. void cmAudioSysTest( cmRpt_t* rpt, int argc, const char* argv[] )
  1030. {
  1031. cmAudioSysCfg_t cfg;
  1032. cmAudioSysSubSys_t ss;
  1033. cmAudioSysH_t h = cmAudioSysNullHandle;
  1034. cmAudioSysStatus_t status;
  1035. _cmAsTestCbRecd cbRecd = {1000.0,0,48000.0,0};
  1036. cfg.ssArray = &ss;
  1037. cfg.ssCnt = 1;
  1038. //cfg.afpArray= NULL;
  1039. //cfg.afpCnt = 0;
  1040. cfg.meterMs = 50;
  1041. if(_cmAsGetBoolOpt(argc,argv,"-h",false))
  1042. _cmAsPrintUsage(rpt);
  1043. cbRecd.srate = _cmAsGetIntOpt(argc,argv,"-r",48000);
  1044. cbRecd.synthFl = _cmAsGetBoolOpt(argc,argv,"-t",false)==false;
  1045. ss.args.rpt = rpt;
  1046. ss.args.inDevIdx = _cmAsGetIntOpt( argc,argv,"-i",0);
  1047. ss.args.outDevIdx = _cmAsGetIntOpt( argc,argv,"-o",2);
  1048. ss.args.syncInputFl = _cmAsGetBoolOpt(argc,argv,"-s",true);
  1049. ss.args.msgQueueByteCnt = _cmAsGetIntOpt( argc,argv,"-m",8192);
  1050. ss.args.devFramesPerCycle = _cmAsGetIntOpt( argc,argv,"-f",512);
  1051. ss.args.dspFramesPerCycle = _cmAsGetIntOpt( argc,argv,"-d",64);;
  1052. ss.args.audioBufCnt = _cmAsGetIntOpt( argc,argv,"-b",3);
  1053. ss.args.srate = cbRecd.srate;
  1054. ss.cbFunc = _cmAsTestCb; // set the DSP entry function
  1055. ss.cbDataPtr = &cbRecd; // set the DSP function argument record
  1056. 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,
  1057. ss.args.msgQueueByteCnt,ss.args.devFramesPerCycle,ss.args.dspFramesPerCycle,ss.args.audioBufCnt,ss.args.srate);
  1058. if( cmApNrtAllocate(rpt) != kOkApRC )
  1059. goto errLabel;
  1060. if( cmApFileAllocate(rpt) != kOkApRC )
  1061. goto errLabel;
  1062. // initialize the audio device system
  1063. if( cmApInitialize(rpt) != kOkApRC )
  1064. goto errLabel;
  1065. cmApReport(rpt);
  1066. // initialize the audio buffer
  1067. if( cmApBufInitialize( cmApDeviceCount(), cfg.meterMs ) != kOkApRC )
  1068. goto errLabel;
  1069. // initialize the audio system
  1070. if( cmAudioSysAllocate(&h,rpt,&cfg) != kOkAsRC )
  1071. goto errLabel;
  1072. // start the audio system
  1073. cmAudioSysEnable(h,true);
  1074. char c = 0;
  1075. printf("q=quit a-g=note n=ch r=rqst s=status\n");
  1076. // simulate a host event loop
  1077. while(c != 'q')
  1078. {
  1079. _cmAsTestMsg msg = {0,0,0,0};
  1080. bool fl = true;
  1081. // wait here for a key press
  1082. c =(char)fgetc(stdin);
  1083. fflush(stdin);
  1084. switch(c)
  1085. {
  1086. case 'c': msg.hz = cmMidiToHz(60); break;
  1087. case 'd': msg.hz = cmMidiToHz(62); break;
  1088. case 'e': msg.hz = cmMidiToHz(64); break;
  1089. case 'f': msg.hz = cmMidiToHz(65); break;
  1090. case 'g': msg.hz = cmMidiToHz(67); break;
  1091. case 'a': msg.hz = cmMidiToHz(69); break;
  1092. case 'b': msg.hz = cmMidiToHz(71); break;
  1093. case 'r': msg.id = 1; break; // request DSP process to send a callback count
  1094. case 'n': ++_cmAsTestChIdx; printf("ch:%i\n",_cmAsTestChIdx); break;
  1095. case 's':
  1096. // report the audio system status
  1097. cmAudioSysStatus(h,0,&status);
  1098. 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);
  1099. //printf("%f \n",status.oMeterArray[0]);
  1100. fl = false;
  1101. break;
  1102. default:
  1103. fl=false;
  1104. }
  1105. if( fl )
  1106. {
  1107. // transmit a command to the DSP process
  1108. cmAudioSysDeliverMsg(h,&msg, sizeof(msg), cmInvalidId);
  1109. }
  1110. // check if messages are waiting to be delivered from the DSP process
  1111. unsigned msgByteCnt;
  1112. if((msgByteCnt = cmAudioSysIsMsgWaiting(h)) > 0 )
  1113. {
  1114. char buf[ msgByteCnt ];
  1115. // rcv a msg from the DSP process
  1116. if( cmAudioSysReceiveMsg(h,buf,msgByteCnt) == kOkAsRC )
  1117. {
  1118. _cmAsTestMsg* msg = (_cmAsTestMsg*)buf;
  1119. switch(msg->id)
  1120. {
  1121. case 1:
  1122. printf("RCV: Callback count:%i\n",msg->uint);
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. // report the audio buffer status
  1128. //cmApBufReport(ss.args.rpt);
  1129. }
  1130. // stop the audio system
  1131. cmAudioSysEnable(h,false);
  1132. goto exitLabel;
  1133. errLabel:
  1134. printf("AUDIO SYSTEM TEST ERROR\n");
  1135. exitLabel:
  1136. cmAudioSysFree(&h);
  1137. cmApFinalize();
  1138. cmApFileFree();
  1139. cmApNrtFree();
  1140. cmApBufFinalize();
  1141. }
  1142. /// [cmAudioSysTest]