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

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