Programmable real-time audio signal processing application
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.

kcApp.cpp 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. #include <FL/Fl.H>
  2. #include <FL/Fl_Double_Window.H>
  3. #include <FL/Fl_Menu_Item.H>
  4. #include <Fl/Fl_Menu_Bar.H>
  5. #include <Fl/Fl_Button.H>
  6. #include <Fl/Fl_Check_Button.H>
  7. #include <Fl/Fl_Text_Buffer.H>
  8. #include <Fl/Fl_Text_Display.H>
  9. #include <Fl/Fl_Box.H>
  10. #include <Fl/Fl_Tabs.H>
  11. #include <Fl/Fl_File_Chooser.H>
  12. #include <Fl/Fl_Value_Slider.H>
  13. #include <Fl/Fl_Menu_Button.H>
  14. #include <Fl/Fl_Select_Browser.H>
  15. #include <Fl/Fl_Value_Input.H>
  16. #include <Fl/Fl_Progress.H>
  17. #include "Fl_Vert_Progress.h"
  18. #include "Fl_File_Btn.h"
  19. #include "Fl_Splitter.h"
  20. #include "cmGlobal.h"
  21. #include "cmFloatTypes.h"
  22. #include "cmRpt.h"
  23. #include "cmErr.h"
  24. #include "cmCtx.h"
  25. #include "cmMem.h"
  26. #include "cmMallocDebug.h"
  27. #include "cmLinkedHeap.h"
  28. #include "cmText.h"
  29. #include "cmSymTbl.h"
  30. #include "cmJson.h"
  31. #include "cmFileSys.h"
  32. #include "cmPrefs.h"
  33. #include "cmAudioFile.h"
  34. #include "cmThread.h"
  35. #include "cmSymTbl.h"
  36. #include "cmProcTest.h"
  37. #include "cmDspValue.h"
  38. #include "cmMsgProtocol.h"
  39. #include "cmAudDspIF.h"
  40. #include "cmAudDspLocal.h"
  41. #include "cmAudioFile.h"
  42. #include "cmAudioFileMgr.h"
  43. #include "cmTime.h"
  44. #include "cmMidi.h"
  45. #include "cmMidiFile.h"
  46. #include "cmTimeLine.h"
  47. #include "cmScore.h"
  48. #include "cmdIf.h"
  49. #include "tlCtl.h"
  50. #include "kcApp.h"
  51. #include "cmGr.h"
  52. #include "cmGrDevCtx.h"
  53. #include "cmGrPlot.h"
  54. #include "cmGrPage.h"
  55. #include "cmGrFltk.h"
  56. #define TIMER_PERIOD (1.0/20.0) // 50ms
  57. kcApp::kcApp(cmCtx_t* ctx, cmPrH_t prH, cmTsMp1cH_t printqH, int w, int h, const cmChar_t* title, cmAiH_t aiH, int argc, char* argv[] )
  58. : Fl_Double_Window(w, h,title),
  59. _ctx(ctx),_prH(prH),_aiH(aiH),
  60. _menu(NULL),_con(NULL),_tabs(NULL),_mstr_grp(NULL),
  61. _as_btn(NULL),_ai_btn(NULL),_ao_btn(NULL),_pgm_btn(NULL),
  62. _ss_btn(NULL),_sr_btn(NULL),_ena_chk(NULL),
  63. _pageList(NULL),_ctlList(NULL),
  64. _stopTimerFl(false),_newPageFl(true),
  65. _incrColW(0),_colW(0),_horzBordFl(false),_horzBord(0),
  66. _ssCnt(0),_ssArray(0),_ssPhase(0),_ssUpdateFl(false),
  67. _printqH(printqH),_printFl(0),
  68. _tlCtl(NULL)
  69. {
  70. // install a callback to cleanup when the app window closes
  71. // (the btn callbacks and _getApp() rely on a pointer to 'this' being found in kcApp.user_data())
  72. callback(_s_callback,this);
  73. // the main window is divided between the menu bar on top
  74. // and a horizontal splitter on the bottom
  75. begin();
  76. _createMenu(w,kMenuH);
  77. end();
  78. // Create a tab view
  79. add(_tabs = new Fl_Tabs(this->x(),this->y()+kMenuH,this->w(),this->h()-kMenuH));
  80. _tabs->callback(_s_tab_cb,this);
  81. _tabs->end();
  82. int tx=x(),ty=y(),th=this->h(),tw=this->w();
  83. _tabs->client_area(tx,ty,tw,th);
  84. // Create the 'Setup' tab group
  85. Fl_Group* setup_grp = new Fl_Group(tx,ty,tw,th,"Setup");
  86. _tabs->add(setup_grp);
  87. _createSetupDlg(setup_grp);
  88. // Create the console window
  89. Fl_Group* con_grp = new Fl_Group(tx,ty,tw,th,"Console");
  90. _tabs->add(con_grp);
  91. Fl_Text_Buffer* buf = new Fl_Text_Buffer();
  92. _con = new Fl_Text_Display(tx,ty,tw,th);
  93. _con->buffer(buf);
  94. con_grp->add(_con);
  95. // Create the master group
  96. _tabs->add(_mstr_grp = new Fl_Group(tx,ty,tw,th,"Master"));
  97. // Create an empty tab group and make it resizable
  98. // to prevent the other tab groups from being resizable.
  99. Fl_Group* wdgt = new Fl_Group(tx,ty+30,1,1);
  100. _tabs->add(wdgt);
  101. _tabs->resizable(wdgt); // make other tabs non-resizable
  102. // make the splitter the resizable group element (thereby making the menu non-resizable).
  103. // see:http://fltk.org/articles.php?L415+I0+T+M1000+P1
  104. resizable(_tabs);
  105. show(argc, argv);
  106. // direct all output to the console window
  107. cmRptSetup(&_ctx->rpt,_s_print,_s_print, this);
  108. cmTsMp1cSetCbFunc(_printqH, _s_print_queue_cb, this );
  109. // install a timer to check for messages from the engine
  110. Fl::add_timeout(TIMER_PERIOD,_s_status_timeout_cb,this);
  111. }
  112. kcApp::~kcApp()
  113. {
  114. _clearCtlList(true);
  115. _clearSsArray();
  116. }
  117. void kcApp::resize(int x, int y, int w, int h)
  118. {
  119. Fl_Double_Window::resize(x, y, w, h);
  120. cmPrefsPathSetInt(_prH,"appWndW",w);
  121. cmPrefsPathSetInt(_prH,"appWndH",h);
  122. cmPrefsPathSetInt(_prH,"appWndX",x);
  123. cmPrefsPathSetInt(_prH,"appWndY",y);
  124. }
  125. void kcApp::tlCtlNewTimeLineFile( tlCtl* tlCtl, const cmChar_t* fn )
  126. {}
  127. void kcApp::tlCtlNewScoreFile( tlCtl* tlCtl, const cmChar_t* fn )
  128. {}
  129. void kcApp::_createSetupDlg( Fl_Group* grp )
  130. {
  131. int ctl_width = 200;
  132. int vBord = 5;
  133. int xx = grp->x() + 5;
  134. int yy = grp->y() + vBord;
  135. grp->begin();
  136. Fl_Button* btn = new Fl_Button(xx,yy,200,kMenuH,"Audio Device Report");
  137. btn->callback( _s_btn_cb, kAudDevRptBtnId );
  138. yy += btn->h() + vBord;
  139. _as_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Audio System Cfg");
  140. _as_btn->callback( _s_btn_cb, kAudioSysCfgBtnId);
  141. yy += _as_btn->h() + vBord;
  142. _ai_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Audio Input Devices");
  143. _ai_btn->callback( _s_btn_cb, kInAudDevBtnId);
  144. yy += _ai_btn->h() + vBord;
  145. _ao_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Audio Output Devices");
  146. _ao_btn->callback( _s_btn_cb, kOutAudDevBtnId);
  147. yy += _ao_btn->h() + vBord;
  148. _pgm_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Programs");
  149. _pgm_btn->callback( _s_btn_cb, kPgmBtnId);
  150. yy += _pgm_btn->h() + vBord;
  151. _ss_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Sub-System");
  152. _ss_btn->callback( _s_btn_cb, kSubSystemIdxBtnId);
  153. yy += _ss_btn->h() + vBord;
  154. _sr_btn = new Fl_Menu_Button(xx,yy,ctl_width,kMenuH,"Sample Rate");
  155. _sr_btn->callback( _s_btn_cb, kSrateBtnId);
  156. _sr_btn->add("44100",0,NULL,(void*)44100,0);
  157. _sr_btn->add("48000",0,NULL,(void*)48000,0);
  158. _sr_btn->add("96000",0,NULL,(void*)96000,0);
  159. yy += _sr_btn->h() + vBord;
  160. btn = new Fl_Button(xx,yy,ctl_width,kMenuH,"Test");
  161. btn->callback( _s_btn_cb, kTestBtnId );
  162. yy += btn->h() + vBord;
  163. btn = new Fl_Button(xx,yy,200,kMenuH,"Print program");
  164. btn->callback( _s_btn_cb, kPrintPgmBtnId );
  165. yy += btn->h() + vBord;
  166. _ena_chk = new Fl_Check_Button(xx,yy,ctl_width,kMenuH,"Enable Audio");
  167. _ena_chk->callback( _s_btn_cb, kEnableBtnId );
  168. yy += _ena_chk->h() + vBord;
  169. // place this ctrl in the lower left corner to prevent the other controls from resizing
  170. Fl_Box* bx = new Fl_Box(FL_NO_BOX,xx+ctl_width,yy,10,10,NULL);
  171. grp->resizable(bx);
  172. grp->end();
  173. }
  174. void kcApp::_createMenu(int w, int h)
  175. {
  176. Fl_Menu_Item items[] =
  177. {
  178. { "kc", 0, 0, 0, FL_SUBMENU },
  179. { "&About", FL_COMMAND + 'a', (Fl_Callback*)_s_menu_cb, (void*)kAboutMenuId },
  180. { "&Quit", FL_COMMAND + 'q', (Fl_Callback*)_s_menu_cb, (void*)kQuitMenuId },
  181. { 0 },
  182. { 0 }
  183. };
  184. _menu = new Fl_Menu_Bar(0,0,w,h);
  185. _menu->copy(items);
  186. this->add(_menu);
  187. }
  188. kcApp::page_t* kcApp::_createPage( const char* title )
  189. {
  190. int tx,ty,th,tw;
  191. _tabs->client_area(tx,ty,tw,th);
  192. page_t* page = new page_t;
  193. page->link = _pageList;
  194. _pageList = page;
  195. _newPageFl = true;
  196. // Create the 'Controls' tab group
  197. _tabs->begin();
  198. page->grp = new Fl_Group(tx,ty,tw,th,title);
  199. page->grp->begin();
  200. Fl_Box* bx = new Fl_Box(FL_NO_BOX,page->grp->x()+page->grp->w(),page->grp->y()+page->grp->h(),1,1,NULL);
  201. page->grp->resizable(bx);
  202. page->grp->end();
  203. _tabs->end();
  204. return page;
  205. }
  206. // return the count of engine ctls
  207. unsigned kcApp::_getCtlCount() const
  208. {
  209. unsigned cnt = 0;
  210. ctl_t* cp = _ctlList;
  211. for(; cp!=NULL; cp=cp->linkPtr)
  212. ++cnt;
  213. return cnt;
  214. }
  215. // delete all engine ctls - (except maybe the master ctrl's)
  216. void kcApp::_clearCtlList(bool mstrFl)
  217. {
  218. ctl_t* ncl = NULL;
  219. ctl_t* cp = _ctlList;
  220. while( cp != NULL )
  221. {
  222. ctl_t* np = cp->linkPtr;
  223. // delete the ctrl unless it is a master ctl and the mstrFl is set
  224. if( cp->mstrFl==false || mstrFl )
  225. {
  226. delete cp;
  227. }
  228. else
  229. {
  230. // append the master ctl to the new ctl list
  231. cp->linkPtr = ncl;
  232. ncl = cp;
  233. }
  234. cp = np;
  235. }
  236. // clear the page groups and pages
  237. page_t* pg = _pageList;
  238. while( pg != NULL )
  239. {
  240. page_t* np = pg->link;
  241. pg->grp->clear();
  242. delete pg->grp;
  243. delete pg;
  244. pg = np;
  245. }
  246. _pageList = NULL;
  247. //ctl_grp->clear();
  248. _ctlList = ncl; // make the new ctl list the current ctl list
  249. if( mstrFl )
  250. _mstr_grp->clear();
  251. _tlCtl = NULL;
  252. }
  253. void kcApp::_clearSsArray()
  254. {
  255. if( _ssArray != NULL )
  256. {
  257. delete[] _ssArray;
  258. _ssArray = NULL;
  259. }
  260. _ssCnt = 0;
  261. }
  262. // find a engine ui ctl given a dsp instance id
  263. kcApp::ctl_t* kcApp::_findCtl( unsigned instId, unsigned asSubIdx, unsigned mstrFl )
  264. {
  265. ctl_t* cp = _ctlList;
  266. for(; cp!=NULL; cp=cp->linkPtr)
  267. if( cp->instId == instId && cp->asSubIdx==asSubIdx && cp->mstrFl==mstrFl )
  268. return cp;
  269. //printf("not found:0x%x ssi:%i mstr:%i\n",instId,asSubIdx,mstrFl);
  270. return NULL;
  271. }
  272. // calc. the position and size of a new ctl being created
  273. // at the request of the engine
  274. void kcApp::_getNewCtlPosn( const cmDspUiHdr_t* m, int& x, int& y, int& w, int& h )
  275. {
  276. enum { kPageH=700, kCtlH=30, kCtlW=200, kHBord=2, kVBord=2 };
  277. // if this is control is being placed in the 'master' group
  278. if( m->uiId == cmInvalidId )
  279. {
  280. //unsigned devIdx = cmAudioSysUiInstIdToDevIndex(m->instId);
  281. unsigned chIdx = cmAudioSysUiInstIdToChIndex(m->instId);
  282. unsigned outFl = !cmAudioSysUiInstIdToInFlag(m->instId);
  283. unsigned ctlId = cmAudioSysUiInstIdToCtlId(m->instId);
  284. int xOffs = 20;
  285. int yOffs = 100;
  286. int chH = 205;
  287. int chW = 44;
  288. int sldrH = 120;
  289. int sldrW = 20;
  290. int chkH = 20;
  291. int chkW = 35;
  292. x = xOffs + (chIdx * chW);
  293. y = yOffs + (m->asSubIdx * 2 * chH) + (outFl * chH);
  294. switch( ctlId )
  295. {
  296. // slider
  297. case kSliderUiAsId: w=sldrW; h=sldrH; break;
  298. // meter
  299. case kMeterUiAsId: w=sldrW; h=sldrH; x+=sldrW; break;
  300. // mute
  301. case kMuteUiAsId: w=chkW; h=chkH; y+=sldrH + 2; break;
  302. // tone
  303. case kToneUiAsId: w=chkW; h=chkH; y+=sldrH + chkH + 4; break;
  304. // pass
  305. case kPassUiAsId: w=chkW; h=chkH; y+=sldrH + 2*chkH + 6; break;
  306. default:
  307. { assert(0); }
  308. }
  309. return;
  310. }
  311. // if this control is being placed in the 'controls' group
  312. ctl_t* cp = _ctlList;
  313. if( w == 0 )
  314. {
  315. if( _colW == 0 )
  316. _colW = kCtlW;
  317. w = _colW;
  318. }
  319. if( h == 0 )
  320. h = kCtlH;
  321. // skip over controls that are assigned to the 'master' group
  322. while(cp != NULL && cp->mstrFl )
  323. cp = cp->linkPtr;
  324. // if the page list is empty - create a default blank page
  325. if( _pageList == NULL )
  326. _createPage("Controls");
  327. Fl_Group* ctl_grp = _pageList->grp;
  328. // if the ctl list is empty
  329. if( cp == NULL || _newPageFl )
  330. {
  331. _newPageFl = false;
  332. x = kHBord + ctl_grp->x();
  333. y = kVBord + ctl_grp->y();
  334. _horzBord = y;
  335. }
  336. else
  337. {
  338. // if a new horizontal - upper border was requested
  339. if( _horzBordFl )
  340. {
  341. _horzBordFl = false;
  342. // locate the current control with the max bottom coordinate
  343. ctl_t* tp = cp;
  344. if( tp != NULL )
  345. {
  346. int maxY = tp->wdgtPtr->y() + tp->wdgtPtr->h();
  347. while(tp!=NULL)
  348. {
  349. if( tp->mstrFl == false && (tp->wdgtPtr->y() + tp->wdgtPtr->h()) > maxY )
  350. maxY = tp->wdgtPtr->y() + tp->wdgtPtr->h();
  351. tp = tp->linkPtr;
  352. }
  353. // set the new upper boundary to just below the bottom of
  354. // the lowest control
  355. _horzBord = maxY + kVBord*2;
  356. }
  357. x = kHBord + ctl_grp->x();
  358. y = _horzBord;
  359. }
  360. else
  361. {
  362. // use the last ctl inserted in the list
  363. // (the first ctl in the list)
  364. // to position the next ctl
  365. x = cp->wdgtPtr->x();
  366. y = cp->wdgtPtr->y() + cp->wdgtPtr->h() + kVBord;
  367. // if this control goes off the page or a new col was requested
  368. if( y+h > kPageH || _incrColW )
  369. {
  370. x += _colW + kHBord;
  371. _colW = _incrColW;
  372. _incrColW = 0;
  373. y = _horzBord;
  374. }
  375. }
  376. }
  377. }
  378. kcApp::ctl_t* kcApp::_createCtl( const cmDspUiHdr_t* m, unsigned typeId, int& x, int& y, int& w, int& h, bool posnFl )
  379. {
  380. ctl_t* cp = new ctl_t;
  381. cp->thisPtr = this;
  382. cp->asSubIdx = m->asSubIdx;
  383. cp->instId = m->instId;
  384. cp->typeId = typeId;
  385. cp->mstrFl = m->uiId == cmInvalidId;
  386. if( posnFl )
  387. _getNewCtlPosn(m,x,y,w,h);
  388. return cp;
  389. }
  390. void kcApp::_insertNewCtl( ctl_t* cp, const cmDspUiHdr_t* m, Fl_Widget* wdgt, unsigned* varIdArray, unsigned varIdCnt )
  391. {
  392. unsigned i;
  393. // _genNewCtlPosn() should have been called before this - where a new page list would have been created.
  394. assert( cp->mstrFl || (cp->mstrFl==false && _pageList != NULL) );
  395. Fl_Group* grp = cp->mstrFl ? _mstr_grp : _pageList->grp;
  396. cp->wdgtPtr = wdgt;
  397. cp->wdgtPtr->callback(_s_ctl_cb, cp );
  398. grp->add(cp->wdgtPtr);
  399. const unsigned* srcVarIdArray = cmDsvUIntCMtx(&m->value);
  400. assert( varIdArray != NULL && cmDsvEleCount(&m->value)==varIdCnt);
  401. for(i=0; i<varIdCnt; ++i)
  402. varIdArray[i] = srcVarIdArray[i];
  403. cp->linkPtr = _ctlList;
  404. _ctlList = cp;
  405. }
  406. // create a slider ctl at the request of the engine
  407. void kcApp::_createSlider( const cmDspUiHdr_t* m )
  408. {
  409. int x,y,w=0,h=0;
  410. ctl_t* cp = _createCtl(m, m->selId==kSliderDuiId? kSldrTypeId : kNumbTypeId, x,y,w,h );
  411. switch(m->selId)
  412. {
  413. case kSliderDuiId:
  414. cp->u.sldr.u.sldr = new Fl_Value_Slider(x,y,w,h);
  415. cp->u.sldr.val = static_cast<Fl_Valuator*>(cp->u.sldr.u.sldr);
  416. cp->u.sldr.u.sldr->type(cp->mstrFl ? FL_VERT_NICE_SLIDER : FL_HOR_NICE_SLIDER);
  417. break;
  418. case kNumberDuiId:
  419. cp->u.sldr.u.numb = new Fl_Value_Input(x,y,w/2,h);
  420. cp->u.sldr.u.numb->when(FL_WHEN_ENTER_KEY | FL_WHEN_RELEASE );
  421. cp->u.sldr.val = static_cast<Fl_Valuator*>(cp->u.sldr.u.numb);
  422. break;
  423. }
  424. _insertNewCtl(cp, m, cp->u.sldr.val, cp->u.sldr.varIdArray, kSldrVarCnt );
  425. }
  426. // handle slider value msg's arriving from the engine
  427. void kcApp::_setSldrValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  428. {
  429. unsigned i=0;
  430. Fl_Valuator* sp = cp->u.sldr.val;
  431. for(i=0; i<kSldrVarCnt; ++i)
  432. if( cp->u.sldr.varIdArray[i] == instVarId )
  433. {
  434. switch(i)
  435. {
  436. case kSldrMinArgIdx:
  437. sp->minimum(cmDsvGetDouble(vp));
  438. //sp->step((sp->maximum()-sp->minimum())/sp->w());
  439. break;
  440. case kSldrMaxArgIdx:
  441. sp->maximum(cmDsvGetDouble(vp));
  442. //sp->step((sp->maximum()-sp->minimum())/sp->w());
  443. break;
  444. case kSldrStpArgIdx:
  445. sp->step(cmDsvGetDouble(vp));
  446. break;
  447. case kSldrValArgIdx:
  448. sp->value(cmDsvGetDouble(vp));
  449. break;
  450. case kSldrLblArgIdx:
  451. {
  452. int ww=0,hh=0;
  453. if( cmDsvStrcz(vp) == NULL )
  454. {
  455. sp->label(NULL);
  456. sp->align(FL_ALIGN_INSIDE | FL_ALIGN_CENTER );
  457. }
  458. else
  459. {
  460. sp->copy_label( cmDsvStrcz(vp) );
  461. sp->align(FL_ALIGN_RIGHT);
  462. }
  463. if( cp->typeId == kSldrTypeId )
  464. {
  465. sp->measure_label(ww,hh);
  466. sp->resize( sp->x(), sp->y(), sp->w() - ww, sp->h());
  467. sp->redraw();
  468. }
  469. }
  470. break;
  471. default:
  472. { assert(0); }
  473. }
  474. break;
  475. }
  476. }
  477. void kcApp::_createText( const cmDspUiHdr_t* m )
  478. {
  479. int x,y,w=0,h=0;
  480. ctl_t* cp = _createCtl(m, kTextTypeId, x,y,w,h );
  481. cp->u.text.text = new Fl_Input(x,y,w,h);
  482. cp->u.text.text->when(FL_WHEN_RELEASE | FL_WHEN_ENTER_KEY | FL_WHEN_NOT_CHANGED);
  483. _insertNewCtl(cp, m, cp->u.text.text, cp->u.text.varIdArray, kTextVarCnt );
  484. }
  485. void kcApp::_setTextValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  486. {
  487. unsigned i=0;
  488. Fl_Input* tp = cp->u.text.text;
  489. for(i=0; i<kTextVarCnt; ++i)
  490. if( cp->u.text.varIdArray[i] == instVarId )
  491. {
  492. switch(i)
  493. {
  494. case kTextValArgIdx:
  495. tp->value( cmDsvStrcz(vp));
  496. break;
  497. case kTextLblArgIdx:
  498. {
  499. const char* lbl = cmDsvStrcz(vp);
  500. if( lbl == NULL )
  501. tp->label(NULL);
  502. else
  503. tp->copy_label( lbl );
  504. tp->align(FL_ALIGN_RIGHT);
  505. tp->resize( tp->x(), tp->y(), tp->w()/2, tp->h());
  506. tp->redraw();
  507. }
  508. break;
  509. default:
  510. { assert(0); }
  511. }
  512. }
  513. }
  514. void kcApp::_createFnameCtl( const cmDspUiHdr_t* m )
  515. {
  516. int x,y,w=0,h=0;
  517. ctl_t* cp = _createCtl(m, kFnamTypeId, x,y,w,h );
  518. cp->u.fnam.fnam = new Fl_File_Btn(x,y,w,h);
  519. _insertNewCtl(cp, m, cp->u.fnam.fnam, cp->u.fnam.varIdArray, kFnamVarCnt );
  520. }
  521. void kcApp::_setFnamValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  522. {
  523. unsigned i=0;
  524. Fl_File_Btn* bp = cp->u.fnam.fnam;
  525. for(i=0; i<kFnamVarCnt; ++i)
  526. if( cp->u.fnam.varIdArray[i] == instVarId )
  527. {
  528. switch(i)
  529. {
  530. case kFnamValArgIdx:
  531. assert( cmDsvIsType( vp, kStrzDsvFl ) );
  532. bp->filename( cmDsvStrcz(vp) );
  533. bp->redraw();
  534. break;
  535. case kFnamPatArgIdx:
  536. assert( cmDsvIsType( vp, kStrzDsvFl ) );
  537. bp->pattern_string( cmDsvStrcz(vp) );
  538. break;
  539. case kFnamDirArgIdx:
  540. bp->type( cmDsvBool(vp) ? Fl_File_Btn::kDir_Type_Id : Fl_File_Btn::kFile_Type_Id );
  541. break;
  542. default:
  543. { assert(0); }
  544. }
  545. }
  546. }
  547. void kcApp::_createMlistCtl( const cmDspUiHdr_t* m )
  548. {
  549. const unsigned* varIdArray = cmDsvUIntCMtx(&m->value);
  550. assert( varIdArray != NULL && cmDsvEleCount(&m->value)==kMlstVarCnt);
  551. int x,y,w=0;
  552. int h = varIdArray[kMlstHgtArgIdx];
  553. bool menuBtnFl = h==0;
  554. h = menuBtnFl ? kMenuH : kMenuH * h;
  555. ctl_t* cp = _createCtl(m, kMlstTypeId, x,y,w,h );
  556. Fl_Widget* wdgt;
  557. if(menuBtnFl)
  558. {
  559. cp->u.mlst.mlst = NULL;
  560. wdgt = cp->u.mlst.mbtn = new Fl_Menu_Button(x,y,w,h);
  561. }
  562. else
  563. {
  564. cp->u.mlst.mbtn = NULL;
  565. wdgt = cp->u.mlst.mlst = new Fl_Select_Browser(x,y,w,h);
  566. }
  567. _insertNewCtl(cp, m, wdgt, cp->u.mlst.varIdArray, kMlstVarCnt );
  568. }
  569. kcApp::kcKmRC_t kcApp::_loadMlist( ctl_t* cp, const cmJsonNode_t* np )
  570. {
  571. assert( cmJsonIsArray(np) );
  572. kcKmRC_t rc = kOkKmRC;
  573. unsigned n = cmJsonChildCount(np);
  574. unsigned ri = 0;
  575. // empty the ctl data list
  576. if( cp->u.mlst.mbtn == NULL )
  577. cp->u.mlst.mlst->clear();
  578. else
  579. cp->u.mlst.mbtn->clear();
  580. // for each element (row) in the JSON array
  581. for(ri=0; ri<n && rc==kOkKmRC; ++ri)
  582. {
  583. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,ri);
  584. unsigned m = cmJsonChildCount(cnp);
  585. unsigned bufCharCnt = 511;
  586. cmChar_t buf[ bufCharCnt + 1 ]; // buffer to hold one row of text
  587. cmChar_t* bbp = buf;
  588. cmChar_t* bep = bbp + bufCharCnt;
  589. unsigned j;
  590. buf[0] = 0;
  591. // for each element (column) of row i
  592. for(j=0; j<m; ++j)
  593. {
  594. const cmJsonNode_t* ep = cmJsonArrayElementC(cnp,j);
  595. cmJsRC_t jsRC;
  596. // convert the element to a string
  597. if( (jsRC = cmJsonLeafToString( ep, bbp, bep - bbp )) != kOkJsRC )
  598. {
  599. switch(jsRC)
  600. {
  601. case kBufTooSmallJsRC:
  602. rc = cmErrMsg(&_ctx->err,kMlistLoadFailKmRC,"The msg list line character buffer is too small at JSON array element index %i.",ri);
  603. break;
  604. case kInvalidNodeTypeJsRC:
  605. rc = cmErrMsg(&_ctx->err,kMlistLoadFailKmRC,"The msg list array element at index %i is not a JSON leaf node.",ri);
  606. break;
  607. default:
  608. rc = cmErrMsg(&_ctx->err,kMlistLoadFailKmRC,"JSON to msg list text conversion failed on array element %i.",ri);
  609. break;
  610. }
  611. break;
  612. }
  613. bbp = buf + strlen(buf);
  614. assert(bbp <= bep);
  615. // add the tab column marker
  616. if( j+1 < m )
  617. {
  618. if( bep - bbp < 1 )
  619. rc = cmErrMsg(&_ctx->err,kMlistLoadFailKmRC,"The msg list line buffer is too small.");
  620. else
  621. {
  622. *bbp++ = '\t';
  623. *bbp = 0;
  624. }
  625. }
  626. }
  627. // insert the text for row i and skip the title row
  628. // See the _setMListValue() and _ctl_cb() for the associated index incr/decr.
  629. // so that the engine list indexes and UI list indexes match
  630. if( rc == kOkKmRC && ri > 0)
  631. {
  632. if( cp->u.mlst.mbtn==NULL)
  633. cp->u.mlst.mlst->add(buf);
  634. else
  635. cp->u.mlst.mbtn->add(buf);
  636. }
  637. }
  638. if( rc == kOkKmRC )
  639. if( cp->u.mlst.mbtn != NULL )
  640. {
  641. if( cp->u.mlst.sel < cp->u.mlst.mbtn->size() )
  642. cp->u.mlst.mbtn->label(cp->u.mlst.mbtn->text(cp->u.mlst.sel));;
  643. }
  644. return rc;
  645. }
  646. void kcApp::_setMlistValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  647. {
  648. unsigned i=0;
  649. for(i=0; i<kMlstVarCnt; ++i)
  650. if( cp->u.mlst.varIdArray[i] == instVarId )
  651. {
  652. switch(i)
  653. {
  654. case kMlstHgtArgIdx:
  655. break;
  656. case kMlstSelArgIdx:
  657. {
  658. unsigned idx = cmDsvUInt(vp);
  659. // decr. to account for skipping title row
  660. // (see the complementary incrementn in _ctl_cb())
  661. if( idx > 0 && cp->u.mlst.mbtn!=NULL)
  662. --idx;
  663. if( cp->u.mlst.mbtn==NULL)
  664. cp->u.mlst.mlst->value(idx);
  665. else
  666. {
  667. cp->u.mlst.sel = idx;
  668. if( cp->u.mlst.sel < cp->u.mlst.mbtn->size() )
  669. {
  670. cp->u.mlst.mbtn->value(cp->u.mlst.sel);
  671. cp->u.mlst.mbtn->label( cp->u.mlst.mbtn->text(cp->u.mlst.sel) );
  672. }
  673. }
  674. cp->wdgtPtr->redraw();
  675. }
  676. break;
  677. case kMlstLstArgIdx:
  678. _loadMlist( cp, cmDsvJson(vp) );
  679. break;
  680. default:
  681. { assert(0); }
  682. }
  683. }
  684. }
  685. // create a meter ctl at the request of the engine
  686. void kcApp::_createMeter( const cmDspUiHdr_t* m )
  687. {
  688. int x,y,w=0,h=15;
  689. ctl_t* cp = _createCtl(m,kMetrTypeId, x,y,w,h);
  690. if( cp->mstrFl )
  691. cp->u.metr.prog = new Fl_Vert_Progress(x,y,w,h);
  692. else
  693. cp->u.metr.prog = new Fl_Progress(x,y,w,h);
  694. cp->u.metr.prog->color( fl_rgb_color((unsigned char)256),fl_rgb_color((unsigned char)128));
  695. _insertNewCtl(cp,m,cp->u.metr.prog,cp->u.metr.varIdArray,kMetrVarCnt);
  696. }
  697. // handle meter value msg's arriving from the engine
  698. void kcApp::_setMeterValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  699. {
  700. unsigned i = 0;
  701. Fl_Progress* sp = cp->u.metr.prog;
  702. bool redrawFl = false;
  703. for(i=0; i<kMetrVarCnt; ++i)
  704. if( cp->u.metr.varIdArray[i] == instVarId )
  705. {
  706. switch(i)
  707. {
  708. case kMetrMinArgIdx:
  709. sp->minimum(cmDsvGetDouble(vp));
  710. redrawFl = true;
  711. break;
  712. case kMetrMaxArgIdx:
  713. sp->maximum(cmDsvGetDouble(vp));
  714. redrawFl = true;
  715. break;
  716. case kMetrValArgIdx:
  717. {
  718. double v = cmDsvGetDouble(vp);
  719. if( sp->value() != v )
  720. {
  721. sp->value(v);
  722. redrawFl = true;
  723. }
  724. break;
  725. }
  726. case kMetrLblArgIdx:
  727. {
  728. int ww=0,hh=0;
  729. sp->copy_label( cmDsvStrcz(vp) );
  730. sp->align(FL_ALIGN_RIGHT);
  731. sp->measure_label(ww,hh);
  732. sp->resize( sp->x(), sp->y(), sp->w() - ww, sp->h());
  733. redrawFl = true;
  734. }
  735. break;
  736. default:
  737. { assert(0); }
  738. }
  739. break;
  740. }
  741. if( redrawFl )
  742. sp->redraw();
  743. }
  744. // create a button ctl at the request of the engine
  745. void kcApp::_createButton( const cmDspUiHdr_t* m )
  746. {
  747. int x,y,w=0,h=0;
  748. ctl_t* cp = _createCtl(m,kButnTypeId, x,y,w,h);
  749. cp->u.butn.butn = new Fl_Button(x,y,w,h);
  750. _insertNewCtl(cp,m,cp->u.butn.butn,cp->u.butn.varIdArray,kButnVarCnt);
  751. }
  752. // handle button value msg's arriving from the engine
  753. void kcApp::_setButtonValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  754. {
  755. unsigned i = 0;
  756. Fl_Button* sp = cp->u.butn.butn;
  757. bool redrawFl = false;
  758. for(i=0; i<kButnVarCnt; ++i)
  759. if( cp->u.butn.varIdArray[i] == instVarId )
  760. {
  761. switch(i)
  762. {
  763. case kButnValArgIdx:
  764. cp->u.butn.val = cmDsvGetDouble(vp);
  765. break;
  766. case kButnLblArgIdx:
  767. {
  768. sp->copy_label( cmDsvStrcz(vp) );
  769. redrawFl = true;
  770. }
  771. break;
  772. default:
  773. { assert(0); }
  774. }
  775. break;
  776. }
  777. if( redrawFl )
  778. sp->redraw();
  779. }
  780. // create a button ctl at the request of the engine
  781. void kcApp::_createCheck( const cmDspUiHdr_t* m )
  782. {
  783. int x,y,w=0,h=0;
  784. ctl_t* cp = _createCtl(m,kChckTypeId, x,y,w,h);
  785. cp->u.chck.chck = new Fl_Check_Button(x,y,w,h);
  786. _insertNewCtl(cp,m,cp->u.chck.chck,cp->u.chck.varIdArray,kChckVarCnt);
  787. }
  788. // handle check button value msg's arriving from the engine
  789. void kcApp::_setCheckValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  790. {
  791. unsigned i = 0;
  792. Fl_Button* sp = cp->u.chck.chck;
  793. bool redrawFl = false;
  794. for(i=0; i<kChckVarCnt; ++i)
  795. if( cp->u.chck.varIdArray[i] == instVarId )
  796. {
  797. switch(i)
  798. {
  799. case kChckValArgIdx:
  800. cp->u.chck.val = cmDsvGetDouble(vp);
  801. cp->u.chck.chck->value(cmDsvGetDouble(vp)>0);
  802. break;
  803. case kChckLblArgIdx:
  804. {
  805. int ww=0,hh=0;
  806. sp->copy_label( cmDsvStrcz(vp) );
  807. sp->align(FL_ALIGN_RIGHT);
  808. sp->measure_label(ww,hh);
  809. sp->resize( sp->x(), sp->y(), sp->w() - ww, sp->h());
  810. redrawFl = true;
  811. }
  812. break;
  813. default:
  814. { assert(0); }
  815. }
  816. break;
  817. }
  818. if( redrawFl )
  819. sp->redraw();
  820. }
  821. void kcApp::_createLabel( const cmDspUiHdr_t* m )
  822. {
  823. int x,y,w=0,h=0;
  824. ctl_t* cp = _createCtl(m,kLablTypeId, x,y,w,h);
  825. cp->u.labl.box = new Fl_Box(x,y,w,h);
  826. _insertNewCtl(cp,m,cp->u.labl.box,cp->u.labl.varIdArray,kLablVarCnt);
  827. }
  828. void kcApp::_setLabelValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  829. {
  830. unsigned i = 0;
  831. Fl_Box* sp = cp->u.labl.box;
  832. bool redrawFl = false;
  833. for(i=0; i<kLablVarCnt; ++i)
  834. if( cp->u.labl.varIdArray[i] == instVarId )
  835. {
  836. switch(i)
  837. {
  838. case kLablValArgIdx:
  839. sp->copy_label(cmDsvStrcz(vp));
  840. redrawFl = true;
  841. break;
  842. case kLablAlignArgIdx:
  843. switch( cmDsvUInt(vp) )
  844. {
  845. case kRightAlignDuiId:
  846. sp->align(FL_ALIGN_RIGHT | FL_ALIGN_INSIDE);
  847. break;
  848. case kLeftAlignDuiId:
  849. sp->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
  850. break;
  851. case kCenterAlignDuiId:
  852. sp->align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE);
  853. break;
  854. default:
  855. { assert(0); }
  856. }
  857. redrawFl = true;
  858. break;
  859. default:
  860. {assert(0);}
  861. }
  862. }
  863. if( redrawFl )
  864. sp->redraw();
  865. }
  866. void kcApp::_createTmln( const cmDspUiHdr_t* m )
  867. {
  868. int x,y,w=0,h=0;
  869. page_t* pg = _createPage("TimeLine");
  870. ctl_t* cp = _createCtl(m, kTmlnTypeId, x,y,w,h, true );
  871. w = pg->grp->w();
  872. h = pg->grp->h();
  873. // currently we only support one score control because
  874. // we have not yet implmenented a method of providing
  875. // timer callbacks to a list of UI controls
  876. if( _tlCtl != NULL )
  877. cp->u.tmln.tlctl = _tlCtl;
  878. else
  879. {
  880. _tlCtl = new tlCtl(_ctx,this,_menu,this);
  881. cp->u.tmln.tlctl = _tlCtl;
  882. }
  883. Fl_Widget* wdgt = cp->u.tmln.tlctl->initTimeLineCtlr(x,y,w,h);
  884. _insertNewCtl(cp,m,wdgt,cp->u.tmln.varIdArray,kTmlnVarCnt);
  885. }
  886. void kcApp::_setTmlnValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  887. {
  888. unsigned i=0;
  889. for(i=0; i<kTmlnVarCnt; ++i)
  890. if( cp->u.tmln.varIdArray[i] == instVarId )
  891. {
  892. switch(i)
  893. {
  894. case kTmlnFileArgIdx:
  895. {
  896. const char* fn;
  897. if((fn = cmDsvStrcz(vp)) != NULL )
  898. {
  899. cp->u.tmln.tlctl->openTlFile(fn);
  900. //cp->u.tmln.tlctl->redraw();
  901. }
  902. }
  903. break;
  904. case kTmlnPathArgIdx:
  905. {
  906. const char* path;
  907. if((path = cmDsvStrcz(vp)) != NULL )
  908. {
  909. cp->u.tmln.tlctl->setAudioFilePath(path);
  910. }
  911. }
  912. break;
  913. case kTmlnSelArgIdx:
  914. //tp->value( cmDsvStrcz(vp));
  915. break;
  916. case kTmlnCursArgIdx:
  917. cp->u.tmln.tlctl->setAudioFileCursor(cmDsvUInt(vp));
  918. break;
  919. default:
  920. { assert(0); }
  921. }
  922. }
  923. }
  924. void kcApp::_createScor( const cmDspUiHdr_t* m )
  925. {
  926. int x,y,w=0,h=0;
  927. page_t* pg = _createPage("Score");
  928. ctl_t* cp = _createCtl(m, kScorTypeId, x,y,w,h, true );
  929. w = pg->grp->w();
  930. h = pg->grp->h();
  931. // currently we only support one score control because
  932. // we have not yet implmenented a method of providing
  933. // timer callbacks to a list of UI controls
  934. if( _tlCtl != NULL )
  935. cp->u.scor.tlctl = _tlCtl;
  936. else
  937. {
  938. _tlCtl = new tlCtl(_ctx,this,_menu,this);
  939. cp->u.scor.tlctl = _tlCtl;
  940. cp->u.scor.smpIdx = cmInvalidIdx;
  941. }
  942. Fl_Widget* wdgt = cp->u.scor.tlctl->initScoreCtlr(x,y,w,h);
  943. _insertNewCtl(cp,m,wdgt,cp->u.scor.varIdArray,kScorVarCnt);
  944. }
  945. void kcApp::_setScorValue( ctl_t* cp, unsigned instVarId, const cmDspValue_t* vp )
  946. {
  947. unsigned i=0;
  948. //tlCtl* tp = cp->u.scor.tlctl;
  949. for(i=0; i<kScorVarCnt; ++i)
  950. if( cp->u.scor.varIdArray[i] == instVarId )
  951. {
  952. switch(i)
  953. {
  954. case kScorSelArgIdx:
  955. //tp->value( cmDsvStrcz(vp));
  956. break;
  957. case kScorFileArgIdx:
  958. {
  959. const char* fn;
  960. if((fn = cmDsvStrcz(vp)) != NULL )
  961. {
  962. cp->u.scor.tlctl->openScoreFile(fn);
  963. //cp->u.scor.tlctl->redraw();
  964. }
  965. }
  966. break;
  967. case kScorSmpIdxArgIdx:
  968. cp->u.scor.smpIdx = cmDsvUInt(vp);
  969. break;
  970. case kScorPitchArgIdx:
  971. cp->u.scor.pitch = cmDsvUInt(vp);
  972. break;
  973. case kScorVelArgIdx:
  974. cp->u.scor.vel = cmDsvUInt(vp);
  975. break;
  976. case kScorEvtIdxArgIdx:
  977. cp->u.scor.evtIdx = cmDsvUInt(vp);
  978. break;
  979. case kScorDynArgIdx:
  980. {
  981. assert(cp->u.scor.evtIdx!=cmInvalidIdx);
  982. _tlCtl->setScoreDynLevel(cp->u.scor.evtIdx,cmDsvUInt(vp));
  983. cp->u.scor.evtIdx = cmInvalidIdx;
  984. }
  985. break;
  986. case kScorLocIdxArgIdx:
  987. {
  988. assert( cp->u.scor.smpIdx != cmInvalidIdx );
  989. cp->u.scor.locIdx = cmDsvUInt(vp);
  990. _tlCtl->setScoreLocation(cp->u.scor.locIdx,cp->u.scor.smpIdx,cp->u.scor.pitch,cp->u.scor.vel);
  991. cp->u.scor.smpIdx = cmInvalidIdx;
  992. }
  993. break;
  994. case kScorValTypeArgIdx:
  995. cp->u.scor.varId = cmDsvUInt(vp);
  996. break;
  997. case kScorValueArgIdx:
  998. _tlCtl->setScoreVarValue(cp->u.scor.locIdx,cp->u.scor.varId, cmDsvDouble(vp));
  999. break;
  1000. default:
  1001. { assert(0); }
  1002. }
  1003. }
  1004. }
  1005. void kcApp::_newColumn( const cmDspUiHdr_t* m )
  1006. {
  1007. _incrColW = cmDsvGetUInt(&m->value);
  1008. if( _incrColW == 0 )
  1009. _incrColW = 200;
  1010. }
  1011. void kcApp::_insertAudioSysCfgLabel(unsigned long idx, const char* label)
  1012. {
  1013. if( idx == 0 )
  1014. _as_btn->clear();
  1015. _as_btn->add(label,0,NULL,(void*)idx,0);
  1016. }
  1017. void kcApp::_insertDeviceLabel( unsigned long devIdx, bool inputFl, const cmChar_t* label )
  1018. {
  1019. Fl_Menu_Button* bp = inputFl ? _ai_btn : _ao_btn;
  1020. // ??? if( idx == 0 )
  1021. // bp->clear();
  1022. bp->add(label,0,NULL,(void*)devIdx,0);
  1023. }
  1024. void kcApp::_insertProgramLabel( unsigned long idx, const cmChar_t* label )
  1025. {
  1026. if( idx == 0 )
  1027. _pgm_btn->clear();
  1028. _pgm_btn->add(label,0,NULL,(void*)idx,0);
  1029. }
  1030. void kcApp::_insertSubSysCnt( unsigned long subSysCnt )
  1031. {
  1032. long unsigned i;
  1033. int bufByteCnt = 15;
  1034. char buf[bufByteCnt+1];
  1035. for(i=0; i<subSysCnt+1; ++i)
  1036. {
  1037. const char* label;
  1038. unsigned long id;
  1039. if( i==0 )
  1040. {
  1041. label = "All";
  1042. id = cmInvalidIdx;
  1043. }
  1044. else
  1045. {
  1046. snprintf(buf,bufByteCnt,"%li",i);
  1047. label = buf;
  1048. id = i-1;
  1049. }
  1050. _ss_btn->add(label,0,NULL,(void*)id);
  1051. }
  1052. _ss_btn->value(0);
  1053. _ss_btn->copy_label( _ss_btn->mvalue()->label() );
  1054. }
  1055. unsigned kcApp::_getCurAudioSubSysIdx()
  1056. {
  1057. const Fl_Menu_Item* mip;
  1058. unsigned retVal = 0;
  1059. if( (mip = _ss_btn->mvalue()) != NULL)
  1060. retVal = mip->argument();
  1061. return retVal;
  1062. }
  1063. void kcApp::_setMenuButton( Fl_Menu_Button* b, unsigned value, const char* dfltLabel )
  1064. {
  1065. const Fl_Menu_Item* mip;
  1066. bool fl = value != cmInvalidIdx;
  1067. // if value is a valid menu index then make it the current menu selection
  1068. if( fl )
  1069. b->value(value);
  1070. // get the new current menu item and set its label
  1071. if( (mip = b->mvalue()) != NULL )
  1072. b->copy_label( fl ? mip->label() : dfltLabel );
  1073. }
  1074. void kcApp::_setDeviceMenuButton( unsigned asSubIdx, bool inputFl, unsigned devIdx )
  1075. {
  1076. // don't set the device name if the currenly selected sub-system is not the
  1077. // same as the one the device is assigned to
  1078. if( _getCurAudioSubSysIdx() != asSubIdx )
  1079. return;
  1080. Fl_Menu_Button* mbp = inputFl ? _ai_btn : _ao_btn;
  1081. const Fl_Menu_Item* map = mbp->menu();
  1082. unsigned n = mbp->size();
  1083. unsigned i;
  1084. char* di = 0; di += devIdx; // BEWARE: devious int to ptr trick
  1085. for(i=0; i<n; ++i)
  1086. if( map[i].user_data() == di )
  1087. break;
  1088. if( i < n )
  1089. {
  1090. _setMenuButton(mbp,i,"<not found>");
  1091. }
  1092. }
  1093. void kcApp::_setSampleRateBtn( unsigned value )
  1094. {
  1095. unsigned i;
  1096. unsigned n = _sr_btn->size();
  1097. for(i=0; i<n-1; ++i)
  1098. {
  1099. if( _sr_btn->menu()[i].argument() == (int)value )
  1100. {
  1101. _sr_btn->value(i);
  1102. _sr_btn->copy_label( _sr_btn->mvalue()->label() );
  1103. return;
  1104. }
  1105. }
  1106. _sr_btn->copy_label("Sample Rate?");
  1107. }
  1108. void kcApp::_updateMeters( unsigned asSubIdx, unsigned devIdx, unsigned inFl, const double* meterArray, unsigned meterCnt )
  1109. {
  1110. unsigned i;
  1111. for(i=0; i<meterCnt; ++i)
  1112. {
  1113. ctl_t* cp;
  1114. cmDspValue_t v;
  1115. unsigned instId = cmAudioSysFormUiInstId(devIdx,i,inFl,kMeterUiAsId);
  1116. if((cp = _findCtl(instId,asSubIdx,true)) != NULL )
  1117. {
  1118. cmDsvSetDouble(&v,meterArray[i]);
  1119. _setMeterValue(cp, kMetrValArgIdx, &v);
  1120. }
  1121. }
  1122. }
  1123. void kcApp::_printStatusCounts()
  1124. {
  1125. const ss_t* ssp = _ssArray;
  1126. printf("Upd:%i Wake:%i Msg:%i Audio:%i\n",ssp->cnt[kUpdateSsIdx],ssp->cnt[kWakeupSsIdx],ssp->cnt[kMsgSsIdx],ssp->cnt[kAudioCbSsIdx]);
  1127. }
  1128. void kcApp::_updateSsStatusIndicator( unsigned asSubIdx, unsigned indicatorIdx, unsigned cnt )
  1129. {
  1130. ss_t* ss = _ssArray + asSubIdx;
  1131. float val = ss->cnt[indicatorIdx] != cnt;
  1132. ss->cnt[indicatorIdx] = cnt;
  1133. if( ss->prog[indicatorIdx]->value() != val )
  1134. {
  1135. ss->prog[indicatorIdx]->value(val);
  1136. ss->prog[indicatorIdx]->redraw();
  1137. }
  1138. }
  1139. void kcApp::_handleStatusMsg(const cmAudioSysStatus_t* st, const double* iMeterArray, const double* oMeterArray )
  1140. {
  1141. //unsigned asSubIdx = ((const unsigned*)msgBuf)[0];
  1142. //const cmAudioSysStatus_t* st = (const cmAudioSysStatus_t*)(msgBuf + (2 * sizeof(unsigned)));
  1143. //const double* iMeterArray = (const double*)(st + 1);
  1144. //const double* oMeterArray = iMeterArray + st->iMeterCnt;
  1145. _updateMeters(st->asSubIdx, st->iDevIdx,1,iMeterArray,st->iMeterCnt);
  1146. _updateMeters(st->asSubIdx, st->oDevIdx,0,oMeterArray,st->oMeterCnt);
  1147. assert( st->asSubIdx < _ssCnt );
  1148. _updateSsStatusIndicator(st->asSubIdx, kUpdateSsIdx, st->updateCnt );
  1149. _updateSsStatusIndicator(st->asSubIdx, kWakeupSsIdx, st->wakeupCnt );
  1150. _updateSsStatusIndicator(st->asSubIdx, kMsgSsIdx, st->msgCbCnt );
  1151. _updateSsStatusIndicator(st->asSubIdx, kAudioCbSsIdx,st->audioCbCnt );
  1152. }
  1153. void kcApp::_clearStatusIndicators()
  1154. {
  1155. for(unsigned i=0; i<_ssCnt; ++i)
  1156. {
  1157. _updateSsStatusIndicator(i, kUpdateSsIdx, _ssArray[i].cnt[kUpdateSsIdx] );
  1158. _updateSsStatusIndicator(i, kWakeupSsIdx, _ssArray[i].cnt[kWakeupSsIdx] );
  1159. _updateSsStatusIndicator(i, kMsgSsIdx, _ssArray[i].cnt[kMsgSsIdx] );
  1160. _updateSsStatusIndicator(i, kAudioCbSsIdx,_ssArray[i].cnt[kAudioCbSsIdx] );
  1161. }
  1162. }
  1163. // Create a master control UI msg and send it to _handleUiMsg(). This function is intended to
  1164. // mimic the reception of a cmDspUiHdr_t msg from the audio system. It is used to create
  1165. // and send values to the controls on the master page.
  1166. void kcApp::_sendMasterUiMsg( unsigned asSubIdx, unsigned selId, unsigned instId, unsigned instVarId, const cmDspValue_t* vp )
  1167. {
  1168. // Determine the size of the message buffer
  1169. unsigned valByteCnt = cmDsvSerialDataByteCount(vp);
  1170. unsigned bufByteCnt = sizeof(cmDspUiHdr_t) + valByteCnt;
  1171. char buf[ bufByteCnt ];
  1172. cmDspUiHdr_t* h = (cmDspUiHdr_t*)buf;
  1173. h->asSubIdx = asSubIdx;
  1174. h->uiId = cmInvalidId; // the uiId field of master controls is always set to cmInvalidId
  1175. h->selId = selId;
  1176. h->flags = 0;
  1177. h->instId = instId;
  1178. h->instVarId = instVarId;
  1179. // Serialize 'v' into the buffer beginning at the address of h->value.
  1180. // (this function relies on the 'hdr.value' field being the last field in 'h')
  1181. cmDsvSerialize( vp, &h->value, sizeof(cmDspValue_t) + valByteCnt);
  1182. _handleUiMsg(h);
  1183. }
  1184. // Send a value to a control on the master page.
  1185. void kcApp::_sendMasterUiValue( unsigned asSubIdx, unsigned instId, const double* v, unsigned vn, const cmChar_t* text )
  1186. {
  1187. cmDspValue_t val;
  1188. unsigned i;
  1189. for(i=0; i<vn; ++i)
  1190. {
  1191. cmDsvSetDouble(&val,v[i]);
  1192. _sendMasterUiMsg(asSubIdx, kValueDuiId, instId, i, &val );
  1193. }
  1194. if( text != NULL )
  1195. {
  1196. cmDsvSetStrcz(&val,text);
  1197. _sendMasterUiMsg(asSubIdx, kValueDuiId, instId, i, &val );
  1198. }
  1199. }
  1200. // Create a control on the master page.
  1201. void kcApp::_createMasterCtl( unsigned asSubIdx, unsigned selId, unsigned instId, unsigned varCnt, const double* dv, unsigned dn, const cmChar_t* label)
  1202. {
  1203. // Create the control variable id array.
  1204. // This array gives the id's used to identify each control variable.
  1205. unsigned varIdArray[ varCnt ];
  1206. unsigned i;
  1207. cmDspValue_t v;
  1208. // Set the var id's for master controls to be the same as the var indexes.
  1209. for(i=0; i<varCnt; ++i)
  1210. varIdArray[i] = i;
  1211. // Encode varIdArray[] as a cmDsvValue.
  1212. cmDsvSetUIntMtx(&v,varIdArray,varCnt,1);
  1213. // create the control
  1214. _sendMasterUiMsg(asSubIdx, selId, instId, cmInvalidId, &v );
  1215. // set the controls initial configuration
  1216. _sendMasterUiValue(asSubIdx, instId, dv, dn, label );
  1217. }
  1218. void kcApp::_handleSsInitMsg( const cmAudioSysSsInitMsg_t* m, const cmChar_t* inDevLabel, const cmChar_t* outDevLabel )
  1219. {
  1220. unsigned i,j;
  1221. if( _ssCnt == 0 )
  1222. {
  1223. _clearSsArray();
  1224. _ssCnt = m->asSubCnt;
  1225. _ssArray = new ss_t[ _ssCnt ];
  1226. }
  1227. assert( m->asSubIdx < _ssCnt );
  1228. ss_t* ss = _ssArray + m->asSubIdx;
  1229. int x = 20;
  1230. int y = 30 + kMenuH;
  1231. int w = 80;
  1232. int h = 20;
  1233. char lblArray[][10] = { "Update","Wakeup","Mesg","Audio" };
  1234. // create the sub-system status indicators
  1235. for(i=0; i<kProgSsCnt; ++i)
  1236. {
  1237. Fl_Progress* prog = new Fl_Progress(x,y,w,h,NULL);
  1238. prog->color( FL_RED, FL_GREEN );
  1239. prog->minimum(0);
  1240. prog->maximum(1);
  1241. prog->copy_label(lblArray[i]);
  1242. _mstr_grp->add(prog);
  1243. ss->cnt[i] = 0;
  1244. ss->prog[i] = prog;
  1245. x += ss->prog[i]->w() + 4;
  1246. }
  1247. for(i=0; i<2; ++i)
  1248. {
  1249. unsigned inFl = i==0;
  1250. unsigned chCnt = inFl ? m->inChCnt : m->outChCnt;
  1251. unsigned devIdx = inFl ? m->inDevIdx : m->outDevIdx;
  1252. double zero = 0;
  1253. for(j=0; j<chCnt; ++j)
  1254. {
  1255. // there are limits on the ranges of the dev and ch due to the need to pack dev/ch/infl/ctl into a 32 bit int.
  1256. assert( devIdx < 0xffff && j < 0x0fff);
  1257. unsigned instId = cmAudioSysFormUiInstId(devIdx,j,inFl,0);
  1258. //printf("ssi:%i instId:0x%x\n",m->asSubIdx,instId+kMeterUiAsId);
  1259. // create the volume slider
  1260. double sv[] = { 3, 0, 0.01, 1 };
  1261. unsigned sn = sizeof(sv)/sizeof(sv[0]);
  1262. _createMasterCtl( m->asSubIdx, kSliderDuiId, instId + kSliderUiAsId, kSldrVarCnt, sv, sn, NULL);
  1263. // create the meter
  1264. double mv[] = { 0, 1.0, 0 };
  1265. unsigned mn = sizeof(mv)/sizeof(mv[0]);
  1266. _createMasterCtl( m->asSubIdx, kMeterDuiId, instId + kMeterUiAsId, kMetrVarCnt, mv, mn, NULL);
  1267. // create the mute button
  1268. _createMasterCtl( m->asSubIdx, kCheckDuiId, instId + kMuteUiAsId, kChckVarCnt, &zero, 1, "M");
  1269. // create the tone button
  1270. _createMasterCtl( m->asSubIdx, kCheckDuiId, instId + kToneUiAsId, kChckVarCnt, &zero, 1, "T");
  1271. // create the pass button
  1272. _createMasterCtl( m->asSubIdx, kCheckDuiId, instId + kPassUiAsId, kChckVarCnt, &zero, 1, "P");
  1273. }
  1274. }
  1275. }
  1276. // handle kValueDuiId messages coming from the engine
  1277. void kcApp::_onRecvValue( const cmDspUiHdr_t* m )
  1278. {
  1279. ctl_t* cp;
  1280. if((cp = _findCtl(m->instId,m->asSubIdx,m->uiId==cmInvalidId)) == NULL )
  1281. return;
  1282. switch( cp->typeId )
  1283. {
  1284. case kNumbTypeId:
  1285. case kSldrTypeId:
  1286. _setSldrValue(cp, m->instVarId, &m->value );
  1287. break;
  1288. case kTextTypeId:
  1289. _setTextValue(cp, m->instVarId, &m->value );
  1290. break;
  1291. case kButnTypeId:
  1292. _setButtonValue(cp, m->instVarId, &m->value);
  1293. break;
  1294. case kChckTypeId:
  1295. _setCheckValue(cp, m->instVarId, &m->value);
  1296. break;
  1297. case kLablTypeId:
  1298. _setLabelValue(cp, m->instVarId, &m->value);
  1299. break;
  1300. case kFnamTypeId:
  1301. _setFnamValue(cp, m->instVarId, &m->value);
  1302. break;
  1303. case kMlstTypeId:
  1304. _setMlistValue(cp, m->instVarId, &m->value);
  1305. break;
  1306. case kMetrTypeId:
  1307. _setMeterValue(cp, m->instVarId, &m->value);
  1308. break;
  1309. case kTmlnTypeId:
  1310. _setTmlnValue(cp, m->instVarId, &m->value);
  1311. break;
  1312. case kScorTypeId:
  1313. _setScorValue(cp, m->instVarId, &m->value);
  1314. break;
  1315. default:
  1316. assert(0);
  1317. }
  1318. }
  1319. // This is the main UI<-Engine msg handler/dispatch function
  1320. void kcApp::_handleUiMsg( const cmDspUiHdr_t* m )
  1321. {
  1322. switch( m->selId )
  1323. {
  1324. case kPrintDuiId:
  1325. cmDsvPrint(&m->value,NULL,&_ctx->rpt);
  1326. break;
  1327. case kNumberDuiId:
  1328. case kSliderDuiId:
  1329. _createSlider(m);
  1330. break;
  1331. case kTextDuiId:
  1332. _createText(m);
  1333. break;
  1334. case kButtonDuiId:
  1335. _createButton(m);
  1336. break;
  1337. case kLabelDuiId:
  1338. _createLabel(m);
  1339. break;
  1340. case kCheckDuiId:
  1341. _createCheck(m);
  1342. break;
  1343. case kFnameDuiId:
  1344. _createFnameCtl(m);
  1345. break;
  1346. case kMsgListDuiId:
  1347. _createMlistCtl(m);
  1348. break;
  1349. case kMeterDuiId:
  1350. _createMeter(m);
  1351. break;
  1352. case kTimeLineDuiId:
  1353. _createTmln(m);
  1354. break;
  1355. case kScoreDuiId:
  1356. _createScor(m);
  1357. break;
  1358. case kValueDuiId:
  1359. _onRecvValue(m);
  1360. break;
  1361. case kColumnDuiId:
  1362. _newColumn(m);
  1363. break;
  1364. case kHBorderDuiId:
  1365. _horzBordFl = true;
  1366. break;
  1367. case kPageDuiId:
  1368. _createPage(cmDsvStrcz(&m->value));
  1369. break;
  1370. case kAudioSysCfgDuiId:
  1371. _insertAudioSysCfgLabel(m->instId,cmDsvStrcz(&m->value));
  1372. break;
  1373. case kDeviceDuiId:
  1374. _insertDeviceLabel(m->instId,m->flags,cmDsvStrcz(&m->value));
  1375. break;
  1376. case kProgramDuiId:
  1377. _insertProgramLabel(m->instId,cmDsvStrcz(&m->value));
  1378. break;
  1379. // the below codes are used to notify the application
  1380. // of changes in state of the audio DSP system
  1381. case kSubSysCntDuiId:
  1382. _insertSubSysCnt(cmDsvUInt(&m->value));
  1383. break;
  1384. case kSetAudioCfgDuiId:
  1385. _setMenuButton(_as_btn,cmDsvUInt(&m->value),"Audio System Cfg");
  1386. break;
  1387. case kSetAudioDevDuiId:
  1388. _setDeviceMenuButton( m->asSubIdx, m->flags, cmDsvUInt(&m->value) );
  1389. break;
  1390. case kSetSampleRateDuiId:
  1391. _setSampleRateBtn(cmDsvUInt(&m->value));
  1392. break;
  1393. case kSetPgmDuiId:
  1394. _setMenuButton(_pgm_btn,cmDsvUInt(&m->value),"Program?");
  1395. _clearCtlList(false);
  1396. break;
  1397. case kEnableDuiId:
  1398. _ena_chk->value(m->flags);
  1399. break;
  1400. }
  1401. }
  1402. // Check for and forward any messages sent to the UI
  1403. // that are waiting in the audio DSP msg queue.
  1404. void kcApp::_getEngMsg()
  1405. {
  1406. if( cmAdIfIsValid(_aiH) )
  1407. {
  1408. unsigned i;
  1409. for(i=0; i<10; ++i)
  1410. {
  1411. cmAiRC_t aiRC;
  1412. // cmAdIfDispatchMsgToHost() results in calls to
  1413. // the _s_handleXXX() message handlers
  1414. if((aiRC = cmAdIfDispatchMsgToHost(_aiH)) != kOkAiRC)
  1415. {
  1416. if( aiRC == kNoMsgAiRC )
  1417. break;
  1418. cmErrMsg(&_ctx->err,kEngFailKmRC,"Audio DSP dispatch message request failed.");
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. }
  1424. kcApp* kcApp::_getApp( Fl_Widget* w )
  1425. {
  1426. // walk up the widget tree until the top widget is found
  1427. Fl_Group* gp = w->parent();
  1428. while( gp->parent() != NULL )
  1429. gp=gp->parent();
  1430. // the user data for the top widget is a pointer kcApp()
  1431. return (kcApp*)gp->user_data();
  1432. }
  1433. void kcApp::_onCloseApp()
  1434. {
  1435. if( _tlCtl != NULL )
  1436. {
  1437. delete _tlCtl;
  1438. _tlCtl = NULL;
  1439. }
  1440. _stopTimerFl = true;
  1441. // When all windows are windows are closed then the app.
  1442. // will close - so hiding the application window
  1443. // causes the program to close.
  1444. //
  1445. // Note that simply returning from this callback will
  1446. // prevent the application from closing. Because the existence
  1447. // of the callback alone is enough to disable default
  1448. // event handling.
  1449. hide();
  1450. }
  1451. void kcApp::_testStub()
  1452. {
  1453. /*
  1454. cmAudioFileTest(
  1455. // "/Users/kevin/media/audio/20100819-Kreisberg/fragments/Bass End_21a.wav",
  1456. "/Users/kevin/media/audio/McGill-1/1 Audio Track.aiff",
  1457. "/Users/kevin/src/octave/cm_audio_file_test.m",_ctx->err.rpt );
  1458. */
  1459. //cmSymTblTest(&_ctx);
  1460. //_kcTranslateFile("/Users/kevin/src/kc/src/data/Sec1_14.txt","/Users/kevin/src/kc/src/data/Sec1_14_out.txt",&_ctx);
  1461. //_kcTranslateFile("/Users/kevin/src/kc/src/data/Mix_17-20.txt","/Users/kevin/src/kc/src/data/Mix_17-20_out.txt",&_ctx);
  1462. /*
  1463. ctl_t* cp = _ctlList;
  1464. for(; cp!=NULL; cp=cp->linkPtr)
  1465. printf("%i %i %i\n", cp->instId,cp->asSubIdx,cp->mstrFl );
  1466. */
  1467. //_printStatusCounts();
  1468. //cmProcTestNoInit(&_ctx);
  1469. _tlCtl->testStub();
  1470. }
  1471. void kcApp::_printPgm()
  1472. {
  1473. const char* pathStr = cmFsUserDir(); // make the default dir the user's home dir
  1474. const char patStr[] = ""; //"Text Files (*.txt)\tAudio Files (*.{wav,aif,aiff})"; // All Files (*.*) is append to this automatically
  1475. const char titleStr[] = "JSON output file name";
  1476. Fl_File_Chooser fc = Fl_File_Chooser(pathStr,patStr,Fl_File_Chooser::CREATE,titleStr);
  1477. fc.preview(0); // default the previous option to 'off'.
  1478. fc.show(); // show the chooser
  1479. // make the chooser modal
  1480. while( fc.shown() )
  1481. Fl::wait();
  1482. if( fc.count() > 0)
  1483. cmAdIfPrintPgm(_aiH, cmInvalidIdx, fc.value(0) );
  1484. }
  1485. void kcApp::_s_callback(Fl_Widget* wp, void* data)
  1486. { ((kcApp*)data)->_callback(NULL); }
  1487. // this callback is called when the window is closing
  1488. void kcApp::_callback(void* data)
  1489. {
  1490. if( Fl::event() == FL_CLOSE )
  1491. {
  1492. _onCloseApp();
  1493. }
  1494. }
  1495. void kcApp::_s_status_timeout_cb(void* userPtr)
  1496. {
  1497. if( ((kcApp*)userPtr)->_status_timeout_cb() )
  1498. Fl::repeat_timeout(TIMER_PERIOD,_s_status_timeout_cb,userPtr);
  1499. }
  1500. bool kcApp::_status_timeout_cb()
  1501. {
  1502. if( cmTsMp1cIsValid(_printqH) )
  1503. _checkPrintQueue();
  1504. if( !_stopTimerFl )
  1505. {
  1506. _getEngMsg();
  1507. if( _tlCtl != NULL )
  1508. _tlCtl->onIdle();
  1509. }
  1510. if( _ssUpdateFl )
  1511. {
  1512. ++_ssPhase;
  1513. if( _ssPhase >= kSsPhaseMax )
  1514. {
  1515. _clearStatusIndicators();
  1516. _ssPhase = 0;
  1517. }
  1518. }
  1519. if( _stopTimerFl==false && cmAdIfIsValid(_aiH) )
  1520. cmAdIfDispatchMsgToHost(_aiH);
  1521. //if( !_stopTimerFl )
  1522. // _getEngStatus();
  1523. return _stopTimerFl==false || cmTsMp1cIsValid(_printqH);
  1524. }
  1525. void kcApp::_s_menu_cb(Fl_Widget *w, void *data)
  1526. {
  1527. const Fl_Menu_Item* mip;
  1528. kcApp* p;
  1529. if((p=_getApp(w)) == NULL )
  1530. return;
  1531. if((mip = p->_menu->mvalue()) == NULL )
  1532. return;
  1533. unsigned id = (long int)mip->user_data();
  1534. switch(id)
  1535. {
  1536. case kAboutMenuId:
  1537. fl_message("%s Compiled:%s %s",PACKAGE_STRING,__DATE__,__TIME__);
  1538. break;
  1539. case kQuitMenuId:
  1540. p->_onCloseApp();
  1541. break;
  1542. }
  1543. }
  1544. void kcApp::_s_tab_cb(Fl_Widget* w, void* data)
  1545. { ((kcApp*)data)->_tab_cb(w); }
  1546. void kcApp::_tab_cb(Fl_Widget*)
  1547. {
  1548. Fl_Widget* w = _tabs->value();
  1549. _ssUpdateFl = w == (Fl_Widget*)_mstr_grp;
  1550. _ssPhase = kSsPhaseMax;
  1551. if( cmAdIfIsValid(_aiH) )
  1552. {
  1553. if( cmAdIfEnableStatusNotify(_aiH, _ssUpdateFl ) != kOkAiRC )
  1554. cmErrMsg(&_ctx->err,kEngFailKmRC,"A request to enable/disable status notification failed.");
  1555. }
  1556. }
  1557. void kcApp::_s_btn_cb(Fl_Widget* w, long data)
  1558. {
  1559. _getApp(w)->_btn_cb(w,data);
  1560. }
  1561. void kcApp::_btn_cb(Fl_Widget* w, long data)
  1562. {
  1563. unsigned arg = w->argument();
  1564. switch( arg )
  1565. {
  1566. case kAudDevRptBtnId:
  1567. cmAdIfDeviceReport(_aiH);
  1568. break;
  1569. case kEnableBtnId:
  1570. cmAdIfEnableAudio(_aiH,static_cast<Fl_Check_Button*>(w)->value()!=0);
  1571. break;
  1572. case kAudioSysCfgBtnId:
  1573. cmAdIfSetAudioSysCfg(_aiH,static_cast<Fl_Menu_Button*>(w)->mvalue()->argument());
  1574. break;
  1575. case kInAudDevBtnId:
  1576. cmAdIfSetAudioDevice(_aiH,_getCurAudioSubSysIdx(),true,static_cast<Fl_Menu_Button*>(w)->mvalue()->argument());
  1577. break;
  1578. case kOutAudDevBtnId:
  1579. cmAdIfSetAudioDevice(_aiH,_getCurAudioSubSysIdx(),false,static_cast<Fl_Menu_Button*>(w)->mvalue()->argument());
  1580. break;
  1581. case kPgmBtnId:
  1582. cmAdIfLoadProgram(_aiH,_getCurAudioSubSysIdx(),static_cast<Fl_Menu_Button*>(w)->mvalue()->argument());
  1583. break;
  1584. case kSubSystemIdxBtnId:
  1585. // TODO: change device and sample rate menu's to reflect the device and srate assigned to this asSubIdx.
  1586. break;
  1587. case kSrateBtnId:
  1588. cmAdIfSetSampleRate(_aiH,_getCurAudioSubSysIdx(),static_cast<Fl_Menu_Button*>(w)->mvalue()->argument());
  1589. break;
  1590. case kTestBtnId:
  1591. _testStub();
  1592. break;
  1593. case kPrintPgmBtnId:
  1594. _printPgm();
  1595. break;
  1596. default:
  1597. { assert(0); }
  1598. }
  1599. }
  1600. void kcApp::_s_ctl_cb(Fl_Widget* w, void* data)
  1601. {
  1602. ctl_t* cp = ((ctl_t*)data);
  1603. cp->thisPtr->_ctl_cb(cp);
  1604. }
  1605. void kcApp::_ctl_cb(ctl_t* cp)
  1606. {
  1607. cmDspValue_t value = cmDspNullValue;
  1608. unsigned instVarId = cmInvalidId;
  1609. switch( cp->typeId )
  1610. {
  1611. case kNumbTypeId:
  1612. case kSldrTypeId:
  1613. instVarId = cp->u.sldr.varIdArray[ kSldrValArgIdx ];
  1614. cmDsvSetDouble(&value,cp->u.sldr.val->value());
  1615. break;
  1616. case kTextTypeId:
  1617. instVarId = cp->u.text.varIdArray[ kTextValArgIdx ];
  1618. cmDsvSetStrz(&value,(cmChar_t*)cp->u.text.text->value());
  1619. break;
  1620. case kButnTypeId:
  1621. instVarId = cp->u.butn.varIdArray[ kButnValArgIdx ];
  1622. cmDsvSetDouble(&value,cp->u.butn.val);
  1623. break;
  1624. case kChckTypeId:
  1625. {
  1626. bool fl = cp->u.chck.chck->value();
  1627. instVarId = cp->u.chck.varIdArray[ kChckValArgIdx ];
  1628. cmDsvSetDouble(&value, fl ? 1.0 : 0.0 );
  1629. }
  1630. break;
  1631. case kFnamTypeId:
  1632. instVarId = cp->u.fnam.varIdArray[ kFnamValArgIdx ];
  1633. cmDsvSetStrz( &value, (cmChar_t*)cp->u.fnam.fnam->filename());
  1634. break;
  1635. case kMlstTypeId:
  1636. instVarId = cp->u.mlst.varIdArray[ kMlstSelArgIdx ];
  1637. // add one to the selected index to account for skipping title row
  1638. if( cp->u.mlst.mbtn==NULL)
  1639. {
  1640. unsigned idx = cp->u.mlst.mlst->value();
  1641. //printf("list:%i\n",idx);
  1642. cmDsvSetUInt( &value, idx);
  1643. }
  1644. else
  1645. {
  1646. unsigned idx = cp->u.mlst.mbtn->value();
  1647. //printf("mbtn:%i\n",idx);
  1648. cmDsvSetUInt( &value, idx + 1);
  1649. cp->u.mlst.mbtn->label( cp->u.mlst.mbtn->text(idx) );
  1650. }
  1651. break;
  1652. case kTmlnTypeId:
  1653. {
  1654. instVarId = cp->u.tmln.varIdArray[ kTmlnSelArgIdx ];
  1655. unsigned selMarkerId = cp->u.tmln.tlctl->timeLineSelectedMarkerId();
  1656. cmDsvSetUInt(&value, selMarkerId );
  1657. }
  1658. break;
  1659. case kScorTypeId:
  1660. {
  1661. instVarId = cp->u.scor.varIdArray[ kScorSelArgIdx ];
  1662. unsigned selEleIdx = cp->u.scor.tlctl->scoreSelectedEleIndex();
  1663. cmDsvSetUInt(&value, selEleIdx );
  1664. }
  1665. break;
  1666. default:
  1667. {assert(0);}
  1668. }
  1669. if( cmAdIfSendMsgToAudioDSP(
  1670. _aiH,
  1671. cp->asSubIdx,
  1672. cp->mstrFl ? kUiMstrSelAsId : kUiSelAsId,
  1673. kValueDuiId,
  1674. 0,
  1675. cp->instId,
  1676. instVarId,
  1677. &value) != kOkAiRC)
  1678. {
  1679. cmErrMsg(&_ctx->err,kEngFailKmRC,"An attempt to send a UI message to the audio DSP interface failed.");
  1680. }
  1681. }
  1682. void kcApp::vprint(const char* fmt, va_list vl )
  1683. {
  1684. int bufCharCnt = 511;
  1685. char buf[bufCharCnt+1];
  1686. int n = vsnprintf(buf,bufCharCnt,fmt,vl);
  1687. if( n > 0 )
  1688. {
  1689. // if the print queue exists (it might not during startup or shutdown) ...
  1690. if( cmTsMp1cIsValid(_printqH) )
  1691. {
  1692. // ... enqueue the text to print
  1693. if( cmTsMp1cEnqueueMsg(_printqH,buf,n+1) != kOkThRC && _printFl==0 )
  1694. {
  1695. // use _printFl to guard against recursion which would eventually overflow the stack.
  1696. ++_printFl;
  1697. cmErrMsg(&_ctx->err,kQueueFailKmRC,"Print enqueue failed on msg:%s.",buf);
  1698. --_printFl;
  1699. }
  1700. }
  1701. else
  1702. _print(buf); // ... otherwise just send the text directly to the output console
  1703. }
  1704. }
  1705. void kcApp::print( const char* fmt, ... )
  1706. {
  1707. va_list vl;
  1708. va_start(vl,fmt);
  1709. vprint(fmt,vl);
  1710. va_end(vl);
  1711. }
  1712. void kcApp::_s_print( void* userPtr, const char* text )
  1713. { ((kcApp*)userPtr)->print(text); }
  1714. cmRC_t kcApp::_s_print_queue_cb(void* userCbPtr, unsigned msgByteCnt, const void* msgDataPtr )
  1715. {
  1716. kcApp* ap = (kcApp*)userCbPtr;
  1717. ap->_print((const char*)msgDataPtr);
  1718. return cmOkRC;
  1719. }
  1720. void kcApp::_checkPrintQueue()
  1721. {
  1722. while( cmTsMp1cMsgWaiting(_printqH) )
  1723. if( cmTsMp1cDequeueMsg(_printqH, NULL, 0) != kOkThRC && _printFl==0 )
  1724. {
  1725. ++_printFl;
  1726. cmErrMsg(&_ctx->err,kPrintQueFailKmRC,"Print dequeue failed.");
  1727. --_printFl;
  1728. }
  1729. }
  1730. void kcApp::_print( const char* text )
  1731. {
  1732. if( _con != NULL )
  1733. _con->insert(text);
  1734. #ifndef NDEBUG
  1735. fputs(text,stdout);
  1736. #endif
  1737. }