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.

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