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.

1155 lines
31KB

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