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.

cmAudDsp.c 37KB

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