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 40KB

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