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.

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