libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmUiRtSysMstr.c 13KB

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