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.

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