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.

1593 lines
46KB

  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_Frozen(false),
  64. m_NextID(0)
  65. {
  66. /* Shared Audio State Information */
  67. m_Info.BUFSIZE = SpiralInfo::BUFSIZE;
  68. m_Info.SAMPLERATE = SpiralInfo::SAMPLERATE;
  69. m_Info.PAUSED = false;
  70. /* obsolete - REMOVE SOON */
  71. m_Info.FRAGSIZE = SpiralInfo::FRAGSIZE;
  72. m_Info.FRAGCOUNT = SpiralInfo::FRAGCOUNT;
  73. m_Info.OUTPUTFILE = SpiralInfo::OUTPUTFILE;
  74. m_Info.MIDIFILE = SpiralInfo::MIDIFILE;
  75. m_Info.POLY = SpiralInfo::POLY;
  76. /* Shared GUI Preferences Information */
  77. m_Info.GUI_COLOUR = SpiralInfo::GUI_COLOUR;
  78. m_Info.SCOPE_BG_COLOUR = SpiralInfo::SCOPE_BG_COLOUR;
  79. m_Info.SCOPE_FG_COLOUR = SpiralInfo::SCOPE_FG_COLOUR;
  80. m_Info.SCOPE_SEL_COLOUR = SpiralInfo::SCOPE_SEL_COLOUR;
  81. m_Info.SCOPE_IND_COLOUR = SpiralInfo::SCOPE_IND_COLOUR;
  82. m_Info.SCOPE_MRK_COLOUR = SpiralInfo::SCOPE_MRK_COLOUR;
  83. m_Info.GUICOL_Device = SpiralInfo::GUICOL_Device;
  84. m_Info.GUIDEVICE_Box = SpiralInfo::GUIDEVICE_Box;
  85. for (int n=0; n<512; n++) Numbers[n]=n;
  86. m_CH.Register("Frozen",&m_Frozen);
  87. }
  88. //////////////////////////////////////////////////////////
  89. SynthModular::~SynthModular()
  90. {
  91. ClearUp();
  92. PluginManager::Get()->PackUpAndGoHome();
  93. system("rm -f ___temp.ssmcopytmp");
  94. }
  95. //////////////////////////////////////////////////////////
  96. void SynthModular::ClearUp()
  97. {
  98. FreezeAll();
  99. for(map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  100. i!=m_DeviceWinMap.end(); i++)
  101. {
  102. //Stop processing of audio if any
  103. if (i->second->m_Device)
  104. {
  105. if (i->second->m_Device->Kill());
  106. }
  107. i->second->m_DeviceGUI->Clear();
  108. if (i->second->m_DeviceGUI->GetPluginWindow())
  109. {
  110. i->second->m_DeviceGUI->GetPluginWindow()->hide();
  111. }
  112. //Delete Device
  113. delete i->second->m_Device;
  114. i->second->m_Device=NULL;
  115. }
  116. m_Canvas->Clear();
  117. m_DeviceWinMap.clear();
  118. m_NextID=0;
  119. ThawAll();
  120. }
  121. //////////////////////////////////////////////////////////
  122. void SynthModular::Update()
  123. {
  124. m_CH.UpdateDataNow();
  125. if (m_Frozen) return;
  126. // for all the plugins
  127. for(map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  128. i!=m_DeviceWinMap.end(); i++)
  129. {
  130. if (i->second->m_Device && i->second->m_Device->IsDead())
  131. {
  132. //Delete Device
  133. delete i->second->m_Device;
  134. i->second->m_Device=NULL;
  135. //Erase Device from DeviceWinMap
  136. m_DeviceWinMap.erase(i);
  137. }
  138. else if (i->second->m_Device) // if it's not a comment
  139. {
  140. #ifdef DEBUG_PLUGINS
  141. cerr<<"Updating channelhandler of plugin "<<i->second->m_PluginID<<endl;
  142. #endif
  143. // updates the data from the gui thread, if it's not blocking
  144. i->second->m_Device->UpdateChannelHandler();
  145. #ifdef DEBUG_PLUGINS
  146. cerr<<"Finished updating"<<endl;
  147. #endif
  148. // If this is an audio device see if we always need to ProcessAudio here
  149. if ((!m_ResetingAudioThread))
  150. {
  151. if (i->second->m_Device->IsAudioDriver())
  152. {
  153. AudioDriver *driver = ((AudioDriver *)i->second->m_Device);
  154. if (driver->ProcessType() == AudioDriver::ALWAYS)
  155. {
  156. driver->ProcessAudio();
  157. }
  158. }
  159. // run any commands we've received from the GUI's
  160. i->second->m_Device->ExecuteCommands();
  161. }
  162. }
  163. }
  164. // run the plugins (only ones connected to anything)
  165. list<int> ExecutionOrder = m_Canvas->GetGraph()->GetSortedList();
  166. for (list<int>::reverse_iterator i=ExecutionOrder.rbegin();
  167. i!=ExecutionOrder.rend(); i++)
  168. {
  169. // use the graphsort order to remove internal latency
  170. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(*i);
  171. if (di!=m_DeviceWinMap.end() && di->second->m_Device && (! di->second->m_Device->IsDead()) && (!m_Info.PAUSED))
  172. {
  173. #ifdef DEBUG_PLUGINS
  174. cerr<<"Executing plugin "<<di->second->m_PluginID<<endl;
  175. #endif
  176. if (m_ResetingAudioThread)
  177. {
  178. di->second->m_Device->Reset();
  179. }
  180. else
  181. {
  182. di->second->m_Device->Execute();
  183. // If this is an audio device see if we need to ProcessAudio here
  184. if (di->second->m_Device->IsAudioDriver())
  185. {
  186. AudioDriver *driver = ((AudioDriver *)di->second->m_Device);
  187. if (driver->ProcessType() == AudioDriver::MANUAL)
  188. {
  189. driver->ProcessAudio();
  190. }
  191. }
  192. }
  193. #ifdef DEBUG_PLUGINS
  194. cerr<<"Finished executing"<<endl;
  195. #endif
  196. }
  197. }
  198. //we can safely turn this off here
  199. m_ResetingAudioThread = false;
  200. }
  201. //////////////////////////////////////////////////////////
  202. void SynthModular::UpdatePluginGUIs()
  203. {
  204. // see if any need deleting
  205. for (map<int,DeviceWin*>::iterator i=m_DeviceWinMap.begin();
  206. i!=m_DeviceWinMap.end(); i++)
  207. {
  208. if (i->second->m_DeviceGUI && i->second->m_DeviceGUI->GetPluginWindow())
  209. {
  210. SpiralPluginGUI *GUI=(SpiralPluginGUI *)i->second->m_DeviceGUI->GetPluginWindow();
  211. GUI->Update();
  212. }
  213. if (i->second->m_DeviceGUI && i->second->m_DeviceGUI->Killed())
  214. {
  215. bool erase = true;
  216. //Stop processing of audio if any
  217. if (i->second->m_Device)
  218. {
  219. if (i->second->m_Device->Kill());
  220. erase = false;
  221. }
  222. //Clear GUI Device
  223. i->second->m_DeviceGUI->Clear();
  224. // Hide Device GUI FIRST
  225. if (i->second->m_DeviceGUI->GetPluginWindow())
  226. {
  227. i->second->m_DeviceGUI->GetPluginWindow()->hide();
  228. }
  229. //Remove Device GUI from canvas
  230. m_Canvas->RemoveDevice(i->second->m_DeviceGUI);
  231. //Delete Device GUI - must delete here or sometimes plugin will randomly crash
  232. delete i->second->m_DeviceGUI;
  233. i->second->m_DeviceGUI = NULL;
  234. //Erase from winmap if no audio to do it
  235. if (erase)
  236. m_DeviceWinMap.erase(i);
  237. }
  238. }
  239. m_Canvas->Poll();
  240. if (m_HostNeedsUpdate)
  241. {
  242. cout << "Updating SampleRate to: " << SpiralInfo::SAMPLERATE << " and Buffer Size to: " << SpiralInfo::BUFSIZE << " to match current Audio Driver." << endl;
  243. UpdateHostInfo();
  244. m_HostNeedsUpdate = false;
  245. }
  246. }
  247. //////////////////////////////////////////////////////////
  248. SpiralWindowType *SynthModular::CreateWindow()
  249. {
  250. m_TopWindow = new SpiralWindowType(MAIN_WIDTH, MAIN_HEIGHT, LABEL.c_str());
  251. m_TopWindow->user_data((void*)(this));
  252. //m_TopWindow->resizable(m_TopWindow);
  253. m_MainMenu = new Fl_Menu_Bar (0, 0, MAIN_WIDTH, 20, "");
  254. m_MainMenu->user_data((void*)(this));
  255. m_MainMenu->box(FL_PLASTIC_UP_BOX);
  256. m_MainMenu->textsize (10);
  257. m_MainMenu->add ("File/New", 0, cb_New, (void*)(this), FL_MENU_DIVIDER);
  258. m_MainMenu->add ("File/Load", 0, cb_Load, (void*)(this), 0);
  259. m_MainMenu->add ("File/Save As", 0, cb_Save, (void*)(this), 0);
  260. m_MainMenu->add ("File/Merge", 0, cb_Merge, (void*)(this), FL_MENU_DIVIDER);
  261. m_MainMenu->add ("File/Exit", 0, cb_Close, (void*)(this), 0);
  262. m_MainMenu->add ("Edit/Cut", 0, cb_Cut, (void*)(this), 0);
  263. m_MainMenu->add ("Edit/Copy", 0, cb_Copy, (void*)(this), 0);
  264. m_MainMenu->add ("Edit/Paste", 0, cb_Paste, (void*)(this), 0);
  265. m_MainMenu->add ("Edit/Delete", 0, cb_Delete, (void*)(this), FL_MENU_DIVIDER);
  266. //m_MainMenu->add ("Edit/Toolbars/Plugins", 0, cb_Undefined, (void*)(this), 0);
  267. //m_MainMenu->add ("Edit/Toolbars/Function", 0, cb_Undefined, (void*)(this), 0);
  268. m_MainMenu->add ("Edit/Options", 0, cb_Options, (void*)(this), 0);
  269. m_MainMenu->add ("Plugins/dummy", 0, NULL, NULL, 0);
  270. m_MainMenu->add ("Audio/Pause", 0, cb_PlayPause, NULL, 0);
  271. m_MainMenu->add ("Audio/Reset", 0, cb_Reset, NULL, 0);
  272. //m_MainMenu->add ("Help/Plugins/dummy", 0, NULL, NULL, 0);
  273. //m_MainMenu->add ("Help/Credits", 0, NULL, (void*)(this), 0);
  274. //m_MainMenu->add ("Help/About", 0, NULL, (void*)(this), 0);
  275. m_TopWindow->add (m_MainMenu);
  276. int but = 50;
  277. int ToolbarHeight = but + 0;
  278. m_Topbar = new Fl_Pack (0, 20, MAIN_WIDTH, ToolbarHeight, "");
  279. m_Topbar->user_data((void*)(this));
  280. m_Topbar->type(FL_HORIZONTAL);
  281. m_Topbar->color(SpiralInfo::GUICOL_Button);
  282. m_TopWindow->add(m_Topbar);
  283. m_ToolbarPanel = new Fl_Pack (0, 20, but*6, ToolbarHeight, "");
  284. m_ToolbarPanel->user_data((void*)(this));
  285. m_ToolbarPanel->type(FL_VERTICAL);
  286. m_ToolbarPanel->color(SpiralInfo::GUICOL_Button);
  287. m_Topbar->add(m_ToolbarPanel);
  288. m_Toolbar = new Fl_Pack (0, 20, but*6, but, "");
  289. m_Toolbar->user_data((void*)(this));
  290. m_Toolbar->type(FL_HORIZONTAL);
  291. m_Toolbar->color(SpiralInfo::GUICOL_Button);
  292. m_ToolbarPanel->add(m_Toolbar);
  293. m_Load = new Fl_Button (0, 0, but, but, "");
  294. m_Load->user_data ((void*)(this));
  295. Fl_Pixmap *tPix = new Fl_Pixmap(load_xpm);
  296. m_Load->image(tPix->copy());
  297. delete tPix;
  298. m_Load->type(0);
  299. m_Load->box(FL_PLASTIC_UP_BOX);
  300. m_Load->color(SpiralInfo::GUICOL_Button);
  301. m_Load->selection_color(SpiralInfo::GUICOL_Tool);
  302. m_Load->labelsize (1);
  303. m_Load->tooltip("Load a patch file");
  304. m_Load->callback((Fl_Callback*)cb_Load);
  305. m_Toolbar->add(m_Load);
  306. m_Save = new Fl_Button(0, 0, but, but, "");
  307. m_Save->user_data ((void*)(this));
  308. tPix = new Fl_Pixmap(save_xpm);
  309. m_Save->image(tPix->copy());
  310. delete tPix;
  311. m_Save->type(0);
  312. m_Save->box(FL_PLASTIC_UP_BOX);
  313. m_Save->color(SpiralInfo::GUICOL_Button);
  314. m_Save->selection_color(SpiralInfo::GUICOL_Tool);
  315. m_Save->labelsize (1);
  316. m_Save->tooltip("Save a patch file");
  317. m_Save->callback((Fl_Callback*)cb_Save);
  318. m_Toolbar->add(m_Save);
  319. m_New = new Fl_Button(0, 0, but, but, "");
  320. m_New->user_data ((void*)(this));
  321. tPix = new Fl_Pixmap(new_xpm);
  322. m_New->image(tPix->copy());
  323. delete tPix;
  324. m_New->type(0);
  325. m_New->box(FL_PLASTIC_UP_BOX);
  326. m_New->color(SpiralInfo::GUICOL_Button);
  327. m_New->selection_color(SpiralInfo::GUICOL_Tool);
  328. m_New->labelsize (1);
  329. m_New->tooltip("New patch");
  330. m_New->callback((Fl_Callback*)cb_New);
  331. m_Toolbar->add(m_New);
  332. m_Options = new Fl_Button(0, 0, but, but, "");
  333. m_Options->user_data ((void*)(this));
  334. tPix = new Fl_Pixmap(options_xpm);
  335. m_Options->image(tPix->copy());
  336. delete tPix;
  337. m_Options->type(0);
  338. m_Options->box(FL_PLASTIC_UP_BOX);
  339. m_Options->color(SpiralInfo::GUICOL_Button);
  340. m_Options->selection_color(SpiralInfo::GUICOL_Tool);
  341. m_Options->labelsize (1);
  342. m_Options->tooltip("Options");
  343. m_Options->callback((Fl_Callback*)cb_Options);
  344. m_Toolbar->add(m_Options);
  345. m_NewComment = new Fl_Button(0, 0, but, but, "");
  346. tPix = new Fl_Pixmap(comment_xpm);
  347. m_NewComment->image(tPix->copy());
  348. delete tPix;
  349. m_NewComment->type(0);
  350. m_NewComment->box(FL_PLASTIC_UP_BOX);
  351. m_NewComment->color(SpiralInfo::GUICOL_Button);
  352. m_NewComment->selection_color(SpiralInfo::GUICOL_Tool);
  353. m_NewComment->labelsize (1);
  354. m_NewComment->tooltip("New comment");
  355. m_NewComment->callback((Fl_Callback*)cb_NewComment);
  356. m_Toolbar->add(m_NewComment);
  357. m_PlayResetGroup = new Fl_Pack (0, 0, but, but, "");
  358. m_PlayResetGroup->color(SpiralInfo::GUICOL_Button);
  359. m_Toolbar->add(m_PlayResetGroup);
  360. m_PlayPause = new Fl_Button(0, 0, but, but/2, "@||");
  361. m_PlayPause->user_data((void*)(this));
  362. m_PlayPause->type(0);
  363. m_PlayPause->box(FL_PLASTIC_UP_BOX);
  364. m_PlayPause->color(SpiralInfo::GUICOL_Button);
  365. m_PlayPause->selection_color(SpiralInfo::GUICOL_Tool);
  366. m_PlayPause->labelsize (10);
  367. m_PlayPause->tooltip("Pause");
  368. m_PlayPause->callback((Fl_Callback*)cb_PlayPause);
  369. m_PlayResetGroup->add(m_PlayPause);
  370. m_Reset = new Fl_Button(0, 0, but, but/2, "Reset");
  371. m_Reset->box(FL_PLASTIC_UP_BOX);
  372. m_Reset->color(SpiralInfo::GUICOL_Button);
  373. m_Reset->user_data((void*)(this));
  374. m_Reset->selection_color(SpiralInfo::GUICOL_Tool);
  375. m_Reset->labelsize (10);
  376. m_Reset->tooltip("Reset Audio State of all Plugins");
  377. m_Reset->callback((Fl_Callback*)cb_Reset);
  378. m_PlayResetGroup->add(m_Reset);
  379. m_GroupFiller = new Fl_Group (0, 0, 0, ToolbarHeight, "");
  380. m_GroupFiller->color(SpiralInfo::GUICOL_Button);
  381. m_Topbar->add (m_GroupFiller);
  382. m_GroupTab = new Fl_Tabs (0, 0, MAIN_WIDTH-m_GroupFiller->w()-but*6, ToolbarHeight, "");
  383. m_GroupTab->user_data ((void*)(this));
  384. m_GroupTab->box(FL_PLASTIC_DOWN_BOX);
  385. m_GroupTab->color(SpiralInfo::GUICOL_Button);
  386. m_GroupTab->callback((Fl_Callback*)cb_GroupTab);
  387. m_Topbar->add (m_GroupTab);
  388. m_Topbar->resizable(m_GroupTab);
  389. /////////////////
  390. ToolbarHeight += 20; // Stretch this a bit to allow room for the menu-bar too.
  391. m_CanvasScroll = new Fl_Scroll (0, ToolbarHeight, MAIN_WIDTH, MAIN_HEIGHT-ToolbarHeight, "");
  392. m_TopWindow->add(m_CanvasScroll);
  393. m_TopWindow->resizable(m_CanvasScroll);
  394. m_Canvas = new Fl_Canvas(-5000, -5000, 10000, 10000, "");
  395. m_Canvas->user_data ((void*)(this));
  396. m_Canvas->type(1);
  397. m_Canvas->box(FL_FLAT_BOX);
  398. m_Canvas->labeltype(FL_ENGRAVED_LABEL);
  399. m_Canvas->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  400. m_Canvas->color(SpiralInfo::GUICOL_Canvas);
  401. m_Canvas->user_data((void*)(this));
  402. m_Canvas->SetConnectionCallback((Fl_Callback*)cb_Connection);
  403. m_Canvas->SetUnconnectCallback((Fl_Callback*)cb_Unconnect);
  404. m_Canvas->SetAddDeviceCallback((Fl_Callback*)cb_NewDeviceFromCanvasMenu);
  405. m_Canvas->SetCutDeviceGroupCallback((Fl_Callback*)cb_Cut);
  406. m_Canvas->SetCopyDeviceGroupCallback((Fl_Callback*)cb_Copy);
  407. m_Canvas->SetPasteDeviceGroupCallback((Fl_Callback*)cb_Paste);
  408. m_Canvas->SetMergePatchCallback((Fl_Callback*)cb_Merge);
  409. m_CanvasScroll->add(m_Canvas);
  410. m_SettingsWindow = new SettingsWindow;
  411. m_SettingsWindow->RegisterApp(this);
  412. return m_TopWindow;
  413. }
  414. //////////////////////////////////////////////////////////
  415. vector<string> SynthModular::BuildPluginList (const string &Path) {
  416. // Scan plugin path for plugins.
  417. DIR *dp;
  418. struct dirent *ep;
  419. struct stat sb;
  420. void *handle;
  421. string fullpath;
  422. const char *path = Path.c_str();
  423. vector<string> ret;
  424. dp = opendir(path);
  425. if (!dp) {
  426. cerr << "WARNING: Could not open path " << path << endl;
  427. }
  428. else {
  429. while ((ep = readdir(dp))) {
  430. // Need full path
  431. fullpath = path;
  432. fullpath.append(ep->d_name);
  433. // Stat file to get type
  434. if (!stat(fullpath.c_str(), &sb)) {
  435. // We only want regular files
  436. if (S_ISREG(sb.st_mode)) {
  437. // We're not fussed about resolving symbols yet, since we are just
  438. // checking if it's a DLL.
  439. handle = dlopen(fullpath.c_str(), RTLD_LAZY);
  440. if (!handle) {
  441. cerr << "WARNING: File " << path << ep->d_name
  442. << " could not be examined" << endl;
  443. cerr << "dlerror() output:" << endl;
  444. cerr << dlerror() << endl;
  445. }
  446. else {
  447. // It's a DLL. Add name to list
  448. ret.push_back(ep->d_name);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. return ret;
  455. }
  456. void SynthModular::LoadPlugins (string pluginPath) {
  457. int Width = 35;
  458. int Height = 35;
  459. int SWidth = 256;
  460. int SHeight = 256;
  461. Fl_Pixmap pic (SSM_xpm);
  462. Fl_Double_Window* Splash = new Fl_Double_Window ((Fl::w()/2) - (SWidth/2), (Fl::h()/2) - (SHeight/2),
  463. SWidth, SHeight, "SSM");
  464. Splash->border(0);
  465. Fl_Box* pbut = new Fl_Box (0, 8, SWidth, SHeight, "");
  466. pbut->box (FL_NO_BOX);
  467. pic.label (pbut);
  468. Fl_Box *splashtext = new Fl_Box (5, SHeight-20, 200, 20, "Loading...");
  469. splashtext->labelsize (10);
  470. splashtext->box (FL_NO_BOX);
  471. splashtext->align (FL_ALIGN_INSIDE | FL_ALIGN_LEFT);
  472. Splash->add (pbut);
  473. Splash->add (splashtext);
  474. Splash->show();
  475. int ID=-1;
  476. vector<string> PluginVector;
  477. if (SpiralInfo::USEPLUGINLIST) PluginVector = SpiralInfo::PLUGINVEC;
  478. else {
  479. if (pluginPath.empty()) PluginVector = BuildPluginList (SpiralInfo::PLUGIN_PATH);
  480. else {
  481. string::iterator i = pluginPath.end() - 1;
  482. if (*i != '/') pluginPath += '/';
  483. PluginVector = BuildPluginList (pluginPath);
  484. }
  485. }
  486. for (vector<string>::iterator i=PluginVector.begin(); i!=PluginVector.end(); i++) {
  487. string Fullpath;
  488. if (pluginPath=="") Fullpath=SpiralInfo::PLUGIN_PATH+*i;
  489. else Fullpath = pluginPath + *"/" + *i;
  490. ID = PluginManager::Get()->LoadPlugin (Fullpath.c_str());
  491. if (ID!=PluginError) {
  492. #ifdef DEBUG_PLUGINS
  493. cerr << ID << " = Plugin [" << *i << "]" << endl;
  494. #endif
  495. Fl_ToolButton *NewButton = new Fl_ToolButton (0, 0, Width, Height, "");
  496. // we can't set user data, because the callback uses it
  497. // NewButton->user_data ((void*)(this));
  498. NewButton->labelsize (1);
  499. Fl_Pixmap *tPix = new Fl_Pixmap (PluginManager::Get()->GetPlugin(ID)->GetIcon());
  500. NewButton->image(tPix->copy(tPix->w(),tPix->h()));
  501. delete tPix;
  502. string GroupName = PluginManager::Get()->GetPlugin(ID)->GetGroupName();
  503. Fl_Pack* the_group=NULL;
  504. // find or create this group, and add an icon
  505. map<string,Fl_Pack*>::iterator gi = m_PluginGroupMap.find (GroupName);
  506. if (gi == m_PluginGroupMap.end()) {
  507. the_group = new Fl_Pack (m_GroupTab->x(), 16, m_GroupTab->w(), m_GroupTab->h() - 15, GroupName.c_str());
  508. the_group->type(FL_HORIZONTAL);
  509. the_group->labelsize(8);
  510. the_group->color(SpiralInfo::GUICOL_Button);
  511. the_group->user_data((void*)(this));
  512. //m_GroupTab->add(the_group);
  513. m_GroupTab->value(the_group);
  514. m_PluginGroupMap[GroupName]=the_group;
  515. }
  516. else the_group = gi->second;
  517. NewButton->type (0);
  518. NewButton->box (FL_NO_BOX);
  519. NewButton->down_box (FL_NO_BOX);
  520. //NewButton->color(SpiralInfo::GUICOL_Button);
  521. //NewButton->selection_color(SpiralInfo::GUICOL_Button);
  522. the_group->add (NewButton);
  523. string PluginName=*i;
  524. // find the first slash, if there is one, and get rid of everything before and including it
  525. unsigned int p = PluginName.find ('/');
  526. if (p < PluginName.length()) PluginName.erase (0, p);
  527. // find last . and get rid of everything after and including it
  528. p = PluginName.rfind ('.');
  529. unsigned int l = PluginName.length ();
  530. if (p < l) PluginName.erase (p, l);
  531. NewButton->tooltip (PluginName.c_str());
  532. // Slashes have significance to the menu widgets, remove them from the GroupName
  533. while ((p = GroupName.find ('/')) < PluginName.length())
  534. GroupName = GroupName.replace (p, 1, " and ");
  535. string MenuEntry = "Plugins/" + GroupName + "/" + PluginName;
  536. m_MainMenu->add (MenuEntry.c_str(), 0, cb_NewDeviceFromMenu, &Numbers[ID], 0);
  537. //MenuEntry = "Help/" + MenuEntry;
  538. //m_MainMenu->add (MenuEntry.c_str(), 0, NULL, &Numbers[ID], 0);
  539. // andy preston
  540. // my next step would be to add the plugins to Andrew's right-click menu
  541. // to free up the middle button
  542. m_Canvas->AddPluginName (PluginName, PluginManager::Get()->GetPlugin(ID)->ID);
  543. // this overwrites the widget's user_data with that specified for the callback
  544. // so we can't use it for other purposes
  545. NewButton->callback ((Fl_Callback*)cb_NewDevice, &Numbers[ID]);
  546. NewButton->show();
  547. m_DeviceVec.push_back (NewButton);
  548. the_group->redraw();
  549. // m_NextPluginButton++;
  550. Fl::check();
  551. splashtext->label (PluginName.c_str());
  552. Splash->redraw();
  553. }
  554. }
  555. map<string,Fl_Pack*>::iterator PlugGrp;
  556. for (PlugGrp = m_PluginGroupMap.begin(); PlugGrp!= m_PluginGroupMap.end(); ++PlugGrp) {
  557. m_GroupTab->add (PlugGrp->second);
  558. PlugGrp->second->add (new Fl_Box (0, 0, 600, 100, ""));
  559. }
  560. // try to show the SpiralSound group
  561. PlugGrp = m_PluginGroupMap.find("SpiralSound");
  562. // can't find it - show the first plugin group
  563. if (PlugGrp==m_PluginGroupMap.end()) PlugGrp=m_PluginGroupMap.begin();
  564. m_GroupTab->value(PlugGrp->second);
  565. bool found_dummy;
  566. int i;
  567. do {
  568. found_dummy = false;
  569. for (i=0; i<m_MainMenu->size(); i++) {
  570. if (m_MainMenu->text (i) != NULL) {
  571. found_dummy = (strcmp ("dummy", m_MainMenu->text (i)) == 0);
  572. if (found_dummy) break;
  573. }
  574. }
  575. if (found_dummy) m_MainMenu->remove (i);
  576. } while (found_dummy);
  577. Splash->hide();
  578. delete Splash;
  579. }
  580. //////////////////////////////////////////////////////////
  581. DeviceGUIInfo SynthModular::BuildDeviceGUIInfo(PluginInfo &PInfo)
  582. {
  583. DeviceGUIInfo Info;
  584. int Height=50;
  585. // tweak the size if we have too many ins/outs
  586. if (PInfo.NumInputs>4 || PInfo.NumOutputs>4)
  587. {
  588. if (PInfo.NumInputs<PInfo.NumOutputs)
  589. {
  590. Height=PInfo.NumOutputs*10+5;
  591. }
  592. else
  593. {
  594. Height=PInfo.NumInputs*10+5;
  595. }
  596. }
  597. // Make the guiinfo struct
  598. Info.XPos = 0;
  599. Info.YPos = 0;
  600. Info.Width = 40;
  601. Info.Height = Height;
  602. Info.NumInputs = PInfo.NumInputs;
  603. Info.NumOutputs = PInfo.NumOutputs;
  604. Info.Name = PInfo.Name;
  605. Info.PortTips = PInfo.PortTips;
  606. Info.PortTypes = PInfo.PortTypes;
  607. return Info;
  608. }
  609. //////////////////////////////////////////////////////////
  610. DeviceWin* SynthModular::NewDeviceWin(int n, int x, int y)
  611. {
  612. DeviceWin *nlw = new DeviceWin;
  613. const HostsideInfo* Plugin=PluginManager::Get()->GetPlugin(n);
  614. if (!Plugin) return NULL;
  615. nlw->m_Device=Plugin->CreateInstance();
  616. if (!nlw->m_Device) return NULL;
  617. nlw->m_Device->SetBlockingCallback(cb_Blocking);
  618. nlw->m_Device->SetUpdateCallback(cb_Update);
  619. nlw->m_Device->SetParent((void*)this);
  620. if ( nlw->m_Device->IsAudioDriver() )
  621. {
  622. AudioDriver *driver = ((AudioDriver*)nlw->m_Device);
  623. driver->SetChangeBufferAndSampleRateCallback(cb_ChangeBufferAndSampleRate);
  624. }
  625. PluginInfo PInfo = nlw->m_Device->Initialise(&m_Info);
  626. SpiralGUIType *temp = nlw->m_Device->CreateGUI();
  627. Fl_Pixmap *Pix = new Fl_Pixmap(Plugin->GetIcon());
  628. nlw->m_PluginID = n;
  629. if (temp) temp->position(x+10,y);
  630. DeviceGUIInfo Info=BuildDeviceGUIInfo(PInfo);
  631. Info.XPos = x; //TOOLBOX_WIDTH+(rand()%400);
  632. Info.YPos = y; //rand()%400;
  633. nlw->m_DeviceGUI = new Fl_DeviceGUI(Info, temp, Pix, nlw->m_Device->IsTerminal());
  634. Fl_Canvas::SetDeviceCallbacks(nlw->m_DeviceGUI, m_Canvas);
  635. m_Canvas->add(nlw->m_DeviceGUI);
  636. m_Canvas->redraw();
  637. return nlw;
  638. }
  639. //////////////////////////////////////////////////////////
  640. void SynthModular::AddDevice(int n, int x=-1, int y=-1)
  641. {
  642. //cerr<<"Adding "<<m_NextID<<endl;
  643. if (x==-1)
  644. {
  645. x = m_CanvasScroll->x()+50;
  646. y = m_CanvasScroll->y()+50;
  647. }
  648. DeviceWin* temp = NewDeviceWin(n,x,y);
  649. if (temp)
  650. {
  651. int ID=m_NextID++;
  652. //cerr<<"adding device "<<ID<<endl;
  653. temp->m_DeviceGUI->SetID(ID);
  654. temp->m_Device->SetUpdateInfoCallback(ID,cb_UpdatePluginInfo);
  655. m_DeviceWinMap[ID]=temp;
  656. }
  657. }
  658. //////////////////////////////////////////////////////////
  659. DeviceWin* SynthModular::NewComment(int n, int x=-1, int y=-1)
  660. {
  661. DeviceWin *nlw = new DeviceWin;
  662. if (x==-1)
  663. {
  664. x = m_CanvasScroll->x()+50;
  665. y = m_CanvasScroll->y()+50;
  666. }
  667. nlw->m_Device=NULL;
  668. nlw->m_PluginID = COMMENT_ID;
  669. DeviceGUIInfo Info;
  670. Info.XPos = x;
  671. Info.YPos = y;
  672. Info.Width = 50;
  673. Info.Height = 20;
  674. Info.NumInputs = 0;
  675. Info.NumOutputs = 0;
  676. Info.Name = "";
  677. nlw->m_DeviceGUI = new Fl_CommentGUI(Info, NULL, NULL);
  678. Fl_Canvas::SetDeviceCallbacks(nlw->m_DeviceGUI, m_Canvas);
  679. m_Canvas->add(nlw->m_DeviceGUI);
  680. m_Canvas->redraw();
  681. return nlw;
  682. }
  683. //////////////////////////////////////////////////////////
  684. void SynthModular::AddComment(int n)
  685. {
  686. //cerr<<"Adding "<<m_NextID<<endl;
  687. DeviceWin* temp = NewComment(n);
  688. if (temp)
  689. {
  690. int ID=m_NextID++;
  691. //cerr<<"adding comment "<<ID<<endl;
  692. temp->m_DeviceGUI->SetID(ID);
  693. m_DeviceWinMap[ID]=temp;
  694. }
  695. }
  696. //////////////////////////////////////////////////////////
  697. void SynthModular::cb_ChangeBufferAndSampleRate_i(long int NewBufferSize, long int NewSamplerate)
  698. {
  699. if (SpiralInfo::BUFSIZE != NewBufferSize)
  700. {
  701. // update the settings
  702. SpiralInfo::BUFSIZE = NewBufferSize;
  703. m_HostNeedsUpdate = true;
  704. }
  705. if (SpiralInfo::SAMPLERATE != NewSamplerate)
  706. {
  707. SpiralInfo::SAMPLERATE = NewSamplerate;
  708. m_HostNeedsUpdate = true;
  709. }
  710. }
  711. void SynthModular::UpdateHostInfo()
  712. {
  713. /* Pause Audio */
  714. FreezeAll();
  715. /* update the settings */
  716. m_Info.BUFSIZE = SpiralInfo::BUFSIZE;
  717. m_Info.SAMPLERATE = SpiralInfo::SAMPLERATE;
  718. /* obsolete - REMOVE SOON */
  719. m_Info.FRAGSIZE = SpiralInfo::FRAGSIZE;
  720. m_Info.FRAGCOUNT = SpiralInfo::FRAGCOUNT;
  721. m_Info.OUTPUTFILE = SpiralInfo::OUTPUTFILE;
  722. m_Info.MIDIFILE = SpiralInfo::MIDIFILE;
  723. m_Info.POLY = SpiralInfo::POLY;
  724. /* Reset all plugin ports/buffers befure Resuming */
  725. ResetAudio();
  726. }
  727. //////////////////////////////////////////////////////////
  728. // called when a callback output plugin wants to run the audio thread
  729. void SynthModular::cb_Update(void* o, bool mode)
  730. {
  731. m_CallbackUpdateMode=mode;
  732. ((SynthModular*)o)->Update();
  733. }
  734. // called by a blocking output plugin to notify the engine its ready to
  735. // take control of the update timing (so take the brakes off)
  736. void SynthModular::cb_Blocking(void* o, bool mode)
  737. {
  738. m_BlockingOutputPluginIsReady=mode;
  739. }
  740. //////////////////////////////////////////////////////////
  741. iostream &SynthModular::StreamPatchIn(iostream &s, bool paste, bool merge)
  742. {
  743. //if we are merging as opposed to loading a new patch
  744. //we have no need to pause audio
  745. if (!merge && !paste)
  746. FreezeAll();
  747. //if we are pasting we don't have any of the file version
  748. //or saving information. since its internal we didn't
  749. //need it, but we do have other things we might need to load
  750. bool has_file_path;
  751. char file_path[1024];
  752. string m_FromFilePath;
  753. string dummy,dummy2;
  754. int ver;
  755. if (paste)
  756. {
  757. m_Copied.devices>>has_file_path;
  758. if (has_file_path)
  759. {
  760. m_Copied.devices.getline(file_path, 1024);
  761. m_FromFilePath = file_path;
  762. cerr << file_path << endl;
  763. }
  764. }
  765. else
  766. {
  767. s>>dummy>>dummy>>dummy>>ver;
  768. if (ver>FILE_VERSION)
  769. {
  770. SpiralInfo::Alert("Bad file, or more recent version.");
  771. ThawAll();
  772. return s;
  773. }
  774. if (ver>2)
  775. {
  776. int MainWinX,MainWinY,MainWinW,MainWinH;
  777. int EditWinX,EditWinY,EditWinW,EditWinH;
  778. s>>MainWinX>>MainWinY>>MainWinW>>MainWinH;
  779. s>>EditWinX>>EditWinY>>EditWinW>>EditWinH;
  780. //o.m_MainWindow->resize(MainWinX,MainWinY,MainWinW,MainWinH);
  781. //o.m_EditorWindow->resize(EditWinX,EditWinY,EditWinW,EditWinH);
  782. }
  783. if (merge)
  784. m_FromFilePath = m_MergeFilePath;
  785. }
  786. //wether pasting or merging we need to clear the current
  787. //selection so we can replace it with the new devices
  788. if (paste || merge)
  789. Fl_Canvas::ClearSelection(m_Canvas);
  790. int Num, ID, PluginID, x,y,ps,px,py;
  791. if (paste)
  792. {
  793. Num = m_Copied.devicecount;
  794. }
  795. else
  796. {
  797. s>>dummy>>Num;
  798. }
  799. for(int n=0; n<Num; n++)
  800. {
  801. #ifdef DEBUG_STREAM
  802. cerr<<"Loading Device "<<n<<endl;
  803. #endif
  804. s>>dummy; // "Device"
  805. s>>ID;
  806. s>>dummy2; // "Plugin"
  807. s>>PluginID;
  808. s>>x>>y;
  809. string Name;
  810. if (paste || ver>3)
  811. {
  812. // load the device name
  813. int size;
  814. char Buf[1024];
  815. s>>size;
  816. s.ignore(1);
  817. if (size > 0) {
  818. s.get(Buf,size+1);
  819. Name=Buf;
  820. } else {
  821. Name = "";
  822. }
  823. }
  824. #ifdef DEBUG_STREAM
  825. cerr<<dummy<<" "<<ID<<" "<<dummy2<<" "<<PluginID<<" "<<x<<" "<<y<<endl;
  826. #endif
  827. if (paste || ver>1) s>>ps>>px>>py;
  828. //if we are merging a patch or pasting we will change duplicate ID's
  829. if (!paste && !merge)
  830. {
  831. // Check we're not duplicating an ID
  832. if (m_DeviceWinMap.find(ID)!=m_DeviceWinMap.end())
  833. {
  834. SpiralInfo::Alert("Duplicate device ID found in file - aborting load");
  835. ThawAll();
  836. return s;
  837. }
  838. }
  839. if (PluginID==COMMENT_ID)
  840. {
  841. DeviceWin* temp = NewComment(PluginID, x, y);
  842. if (temp)
  843. {
  844. if (paste || merge)
  845. {
  846. m_Copied.m_DeviceIds[ID] = m_NextID++;
  847. ID = m_Copied.m_DeviceIds[ID];
  848. }
  849. temp->m_DeviceGUI->SetID(ID);
  850. m_DeviceWinMap[ID]=temp;
  851. ((Fl_CommentGUI*)(m_DeviceWinMap[ID]->m_DeviceGUI))->StreamIn(s); // load the plugin
  852. if (paste || merge)
  853. Fl_Canvas::AppendSelection(ID, m_Canvas);
  854. else
  855. if (m_NextID<=ID) m_NextID=ID+1;
  856. }
  857. }
  858. else
  859. {
  860. DeviceWin* temp = NewDeviceWin(PluginID, x, y);
  861. if (temp)
  862. {
  863. int oldID=ID;
  864. if (paste || merge)
  865. {
  866. m_Copied.m_DeviceIds[ID] = m_NextID++;
  867. ID = m_Copied.m_DeviceIds[ID];
  868. }
  869. temp->m_DeviceGUI->SetID(ID);
  870. if (paste || ver>3)
  871. {
  872. // set the titlebars
  873. temp->m_DeviceGUI->SetName(Name);
  874. }
  875. temp->m_Device->SetUpdateInfoCallback(ID,cb_UpdatePluginInfo);
  876. m_DeviceWinMap[ID]=temp;
  877. m_DeviceWinMap[ID]->m_Device->StreamIn(s); // load the plugin
  878. // load external files
  879. if (paste || merge)
  880. m_DeviceWinMap[ID]->m_Device->LoadExternalFiles(m_FromFilePath+"_files/", oldID);
  881. else
  882. m_DeviceWinMap[ID]->m_Device->LoadExternalFiles(m_FilePath+"_files/");
  883. if ((paste || ver>1) && m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow())
  884. {
  885. // set the GUI up with the loaded values
  886. // looks messy, but if we do it here, the plugin and it's gui can remain
  887. // totally seperated.
  888. ((SpiralPluginGUI*)(m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()))->
  889. UpdateValues(m_DeviceWinMap[ID]->m_Device);
  890. // updates the data in the channel buffers, so the values don't
  891. // get overwritten in the next tick. (should maybe be somewhere else)
  892. m_DeviceWinMap[ID]->m_Device->GetChannelHandler()->FlushChannels();
  893. // position the plugin window in the main window
  894. //m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()->position(px,py);
  895. if (ps)
  896. {
  897. m_DeviceWinMap[ID]->m_DeviceGUI->Maximise();
  898. // reposition after maximise
  899. m_DeviceWinMap[ID]->m_DeviceGUI->position(x,y);
  900. }
  901. else m_DeviceWinMap[ID]->m_DeviceGUI->Minimise();
  902. if (paste || merge)
  903. Fl_Canvas::AppendSelection(ID, m_Canvas);
  904. }
  905. if (!paste && !merge)
  906. if (m_NextID<=ID) m_NextID=ID+1;
  907. }
  908. else
  909. {
  910. // can't really recover if the plugin ID doesn't match a plugin, as
  911. // we have no idea how much data in the stream belongs to this plugin
  912. SpiralInfo::Alert("Error in stream, can't really recover data from here on.");
  913. return s;
  914. }
  915. }
  916. }
  917. if (!paste && !merge)
  918. {
  919. s>>*m_Canvas;
  920. ThawAll();
  921. }
  922. return s;
  923. }
  924. iostream &operator>>(iostream &s, SynthModular &o)
  925. {
  926. return o.StreamPatchIn(s, false, false);
  927. }
  928. //////////////////////////////////////////////////////////
  929. ostream &operator<<(ostream &s, SynthModular &o)
  930. {
  931. o.FreezeAll();
  932. s<<"SpiralSynthModular File Ver "<<FILE_VERSION<<endl;
  933. // make external files dir
  934. bool ExternalDirUsed=false;
  935. string command("mkdir '"+o.m_FilePath+"_files'");
  936. system(command.c_str());
  937. if (FILE_VERSION>2)
  938. {
  939. s<<o.m_TopWindow->x()<<" "<<o.m_TopWindow->y()<<" ";
  940. s<<o.m_TopWindow->w()<<" "<<o.m_TopWindow->h()<<" ";
  941. s<<0<<" "<<0<<" ";
  942. s<<0<<" "<<0<<endl;
  943. }
  944. // save out the SynthModular
  945. s<<"SectionList"<<endl;
  946. s<<o.m_DeviceWinMap.size()<<endl;
  947. for(map<int,DeviceWin*>::iterator i=o.m_DeviceWinMap.begin();
  948. i!=o.m_DeviceWinMap.end(); i++)
  949. {
  950. if (i->second->m_DeviceGUI && ((i->second->m_Device) || (i->second->m_PluginID==COMMENT_ID)))
  951. {
  952. s<<endl;
  953. s<<"Device ";
  954. s<<i->first<<" "; // save the id
  955. s<<"Plugin ";
  956. s<<i->second->m_PluginID<<endl;
  957. s<<i->second->m_DeviceGUI->x()<<" ";
  958. s<<i->second->m_DeviceGUI->y()<<" ";
  959. s<<i->second->m_DeviceGUI->GetName().size()<<" ";
  960. s<<i->second->m_DeviceGUI->GetName()<<" ";
  961. if (i->second->m_DeviceGUI->GetPluginWindow())
  962. {
  963. s<<i->second->m_DeviceGUI->GetPluginWindow()->visible()<<" ";
  964. s<<i->second->m_DeviceGUI->GetPluginWindow()->x()<<" ";
  965. s<<i->second->m_DeviceGUI->GetPluginWindow()->y()<<" ";
  966. }
  967. else
  968. {
  969. s<<0<<" "<<0<<" "<<0;
  970. }
  971. s<<endl;
  972. if (i->second->m_PluginID==COMMENT_ID)
  973. {
  974. // save the comment gui
  975. ((Fl_CommentGUI*)(i->second->m_DeviceGUI))->StreamOut(s);
  976. }
  977. else
  978. {
  979. // save the plugin
  980. i->second->m_Device->StreamOut(s);
  981. }
  982. s<<endl;
  983. // save external files
  984. if (i->second->m_Device && i->second->m_Device->SaveExternalFiles(o.m_FilePath+"_files/"))
  985. {
  986. ExternalDirUsed=true;
  987. }
  988. }
  989. }
  990. s<<endl<<*o.m_Canvas<<endl;
  991. // remove it if it wasn't used
  992. if (!ExternalDirUsed)
  993. {
  994. // i guess rmdir won't work if there is something in the dir
  995. // anyway, but best to be on the safe side. (could do rm -rf) :)
  996. string command("rmdir "+o.m_FilePath+"_files");
  997. system(command.c_str());
  998. }
  999. o.ThawAll();
  1000. return s;
  1001. }
  1002. //////////////////////////////////////////////////////////////////////////////////////////
  1003. // Callbacks
  1004. /////////////////////////////////
  1005. // File Menu & associated buttons
  1006. // New
  1007. inline void SynthModular::cb_New_i (Fl_Widget *o, void *v) {
  1008. if (m_DeviceWinMap.size()>0 && !Pawfal_YesNo ("New - Lose changes to current patch?"))
  1009. return;
  1010. m_TopWindow->label (TITLEBAR.c_str());
  1011. ClearUp();
  1012. }
  1013. void SynthModular::cb_New (Fl_Widget *o, void *v) {
  1014. ((SynthModular*)(o->user_data()))->cb_New_i (o, v);
  1015. }
  1016. // Load
  1017. inline void SynthModular::cb_Load_i (Fl_Widget *o, void *v) {
  1018. if (m_DeviceWinMap.size()>0 && !Pawfal_YesNo ("Load - Lose changes to current patch?"))
  1019. return;
  1020. char *fn=fl_file_chooser ("Load a patch", "*.ssm", NULL);
  1021. if (fn && fn!='\0') {
  1022. ifstream in (fn);
  1023. if (in) {
  1024. fstream inf;
  1025. inf.open (fn, ios::in);
  1026. m_FilePath = fn;
  1027. ClearUp();
  1028. inf >> *this;
  1029. inf.close();
  1030. TITLEBAR = LABEL + " " + fn;
  1031. m_TopWindow->label (TITLEBAR.c_str());
  1032. }
  1033. }
  1034. }
  1035. void SynthModular::cb_Load(Fl_Widget *o, void *v) {
  1036. ((SynthModular*)(o->user_data()))->cb_Load_i (o, v);
  1037. }
  1038. // Save
  1039. inline void SynthModular::cb_Save_i (Fl_Widget *o, void *v) {
  1040. char *fn=fl_file_chooser("Save a patch", "*.ssm", NULL);
  1041. if (fn && fn!='\0') {
  1042. ifstream ifl (fn);
  1043. if (ifl) {
  1044. if (!Pawfal_YesNo ("File [%s] exists, overwrite?", fn))
  1045. return;
  1046. }
  1047. ofstream of (fn);
  1048. if (of) {
  1049. m_FilePath = fn;
  1050. of << *this;
  1051. TITLEBAR = LABEL + " " + fn;
  1052. m_TopWindow->label (TITLEBAR.c_str());
  1053. }
  1054. else {
  1055. fl_message (string ("Error saving " + string(fn)).c_str());
  1056. }
  1057. }
  1058. }
  1059. void SynthModular::cb_Save (Fl_Widget *o, void *v) {
  1060. ((SynthModular*)(o->user_data()))->cb_Save_i (o, v);
  1061. }
  1062. // Merge
  1063. inline void SynthModular::cb_Merge_i (Fl_Widget *o, void *v) {
  1064. char *fn = fl_file_chooser ("Merge a patch", "*.ssm", NULL);
  1065. if (fn && fn!='\0') {
  1066. ifstream in (fn);
  1067. if (in) {
  1068. fstream inf;
  1069. inf.open (fn, ios::in);
  1070. m_MergeFilePath = fn;
  1071. StreamPatchIn (inf, false, true);
  1072. m_Canvas->StreamSelectionWiresIn (inf, m_Copied.m_DeviceIds, true, false);
  1073. inf.close();
  1074. }
  1075. }
  1076. }
  1077. void SynthModular::cb_Merge (Fl_Widget *o, void *v) {
  1078. ((SynthModular*)(o->parent()->user_data()))->cb_Merge_i (o, v);
  1079. }
  1080. // Close
  1081. inline void SynthModular::cb_Close_i (Fl_Widget *o, void *v) {
  1082. m_SettingsWindow->hide();
  1083. delete m_SettingsWindow;
  1084. m_TopWindow->hide();
  1085. delete m_TopWindow;
  1086. }
  1087. void SynthModular::cb_Close (Fl_Widget *o, void *v) {
  1088. ((SynthModular*)(o->user_data()))->cb_Close_i (o, v);
  1089. }
  1090. /////////////////////////////////
  1091. // Edit Menu
  1092. // Cut
  1093. inline void SynthModular::cb_Cut_i(Fl_Widget *o, void *v) {
  1094. if (! m_Canvas->HaveSelection()) return;
  1095. // show some warning here
  1096. cb_Copy_i (o, v); // should we be calling an inline function here??????
  1097. for (unsigned int i=0; i<m_Canvas->Selection().m_DeviceIds.size(); i++) {
  1098. int ID = m_Canvas->Selection().m_DeviceIds[i];
  1099. Fl_DeviceGUI::Kill(m_DeviceWinMap[ID]->m_DeviceGUI);
  1100. }
  1101. Fl_Canvas::ClearSelection(m_Canvas);
  1102. }
  1103. void SynthModular::cb_Cut (Fl_Widget *o, void *v) {
  1104. ((SynthModular*)(o->parent()->user_data()))->cb_Cut_i (o, v);
  1105. }
  1106. // Copy
  1107. inline void SynthModular::cb_Copy_i (Fl_Widget *o, void *v) {
  1108. if (! m_Canvas->HaveSelection()) return;
  1109. m_Copied.devices.open ("___temp.ssmcopytmp", ios::out);
  1110. m_Copied.devicecount = 0;
  1111. m_Copied.m_DeviceIds.clear();
  1112. if (m_FilePath != "") {
  1113. m_Copied.devices << true << " " << m_FilePath << endl;
  1114. }
  1115. else m_Copied.devices << false << endl;
  1116. for (unsigned int i=0; i<m_Canvas->Selection().m_DeviceIds.size(); i++) {
  1117. int ID = m_Canvas->Selection().m_DeviceIds[i];
  1118. std::map<int,DeviceWin*>::iterator i = m_DeviceWinMap.find(ID);
  1119. m_Copied.m_DeviceIds[ID] = ID;
  1120. m_Copied.devicecount += 1;
  1121. m_Copied.devices << "Device " << i->first << " " ; // save the id
  1122. m_Copied.devices << "Plugin " <<i->second->m_PluginID << endl;
  1123. m_Copied.devices << i->second->m_DeviceGUI->x() << " ";
  1124. m_Copied.devices << i->second->m_DeviceGUI->y() << " ";
  1125. m_Copied.devices << i->second->m_DeviceGUI->GetName().size() << " ";
  1126. m_Copied.devices << i->second->m_DeviceGUI->GetName() << " ";
  1127. if (i->second->m_DeviceGUI->GetPluginWindow()) {
  1128. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->visible() << " ";
  1129. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->x() << " ";
  1130. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->y() << " ";
  1131. }
  1132. else m_Copied.devices << 0 << " " << 0 << " " << 0;
  1133. m_Copied.devices << endl;
  1134. if (i->second->m_PluginID == COMMENT_ID) {
  1135. // save the comment gui
  1136. ((Fl_CommentGUI*)(i->second->m_DeviceGUI))->StreamOut (m_Copied.devices);
  1137. }
  1138. else {
  1139. // save the plugin
  1140. i->second->m_Device->StreamOut (m_Copied.devices);
  1141. }
  1142. m_Copied.devices<<endl;
  1143. }
  1144. m_Canvas->StreamSelectionWiresOut(m_Copied.devices);
  1145. m_Copied.devices.close();
  1146. Fl_Canvas::EnablePaste (m_Canvas);
  1147. }
  1148. void SynthModular::cb_Copy (Fl_Widget *o, void *v) {
  1149. ((SynthModular*)(o->parent()->user_data()))->cb_Copy_i (o, v);
  1150. }
  1151. // Paste
  1152. inline void SynthModular::cb_Paste_i (Fl_Widget *o, void *v) {
  1153. if (m_Copied.devicecount <= 0) return;
  1154. m_Copied.devices.open ("___temp.ssmcopytmp", ios::in);
  1155. StreamPatchIn(m_Copied.devices, true, false);
  1156. m_Canvas->StreamSelectionWiresIn (m_Copied.devices, m_Copied.m_DeviceIds, false, true);
  1157. m_Copied.devices.close();
  1158. }
  1159. void SynthModular::cb_Paste (Fl_Widget *o, void *v) {
  1160. ((SynthModular*)(o->parent()->user_data()))->cb_Paste_i (o, v);
  1161. }
  1162. // Delete
  1163. inline void SynthModular::cb_Delete_i (Fl_Widget *o, void *v) {
  1164. m_Canvas->DeleteSelection();
  1165. }
  1166. void SynthModular::cb_Delete (Fl_Widget *o, void *v) {
  1167. ((SynthModular*)(o->user_data()))->cb_Delete_i (o, v);
  1168. }
  1169. // Options
  1170. inline void SynthModular::cb_Options_i (Fl_Widget *o, void *v) {
  1171. m_SettingsWindow->show();
  1172. }
  1173. void SynthModular::cb_Options (Fl_Widget* o, void* v) {
  1174. ((SynthModular*)(o->user_data()))->cb_Options_i (o, v);
  1175. }
  1176. /////////////////////////////////
  1177. // Plugin Menu
  1178. // This callback has the name that the callback for the canvas menu
  1179. // used to have please note - that is now NewDeviceFromCanvasMenu
  1180. inline void SynthModular::cb_NewDeviceFromMenu_i (Fl_Widget *o, void *v) {
  1181. AddDevice (*((int*)v));
  1182. }
  1183. void SynthModular::cb_NewDeviceFromMenu (Fl_Widget *o, void *v) {
  1184. ((SynthModular*)(o->user_data()))->cb_NewDeviceFromMenu_i (o, v);
  1185. }
  1186. // (Plugin Buttons)
  1187. inline void SynthModular::cb_NewDevice_i (Fl_Button *o, void *v) {
  1188. AddDevice (*((int*)v));
  1189. }
  1190. void SynthModular::cb_NewDevice (Fl_Button *o, void *v) {
  1191. ((SynthModular*)(o->parent()->user_data()))->cb_NewDevice_i (o, v);
  1192. }
  1193. // (Plugin Canvas Menu)
  1194. inline void SynthModular::cb_NewDeviceFromCanvasMenu_i(Fl_Canvas* o, void* v)
  1195. {
  1196. AddDevice(*((int*)v),*((int*)v+1),*((int*)v+2));
  1197. }
  1198. void SynthModular::cb_NewDeviceFromCanvasMenu(Fl_Canvas* o, void* v)
  1199. {((SynthModular*)(o->user_data()))->cb_NewDeviceFromCanvasMenu_i(o,v);}
  1200. /////////////////////////////////
  1201. // Audio Menu
  1202. // Play / Pause
  1203. inline void SynthModular::cb_PlayPause_i (Fl_Widget *o, void *v) {
  1204. string oldname = m_PlayPause->tooltip ();
  1205. if (m_Info.PAUSED) {
  1206. m_PlayPause->label ("@||");
  1207. m_PlayPause->tooltip ("Pause");
  1208. ResumeAudio();
  1209. }
  1210. else {
  1211. m_PlayPause->label ("@>");
  1212. m_PlayPause->tooltip ("Play");
  1213. PauseAudio();
  1214. }
  1215. for (int i=0; i<m_MainMenu->size(); i++) {
  1216. if (m_MainMenu->text (i) != NULL) {
  1217. if (oldname == m_MainMenu->text (i)) {
  1218. m_MainMenu->replace (i, m_PlayPause->tooltip());
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. void SynthModular::cb_PlayPause (Fl_Widget *o, void *v) {
  1225. ((SynthModular*)(o->user_data()))->cb_PlayPause_i (o, v);
  1226. }
  1227. // Reset
  1228. inline void SynthModular::cb_Reset_i (Fl_Widget *o, void *v) {
  1229. ResetAudio();
  1230. }
  1231. void SynthModular::cb_Reset (Fl_Widget *o, void *v) {
  1232. ((SynthModular*)(o->user_data()))->cb_Reset_i (o, v);
  1233. }
  1234. //////////////////////////////////////////////////////////
  1235. inline void SynthModular::cb_NewComment_i(Fl_Button* o, void* v)
  1236. {
  1237. AddComment(-1);
  1238. }
  1239. void SynthModular::cb_NewComment(Fl_Button* o, void* v)
  1240. {((SynthModular*)(o->parent()->user_data()))->cb_NewComment_i(o,v);}
  1241. //////////////////////////////////////////////////////////
  1242. inline void SynthModular::cb_GroupTab_i(Fl_Tabs* o, void* v)
  1243. {
  1244. m_GroupTab->redraw();
  1245. }
  1246. void SynthModular::cb_GroupTab(Fl_Tabs* o, void* v)
  1247. {((SynthModular*)(o->parent()->user_data()))->cb_GroupTab_i(o,v);}
  1248. //////////////////////////////////////////////////////////
  1249. inline void SynthModular::cb_Connection_i(Fl_Canvas* o, void* v)
  1250. {
  1251. CanvasWire *Wire;
  1252. Wire=(CanvasWire*)v;
  1253. map<int,DeviceWin*>::iterator si=m_DeviceWinMap.find(Wire->OutputID);
  1254. if (si==m_DeviceWinMap.end())
  1255. {
  1256. char num[32];
  1257. sprintf(num,"%d",Wire->OutputID);
  1258. SpiralInfo::Alert("Warning: Connection problem - can't find source "+string(num));
  1259. return;
  1260. }
  1261. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  1262. if (di==m_DeviceWinMap.end())
  1263. {
  1264. char num[32];
  1265. sprintf(num,"%d",Wire->InputID);
  1266. SpiralInfo::Alert("Warning: Connection problem - can't find destination "+string(num));
  1267. return;
  1268. }
  1269. Sample *sample=NULL;
  1270. if (!si->second->m_Device->GetOutput(Wire->OutputPort,&sample))
  1271. {
  1272. char num[32];
  1273. sprintf(num,"%d,%d",Wire->OutputID,Wire->OutputPort);
  1274. SpiralInfo::Alert("Warning: Connection problem - can't find source output "+string(num));
  1275. return;
  1276. }
  1277. if (!di->second->m_Device->SetInput(Wire->InputPort,(const Sample*)sample))
  1278. {
  1279. char num[32];
  1280. sprintf(num,"%d,%d",Wire->InputID,Wire->InputPort);
  1281. SpiralInfo::Alert("Warning: Connection problem - can't find source input "+string(num));
  1282. return;
  1283. }
  1284. }
  1285. void SynthModular::cb_Connection(Fl_Canvas* o, void* v)
  1286. {((SynthModular*)(o->user_data()))->cb_Connection_i(o,v);}
  1287. //////////////////////////////////////////////////////////
  1288. inline void SynthModular::cb_Unconnect_i(Fl_Canvas* o, void* v)
  1289. {
  1290. CanvasWire *Wire;
  1291. Wire=(CanvasWire*)v;
  1292. //cerr<<Wire->InputID<<" "<<Wire->InputPort<<endl;
  1293. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  1294. if (di==m_DeviceWinMap.end())
  1295. {
  1296. //cerr<<"Can't find destination device "<<Wire->InputID<<endl;
  1297. return;
  1298. }
  1299. SpiralPlugin *Plugin=di->second->m_Device;
  1300. if (Plugin && !Plugin->SetInput(Wire->InputPort,NULL))
  1301. { cerr<<"Can't find destination device's Input"<<endl; return; }
  1302. }
  1303. void SynthModular::cb_Unconnect(Fl_Canvas* o, void* v)
  1304. {((SynthModular*)(o->user_data()))->cb_Unconnect_i(o,v);}
  1305. //////////////////////////////////////////////////////////
  1306. void SynthModular::cb_UpdatePluginInfo(int ID, void *PInfo)
  1307. {
  1308. map<int,DeviceWin*>::iterator i=m_DeviceWinMap.find(ID);
  1309. if (i!=m_DeviceWinMap.end())
  1310. {
  1311. DeviceGUIInfo Info=BuildDeviceGUIInfo(*((PluginInfo*)PInfo));
  1312. (*i).second->m_DeviceGUI->Setup(Info);
  1313. (*i).second->m_DeviceGUI->redraw();
  1314. }
  1315. }
  1316. //////////////////////////////////////////////////////////
  1317. void SynthModular::LoadPatch(const char *fn)
  1318. {
  1319. ifstream in(fn);
  1320. if (in)
  1321. {
  1322. fstream inf;
  1323. inf.open(fn, std::ios::in);
  1324. m_FilePath=fn;
  1325. ClearUp();
  1326. inf>>*this;
  1327. inf.close();
  1328. TITLEBAR=LABEL+" "+fn;
  1329. m_TopWindow->label(TITLEBAR.c_str());
  1330. }
  1331. }