libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmAudDsp.c 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. cmRptPrintf(p->err.rpt,"\nAUDIO DEVICES\n");
  1020. cmApReport(p->err.rpt);
  1021. cmRptPrintf(p->err.rpt,"\nMIDI DEVICES\n");
  1022. cmMpReport(p->err.rpt);
  1023. break;
  1024. case kSetAudioCfgDuiId:
  1025. rc = _cmAudDspLoadAudioSys(p,cmDsvUInt(&m->value));
  1026. break;
  1027. case kSetPgmDuiId:
  1028. rc = _cmAudDspLoadPgm(p,m->asSubIdx,cmDsvUInt(&m->value));
  1029. break;
  1030. case kSetAudioDevDuiId:
  1031. rc = _cmAudDspSetDevice(p,m->asSubIdx,m->flags,cmDsvUInt(&m->value));
  1032. break;
  1033. case kSetSampleRateDuiId:
  1034. rc = _cmAudDspSetSampleRate(p,m->asSubIdx,cmDsvDouble(&m->value));
  1035. break;
  1036. case kEnableDuiId:
  1037. rc = _cmAudDspEnableAudio(p,m->flags);
  1038. break;
  1039. case kSetNotifyEnableDuiId:
  1040. cmAudioSysStatusNotifyEnable(p->asH, cmInvalidIdx, m->flags );
  1041. break;
  1042. case kClientMsgPollDuiId:
  1043. rc = _cmAudDspClientMsgPoll(p);
  1044. break;
  1045. case kPrintPgmDuiId:
  1046. _cmAudDspPrintPgm(p,m->asSubIdx,cmDsvStrcz(&m->value));
  1047. break;
  1048. default:
  1049. if( cmAudioSysDeliverMsg(p->asH,msg,msgByteCnt,cmInvalidId) != kOkAsRC )
  1050. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Message delivery to the audio system failed.");
  1051. break;
  1052. }
  1053. // errLabel:
  1054. return rc;
  1055. }