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.

1245 lines
32KB

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