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

cmAudDsp.c 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. "srateMult", kIntTId, &asap->srateMult,
  311. NULL )) != kOkJsRC )
  312. {
  313. rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, cmStringNullGuard(p->asCfgArray[i].label));
  314. goto errLabel;
  315. }
  316. }
  317. }
  318. errLabel:
  319. return rc;
  320. }
  321. cmAdRC_t _cmAdSetup( cmAd_t* p )
  322. {
  323. unsigned i;
  324. cmAdRC_t rc = kOkAdRC;
  325. for(i=0; i<p->asCfgCnt; ++i)
  326. {
  327. unsigned j;
  328. p->asCfgArray[i].cfg.meterMs = p->meterMs;
  329. // BUG BUG BUG - each sub-system should have it's own network
  330. // manager, and socket port.
  331. p->asCfgArray[i].cfg.netH = p->netH;
  332. for(j=0; j<p->asCfgArray[i].cfg.ssCnt; ++j)
  333. {
  334. p->asCfgArray[i].cfg.ssArray[j].cbDataPtr = NULL;
  335. p->asCfgArray[i].cfg.ssArray[j].cbFunc = _cmAudDspCallback;
  336. p->asCfgArray[i].cfg.ssArray[j].args.rpt = p->err.rpt;
  337. }
  338. }
  339. return rc;
  340. }
  341. cmAdRC_t _cmAdCreateSerialPort( cmAd_t* p )
  342. {
  343. cmAdRC_t rc = kOkAdRC;
  344. if( p->serialDeviceStr != NULL )
  345. {
  346. p->serialPortH = cmSeCreate( &p->ctx, &p->serialPortH, p->serialDeviceStr, p->serialBaud, p->serialCfgFlags, NULL, NULL, p->serialPollPeriodMs );
  347. if( !cmSeIsOpen(p->serialPortH) )
  348. {
  349. rc = cmErrMsg(&p->err,kSerialDevCreateFailAdRC,"The serial device '%s' creation failed.",cmStringNullGuard(p->serialDeviceStr));
  350. }
  351. }
  352. return rc;
  353. }
  354. cmAdRC_t _cmAdCreateAggDevices( cmAd_t* p )
  355. {
  356. cmAdRC_t rc = kOkAdRC;
  357. unsigned i;
  358. if( cmApAggAllocate(p->err.rpt) != kOkAgRC )
  359. return cmErrMsg(&p->err,kAggDevSysFailAdRC,"The aggregate device system allocation failed.");
  360. for(i=0; i<p->aggDevCnt; ++i)
  361. {
  362. cmAdAggDev_t* adp = p->aggDevArray + i;
  363. if( cmApAggCreateDevice(adp->label,adp->physDevCnt,adp->physDevIdxArray,kInAggFl | kOutAggFl) != kOkAgRC )
  364. rc = cmErrMsg(&p->err,kAggDevCreateFailAdRC,"The aggregate device '%s' creation failed.",cmStringNullGuard(adp->label));
  365. }
  366. return rc;
  367. }
  368. cmAdRC_t _cmAdCreateNrtDevices( cmAd_t* p )
  369. {
  370. cmAdRC_t rc = kOkAdRC;
  371. unsigned i;
  372. if( cmApNrtAllocate(p->err.rpt) != kOkApRC )
  373. return cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device system allocation failed.");
  374. for(i=0; i<p->nrtDevCnt; ++i)
  375. {
  376. cmAdNrtDev_t* adp = p->nrtDevArray + i;
  377. if( cmApNrtCreateDevice(adp->label,adp->srate,adp->iChCnt,adp->oChCnt,adp->cbPeriodMs) != kOkApRC )
  378. rc = cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device '%s' creation failed.",cmStringNullGuard(adp->label));
  379. }
  380. return rc;
  381. }
  382. cmAdRC_t _cmAdCreateAfpDevices( cmAd_t* p )
  383. {
  384. cmAdRC_t rc = kOkAdRC;
  385. if( cmApFileAllocate(p->err.rpt) != kOkApRC )
  386. return cmErrMsg(&p->err,kAfpDevSysFailAdRC,"The audio file device system allocation failed.");
  387. unsigned i;
  388. // create the audio file devices
  389. for(i=0; i<p->afpDevCnt; ++i)
  390. {
  391. //const cmAudioSysFilePort_t* afp = cfg->afpArray + i;
  392. cmAdAfpDev_t* afp = p->afpDevArray + i;
  393. if( cmApFileDeviceCreate( afp->label, afp->inAudioFn, afp->outAudioFn, afp->oBits, afp->oChCnt ) != kOkApRC )
  394. rc = cmErrMsg(&p->err,kAfpDevSysFailAdRC,"The audio file device '%s' creation failed.",cmStringNullGuard(afp->label));
  395. }
  396. return rc;
  397. }
  398. cmAdRC_t _cmAdSendAudioSysCfgLabels( cmAd_t* p)
  399. {
  400. cmAdRC_t rc = kOkAdRC;
  401. unsigned i;
  402. for(i=0; i<p->asCfgCnt; ++i)
  403. {
  404. cmDspValue_t v;
  405. cmDsvSetStrcz(&v, p->asCfgArray[i].label);
  406. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kAudioSysCfgDuiId,0,i,p->asCfgCnt,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  407. {
  408. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending audio system cfg. label message to host.");
  409. break;
  410. }
  411. }
  412. return rc;
  413. }
  414. cmAdRC_t _cmAdSendAudioSysCfgDefault( cmAd_t* p)
  415. {
  416. cmAdRC_t rc = kOkAdRC;
  417. unsigned i;
  418. for(i=0; i<p->asCfgCnt; ++i)
  419. if( p->asCfgArray[i].dfltFl )
  420. {
  421. cmDspValue_t v;
  422. cmDsvSetStrcz(&v, p->asCfgArray[i].label);
  423. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kAudioSysCfgDfltDuiId,0,i,p->asCfgCnt,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  424. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending audio system default cfg. label message to host.");
  425. break;
  426. }
  427. return rc;
  428. }
  429. cmAdRC_t _cmAdSendDeviceLabels( cmAd_t* p )
  430. {
  431. cmAdRC_t rc = kOkAdRC;
  432. unsigned i,j;
  433. unsigned n = cmApDeviceCount();
  434. for(i=0; i<2; ++i)
  435. {
  436. bool inputFl = i==0;
  437. for(j=0; j<n; ++j)
  438. if( cmApDeviceChannelCount(j,inputFl) )
  439. {
  440. cmDspValue_t v;
  441. cmDsvSetStrcz(&v, cmApDeviceLabel(j));
  442. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kDeviceDuiId,inputFl,j,n,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  443. {
  444. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending device label message to host.");
  445. break;
  446. }
  447. }
  448. }
  449. return rc;
  450. }
  451. cmAdRC_t _cmAdSendProgramLabels( cmAd_t* p )
  452. {
  453. cmAdRC_t rc = kOkAdRC;
  454. unsigned pgmCnt = cmDspSysPgmCount(p->dsH);
  455. unsigned i;
  456. for(i=0; i<pgmCnt; ++i)
  457. {
  458. cmDspValue_t v;
  459. cmDsvSetStrcz(&v, cmDspPgmLabel(p->dsH,i));
  460. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kProgramDuiId,0,i,pgmCnt,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  461. {
  462. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending program label message to host.");
  463. break;
  464. }
  465. }
  466. return rc;
  467. }
  468. cmAdRC_t _cmAdSendProgramDefault( cmAd_t* p)
  469. {
  470. cmAdRC_t rc = kOkAdRC;
  471. if( p->dfltProgramLabel != NULL )
  472. {
  473. cmDspValue_t v;
  474. cmDsvSetStrcz(&v, p->dfltProgramLabel);
  475. if( cmMsgSend( &p->err,cmInvalidIdx,kUiSelAsId,kProgramDfltDuiId,0,0,0,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  476. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending default program label message to host.");
  477. }
  478. return rc;
  479. }
  480. cmAdRC_t _cmAudDspFree( cmAd_t* p )
  481. {
  482. cmAdRC_t rc = kOkAdRC;
  483. if( cmAudioSysFree(&p->asH) != kOkAsRC )
  484. {
  485. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system release failed.");
  486. goto errLabel;
  487. }
  488. if( cmDspSysFinalize(&p->dsH) != kOkDspRC )
  489. {
  490. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"DSP system finalization failed.");
  491. goto errLabel;
  492. }
  493. if( cmUdpNetFree(&p->netH) != kOkUnRC )
  494. {
  495. rc = cmErrMsg(&p->err,kNetSysFailAdRC,"UDP Network finalization failed.");
  496. goto errLabel;
  497. }
  498. if( cmSeIsOpen(p->serialPortH) )
  499. {
  500. if( cmSeDestroy(&p->serialPortH) != kOkSeRC )
  501. {
  502. rc = cmErrMsg(&p->err,kSerialPortFailAdRC,"Serial port finalize failed.");
  503. goto errLabel;
  504. }
  505. }
  506. if( cmMpIsInitialized() )
  507. if( cmMpFinalize() != kOkMpRC )
  508. {
  509. rc = cmErrMsg(&p->err,kMidiSysFailAdRC,"MIDI system finalization failed.");
  510. goto errLabel;
  511. }
  512. if( cmApFinalize() != kOkApRC )
  513. {
  514. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port finalization failed.");
  515. goto errLabel;
  516. }
  517. if( cmApBufFinalize() != kOkApRC )
  518. {
  519. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port buffer finalization failed.");
  520. goto errLabel;
  521. }
  522. if( cmApFileFree() != kOkApRC )
  523. {
  524. rc = cmErrMsg(&p->err,kAfpDevSysFailAdRC,"The audio file device system release failed.");
  525. goto errLabel;
  526. }
  527. if( cmApNrtFree() != kOkAgRC )
  528. {
  529. rc = cmErrMsg(&p->err,kNrtDevSysFailAdRC,"The non-real-time device system release failed.");
  530. goto errLabel;
  531. }
  532. if( cmApAggFree() != kOkAgRC )
  533. {
  534. rc = cmErrMsg(&p->err,kAggDevSysFailAdRC,"The aggregate device system release failed.");
  535. goto errLabel;
  536. }
  537. cmMemPtrFree(&p->nrtDevArray);
  538. unsigned i;
  539. for(i=0; i<p->aggDevCnt; ++i)
  540. cmMemPtrFree(&p->aggDevArray[i].physDevIdxArray);
  541. cmMemPtrFree(&p->aggDevArray);
  542. for(i=0; i<p->asCfgCnt; ++i)
  543. cmMemPtrFree(&p->asCfgArray[i].cfg.ssArray);
  544. cmMemPtrFree(&p->asCfgArray);
  545. cmMemPtrFree(&p->dsSsArray);
  546. if( cmJsonFinalize(&p->sysJsH) != kOkJsRC )
  547. {
  548. rc = cmErrMsg(&p->err,kJsonFailAdRC,"System JSON tree finalization failed.");
  549. goto errLabel;
  550. }
  551. if( p->sysJsFn != NULL )
  552. cmFsFreeFn(p->sysJsFn);
  553. cmMemFree(p);
  554. errLabel:
  555. return rc;
  556. }
  557. cmAdRC_t cmAudDspAlloc( cmCtx_t* ctx, cmAdH_t* hp, cmMsgSendFuncPtr_t cbFunc, void* cbDataPtr )
  558. {
  559. cmAdRC_t rc = kOkAdRC;
  560. cmAdRC_t rc0 = kOkAdRC;
  561. if((rc = cmAudDspFree(hp)) != kOkAdRC )
  562. return rc;
  563. cmAd_t* p = cmMemAllocZ(cmAd_t,1);
  564. cmErrSetup(&p->err,&ctx->rpt,"Audio DSP Engine");
  565. // form the audio dsp resource file name
  566. if((p->sysJsFn = cmFsMakeFn( cmFsPrefsDir(),cmAudDspSys_FILENAME,NULL,NULL)) == NULL )
  567. {
  568. rc = cmErrMsg(&p->err,kFileSysFailAdRC,"Unable to form the audio dsp system resource file name.");
  569. goto errLabel;
  570. }
  571. // open the audio dsp resource file
  572. if(cmJsonInitializeFromFile(&p->sysJsH,p->sysJsFn,ctx) != kOkJsRC )
  573. {
  574. rc = cmErrMsg(&p->err,kJsonFailAdRC,"Unable to open the audio dsp resource file: '%s'.",cmStringNullGuard(p->sysJsFn));
  575. goto errLabel;
  576. }
  577. // parse the JSON tree
  578. if((rc = _cmAdParseSysJsonTree(p)) != kOkAdRC )
  579. goto errLabel;
  580. // create the aggregate device
  581. if( _cmAdCreateAggDevices(p) != kOkAdRC )
  582. goto errLabel;
  583. // create the non-real-time devices
  584. if( _cmAdCreateNrtDevices(p) != kOkAdRC )
  585. goto errLabel;
  586. // create the audio file devices
  587. if( _cmAdCreateAfpDevices(p) != kOkAdRC )
  588. goto errLabel;
  589. // initialize the audio device system
  590. if( cmApInitialize(&ctx->rpt) != kOkApRC )
  591. {
  592. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port intialization failed.");
  593. goto errLabel;
  594. }
  595. // initialize the audio buffer
  596. if( cmApBufInitialize( cmApDeviceCount(), p->meterMs ) != kOkApRC )
  597. {
  598. rc = cmErrMsg(&p->err,kAudioPortFailAdRC,"Audio port buffer initialization failed.");
  599. goto errLabel;
  600. }
  601. // create the serial port
  602. if( _cmAdCreateSerialPort(p) != kOkAdRC )
  603. {
  604. rc = cmErrMsg(&p->err,kSerialPortFailAdRC,"The MIDI system initialization failed.");
  605. goto errLabel;
  606. }
  607. // initialize the MIDI system
  608. if( cmMpInitialize(ctx,NULL,NULL,p->midiPortBufByteCnt,"app") != kOkMpRC )
  609. {
  610. rc = cmErrMsg(&p->err,kMidiSysFailAdRC,"The MIDI system initialization failed.");
  611. goto errLabel;
  612. }
  613. // initialize the UDP network - but do not go into 'listening' mode.
  614. if( cmUdpNetAllocJson(ctx,&p->netH,p->sysJsH,_cmAdUdpNetCallback,p,kNetOptionalUnFl) != kOkUnRC )
  615. {
  616. cmErrMsg(&p->err,kNetSysFailAdRC,"The UDP network initialization failed.");
  617. goto errLabel;
  618. }
  619. if((rc = _cmAdSetup(p)) != kOkAdRC )
  620. goto errLabel;
  621. // initialize the DSP system
  622. if( cmDspSysInitialize(ctx,&p->dsH,p->netH,p->serialPortH) )
  623. {
  624. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP system initialization failed.");
  625. goto errLabel;
  626. }
  627. // allocate the audio system
  628. if( cmAudioSysAllocate(&p->asH, &ctx->rpt, NULL ) != kOkAsRC )
  629. {
  630. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system allocation failed.");
  631. goto errLabel;
  632. }
  633. p->cbFunc = cbFunc;
  634. p->cbDataPtr = cbDataPtr;
  635. p->curAsCfgIdx = cmInvalidIdx;
  636. p->ctx = *ctx;
  637. hp->h = p;
  638. errLabel:
  639. if( rc != kOkAdRC )
  640. rc0 = _cmAudDspFree(p);
  641. return rc == kOkAdRC ? rc0 : rc;
  642. }
  643. cmAdRC_t cmAudDspFree( cmAdH_t* hp )
  644. {
  645. cmAdRC_t rc = kOkAdRC;
  646. if( hp == NULL || cmAudDspIsValid(*hp)==false )
  647. return kOkAdRC;
  648. cmAd_t* p = _cmAdHandleToPtr(*hp);
  649. if((rc = _cmAudDspFree(p)) != kOkAdRC )
  650. return rc;
  651. hp->h = NULL;
  652. return rc;
  653. }
  654. cmAdRC_t cmAudDspSendSetup( cmAdH_t h )
  655. {
  656. cmAdRC_t rc = kOkAdRC;
  657. cmAd_t* p = _cmAdHandleToPtr( h );
  658. // notify the client of the available audio system configurations
  659. if((rc = _cmAdSendAudioSysCfgLabels(p)) != kOkAdRC )
  660. goto errLabel;
  661. // notify the client of the available devices
  662. if((rc = _cmAdSendDeviceLabels(p)) != kOkAdRC)
  663. goto errLabel;
  664. // notify the client of the available programs
  665. if((rc = _cmAdSendProgramLabels(p)) != kOkAdRC )
  666. goto errLabel;
  667. // notify the client of the default audio sys cfg
  668. if((rc= _cmAdSendAudioSysCfgDefault(p)) != kOkAdRC )
  669. goto errLabel;
  670. // notify the client of the default program
  671. if((rc= _cmAdSendProgramDefault(p)) != kOkAdRC )
  672. goto errLabel;
  673. errLabel:
  674. return rc;
  675. }
  676. bool cmAudDspIsValid( cmAdH_t h )
  677. { return h.h != NULL; }
  678. cmAdRC_t _cmAudDspUnloadPgm( cmAd_t* p, unsigned asSubSysIdx )
  679. {
  680. cmAdRC_t rc = kOkAdRC;
  681. const cmAdAsCfg_t* cfgPtr = NULL;
  682. // Must disable audio thread callbacks to _cmAudDspCallback()
  683. // while changing DSP system data structures.
  684. if( cmAudioSysIsEnabled(p->asH) )
  685. if(cmAudioSysEnable(p->asH,false) != kOkAsRC )
  686. {
  687. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system could not be disabled.");
  688. goto errLabel;
  689. }
  690. // validate the sub-system index
  691. if( asSubSysIdx >= p->dsSsCnt )
  692. {
  693. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The invalid sub-system index %i was encountered while unloading a program.",asSubSysIdx);
  694. goto errLabel;
  695. }
  696. // if a valid cfg recd exists
  697. if( p->curAsCfgIdx != cmInvalidIdx )
  698. {
  699. // pointer to audio system configuration
  700. cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  701. // count of audio system sub-systems should be the same as the current cfg
  702. assert( p->dsSsCnt == cfgPtr->cfg.ssCnt );
  703. // mark the DSP program as unloaded and pre-sync
  704. p->dsSsArray[ asSubSysIdx ].isLoadedFl = false;
  705. p->dsSsArray[ asSubSysIdx ].isSyncFl = false;
  706. p->syncFl = false;
  707. }
  708. // unload the current program
  709. if( cmDspSysUnload(p->dsSsArray[asSubSysIdx].dsH) != kOkDspRC )
  710. {
  711. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"Program unload failed.");
  712. goto errLabel;
  713. }
  714. errLabel:
  715. return rc;
  716. }
  717. cmAdRC_t _cmAudDspUnloadAudioSys( cmAd_t* p )
  718. {
  719. unsigned i;
  720. cmAdRC_t rc = kOkAdRC;
  721. p->syncFl = false;
  722. for(i=1; i<p->dsSsCnt; ++i)
  723. {
  724. if((rc = _cmAudDspUnloadPgm(p,i)) != kOkAdRC )
  725. goto errLabel;
  726. if( cmDspSysFinalize(&p->dsSsArray[i].dsH) != kOkDspRC )
  727. {
  728. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"DSP system finalization failed.");
  729. goto errLabel;
  730. }
  731. }
  732. p->curAsCfgIdx = cmInvalidIdx;
  733. errLabel:
  734. return rc;
  735. }
  736. cmAdRC_t _cmAdSendIntMsgToHost( cmAd_t* p, unsigned asSubIdx, unsigned selId, unsigned flags, unsigned intValue )
  737. {
  738. cmAdRC_t rc = kOkAdRC;
  739. cmDspValue_t v;
  740. cmDsvSetUInt(&v,intValue);
  741. if( cmMsgSend( &p->err,asSubIdx,kUiSelAsId,selId,flags,cmInvalidId,cmInvalidId,&v,p->cbFunc,p->cbDataPtr) != kOkMsgRC )
  742. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Error sending message to host.");
  743. return rc;
  744. }
  745. // verify that a valid audio cfg has been selected
  746. cmAdRC_t _cmAdIsAudioSysLoaded( cmAd_t* p )
  747. {
  748. cmAdRC_t rc = kOkAdRC;
  749. if( cmAudioSysHandleIsValid(p->asH) == false )
  750. {
  751. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system is not allocated.");
  752. goto errLabel;
  753. }
  754. if( p->curAsCfgIdx == cmInvalidIdx )
  755. return kInvalidCfgIdxAdRC;
  756. // verify that an audio system is loaded
  757. if( cmAudioSysIsInitialized(p->asH) && p->curAsCfgIdx == cmInvalidIdx )
  758. {
  759. rc = cmErrMsg(&p->err,kInvalidCfgIdxAdRC,"The audio system has not been configured.");
  760. goto errLabel;
  761. }
  762. // count of audio system sub-systems should be the same as the current cfg
  763. assert( p->dsSsCnt == p->asCfgArray[p->curAsCfgIdx].cfg.ssCnt );
  764. errLabel:
  765. return rc;
  766. }
  767. // verify that a valid audio cfg and DSP program has been selected
  768. cmAdRC_t _cmAdIsPgmLoaded( cmAd_t* p, bool verboseFl )
  769. {
  770. cmAdRC_t rc;
  771. // a program cannot be loaded if the audio system has not been configured
  772. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  773. return rc;
  774. unsigned i;
  775. // for each sub-system
  776. for(i=0; i<p->dsSsCnt; ++i)
  777. {
  778. // verify that the DSP system has been created
  779. if( cmDspSysIsValid(p->dsSsArray[i].dsH) == false )
  780. return cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP sub-system at index %i is not initialized.",i);
  781. // verify that the DSP program was loaded
  782. if( p->dsSsArray[ i ].isLoadedFl == false )
  783. {
  784. if( verboseFl )
  785. cmErrMsg(&p->err,kNoPgmLoadedAdRC,"There is no program loaded.");
  786. return kNoPgmLoadedAdRC;
  787. }
  788. }
  789. return rc;
  790. }
  791. bool _cmAudDspIsPgmSynced( cmAd_t* p )
  792. {
  793. unsigned syncCnt = 0;
  794. unsigned i;
  795. if( p->syncFl )
  796. return true;
  797. // if the pgm is not loaded then it cannot be sync'd
  798. if(_cmAdIsPgmLoaded(p,false) != kOkAdRC )
  799. return false;
  800. // check each sub-system
  801. for(i=0; i<p->dsSsCnt; ++i)
  802. {
  803. unsigned syncState = cmDspSysSyncState(p->dsSsArray[i].dsH);
  804. // if the subsys is already synced
  805. if( p->dsSsArray[i].isSyncFl )
  806. ++syncCnt;
  807. else
  808. {
  809. switch( syncState )
  810. {
  811. // the sub-sys is pre or pending sync mode
  812. case kSyncPreDspId:
  813. case kSyncPendingDspId:
  814. break;
  815. // sync mode completed - w/ success or fail
  816. case kSyncSuccessDspId:
  817. case kSyncFailDspId:
  818. {
  819. // notify the client of the the sync state
  820. bool syncFl = syncState == kSyncSuccessDspId;
  821. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSyncDuiId,syncFl,cmInvalidIdx);
  822. p->dsSsArray[i].isSyncFl = syncFl;
  823. }
  824. break;
  825. }
  826. }
  827. }
  828. p->syncFl = syncCnt == p->dsSsCnt;
  829. return p->syncFl;
  830. }
  831. cmAdRC_t _cmAudDspLoadPgm( cmAd_t* p, unsigned asSubSysIdx, unsigned pgmIdx )
  832. {
  833. cmAdRC_t rc = kOkAdRC;
  834. unsigned i;
  835. p->syncFl = false;
  836. // the audio system must be configured before a program is loaded
  837. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  838. return cmErrMsg(&p->err,rc,"The audio system is not configured. Program load failed.");
  839. // validate the sub-system index arg.
  840. if( asSubSysIdx!=cmInvalidIdx && asSubSysIdx >= p->dsSsCnt )
  841. {
  842. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid. Program load failed.",asSubSysIdx);
  843. goto errLabel;
  844. }
  845. // for each sub-system
  846. for(i=0; i<p->dsSsCnt; ++i)
  847. if( asSubSysIdx==cmInvalidIdx || i==asSubSysIdx )
  848. {
  849. // unload any currently loaded program on this sub-system
  850. // (unloading a program automatically disables the audio system)
  851. if((rc = _cmAudDspUnloadPgm(p, i )) != kOkAdRC )
  852. goto errLabel;
  853. // load the program
  854. if( cmDspSysLoad(p->dsSsArray[ i ].dsH, cmAudioSysContext(p->asH,i), pgmIdx ) != kOkDspRC )
  855. {
  856. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The program load failed on audio sub-system %i.",i);
  857. goto errLabel;
  858. }
  859. // update the state of the DSP sub-system
  860. p->dsSsArray[i].curPgmIdx = pgmIdx;
  861. p->dsSsArray[i].isLoadedFl = true;
  862. p->dsSsArray[i].isSyncFl = false;
  863. p->syncFl = false;
  864. // notify the host of the new program
  865. _cmAdSendIntMsgToHost(p,i,kSetPgmDuiId,0,pgmIdx);
  866. }
  867. errLabel:
  868. return rc;
  869. }
  870. cmAdRC_t _cmAudDspPrintPgm( cmAd_t* p, unsigned asSubSysIdx, const cmChar_t* fn )
  871. {
  872. cmAdRC_t rc = kOkAdRC;
  873. unsigned i;
  874. // the audio system must be configured before a program is loaded
  875. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  876. return cmErrMsg(&p->err,rc,"The audio system is not configured. Program print failed.");
  877. // validate the sub-system index arg.
  878. if( asSubSysIdx!=cmInvalidIdx && asSubSysIdx >= p->dsSsCnt )
  879. {
  880. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid. Program print failed.",asSubSysIdx);
  881. goto errLabel;
  882. }
  883. // for each sub-system
  884. for(i=0; i<p->dsSsCnt; ++i)
  885. if( i==asSubSysIdx || asSubSysIdx==cmInvalidIdx )
  886. {
  887. if( cmDspSysPrintPgm(p->dsSsArray[i].dsH,fn) != kOkDspRC )
  888. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The program print failed.");
  889. else
  890. {
  891. if( cmDspPgmJsonToDot(&p->ctx,fn,fn) != kOkDspRC )
  892. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The program print conversion to DOT failed.");
  893. }
  894. break;
  895. }
  896. errLabel:
  897. return rc;
  898. }
  899. cmAdRC_t _cmAdReinitAudioSys( cmAd_t* p )
  900. {
  901. cmAdRC_t rc = kOkAdRC;
  902. p->syncFl = false;
  903. // pointer to the new audio system configuration
  904. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  905. // initialize the audio system
  906. if( cmAudioSysInitialize(p->asH, &cfgPtr->cfg ) != kOkAsRC )
  907. {
  908. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system initialization failed.");
  909. goto errLabel;
  910. }
  911. // reload any currently loaded programs
  912. unsigned i;
  913. for(i=0; i<p->dsSsCnt; ++i)
  914. {
  915. unsigned pgmIdx;
  916. if((pgmIdx = p->dsSsArray[i].curPgmIdx) != cmInvalidIdx )
  917. if((rc = _cmAudDspLoadPgm(p,i,pgmIdx)) != kOkAdRC )
  918. break;
  919. }
  920. errLabel:
  921. return rc;
  922. }
  923. cmAdRC_t _cmAudDspLoadAudioSys( cmAd_t* p, unsigned asCfgIdx )
  924. {
  925. cmAdRC_t rc = kOkAdRC;
  926. cmAdAsCfg_t* cfgPtr;
  927. unsigned i;
  928. // validate asCfgIdx
  929. if( asCfgIdx >= p->asCfgCnt )
  930. {
  931. cmErrMsg(&p->err,kInvalidCfgIdxAdRC,"The audio system index %i is invalid.",asCfgIdx);
  932. goto errLabel;
  933. }
  934. // clear the current audio system setup - this will automatically disable the audio system
  935. if((rc = _cmAudDspUnloadAudioSys(p)) != kOkAdRC )
  936. goto errLabel;
  937. // pointer to the new audio system configuration
  938. cfgPtr = p->asCfgArray + asCfgIdx;
  939. // get the count of audio system sub-systems
  940. p->dsSsCnt = cfgPtr->cfg.ssCnt;
  941. // store the index of the current audio system configuration
  942. p->curAsCfgIdx = asCfgIdx;
  943. if( p->dsSsCnt > 0 )
  944. {
  945. p->dsSsArray = cmMemResizeZ(cmAdDsSubSys_t, p->dsSsArray, p->dsSsCnt );
  946. for(i=0; i<p->dsSsCnt; ++i)
  947. {
  948. cmDspSysH_t dsH;
  949. // the first sub-system will always use the existing DSP system handle ...
  950. if( i==0 )
  951. dsH = p->dsH;
  952. else
  953. {
  954. // ... and allocate additional DSP systems when more than one sub-sys is
  955. // defined in the audio system configuration
  956. if( cmDspSysInitialize(&p->ctx,&dsH,p->netH,p->serialPortH) != kOkDspRC )
  957. {
  958. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"Unable to initialize an additional DSP system.");
  959. goto errLabel;
  960. }
  961. }
  962. p->dsSsArray[i].dsH = dsH;
  963. p->dsSsArray[i].curPgmIdx = cmInvalidIdx;
  964. p->dsSsArray[i].isLoadedFl= false;
  965. // this cbDataPtr is picked up in _cmAudDspCallback().
  966. // It is used to connect the audio system to a DSP system handle.
  967. cfgPtr->cfg.ssArray[i].cbDataPtr = p->dsSsArray + i;
  968. }
  969. }
  970. // notify the client of the change of audio configuration
  971. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSetAudioCfgDuiId,0,asCfgIdx);
  972. // notify the client of the count of audio sub-systems
  973. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kSubSysCntDuiId,0,p->dsSsCnt);
  974. // for each sub-system
  975. for(i=0; i<p->dsSsCnt; ++i)
  976. {
  977. // notify the client of the currently selected devices
  978. _cmAdSendIntMsgToHost(p,i,kSetAudioDevDuiId,true, cfgPtr->cfg.ssArray[i].args.inDevIdx);
  979. _cmAdSendIntMsgToHost(p,i,kSetAudioDevDuiId,false,cfgPtr->cfg.ssArray[i].args.outDevIdx);
  980. // notify the client of the sample rate
  981. _cmAdSendIntMsgToHost(p,i,kSetSampleRateDuiId,0,(unsigned)cfgPtr->cfg.ssArray[i].args.srate);
  982. _cmAdSendIntMsgToHost(p,i,kSetPgmDuiId,0,cmInvalidIdx);
  983. }
  984. // the audio system configuration changed so we need to initialize the audio system
  985. if((rc = _cmAdReinitAudioSys(p)) != kOkAdRC )
  986. goto errLabel;
  987. errLabel:
  988. if( rc != kOkAdRC )
  989. _cmAudDspUnloadAudioSys(p);
  990. return rc;
  991. }
  992. cmAdRC_t _cmAudDspEnableAudio( cmAd_t* p, bool enableFl )
  993. {
  994. cmAdRC_t rc = kOkAdRC;
  995. if( enableFl )
  996. {
  997. // verify an audio system cfg and DSP program has been selected
  998. if(( rc = _cmAdIsPgmLoaded(p,true)) != kOkAdRC )
  999. return cmErrMsg(&p->err,rc,"Audio enable failed.");
  1000. // if the audio system is already enabled/disabled then do nothing
  1001. if( cmAudioSysIsEnabled(p->asH) == enableFl )
  1002. return kOkAdRC;
  1003. // for each sub-system
  1004. unsigned i;
  1005. for(i=0; i<p->dsSsCnt; ++i)
  1006. {
  1007. if( cmDspSysReset(p->dsSsArray[i].dsH) != kOkDspRC )
  1008. {
  1009. rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP system reset failed.");
  1010. goto errLabel;
  1011. }
  1012. }
  1013. }
  1014. // start/stop the audio sub-system
  1015. if( cmAudioSysEnable(p->asH,enableFl) != kOkAsRC )
  1016. {
  1017. rc = cmErrMsg(&p->err,kAudioSysFailAdRC,"The audio system %s failed.", enableFl ? "enable" : "disable");
  1018. goto errLabel;
  1019. }
  1020. // notify the host of the new enable state
  1021. _cmAdSendIntMsgToHost(p,cmInvalidIdx,kEnableDuiId,enableFl,cmInvalidIdx);
  1022. errLabel:
  1023. return rc;
  1024. }
  1025. cmAdRC_t _cmAudDspSetDevice(cmAd_t* p,unsigned asSubIdx, bool inputFl, unsigned devIdx)
  1026. {
  1027. cmAdRC_t rc;
  1028. // a device cannot be set if the audio system is not already configured
  1029. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  1030. return cmErrMsg(&p->err,rc,"Set audio device failed.");
  1031. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  1032. // validate the sub-system index
  1033. if( asSubIdx >= p->dsSsCnt )
  1034. {
  1035. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid.",asSubIdx);
  1036. goto errLabel;
  1037. }
  1038. // assign the new device index to the indicated audio system configuration recd
  1039. if( inputFl )
  1040. {
  1041. if( cfgPtr->cfg.ssArray[ asSubIdx ].args.inDevIdx != devIdx )
  1042. cfgPtr->cfg.ssArray[ asSubIdx ].args.inDevIdx = devIdx;
  1043. }
  1044. else
  1045. {
  1046. if( cfgPtr->cfg.ssArray[ asSubIdx ].args.outDevIdx != devIdx )
  1047. cfgPtr->cfg.ssArray[ asSubIdx ].args.outDevIdx = devIdx;
  1048. }
  1049. // notify the host that the new device has been set
  1050. _cmAdSendIntMsgToHost(p,asSubIdx,kSetAudioDevDuiId,inputFl, devIdx);
  1051. // reinitialize the audio system
  1052. rc = _cmAdReinitAudioSys(p);
  1053. errLabel:
  1054. return rc;
  1055. }
  1056. cmAdRC_t _cmAudDspSetSampleRate(cmAd_t* p, unsigned asSubIdx, double srate )
  1057. {
  1058. cmAdRC_t rc;
  1059. if((rc = _cmAdIsAudioSysLoaded(p)) != kOkAdRC )
  1060. return cmErrMsg(&p->err,rc,"Set audio device failed.");
  1061. cmAdAsCfg_t* cfgPtr = p->asCfgArray + p->curAsCfgIdx;
  1062. // validate the sub-system index
  1063. if( asSubIdx != cmInvalidIdx && asSubIdx >= p->dsSsCnt )
  1064. {
  1065. rc = cmErrMsg(&p->err,kInvalidSubSysIdxAdRC,"The sub-system index %i is invalid.",asSubIdx);
  1066. goto errLabel;
  1067. }
  1068. unsigned i;
  1069. for(i=0; i<p->dsSsCnt; ++i)
  1070. {
  1071. // assign the new device index to the indicated audio system configuration recd
  1072. if( asSubIdx==cmInvalidIdx || asSubIdx == i )
  1073. {
  1074. if( cfgPtr->cfg.ssArray[ i ].args.srate != srate )
  1075. cfgPtr->cfg.ssArray[ i ].args.srate = srate;
  1076. }
  1077. }
  1078. // notify the client of the new sample rate
  1079. _cmAdSendIntMsgToHost(p,asSubIdx,kSetSampleRateDuiId,0,(unsigned)srate);
  1080. // reinitialize the audio system
  1081. rc = _cmAdReinitAudioSys(p);
  1082. errLabel:
  1083. return rc;
  1084. }
  1085. cmAdRC_t _cmAudDspClientMsgPoll( cmAd_t* p )
  1086. {
  1087. unsigned i = 0;
  1088. // if the program is not synced then don't bother polling the audio system
  1089. if( _cmAudDspIsPgmSynced(p) == false )
  1090. return kOkAdRC;
  1091. for(i=0; i<p->msgsPerClientPoll; ++i)
  1092. {
  1093. if( cmAudioSysIsMsgWaiting(p->asH) == 0 )
  1094. break;
  1095. if(cmAudioSysReceiveMsg(p->asH,NULL,0) != kOkAsRC )
  1096. return cmErrMsg(&p->err,kAudioSysFailAdRC,"The delivery of an audio system msg for the client failed.");
  1097. }
  1098. return kOkAdRC;
  1099. }
  1100. cmAdRC_t cmAudDspReceiveClientMsg( cmAdH_t h, unsigned msgByteCnt, const void* msg )
  1101. {
  1102. cmAdRC_t rc = kOkAdRC;
  1103. cmAd_t* p = _cmAdHandleToPtr(h);
  1104. cmDspUiHdr_t* m = (cmDspUiHdr_t*)msg;
  1105. /*
  1106. if( m->uiId != kUiSelAsId )
  1107. {
  1108. rc = cmErrMsg(&p->err,kUnknownMsgTypeAdRC,"The message type %i is unknown.");
  1109. goto errLabel;
  1110. }
  1111. */
  1112. switch( m->selId )
  1113. {
  1114. case kDevReportDuiId:
  1115. cmRptPrintf(p->err.rpt,"\nAUDIO DEVICES\n");
  1116. cmApReport(p->err.rpt);
  1117. cmRptPrintf(p->err.rpt,"\nMIDI DEVICES\n");
  1118. cmMpReport(p->err.rpt);
  1119. break;
  1120. case kSetAudioCfgDuiId:
  1121. rc = _cmAudDspLoadAudioSys(p,cmDsvUInt(&m->value));
  1122. break;
  1123. case kSetPgmDuiId:
  1124. rc = _cmAudDspLoadPgm(p,m->asSubIdx,cmDsvUInt(&m->value));
  1125. break;
  1126. case kSetAudioDevDuiId:
  1127. rc = _cmAudDspSetDevice(p,m->asSubIdx,m->flags,cmDsvUInt(&m->value));
  1128. break;
  1129. case kSetSampleRateDuiId:
  1130. rc = _cmAudDspSetSampleRate(p,m->asSubIdx,cmDsvDouble(&m->value));
  1131. break;
  1132. case kEnableDuiId:
  1133. rc = _cmAudDspEnableAudio(p,m->flags);
  1134. break;
  1135. case kSetNotifyEnableDuiId:
  1136. cmAudioSysStatusNotifyEnable(p->asH, cmInvalidIdx, m->flags );
  1137. break;
  1138. case kClientMsgPollDuiId:
  1139. rc = _cmAudDspClientMsgPoll(p);
  1140. break;
  1141. case kPrintPgmDuiId:
  1142. _cmAudDspPrintPgm(p,m->asSubIdx,cmDsvStrcz(&m->value));
  1143. break;
  1144. default:
  1145. if( cmAudioSysDeliverMsg(p->asH,msg,msgByteCnt,cmInvalidId) != kOkAsRC )
  1146. rc = cmErrMsg(&p->err,kSendMsgFailAdRC,"Message delivery to the audio system failed.");
  1147. break;
  1148. }
  1149. // errLabel:
  1150. return rc;
  1151. }