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.

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