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.

cmAudioNrtDev.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmTime.h"
  9. #include "cmAudioPort.h"
  10. #include "cmAudioNrtDev.h"
  11. #include "cmThread.h"
  12. enum
  13. {
  14. kStartedApNrtFl = 0x01,
  15. kInStateApNrtFl = 0x02
  16. };
  17. typedef struct
  18. {
  19. unsigned phase;
  20. bool implFl;
  21. double gain;
  22. } cmApNrtCh_t;
  23. typedef struct cmApNrtDev_str
  24. {
  25. unsigned flags;
  26. unsigned devIdx; // nrt device index
  27. unsigned baseApDevIdx; // global audio device index for first nrt device
  28. cmChar_t* label;
  29. unsigned iChCnt;
  30. unsigned oChCnt;
  31. double srate;
  32. unsigned fpc;
  33. cmThreadH_t thH;
  34. cmApCallbackPtr_t cbPtr;
  35. void* cbArg;
  36. unsigned cbPeriodMs;
  37. struct cmApNrtDev_str* link;
  38. unsigned curTimeSmp;
  39. cmApNrtCh_t* iChArray;
  40. cmApNrtCh_t* oChArray;
  41. } cmApNrtDev_t;
  42. typedef struct
  43. {
  44. cmErr_t err;
  45. unsigned devCnt;
  46. cmApNrtDev_t* devs;
  47. unsigned baseApDevIdx;
  48. } cmApNrt_t;
  49. cmApNrt_t* _cmNrt = NULL;
  50. cmApNrtDev_t* _cmApNrtIndexToDev( unsigned idx )
  51. {
  52. cmApNrtDev_t* dp = _cmNrt->devs;
  53. unsigned i;
  54. for(i=0; dp!=NULL && i<idx; ++i)
  55. dp = dp->link;
  56. assert( dp != NULL );
  57. return dp;
  58. }
  59. void cmApNrtGenImpulseCh( cmApNrtDev_t* dp, unsigned chIdx, cmSample_t* buf, unsigned chCnt, unsigned frmCnt )
  60. {
  61. double periodMs = 500; // impulse period
  62. double durMs = 50; // impulse length
  63. double loDb = -90.0;
  64. double hiDb = -20.0;
  65. double loLin = pow(10.0,loDb/20.0);
  66. double hiLin = pow(10.0,hiDb/20.0);
  67. unsigned periodSmp = (unsigned)(periodMs * dp->srate / 1000.0);
  68. unsigned durSmp = (unsigned)( durMs * dp->srate / 1000.0);
  69. assert( chIdx < chCnt );
  70. cmApNrtCh_t* cp = dp->iChArray + chIdx;
  71. cmSample_t* sp = buf + chIdx;
  72. unsigned i;
  73. for(i=0; i<frmCnt; ++i, sp+=chCnt )
  74. {
  75. unsigned limit = periodSmp;
  76. double gain = loLin;
  77. cp->phase += 1;
  78. if( cp->implFl )
  79. {
  80. gain = cp->gain;
  81. limit = durSmp;
  82. }
  83. *sp = (gain * 2.0 * rand()/RAND_MAX) - 1.0;
  84. if( cp->phase > limit )
  85. {
  86. cp->phase = 0;
  87. cp->implFl = !cp->implFl;
  88. if( cp->implFl )
  89. cp->gain = loLin + (hiLin - loLin) * rand()/RAND_MAX ;
  90. }
  91. }
  92. }
  93. void cmApNrtGenImpulse( cmApNrtDev_t* dp, cmSample_t* buf, unsigned chCnt, unsigned frmCnt )
  94. {
  95. unsigned i=0;
  96. for(; i<chCnt; ++i)
  97. cmApNrtGenImpulseCh(dp,i,buf,chCnt,frmCnt);
  98. }
  99. void cmApNrtGenSamples( cmApNrtDev_t* dp, cmSample_t* buf, unsigned chCnt, unsigned frmCnt )
  100. {
  101. memset(buf,0,chCnt*frmCnt*sizeof(cmSample_t));
  102. cmApNrtGenImpulse(dp,buf,chCnt,frmCnt);
  103. dp->curTimeSmp += frmCnt;
  104. }
  105. // return 'false' to terminate otherwise return 'true'.
  106. bool cmApNrtThreadFunc(void* param)
  107. {
  108. cmApNrtDev_t* dp = (cmApNrtDev_t*)param;
  109. cmSleepUs( dp->cbPeriodMs * 1000 );
  110. cmApAudioPacket_t pkt;
  111. bool inFl = cmIsFlag(dp->flags,kInStateApNrtFl);
  112. pkt.devIdx = dp->devIdx + dp->baseApDevIdx;
  113. pkt.begChIdx = 0;
  114. pkt.audioFramesCnt = dp->fpc;
  115. pkt.bitsPerSample = 32;
  116. pkt.flags = kInterleavedApFl | kFloatApFl;
  117. pkt.userCbPtr = dp->cbArg;
  118. if( inFl )
  119. {
  120. unsigned bn = dp->iChCnt * dp->fpc;
  121. cmSample_t b[ bn ];
  122. pkt.chCnt = dp->iChCnt;
  123. pkt.audioBytesPtr = b;
  124. cmApNrtGenSamples(dp,b,dp->iChCnt,dp->fpc);
  125. dp->cbPtr(&pkt,1,NULL,0 ); // send the samples to the application
  126. }
  127. else
  128. {
  129. unsigned bn = dp->oChCnt * dp->fpc;
  130. cmSample_t b[ bn ];
  131. pkt.chCnt = dp->oChCnt;
  132. pkt.audioBytesPtr = b;
  133. dp->cbPtr(NULL,0,&pkt,1 ); // recv the samples from the application
  134. }
  135. dp->flags = cmTogFlag(dp->flags,kInStateApNrtFl);
  136. return true;
  137. }
  138. cmApRC_t cmApNrtAllocate( cmRpt_t* rpt )
  139. {
  140. if( _cmNrt != NULL )
  141. cmApNrtFree();
  142. _cmNrt = cmMemAllocZ(cmApNrt_t,1);
  143. cmErrSetup(&_cmNrt->err,rpt,"cmAudioNrtDev");
  144. _cmNrt->devCnt = 0;
  145. _cmNrt->devs = NULL;
  146. _cmNrt->baseApDevIdx = 0;
  147. return kOkApRC;
  148. }
  149. cmApRC_t cmApNrtFree()
  150. {
  151. cmApRC_t rc = kOkApRC;
  152. if( _cmNrt == NULL )
  153. return rc;
  154. cmApNrtDev_t* dp = _cmNrt->devs;
  155. while( dp != NULL )
  156. {
  157. cmApNrtDev_t* np = dp->link;
  158. if( cmThreadIsValid(dp->thH) )
  159. if( cmThreadDestroy(&dp->thH) != kOkThRC )
  160. rc = cmErrMsg(&_cmNrt->err,kThreadFailApRC,"Thread destroy failed.");
  161. cmMemFree(dp->label);
  162. cmMemFree(dp->iChArray);
  163. cmMemFree(dp->oChArray);
  164. cmMemFree(dp);
  165. dp = np;
  166. }
  167. if( rc == kOkApRC )
  168. {
  169. cmMemPtrFree(&_cmNrt);
  170. }
  171. return rc;
  172. }
  173. cmApRC_t cmApNrtCreateDevice(
  174. const cmChar_t* label,
  175. double srate,
  176. unsigned iChCnt,
  177. unsigned oChCnt,
  178. unsigned cbPeriodMs )
  179. {
  180. cmApRC_t rc = kOkApRC;
  181. cmApNrtDev_t* dp = cmMemAllocZ(cmApNrtDev_t,1);
  182. dp->devIdx = _cmNrt->devCnt;
  183. dp->baseApDevIdx = _cmNrt->baseApDevIdx;
  184. dp->label = cmMemAllocStr(label);
  185. dp->iChCnt = iChCnt;
  186. dp->oChCnt = oChCnt;
  187. dp->srate = srate;
  188. dp->fpc = 0;
  189. dp->cbPeriodMs = cbPeriodMs;
  190. dp->cbPtr = NULL;
  191. dp->cbArg = NULL;
  192. dp->link = NULL;
  193. dp->iChArray = iChCnt==0 ? NULL : cmMemAllocZ(cmApNrtCh_t,iChCnt);
  194. dp->oChArray = oChCnt==0 ? NULL : cmMemAllocZ(cmApNrtCh_t,oChCnt);
  195. // attach the new recd to the end of the list
  196. cmApNrtDev_t* np = _cmNrt->devs;
  197. while( np != NULL && np->link != NULL )
  198. np = np->link;
  199. if( np == NULL )
  200. _cmNrt->devs = dp;
  201. else
  202. np->link = dp;
  203. if( cmThreadCreate( &dp->thH, cmApNrtThreadFunc, dp, _cmNrt->err.rpt ) != kOkThRC )
  204. rc = cmErrMsg(&_cmNrt->err,kThreadFailApRC,"Thread create failed.");
  205. ++_cmNrt->devCnt;
  206. return rc;
  207. }
  208. cmApRC_t cmApNrtInitialize( cmRpt_t* rpt, unsigned baseApDevIdx )
  209. {
  210. // set the baseApDevIdx for each device
  211. cmApNrtDev_t* dp = _cmNrt->devs;
  212. for(; dp!=NULL; dp=dp->link)
  213. dp->baseApDevIdx = baseApDevIdx;
  214. // store the baseApDevIdx for any devices that may be created after initialization
  215. _cmNrt->baseApDevIdx = baseApDevIdx;
  216. return kOkApRC;
  217. }
  218. cmApRC_t cmApNrtFinalize()
  219. {
  220. return kOkApRC;
  221. }
  222. unsigned cmApNrtDeviceCount()
  223. { return _cmNrt==NULL ? 0 : _cmNrt->devCnt; }
  224. const char* cmApNrtDeviceLabel( unsigned devIdx )
  225. {
  226. assert( devIdx < _cmNrt->devCnt );
  227. const cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  228. return dp->label;
  229. }
  230. unsigned cmApNrtDeviceChannelCount( unsigned devIdx, bool inputFl )
  231. {
  232. assert( devIdx < _cmNrt->devCnt );
  233. const cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  234. return inputFl ? dp->iChCnt : dp->oChCnt;
  235. }
  236. double cmApNrtDeviceSampleRate( unsigned devIdx )
  237. {
  238. assert( devIdx < _cmNrt->devCnt );
  239. const cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  240. return dp->srate;
  241. }
  242. unsigned cmApNrtDeviceFramesPerCycle( unsigned devIdx, bool inputFl )
  243. {
  244. assert( devIdx < _cmNrt->devCnt );
  245. const cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  246. return dp->fpc;
  247. }
  248. cmApRC_t cmApNrtDeviceSetup(
  249. unsigned devIdx,
  250. double srate,
  251. unsigned framesPerCycle,
  252. cmApCallbackPtr_t callbackPtr,
  253. void* userCbPtr )
  254. {
  255. assert( devIdx < _cmNrt->devCnt );
  256. cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  257. dp->srate = srate;
  258. dp->fpc = framesPerCycle;
  259. dp->cbPtr = callbackPtr;
  260. dp->cbArg = userCbPtr;
  261. return kOkApRC;
  262. }
  263. cmApRC_t cmApNrtDeviceStart( unsigned devIdx )
  264. {
  265. assert( devIdx < _cmNrt->devCnt );
  266. cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  267. dp->curTimeSmp = 0;
  268. if( cmThreadPause( dp->thH, 0 ) != kOkThRC )
  269. return cmErrMsg(&_cmNrt->err,kThreadFailApRC,"Thread start failed.");
  270. dp->flags = cmSetFlag(dp->flags,kStartedApNrtFl);
  271. return kOkApRC;
  272. }
  273. cmApRC_t cmApNrtDeviceStop( unsigned devIdx )
  274. {
  275. assert( devIdx < _cmNrt->devCnt );
  276. cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  277. if( cmThreadPause( dp->thH, kPauseThFl ) != kOkThRC )
  278. return cmErrMsg(&_cmNrt->err,kThreadFailApRC,"Thread pause failed.");
  279. dp->flags = cmClrFlag(dp->flags,kStartedApNrtFl);
  280. return kOkApRC;
  281. }
  282. bool cmApNrtDeviceIsStarted( unsigned devIdx )
  283. {
  284. assert( devIdx < _cmNrt->devCnt );
  285. const cmApNrtDev_t* dp = _cmApNrtIndexToDev(devIdx);
  286. return cmIsFlag(dp->flags,kStartedApNrtFl);
  287. }