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.

1064 lines
28KB

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