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.

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