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 57KB

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