libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmApBuf.c 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. cmApSample_t s0; // buffered sample used for srate conversion
  53. } cmApCh;
  54. typedef struct
  55. {
  56. unsigned chCnt; // Count of channels
  57. cmApCh* chArray; // chArray[chCnt] channel record array
  58. unsigned n; // Length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle
  59. double srate; // Device sample rate;
  60. int srateMult; // Internal sample rate multiplier (negative values for dividing). This srateMult*srate is the sample rate of
  61. // signals held in the chApCh[] input and output buffers. Sample rate conversion to/from this rate
  62. // occurs in cmApBufUpdate() as signals go from/to the audio device.
  63. unsigned faultCnt; // error count since start
  64. unsigned framesPerCycle; // expected count of frames per channel to/from the audio device on each call to cmApBufUpdate()
  65. unsigned dspFrameCnt; // number of frames per channel in buffers returned by cmApBufGet().
  66. cmTimeSpec_t timeStamp; // base (starting) time stamp for this device
  67. unsigned ioFrameCnt; // count of frames input or output for this device
  68. } cmApIO;
  69. typedef struct
  70. {
  71. // ioArray[] always contains 2 elements - one for input the other for output.
  72. cmApIO ioArray[kIoApCnt];
  73. } cmApDev;
  74. typedef struct
  75. {
  76. cmApDev* devArray;
  77. unsigned devCnt;
  78. unsigned meterMs;
  79. cmApSample_t* zeroBuf; // buffer of zeros
  80. unsigned zeroBufCnt; // max of all dspFrameCnt for all devices.
  81. unsigned abufIdx;
  82. cmApSample_t abuf[ 16384 ];
  83. } cmApBuf;
  84. cmApBuf _cmApBuf;
  85. // Copy the source channel (srcChIdx) to the destination buffer and apply up-sampling.
  86. // 'src' is an interleaved buffer with 'srcN' samples per channel and 'srcChN' channels (total size in samples = srcN*srcChN)
  87. // 'dst' is a non-interleaved (single channel) circular buffer of length 'dstN' where 'dstIdx' is the index of the first dst slot to receive a sample..
  88. // Return the index into dst[] of the next location to receive an incoming sample.
  89. unsigned _cmApCopyInUpSample( const cmApSample_t* src, unsigned srcN, unsigned srcChN, unsigned srcChIdx, cmApSample_t* dst, unsigned dstN, unsigned dstIdx, unsigned mult, double gain, cmApSample_t* s0_Ref )
  90. {
  91. cmApSample_t s0 = *s0_Ref;
  92. unsigned si,k,di=dstIdx;
  93. for(si=0; si<srcN; ++si)
  94. {
  95. cmApSample_t s1 = src[si*srcChN+srcChIdx];
  96. for(k=1; k<mult; ++k)
  97. {
  98. dst[di] = gain * (s0 + (s1 - s0) * k / mult );
  99. di = (di+1) % dstN;
  100. }
  101. dst[di] = gain * s1;
  102. di = (di+1) % dstN;
  103. s0 = s1;
  104. }
  105. *s0_Ref = s0;
  106. return di;
  107. }
  108. // Copy the source channel (srcChIdx) to the destination buffer and apply down-sampling.
  109. // 'src' is an interleaved linear buffer with 'srcN' samples per channel and 'srcChN' channels,
  110. // 'dst' is a non-interleaved circular buffer of length 'dstN' where 'dstIdx' is the index of the first dst slot to receive a sample..
  111. // Return the index into dst[] of the next location to receive an incoming sample.
  112. unsigned _cmApCopyInDnSample( const cmApSample_t* src, unsigned srcN, unsigned srcChN, unsigned srcChIdx, cmApSample_t* dst, unsigned dstN, unsigned dstIdx, unsigned div, double gain )
  113. {
  114. unsigned si,di=dstIdx;
  115. for(si=0; si<srcN; si+=div)
  116. {
  117. dst[di] = gain * src[si*srcChN+srcChIdx];
  118. di = (di+1) % dstN;
  119. }
  120. return di;
  121. }
  122. // Copy samples from an interleaved src buffer to a non-interleaved dst buffer with sample rate conversion
  123. unsigned _cmApCopyInSamples( const cmApSample_t* src, unsigned srcN, unsigned srcChN, unsigned srcChIdx, cmApSample_t* dst, unsigned dstN, unsigned dstIdx, int srateMult, double gain, cmApSample_t* s0_Ref )
  124. {
  125. if( srateMult < 1 )
  126. return _cmApCopyInDnSample( src, srcN, srcChN, srcChIdx, dst, dstN, dstIdx, -srateMult, gain );
  127. return _cmApCopyInUpSample( src, srcN, srcChN, srcChIdx, dst, dstN, dstIdx, srateMult, gain, s0_Ref );
  128. }
  129. void printBuf( const cmApSample_t* src, unsigned srcN, unsigned bi, unsigned n )
  130. {
  131. unsigned i,j;
  132. for(i=bi,j=0; j<n; ++i,++j)
  133. {
  134. i = i % srcN;
  135. printf("(%i,%i,%f),\n",j,i,src[i]);
  136. }
  137. printf("-----\n");
  138. }
  139. // Copy
  140. // 'src' is a non-interleaved circular buf of total length 'srcN', with the first sample coming at 'srcIdx'.
  141. // 'dst' is an interleaved buffer of length 'dstN' with 'dstChN' channels
  142. // Return the index of the next src sample.
  143. unsigned _cmApCopyOutUpSample( const cmApSample_t* src, unsigned srcN, unsigned srcIdx, cmApSample_t* dst, unsigned dstN, unsigned dstChN, unsigned dstChIdx, int mult, double gain, cmApSample_t* s0_Ref )
  144. {
  145. unsigned di,si=srcIdx;
  146. cmApSample_t s0 = *s0_Ref;
  147. for(di=0; di<dstN; ++di)
  148. {
  149. cmApSample_t s1 = src[si];
  150. unsigned k;
  151. for(k=1; k<mult && di<dstN; ++di,++k)
  152. {
  153. dst[di*dstChN+dstChIdx] = gain * (s0 + (s1 - s0) * k / mult );
  154. }
  155. if( di < dstN )
  156. {
  157. dst[di*dstChN+dstChIdx] = gain * s1;
  158. ++di;
  159. }
  160. si = (si + 1) % srcN;
  161. s0 = s1;
  162. }
  163. *s0_Ref = s0;
  164. return si;
  165. }
  166. // 'src' is a non-interleaved (single channel) circular buf of total length 'srcN', with the first sample coming at 'srcIdx'.
  167. // 'dst' is an interleaved buffer of length 'dstN' with 'dstChN' channels
  168. // Return the index of the next src sample.
  169. unsigned _cmApCopyOutDnSample( const cmApSample_t* src, unsigned srcN, unsigned srcIdx, cmApSample_t* dst, unsigned dstN, unsigned dstChN, unsigned dstChIdx, int div, double gain )
  170. {
  171. unsigned di,si;
  172. // The total count of output samples is determined by 'dstN'
  173. // Downsampling is acheived by advancing the src index by 'div' samples.
  174. for(di=0,si=srcIdx; di<dstN; ++di)
  175. {
  176. dst[di*dstChN+dstChIdx] = gain * src[si];
  177. si = (si + div) % srcN;
  178. }
  179. return si;
  180. }
  181. // Copy samples from a non-interleaved src buffer to an interleaved dst buffer with sample rate conversion
  182. unsigned _cmApCopyOutSamples( const cmApSample_t* src, unsigned srcN, unsigned srcIdx, cmApSample_t* dst, unsigned dstN, unsigned dstChN, unsigned dstChIdx, int srateMult, double gain, cmApSample_t* s0_Ref )
  183. {
  184. if( srateMult > 0 )
  185. return _cmApCopyOutDnSample(src, srcN, srcIdx, dst, dstN, dstChN, dstChIdx, srateMult, gain );
  186. return _cmApCopyOutUpSample( src, srcN, srcIdx, dst, dstN, dstChN, dstChIdx, -srateMult, gain, s0_Ref );
  187. }
  188. cmApSample_t _cmApMeterValue( const cmApCh* cp )
  189. {
  190. double sum = 0;
  191. unsigned i;
  192. for(i=0; i<cp->mn; ++i)
  193. sum += cp->m[i];
  194. return cp->mn==0 ? 0 : (cmApSample_t)sqrt(sum/cp->mn);
  195. }
  196. void _cmApSine0( cmApCh* cp, cmApSample_t* b0, unsigned n0, cmApSample_t* b1, unsigned n1, unsigned stride, float srate )
  197. {
  198. unsigned i;
  199. for(i=0; i<n0; ++i,++cp->phs)
  200. b0[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
  201. for(i=0; i<n1; ++i,++cp->phs)
  202. b1[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
  203. }
  204. // Synthesize a sine signal of length sigN*srateMult starting at dst[dstidx]
  205. // Assume dst[dstN] is a circular buffer
  206. unsigned _cmApSine( cmApCh* cp, cmApSample_t* dst, unsigned dstN, unsigned dstIdx, unsigned dstChCnt, unsigned sigN, unsigned srateMult, float srate, double gain )
  207. {
  208. unsigned i,di;
  209. sigN = srateMult < 0 ? sigN/abs(srateMult) : sigN * srateMult;
  210. for(i=0, di=dstIdx; i<sigN; ++i,++cp->phs)
  211. {
  212. dst[di] = (cmApSample_t)(gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
  213. di = (di+dstChCnt) % dstN;
  214. }
  215. return sigN;
  216. }
  217. cmApSample_t _cmApMeter( const cmApSample_t* b, unsigned bn, unsigned stride )
  218. {
  219. const cmApSample_t* ep = b + bn;
  220. cmApSample_t sum = 0;
  221. for(; b<ep; b+=stride)
  222. sum += *b * *b;
  223. return sum / bn;
  224. }
  225. void _cmApChFinalize( cmApCh* chPtr )
  226. {
  227. cmMemPtrFree( &chPtr->b );
  228. cmMemPtrFree( &chPtr->m );
  229. }
  230. // n=buf sample cnt mn=meter buf smp cnt
  231. void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn )
  232. {
  233. _cmApChFinalize(chPtr);
  234. chPtr->b = n==0 ? NULL : cmMemAllocZ( cmApSample_t, n );
  235. chPtr->ii = 0;
  236. chPtr->oi = 0;
  237. chPtr->fn = 0;
  238. chPtr->fl = (n!=0 ? kChApFl : 0);
  239. chPtr->hz = 1000;
  240. chPtr->gain = 1.0;
  241. chPtr->mn = mn;
  242. chPtr->m = cmMemAllocZ(cmApSample_t,mn);
  243. chPtr->mi = 0;
  244. }
  245. void _cmApIoFinalize( cmApIO* ioPtr )
  246. {
  247. unsigned i;
  248. for(i=0; i<ioPtr->chCnt; ++i)
  249. _cmApChFinalize( ioPtr->chArray + i );
  250. cmMemPtrFree(&ioPtr->chArray);
  251. ioPtr->chCnt = 0;
  252. ioPtr->n = 0;
  253. }
  254. void _cmApIoInitialize( cmApIO* ioPtr, double srate, unsigned framesPerCycle, unsigned chCnt, unsigned n, unsigned meterBufN, unsigned dspFrameCnt, int srateMult )
  255. {
  256. unsigned i;
  257. if( srateMult == 0 )
  258. srateMult = 1;
  259. _cmApIoFinalize(ioPtr);
  260. n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt
  261. ioPtr->chArray = chCnt==0 ? NULL : cmMemAllocZ( cmApCh, chCnt );
  262. ioPtr->chCnt = chCnt;
  263. ioPtr->n = srateMult<0 ? n : n*srateMult;
  264. ioPtr->faultCnt = 0;
  265. ioPtr->framesPerCycle = framesPerCycle;
  266. ioPtr->srate = srate;
  267. ioPtr->srateMult = srateMult;
  268. ioPtr->dspFrameCnt = dspFrameCnt;
  269. ioPtr->timeStamp.tv_sec = 0;
  270. ioPtr->timeStamp.tv_nsec = 0;
  271. ioPtr->ioFrameCnt = 0;
  272. for(i=0; i<chCnt; ++i )
  273. _cmApChInitialize( ioPtr->chArray + i, ioPtr->n, meterBufN );
  274. }
  275. void _cmApDevFinalize( cmApDev* dp )
  276. {
  277. unsigned i;
  278. for(i=0; i<kIoApCnt; ++i)
  279. _cmApIoFinalize( dp->ioArray+i);
  280. }
  281. void _cmApDevInitialize( cmApDev* dp, double srate, unsigned iFpC, unsigned iChCnt, unsigned iBufN, unsigned oFpC, unsigned oChCnt, unsigned oBufN, unsigned meterBufN, unsigned dspFrameCnt, int srateMult )
  282. {
  283. unsigned i;
  284. _cmApDevFinalize(dp);
  285. for(i=0; i<kIoApCnt; ++i)
  286. {
  287. unsigned chCnt = i==kInApIdx ? iChCnt : oChCnt;
  288. unsigned bufN = i==kInApIdx ? iBufN : oBufN;
  289. unsigned fpc = i==kInApIdx ? iFpC : oFpC;
  290. _cmApIoInitialize( dp->ioArray+i, srate, fpc, chCnt, bufN, meterBufN, dspFrameCnt, srateMult );
  291. }
  292. }
  293. cmAbRC_t cmApBufInitialize( unsigned devCnt, unsigned meterMs )
  294. {
  295. cmAbRC_t rc;
  296. if((rc = cmApBufFinalize()) != kOkAbRC )
  297. return rc;
  298. _cmApBuf.devArray = cmMemAllocZ( cmApDev, devCnt );
  299. _cmApBuf.devCnt = devCnt;
  300. cmApBufSetMeterMs(meterMs);
  301. return kOkAbRC;
  302. }
  303. cmAbRC_t cmApBufFinalize()
  304. {
  305. unsigned i;
  306. for(i=0; i<_cmApBuf.devCnt; ++i)
  307. _cmApDevFinalize(_cmApBuf.devArray + i);
  308. cmMemPtrFree( &_cmApBuf.devArray );
  309. cmMemPtrFree( &_cmApBuf.zeroBuf );
  310. _cmApBuf.devCnt = 0;
  311. FILE* fp;
  312. if(_cmApBuf.abufIdx>0 )
  313. {
  314. if((fp = fopen("/home/kevin/temp/temp.csv","wt")) != NULL )
  315. {
  316. int i;
  317. for(i=0; i<_cmApBuf.abufIdx; ++i)
  318. {
  319. fprintf(fp,"%f,\n",_cmApBuf.abuf[i]);
  320. }
  321. fclose(fp);
  322. }
  323. }
  324. return kOkAbRC;
  325. }
  326. cmAbRC_t cmApBufSetup(
  327. unsigned devIdx,
  328. double srate,
  329. unsigned dspFrameCnt,
  330. unsigned bufCnt,
  331. unsigned inChCnt,
  332. unsigned inFramesPerCycle,
  333. unsigned outChCnt,
  334. unsigned outFramesPerCycle,
  335. int srateMult)
  336. {
  337. cmApDev* devPtr = _cmApBuf.devArray + devIdx;
  338. unsigned iBufN = bufCnt * inFramesPerCycle;
  339. unsigned oBufN = bufCnt * outFramesPerCycle;
  340. unsigned meterBufN = cmMax(1,floor(srate * _cmApBuf.meterMs / (1000.0 * outFramesPerCycle)));
  341. _cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt, srateMult );
  342. if( inFramesPerCycle > _cmApBuf.zeroBufCnt || outFramesPerCycle > _cmApBuf.zeroBufCnt )
  343. {
  344. _cmApBuf.zeroBufCnt = cmMax(inFramesPerCycle,outFramesPerCycle);
  345. _cmApBuf.zeroBuf = cmMemResizeZ(cmApSample_t,_cmApBuf.zeroBuf,_cmApBuf.zeroBufCnt);
  346. }
  347. return kOkAbRC;
  348. }
  349. cmAbRC_t cmApBufPrimeOutput( unsigned devIdx, unsigned audioCycleCnt )
  350. {
  351. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  352. unsigned i;
  353. for(i=0; i<iop->chCnt; ++i)
  354. {
  355. cmApCh* cp = iop->chArray + i;
  356. unsigned bn = iop->n * sizeof(cmApSample_t);
  357. memset(cp->b,0,bn);
  358. cp->oi = 0;
  359. cp->ii = iop->framesPerCycle * audioCycleCnt;
  360. cp->fn = iop->framesPerCycle * audioCycleCnt;
  361. }
  362. return kOkAbRC;
  363. }
  364. void cmApBufOnPortEnable( unsigned devIdx, bool enableFl )
  365. {
  366. if( devIdx == cmInvalidIdx || enableFl==false)
  367. return;
  368. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  369. iop->timeStamp.tv_sec = 0;
  370. iop->timeStamp.tv_nsec = 0;
  371. iop->ioFrameCnt = 0;
  372. iop = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  373. iop->timeStamp.tv_sec = 0;
  374. iop->timeStamp.tv_nsec = 0;
  375. iop->ioFrameCnt = 0;
  376. }
  377. cmAbRC_t cmApBufUpdate(
  378. cmApAudioPacket_t* inPktArray,
  379. unsigned inPktCnt,
  380. cmApAudioPacket_t* outPktArray,
  381. unsigned outPktCnt )
  382. {
  383. unsigned i,j;
  384. // copy samples from the packet to the buffer
  385. if( inPktArray != NULL )
  386. {
  387. for(i=0; i<inPktCnt; ++i)
  388. {
  389. cmApAudioPacket_t* pp = inPktArray + i;
  390. cmApIO* ip = _cmApBuf.devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd
  391. // if the base time stamp has not yet been set - then set it
  392. if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 )
  393. ip->timeStamp = pp->timeStamp;
  394. // for each source packet channel and enabled dest channel
  395. for(j=0; j<pp->chCnt; ++j)
  396. {
  397. cmApCh* cp = ip->chArray + pp->begChIdx +j; // dest ch
  398. assert(pp->begChIdx + j < ip->chCnt );
  399. // if the incoming samples would overflow the buffer then ignore them
  400. if( cp->fn + pp->audioFramesCnt > ip->n )
  401. {
  402. ++ip->faultCnt; // record input overflow
  403. continue;
  404. }
  405. // if the incoming samples would go off the end of the buffer then
  406. // copy in the samples in two segments (one at the end and another at begin of dest channel)
  407. bool enaFl = cmIsFlag(cp->fl,kChApFl) && cmIsFlag(cp->fl,kMuteApFl)==false;
  408. const cmApSample_t* sp = enaFl ? ((cmApSample_t*)pp->audioBytesPtr) + j : _cmApBuf.zeroBuf;
  409. //unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved)
  410. //cmApSample_t* dp = cp->b + cp->ii;
  411. //const cmApSample_t* ep = dp + n0;
  412. // update the meter
  413. if( cmIsFlag(cp->fl,kMeterApFl) )
  414. {
  415. cp->m[cp->mi] = _cmApMeter(sp,pp->audioFramesCnt,pp->chCnt);
  416. cp->mi = (cp->mi + 1) % cp->mn;
  417. }
  418. unsigned incrSmpN = 0;
  419. // if the test tone is enabled on this input channel
  420. if( enaFl && cmIsFlag(cp->fl,kToneApFl) )
  421. {
  422. //_cmApSine(cp, dp, n0, cp->b, n1, 1, ip->srate );
  423. incrSmpN = _cmApSine( cp, cp->b, ip->n, cp->ii, 1, pp->audioFramesCnt, ip->srateMult, ip->srate, cp->gain );
  424. }
  425. else // otherwise copy samples from the packet to the buffer
  426. {
  427. unsigned pi = cp->ii;
  428. cp->ii = _cmApCopyInSamples( (cmApSample_t*)pp->audioBytesPtr, pp->audioFramesCnt, pp->chCnt, j, cp->b, ip->n, cp->ii, ip->srateMult, cp->gain, &cp->s0 );
  429. if( false )
  430. if( j == 2 && _cmApBuf.abufIdx < 16384 )
  431. {
  432. int ii;
  433. for(ii=pi; ii!=cp->ii && _cmApBuf.abufIdx<16384; _cmApBuf.abufIdx++)
  434. {
  435. _cmApBuf.abuf[ _cmApBuf.abufIdx ] = cp->b[ii];
  436. ii = (ii + 1) % ip->n;
  437. }
  438. }
  439. incrSmpN = cp->ii > pi ? cp->ii-pi : (ip->n-pi) + cp->ii;
  440. }
  441. cmThUIntIncr(&cp->fn,incrSmpN);
  442. }
  443. }
  444. }
  445. // copy samples from the buffer to the packet
  446. if( outPktArray != NULL )
  447. {
  448. for(i=0; i<outPktCnt; ++i)
  449. {
  450. cmApAudioPacket_t* pp = outPktArray + i;
  451. cmApIO* op = _cmApBuf.devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd
  452. // if the base timestamp has not yet been set then set it.
  453. if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 )
  454. op->timeStamp = pp->timeStamp;
  455. // for each dest packet channel and enabled source channel
  456. for(j=0; j<pp->chCnt; ++j)
  457. {
  458. cmApCh* cp = op->chArray + pp->begChIdx + j; // dest ch
  459. unsigned n0 = op->n - cp->oi; // first src segment
  460. unsigned n1 = 0; // second src segment
  461. volatile unsigned fn = cp->fn; // store fn because it may be changed by the client thread
  462. // if the outgoing samples will underflow the buffer
  463. if( pp->audioFramesCnt > fn )
  464. {
  465. ++op->faultCnt; // record an output underflow
  466. // if the buffer is empty - zero the packet and return
  467. if( fn == 0 )
  468. {
  469. memset( pp->audioBytesPtr, 0, pp->audioFramesCnt*sizeof(cmApSample_t));
  470. continue;
  471. }
  472. // ... otherwise decrease the count of returned samples
  473. pp->audioFramesCnt = fn;
  474. }
  475. // if the outgong segments would go off the end of the buffer then
  476. // arrange to wrap to the begining of the buffer
  477. if( n0 < pp->audioFramesCnt )
  478. n1 = pp->audioFramesCnt-n0;
  479. else
  480. n0 = pp->audioFramesCnt;
  481. cmApSample_t* bpp = ((cmApSample_t*)pp->audioBytesPtr) + j;
  482. //cmApSample_t* dp = bpp;
  483. bool enaFl = cmIsFlag(cp->fl,kChApFl) && cmIsFlag(cp->fl,kMuteApFl)==false;
  484. unsigned decrSmpN = 0;
  485. // if the tone is enabled on this channel
  486. if( enaFl && cmIsFlag(cp->fl,kToneApFl) )
  487. {
  488. //_cmApSine(cp, dp, n0, dp + n0*pp->chCnt, n1, pp->chCnt, op->srate );
  489. decrSmpN = _cmApSine( cp, (cmApSample_t*)pp->audioBytesPtr, pp->audioFramesCnt, 0, pp->chCnt, pp->audioFramesCnt, op->srateMult, op->srate, cp->gain );
  490. }
  491. else // otherwise copy samples from the output buffer to the packet
  492. {
  493. const cmApSample_t* sp = enaFl ? cp->b + cp->oi : _cmApBuf.zeroBuf;
  494. //const cmApSample_t* ep = sp + n0;
  495. unsigned pi = cp->oi;
  496. cp->oi = _cmApCopyOutSamples( enaFl ? cp->b : _cmApBuf.zeroBuf, op->n, cp->oi, (cmApSample_t*)pp->audioBytesPtr, pp->audioFramesCnt, pp->chCnt, j, op->srateMult, cp->gain, &cp->s0 );
  497. decrSmpN = cp->oi>pi ? cp->oi-pi : (op->n-pi) + cp->oi;
  498. if( true )
  499. if( j == 2 && _cmApBuf.abufIdx < 16384 )
  500. {
  501. int ii;
  502. for(ii=pi; ii!=cp->oi && _cmApBuf.abufIdx<16384; _cmApBuf.abufIdx++)
  503. {
  504. _cmApBuf.abuf[ _cmApBuf.abufIdx ] = cp->b[ii];
  505. ii = (ii + 1) % op->n;
  506. }
  507. }
  508. /*
  509. // copy the first segment
  510. for(; sp < ep; dp += pp->chCnt )
  511. *dp = cp->gain * *sp++;
  512. // if there is a second segment
  513. if( n1 > 0 )
  514. {
  515. // copy the second segment
  516. sp = enaFl ? cp->b : _cmApBuf.zeroBuf;
  517. ep = sp + n1;
  518. for(; sp<ep; dp += pp->chCnt )
  519. *dp = cp->gain * *sp++;
  520. }
  521. */
  522. }
  523. // update the meter
  524. if( cmIsFlag(cp->fl,kMeterApFl) )
  525. {
  526. cp->m[cp->mi] = _cmApMeter(((cmApSample_t*)pp->audioBytesPtr)+j,pp->audioFramesCnt,pp->chCnt);
  527. cp->mi = (cp->mi + 1) % cp->mn;
  528. }
  529. // advance the output channel buffer
  530. /*
  531. cp->oi = n1>0 ? n1 : cp->oi + n0;
  532. cmThUIntDecr(&cp->fn,pp->audioFramesCnt);
  533. */
  534. //cp->fn -= pp->audioFramesCnt;
  535. cmThUIntDecr(&cp->fn,decrSmpN);
  536. }
  537. }
  538. }
  539. return kOkAbRC;
  540. }
  541. unsigned cmApBufMeterMs()
  542. { return _cmApBuf.meterMs; }
  543. void cmApBufSetMeterMs( unsigned meterMs )
  544. {
  545. _cmApBuf.meterMs = cmMin(1000,cmMax(10,meterMs));
  546. }
  547. unsigned cmApBufChannelCount( unsigned devIdx, unsigned flags )
  548. {
  549. if( devIdx == cmInvalidIdx )
  550. return 0;
  551. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  552. return _cmApBuf.devArray[devIdx].ioArray[ idx ].chCnt;
  553. }
  554. void cmApBufSetFlag( unsigned devIdx, unsigned chIdx, unsigned flags )
  555. {
  556. if( devIdx == cmInvalidIdx )
  557. return;
  558. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  559. bool enableFl = flags & kEnableApFl ? true : false;
  560. unsigned i = chIdx != -1 ? chIdx : 0;
  561. unsigned n = chIdx != -1 ? chIdx+1 : _cmApBuf.devArray[devIdx].ioArray[idx].chCnt;
  562. for(; i<n; ++i)
  563. {
  564. cmApCh* cp = _cmApBuf.devArray[devIdx].ioArray[idx].chArray + i;
  565. cp->fl = cmEnaFlag(cp->fl, flags & (kChApFl|kToneApFl|kMeterApFl|kMuteApFl|kPassApFl), enableFl );
  566. }
  567. }
  568. bool cmApBufIsFlag( unsigned devIdx, unsigned chIdx, unsigned flags )
  569. {
  570. if( devIdx == cmInvalidIdx )
  571. return false;
  572. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  573. return cmIsFlag(_cmApBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags);
  574. }
  575. void cmApBufEnableChannel( unsigned devIdx, unsigned chIdx, unsigned flags )
  576. { cmApBufSetFlag(devIdx,chIdx,flags | kChApFl); }
  577. bool cmApBufIsChannelEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  578. { return cmApBufIsFlag(devIdx, chIdx, flags | kChApFl); }
  579. void cmApBufEnableTone( unsigned devIdx, unsigned chIdx, unsigned flags )
  580. { cmApBufSetFlag(devIdx,chIdx,flags | kToneApFl); }
  581. bool cmApBufIsToneEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  582. { return cmApBufIsFlag(devIdx,chIdx,flags | kToneApFl); }
  583. void cmApBufEnableMute( unsigned devIdx, unsigned chIdx, unsigned flags )
  584. { cmApBufSetFlag(devIdx,chIdx,flags | kMuteApFl); }
  585. bool cmApBufIsMuteEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  586. { return cmApBufIsFlag(devIdx,chIdx,flags | kMuteApFl); }
  587. void cmApBufEnablePass( unsigned devIdx, unsigned chIdx, unsigned flags )
  588. { cmApBufSetFlag(devIdx,chIdx,flags | kPassApFl); }
  589. bool cmApBufIsPassEnabled( unsigned devIdx, unsigned chIdx, unsigned flags )
  590. { return cmApBufIsFlag(devIdx,chIdx,flags | kPassApFl); }
  591. void cmApBufEnableMeter( unsigned devIdx, unsigned chIdx, unsigned flags )
  592. { cmApBufSetFlag(devIdx,chIdx,flags | kMeterApFl); }
  593. bool cmApBufIsMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags )
  594. { return cmApBufIsFlag(devIdx,chIdx,flags | kMeterApFl); }
  595. cmApSample_t cmApBufMeter(unsigned devIdx, unsigned chIdx, unsigned flags )
  596. {
  597. if( devIdx == cmInvalidIdx )
  598. return 0;
  599. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  600. const cmApCh* cp = _cmApBuf.devArray[devIdx].ioArray[idx].chArray + chIdx;
  601. return _cmApMeterValue(cp);
  602. }
  603. void cmApBufSetGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain )
  604. {
  605. if( devIdx == cmInvalidIdx )
  606. return;
  607. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  608. unsigned i = chIdx != -1 ? chIdx : 0;
  609. unsigned n = i + (chIdx != -1 ? 1 : _cmApBuf.devArray[devIdx].ioArray[idx].chCnt);
  610. for(; i<n; ++i)
  611. _cmApBuf.devArray[devIdx].ioArray[idx].chArray[i].gain = gain;
  612. }
  613. double cmApBufGain( unsigned devIdx, unsigned chIdx, unsigned flags )
  614. {
  615. if( devIdx == cmInvalidIdx )
  616. return 0;
  617. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  618. return _cmApBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].gain;
  619. }
  620. unsigned cmApBufGetStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr )
  621. {
  622. if( devIdx == cmInvalidIdx )
  623. return 0;
  624. unsigned ioIdx = cmIsFlag(flags,kInApFl) ? kInApIdx : kOutApIdx;
  625. cmApIO* iop = _cmApBuf.devArray[devIdx].ioArray + ioIdx;
  626. unsigned chCnt = cmMin(iop->chCnt, meterCnt );
  627. unsigned i;
  628. if( faultCntPtr != NULL )
  629. *faultCntPtr = iop->faultCnt;
  630. for(i=0; i<chCnt; ++i)
  631. meterArray[i] = _cmApMeterValue(iop->chArray + i);
  632. return chCnt;
  633. }
  634. bool cmApBufIsDeviceReady( unsigned devIdx, unsigned flags )
  635. {
  636. //bool iFl = true;
  637. //bool oFl = true;
  638. unsigned i = 0;
  639. if( devIdx == cmInvalidIdx )
  640. return false;
  641. if( flags & kInApFl )
  642. {
  643. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  644. for(i=0; i<ioPtr->chCnt; ++i)
  645. if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt )
  646. return false;
  647. //iFl = ioPtr->fn > ioPtr->dspFrameCnt;
  648. }
  649. if( flags & kOutApFl )
  650. {
  651. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  652. for(i=0; i<ioPtr->chCnt; ++i)
  653. if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt )
  654. return false;
  655. //oFl = (ioPtr->n - ioPtr->fn) > ioPtr->dspFrameCnt;
  656. }
  657. return true;
  658. //return iFl & oFl;
  659. }
  660. // Note that his function returns audio samples but does NOT
  661. // change any internal states.
  662. void cmApBufGet( unsigned devIdx, unsigned flags, cmApSample_t* bufArray[], unsigned bufChCnt )
  663. {
  664. unsigned i;
  665. if( devIdx == cmInvalidIdx )
  666. {
  667. for(i=0; i<bufChCnt; ++i)
  668. bufArray[i] = NULL;
  669. return;
  670. }
  671. unsigned idx = flags & kInApFl ? kInApIdx : kOutApIdx;
  672. const cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + idx;
  673. unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt;
  674. cmApCh* cp = ioPtr->chArray;
  675. for(i=0; i<n; ++i,++cp)
  676. {
  677. unsigned offs = flags & kInApFl ? cp->oi : cp->ii;
  678. bufArray[i] = cmIsFlag(cp->fl,kChApFl) ? cp->b + offs : NULL;
  679. }
  680. }
  681. void _cmApBufCalcTimeStamp( double srate, const cmTimeSpec_t* baseTimeStamp, unsigned frmCnt, cmTimeSpec_t* retTimeStamp )
  682. {
  683. if( retTimeStamp==NULL )
  684. return;
  685. double secs = frmCnt / srate;
  686. unsigned int_secs = floor(secs);
  687. double frac_secs = secs - int_secs;
  688. retTimeStamp->tv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000);
  689. retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs;
  690. if( retTimeStamp->tv_nsec > 1000000000 )
  691. {
  692. retTimeStamp->tv_nsec -= 1000000000;
  693. retTimeStamp->tv_sec += 1;
  694. }
  695. }
  696. void cmApBufGetIO( unsigned iDevIdx, cmApSample_t* iBufArray[], unsigned iBufChCnt, cmTimeSpec_t* iTimeStampPtr, unsigned oDevIdx, cmApSample_t* oBufArray[], unsigned oBufChCnt, cmTimeSpec_t* oTimeStampPtr )
  697. {
  698. cmApBufGet( iDevIdx, kInApFl, iBufArray, iBufChCnt );
  699. cmApBufGet( oDevIdx, kOutApFl,oBufArray, oBufChCnt );
  700. unsigned i = 0;
  701. if( iDevIdx != cmInvalidIdx && oDevIdx != cmInvalidIdx )
  702. {
  703. const cmApIO* ip = _cmApBuf.devArray[iDevIdx].ioArray + kInApIdx;
  704. const cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  705. unsigned minChCnt = cmMin(iBufChCnt,oBufChCnt);
  706. unsigned frmCnt = cmMin(ip->dspFrameCnt,op->dspFrameCnt);
  707. unsigned byteCnt = frmCnt * sizeof(cmApSample_t);
  708. _cmApBufCalcTimeStamp(ip->srate, &ip->timeStamp, ip->ioFrameCnt, iTimeStampPtr );
  709. _cmApBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
  710. for(i=0; i<minChCnt; ++i)
  711. {
  712. cmApCh* ocp = op->chArray + i;
  713. cmApCh* icp = ip->chArray + i;
  714. if( oBufArray[i] != NULL )
  715. {
  716. // if either the input or output channel is marked for pass-through
  717. if( cmAllFlags(ocp->fl,kPassApFl) || cmAllFlags(icp->fl,kPassApFl) )
  718. {
  719. memcpy( oBufArray[i], iBufArray[i], byteCnt );
  720. // set the output buffer to NULL to prevent it being over written by the client
  721. oBufArray[i] = NULL;
  722. }
  723. else
  724. {
  725. // zero the output buffer
  726. memset(oBufArray[i],0,byteCnt);
  727. }
  728. }
  729. }
  730. }
  731. if( oDevIdx != cmInvalidIdx )
  732. {
  733. const cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  734. unsigned byteCnt = op->dspFrameCnt * sizeof(cmApSample_t);
  735. _cmApBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
  736. for(; i<oBufChCnt; ++i)
  737. if( oBufArray[i] != NULL )
  738. memset( oBufArray[i], 0, byteCnt );
  739. }
  740. }
  741. void cmApBufAdvance( unsigned devIdx, unsigned flags )
  742. {
  743. unsigned i;
  744. if( devIdx == cmInvalidIdx )
  745. return;
  746. if( flags & kInApFl )
  747. {
  748. cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kInApIdx;
  749. for(i=0; i<ioPtr->chCnt; ++i)
  750. {
  751. cmApCh* cp = ioPtr->chArray + i;
  752. cp->oi = (cp->oi + ioPtr->dspFrameCnt) % ioPtr->n;
  753. cmThUIntDecr(&cp->fn,ioPtr->dspFrameCnt);
  754. }
  755. // count the number of samples input from this device
  756. if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
  757. cmThUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
  758. }
  759. if( flags & kOutApFl )
  760. {
  761. cmApIO* ioPtr = _cmApBuf.devArray[devIdx].ioArray + kOutApIdx;
  762. for(i=0; i<ioPtr->chCnt; ++i)
  763. {
  764. cmApCh* cp = ioPtr->chArray + i;
  765. cp->ii = (cp->ii + ioPtr->dspFrameCnt) % ioPtr->n;
  766. cmThUIntIncr(&cp->fn,ioPtr->dspFrameCnt);
  767. }
  768. // count the number of samples output from this device
  769. if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
  770. cmThUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
  771. }
  772. }
  773. void cmApBufInputToOutput( unsigned iDevIdx, unsigned oDevIdx )
  774. {
  775. if( iDevIdx == cmInvalidIdx || oDevIdx == cmInvalidIdx )
  776. return;
  777. unsigned iChCnt = cmApBufChannelCount( iDevIdx, kInApFl );
  778. unsigned oChCnt = cmApBufChannelCount( oDevIdx, kOutApFl );
  779. unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt;
  780. unsigned i;
  781. cmApSample_t* iBufPtrArray[ iChCnt ];
  782. cmApSample_t* oBufPtrArray[ oChCnt ];
  783. while( cmApBufIsDeviceReady( iDevIdx, kInApFl ) && cmApBufIsDeviceReady( oDevIdx, kOutApFl ) )
  784. {
  785. cmApBufGet( iDevIdx, kInApFl, iBufPtrArray, iChCnt );
  786. cmApBufGet( oDevIdx, kOutApFl, oBufPtrArray, oChCnt );
  787. // Warning: buffer pointers to disabled channels will be set to NULL
  788. for(i=0; i<chCnt; ++i)
  789. {
  790. cmApIO* ip = _cmApBuf.devArray[iDevIdx ].ioArray + kInApIdx;
  791. cmApIO* op = _cmApBuf.devArray[oDevIdx].ioArray + kOutApIdx;
  792. assert( ip->dspFrameCnt == op->dspFrameCnt );
  793. unsigned byteCnt = ip->dspFrameCnt * sizeof(cmApSample_t);
  794. if( oBufPtrArray[i] != NULL )
  795. {
  796. // the input channel is not disabled
  797. if( iBufPtrArray[i]!=NULL )
  798. memcpy(oBufPtrArray[i],iBufPtrArray[i],byteCnt);
  799. else
  800. // the input channel is disabled but the output is not - so fill the output with zeros
  801. memset(oBufPtrArray[i],0,byteCnt);
  802. }
  803. }
  804. cmApBufAdvance( iDevIdx, kInApFl );
  805. cmApBufAdvance( oDevIdx, kOutApFl );
  806. }
  807. }
  808. void cmApBufReport( cmRpt_t* rpt )
  809. {
  810. unsigned i,j,k;
  811. for(i=0; i<_cmApBuf.devCnt; ++i)
  812. {
  813. cmRptPrintf(rpt,"%i ",i);
  814. for(j=0; j<kIoApCnt; ++j)
  815. {
  816. cmApIO* ip = _cmApBuf.devArray[i].ioArray + j;
  817. unsigned ii = 0;
  818. unsigned oi = 0;
  819. unsigned fn = 0;
  820. cmApSample_t mtr = 0;
  821. for(k=0; k<ip->chCnt; ++k)
  822. {
  823. cmApCh* cp = ip->chArray + i;
  824. ii += cp->ii;
  825. oi += cp->oi;
  826. fn += cp->fn;
  827. mtr += _cmApMeterValue(cp);
  828. }
  829. cmRptPrintf(rpt,"%s - i:%7i o:%7i f:%7i n:%7i err %s:%7i mtr:%5.4f ",
  830. j==0?"IN ":"OUT",
  831. ii,oi,fn,ip->n, (j==0?"over ":"under"), ip->faultCnt, mtr);
  832. }
  833. cmRptPrintf(rpt,"\n");
  834. }
  835. }
  836. /// [cmApBufExample]
  837. void cmApBufTest( cmRpt_t* rpt )
  838. {
  839. unsigned devIdx = 0;
  840. unsigned devCnt = 1 ;
  841. unsigned dspFrameCnt = 10;
  842. unsigned cycleCnt = 3;
  843. unsigned framesPerCycle = 25;
  844. unsigned inChCnt = 2;
  845. unsigned outChCnt = inChCnt;
  846. unsigned sigN = cycleCnt*framesPerCycle*inChCnt;
  847. double srate = 44100.0;
  848. unsigned meterMs = 50;
  849. int srateMult = 1;
  850. unsigned bufChCnt= inChCnt;
  851. cmApSample_t* inBufArray[ bufChCnt ];
  852. cmApSample_t* outBufArray[ bufChCnt ];
  853. cmApSample_t iSig[ sigN ];
  854. cmApSample_t oSig[ sigN ];
  855. cmApSample_t* os = oSig;
  856. cmApAudioPacket_t pkt;
  857. int i,j;
  858. // create a simulated signal
  859. for(i=0; i<sigN; ++i)
  860. {
  861. iSig[i] = i;
  862. oSig[i] = 0;
  863. }
  864. pkt.devIdx = 0;
  865. pkt.begChIdx = 0;
  866. pkt.chCnt = inChCnt;
  867. pkt.audioFramesCnt = framesPerCycle;
  868. pkt.bitsPerSample = 32;
  869. pkt.flags = 0;
  870. // initialize a the audio buffer
  871. cmApBufInitialize(devCnt,meterMs);
  872. // setup the buffer with the specific parameters to by used by the host audio ports
  873. cmApBufSetup(devIdx,srate,dspFrameCnt,cycleCnt,inChCnt,framesPerCycle,outChCnt,framesPerCycle, srateMult);
  874. // simulate cylcing through sigN buffer transactions
  875. for(i=0; i<sigN; i+=framesPerCycle*inChCnt)
  876. {
  877. // setup an incoming audio packet
  878. pkt.audioFramesCnt = framesPerCycle;
  879. pkt.audioBytesPtr = iSig+i;
  880. // simulate a call from the audio port with incoming samples
  881. // (fill the audio buffers internal input buffers)
  882. cmApBufUpdate(&pkt,1,NULL,0);
  883. // if all devices need to be serviced
  884. while( cmApBufIsDeviceReady( devIdx, kInApFl | kOutApFl ))
  885. {
  886. // get pointers to full internal input buffers
  887. cmApBufGet(devIdx, kInApFl, inBufArray, bufChCnt );
  888. // get pointers to empty internal output buffers
  889. cmApBufGet(devIdx, kOutApFl, outBufArray, bufChCnt );
  890. // Warning: pointers to disabled channels will be set to NULL.
  891. // simulate a play through by copying the incoming samples to the outgoing buffers.
  892. for(j=0; j<bufChCnt; ++j)
  893. if( outBufArray[j] != NULL )
  894. {
  895. // if the input is disabled - but the output is not then zero the output buffer
  896. if( inBufArray[j] == NULL )
  897. memset( outBufArray[j], 0, dspFrameCnt*sizeof(cmApSample_t));
  898. else
  899. // copy the input to the output
  900. memcpy( outBufArray[j], inBufArray[j], dspFrameCnt*sizeof(cmApSample_t));
  901. }
  902. // signal the buffer that this cycle is complete.
  903. // (marks current internal input/output buffer empty/full)
  904. cmApBufAdvance( devIdx, kInApFl | kOutApFl );
  905. }
  906. pkt.audioBytesPtr = os;
  907. // simulate a call from the audio port picking up outgoing samples
  908. // (empties the audio buffers internal output buffers)
  909. cmApBufUpdate(NULL,0,&pkt,1);
  910. os += pkt.audioFramesCnt * pkt.chCnt;
  911. }
  912. for(i=0; i<sigN; ++i)
  913. cmRptPrintf(rpt,"%f ",oSig[i]);
  914. cmRptPrintf(rpt,"\n");
  915. cmApBufFinalize();
  916. }
  917. /// [cmApBufExample]