libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmJson.h"
  9. #include "cmThread.h"
  10. #include "cmUdpPort.h"
  11. #include "cmRtSysMsg.h"
  12. #include "cmRtSys.h"
  13. #include "cmUiDrvr.h"
  14. #include "cmUi.h"
  15. #include "cmUiRtSysMstr.h"
  16. enum
  17. {
  18. kLabelAmId,
  19. kInSliderAmId,
  20. kInMeterAmId,
  21. kInToneAmId,
  22. kInPassAmId,
  23. kInMuteAmId,
  24. kOutSliderAmId,
  25. kOutMeterAmId,
  26. kOutToneAmId,
  27. kOutPassAmId,
  28. kOutMuteAmId,
  29. kAmCnt
  30. };
  31. enum
  32. {
  33. kMinDb = 24,
  34. kMaxDb = -24,
  35. kMtrMin = 0,
  36. kMtrMax = 100
  37. };
  38. typedef struct cmAmPanel_str
  39. {
  40. unsigned rtSubIdx;
  41. unsigned panelId;
  42. unsigned updateId;
  43. unsigned wakeupId;
  44. unsigned msgCbId;
  45. unsigned audioCbId;
  46. unsigned updateCnt;
  47. unsigned wakeupCnt;
  48. unsigned msgCbCnt;
  49. unsigned audioCbCnt;
  50. unsigned baseOutId;
  51. unsigned iDevIdx;
  52. unsigned oDevIdx;
  53. unsigned iChCnt;
  54. unsigned oChCnt;
  55. unsigned a[ kAmCnt ];
  56. struct cmAmPanel_str* link;
  57. } cmAmPanel_t;
  58. typedef struct
  59. {
  60. cmErr_t err;
  61. unsigned appId;
  62. cmUiH_t uiH;
  63. cmRtSysH_t asH;
  64. unsigned nextId;
  65. cmAmPanel_t* list;
  66. } cmAm_t;
  67. cmUiRtMstrH_t cmUiRtMstrNullHandle = cmSTATIC_NULL_HANDLE;
  68. cmAm_t* _cmUiAmHandleToPtr( cmUiRtMstrH_t h )
  69. {
  70. cmAm_t* p = (cmAm_t*)h.h;
  71. assert( p!=NULL);
  72. return p;
  73. }
  74. cmAmRC_t _cmUiAmFreePanels( cmAm_t* p, bool callDriverFl )
  75. {
  76. cmAmRC_t rc = kOkAmRC;
  77. cmAmPanel_t* pp = p->list;
  78. while( pp != NULL )
  79. {
  80. cmAmPanel_t* np = pp->link;
  81. unsigned panelId = pp->panelId;
  82. if( callDriverFl )
  83. if( cmUiClearPanel( p->uiH, p->appId, panelId ) != kOkUiRC )
  84. {
  85. rc = cmErrMsg(&p->err,kUiFailAmRC,"The panel %i clear failed.",panelId);
  86. goto errLabel;
  87. }
  88. cmMemFree(pp);
  89. pp = np;
  90. }
  91. p->nextId = 0;
  92. p->list = NULL;
  93. errLabel:
  94. return rc;
  95. }
  96. cmAmRC_t _cmUiAmFree( cmAm_t* p )
  97. {
  98. _cmUiAmFreePanels(p,false);
  99. if( cmUiDestroyApp( p->uiH, p->appId ) != kOkUiRC )
  100. cmErrMsg(&p->err,kUiFailAmRC,"UI Mgr. app destroy failed.");
  101. cmMemFree(p);
  102. return kOkAmRC;
  103. }
  104. cmAmRC_t cmUiRtSysMstrAlloc( cmCtx_t* ctx, cmUiRtMstrH_t* hp, cmUiH_t uiH, cmRtSysH_t asH, unsigned appId )
  105. {
  106. cmAmRC_t rc = kOkAmRC;
  107. if((rc = cmUiRtSysMstrFree(hp)) != kOkAmRC )
  108. return rc;
  109. cmAm_t* p = cmMemAllocZ(cmAm_t,1);
  110. cmErrSetup(&p->err,&ctx->rpt,"Audio System Master UI");
  111. p->appId = appId;
  112. p->uiH = uiH;
  113. p->asH = asH;
  114. p->nextId = 0;
  115. // allocate the UI Mgr. app. slot for the audio system master control UI.
  116. if( cmUiCreateApp( uiH, appId, cmInvalidId ) != kOkUiRC )
  117. {
  118. rc = cmErrMsg(&p->err,kUiFailAmRC,"The UI Mgr. failed while creating the Audio System UI app. slot.");
  119. goto errLabel;
  120. }
  121. hp->h = p;
  122. errLabel:
  123. if( rc != kOkAmRC )
  124. _cmUiAmFree(p);
  125. return rc;
  126. }
  127. cmAmRC_t cmUiRtSysMstrFree( cmUiRtMstrH_t* hp )
  128. {
  129. cmAmRC_t rc = kOkAmRC;
  130. if(hp==NULL || cmUiRtSysMstrIsValid(*hp)==false )
  131. return kOkAmRC;
  132. cmAm_t* p = _cmUiAmHandleToPtr(*hp);
  133. if((rc = _cmUiAmFree(p)) != kOkAmRC )
  134. return rc;
  135. hp->h = NULL;
  136. return rc;
  137. }
  138. bool cmUiRtSysMstrIsValid( cmUiRtMstrH_t h )
  139. { return h.h != NULL; }
  140. cmAmRC_t cmUiRtSysMstrInitialize( cmUiRtMstrH_t amH, const cmRtSysCtx_t* c, const cmChar_t* inDevLabel, const cmChar_t* outDevLabel )
  141. {
  142. cmAmRC_t rc = kOkAmRC;
  143. cmAm_t* p = _cmUiAmHandleToPtr(amH);
  144. cmUiH_t uiH = p->uiH;
  145. unsigned panelId = cmInvalidId;
  146. unsigned colW = 50;
  147. unsigned ctlW = 45;
  148. unsigned n = 31;
  149. cmChar_t chNumStr[ n+1 ];
  150. int w;
  151. cmAmPanel_t* pp = NULL;
  152. // This function is called once for each audio sub-system.
  153. // If this is the first call in the sequence then clear the previous setup.
  154. if( c->rtSubIdx == 0 )
  155. {
  156. if((rc = _cmUiAmFreePanels(p,true)) != kOkAmRC )
  157. goto errLabel;
  158. assert(p->list == NULL );
  159. }
  160. // create the panel recd and link it to the beginning of the list
  161. pp = cmMemAllocZ(cmAmPanel_t,1);
  162. pp->link = p->list;
  163. p->list = pp;
  164. pp->rtSubIdx = c->rtSubIdx;
  165. pp->iDevIdx = c->ss->args.inDevIdx;
  166. pp->oDevIdx = c->ss->args.outDevIdx;
  167. pp->iChCnt = c->iChCnt;
  168. pp->oChCnt = c->oChCnt;
  169. pp->panelId = p->nextId++;
  170. pp->updateId = p->nextId++;
  171. pp->wakeupId = p->nextId++;
  172. pp->msgCbId = p->nextId++;
  173. pp->audioCbId = p->nextId++;
  174. pp->a[kLabelAmId] = p->nextId;
  175. pp->a[kInSliderAmId] = p->nextId += c->iChCnt;
  176. pp->a[kInMeterAmId] = p->nextId += c->iChCnt;
  177. pp->a[kInToneAmId] = p->nextId += c->iChCnt;
  178. pp->a[kInPassAmId] = p->nextId += c->iChCnt;
  179. pp->a[kInMuteAmId] = p->nextId += c->iChCnt;
  180. pp->baseOutId = p->nextId += c->iChCnt;
  181. pp->a[kOutSliderAmId] = pp->baseOutId;
  182. pp->a[kOutMeterAmId] = p->nextId += c->oChCnt;
  183. pp->a[kOutToneAmId] = p->nextId += c->oChCnt;
  184. pp->a[kOutPassAmId] = p->nextId += c->oChCnt;
  185. pp->a[kOutMuteAmId] = p->nextId += c->oChCnt;
  186. p->nextId += c->oChCnt;
  187. panelId = pp->panelId;
  188. if( cmUiCreatePanel(uiH, p->appId, panelId, "Master", 0 ) != kOkUiRC )
  189. {
  190. rc = cmErrMsg(&p->err,kUiFailAmRC,"Panel %i create failed.",panelId);
  191. goto errLabel;
  192. }
  193. cmUiSetFillRows( uiH, p->appId, panelId, true );
  194. cmUiCreateProgress(uiH, p->appId, panelId, pp->updateId, "Update", 0, 0, 1, 0 );
  195. cmUiCreateProgress(uiH, p->appId, panelId, pp->wakeupId, "Wakeup", 0, 0, 1, 0 );
  196. cmUiCreateProgress(uiH, p->appId, panelId, pp->msgCbId, "Message", 0, 0, 1, 0 );
  197. cmUiCreateProgress(uiH, p->appId, panelId, pp->audioCbId,"Audio", 0, 0, 1, 0 );
  198. cmUiSetFillRows( uiH, p->appId, panelId, false );
  199. cmUiNewLine( uiH, p->appId, panelId );
  200. cmUiCreateLabel( uiH, p->appId, panelId, cmInvalidId, inDevLabel, kInsideUiFl | kLeftUiFl );
  201. cmUiNewLine( uiH, p->appId, panelId );
  202. unsigned i;
  203. for(i=0; i<c->iChCnt; ++i)
  204. {
  205. snprintf(chNumStr,n,"%i",i);
  206. cmUiSetNextW( uiH, p->appId, panelId, ctlW );
  207. cmUiCreateLabel( uiH, p->appId, panelId, cmInvalidId, chNumStr, 0 );
  208. cmUiCreateVSlider(uiH, p->appId, panelId, pp->a[kInSliderAmId] + i, NULL, 0, kMinDb, kMaxDb, 0.1, 0 );
  209. cmUiPlaceRight( uiH, p->appId, panelId );
  210. cmUiCreateVMeter( uiH, p->appId, panelId, pp->a[kInMeterAmId] + i, NULL, 0, kMtrMin, kMtrMax, 0 );
  211. w = cmUiSetW( uiH, p->appId, panelId, ctlW );
  212. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kInToneAmId] + i, "T", 0, false );
  213. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kInPassAmId] + i, "P", 0, false );
  214. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kInMuteAmId] + i, "M", 0, false );
  215. cmUiSetW( uiH, p->appId, panelId, w );
  216. cmUiSetBaseCol( uiH, p->appId, panelId, 5 + (i+1)*colW);
  217. }
  218. cmUiSetBaseCol( uiH, p->appId, panelId, 0);
  219. cmUiNewLine( uiH, p->appId, panelId );
  220. cmUiCreateLabel( uiH,p->appId, panelId, cmInvalidId, outDevLabel, kInsideUiFl | kLeftUiFl );
  221. cmUiNewLine( uiH, p->appId, panelId );
  222. for(i=0; i<c->oChCnt; ++i)
  223. {
  224. snprintf(chNumStr,n,"%i",i);
  225. cmUiSetNextW( uiH, p->appId, panelId, ctlW );
  226. cmUiCreateLabel( uiH, p->appId, panelId, cmInvalidId, chNumStr, 0 );
  227. cmUiCreateVSlider(uiH, p->appId, panelId, pp->a[kOutSliderAmId] + i, NULL, 0, kMinDb, kMaxDb, 0.1, 0 );
  228. cmUiPlaceRight( uiH, p->appId, panelId );
  229. cmUiCreateVMeter( uiH, p->appId, panelId, pp->a[kOutMeterAmId] + i, NULL, 0, kMtrMin, kMtrMax, 0 );
  230. w = cmUiSetW( uiH, p->appId, panelId, ctlW );
  231. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kOutToneAmId] + i, "T", 0, false );
  232. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kOutPassAmId] + i, "P", 0, false );
  233. cmUiCreateCheck( uiH, p->appId, panelId, pp->a[kOutMuteAmId] + i, "M", 0, false );
  234. cmUiSetW( uiH, p->appId, panelId, w );
  235. cmUiSetBaseCol( uiH, p->appId, panelId, 5 + (i+1)*colW);
  236. }
  237. errLabel:
  238. return rc;
  239. }
  240. cmAmPanel_t* _cmUiAmFindPanel( cmAm_t* p, unsigned panelId, bool errFl )
  241. {
  242. cmAmPanel_t* pp = p->list;
  243. for(; pp!=NULL; pp=pp->link)
  244. if( pp->panelId == panelId )
  245. return pp;
  246. if( errFl )
  247. cmErrMsg(&p->err,kPanelNotFoundAmRC,"The panel %i was not found.",panelId);
  248. return NULL;
  249. }
  250. unsigned _cmUiAmCtlTypeId( cmAm_t* p, cmAmPanel_t* pp, cmUiCId_t cId, unsigned usrId,
  251. unsigned sliderId, unsigned toneId, unsigned passId, unsigned muteId )
  252. {
  253. switch( cId )
  254. {
  255. case kSliderUiCId:
  256. assert( pp->a[sliderId] <= usrId && usrId < pp->a[sliderId]+pp->oChCnt);
  257. return sliderId;
  258. break;
  259. case kCheckUiCId:
  260. if( pp->a[toneId] <= usrId && usrId < pp->a[toneId]+pp->oChCnt )
  261. return toneId;
  262. if( pp->a[passId] <= usrId && usrId < pp->a[passId]+pp->oChCnt )
  263. return passId;
  264. if( pp->a[muteId] <= usrId && usrId < pp->a[muteId]+pp->oChCnt )
  265. return muteId;
  266. break;
  267. default:
  268. break;
  269. }
  270. return cmInvalidId;
  271. }
  272. cmUiRC_t cmUiRtSysMstrOnUiEvent( cmUiRtMstrH_t h, const cmUiDriverArg_t* a )
  273. {
  274. cmUiRC_t rc = kOkUiRC;
  275. cmAm_t* p = _cmUiAmHandleToPtr(h);
  276. cmAmPanel_t* pp;
  277. cmRtSysMstr_t r;
  278. unsigned typeId;
  279. bool tabSelFl = a->dId==kSetValDId && a->cId == kPanelUiCId;
  280. if((pp = _cmUiAmFindPanel( p, a->panelId, !tabSelFl )) == NULL)
  281. {
  282. if( tabSelFl )
  283. return kOkUiRC;
  284. return cmErrLastRC(&p->err);
  285. }
  286. // if the panel tab was selected/deslected ival will be equal to 1/0
  287. if( a->usrId == pp->panelId )
  288. {
  289. cmRtSysStatusNotifyEnable(p->asH, pp->rtSubIdx, a->ival );
  290. return rc;
  291. }
  292. // based on the usrId determine which control generated the event
  293. if( a->usrId >= pp->baseOutId )
  294. typeId = _cmUiAmCtlTypeId(p,pp,a->cId,a->usrId,kOutSliderAmId,kOutToneAmId,kOutPassAmId,kOutMuteAmId);
  295. else
  296. typeId = _cmUiAmCtlTypeId(p,pp,a->cId,a->usrId,kInSliderAmId,kInToneAmId,kInPassAmId,kInMuteAmId);
  297. // this control is not a slider or check btn so ignore it
  298. if( typeId == cmInvalidId )
  299. return rc;
  300. unsigned asInFl = 0;
  301. unsigned asCtlId = cmInvalidId;
  302. unsigned asCh = a->usrId - pp->a[typeId];
  303. double asValue = 0;
  304. switch( typeId )
  305. {
  306. case kInSliderAmId:
  307. asInFl = 1;
  308. asCtlId = kSliderUiRtId;
  309. asValue = a->fval;
  310. break;
  311. case kInToneAmId:
  312. asInFl = 1;
  313. asCtlId = kToneUiRtId;
  314. asValue = a->ival;
  315. break;
  316. case kInPassAmId:
  317. asInFl = 1;
  318. asCtlId = kPassUiRtId;
  319. asValue = a->ival;
  320. break;
  321. case kInMuteAmId:
  322. asInFl = 1;;
  323. asCtlId = kMuteUiRtId;
  324. asValue = a->ival;
  325. break;
  326. case kOutSliderAmId:
  327. asCtlId = kSliderUiRtId;
  328. asValue = a->fval;
  329. break;
  330. case kOutToneAmId:
  331. asCtlId = kToneUiRtId;
  332. asValue = a->ival;
  333. break;
  334. case kOutPassAmId:
  335. asCtlId = kPassUiRtId;
  336. asValue = a->ival;
  337. break;
  338. case kOutMuteAmId:
  339. asCtlId = kMuteUiRtId;
  340. asValue = a->ival;
  341. break;
  342. }
  343. unsigned asDevIdx = asInFl ? pp->iDevIdx : pp->oDevIdx;
  344. r.hdr.rtSubIdx = pp->rtSubIdx;
  345. r.hdr.selId = kUiMstrSelRtId;
  346. r.devIdx = asDevIdx;
  347. r.chIdx = asCh;
  348. r.inFl = asInFl;
  349. r.ctlId = asCtlId;
  350. r.value = asValue;
  351. if( cmRtSysDeliverMsg(p->asH, &r, sizeof(r), cmInvalidId ) != kOkRtRC )
  352. rc = cmErrMsg(&p->err,kSubSysFailUiRC,"Audio System master control UI message delivery to the audio system failed.");
  353. return rc;
  354. }
  355. int _cmUiAmLinToDb( double v )
  356. {
  357. if( v <= 0 )
  358. return 0;
  359. v = round(20.0*log10(v)+100.0);
  360. return cmMin(kMtrMax,cmMax(kMtrMin,v));
  361. }
  362. cmUiRC_t cmUiRtSysMstrOnStatusEvent( cmUiRtMstrH_t h, const cmRtSysStatus_t* m, const double* iMeterArray, const double* oMeterArray )
  363. {
  364. cmAm_t* p = _cmUiAmHandleToPtr(h);
  365. cmAmPanel_t* pp = p->list;
  366. for(; pp!=NULL; pp=pp->link)
  367. if(pp->rtSubIdx == m->hdr.rtSubIdx )
  368. break;
  369. if( pp == NULL )
  370. return cmErrMsg(&p->err,kPanelNotFoundUiRC,"The panel associated with Audio system index %i could not be found.",m->hdr.rtSubIdx);
  371. cmUiSetInt(p->uiH, p->appId, pp->updateId, m->updateCnt != pp->updateCnt );
  372. cmUiSetInt(p->uiH, p->appId, pp->wakeupId, m->wakeupCnt != pp->wakeupCnt );
  373. cmUiSetInt(p->uiH, p->appId, pp->msgCbId, m->msgCbCnt != pp->msgCbCnt );
  374. cmUiSetInt(p->uiH, p->appId, pp->audioCbId, m->audioCbCnt != pp->audioCbCnt );
  375. pp->updateCnt = m->updateCnt;
  376. pp->wakeupCnt = m->wakeupCnt;
  377. pp->msgCbCnt = m->msgCbCnt;
  378. pp->audioCbCnt= m->audioCbCnt;
  379. unsigned i;
  380. for(i=0; i<m->iMeterCnt; ++i)
  381. cmUiSetInt(p->uiH, p->appId, pp->a[kInMeterAmId]+i, _cmUiAmLinToDb(iMeterArray[i]) );
  382. for(i=0; i<m->oMeterCnt; ++i)
  383. cmUiSetInt(p->uiH, p->appId, pp->a[kOutMeterAmId]+i, _cmUiAmLinToDb(oMeterArray[i]) );
  384. return kOkUiRC;
  385. }
  386. void cmUiRtSysMstrClearStatus( cmUiRtMstrH_t h )
  387. {
  388. cmAm_t* p = _cmUiAmHandleToPtr(h);
  389. cmAmPanel_t* pp = p->list;
  390. for(; pp!=NULL; pp=pp->link)
  391. {
  392. cmUiSetInt(p->uiH, p->appId, pp->updateId, 0 );
  393. cmUiSetInt(p->uiH, p->appId, pp->wakeupId, 0 );
  394. cmUiSetInt(p->uiH, p->appId, pp->msgCbId, 0 );
  395. cmUiSetInt(p->uiH, p->appId, pp->audioCbId, 0 );
  396. }
  397. }