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.

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