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

cmApBuf.c 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmMem.h"
  6. #include "cmMallocDebug.h"
  7. #include "cmTime.h"
  8. #include "cmAudioPort.h"
  9. #include "cmApBuf.h"
  10. #include "cmThread.h"
  11. /*
  12. This API is in general called by two types of threads:
  13. audio devices threads and the client thread. There
  14. may be multiple devie threads however there is only
  15. one client thread.
  16. The audio device threads only call cmApBufUpdate().
  17. cmApBufUpdate() is never called by any other threads.
  18. A call from the audio update threads targets specific channels
  19. (cmApCh records). The variables within each channels that
  20. it modifies are confined to:
  21. on input channels: increments ii and increments fn (data is entering the ch. buffers)
  22. on output channels: increments oi and decrements fn (data is leaving the ch. buffers)
  23. The client picks up incoming audio and provides outgoing audio via
  24. cmApBufGet(). It then informs the cmApBuf() that it has completed
  25. the audio data transfer by calling cmApBufAdvance().
  26. cmApBufAdvance() modifies the following internal variables:
  27. on input channels: increments oi and decrements fn (data has left the ch buffer)
  28. on output channels: increments ii and increments fn (data has enterned the ch. buffer)
  29. Based on the above scenario the channel ii and oi variables are always thread-safe
  30. because they are only changed by a single thread.
  31. ii oi fn
  32. ------ ----- ----
  33. input ch: audio client both
  34. output ch: client audio both
  35. The fn variable however is not thread-safe and therefore care must be taken as
  36. to how it is read and updated.
  37. */
  38. enum { kInApIdx=0, kOutApIdx=1, kIoApCnt=2 };
  39. typedef struct
  40. {
  41. unsigned fl; // kChApFl|kToneApFl|kMeterApFl ...
  42. cmApSample_t* b; // b[n]
  43. unsigned ii; // next in
  44. unsigned oi; // next out
  45. unsigned fn; // full cnt - count of samples currently in the buffer - incr'd by incoming, decr'd by outgoing
  46. unsigned phs; // tone phase
  47. double hz; // tone frequency
  48. double gain; // channel gain
  49. cmApSample_t* m; // m[mn] meter sample sum
  50. unsigned mn; // length of m[]
  51. unsigned mi; // next ele of m[] to rcv sum
  52. } cmApCh;
  53. typedef struct
  54. {
  55. unsigned chCnt;
  56. cmApCh* chArray;
  57. unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle
  58. double srate; // device sample rate;
  59. unsigned faultCnt;
  60. unsigned framesPerCycle;
  61. unsigned dspFrameCnt;
  62. cmTimeSpec_t timeStamp; // base (starting) time stamp for this device
  63. unsigned ioFrameCnt; // count of frames input or output for this device
  64. } cmApIO;
  65. typedef struct
  66. {
  67. // ioArray[] always contains 2 elements - one for input the other for output.
  68. cmApIO ioArray[kIoApCnt];
  69. } cmApDev;
  70. typedef struct
  71. {
  72. cmApDev* devArray;
  73. unsigned devCnt;
  74. unsigned meterMs;
  75. cmApSample_t* zeroBuf; // buffer of zeros
  76. unsigned zeroBufCnt; // max of all dspFrameCnt for all devices.
  77. } cmApBuf;
  78. cmApBuf _cmApBuf;
  79. cmApSample_t _cmApMeterValue( const cmApCh* cp )
  80. {
  81. double sum = 0;
  82. unsigned i;
  83. for(i=0; i<cp->mn; ++i)
  84. sum += cp->m[i];
  85. return cp->mn==0 ? 0 : (cmApSample_t)sqrt(sum/cp->mn);
  86. }
  87. void _cmApSine( cmApCh* cp, cmApSample_t* b0, unsigned n0, cmApSample_t* b1, unsigned n1, unsigned stride, float srate )
  88. {
  89. unsigned i;
  90. for(i=0; i<n0; ++i,++cp->phs)
  91. b0[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
  92. for(i=0; i<n1; ++i,++cp->phs)
  93. b1[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
  94. }
  95. cmApSample_t _cmApMeter( const cmApSample_t* b, unsigned bn, unsigned stride )
  96. {
  97. const cmApSample_t* ep = b + bn;
  98. cmApSample_t sum = 0;
  99. for(; b<ep; b+=stride)
  100. sum += *b * *b;
  101. return sum / bn;
  102. }
  103. void _cmApChFinalize( cmApCh* chPtr )
  104. {
  105. cmMemPtrFree( &chPtr->b );
  106. cmMemPtrFree( &chPtr->m );
  107. }
  108. // n=buf sample cnt mn=meter buf smp cnt
  109. void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn )
  110. {
  111. _cmApChFinalize(chPtr);
  112. chPtr->b = n==0 ? NULL : cmMemAllocZ( cmApSample_t, n );
  113. chPtr->ii = 0;
  114. chPtr->oi = 0;
  115. chPtr->fn = 0;
  116. chPtr->fl = (n!=0 ? kChApFl : 0);
  117. chPtr->hz = 1000;
  118. chPtr->gain = 1.0;
  119. chPtr->mn = mn;
  120. chPtr->m = cmMemAllocZ(cmApSample_t,mn);
  121. chPtr->mi = 0;
  122. }
  123. void _cmApIoFinalize( cmApIO* ioPtr )
  124. {
  125. unsigned i;
  126. for(i=0; i<ioPtr->chCnt; ++i)
  127. _cmApChFinalize( ioPtr->chArray + i );
  128. cmMemPtrFree(&ioPtr->chArray);
  129. ioPtr->chCnt = 0;
  130. ioPtr->n = 0;
  131. }
  132. void _cmApIoInitialize( cmApIO* ioPtr, double srate, unsigned framesPerCycle, unsigned chCnt, unsigned n, unsigned meterBufN, unsigned dspFrameCnt )
  133. {
  134. unsigned i;
  135. _cmApIoFinalize(ioPtr);
  136. n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt
  137. ioPtr->chArray = chCnt==0 ? NULL : cmMemAllocZ( cmApCh, chCnt );
  138. ioPtr->chCnt = chCnt;
  139. ioPtr->n = n;
  140. ioPtr->faultCnt = 0;
  141. ioPtr->framesPerCycle = framesPerCycle;
  142. ioPtr->srate = srate;
  143. ioPtr->dspFrameCnt = dspFrameCnt;
  144. ioPtr->timeStamp.tv_sec = 0;
  145. ioPtr->timeStamp.tv_nsec = 0;
  146. ioPtr->ioFrameCnt = 0;
  147. for(i=0; i<chCnt; ++i )
  148. _cmApChInitialize( ioPtr->chArray + i, n, meterBufN );
  149. }
  150. void _cmApDevFinalize( cmApDev* dp )
  151. {
  152. unsigned i;
  153. for(i=0; i<kIoApCnt; ++i)
  154. _cmApIoFinalize( dp->ioArray+i);
  155. }
  156. void _cmApDevInitialize( cmApDev* dp, double srate, unsigned iFpC, unsigned iChCnt, unsigned iBufN, unsigned oFpC, unsigned oChCnt, unsigned oBufN, unsigned meterBufN, unsigned dspFrameCnt )
  157. {
  158. unsigned i;
  159. _cmApDevFinalize(dp);
  160. for(i=0; i<kIoApCnt; ++i)
  161. {
  162. unsigned chCnt = i==kInApIdx ? iChCnt : oChCnt;
  163. unsigned bufN = i==kInApIdx ? iBufN : oBufN;
  164. unsigned fpc = i==kInApIdx ? iFpC : oFpC;
  165. _cmApIoInitialize( dp->ioArray+i, srate, fpc, chCnt, bufN, meterBufN, dspFrameCnt );
  166. }
  167. }
  168. cmAbRC_t cmApBufInitialize( unsigned devCnt, unsigned meterMs )
  169. {
  170. cmAbRC_t rc;
  171. if((rc = cmApBufFinalize()) != kOkAbRC )
  172. return rc;
  173. _cmApBuf.devArray = cmMemAllocZ( cmApDev, devCnt );
  174. _cmApBuf.devCnt = devCnt;
  175. cmApBufSetMeterMs(meterMs);
  176. return kOkAbRC;
  177. }
  178. cmAbRC_t cmApBufFinalize()
  179. {
  180. unsigned i;
  181. for(i=0; i<_cmApBuf.devCnt; ++i)
  182. _cmApDevFinalize(_cmApBuf.devArray + i);
  183. cmMemPtrFree( &_cmApBuf.devArray );
  184. cmMemPtrFree( &_cmApBuf.zeroBuf );
  185. _cmApBuf.devCnt = 0;
  186. return kOkAbRC;
  187. }
  188. cmAbRC_t cmApBufSetup(
  189. unsigned devIdx,
  190. double srate,
  191. unsigned dspFrameCnt,
  192. unsigned bufCnt,
  193. unsigned inChCnt,
  194. unsigned inFramesPerCycle,
  195. unsigned outChCnt,
  196. unsigned outFramesPerCycle)
  197. {
  198. cmApDev* devPtr = _cmApBuf.devArray + devIdx;
  199. unsigned iBufN = bufCnt * inFramesPerCycle;
  200. unsigned oBufN = bufCnt * outFramesPerCycle;
  201. unsigned meterBufN = cmMax(1,floor(srate * _cmApBuf.meterMs / (1000.0 * outFramesPerCycle)));
  202. _cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt );
  203. if( inFramesPerCycle > _cmApBuf.zeroBufCnt || outFramesPerCycle > _cmApBuf.zeroBufCnt )
  204. {
  205. _cmApBuf.zeroBufCnt = cmMax(inFramesPerCycle,outFramesPerCycle);
  206. _cmApBuf.zeroBuf = cmMemResizeZ(cmApSample_t,_cmApBuf.zeroBuf,_cmApBuf.zeroBufCnt);
  207. }
  208. return kOkAbRC;
  209. }
  210. cmAbRC_t cmApBufPrimeOutput( unsigned devIdx, unsigned audioCycleCnt )
  211. {
  212. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  213. unsigned i;
  214. for(i=0; i<iop->chCnt; ++i)
  215. {
  216. cmApCh* cp = iop->chArray + i;
  217. unsigned bn = iop->n * sizeof(cmApSample_t);
  218. memset(cp->b,0,bn);
  219. cp->oi = 0;
  220. cp->ii = iop->framesPerCycle * audioCycleCnt;
  221. cp->fn = iop->framesPerCycle * audioCycleCnt;
  222. }
  223. return kOkAbRC;
  224. }
  225. void cmApBufOnPortEnable( unsigned devIdx, bool enableFl )
  226. {
  227. if( devIdx == cmInvalidIdx || enableFl==false)
  228. return;
  229. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  230. iop->timeStamp.tv_sec = 0;
  231. iop->timeStamp.tv_nsec = 0;
  232. iop->ioFrameCnt = 0;
  233. iop = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  234. iop->timeStamp.tv_sec = 0;
  235. iop->timeStamp.tv_nsec = 0;
  236. iop->ioFrameCnt = 0;
  237. }
  238. cmAbRC_t cmApBufUpdate(
  239. cmApAudioPacket_t* inPktArray,
  240. unsigned inPktCnt,
  241. cmApAudioPacket_t* outPktArray,
  242. unsigned outPktCnt )
  243. {
  244. unsigned i,j;
  245. // copy samples from the packet to the buffer
  246. if( inPktArray != NULL )
  247. {
  248. for(i=0; i<inPktCnt; ++i)
  249. {
  250. cmApAudioPacket_t* pp = inPktArray + i;
  251. cmApIO* ip = _cmApBuf.devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd
  252. // if the base time stamp has not yet been set - then set it
  253. if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 )
  254. ip->timeStamp = pp->timeStamp;
  255. // for each source packet channel and enabled dest channel
  256. for(j=0; j<pp->chCnt; ++j)
  257. {
  258. cmApCh* cp = ip->chArray + pp->begChIdx +j; // dest ch
  259. unsigned n0 = ip->n - cp->ii; // first dest segment
  260. unsigned n1 = 0; // second dest segment
  261. assert(pp->begChIdx + j < ip->chCnt );
  262. // if the incoming samples would overflow the buffer then ignore them
  263. if( cp->fn + pp->audioFramesCnt > ip->n )
  264. {
  265. ++ip->faultCnt; // record input overflow
  266. continue;
  267. }
  268. // if the incoming samples would go off the end of the buffer then
  269. // copy in the samples in two segments (one at the end and another at begin of dest channel)
  270. if( n0 < pp->audioFramesCnt )
  271. n1 = pp->audioFramesCnt-n0;
  272. else
  273. n0 = pp->audioFramesCnt;
  274. bool enaFl = cmIsFlag(cp->fl,kChApFl) && cmIsFlag(cp->fl,kMuteApFl)==false;
  275. const cmApSample_t* sp = enaFl ? ((cmApSample_t*)pp->audioBytesPtr) + j : _cmApBuf.zeroBuf;
  276. unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved)
  277. cmApSample_t* dp = cp->b + cp->ii;
  278. const cmApSample_t* ep = dp + n0;
  279. // update the meter
  280. if( cmIsFlag(cp->fl,kMeterApFl) )
  281. {
  282. cp->m[cp->mi] = _cmApMeter(sp,pp->audioFramesCnt,pp->chCnt);
  283. cp->mi = (cp->mi + 1) % cp->mn;
  284. }
  285. // if the test tone is enabled on this input channel
  286. if( enaFl && cmIsFlag(cp->fl,kToneApFl) )
  287. {
  288. _cmApSine(cp, dp, n0, cp->b, n1, 1, ip->srate );
  289. }
  290. else // otherwise copy samples from the packet to the buffer
  291. {
  292. // copy the first segment
  293. for(; dp < ep; sp += ssn )
  294. *dp++ = cp->gain * *sp;
  295. // if there is a second segment
  296. if( n1 > 0 )
  297. {
  298. // copy the second segment
  299. dp = cp->b;
  300. ep = dp + n1;
  301. for(; dp<ep; sp += ssn )
  302. *dp++ = cp->gain * *sp;
  303. }
  304. }
  305. // advance the input channel buffer
  306. cp->ii = n1>0 ? n1 : cp->ii + n0;
  307. //cp->fn += pp->audioFramesCnt;
  308. cmThUIntIncr(&cp->fn,pp->audioFramesCnt);
  309. }
  310. }
  311. }
  312. // copy samples from the buffer to the packet
  313. if( outPktArray != NULL )
  314. {
  315. for(i=0; i<outPktCnt; ++i)
  316. {
  317. cmApAudioPacket_t* pp = outPktArray + i;
  318. cmApIO* op = _cmApBuf.devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd
  319. // if the base timestamp has not yet been set then set it.
  320. if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 )
  321. op->timeStamp = pp->timeStamp;
  322. // for each dest packet channel and enabled source channel
  323. for(j=0; j<pp->chCnt; ++j)
  324. {
  325. cmApCh* cp = op->chArray + pp->begChIdx + j; // dest ch
  326. unsigned n0 = op->n - cp->oi; // first src segment
  327. unsigned n1 = 0; // second src segment
  328. volatile unsigned fn = cp->fn; // store fn because it may be changed by the client thread
  329. // if the outgoing samples will underflow the buffer
  330. if( pp->audioFramesCnt > fn )
  331. {
  332. ++op->faultCnt; // record an output underflow
  333. // if the buffer is empty - zero the packet and return
  334. if( fn == 0 )
  335. {
  336. memset( pp->audioBytesPtr, 0, pp->audioFramesCnt*sizeof(cmApSample_t));
  337. continue;
  338. }
  339. // ... otherwise decrease the count of returned samples
  340. pp->audioFramesCnt = fn;
  341. }
  342. // if the outgong segments would go off the end of the buffer then
  343. // arrange to wrap to the begining of the buffer
  344. if( n0 < pp->audioFramesCnt )
  345. n1 = pp->audioFramesCnt-n0;
  346. else
  347. n0 = pp->audioFramesCnt;
  348. cmApSample_t* dp = ((cmApSample_t*)pp->audioBytesPtr) + j;
  349. bool enaFl = cmIsFlag(cp->fl,kChApFl) && cmIsFlag(cp->fl,kMuteApFl)==false;
  350. // if the tone is enabled on this channel
  351. if( enaFl && cmIsFlag(cp->fl,kToneApFl) )
  352. {
  353. _cmApSine(cp, dp, n0, dp + n0*pp->chCnt, n1, pp->chCnt, op->srate );
  354. }
  355. else // otherwise copy samples from the output buffer to the packet
  356. {
  357. const cmApSample_t* sp = enaFl ? cp->b + cp->oi : _cmApBuf.zeroBuf;
  358. const cmApSample_t* ep = sp + n0;
  359. // copy the first segment
  360. for(; sp < ep; dp += pp->chCnt )
  361. *dp = cp->gain * *sp++;
  362. // if there is a second segment
  363. if( n1 > 0 )
  364. {
  365. // copy the second segment
  366. sp = enaFl ? cp->b : _cmApBuf.zeroBuf;
  367. ep = sp + n1;
  368. for(; sp<ep; dp += pp->chCnt )
  369. *dp = cp->gain * *sp++;
  370. }
  371. }
  372. // update the meter
  373. if( cmIsFlag(cp->fl,kMeterApFl) )
  374. {
  375. cp->m[cp->mi] = _cmApMeter(((cmApSample_t*)pp->audioBytesPtr)+j,pp->audioFramesCnt,pp->chCnt);
  376. cp->mi = (cp->mi + 1) % cp->mn;
  377. }
  378. // advance the output channel buffer
  379. cp->oi = n1>0 ? n1 : cp->oi + n0;
  380. //cp->fn -= pp->audioFramesCnt;
  381. cmThUIntDecr(&cp->fn,pp->audioFramesCnt);
  382. }
  383. }
  384. }
  385. return kOkAbRC;
  386. }
  387. unsigned cmApBufMeterMs()
  388. { return _cmApBuf.meterMs; }
  389. void cmApBufSetMeterMs( unsigned meterMs )
  390. {
  391. _cmApBuf.meterMs = cmMin(1000,cmMax(10,meterMs));
  392. }
  393. unsigned cmApBufChannelCount( unsigned devIdx, unsigned flags )
  394. {
  395. if( devIdx == cmInvalidIdx )
  396. return 0;
  397. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  398. return _cmApBuf.devArray[devIdx].ioArray[ idx ].chCnt;
  399. }
  400. void cmApBufSetFlag( unsigned devIdx, unsigned chIdx, unsigned flags )
  401. {
  402. if( devIdx == cmInvalidIdx )
  403. return;
  404. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  405. bool enableFl = flags & kEnableApFl ? true : false;
  406. unsigned i = chIdx != -1 ? chIdx : 0;
  407. unsigned n = chIdx != -1 ? chIdx+1 : _cmApBuf.devArray[devIdx].ioArray[idx].chCnt;
  408. for(; i<n; ++i)
  409. {
  410. cmApCh* cp = _cmApBuf.devArray[devIdx].ioArray[idx].chArray + i;
  411. cp->fl = cmEnaFlag(cp->fl, flags & (kChApFl|kToneApFl|kMeterApFl|kMuteApFl|kPassApFl), enableFl );
  412. }
  413. }
  414. bool cmApBufIsFlag( unsigned devIdx, unsigned chIdx, unsigned flags )
  415. {
  416. if( devIdx == cmInvalidIdx )
  417. return false;
  418. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  419. return cmIsFlag(_cmApBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags);
  420. }
  421. void cmApBufEnableChannel( unsigned devIdx, unsigned chIdx, unsigned flags )
  422. { cmApBufSetFlag(devIdx,chIdx,flags | kChApFl); }
  423. bool cmApBufIsChannelEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  424. { return cmApBufIsFlag(devIdx, chIdx, flags | kChApFl); }
  425. void cmApBufEnableTone( unsigned devIdx, unsigned chIdx, unsigned flags )
  426. { cmApBufSetFlag(devIdx,chIdx,flags | kToneApFl); }
  427. bool cmApBufIsToneEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  428. { return cmApBufIsFlag(devIdx,chIdx,flags | kToneApFl); }
  429. void cmApBufEnableMute( unsigned devIdx, unsigned chIdx, unsigned flags )
  430. { cmApBufSetFlag(devIdx,chIdx,flags | kMuteApFl); }
  431. bool cmApBufIsMuteEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  432. { return cmApBufIsFlag(devIdx,chIdx,flags | kMuteApFl); }
  433. void cmApBufEnablePass( unsigned devIdx, unsigned chIdx, unsigned flags )
  434. { cmApBufSetFlag(devIdx,chIdx,flags | kPassApFl); }
  435. bool cmApBufIsPassEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  436. { return cmApBufIsFlag(devIdx,chIdx,flags | kPassApFl); }
  437. void cmApBufEnableMeter( unsigned devIdx, unsigned chIdx, unsigned flags )
  438. { cmApBufSetFlag(devIdx,chIdx,flags | kMeterApFl); }
  439. bool cmApBufIsMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags )
  440. { return cmApBufIsFlag(devIdx,chIdx,flags | kMeterApFl); }
  441. cmApSample_t cmApBufMeter(unsigned devIdx, unsigned chIdx, unsigned flags )
  442. {
  443. if( devIdx == cmInvalidIdx )
  444. return 0;
  445. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  446. const cmApCh* cp = _cmApBuf.devArray[devIdx].ioArray[idx].chArray + chIdx;
  447. return _cmApMeterValue(cp);
  448. }
  449. void cmApBufSetGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain )
  450. {
  451. if( devIdx == cmInvalidIdx )
  452. return;
  453. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  454. unsigned i = chIdx != -1 ? chIdx : 0;
  455. unsigned n = i + (chIdx != -1 ? 1 : _cmApBuf.devArray[devIdx].ioArray[idx].chCnt);
  456. for(; i<n; ++i)
  457. _cmApBuf.devArray[devIdx].ioArray[idx].chArray[i].gain = gain;
  458. }
  459. double cmApBufGain( unsigned devIdx, unsigned chIdx, unsigned flags )
  460. {
  461. if( devIdx == cmInvalidIdx )
  462. return 0;
  463. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  464. return _cmApBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].gain;
  465. }
  466. unsigned cmApBufGetStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr )
  467. {
  468. if( devIdx == cmInvalidIdx )
  469. return 0;
  470. unsigned ioIdx = cmIsFlag(flags,kInApFl) ? kInApIdx : kOutApIdx;
  471. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + ioIdx;
  472. unsigned chCnt = cmMin(iop->chCnt, meterCnt );
  473. unsigned i;
  474. if( faultCntPtr != NULL )
  475. *faultCntPtr = iop->faultCnt;
  476. for(i=0; i<chCnt; ++i)
  477. meterArray[i] = _cmApMeterValue(iop->chArray + i);
  478. return chCnt;
  479. }
  480. bool cmApBufIsDeviceReady( unsigned devIdx, unsigned flags )
  481. {
  482. //bool iFl = true;
  483. //bool oFl = true;
  484. unsigned i = 0;
  485. if( devIdx == cmInvalidIdx )
  486. return false;
  487. if( flags & kInApFl )
  488. {
  489. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  490. for(i=0; i<ioPtr->chCnt; ++i)
  491. if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt )
  492. return false;
  493. //iFl = ioPtr->fn > ioPtr->dspFrameCnt;
  494. }
  495. if( flags & kOutApFl )
  496. {
  497. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  498. for(i=0; i<ioPtr->chCnt; ++i)
  499. if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt )
  500. return false;
  501. //oFl = (ioPtr->n - ioPtr->fn) > ioPtr->dspFrameCnt;
  502. }
  503. return true;
  504. //return iFl & oFl;
  505. }
  506. // Note that his function returns audio samples but does NOT
  507. // change any internal states.
  508. void cmApBufGet( unsigned devIdx, unsigned flags, cmApSample_t* bufArray[], unsigned bufChCnt )
  509. {
  510. unsigned i;
  511. if( devIdx == cmInvalidIdx )
  512. {
  513. for(i=0; i<bufChCnt; ++i)
  514. bufArray[i] = NULL;
  515. return;
  516. }
  517. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  518. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + idx;
  519. unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt;
  520. //unsigned offs = flags & kInApFl ? ioPtr->oi : ioPtr->ii;
  521. cmApCh* cp = ioPtr->chArray;
  522. for(i=0; i<n; ++i,++cp)
  523. {
  524. unsigned offs = flags & kInApFl ? cp->oi : cp->ii;
  525. bufArray[i] = cmIsFlag(cp->fl,kChApFl) ? cp->b + offs : NULL;
  526. }
  527. }
  528. void _cmApBufCalcTimeStamp( double srate, const cmTimeSpec_t* baseTimeStamp, unsigned frmCnt, cmTimeSpec_t* retTimeStamp )
  529. {
  530. if( retTimeStamp==NULL )
  531. return;
  532. double secs = frmCnt / srate;
  533. unsigned int_secs = floor(secs);
  534. double frac_secs = secs - int_secs;
  535. retTimeStamp->tv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000);
  536. retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs;
  537. if( retTimeStamp->tv_nsec > 1000000000 )
  538. {
  539. retTimeStamp->tv_nsec -= 1000000000;
  540. retTimeStamp->tv_sec += 1;
  541. }
  542. }
  543. void cmApBufGetIO( unsigned iDevIdx, cmApSample_t* iBufArray[], unsigned iBufChCnt, cmTimeSpec_t* iTimeStampPtr, unsigned oDevIdx, cmApSample_t* oBufArray[], unsigned oBufChCnt, cmTimeSpec_t* oTimeStampPtr )
  544. {
  545. cmApBufGet( iDevIdx, kInApFl, iBufArray, iBufChCnt );
  546. cmApBufGet( oDevIdx, kOutApFl,oBufArray, oBufChCnt );
  547. unsigned i = 0;
  548. if( iDevIdx != cmInvalidIdx && oDevIdx != cmInvalidIdx )
  549. {
  550. const cmApIO* ip = _cmApBuf.devArray[iDevIdx].ioArray + kInApIdx;
  551. const cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  552. unsigned minChCnt = cmMin(iBufChCnt,oBufChCnt);
  553. unsigned frmCnt = cmMin(ip->dspFrameCnt,op->dspFrameCnt);
  554. unsigned byteCnt = frmCnt * sizeof(cmApSample_t);
  555. _cmApBufCalcTimeStamp(ip->srate, &ip->timeStamp, ip->ioFrameCnt, iTimeStampPtr );
  556. _cmApBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
  557. for(i=0; i<minChCnt; ++i)
  558. {
  559. cmApCh* ocp = op->chArray + i;
  560. cmApCh* icp = ip->chArray + i;
  561. if( oBufArray[i] != NULL )
  562. {
  563. // if either the input or output channel is marked for pass-through
  564. if( cmAllFlags(ocp->fl,kPassApFl) || cmAllFlags(icp->fl,kPassApFl) )
  565. {
  566. memcpy( oBufArray[i], iBufArray[i], byteCnt );
  567. // set the output buffer to NULL to prevent it being over written by the client
  568. oBufArray[i] = NULL;
  569. }
  570. else
  571. {
  572. // zero the output buffer
  573. memset(oBufArray[i],0,byteCnt);
  574. }
  575. }
  576. }
  577. }
  578. if( oDevIdx != cmInvalidIdx )
  579. {
  580. const cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  581. unsigned byteCnt = op->dspFrameCnt * sizeof(cmApSample_t);
  582. _cmApBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
  583. for(; i<oBufChCnt; ++i)
  584. if( oBufArray[i] != NULL )
  585. memset( oBufArray[i], 0, byteCnt );
  586. }
  587. }
  588. void cmApBufAdvance( unsigned devIdx, unsigned flags )
  589. {
  590. unsigned i;
  591. if( devIdx == cmInvalidIdx )
  592. return;
  593. if( flags & kInApFl )
  594. {
  595. cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  596. for(i=0; i<ioPtr->chCnt; ++i)
  597. {
  598. cmApCh* cp = ioPtr->chArray + i;
  599. cp->oi = (cp->oi + ioPtr->dspFrameCnt) % ioPtr->n;
  600. cmThUIntDecr(&cp->fn,ioPtr->dspFrameCnt);
  601. }
  602. // count the number of samples input from this device
  603. if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
  604. cmThUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
  605. }
  606. if( flags & kOutApFl )
  607. {
  608. cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  609. for(i=0; i<ioPtr->chCnt; ++i)
  610. {
  611. cmApCh* cp = ioPtr->chArray + i;
  612. cp->ii = (cp->ii + ioPtr->dspFrameCnt) % ioPtr->n;
  613. cmThUIntIncr(&cp->fn,ioPtr->dspFrameCnt);
  614. }
  615. // count the number of samples output from this device
  616. if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
  617. cmThUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
  618. }
  619. }
  620. void cmApBufInputToOutput( unsigned iDevIdx, unsigned oDevIdx )
  621. {
  622. if( iDevIdx == cmInvalidIdx || oDevIdx == cmInvalidIdx )
  623. return;
  624. unsigned iChCnt = cmApBufChannelCount( iDevIdx, kInApFl );
  625. unsigned oChCnt = cmApBufChannelCount( oDevIdx, kOutApFl );
  626. unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt;
  627. unsigned i;
  628. cmApSample_t* iBufPtrArray[ iChCnt ];
  629. cmApSample_t* oBufPtrArray[ oChCnt ];
  630. while( cmApBufIsDeviceReady( iDevIdx, kInApFl ) && cmApBufIsDeviceReady( oDevIdx, kOutApFl ) )
  631. {
  632. cmApBufGet( iDevIdx, kInApFl, iBufPtrArray, iChCnt );
  633. cmApBufGet( oDevIdx, kOutApFl, oBufPtrArray, oChCnt );
  634. // Warning: buffer pointers to disabled channels will be set to NULL
  635. for(i=0; i<chCnt; ++i)
  636. {
  637. cmApIO* ip = _cmApBuf.devArray[iDevIdx ].ioArray + kInApIdx;
  638. cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  639. assert( ip->dspFrameCnt == op->dspFrameCnt );
  640. unsigned byteCnt = ip->dspFrameCnt * sizeof(cmApSample_t);
  641. if( oBufPtrArray[i] != NULL )
  642. {
  643. // the input channel is not disabled
  644. if( iBufPtrArray[i]!=NULL )
  645. memcpy(oBufPtrArray[i],iBufPtrArray[i],byteCnt);
  646. else
  647. // the input channel is disabled but the output is not - so fill the output with zeros
  648. memset(oBufPtrArray[i],0,byteCnt);
  649. }
  650. }
  651. cmApBufAdvance( iDevIdx, kInApFl );
  652. cmApBufAdvance( oDevIdx, kOutApFl );
  653. }
  654. }
  655. void cmApBufReport( cmRpt_t* rpt )
  656. {
  657. unsigned i,j,k;
  658. for(i=0; i<_cmApBuf.devCnt; ++i)
  659. {
  660. cmRptPrintf(rpt,"%i ",i);
  661. for(j=0; j<kIoApCnt; ++j)
  662. {
  663. cmApIO* ip = _cmApBuf.devArray[i].ioArray + j;
  664. unsigned ii = 0;
  665. unsigned oi = 0;
  666. unsigned fn = 0;
  667. cmApSample_t mtr = 0;
  668. for(k=0; k<ip->chCnt; ++k)
  669. {
  670. cmApCh* cp = ip->chArray + i;
  671. ii += cp->ii;
  672. oi += cp->oi;
  673. fn += cp->fn;
  674. mtr += _cmApMeterValue(cp);
  675. }
  676. cmRptPrintf(rpt,"%s - i:%7i o:%7i f:%7i n:%7i err %s:%7i mtr:%5.4f ",
  677. j==0?"IN":"OUT",
  678. ii,oi,fn,ip->n, (j==0?"over":"under"), ip->faultCnt, mtr);
  679. }
  680. cmRptPrintf(rpt,"\n");
  681. }
  682. }
  683. /// [cmApBufExample]
  684. void cmApBufTest( cmRpt_t* rpt )
  685. {
  686. unsigned devIdx = 0;
  687. unsigned devCnt = 1 ;
  688. unsigned dspFrameCnt = 10;
  689. unsigned cycleCnt = 3;
  690. unsigned framesPerCycle = 25;
  691. unsigned inChCnt = 2;
  692. unsigned outChCnt = inChCnt;
  693. unsigned sigN = cycleCnt*framesPerCycle*inChCnt;
  694. double srate = 44100.0;
  695. unsigned meterMs = 50;
  696. unsigned bufChCnt= inChCnt;
  697. cmApSample_t* inBufArray[ bufChCnt ];
  698. cmApSample_t* outBufArray[ bufChCnt ];
  699. cmApSample_t iSig[ sigN ];
  700. cmApSample_t oSig[ sigN ];
  701. cmApSample_t* os = oSig;
  702. cmApAudioPacket_t pkt;
  703. int i,j;
  704. // create a simulated signal
  705. for(i=0; i<sigN; ++i)
  706. {
  707. iSig[i] = i;
  708. oSig[i] = 0;
  709. }
  710. pkt.devIdx = 0;
  711. pkt.begChIdx = 0;
  712. pkt.chCnt = inChCnt;
  713. pkt.audioFramesCnt = framesPerCycle;
  714. pkt.bitsPerSample = 32;
  715. pkt.flags = 0;
  716. // initialize a the audio buffer
  717. cmApBufInitialize(devCnt,meterMs);
  718. // setup the buffer with the specific parameters to by used by the host audio ports
  719. cmApBufSetup(devIdx,srate,dspFrameCnt,cycleCnt,inChCnt,framesPerCycle,outChCnt,framesPerCycle);
  720. // simulate cylcing through sigN buffer transactions
  721. for(i=0; i<sigN; i+=framesPerCycle*inChCnt)
  722. {
  723. // setup an incoming audio packet
  724. pkt.audioFramesCnt = framesPerCycle;
  725. pkt.audioBytesPtr = iSig+i;
  726. // simulate a call from the audio port with incoming samples
  727. // (fill the audio buffers internal input buffers)
  728. cmApBufUpdate(&pkt,1,NULL,0);
  729. // if all devices need to be serviced
  730. while( cmApBufIsDeviceReady( devIdx, kInApFl | kOutApFl ))
  731. {
  732. // get pointers to full internal input buffers
  733. cmApBufGet(devIdx, kInApFl, inBufArray, bufChCnt );
  734. // get pointers to empty internal output buffers
  735. cmApBufGet(devIdx, kOutApFl, outBufArray, bufChCnt );
  736. // Warning: pointers to disabled channels will be set to NULL.
  737. // simulate a play through by copying the incoming samples to the outgoing buffers.
  738. for(j=0; j<bufChCnt; ++j)
  739. if( outBufArray[j] != NULL )
  740. {
  741. // if the input is disabled - but the output is not then zero the output buffer
  742. if( inBufArray[j] == NULL )
  743. memset( outBufArray[j], 0, dspFrameCnt*sizeof(cmApSample_t));
  744. else
  745. // copy the input to the output
  746. memcpy( outBufArray[j], inBufArray[j], dspFrameCnt*sizeof(cmApSample_t));
  747. }
  748. // signal the buffer that this cycle is complete.
  749. // (marks current internal input/output buffer empty/full)
  750. cmApBufAdvance( devIdx, kInApFl | kOutApFl );
  751. }
  752. pkt.audioBytesPtr = os;
  753. // simulate a call from the audio port picking up outgoing samples
  754. // (empties the audio buffers internal output buffers)
  755. cmApBufUpdate(NULL,0,&pkt,1);
  756. os += pkt.audioFramesCnt * pkt.chCnt;
  757. }
  758. for(i=0; i<sigN; ++i)
  759. cmRptPrintf(rpt,"%f ",oSig[i]);
  760. cmRptPrintf(rpt,"\n");
  761. cmApBufFinalize();
  762. }
  763. /// [cmApBufExample]