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.

1144 lines
30KB

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