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.

1585 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. string m_FromFilePath;
  752. string dummy,dummy2;
  753. int ver;
  754. if (paste)
  755. {
  756. m_Copied.devices>>has_file_path;
  757. if (has_file_path)
  758. m_Copied.devices>>m_FromFilePath;
  759. }
  760. else
  761. {
  762. s>>dummy>>dummy>>dummy>>ver;
  763. if (ver>FILE_VERSION)
  764. {
  765. SpiralInfo::Alert("Bad file, or more recent version.");
  766. ThawAll();
  767. return s;
  768. }
  769. if (ver>2)
  770. {
  771. int MainWinX,MainWinY,MainWinW,MainWinH;
  772. int EditWinX,EditWinY,EditWinW,EditWinH;
  773. s>>MainWinX>>MainWinY>>MainWinW>>MainWinH;
  774. s>>EditWinX>>EditWinY>>EditWinW>>EditWinH;
  775. //o.m_MainWindow->resize(MainWinX,MainWinY,MainWinW,MainWinH);
  776. //o.m_EditorWindow->resize(EditWinX,EditWinY,EditWinW,EditWinH);
  777. }
  778. if (merge)
  779. m_FromFilePath = m_MergeFilePath;
  780. }
  781. //wether pasting or merging we need to clear the current
  782. //selection so we can replace it with the new devices
  783. if (paste || merge)
  784. Fl_Canvas::ClearSelection(m_Canvas);
  785. int Num, ID, PluginID, x,y,ps,px,py;
  786. if (paste)
  787. {
  788. Num = m_Copied.devicecount;
  789. }
  790. else
  791. {
  792. s>>dummy>>Num;
  793. }
  794. for(int n=0; n<Num; n++)
  795. {
  796. #ifdef DEBUG_STREAM
  797. cerr<<"Loading Device "<<n<<endl;
  798. #endif
  799. s>>dummy; // "Device"
  800. s>>ID;
  801. s>>dummy2; // "Plugin"
  802. s>>PluginID;
  803. s>>x>>y;
  804. string Name;
  805. if (paste || ver>3)
  806. {
  807. // load the device name
  808. int size;
  809. char Buf[1024];
  810. s>>size;
  811. s.ignore(1);
  812. if (size > 0) {
  813. s.get(Buf,size+1);
  814. Name=Buf;
  815. } else {
  816. Name = "";
  817. }
  818. }
  819. #ifdef DEBUG_STREAM
  820. cerr<<dummy<<" "<<ID<<" "<<dummy2<<" "<<PluginID<<" "<<x<<" "<<y<<endl;
  821. #endif
  822. if (paste || ver>1) s>>ps>>px>>py;
  823. //if we are merging a patch or pasting we will change duplicate ID's
  824. if (!paste && !merge)
  825. {
  826. // Check we're not duplicating an ID
  827. if (m_DeviceWinMap.find(ID)!=m_DeviceWinMap.end())
  828. {
  829. SpiralInfo::Alert("Duplicate device ID found in file - aborting load");
  830. ThawAll();
  831. return s;
  832. }
  833. }
  834. if (PluginID==COMMENT_ID)
  835. {
  836. DeviceWin* temp = NewComment(PluginID, x, y);
  837. if (temp)
  838. {
  839. if (paste || merge)
  840. {
  841. m_Copied.m_DeviceIds[ID] = m_NextID++;
  842. ID = m_Copied.m_DeviceIds[ID];
  843. }
  844. temp->m_DeviceGUI->SetID(ID);
  845. m_DeviceWinMap[ID]=temp;
  846. ((Fl_CommentGUI*)(m_DeviceWinMap[ID]->m_DeviceGUI))->StreamIn(s); // load the plugin
  847. if (paste || merge)
  848. Fl_Canvas::AppendSelection(ID, m_Canvas);
  849. else
  850. if (m_NextID<=ID) m_NextID=ID+1;
  851. }
  852. }
  853. else
  854. {
  855. DeviceWin* temp = NewDeviceWin(PluginID, x, y);
  856. if (temp)
  857. {
  858. int oldID=ID;
  859. if (paste || merge)
  860. {
  861. m_Copied.m_DeviceIds[ID] = m_NextID++;
  862. ID = m_Copied.m_DeviceIds[ID];
  863. }
  864. temp->m_DeviceGUI->SetID(ID);
  865. if (paste || ver>3)
  866. {
  867. // set the titlebars
  868. temp->m_DeviceGUI->SetName(Name);
  869. }
  870. temp->m_Device->SetUpdateInfoCallback(ID,cb_UpdatePluginInfo);
  871. m_DeviceWinMap[ID]=temp;
  872. m_DeviceWinMap[ID]->m_Device->StreamIn(s); // load the plugin
  873. // load external files
  874. if (paste || merge)
  875. m_DeviceWinMap[ID]->m_Device->LoadExternalFiles(m_FromFilePath+"_files/", oldID);
  876. else
  877. m_DeviceWinMap[ID]->m_Device->LoadExternalFiles(m_FilePath+"_files/");
  878. if ((paste || ver>1) && m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow())
  879. {
  880. // set the GUI up with the loaded values
  881. // looks messy, but if we do it here, the plugin and it's gui can remain
  882. // totally seperated.
  883. ((SpiralPluginGUI*)(m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()))->
  884. UpdateValues(m_DeviceWinMap[ID]->m_Device);
  885. // updates the data in the channel buffers, so the values don't
  886. // get overwritten in the next tick. (should maybe be somewhere else)
  887. m_DeviceWinMap[ID]->m_Device->GetChannelHandler()->FlushChannels();
  888. // position the plugin window in the main window
  889. //m_DeviceWinMap[ID]->m_DeviceGUI->GetPluginWindow()->position(px,py);
  890. if (ps)
  891. {
  892. m_DeviceWinMap[ID]->m_DeviceGUI->Maximise();
  893. // reposition after maximise
  894. m_DeviceWinMap[ID]->m_DeviceGUI->position(x,y);
  895. }
  896. else m_DeviceWinMap[ID]->m_DeviceGUI->Minimise();
  897. if (paste || merge)
  898. Fl_Canvas::AppendSelection(ID, m_Canvas);
  899. }
  900. if (!paste && !merge)
  901. if (m_NextID<=ID) m_NextID=ID+1;
  902. }
  903. else
  904. {
  905. // can't really recover if the plugin ID doesn't match a plugin, as
  906. // we have no idea how much data in the stream belongs to this plugin
  907. SpiralInfo::Alert("Error in stream, can't really recover data from here on.");
  908. return s;
  909. }
  910. }
  911. }
  912. if (!paste && !merge)
  913. {
  914. s>>*m_Canvas;
  915. ThawAll();
  916. }
  917. return s;
  918. }
  919. iostream &operator>>(iostream &s, SynthModular &o)
  920. {
  921. return o.StreamPatchIn(s, false, false);
  922. }
  923. //////////////////////////////////////////////////////////
  924. ostream &operator<<(ostream &s, SynthModular &o)
  925. {
  926. o.FreezeAll();
  927. s<<"SpiralSynthModular File Ver "<<FILE_VERSION<<endl;
  928. // make external files dir
  929. bool ExternalDirUsed=false;
  930. string command("mkdir "+o.m_FilePath+"_files");
  931. system(command.c_str());
  932. if (FILE_VERSION>2)
  933. {
  934. s<<o.m_TopWindow->x()<<" "<<o.m_TopWindow->y()<<" ";
  935. s<<o.m_TopWindow->w()<<" "<<o.m_TopWindow->h()<<" ";
  936. s<<0<<" "<<0<<" ";
  937. s<<0<<" "<<0<<endl;
  938. }
  939. // save out the SynthModular
  940. s<<"SectionList"<<endl;
  941. s<<o.m_DeviceWinMap.size()<<endl;
  942. for(map<int,DeviceWin*>::iterator i=o.m_DeviceWinMap.begin();
  943. i!=o.m_DeviceWinMap.end(); i++)
  944. {
  945. if (i->second->m_DeviceGUI && ((i->second->m_Device) || (i->second->m_PluginID==COMMENT_ID)))
  946. {
  947. s<<endl;
  948. s<<"Device ";
  949. s<<i->first<<" "; // save the id
  950. s<<"Plugin ";
  951. s<<i->second->m_PluginID<<endl;
  952. s<<i->second->m_DeviceGUI->x()<<" ";
  953. s<<i->second->m_DeviceGUI->y()<<" ";
  954. s<<i->second->m_DeviceGUI->GetName().size()<<" ";
  955. s<<i->second->m_DeviceGUI->GetName()<<" ";
  956. if (i->second->m_DeviceGUI->GetPluginWindow())
  957. {
  958. s<<i->second->m_DeviceGUI->GetPluginWindow()->visible()<<" ";
  959. s<<i->second->m_DeviceGUI->GetPluginWindow()->x()<<" ";
  960. s<<i->second->m_DeviceGUI->GetPluginWindow()->y()<<" ";
  961. }
  962. else
  963. {
  964. s<<0<<" "<<0<<" "<<0;
  965. }
  966. s<<endl;
  967. if (i->second->m_PluginID==COMMENT_ID)
  968. {
  969. // save the comment gui
  970. ((Fl_CommentGUI*)(i->second->m_DeviceGUI))->StreamOut(s);
  971. }
  972. else
  973. {
  974. // save the plugin
  975. i->second->m_Device->StreamOut(s);
  976. }
  977. s<<endl;
  978. // save external files
  979. if (i->second->m_Device && i->second->m_Device->SaveExternalFiles(o.m_FilePath+"_files/"))
  980. {
  981. ExternalDirUsed=true;
  982. }
  983. }
  984. }
  985. s<<endl<<*o.m_Canvas<<endl;
  986. // remove it if it wasn't used
  987. if (!ExternalDirUsed)
  988. {
  989. // i guess rmdir won't work if there is something in the dir
  990. // anyway, but best to be on the safe side. (could do rm -rf) :)
  991. string command("rmdir "+o.m_FilePath+"_files");
  992. system(command.c_str());
  993. }
  994. o.ThawAll();
  995. return s;
  996. }
  997. //////////////////////////////////////////////////////////////////////////////////////////
  998. // Callbacks
  999. /////////////////////////////////
  1000. // File Menu & associated buttons
  1001. // New
  1002. inline void SynthModular::cb_New_i (Fl_Widget *o, void *v) {
  1003. if (m_DeviceWinMap.size()>0 && !Pawfal_YesNo ("New - Lose changes to current patch?"))
  1004. return;
  1005. m_TopWindow->label (TITLEBAR.c_str());
  1006. ClearUp();
  1007. }
  1008. void SynthModular::cb_New (Fl_Widget *o, void *v) {
  1009. ((SynthModular*)(o->user_data()))->cb_New_i (o, v);
  1010. }
  1011. // Load
  1012. inline void SynthModular::cb_Load_i (Fl_Widget *o, void *v) {
  1013. if (m_DeviceWinMap.size()>0 && !Pawfal_YesNo ("Load - Lose changes to current patch?"))
  1014. return;
  1015. char *fn=fl_file_chooser ("Load a patch", "*.ssm", NULL);
  1016. if (fn && fn!='\0') {
  1017. ifstream in (fn);
  1018. if (in) {
  1019. fstream inf;
  1020. inf.open (fn, ios::in);
  1021. m_FilePath = fn;
  1022. ClearUp();
  1023. inf >> *this;
  1024. inf.close();
  1025. TITLEBAR = LABEL + " " + fn;
  1026. m_TopWindow->label (TITLEBAR.c_str());
  1027. }
  1028. }
  1029. }
  1030. void SynthModular::cb_Load(Fl_Widget *o, void *v) {
  1031. ((SynthModular*)(o->user_data()))->cb_Load_i (o, v);
  1032. }
  1033. // Save
  1034. inline void SynthModular::cb_Save_i (Fl_Widget *o, void *v) {
  1035. char *fn=fl_file_chooser("Save a patch", "*.ssm", NULL);
  1036. if (fn && fn!='\0') {
  1037. ifstream ifl (fn);
  1038. if (ifl) {
  1039. if (!Pawfal_YesNo ("File [%s] exists, overwrite?", fn))
  1040. return;
  1041. }
  1042. ofstream of (fn);
  1043. if (of) {
  1044. m_FilePath = fn;
  1045. of << *this;
  1046. TITLEBAR = LABEL + " " + fn;
  1047. m_TopWindow->label (TITLEBAR.c_str());
  1048. }
  1049. else {
  1050. fl_message (string ("Error saving " + string(fn)).c_str());
  1051. }
  1052. }
  1053. }
  1054. void SynthModular::cb_Save (Fl_Widget *o, void *v) {
  1055. ((SynthModular*)(o->user_data()))->cb_Save_i (o, v);
  1056. }
  1057. // Merge
  1058. inline void SynthModular::cb_Merge_i (Fl_Widget *o, void *v) {
  1059. char *fn = fl_file_chooser ("Merge a patch", "*.ssm", NULL);
  1060. if (fn && fn!='\0') {
  1061. ifstream in (fn);
  1062. if (in) {
  1063. fstream inf;
  1064. inf.open (fn, ios::in);
  1065. m_MergeFilePath = fn;
  1066. StreamPatchIn (inf, false, true);
  1067. m_Canvas->StreamSelectionWiresIn (inf, m_Copied.m_DeviceIds, true, false);
  1068. inf.close();
  1069. }
  1070. }
  1071. }
  1072. void SynthModular::cb_Merge (Fl_Widget *o, void *v) {
  1073. ((SynthModular*)(o->parent()->user_data()))->cb_Merge_i (o, v);
  1074. }
  1075. // Close
  1076. inline void SynthModular::cb_Close_i (Fl_Widget *o, void *v) {
  1077. m_SettingsWindow->hide();
  1078. delete m_SettingsWindow;
  1079. m_TopWindow->hide();
  1080. delete m_TopWindow;
  1081. }
  1082. void SynthModular::cb_Close (Fl_Widget *o, void *v) {
  1083. ((SynthModular*)(o->user_data()))->cb_Close_i (o, v);
  1084. }
  1085. /////////////////////////////////
  1086. // Edit Menu
  1087. // Cut
  1088. inline void SynthModular::cb_Cut_i(Fl_Widget *o, void *v) {
  1089. if (! m_Canvas->HaveSelection()) return;
  1090. // show some warning here
  1091. cb_Copy_i (o, v); // should we be calling an inline function here??????
  1092. for (unsigned int i=0; i<m_Canvas->Selection().m_DeviceIds.size(); i++) {
  1093. int ID = m_Canvas->Selection().m_DeviceIds[i];
  1094. Fl_DeviceGUI::Kill(m_DeviceWinMap[ID]->m_DeviceGUI);
  1095. }
  1096. Fl_Canvas::ClearSelection(m_Canvas);
  1097. }
  1098. void SynthModular::cb_Cut (Fl_Widget *o, void *v) {
  1099. ((SynthModular*)(o->parent()->user_data()))->cb_Cut_i (o, v);
  1100. }
  1101. // Copy
  1102. inline void SynthModular::cb_Copy_i (Fl_Widget *o, void *v) {
  1103. if (! m_Canvas->HaveSelection()) return;
  1104. m_Copied.devices.open ("___temp.ssmcopytmp", ios::out);
  1105. m_Copied.devicecount = 0;
  1106. m_Copied.m_DeviceIds.clear();
  1107. if (m_FilePath != "") m_Copied.devices << true << " " << m_FilePath << endl;
  1108. else m_Copied.devices << false << endl;
  1109. for (unsigned int i=0; i<m_Canvas->Selection().m_DeviceIds.size(); i++) {
  1110. int ID = m_Canvas->Selection().m_DeviceIds[i];
  1111. std::map<int,DeviceWin*>::iterator i = m_DeviceWinMap.find(ID);
  1112. m_Copied.m_DeviceIds[ID] = ID;
  1113. m_Copied.devicecount += 1;
  1114. m_Copied.devices << "Device " << i->first << " " ; // save the id
  1115. m_Copied.devices << "Plugin " <<i->second->m_PluginID << endl;
  1116. m_Copied.devices << i->second->m_DeviceGUI->x() << " ";
  1117. m_Copied.devices << i->second->m_DeviceGUI->y() << " ";
  1118. m_Copied.devices << i->second->m_DeviceGUI->GetName().size() << " ";
  1119. m_Copied.devices << i->second->m_DeviceGUI->GetName() << " ";
  1120. if (i->second->m_DeviceGUI->GetPluginWindow()) {
  1121. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->visible() << " ";
  1122. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->x() << " ";
  1123. m_Copied.devices << i->second->m_DeviceGUI->GetPluginWindow()->y() << " ";
  1124. }
  1125. else m_Copied.devices << 0 << " " << 0 << " " << 0;
  1126. m_Copied.devices << endl;
  1127. if (i->second->m_PluginID == COMMENT_ID) {
  1128. // save the comment gui
  1129. ((Fl_CommentGUI*)(i->second->m_DeviceGUI))->StreamOut (m_Copied.devices);
  1130. }
  1131. else {
  1132. // save the plugin
  1133. i->second->m_Device->StreamOut (m_Copied.devices);
  1134. }
  1135. m_Copied.devices<<endl;
  1136. }
  1137. m_Canvas->StreamSelectionWiresOut(m_Copied.devices);
  1138. m_Copied.devices.close();
  1139. Fl_Canvas::EnablePaste (m_Canvas);
  1140. }
  1141. void SynthModular::cb_Copy (Fl_Widget *o, void *v) {
  1142. ((SynthModular*)(o->parent()->user_data()))->cb_Copy_i (o, v);
  1143. }
  1144. // Paste
  1145. inline void SynthModular::cb_Paste_i (Fl_Widget *o, void *v) {
  1146. if (m_Copied.devicecount <= 0) return;
  1147. m_Copied.devices.open ("___temp.ssmcopytmp", ios::in);
  1148. StreamPatchIn(m_Copied.devices, true, false);
  1149. m_Canvas->StreamSelectionWiresIn (m_Copied.devices, m_Copied.m_DeviceIds, false, true);
  1150. m_Copied.devices.close();
  1151. }
  1152. void SynthModular::cb_Paste (Fl_Widget *o, void *v) {
  1153. ((SynthModular*)(o->parent()->user_data()))->cb_Paste_i (o, v);
  1154. }
  1155. // Delete
  1156. inline void SynthModular::cb_Delete_i (Fl_Widget *o, void *v) {
  1157. m_Canvas->DeleteSelection();
  1158. }
  1159. void SynthModular::cb_Delete (Fl_Widget *o, void *v) {
  1160. ((SynthModular*)(o->user_data()))->cb_Delete_i (o, v);
  1161. }
  1162. // Options
  1163. inline void SynthModular::cb_Options_i (Fl_Widget *o, void *v) {
  1164. m_SettingsWindow->show();
  1165. }
  1166. void SynthModular::cb_Options (Fl_Widget* o, void* v) {
  1167. ((SynthModular*)(o->user_data()))->cb_Options_i (o, v);
  1168. }
  1169. /////////////////////////////////
  1170. // Plugin Menu
  1171. // This callback has the name that the callback for the canvas menu
  1172. // used to have please note - that is now NewDeviceFromCanvasMenu
  1173. inline void SynthModular::cb_NewDeviceFromMenu_i (Fl_Widget *o, void *v) {
  1174. AddDevice (*((int*)v));
  1175. }
  1176. void SynthModular::cb_NewDeviceFromMenu (Fl_Widget *o, void *v) {
  1177. ((SynthModular*)(o->user_data()))->cb_NewDeviceFromMenu_i (o, v);
  1178. }
  1179. // (Plugin Buttons)
  1180. inline void SynthModular::cb_NewDevice_i (Fl_Button *o, void *v) {
  1181. AddDevice (*((int*)v));
  1182. }
  1183. void SynthModular::cb_NewDevice (Fl_Button *o, void *v) {
  1184. ((SynthModular*)(o->parent()->user_data()))->cb_NewDevice_i (o, v);
  1185. }
  1186. // (Plugin Canvas Menu)
  1187. inline void SynthModular::cb_NewDeviceFromCanvasMenu_i(Fl_Canvas* o, void* v)
  1188. {
  1189. AddDevice(*((int*)v),*((int*)v+1),*((int*)v+2));
  1190. }
  1191. void SynthModular::cb_NewDeviceFromCanvasMenu(Fl_Canvas* o, void* v)
  1192. {((SynthModular*)(o->user_data()))->cb_NewDeviceFromCanvasMenu_i(o,v);}
  1193. /////////////////////////////////
  1194. // Audio Menu
  1195. // Play / Pause
  1196. inline void SynthModular::cb_PlayPause_i (Fl_Widget *o, void *v) {
  1197. string oldname = m_PlayPause->tooltip ();
  1198. if (m_Info.PAUSED) {
  1199. m_PlayPause->label ("@||");
  1200. m_PlayPause->tooltip ("Pause");
  1201. ResumeAudio();
  1202. }
  1203. else {
  1204. m_PlayPause->label ("@>");
  1205. m_PlayPause->tooltip ("Play");
  1206. PauseAudio();
  1207. }
  1208. for (int i=0; i<m_MainMenu->size(); i++) {
  1209. if (m_MainMenu->text (i) != NULL) {
  1210. if (oldname == m_MainMenu->text (i)) {
  1211. m_MainMenu->replace (i, m_PlayPause->tooltip());
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. void SynthModular::cb_PlayPause (Fl_Widget *o, void *v) {
  1218. ((SynthModular*)(o->user_data()))->cb_PlayPause_i (o, v);
  1219. }
  1220. // Reset
  1221. inline void SynthModular::cb_Reset_i (Fl_Widget *o, void *v) {
  1222. ResetAudio();
  1223. }
  1224. void SynthModular::cb_Reset (Fl_Widget *o, void *v) {
  1225. ((SynthModular*)(o->user_data()))->cb_Reset_i (o, v);
  1226. }
  1227. //////////////////////////////////////////////////////////
  1228. inline void SynthModular::cb_NewComment_i(Fl_Button* o, void* v)
  1229. {
  1230. AddComment(-1);
  1231. }
  1232. void SynthModular::cb_NewComment(Fl_Button* o, void* v)
  1233. {((SynthModular*)(o->parent()->user_data()))->cb_NewComment_i(o,v);}
  1234. //////////////////////////////////////////////////////////
  1235. inline void SynthModular::cb_GroupTab_i(Fl_Tabs* o, void* v)
  1236. {
  1237. m_GroupTab->redraw();
  1238. }
  1239. void SynthModular::cb_GroupTab(Fl_Tabs* o, void* v)
  1240. {((SynthModular*)(o->parent()->user_data()))->cb_GroupTab_i(o,v);}
  1241. //////////////////////////////////////////////////////////
  1242. inline void SynthModular::cb_Connection_i(Fl_Canvas* o, void* v)
  1243. {
  1244. CanvasWire *Wire;
  1245. Wire=(CanvasWire*)v;
  1246. map<int,DeviceWin*>::iterator si=m_DeviceWinMap.find(Wire->OutputID);
  1247. if (si==m_DeviceWinMap.end())
  1248. {
  1249. char num[32];
  1250. sprintf(num,"%d",Wire->OutputID);
  1251. SpiralInfo::Alert("Warning: Connection problem - can't find source "+string(num));
  1252. return;
  1253. }
  1254. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  1255. if (di==m_DeviceWinMap.end())
  1256. {
  1257. char num[32];
  1258. sprintf(num,"%d",Wire->InputID);
  1259. SpiralInfo::Alert("Warning: Connection problem - can't find destination "+string(num));
  1260. return;
  1261. }
  1262. Sample *sample=NULL;
  1263. if (!si->second->m_Device->GetOutput(Wire->OutputPort,&sample))
  1264. {
  1265. char num[32];
  1266. sprintf(num,"%d,%d",Wire->OutputID,Wire->OutputPort);
  1267. SpiralInfo::Alert("Warning: Connection problem - can't find source output "+string(num));
  1268. return;
  1269. }
  1270. if (!di->second->m_Device->SetInput(Wire->InputPort,(const Sample*)sample))
  1271. {
  1272. char num[32];
  1273. sprintf(num,"%d,%d",Wire->InputID,Wire->InputPort);
  1274. SpiralInfo::Alert("Warning: Connection problem - can't find source input "+string(num));
  1275. return;
  1276. }
  1277. }
  1278. void SynthModular::cb_Connection(Fl_Canvas* o, void* v)
  1279. {((SynthModular*)(o->user_data()))->cb_Connection_i(o,v);}
  1280. //////////////////////////////////////////////////////////
  1281. inline void SynthModular::cb_Unconnect_i(Fl_Canvas* o, void* v)
  1282. {
  1283. CanvasWire *Wire;
  1284. Wire=(CanvasWire*)v;
  1285. //cerr<<Wire->InputID<<" "<<Wire->InputPort<<endl;
  1286. map<int,DeviceWin*>::iterator di=m_DeviceWinMap.find(Wire->InputID);
  1287. if (di==m_DeviceWinMap.end())
  1288. {
  1289. //cerr<<"Can't find destination device "<<Wire->InputID<<endl;
  1290. return;
  1291. }
  1292. SpiralPlugin *Plugin=di->second->m_Device;
  1293. if (Plugin && !Plugin->SetInput(Wire->InputPort,NULL))
  1294. { cerr<<"Can't find destination device's Input"<<endl; return; }
  1295. }
  1296. void SynthModular::cb_Unconnect(Fl_Canvas* o, void* v)
  1297. {((SynthModular*)(o->user_data()))->cb_Unconnect_i(o,v);}
  1298. //////////////////////////////////////////////////////////
  1299. void SynthModular::cb_UpdatePluginInfo(int ID, void *PInfo)
  1300. {
  1301. map<int,DeviceWin*>::iterator i=m_DeviceWinMap.find(ID);
  1302. if (i!=m_DeviceWinMap.end())
  1303. {
  1304. DeviceGUIInfo Info=BuildDeviceGUIInfo(*((PluginInfo*)PInfo));
  1305. (*i).second->m_DeviceGUI->Setup(Info);
  1306. (*i).second->m_DeviceGUI->redraw();
  1307. }
  1308. }
  1309. //////////////////////////////////////////////////////////
  1310. void SynthModular::LoadPatch(const char *fn)
  1311. {
  1312. ifstream in(fn);
  1313. if (in)
  1314. {
  1315. fstream inf;
  1316. inf.open(fn, std::ios::in);
  1317. m_FilePath=fn;
  1318. ClearUp();
  1319. inf>>*this;
  1320. inf.close();
  1321. TITLEBAR=LABEL+" "+fn;
  1322. m_TopWindow->label(TITLEBAR.c_str());
  1323. }
  1324. }