libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmAudDsp.c 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 "cmLinkedHeap.h"
  10. #include "cmSymTbl.h"
  11. #include "cmJson.h"
  12. #include "cmFileSys.h"
  13. #include "cmPrefs.h"
  14. #include "cmAudioPort.h"
  15. #include "cmAudioAggDev.h"
  16. #include "cmAudioNrtDev.h"
  17. #include "cmApBuf.h"
  18. #include "cmMidi.h"
  19. #include "cmMidiPort.h"
  20. #include "dsp/cmDspValue.h"
  21. #include "cmMsgProtocol.h"
  22. #include "cmThread.h"
  23. #include "cmUdpPort.h"
  24. #include "cmUdpNet.h"
  25. #include "cmAudioSys.h"
  26. #include "cmProcObj.h"
  27. #include "dsp/cmDspCtx.h"
  28. #include "dsp/cmDspClass.h"
  29. #include "dsp/cmDspSys.h"
  30. #include "cmAudDsp.h"
  31. cmAdH_t cmAdNullHandle = cmSTATIC_NULL_HANDLE;
  32. typedef struct
  33. {
  34. cmDspSysH_t dsH;
  35. unsigned curPgmIdx;
  36. bool isLoadedFl;
  37. unsigned isSyncFl;
  38. } cmAdDsSubSys_t;
  39. typedef struct
  40. {
  41. const cmChar_t* label;
  42. cmAudioSysCfg_t cfg;
  43. } cmAdAsCfg_t;
  44. typedef struct
  45. {
  46. const cmChar_t* label;
  47. unsigned physDevCnt;
  48. unsigned* physDevIdxArray;
  49. } cmAdAggDev_t;
  50. typedef struct
  51. {
  52. const cmChar_t* label;
  53. double srate;
  54. unsigned iChCnt;
  55. unsigned oChCnt;
  56. unsigned cbPeriodMs;
  57. } cmAdNrtDev_t;
  58. typedef struct
  59. {
  60. cmErr_t err;
  61. cmCtx_t ctx;
  62. cmMsgSendFuncPtr_t cbFunc;
  63. void* cbDataPtr;
  64. cmJsonH_t sysJsH;
  65. const cmChar_t* sysJsFn;
  66. cmUdpNetH_t netH;
  67. cmDspSysH_t dsH;
  68. cmAudioSysH_t asH;
  69. unsigned midiPortBufByteCnt;
  70. unsigned meterMs;
  71. unsigned msgsPerClientPoll;
  72. cmAdAggDev_t* aggDevArray;
  73. unsigned aggDevCnt;
  74. cmAdNrtDev_t* nrtDevArray;
  75. unsigned nrtDevCnt;
  76. cmAdAsCfg_t* asCfgArray;
  77. unsigned asCfgCnt;
  78. unsigned curAsCfgIdx;
  79. unsigned dsSsCnt;
  80. cmAdDsSubSys_t* dsSsArray;
  81. bool syncFl; // all cmAdDsSubSys's are synced
  82. } cmAd_t;
  83. cmAd_t* _cmAdHandleToPtr( cmAdH_t h )
  84. {
  85. cmAd_t* p = (cmAd_t*)h.h;
  86. assert( p != NULL );
  87. return p;
  88. }
  89. // cmAudioSys DSP processing callback - this is the entry point
  90. // from the cmAudioSystem to the cmDspSystem. Note that it is called from the
  91. // the audio processing thread in cmAudioSys.c: _cmAsDeliverMsgsWithLock()
  92. cmRC_t _cmAudDspCallback( void *cbPtr, unsigned msgByteCnt, const void* msgDataPtr )
  93. {
  94. cmAudioSysCtx_t* ctx = (cmAudioSysCtx_t*)cbPtr;
  95. cmAdDsSubSys_t* p = (cmAdDsSubSys_t*)ctx->ss->cbDataPtr;
  96. if( p != NULL && p->isLoadedFl )
  97. return cmDspSysRcvMsg(p->dsH,ctx, msgDataPtr, msgByteCnt, ctx->srcNetNodeId );
  98. return cmOkRC;
  99. }
  100. // This function is called by cmAudioSysReceiveMsg() to transfer messages from the
  101. // cmAudioSys or cmDspSys to the client.
  102. cmRC_t _cmAudioSysToClientCallback(void* userCbPtr, unsigned msgByteCnt, const void* msgDataPtr )
  103. {
  104. cmAd_t* p = (cmAd_t*)userCbPtr;
  105. return p->cbFunc(p->cbDataPtr, msgByteCnt, msgDataPtr );
  106. }
  107. // This function is called by cmUdpNetReceive(), which is called in
  108. // cmAudioSys.c:_cmAsThreadCallback() just prior to executing the DSP process.
  109. void _cmAdUdpNetCallback( void* cbArg, cmUdpNetH_t h, const char* data, unsigned dataByteCnt, unsigned srcNetNodeId )
  110. {
  111. cmAd_t* p = (cmAd_t*)cbArg;
  112. // send the incoming message to the audio system for later delivery to the DSP system
  113. cmAudioSysDeliverMsg(p->asH, data, dataByteCnt, srcNetNodeId );
  114. }
  115. cmAdRC_t _cmAdParseMemberErr( cmAd_t* p, cmJsRC_t jsRC, const cmChar_t* errLabel, const cmChar_t* objectLabel )
  116. {
  117. if( jsRC == kNodeNotFoundJsRC && errLabel != NULL )
  118. return cmErrMsg(&p->err,kJsonFailAdRC,"The required field '%s'was not found in the audio DSP resource tree in the object '%s' in the file '%s'.",errLabel,cmStringNullGuard(objectLabel), cmStringNullGuard(p->sysJsFn));
  119. return cmErrMsg(&p->err,kJsonFailAdRC,"JSON parsing failed on the Audio DSP resource file '%s' in the resource object '%s'.",cmStringNullGuard(p->sysJsFn),cmStringNullGuard(objectLabel));
  120. }
  121. cmAdRC_t _cmAdParseSysJsonTree( cmAd_t* p )
  122. {
  123. cmAdRC_t rc = kOkAdRC;
  124. cmJsonNode_t* asCfgArrNodePtr = NULL;
  125. cmJsonNode_t* aggDevArrNodePtr = NULL;
  126. cmJsonNode_t* nrtDevArrNodePtr = NULL;
  127. cmJsonNode_t* audDspNodePtr = NULL;
  128. const cmChar_t* errLabelPtr = NULL;
  129. unsigned i;
  130. cmJsRC_t jsRC = kOkJsRC;
  131. // locate the aud_dsp container object
  132. if( cmJsonNodeMember( cmJsonRoot(p->sysJsH), "aud_dsp", &audDspNodePtr ) != kOkJsRC )
  133. {
  134. rc = cmErrMsg(&p->err,kJsonFailAdRC,"The audio DSP system resource file '%s' does not contain an 'aud_dsp' object.",cmStringNullGuard(p->sysJsFn));
  135. goto errLabel;
  136. }
  137. // locate the read the aud_dsp sub-elements
  138. if(( jsRC = cmJsonMemberValues( audDspNodePtr, &errLabelPtr,
  139. "midiPortBufByteCnt", kIntTId, &p->midiPortBufByteCnt,
  140. "meterMs", kIntTId, &p->meterMs,
  141. "msgsPerClientPoll", kIntTId, &p->msgsPerClientPoll,
  142. "audioSysCfgArray", kArrayTId, &asCfgArrNodePtr,
  143. "aggDevArray", kArrayTId | kOptArgJsFl, &aggDevArrNodePtr,
  144. "nrtDevArray", kArrayTId | kOptArgJsFl, &nrtDevArrNodePtr,
  145. NULL )) != kOkJsRC )
  146. {
  147. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, "aud_dsp" );
  148. goto errLabel;
  149. }
  150. // parse the aggregate device specifications into p->aggDevArray[].
  151. if( aggDevArrNodePtr != NULL && (p->aggDevCnt = cmJsonChildCount(aggDevArrNodePtr)) > 0)
  152. {
  153. // alloc the aggregate spec. array
  154. p->aggDevArray = cmMemResizeZ( cmAdAggDev_t, p->aggDevArray, p->aggDevCnt );
  155. // for each agg. device spec. recd
  156. for(i=0; i<p->aggDevCnt; ++i)
  157. {
  158. const cmJsonNode_t* np = cmJsonArrayElementC(aggDevArrNodePtr,i);
  159. const cmJsonNode_t* devIdxArrNodePtr = NULL;
  160. unsigned j;
  161. // read aggDevArray record values
  162. if(( jsRC = cmJsonMemberValues( np, &errLabelPtr,
  163. "label", kStringTId, &p->aggDevArray[i].label,
  164. "physDevIdxArray", kArrayTId, &devIdxArrNodePtr,
  165. NULL )) != kOkJsRC )
  166. {
  167. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, cmStringNullGuard(p->aggDevArray[i].label) );
  168. goto errLabel;
  169. }
  170. unsigned physDevCnt = cmJsonChildCount(devIdxArrNodePtr);
  171. // alloc the dev idx array for to hold the phys. dev indexes for this agg device
  172. p->aggDevArray[i].physDevIdxArray = cmMemResizeZ( unsigned, p->aggDevArray[i].physDevIdxArray, physDevCnt);
  173. p->aggDevArray[i].physDevCnt = physDevCnt;
  174. // store the phys. dev. idx's
  175. for(j=0; j<physDevCnt; ++j)
  176. if( cmJsonUIntValue( cmJsonArrayElementC(devIdxArrNodePtr,j), p->aggDevArray[i].physDevIdxArray + j ) != kOkJsRC )
  177. {
  178. rc = cmErrMsg(&p->err,kJsonFailAdRC,"Unable to retrieve a physical device index for the aggregate device '%s'.", cmStringNullGuard(p->aggDevArray[i].label));
  179. goto errLabel;
  180. }
  181. }
  182. }
  183. // parse the non-real-time device specifications into p->nrtDevArray[].
  184. if( nrtDevArrNodePtr != NULL && (p->nrtDevCnt = cmJsonChildCount(nrtDevArrNodePtr)) > 0)
  185. {
  186. // alloc the non-real-time spec. array
  187. p->nrtDevArray = cmMemResizeZ( cmAdNrtDev_t, p->nrtDevArray, p->nrtDevCnt );
  188. // for each nrt. device spec. recd
  189. for(i=0; i<p->nrtDevCnt; ++i)
  190. {
  191. const cmJsonNode_t* np = cmJsonArrayElementC(nrtDevArrNodePtr,i);
  192. // read nrtDevArray record values
  193. if(( jsRC = cmJsonMemberValues( np, &errLabelPtr,
  194. "label", kStringTId, &p->nrtDevArray[i].label,
  195. "srate", kRealTId, &p->nrtDevArray[i].srate,
  196. "iChCnt", kIntTId, &p->nrtDevArray[i].iChCnt,
  197. "oChCnt", kIntTId, &p->nrtDevArray[i].oChCnt,
  198. "cbPeriodMs", kIntTId, &p->nrtDevArray[i].cbPeriodMs,
  199. NULL )) != kOkJsRC )
  200. {
  201. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, cmStringNullGuard(p->nrtDevArray[i].label) );
  202. goto errLabel;
  203. }
  204. }
  205. }
  206. if((p->asCfgCnt = cmJsonChildCount(asCfgArrNodePtr)) == 0 )
  207. goto errLabel;
  208. p->asCfgArray = cmMemResizeZ( cmAdAsCfg_t, p->asCfgArray, p->asCfgCnt);
  209. // for each cmAsAudioSysCfg record in audioSysCfgArray[]
  210. for(i=0; i<p->asCfgCnt; ++i)
  211. {
  212. unsigned j;
  213. const cmJsonNode_t* asCfgNodePtr = cmJsonArrayElementC(asCfgArrNodePtr,i);
  214. const cmJsonNode_t* ssArrayNodePtr = NULL;
  215. const char* cfgLabel = NULL;
  216. // read cmAsAudioSysCfg record values
  217. if(( jsRC = cmJsonMemberValues( asCfgNodePtr, &errLabelPtr,
  218. "label", kStringTId, &cfgLabel,
  219. "ssArray", kArrayTId, &ssArrayNodePtr,
  220. NULL )) != kOkJsRC )
  221. {
  222. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, cmStringNullGuard(p->asCfgArray[i].label) );
  223. goto errLabel;
  224. }
  225. p->asCfgArray[i].label = cfgLabel;
  226. p->asCfgArray[i].cfg.ssCnt = cmJsonChildCount( ssArrayNodePtr );
  227. p->asCfgArray[i].cfg.ssArray = cmMemResizeZ( cmAudioSysSubSys_t, p->asCfgArray[i].cfg.ssArray, p->asCfgArray[i].cfg.ssCnt );
  228. p->asCfgArray[i].cfg.clientCbFunc = _cmAudioSysToClientCallback;
  229. p->asCfgArray[i].cfg.clientCbData = p;
  230. // for each audio system sub-subsystem
  231. for(j=0; j<p->asCfgArray[i].cfg.ssCnt; ++j)
  232. {
  233. cmAudioSysArgs_t* asap = &p->asCfgArray[i].cfg.ssArray[j].args;
  234. const cmJsonNode_t* argsNodePtr = cmJsonArrayElementC(ssArrayNodePtr,j);
  235. if((jsRC = cmJsonMemberValues( argsNodePtr, &errLabelPtr,
  236. "inDevIdx", kIntTId, &asap->inDevIdx,
  237. "outDevIdx", kIntTId, &asap->outDevIdx,
  238. "syncToInputFl", kTrueTId, &asap->syncInputFl,
  239. "msgQueueByteCnt", kIntTId, &asap->msgQueueByteCnt,
  240. "devFramesPerCycle", kIntTId, &asap->devFramesPerCycle,
  241. "dspFramesPerCycle", kIntTId, &asap->dspFramesPerCycle,
  242. "audioBufCnt", kIntTId, &asap->audioBufCnt,
  243. "srate", kRealTId, &asap->srate,
  244. NULL )) != kOkJsRC )
  245. {
  246. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, cmStringNullGuard(p->asCfgArray[i].label));
  247. goto errLabel;
  248. }
  249. }
  250. }
  251. errLabel:
  252. return rc;
  253. }
  254. cmAdRC_t _cmAdSetup( cmAd_t* p )
  255. {
  256. unsigned i;
  257. cmAdRC_t rc = kOkAdRC;
  258. for(i=0; i<p->asCfgCnt; ++i)
  259. {
  260. unsigned j;
  261. p->asCfgArray[i].cfg.meterMs = p->meterMs;
  262. // BUG BUG BUG - each sub-system should have it's own network
  263. // manager, and socket port.
  264. p->asCfgArray[i].cfg.netH = p->netH;
  265. for(j=0; j<p->asCfgArray[i].cfg.ssCnt; ++j)
  266. {
  267. p->asCfgArray[i].cfg.ssArray[j].cbDataPtr = NULL;
  268. p->asCfgArray[i].cfg.ssArray[j].cbFunc = _cmAudDspCallback;
  269. p->asCfgArray[i].cfg.ssArray[j].args.rpt = p->err.rpt;
  270. }
  271. }
  272. return rc;
  273. }
  274. cmAdRC_t _cmAdCreateAggDevices( cmAd_t* p )
  275. {
  276. cmAdRC_t rc = kOkAdRC;
  277. unsigned i;
  278. if( cmApAggAllocate(p->err.rpt) != kOkAgRC )
  279. return cmErrMsg(&p->err,kAggDevSysFailAdRC,"The aggregate device system allocation failed.");
  280. for(i=0; i<p->aggDevCnt; ++i)
  281. {
  282. cmAdAggDev_t* adp = p->aggDevArray + i;
  283. if( cmApAggCreateDevice(adp->label,adp->physDevCnt,adp->physDevIdxArray,kInAggFl | kOutAggFl) != kOkAgRC )
  284. rc = cmErrMsg(&p->err,kAggDevCreateFailAdRC,"The aggregate device '%s' creation failed.",cmStringNullGuard(adp->label));
  285. }
  286. return rc;
  287. }
  288. cmAdRC_t _cmAdCreateNrtDevices( cmAd_t* p )
  289. {
  290. cmAdRC_t rc = kOkAdRC;
  291. unsigned i;
  292. if( cmApNrtAllocate(p->err.rpt) != kOkApRC )
  293. return cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device system allocation failed.");
  294. for(i=0; i<p->nrtDevCnt; ++i)
  295. {
  296. cmAdNrtDev_t* adp = p->nrtDevArray + i;
  297. if( cmApNrtCreateDevice(adp->label,adp->srate,adp->iChCnt,adp->oChCnt,adp->cbPeriodMs) != kOkApRC )
  298. rc = cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device '%s' creation failed.",cmStringNullGuard(adp->label));
  299. }
  300. return rc;
  301. }
  302. cmAdRC_t _cmAdSendAudioSysCfgLabels( cmAd_t* p)
  303. {
  304. cmAdRC_t rc = kOkAdRC;
  305. unsigned i;
  306. for(i=0; i<p->asCfgCnt; ++i)
  307. {
  308. cmDspValue_t v;
  309. cmDsvSetStrcz(&v, p->asCfgArray[i].label);
  310. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kAudioSysCfgDuiId,0,i,p->asCfgCnt,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  311. {
  312. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending audio system cfg. label message to host.");
  313. break;
  314. }
  315. }
  316. return rc;
  317. }
  318. cmAdRC_t _cmAdSendDeviceLabels( cmAd_t* p )
  319. {
  320. cmAdRC_t rc = kOkAdRC;
  321. unsigned i,j;
  322. unsigned n = cmApDeviceCount();
  323. for(i=0; i<2; ++i)
  324. {
  325. bool inputFl = i==0;
  326. for(j=0; j<n; ++j)
  327. if( cmApDeviceChannelCount(j,inputFl) )
  328. {
  329. cmDspValue_t v;
  330. cmDsvSetStrcz(&v, cmApDeviceLabel(j));
  331. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kDeviceDuiId,inputFl,j,n,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  332. {
  333. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending device label message to host.");
  334. break;
  335. }
  336. }
  337. }
  338. return rc;
  339. }
  340. cmAdRC_t _cmAdSendProgramLabels( cmAd_t* p )
  341. {
  342. cmAdRC_t rc = kOkAdRC;
  343. unsigned pgmCnt = cmDspSysPgmCount(p->dsH);
  344. unsigned i;
  345. for(i=0; i<pgmCnt; ++i)
  346. {
  347. cmDspValue_t v;
  348. cmDsvSetStrcz(&v, cmDspPgmLabel(p->dsH,i));
  349. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kProgramDuiId,0,i,pgmCnt,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  350. {
  351. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending program label message to host.");
  352. break;
  353. }
  354. }
  355. return rc;
  356. }
  357. cmAdRC_t _cmAudDspFree( cmAd_t* p )
  358. {
  359. cmAdRC_t rc = kOkAdRC;
  360. if( cmAudioSysFree(&p->asH) != kOkAsRC )
  361. {
  362. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system release failed.");
  363. goto errLabel;
  364. }
  365. if( cmDspSysFinalize(&p->dsH) != kOkDspRC )
  366. {
  367. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"DSP system finalization failed.");
  368. goto errLabel;
  369. }
  370. if( cmUdpNetFree(&p->netH) != kOkUnRC )
  371. {
  372. rc = cmErrMsg(&p->err,kNetSysFailAdRC,"UDP Network finalization failed.");
  373. goto errLabel;
  374. }
  375. if( cmMpIsInitialized() )
  376. if( cmMpFinalize() != kOkMpRC )
  377. {
  378. rc = cmErrMsg(&p->err,kMidiSysFailAdRC,"MIDI system finalization failed.");
  379. goto errLabel;
  380. }
  381. if( cmApFinalize() != kOkApRC )
  382. {
  383. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port finalization failed.");
  384. goto errLabel;
  385. }
  386. if( cmApBufFinalize() != kOkApRC )
  387. {
  388. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port buffer finalization failed.");
  389. goto errLabel;
  390. }
  391. if( cmApNrtFree() != kOkAgRC )
  392. {
  393. rc = cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device system realease failed.");
  394. goto errLabel;
  395. }
  396. if( cmApAggFree() != kOkAgRC )
  397. {
  398. rc = cmErrMsg(&p->err,kAggDevSysFailAdRC,"The aggregate device system release failed.");
  399. goto errLabel;
  400. }
  401. cmMemPtrFree(&p->nrtDevArray);
  402. unsigned i;
  403. for(i=0; i<p->aggDevCnt; ++i)
  404. cmMemPtrFree(&p->aggDevArray[i].physDevIdxArray);
  405. cmMemPtrFree(&p->aggDevArray);
  406. for(i=0; i<p->asCfgCnt; ++i)
  407. cmMemPtrFree(&p->asCfgArray[i].cfg.ssArray);
  408. cmMemPtrFree(&p->asCfgArray);
  409. cmMemPtrFree(&p->dsSsArray);
  410. if( cmJsonFinalize(&p->sysJsH) != kOkJsRC )
  411. {
  412. rc = cmErrMsg(&p->err,kJsonFailAdRC,"System JSON tree finalization failed.");
  413. goto errLabel;
  414. }
  415. if( p->sysJsFn != NULL )
  416. cmFsFreeFn(p->sysJsFn);
  417. cmMemFree(p);
  418. errLabel:
  419. return rc;
  420. }
  421. cmAdRC_t cmAudDspAlloc( cmCtx_t* ctx, cmAdH_t* hp, cmMsgSendFuncPtr_t cbFunc, void* cbDataPtr )
  422. {
  423. cmAdRC_t rc = kOkAdRC;
  424. cmAdRC_t rc0 = kOkAdRC;
  425. if((rc = cmAudDspFree(hp)) != kOkAdRC )
  426. return rc;
  427. cmAd_t* p = cmMemAllocZ(cmAd_t,1);
  428. cmErrSetup(&p->err,&ctx->rpt,"Audio DSP Engine");
  429. // form the audio dsp resource file name
  430. if((p->sysJsFn = cmFsMakeFn( cmFsPrefsDir(),cmAudDspSys_FILENAME,NULL,NULL)) == NULL )
  431. {
  432. rc = cmErrMsg(&p->err,kFileSysFailAdRC,"Unable to form the audio dsp system resource file name.");
  433. goto errLabel;
  434. }
  435. // open the audio dsp resource file
  436. if(cmJsonInitializeFromFile(&p->sysJsH,p->sysJsFn,ctx) != kOkJsRC )
  437. {
  438. rc = cmErrMsg(&p->err,kJsonFailAdRC,"Unable to open the audio dsp resource file: '%s'.",cmStringNullGuard(p->sysJsFn));
  439. goto errLabel;
  440. }
  441. // parse the JSON tree
  442. if((rc = _cmAdParseSysJsonTree(p)) != kOkAdRC )
  443. goto errLabel;
  444. // create the aggregate device
  445. if( _cmAdCreateAggDevices(p) != kOkAdRC )
  446. goto errLabel;
  447. // create the non-real-time devices
  448. if( _cmAdCreateNrtDevices(p) != kOkAdRC )
  449. goto errLabel;
  450. // initialize the audio device system
  451. if( cmApInitialize(&ctx->rpt) != kOkApRC )
  452. {
  453. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port intialization failed.");
  454. goto errLabel;
  455. }
  456. // initialize the audio buffer
  457. if( cmApBufInitialize( cmApDeviceCount(), p->meterMs ) != kOkApRC )
  458. {
  459. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port buffer initialization failed.");
  460. goto errLabel;
  461. }
  462. // initialize the MIDI system
  463. if( cmMpInitialize(NULL,NULL,p->midiPortBufByteCnt,"app",&ctx->rpt) != kOkMpRC )
  464. {
  465. rc = cmErrMsg(&p->err,kMidiSysFailAdRC,"The MIDI system initialization failed.");
  466. goto errLabel;
  467. }
  468. // initialize the UDP network - but do not go into 'listening' mode.
  469. if( cmUdpNetAllocJson(ctx,&p->netH,p->sysJsH,_cmAdUdpNetCallback,p,kNetOptionalUnFl) != kOkUnRC )
  470. {
  471. cmErrMsg(&p->err,kNetSysFailAdRC,"The UDP network initialization failed.");
  472. goto errLabel;
  473. }
  474. if((rc = _cmAdSetup(p)) != kOkAdRC )
  475. goto errLabel;
  476. // initialize the DSP system
  477. if( cmDspSysInitialize(ctx,&p->dsH,p->netH) )
  478. {
  479. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP system initialization failed.");
  480. goto errLabel;
  481. }
  482. // allocate the audio system
  483. if( cmAudioSysAllocate(&p->asH, &ctx->rpt, NULL ) != kOkAsRC )
  484. {
  485. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system allocation failed.");
  486. goto errLabel;
  487. }
  488. p->cbFunc = cbFunc;
  489. p->cbDataPtr = cbDataPtr;
  490. p->curAsCfgIdx = cmInvalidIdx;
  491. p->ctx = *ctx;
  492. // notify the client of the available audio system configurations
  493. if((rc = _cmAdSendAudioSysCfgLabels(p)) != kOkAdRC )
  494. goto errLabel;
  495. // notify the client of the available devices
  496. if((rc = _cmAdSendDeviceLabels(p)) != kOkAdRC)
  497. goto errLabel;
  498. // notify the client of the available programs
  499. if((rc = _cmAdSendProgramLabels(p)) != kOkAdRC )
  500. goto errLabel;
  501. hp->h = p;
  502. errLabel:
  503. if( rc != kOkAdRC )
  504. rc0 = _cmAudDspFree(p);
  505. return rc == kOkAdRC ? rc0 : rc;
  506. }
  507. cmAdRC_t cmAudDspFree( cmAdH_t* hp )
  508. {
  509. cmAdRC_t rc = kOkAdRC;
  510. if( hp == NULL || cmAudDspIsValid(*hp)==false )
  511. return kOkAdRC;
  512. cmAd_t* p = _cmAdHandleToPtr(*hp);
  513. if((rc = _cmAudDspFree(p)) != kOkAdRC )
  514. return rc;
  515. hp->h = NULL;
  516. return rc;
  517. }
  518. bool cmAudDspIsValid( cmAdH_t h )
  519. { return h.h != NULL; }
  520. cmAdRC_t _cmAudDspUnloadPgm( cmAd_t* p, unsigned asSubSysIdx )
  521. {
  522. cmAdRC_t rc = kOkAdRC;
  523. const cmAdAsCfg_t* cfgPtr = NULL;
  524. // Must disable audio thread callbacks to _cmAudDspCallback()
  525. // while changing DSP system data structures.
  526. if( cmAudioSysIsEnabled(p->asH) )
  527. if(cmAudioSysEnable(p->asH,false) != kOkAsRC )
  528. {
  529. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system could not be disabled.");
  530. goto errLabel;
  531. }
  532. // validate the sub-system index
  533. if( asSubSysIdx >= p->dsSsCnt )
  534. {
  535. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The invalid sub-system index %i was encountered while unloading a program.",asSubSysIdx);
  536. goto errLabel;
  537. }
  538. // if a valid cfg recd exists
  539. if( p->curAsCfgIdx != cmInvalidIdx )
  540. {
  541. // pointer to audio system configuration
  542. cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  543. // count of audio system sub-systems should be the same as the current cfg
  544. assert( p->dsSsCnt == cfgPtr->cfg.ssCnt );
  545. // mark the DSP program as unloaded and pre-sync
  546. p->dsSsArray[ asSubSysIdx ].isLoadedFl = false;
  547. p->dsSsArray[ asSubSysIdx ].isSyncFl = false;
  548. p->syncFl = false;
  549. }
  550. // unload the current program
  551. if( cmDspSysUnload(p->dsSsArray[asSubSysIdx].dsH) != kOkDspRC )
  552. {
  553. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"Program unload failed.");
  554. goto errLabel;
  555. }
  556. errLabel:
  557. return rc;
  558. }
  559. cmAdRC_t _cmAudDspUnloadAudioSys( cmAd_t* p )
  560. {
  561. unsigned i;
  562. cmAdRC_t rc = kOkAdRC;
  563. p->syncFl = false;
  564. for(i=1; i<p->dsSsCnt; ++i)
  565. {
  566. if((rc = _cmAudDspUnloadPgm(p,i)) != kOkAdRC )
  567. goto errLabel;
  568. if( cmDspSysFinalize(&p->dsSsArray[i].dsH) != kOkDspRC )
  569. {
  570. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"DSP system finalization failed.");
  571. goto errLabel;
  572. }
  573. }
  574. p->curAsCfgIdx = cmInvalidIdx;
  575. errLabel:
  576. return rc;
  577. }
  578. cmAdRC_t _cmAdSendIntMsgToHost( cmAd_t* p, unsigned asSubIdx, unsigned selId, unsigned flags, unsigned intValue )
  579. {
  580. cmAdRC_t rc = kOkAdRC;
  581. cmDspValue_t v;
  582. cmDsvSetUInt(&v,intValue);
  583. if( cmMsgSend( &p->err,asSubIdx,kUiSelAsId,selId,flags,cmInvalidId,cmInvalidId,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  584. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending message to host.");
  585. return rc;
  586. }
  587. // verify that a valid audio cfg has been selected
  588. cmAdRC_t _cmAdIsAudioSysLoaded( cmAd_t* p )
  589. {
  590. cmAdRC_t rc = kOkAdRC;
  591. if( cmAudioSysHandleIsValid(p->asH) == false )
  592. {
  593. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system is not allocated.");
  594. goto errLabel;
  595. }
  596. if( p->curAsCfgIdx == cmInvalidIdx )
  597. return kInvalidCfgIdxAdRC;
  598. // verify that an audio system is loaded
  599. if( cmAudioSysIsInitialized(p->asH) && p->curAsCfgIdx == cmInvalidIdx )
  600. {
  601. rc = cmErrMsg(&p->err,kInvalidCfgIdxAdRC,"The audio system has not been configured.");
  602. goto errLabel;
  603. }
  604. // count of audio system sub-systems should be the same as the current cfg
  605. assert( p->dsSsCnt == p->asCfgArray[p->curAsCfgIdx].cfg.ssCnt );
  606. errLabel:
  607. return rc;
  608. }
  609. // verify that a valid audio cfg and DSP program has been selected
  610. cmAdRC_t _cmAdIsPgmLoaded( cmAd_t* p, bool verboseFl )
  611. {
  612. cmAdRC_t rc;
  613. // a program cannot be loaded if the audio system has not been configured
  614. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  615. return rc;
  616. unsigned i;
  617. // for each sub-system
  618. for(i=0; i<p->dsSsCnt; ++i)
  619. {
  620. // verify that the DSP system has been created
  621. if( cmDspSysIsValid(p->dsSsArray[i].dsH) == false )
  622. return cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP sub-system at index %i is not initialized.",i);
  623. // verify that the DSP program was loaded
  624. if( p->dsSsArray[ i ].isLoadedFl == false )
  625. {
  626. if( verboseFl )
  627. cmErrMsg(&p->err,kNoPgmLoadedAdRC,"There is no program loaded.");
  628. return kNoPgmLoadedAdRC;
  629. }
  630. }
  631. return rc;
  632. }
  633. bool _cmAudDspIsPgmSynced( cmAd_t* p )
  634. {
  635. unsigned syncCnt = 0;
  636. unsigned i;
  637. if( p->syncFl )
  638. return true;
  639. // if the pgm is not loaded then it cannot be sync'd
  640. if(_cmAdIsPgmLoaded(p,false) != kOkAdRC )
  641. return false;
  642. // check each sub-system
  643. for(i=0; i<p->dsSsCnt; ++i)
  644. {
  645. unsigned syncState = cmDspSysSyncState(p->dsSsArray[i].dsH);
  646. // if the subsys is already synced
  647. if( p->dsSsArray[i].isSyncFl )
  648. ++syncCnt;
  649. else
  650. {
  651. switch( syncState )
  652. {
  653. // the sub-sys is pre or pending sync mode
  654. case kSyncPreDspId:
  655. case kSyncPendingDspId:
  656. break;
  657. // sync mode completed - w/ success or fail
  658. case kSyncSuccessDspId:
  659. case kSyncFailDspId:
  660. {
  661. // notify the client of the the sync state
  662. bool syncFl = syncState == kSyncSuccessDspId;
  663. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSyncDuiId,syncFl,cmInvalidIdx);
  664. p->dsSsArray[i].isSyncFl = syncFl;
  665. }
  666. break;
  667. }
  668. }
  669. }
  670. p->syncFl = syncCnt == p->dsSsCnt;
  671. return p->syncFl;
  672. }
  673. cmAdRC_t _cmAudDspLoadPgm( cmAd_t* p, unsigned asSubSysIdx, unsigned pgmIdx )
  674. {
  675. cmAdRC_t rc = kOkAdRC;
  676. unsigned i;
  677. p->syncFl = false;
  678. // the audio system must be configured before a program is loaded
  679. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  680. return cmErrMsg(&p->err,rc,"Program load failed.");
  681. // validate the sub-system index arg.
  682. if( asSubSysIdx!=cmInvalidIdx && asSubSysIdx >= p->dsSsCnt )
  683. {
  684. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid.",asSubSysIdx);
  685. goto errLabel;
  686. }
  687. // for each sub-system
  688. for(i=0; i<p->dsSsCnt; ++i)
  689. if( asSubSysIdx==cmInvalidIdx || i==asSubSysIdx )
  690. {
  691. // unload any currently loaded program on this sub-system
  692. // (unloading a program automatically disables the audio system)
  693. if((rc = _cmAudDspUnloadPgm(p, i )) != kOkAdRC )
  694. goto errLabel;
  695. // load the program
  696. if( cmDspSysLoad(p->dsSsArray[ i ].dsH, cmAudioSysContext(p->asH,i), pgmIdx ) != kOkDspRC )
  697. {
  698. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The program load failed on audio sub-system %i.",i);
  699. goto errLabel;
  700. }
  701. // update the state of the DSP sub-system
  702. p->dsSsArray[i].curPgmIdx = pgmIdx;
  703. p->dsSsArray[i].isLoadedFl = true;
  704. p->dsSsArray[i].isSyncFl = false;
  705. p->syncFl = false;
  706. // notify the host of the new program
  707. _cmAdSendIntMsgToHost(p,i,kSetPgmDuiId,0,pgmIdx);
  708. }
  709. errLabel:
  710. return rc;
  711. }
  712. cmAdRC_t _cmAdReinitAudioSys( cmAd_t* p )
  713. {
  714. cmAdRC_t rc = kOkAdRC;
  715. p->syncFl = false;
  716. // pointer to the new audio system configuration
  717. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  718. // initialize the audio system
  719. if( cmAudioSysInitialize(p->asH, &cfgPtr->cfg ) != kOkAsRC )
  720. {
  721. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system initialization failed.");
  722. goto errLabel;
  723. }
  724. // reload any currently loaded programs
  725. unsigned i;
  726. for(i=0; i<p->dsSsCnt; ++i)
  727. {
  728. unsigned pgmIdx;
  729. if((pgmIdx = p->dsSsArray[i].curPgmIdx) != cmInvalidIdx )
  730. if((rc = _cmAudDspLoadPgm(p,i,pgmIdx)) != kOkAdRC )
  731. break;
  732. }
  733. errLabel:
  734. return rc;
  735. }
  736. cmAdRC_t _cmAudDspLoadAudioSys( cmAd_t* p, unsigned asCfgIdx )
  737. {
  738. cmAdRC_t rc = kOkAdRC;
  739. cmAdAsCfg_t* cfgPtr;
  740. unsigned i;
  741. // validate asCfgIdx
  742. if( asCfgIdx >= p->asCfgCnt )
  743. {
  744. cmErrMsg(&p->err,kInvalidCfgIdxAdRC,"The audio system index %i is invalid.",asCfgIdx);
  745. goto errLabel;
  746. }
  747. // clear the current audio system setup - this will automatically disable the audio system
  748. if((rc = _cmAudDspUnloadAudioSys(p)) != kOkAdRC )
  749. goto errLabel;
  750. // pointer to the new audio system configuration
  751. cfgPtr = p->asCfgArray + asCfgIdx;
  752. // get the count of audio system sub-systems
  753. p->dsSsCnt = cfgPtr->cfg.ssCnt;
  754. // store the index of the current audio system configuration
  755. p->curAsCfgIdx = asCfgIdx;
  756. if( p->dsSsCnt > 0 )
  757. {
  758. p->dsSsArray = cmMemResizeZ(cmAdDsSubSys_t, p->dsSsArray, p->dsSsCnt );
  759. for(i=0; i<p->dsSsCnt; ++i)
  760. {
  761. cmDspSysH_t dsH;
  762. // the first sub-system will always use the existing DSP system handle ...
  763. if( i==0 )
  764. dsH = p->dsH;
  765. else
  766. {
  767. // ... and allocate additional DSP systems when more than one sub-sys is
  768. // defined in the audio system configuration
  769. if( cmDspSysInitialize(&p->ctx,&dsH,p->netH) != kOkDspRC )
  770. {
  771. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"Unable to initialize an additional DSP system.");
  772. goto errLabel;
  773. }
  774. }
  775. p->dsSsArray[i].dsH = dsH;
  776. p->dsSsArray[i].curPgmIdx = cmInvalidIdx;
  777. p->dsSsArray[i].isLoadedFl= false;
  778. // this cbDataPtr is picked up in _cmAudDspCallback().
  779. // It is used to connect the audio system to a DSP system handle.
  780. cfgPtr->cfg.ssArray[i].cbDataPtr = p->dsSsArray + i;
  781. }
  782. }
  783. // notify the client of the change of audio configuration
  784. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSetAudioCfgDuiId,0,asCfgIdx);
  785. // notify the client of the count of audio sub-systems
  786. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSubSysCntDuiId,0,p->dsSsCnt);
  787. // for each sub-system
  788. for(i=0; i<p->dsSsCnt; ++i)
  789. {
  790. // notify the client of the currently selected devices
  791. _cmAdSendIntMsgToHost(p,i,kSetAudioDevDuiId,true, cfgPtr->cfg.ssArray[i].args.inDevIdx);
  792. _cmAdSendIntMsgToHost(p,i,kSetAudioDevDuiId,false,cfgPtr->cfg.ssArray[i].args.outDevIdx);
  793. // notify the client of the sample rate
  794. _cmAdSendIntMsgToHost(p,i,kSetSampleRateDuiId,0,(unsigned)cfgPtr->cfg.ssArray[i].args.srate);
  795. _cmAdSendIntMsgToHost(p,i,kSetPgmDuiId,0,cmInvalidIdx);
  796. }
  797. // the audio system configuration changed so we need to initialize the audio system
  798. if((rc = _cmAdReinitAudioSys(p)) != kOkAdRC )
  799. goto errLabel;
  800. errLabel:
  801. if( rc != kOkAdRC )
  802. _cmAudDspUnloadAudioSys(p);
  803. return rc;
  804. }
  805. cmAdRC_t _cmAudDspEnableAudio( cmAd_t* p, bool enableFl )
  806. {
  807. cmAdRC_t rc = kOkAdRC;
  808. if( enableFl )
  809. {
  810. // verify an audio system cfg and DSP program has been selected
  811. if(( rc = _cmAdIsPgmLoaded(p,true)) != kOkAdRC )
  812. return cmErrMsg(&p->err,rc,"Audio enable failed.");
  813. // if the audio system is already enabled/disabled then do nothing
  814. if( cmAudioSysIsEnabled(p->asH) == enableFl )
  815. return kOkAdRC;
  816. // for each sub-system
  817. unsigned i;
  818. for(i=0; i<p->dsSsCnt; ++i)
  819. {
  820. if( cmDspSysReset(p->dsSsArray[i].dsH) != kOkDspRC )
  821. {
  822. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP system reset failed.");
  823. goto errLabel;
  824. }
  825. }
  826. }
  827. // start/stop the audio sub-system
  828. if( cmAudioSysEnable(p->asH,enableFl) != kOkAsRC )
  829. {
  830. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system %s failed.", enableFl ? "enable" : "disable");
  831. goto errLabel;
  832. }
  833. // notify the host of the new enable state
  834. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kEnableDuiId,enableFl,cmInvalidIdx);
  835. errLabel:
  836. return rc;
  837. }
  838. cmAdRC_t _cmAudDspSetDevice(cmAd_t* p,unsigned asSubIdx, bool inputFl, unsigned devIdx)
  839. {
  840. cmAdRC_t rc;
  841. // a device cannot be set if the audio system is not already configured
  842. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  843. return cmErrMsg(&p->err,rc,"Set audio device failed.");
  844. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  845. // validate the sub-system index
  846. if( asSubIdx >= p->dsSsCnt )
  847. {
  848. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid.",asSubIdx);
  849. goto errLabel;
  850. }
  851. // assign the new device index to the indicated audio system configuration recd
  852. if( inputFl )
  853. {
  854. if( cfgPtr->cfg.ssArray[ asSubIdx ].args.inDevIdx != devIdx )
  855. cfgPtr->cfg.ssArray[ asSubIdx ].args.inDevIdx = devIdx;
  856. }
  857. else
  858. {
  859. if( cfgPtr->cfg.ssArray[ asSubIdx ].args.outDevIdx != devIdx )
  860. cfgPtr->cfg.ssArray[ asSubIdx ].args.outDevIdx = devIdx;
  861. }
  862. // notify the host that the new device has been set
  863. _cmAdSendIntMsgToHost(p,asSubIdx,kSetAudioDevDuiId,inputFl, devIdx);
  864. // reinitialize the audio system
  865. rc = _cmAdReinitAudioSys(p);
  866. errLabel:
  867. return rc;
  868. }
  869. cmAdRC_t _cmAudDspSetSampleRate(cmAd_t* p, unsigned asSubIdx, double srate )
  870. {
  871. cmAdRC_t rc;
  872. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  873. return cmErrMsg(&p->err,rc,"Set audio device failed.");
  874. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  875. // validate the sub-system index
  876. if( asSubIdx != cmInvalidIdx && asSubIdx >= p->dsSsCnt )
  877. {
  878. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid.",asSubIdx);
  879. goto errLabel;
  880. }
  881. unsigned i;
  882. for(i=0; i<p->dsSsCnt; ++i)
  883. {
  884. // assign the new device index to the indicated audio system configuration recd
  885. if( asSubIdx==cmInvalidIdx || asSubIdx == i )
  886. {
  887. if( cfgPtr->cfg.ssArray[ i ].args.srate != srate )
  888. cfgPtr->cfg.ssArray[ i ].args.srate = srate;
  889. }
  890. }
  891. // notify the client of the new sample rate
  892. _cmAdSendIntMsgToHost(p,asSubIdx,kSetSampleRateDuiId,0,(unsigned)srate);
  893. // reinitialize the audio system
  894. rc = _cmAdReinitAudioSys(p);
  895. errLabel:
  896. return rc;
  897. }
  898. cmAdRC_t _cmAudDspClientMsgPoll( cmAd_t* p )
  899. {
  900. unsigned i = 0;
  901. // if the program is not synced then don't bother polling the audio system
  902. if( _cmAudDspIsPgmSynced(p) == false )
  903. return kOkAdRC;
  904. for(i=0; i<p->msgsPerClientPoll; ++i)
  905. {
  906. if( cmAudioSysIsMsgWaiting(p->asH) == 0 )
  907. break;
  908. if(cmAudioSysReceiveMsg(p->asH,NULL,0) != kOkAsRC )
  909. return cmErrMsg(&p->err,kAudioSysFailAdRC,"The delivery of an audio system msg for the client failed.");
  910. }
  911. return kOkAdRC;
  912. }
  913. cmAdRC_t cmAudDspReceiveClientMsg( cmAdH_t h, unsigned msgByteCnt, const void* msg )
  914. {
  915. cmAdRC_t rc = kOkAdRC;
  916. cmAd_t* p = _cmAdHandleToPtr(h);
  917. cmDspUiHdr_t* m = (cmDspUiHdr_t*)msg;
  918. /*
  919. if( m->uiId != kUiSelAsId )
  920. {
  921. rc = cmErrMsg(&p->err,kUnknownMsgTypeAdRC,"The message type %i is unknown.");
  922. goto errLabel;
  923. }
  924. */
  925. switch( m->selId )
  926. {
  927. case kDevReportDuiId:
  928. cmApReport(p->err.rpt);
  929. break;
  930. case kSetAudioCfgDuiId:
  931. rc = _cmAudDspLoadAudioSys(p,cmDsvUInt(&m->value));
  932. break;
  933. case kSetPgmDuiId:
  934. rc = _cmAudDspLoadPgm(p,m->asSubIdx,cmDsvUInt(&m->value));
  935. break;
  936. case kSetAudioDevDuiId:
  937. rc = _cmAudDspSetDevice(p,m->asSubIdx,m->flags,cmDsvUInt(&m->value));
  938. break;
  939. case kSetSampleRateDuiId:
  940. rc = _cmAudDspSetSampleRate(p,m->asSubIdx,cmDsvDouble(&m->value));
  941. break;
  942. case kEnableDuiId:
  943. rc = _cmAudDspEnableAudio(p,m->flags);
  944. break;
  945. case kSetNotifyEnableDuiId:
  946. cmAudioSysStatusNotifyEnable(p->asH, cmInvalidIdx, m->flags );
  947. break;
  948. case kClientMsgPollDuiId:
  949. rc = _cmAudDspClientMsgPoll(p);
  950. break;
  951. default:
  952. if( cmAudioSysDeliverMsg(p->asH,msg,msgByteCnt,cmInvalidId) != kOkAsRC )
  953. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Message delivery to the audio system failed.");
  954. break;
  955. }
  956. // errLabel:
  957. return rc;
  958. }