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.

1101 lines
29KB

  1. /* SpiralSynthModular
  2. * Copyleft (C) 2002 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <string>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <sstream>
  22. #include <FL/Fl.H>
  23. #include <FL/Enumerations.H>
  24. #include <FL/fl_file_chooser.h>
  25. #include <FL/fl_ask.h>
  26. #include <FL/Fl_Box.h>
  27. #include <FL/Fl_Tooltip.h>
  28. #include "SpiralSynthModular.h"
  29. #include "SpiralSound/PluginManager.h"
  30. #include "SpiralSound/Plugins/SpiralPluginGUI.h"
  31. #include "GUI/SSM.xpm"
  32. #include "GUI/load.xpm"
  33. #include "GUI/save.xpm"
  34. #include "GUI/new.xpm"
  35. #include "GUI/options.xpm"
  36. #include "GUI/edit.xpm"
  37. #include "GUI/comment.xpm"
  38. //#define DEBUG_PLUGINS
  39. const static string LABEL = "SpiralSynthModular 0.1.1 MultiThreaded";
  40. static string TITLEBAR;
  41. static const int FILE_VERSION = 3;
  42. static int Numbers[512];
  43. static const int MAIN_WIDTH = 700;
  44. static const int MAIN_HEIGHT = 300;
  45. static const int SLIDER_WIDTH = 15;
  46. static const int TOOLBOX_HEIGHT = MAIN_HEIGHT-40;
  47. static const int TOOLBOX_WIDTH = 132+SLIDER_WIDTH;
  48. static const int ICON_DEPTH = 3;
  49. static const int COMMENT_ID = -1;
  50. using namespace std;
  51. map<int,DeviceWin*> SynthModular::m_DeviceWinMap;
  52. bool SynthModular::m_CallbackUpdateMode = false;
  53. //////////////////////////////////////////////////////////
  54. DeviceWin::~DeviceWin()
  55. {
  56. }
  57. //////////////////////////////////////////////////////////
  58. SynthModular::SynthModular():
  59. m_NextID(0),
  60. m_NextPluginButton(1),
  61. m_NextPluginButtonXPos(5),
  62. m_NextPluginButtonYPos(20),
  63. m_PauseAudio(false)
  64. {
  65. m_Info.BUFSIZE = SpiralInfo::BUFSIZE;
  66. m_Info.FRAGSIZE = SpiralInfo::FRAGSIZE;
  67. m_Info.FRAGCOUNT = SpiralInfo::FRAGCOUNT;
  68. m_Info.SAMPLERATE = SpiralInfo::SAMPLERATE;
  69. m_Info.OUTPUTFILE = SpiralInfo::OUTPUTFILE;
  70. m_Info.MIDIFILE = SpiralInfo::MIDIFILE;
  71. m_Info.POLY = SpiralInfo::POLY;
  72. //m_Info.GUI_COLOUR = SpiralInfo::GUI_COLOUR;
  73. for (int n=0; n<512; n++) Numbers[n]=n;
  74. m_CH.Register("PauseAudio",&m_PauseAudio);
  75. }
  76. //////////////////////////////////////////////////////////
  77. SynthModular::~SynthModular()
  78. {
  79. ClearUp();
  80. PluginManager::Get()->PackUpAndGoHome();
  81. }
  82. //////////////////////////////////////////////////////////
  83. void SynthModular::ClearUp()
  84. {
  85. PauseAudio();
  86. for(map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  87. i!=m_DeviceWinMap.end(); i++)
  88. {
  89. if (i->second->m_DeviceGUI->GetPluginWindow())
  90. {
  91. i->second->m_DeviceGUI->GetPluginWindow()->hide();
  92. m_MainWindow->remove(i->second->m_DeviceGUI->GetPluginWindow());
  93. }
  94. // deleted by Canvas::Remove()? seems to cause random crashes
  95. //delete i->second->m_DeviceGUI;
  96. delete i->second->m_Device;
  97. }
  98. m_Canvas->Clear();
  99. m_DeviceWinMap.clear();
  100. m_NextID=0;
  101. ResumeAudio();
  102. }
  103. //////////////////////////////////////////////////////////
  104. void SynthModular::Update()
  105. {
  106. m_CH.UpdateDataNow();
  107. if (m_PauseAudio) return;
  108. // for all the plugins
  109. for(map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  110. i!=m_DeviceWinMap.end(); i++)
  111. {
  112. if (i->second->m_Device) // if it's not a comment
  113. {
  114. #ifdef DEBUG_PLUGINS
  115. cerr<<"Updating channelhandler of pluin "<<i->second->m_PluginID<<endl;
  116. #endif
  117. // updates the data from the gui thread, if it's not blocking
  118. i->second->m_Device->UpdateChannelHandler();
  119. #ifdef DEBUG_PLUGINS
  120. cerr<<"Finished updating"<<endl;
  121. #endif
  122. // run any commands we've received from the GUI's
  123. i->second->m_Device->ExecuteCommands();
  124. }
  125. }
  126. // run the plugins (only ones connected to anything)
  127. list<int> ExecutionOrder = m_Canvas->GetGraph()->GetSortedList();
  128. for (list<int>::reverse_iterator i=ExecutionOrder.rbegin();
  129. i!=ExecutionOrder.rend(); i++)
  130. {
  131. // use the graphsort order to remove internal latency
  132. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(*i);
  133. if (di!=m_DeviceWinMap.end() && di->second->m_Device)
  134. {
  135. #ifdef DEBUG_PLUGINS
  136. cerr<<"Executing plugin "<<di->second->m_PluginID<<endl;
  137. #endif
  138. di->second->m_Device->Execute();
  139. #ifdef DEBUG_PLUGINS
  140. cerr<<"Finished executing"<<endl;
  141. #endif
  142. }
  143. }
  144. }
  145. //////////////////////////////////////////////////////////
  146. void SynthModular::UpdatePluginGUIs()
  147. {
  148. // see if any need deleting
  149. for (map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  150. i!=m_DeviceWinMap.end(); i++)
  151. {
  152. if (i->second->m_DeviceGUI->GetPluginWindow())
  153. {
  154. SpiralPluginGUI *GUI=(SpiralPluginGUI *)i->second->m_DeviceGUI->GetPluginWindow();
  155. GUI->Update();
  156. }
  157. if (i->second->m_DeviceGUI->Killed())
  158. {
  159. if (i->second->m_DeviceGUI->GetPluginWindow())
  160. {
  161. i->second->m_DeviceGUI->GetPluginWindow()->hide();
  162. m_MainWindow->remove(i->second->m_DeviceGUI->GetPluginWindow());
  163. }
  164. i->second->m_DeviceGUI->Clear();
  165. m_Canvas->RemoveDevice(i->second->m_DeviceGUI);
  166. // deleted by Canvas::Remove()? seems to cause random crashes
  167. //delete i->second->m_DeviceGUI;
  168. if (i->second->m_Device)
  169. {
  170. PauseAudio();
  171. delete i->second->m_Device;
  172. ResumeAudio();
  173. }
  174. m_DeviceWinMap.erase(i);
  175. break;
  176. }
  177. }
  178. }
  179. //////////////////////////////////////////////////////////
  180. SpiralWindowType *SynthModular::CreateWindow()
  181. {
  182. int xoff=0, yoff=10, but=64, gap=MAIN_HEIGHT/4, n=0;
  183. m_TopWindow = new SpiralWindowType(MAIN_WIDTH, MAIN_HEIGHT*2, LABEL.c_str());
  184. m_TopWindow->resizable(m_TopWindow);
  185. m_TopTile = new Fl_Tile(0,0,MAIN_WIDTH, MAIN_HEIGHT*2, "");
  186. m_TopWindow->add(m_TopTile);
  187. m_MainWindow = new Fl_Tile(0,0,MAIN_WIDTH, MAIN_HEIGHT, "");
  188. m_MainWindow->color(SpiralSynthModularInfo::GUICOL_Canvas);
  189. //m_MainWindow->callback((Fl_Callback*)cb_Close);
  190. m_MainWindow->user_data((void*)(this));
  191. m_TopTile->add(m_MainWindow);
  192. m_MainButtons = new Fl_Group(0, 0, but, MAIN_HEIGHT, "");
  193. m_MainButtons->type(1);
  194. m_MainButtons->color(SpiralSynthModularInfo::GUICOL_Tool);
  195. m_MainButtons->box(FL_FLAT_BOX);
  196. m_MainButtons->user_data((void*)(this));
  197. m_MainWindow->add(m_MainButtons);
  198. m_AppScroll = new Fl_Scroll(but, 0, MAIN_WIDTH-but, MAIN_HEIGHT, "");
  199. m_AppScroll->scrollbar.align(FL_ALIGN_RIGHT);
  200. m_MainWindow->add(m_AppScroll);
  201. //m_MainWindow->resizable(m_AppScroll);
  202. m_AppGroup = new Fl_Group(-5000, -5000, 10000, 10000, "");
  203. m_AppGroup->type(1);
  204. m_AppGroup->box(FL_FLAT_BOX);
  205. m_AppGroup->labeltype(FL_ENGRAVED_LABEL);
  206. m_AppGroup->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  207. m_AppGroup->color(SpiralSynthModularInfo::GUICOL_Canvas);
  208. m_AppScroll->add(m_AppGroup);
  209. m_Load = new Fl_Button(xoff, 5+yoff, but, but, "");
  210. m_Load->box(FL_NO_BOX);
  211. Fl_Pixmap *tPix = new Fl_Pixmap(load_xpm);
  212. m_Load->image(tPix->copy(tPix->w(),tPix->h()));
  213. m_Load->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  214. m_Load->tooltip("Load a design file");
  215. m_Load->callback((Fl_Callback*)cb_Load);
  216. m_MainButtons->add(m_Load);
  217. n++;
  218. m_Save = new Fl_Button(xoff, n*gap+yoff, but, but, "");
  219. m_Save->box(FL_NO_BOX);
  220. tPix = new Fl_Pixmap(save_xpm);
  221. m_Save->image(tPix->copy(tPix->w(),tPix->h()));
  222. delete tPix;
  223. m_Save->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  224. m_Save->tooltip("Save a design file");
  225. m_Save->callback((Fl_Callback*)cb_Save);
  226. m_MainButtons->add(m_Save);
  227. n++;
  228. m_New = new Fl_Button(xoff, n*gap+yoff, but, but, "");
  229. m_New->box(FL_NO_BOX);
  230. tPix = new Fl_Pixmap(new_xpm);
  231. m_New->image(tPix->copy(tPix->w(),tPix->h()));
  232. delete tPix;
  233. m_New->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  234. m_New->tooltip("New design");
  235. m_New->callback((Fl_Callback*)cb_New);
  236. m_MainButtons->add(m_New);
  237. n++;
  238. m_Options = new Fl_Button(xoff, n*gap+yoff, but, but, "");
  239. m_Options->box(FL_NO_BOX);
  240. tPix = new Fl_Pixmap(options_xpm);
  241. m_Options->image(tPix->copy(tPix->w(),tPix->h()));
  242. delete tPix;
  243. m_Options->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  244. m_Options->tooltip("Options");
  245. m_Options->callback((Fl_Callback*)cb_Rload);
  246. m_MainButtons->add(m_Options);
  247. n++;
  248. /*m_OpenEditor = new Fl_Button(5+xoff, n*gap+yoff, but, but, "");
  249. m_OpenEditor->box(FL_NO_BOX);
  250. tPix = new Fl_Pixmap(edit_xpm);
  251. m_OpenEditor->image(tPix->copy(tPix->w(),tPix->h()));
  252. delete tPix;
  253. m_OpenEditor->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  254. m_OpenEditor->tooltip("Open/Close Editor");
  255. m_OpenEditor->callback((Fl_Callback*)cb_OpenEditor);
  256. m_MainButtons->add(m_OpenEditor);
  257. n++;*/
  258. /////////////////
  259. m_EditorWindow = new Fl_Tile(0,MAIN_HEIGHT,MAIN_WIDTH, MAIN_HEIGHT, "");
  260. m_EditorWindow->color(SpiralSynthModularInfo::GUICOL_Tool);
  261. m_TopTile->add(m_EditorWindow);
  262. int edy = MAIN_HEIGHT;
  263. Fl_Group *Left = new Fl_Group(0,MAIN_HEIGHT,TOOLBOX_WIDTH,MAIN_HEIGHT);
  264. m_EditorWindow->add(Left);
  265. m_EditorWindow->resizable(Left);
  266. m_ToolBox = new Fl_Scroll(0,0+edy,TOOLBOX_WIDTH, TOOLBOX_HEIGHT, "");
  267. m_ToolBox->type(Fl_Scroll::VERTICAL_ALWAYS);
  268. m_ToolBox->box(FL_FLAT_BOX);
  269. m_ToolBox->labeltype(FL_ENGRAVED_LABEL);
  270. m_ToolBox->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  271. m_ToolBox->scrollbar.align(FL_ALIGN_LEFT);
  272. m_ToolBox->color(SpiralSynthModularInfo::GUICOL_Tool);
  273. m_ToolBox->user_data((void*)(this));
  274. Left->add(m_ToolBox);
  275. // m_EditorWindow->resizable(m_ToolBox);
  276. m_ToolPack = new Fl_Pack(SLIDER_WIDTH+5,5+edy,TOOLBOX_WIDTH-10, TOOLBOX_HEIGHT-40,"");
  277. m_ToolPack->type(FL_VERTICAL);
  278. m_ToolPack->box(FL_NO_BOX);
  279. m_ToolPack->color(SpiralSynthModularInfo::GUICOL_Tool);
  280. m_ToolPack->user_data((void*)(this));
  281. m_ToolBox->add(m_ToolPack);
  282. //m_EditorWindow->resizable(m_ToolBox);
  283. xoff=0; yoff=MAIN_HEIGHT+TOOLBOX_HEIGHT;
  284. m_Buttons = new Fl_Group(xoff, yoff, TOOLBOX_WIDTH, MAIN_HEIGHT*2-TOOLBOX_HEIGHT, "");
  285. m_Buttons->type(1);
  286. m_Buttons->color(SpiralSynthModularInfo::GUICOL_Tool);
  287. m_Buttons->box(FL_FLAT_BOX);
  288. m_Buttons->user_data((void*)(this));
  289. Left->add(m_Buttons);
  290. m_CanvasScroll = new Fl_Scroll(TOOLBOX_WIDTH, 0+edy, MAIN_WIDTH-TOOLBOX_WIDTH, MAIN_HEIGHT, "");
  291. m_EditorWindow->add(m_CanvasScroll);
  292. m_EditorWindow->resizable(m_CanvasScroll);
  293. m_Canvas = new Fl_Canvas(-5000, -5000, 10000, 10000, "");
  294. m_Canvas->type(1);
  295. m_Canvas->box(FL_FLAT_BOX);
  296. m_Canvas->labeltype(FL_ENGRAVED_LABEL);
  297. m_Canvas->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  298. m_Canvas->color(SpiralSynthModularInfo::GUICOL_Canvas);
  299. m_Canvas->user_data((void*)(this));
  300. m_Canvas->SetConnectionCallback((Fl_Callback*)cb_Connection);
  301. m_Canvas->SetUnconnectCallback((Fl_Callback*)cb_Unconnect);
  302. m_Canvas->SetAddDeviceCallback((Fl_Callback*)cb_NewDeviceFromMenu);
  303. m_CanvasScroll->add(m_Canvas);
  304. m_NewComment = new Fl_Button(TOOLBOX_WIDTH/2-16, MAIN_HEIGHT*2-25, 32, 20, "");
  305. m_NewComment->box(FL_NO_BOX);
  306. m_Canvas->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  307. tPix = new Fl_Pixmap(comment_xpm);
  308. m_NewComment->image(tPix->copy(tPix->w(),tPix->h()));
  309. delete tPix;
  310. m_NewComment->color(SpiralSynthModularInfo::GUICOL_Button);
  311. m_NewComment->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  312. m_NewComment->type(0);
  313. m_NewComment->shortcut(FL_F + 10);
  314. m_NewComment->tooltip("New comment in editor");
  315. m_NewComment->user_data((void*)(this));
  316. m_NewComment->callback((Fl_Callback*)cb_NewComment);
  317. m_Buttons->add(m_NewComment);
  318. m_SettingsWindow = new SettingsWindow;
  319. m_SettingsWindow->RegisterApp(this);
  320. return m_TopWindow;
  321. }
  322. //////////////////////////////////////////////////////////
  323. void SynthModular::LoadPlugins()
  324. {
  325. int Width = 40;
  326. int Height = 40;
  327. int SWidth = 392;
  328. int SHeight = 187;
  329. Fl_Pixmap pic(SSM_xpm);
  330. Fl_Double_Window* Splash = new Fl_Double_Window((Fl::w()/2)-(SWidth/2),
  331. (Fl::h()/2)-(SHeight/2),
  332. SWidth,SHeight,"SSM");
  333. Splash->border(0);
  334. Fl_Box* pbut = new Fl_Box(0,8,SWidth,SHeight,"");
  335. pbut->box(FL_NO_BOX);
  336. pic.label(pbut);
  337. Fl_Box *splashtext = new Fl_Box(5,SHeight-20,200,20,"Loading...");
  338. splashtext->labelsize(10);
  339. splashtext->box(FL_NO_BOX);
  340. splashtext->align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
  341. Splash->add(pbut);
  342. Splash->add(splashtext);
  343. Splash->show();
  344. int ID=-1;
  345. int Icon=0;
  346. Fl_Pack *IconPack;
  347. IconPack = new Fl_Pack(0,0,TOOLBOX_WIDTH-SLIDER_WIDTH,Height,"");
  348. IconPack->type(FL_HORIZONTAL);
  349. IconPack->color(SpiralSynthModularInfo::GUICOL_Tool);
  350. IconPack->user_data((void*)(this));
  351. m_ToolPack->add(IconPack);
  352. for (vector<string>::iterator i=SpiralSynthModularInfo::PLUGINVEC.begin();
  353. i!=SpiralSynthModularInfo::PLUGINVEC.end(); i++)
  354. {
  355. string Fullpath=SpiralSynthModularInfo::PLUGIN_PATH+*i;
  356. ID=PluginManager::Get()->LoadPlugin(Fullpath.c_str());
  357. if (ID!=PluginError)
  358. {
  359. #ifdef DEBUG_PLUGINS
  360. cerr<<"Plugin ["<<*i<<"] = "<<ID<<endl;
  361. #endif
  362. if (Icon>=ICON_DEPTH)
  363. {
  364. Icon=0;
  365. IconPack = new Fl_Pack(0,0,TOOLBOX_WIDTH-SLIDER_WIDTH,Height,"");
  366. IconPack->type(FL_HORIZONTAL);
  367. IconPack->color(SpiralSynthModularInfo::GUICOL_Tool);
  368. IconPack->user_data((void*)(this));
  369. m_ToolPack->add(IconPack);
  370. }
  371. Fl_Button *NewButton = new Fl_Button(0,0,Width,Height,"");
  372. NewButton->labelsize(10);
  373. Fl_Pixmap *tPix = new Fl_Pixmap(PluginManager::Get()->GetPlugin(ID)->GetIcon());
  374. NewButton->image(tPix->copy(tPix->w(),tPix->h()));
  375. delete tPix;
  376. IconPack->add(NewButton);
  377. NewButton->type(0);
  378. NewButton->box(FL_PLASTIC_UP_BOX);
  379. NewButton->align(FL_ALIGN_INSIDE|FL_ALIGN_TOP);
  380. NewButton->color(SpiralSynthModularInfo::GUICOL_Button);
  381. NewButton->selection_color(SpiralSynthModularInfo::GUICOL_Tool);
  382. string tooltip=*i;
  383. // find the first / if there is one, and get rid of everything before and including it
  384. unsigned int p = tooltip.find ('/');
  385. if (p < tooltip.length()) tooltip.erase (0, p);
  386. // find last . and get rid of everything after and including it
  387. p = tooltip.rfind ('.');
  388. unsigned int l = tooltip.length ();
  389. if (p < l) tooltip.erase (p, l);
  390. m_Canvas->AddPluginName (tooltip, PluginManager::Get()->GetPlugin(ID)->ID);
  391. splashtext->label (tooltip.c_str());
  392. Splash->redraw();
  393. NewButton->tooltip (tooltip.c_str());
  394. NewButton->callback((Fl_Callback*)cb_NewDevice,&Numbers[ID]);
  395. NewButton->show();
  396. m_DeviceVec.push_back(NewButton);
  397. Icon++;
  398. m_ToolBox->redraw();
  399. m_NextPluginButton++;
  400. Fl::check();
  401. }
  402. }
  403. Splash->hide();
  404. delete Splash;
  405. }
  406. //////////////////////////////////////////////////////////
  407. DeviceGUIInfo SynthModular::BuildDeviceGUIInfo(PluginInfo &PInfo)
  408. {
  409. DeviceGUIInfo Info;
  410. int Height=50;
  411. // tweak the size if we have too many ins/outs
  412. if (PInfo.NumInputs>4 || PInfo.NumOutputs>4)
  413. {
  414. if (PInfo.NumInputs<PInfo.NumOutputs)
  415. {
  416. Height=PInfo.NumOutputs*10+5;
  417. }
  418. else
  419. {
  420. Height=PInfo.NumInputs*10+5;
  421. }
  422. }
  423. // Make the guiinfo struct
  424. Info.XPos = 0;
  425. Info.YPos = 0;
  426. Info.Width = 40;
  427. Info.Height = Height;
  428. Info.NumInputs = PInfo.NumInputs;
  429. Info.NumOutputs = PInfo.NumOutputs;
  430. Info.Name = PInfo.Name;
  431. Info.PortTips = PInfo.PortTips;
  432. Info.PortTypes = PInfo.PortTypes;
  433. return Info;
  434. }
  435. //////////////////////////////////////////////////////////
  436. DeviceWin* SynthModular::NewDeviceWin(int n, int x, int y)
  437. {
  438. DeviceWin *nlw = new DeviceWin;
  439. const HostsideInfo* Plugin=PluginManager::Get()->GetPlugin(n);
  440. if (!Plugin) return NULL;
  441. nlw->m_Device=Plugin->CreateInstance();
  442. if (!nlw->m_Device)
  443. {
  444. return NULL;
  445. }
  446. nlw->m_Device->SetUpdateCallback(cb_Update);
  447. nlw->m_Device->SetParent((void*)this);
  448. PluginInfo PInfo = nlw->m_Device->Initialise(&m_Info);
  449. SpiralGUIType *temp = nlw->m_Device->CreateGUI();
  450. Fl_Pixmap *Pix = new Fl_Pixmap(Plugin->GetIcon());
  451. nlw->m_PluginID = n;
  452. if (temp)
  453. {
  454. temp->hide();
  455. temp->position(200,50);
  456. m_AppGroup->add(temp);
  457. m_MainWindow->redraw();
  458. }
  459. DeviceGUIInfo Info=BuildDeviceGUIInfo(PInfo);
  460. Info.XPos = x; //TOOLBOX_WIDTH+(rand()%400);
  461. Info.YPos = y; //rand()%400;
  462. nlw->m_DeviceGUI = new Fl_DeviceGUI(Info, temp, Pix);
  463. m_Canvas->add(nlw->m_DeviceGUI);
  464. m_Canvas->redraw();
  465. return nlw;
  466. }
  467. //////////////////////////////////////////////////////////
  468. void SynthModular::AddDevice(int n, int x=TOOLBOX_WIDTH+50, int y=400)
  469. {
  470. //cerr<<"Adding "<<m_NextID<<endl;
  471. DeviceWin* temp = NewDeviceWin(n,x,y);
  472. if (temp)
  473. {
  474. int ID=m_NextID++;
  475. //cerr<<"adding device "<<ID<<endl;
  476. temp->m_DeviceGUI->SetID(ID);
  477. temp->m_Device->SetUpdateInfoCallback(ID,cb_UpdatePluginInfo);
  478. m_DeviceWinMap[ID]=temp;
  479. }
  480. }
  481. //////////////////////////////////////////////////////////
  482. DeviceWin* SynthModular::NewComment(int n, int x=TOOLBOX_WIDTH+50, int y=400)
  483. {
  484. DeviceWin *nlw = new DeviceWin;
  485. nlw->m_Device=NULL;
  486. nlw->m_PluginID = COMMENT_ID;
  487. DeviceGUIInfo Info;
  488. Info.XPos = x;
  489. Info.YPos = y;
  490. Info.Width = 50;
  491. Info.Height = 20;
  492. Info.NumInputs = 0;
  493. Info.NumOutputs = 0;
  494. Info.Name = "";
  495. nlw->m_DeviceGUI = new Fl_CommentGUI(Info, NULL, NULL);
  496. m_Canvas->add(nlw->m_DeviceGUI);
  497. m_Canvas->redraw();
  498. return nlw;
  499. }
  500. //////////////////////////////////////////////////////////
  501. void SynthModular::AddComment(int n)
  502. {
  503. //cerr<<"Adding "<<m_NextID<<endl;
  504. DeviceWin* temp = NewComment(n);
  505. if (temp)
  506. {
  507. int ID=m_NextID++;
  508. //cerr<<"adding comment "<<ID<<endl;
  509. temp->m_DeviceGUI->SetID(ID);
  510. m_DeviceWinMap[ID]=temp;
  511. }
  512. }
  513. //////////////////////////////////////////////////////////
  514. void SynthModular::UpdateHostInfo()
  515. {
  516. std::stringstream str;
  517. str<<*this;
  518. ClearUp();
  519. // update the settings
  520. m_Info.BUFSIZE = SpiralInfo::BUFSIZE;
  521. m_Info.FRAGSIZE = SpiralInfo::FRAGSIZE;
  522. m_Info.FRAGCOUNT = SpiralInfo::FRAGCOUNT;
  523. m_Info.SAMPLERATE = SpiralInfo::SAMPLERATE;
  524. m_Info.OUTPUTFILE = SpiralInfo::OUTPUTFILE;
  525. m_Info.MIDIFILE = SpiralInfo::MIDIFILE;
  526. m_Info.POLY = SpiralInfo::POLY;
  527. str>>*this;
  528. }
  529. //////////////////////////////////////////////////////////
  530. void SynthModular::cb_Update(void* o, bool mode)
  531. {
  532. m_CallbackUpdateMode=mode;
  533. ((SynthModular*)o)->Update();
  534. }
  535. //////////////////////////////////////////////////////////
  536. istream &operator>>(istream &s, SynthModular &o)
  537. {
  538. o.PauseAudio();
  539. string dummy;
  540. int ver;
  541. s>>dummy>>dummy>>dummy>>ver;
  542. if (ver>FILE_VERSION)
  543. {
  544. SpiralInfo::Alert("Bad file, or more recent version.");
  545. return s;
  546. }
  547. if (ver>2)
  548. {
  549. int MainWinX,MainWinY,MainWinW,MainWinH;
  550. int EditWinX,EditWinY,EditWinW,EditWinH;
  551. s>>MainWinX>>MainWinY>>MainWinW>>MainWinH;
  552. s>>EditWinX>>EditWinY>>EditWinW>>EditWinH;
  553. //o.m_MainWindow->resize(MainWinX,MainWinY,MainWinW,MainWinH);
  554. //o.m_EditorWindow->resize(EditWinX,EditWinY,EditWinW,EditWinH);
  555. }
  556. int Num, ID, PluginID, x,y,ps,px,py;
  557. s>>dummy>>Num;
  558. for(int n=0; n<Num; n++)
  559. {
  560. //cerr<<"Loading Device "<<n<<endl;
  561. s>>dummy; //cerr<<dummy<<" ";
  562. s>>ID; //cerr<<ID<<" ";
  563. s>>dummy; //cerr<<dummy<<" ";
  564. s>>PluginID; //cerr<<PluginID<<endl;
  565. s>>x>>y;
  566. if (ver>1) s>>ps>>px>>py;
  567. // Check we're not duplicating an ID
  568. if (o.m_DeviceWinMap.find(ID)!=o.m_DeviceWinMap.end())
  569. {
  570. SpiralInfo::Alert("Duplicate device ID found in file - aborting load");
  571. return s;
  572. }
  573. if (PluginID==COMMENT_ID)
  574. {
  575. DeviceWin* temp = o.NewComment(PluginID, x, y);
  576. if (temp)
  577. {
  578. temp->m_DeviceGUI->SetID(ID);
  579. o.m_DeviceWinMap[ID]=temp;
  580. ((Fl_CommentGUI*)(o.m_DeviceWinMap[ID]->m_DeviceGUI))->StreamIn(s); // load the plugin
  581. if (o.m_NextID<=ID) o.m_NextID=ID+1;
  582. }
  583. }
  584. else
  585. {
  586. DeviceWin* temp = o.NewDeviceWin(PluginID, x, y);
  587. if (temp)
  588. {
  589. temp->m_DeviceGUI->SetID(ID);
  590. temp->m_Device->SetUpdateInfoCallback(ID,o.cb_UpdatePluginInfo);
  591. o.m_DeviceWinMap[ID]=temp;
  592. o.m_DeviceWinMap[ID]->m_Device->StreamIn(s); // load the plugin
  593. if (ver>1 && o.m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow())
  594. {
  595. // set the GUI up with the loaded values
  596. // looks messy, but if we do it here, the plugin and it's gui can remain
  597. // totally seperated.
  598. ((SpiralPluginGUI*)(o.m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()))->
  599. UpdateValues(o.m_DeviceWinMap[ID]->m_Device);
  600. // updates the data in the channel buffers, so the values don't
  601. // get overwritten in the next tick. (should maybe be somewhere else)
  602. o.m_DeviceWinMap[ID]->m_Device->GetChannelHandler()->FlushChannels();
  603. // position the plugin window in the main window
  604. o.m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()->position(px,py);
  605. if (ps) o.m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()->show();
  606. else o.m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()->hide();
  607. // load external files
  608. o.m_DeviceWinMap[ID]->m_Device->LoadExternalFiles(o.m_FilePath+"_files/");
  609. }
  610. if (o.m_NextID<=ID) o.m_NextID=ID+1;
  611. }
  612. else
  613. {
  614. // can't really recover if the plugin ID doesn't match a plugin, as
  615. // we have no idea how much data in the stream belongs to this plugin
  616. SpiralInfo::Alert("Error in stream, can't really recover data from here on.");
  617. return s;
  618. }
  619. }
  620. }
  621. s>>*o.m_Canvas;
  622. o.ResumeAudio();
  623. return s;
  624. }
  625. //////////////////////////////////////////////////////////
  626. ostream &operator<<(ostream &s, SynthModular &o)
  627. {
  628. o.PauseAudio();
  629. s<<"SpiralSynthModular File Ver "<<FILE_VERSION<<endl;
  630. // make external files dir
  631. bool ExternalDirUsed=false;
  632. string command("mkdir "+o.m_FilePath+"_files");
  633. system(command.c_str());
  634. if (FILE_VERSION>2)
  635. {
  636. s<<o.m_MainWindow->x()<<" "<<o.m_MainWindow->y()<<" ";
  637. s<<o.m_MainWindow->w()<<" "<<o.m_MainWindow->h()<<" ";
  638. s<<o.m_EditorWindow->x()<<" "<<o.m_EditorWindow->y()<<" ";
  639. s<<o.m_EditorWindow->w()<<" "<<o.m_EditorWindow->h()<<endl;
  640. }
  641. // save out the SynthModular
  642. s<<"SectionList"<<endl;
  643. s<<o.m_DeviceWinMap.size()<<endl;
  644. for(map<int,DeviceWin*>::iterator i=o.m_DeviceWinMap.begin();
  645. i!=o.m_DeviceWinMap.end(); i++)
  646. {
  647. s<<endl;
  648. s<<"Device ";
  649. s<<i->first<<" "; // save the id
  650. s<<"Plugin ";
  651. s<<i->second->m_PluginID<<endl;
  652. s<<i->second->m_DeviceGUI->x()<<" ";
  653. s<<i->second->m_DeviceGUI->y()<<" ";
  654. if (i->second->m_DeviceGUI->GetPluginWindow())
  655. {
  656. s<<i->second->m_DeviceGUI->GetPluginWindow()->visible()<<" ";
  657. s<<i->second->m_DeviceGUI->GetPluginWindow()->x()<<" ";
  658. s<<i->second->m_DeviceGUI->GetPluginWindow()->y()<<" ";
  659. }
  660. else
  661. {
  662. s<<0<<" "<<0<<" "<<0;
  663. }
  664. s<<endl;
  665. if (i->second->m_PluginID==COMMENT_ID)
  666. {
  667. // save the comment gui
  668. ((Fl_CommentGUI*)(i->second->m_DeviceGUI))->StreamOut(s);
  669. }
  670. else
  671. {
  672. // save the plugin
  673. i->second->m_Device->StreamOut(s);
  674. }
  675. s<<endl;
  676. // save external files
  677. if (i->second->m_Device && i->second->m_Device->SaveExternalFiles(o.m_FilePath+"_files/"))
  678. {
  679. ExternalDirUsed=true;
  680. }
  681. }
  682. s<<endl<<*o.m_Canvas<<endl;
  683. // remove it if it wasn't used
  684. if (!ExternalDirUsed)
  685. {
  686. // i guess rmdir won't work if there is something in the dir
  687. // anyway, but best to be on the safe side. (could do rm -rf) :)
  688. string command("rmdir "+o.m_FilePath+"_files");
  689. system(command.c_str());
  690. }
  691. o.ResumeAudio();
  692. return s;
  693. }
  694. //////////////////////////////////////////////////////////
  695. inline void SynthModular::cb_Close_i(Fl_Window* o, void* v)
  696. {
  697. m_SettingsWindow->hide();
  698. delete m_SettingsWindow;
  699. m_EditorWindow->hide();
  700. delete m_EditorWindow;
  701. o->hide();
  702. }
  703. void SynthModular::cb_Close(Fl_Window* o, void* v)
  704. {((SynthModular*)(o->user_data()))->cb_Close_i(o,v);}
  705. //////////////////////////////////////////////////////////
  706. inline void SynthModular::cb_Load_i(Fl_Button* o, void* v)
  707. {
  708. if (m_DeviceWinMap.size()>0 && !fl_ask("Load - Loose changes to current design?"))
  709. {
  710. return;
  711. }
  712. char *fn=fl_file_chooser("Load a design", "*.ssm", NULL);
  713. if (fn && fn!='\0')
  714. {
  715. ifstream inf(fn);
  716. if (inf)
  717. {
  718. m_FilePath=fn;
  719. ClearUp();
  720. inf>>*this;
  721. TITLEBAR=LABEL+" "+fn;
  722. m_TopWindow->label(TITLEBAR.c_str());
  723. }
  724. }
  725. }
  726. void SynthModular::cb_Load(Fl_Button* o, void* v)
  727. {((SynthModular*)(o->parent()->user_data()))->cb_Load_i(o,v);}
  728. //////////////////////////////////////////////////////////
  729. inline void SynthModular::cb_Save_i(Fl_Button* o, void* v)
  730. {
  731. char *fn=fl_file_chooser("Save a design", "*.ssm", NULL);
  732. if (fn && fn!='\0')
  733. {
  734. ifstream ifl(fn);
  735. if (ifl)
  736. {
  737. if (!fl_ask("File [%s] exists, overwrite?",fn))
  738. {
  739. return;
  740. }
  741. }
  742. ofstream of(fn);
  743. if (of)
  744. {
  745. m_FilePath=fn;
  746. of<<*this;
  747. TITLEBAR=LABEL+" "+fn;
  748. m_TopWindow->label(TITLEBAR.c_str());
  749. }
  750. }
  751. }
  752. void SynthModular::cb_Save(Fl_Button* o, void* v)
  753. {((SynthModular*)(o->parent()->user_data()))->cb_Save_i(o,v);}
  754. //////////////////////////////////////////////////////////
  755. inline void SynthModular::cb_New_i(Fl_Button* o, void* v)
  756. {
  757. if (m_DeviceWinMap.size()>0 && !fl_ask("New - Loose changes to current design?"))
  758. {
  759. return;
  760. }
  761. m_TopWindow->label(TITLEBAR.c_str());
  762. ClearUp();
  763. }
  764. void SynthModular::cb_New(Fl_Button* o, void* v)
  765. {((SynthModular*)(o->parent()->user_data()))->cb_New_i(o,v);}
  766. //////////////////////////////////////////////////////////
  767. inline void SynthModular::cb_NewDevice_i(Fl_Button* o, void* v)
  768. {
  769. AddDevice(*((int*)v));
  770. }
  771. void SynthModular::cb_NewDevice(Fl_Button* o, void* v)
  772. {((SynthModular*)(o->parent()->user_data()))->cb_NewDevice_i(o,v);}
  773. //////////////////////////////////////////////////////////
  774. inline void SynthModular::cb_NewDeviceFromMenu_i(Fl_Canvas* o, void* v)
  775. {
  776. AddDevice(*((int*)v),*((int*)v+1),*((int*)v+2));
  777. }
  778. void SynthModular::cb_NewDeviceFromMenu(Fl_Canvas* o, void* v)
  779. {((SynthModular*)(o->user_data()))->cb_NewDeviceFromMenu_i(o,v);}
  780. //////////////////////////////////////////////////////////
  781. inline void SynthModular::cb_NewComment_i(Fl_Button* o, void* v)
  782. {
  783. AddComment(-1);
  784. }
  785. void SynthModular::cb_NewComment(Fl_Button* o, void* v)
  786. {((SynthModular*)(o->user_data()))->cb_NewComment_i(o,v);}
  787. //////////////////////////////////////////////////////////
  788. inline void SynthModular::cb_OpenEditor_i(Fl_Button* o, void* v)
  789. {
  790. //if (m_EditorWindow->shown()) m_EditorWindow->hide();
  791. //else m_EditorWindow->show();
  792. }
  793. void SynthModular::cb_OpenEditor(Fl_Button* o, void* v)
  794. {((SynthModular*)(o->parent()->user_data()))->cb_OpenEditor_i(o,v);}
  795. //////////////////////////////////////////////////////////
  796. inline void SynthModular::cb_Rload_i(Fl_Button* o, void* v)
  797. {
  798. m_SettingsWindow->show();
  799. /*PluginManager::Get()->UnloadAll();
  800. m_ToolBox->remove(m_ToolPack);
  801. delete m_ToolPack;
  802. m_ToolPack = new Fl_Pack(5,20,TOOLBOX_WIDTH-10, TOOLBOX_HEIGHT-40,"");
  803. m_ToolPack->type(FL_VERTICAL);
  804. m_ToolPack->box(FL_NO_BOX);
  805. m_ToolPack->color(SpiralSynthModularInfo::GUICOL_Tool);
  806. m_ToolPack->user_data((void*)(this));
  807. m_ToolBox->add(m_ToolPack);
  808. m_ToolBox->redraw();
  809. LoadPlugins();*/
  810. }
  811. void SynthModular::cb_Rload(Fl_Button* o, void* v)
  812. {((SynthModular*)(o->parent()->user_data()))->cb_Rload_i(o,v);}
  813. //////////////////////////////////////////////////////////
  814. inline void SynthModular::cb_Connection_i(Fl_Canvas* o, void* v)
  815. {
  816. CanvasWire *Wire;
  817. Wire=(CanvasWire*)v;
  818. map<int,DeviceWin*>::iterator si=m_DeviceWinMap.find(Wire->OutputID);
  819. if (si==m_DeviceWinMap.end())
  820. {
  821. char num[32];
  822. sprintf(num,"%d",Wire->OutputID);
  823. SpiralInfo::Alert("Warning: Connection problem - can't find source "+string(num));
  824. return;
  825. }
  826. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  827. if (di==m_DeviceWinMap.end())
  828. {
  829. char num[32];
  830. sprintf(num,"%d",Wire->InputID);
  831. SpiralInfo::Alert("Warning: Connection problem - can't find destination "+string(num));
  832. return;
  833. }
  834. Sample *sample=NULL;
  835. if (!si->second->m_Device->GetOutput(Wire->OutputPort,&sample))
  836. {
  837. char num[32];
  838. sprintf(num,"%d,%d",Wire->OutputID,Wire->OutputPort);
  839. SpiralInfo::Alert("Warning: Connection problem - can't find source output "+string(num));
  840. return;
  841. }
  842. if (!di->second->m_Device->SetInput(Wire->InputPort,(const Sample*)sample))
  843. {
  844. char num[32];
  845. sprintf(num,"%d,%d",Wire->InputID,Wire->InputPort);
  846. SpiralInfo::Alert("Warning: Connection problem - can't find source input "+string(num));
  847. return;
  848. }
  849. }
  850. void SynthModular::cb_Connection(Fl_Canvas* o, void* v)
  851. {((SynthModular*)(o->user_data()))->cb_Connection_i(o,v);}
  852. //////////////////////////////////////////////////////////
  853. inline void SynthModular::cb_Unconnect_i(Fl_Canvas* o, void* v)
  854. {
  855. CanvasWire *Wire;
  856. Wire=(CanvasWire*)v;
  857. //cerr<<Wire->InputID<<" "<<Wire->InputPort<<endl;
  858. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  859. if (di==m_DeviceWinMap.end())
  860. {
  861. //cerr<<"Can't find destination device "<<Wire->InputID<<endl;
  862. return;
  863. }
  864. if (!di->second->m_Device->SetInput(Wire->InputPort,NULL))
  865. { cerr<<"Can't find destination device's Input"<<endl; return; }
  866. }
  867. void SynthModular::cb_Unconnect(Fl_Canvas* o, void* v)
  868. {((SynthModular*)(o->user_data()))->cb_Unconnect_i(o,v);}
  869. //////////////////////////////////////////////////////////
  870. void SynthModular::cb_UpdatePluginInfo(int ID, void *PInfo)
  871. {
  872. map<int,DeviceWin*>::iterator i=m_DeviceWinMap.find(ID);
  873. if (i!=m_DeviceWinMap.end())
  874. {
  875. DeviceGUIInfo Info=BuildDeviceGUIInfo(*((PluginInfo*)PInfo));
  876. (*i).second->m_DeviceGUI->Setup(Info);
  877. (*i).second->m_DeviceGUI->redraw();
  878. }
  879. }
  880. //////////////////////////////////////////////////////////
  881. void SynthModular::LoadPatch(const char *fn)
  882. {
  883. ifstream inf(fn);
  884. if (inf)
  885. {
  886. m_FilePath=fn;
  887. ClearUp();
  888. inf>>*this;
  889. TITLEBAR=LABEL+" "+fn;
  890. m_TopWindow->label(TITLEBAR.c_str());
  891. }
  892. }
  893. //////////////////////////////////////////////////////////