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.

264 lines
6.5KB

  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <iostream>
  22. #include <cstdlib>
  23. #include <FL/Fl.H>
  24. #include <FL/Fl_Tooltip.h>
  25. #include <unistd.h>
  26. #include <sys/time.h>
  27. #include <sys/resource.h>
  28. #include "SpiralSynthModular.h"
  29. pthread_t loopthread,watchdogthread;
  30. SynthModular *synth;
  31. char watchdog_check = 1;
  32. char gui_watchdog_check = 1;
  33. int pthread_create_realtime (pthread_t *new_thread,
  34. void *(*start)(void *), void *arg,
  35. int priority);
  36. bool CallbackOnly = false;
  37. bool FIFO = false;
  38. bool GUI = true;
  39. /////////////////////////////////////////////////////////////
  40. void watchdog (void *arg)
  41. {
  42. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  43. watchdog_check = 0;
  44. gui_watchdog_check = 0;
  45. while (1)
  46. {
  47. usleep (10000000);
  48. // gui watchdog goes off with modal dialog boxes
  49. if (watchdog_check == 0)// || gui_watchdog_check== 0)
  50. {
  51. cerr<<"ssm watchdog: timeout - killing ssm"<<endl;
  52. if (watchdog_check==0) cerr<<"diagnosis: audio hung?"<<endl;
  53. if (gui_watchdog_check==0) cerr<<"diagnosis: gui starved by audio"<<endl;
  54. exit (1);
  55. }
  56. watchdog_check = 0;
  57. gui_watchdog_check = 0;
  58. }
  59. }
  60. ///////////////////////////////////////////////////////////////////////
  61. void audioloop(void* o)
  62. {
  63. while(1)
  64. {
  65. if (!synth->CallbackMode())
  66. {
  67. // do funky stuff
  68. synth->Update();
  69. // put the brakes on if there is no blocking output running
  70. if (!synth->IsBlockingOutputPluginReady()||
  71. synth->IsPaused())
  72. {
  73. usleep(10000);
  74. }
  75. }
  76. else
  77. {
  78. // the engine is currently in callback mode, so we don't
  79. // need to do anything unless we are switched back
  80. usleep(1000000);
  81. }
  82. watchdog_check = 1;
  83. }
  84. }
  85. //////////////////////////////////////////////////////
  86. #if __APPLE__
  87. #include <CoreFoundation/CFBundle.h>
  88. #include <libgen.h>
  89. #endif
  90. #include "GraphSort.h"
  91. int main(int argc, char **argv)
  92. {
  93. #if __APPLE__
  94. // --with-plugindir=./Libraries
  95. system("pwd");
  96. CFBundleRef main = CFBundleGetMainBundle();
  97. CFURLRef url = main ? CFBundleCopyExecutableURL(main) : NULL;
  98. CFStringRef path = url ? CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle) : NULL;
  99. char * dst = (char*)CFStringGetCStringPtr(path, 0);
  100. printf("main %p url %p path %p dst %p", main, url, path, dst);
  101. if (dst) {
  102. printf("Have a valid name '%s'\n", dst);
  103. chdir(dirname(dst));
  104. chdir("..");
  105. } else
  106. printf("No base pathname\n");
  107. #endif
  108. srand(time(NULL));
  109. SpiralSynthModularInfo::Get()->LoadPrefs();
  110. // get args
  111. string cmd_filename="";
  112. bool cmd_specd = false;
  113. string cmd_pluginPath="";
  114. // parse the args
  115. if (argc>1)
  116. {
  117. for (int a=1; a<argc; a++)
  118. {
  119. if (!strcmp(argv[a],"--NoGUI")) GUI = false;
  120. else if (!strcmp(argv[a],"--Realtime")) FIFO = true;
  121. else if (!strcmp(argv[a],"-h"))
  122. {
  123. cout<<"usage: spiralsynthmodular [patch.ssm] [options]"<<endl<<endl
  124. <<"options list"<<endl
  125. <<"-h : help"<<endl
  126. <<"-v : print version"<<endl
  127. <<"--NoGUI : run without GUI (only useful when loading patch from command line"<<endl
  128. <<"--Realtime : spawn audio thread with FIFO scheduling (run as root)"<<endl
  129. <<"--PluginPath <PATH> : look for plugins in the specified directory"<<endl;
  130. exit(0);
  131. }
  132. else if (!strcmp(argv[a],"-v"))
  133. {
  134. cout<<VER_STRING<<endl; exit(0);
  135. }
  136. else if (!strcmp(argv[a],"--PluginPath"))
  137. {
  138. a++;
  139. cmd_pluginPath=argv[a];
  140. }
  141. else
  142. {
  143. cmd_filename = argv[1];
  144. cmd_specd = true;
  145. }
  146. }
  147. }
  148. // set some fltk defaults
  149. Fl_Tooltip::size(10);
  150. Fl::visible_focus(false);
  151. Fl::visual(FL_DOUBLE|FL_RGB);
  152. synth=new SynthModular;
  153. // setup the synth
  154. Fl_Window* win = synth->CreateWindow();
  155. synth->LoadPlugins(cmd_pluginPath);
  156. win->xclass("");
  157. if (GUI) win->show(1, argv); // prevents stuff happening before the plugins have loaded
  158. // spawn the audio thread
  159. if (FIFO)
  160. {
  161. pthread_create_realtime(&watchdogthread,(void*(*)(void*))watchdog,NULL,sched_get_priority_max(SCHED_FIFO));
  162. pthread_create_realtime(&loopthread,(void*(*)(void*))audioloop,NULL,sched_get_priority_max(SCHED_FIFO)-1);
  163. }
  164. else
  165. {
  166. pthread_create(&loopthread,NULL,(void*(*)(void*))audioloop,NULL);
  167. // reduce the priority of the gui
  168. if (setpriority(PRIO_PROCESS,0,20)) cerr<<"Could not set priority for GUI thread"<<endl;
  169. }
  170. // do we need to load a patch on startup?
  171. if (cmd_specd) synth->LoadPatch(cmd_filename.c_str());
  172. if (!GUI)
  173. {
  174. // if there is no gui needed, there is nothing more to do here
  175. Fl::check();
  176. for (;;) sleep(1);
  177. }
  178. for (;;)
  179. {
  180. if (!Fl::check()) break;
  181. synth->UpdatePluginGUIs(); // deletes any if necc
  182. usleep(10000);
  183. gui_watchdog_check=1;
  184. }
  185. //pthread_cancel(loopthread);
  186. delete synth;
  187. return 1;
  188. }
  189. // nicked from Paul Barton-Davis' Ardour code :)
  190. int pthread_create_realtime (pthread_t *new_thread,
  191. void *(*start)(void *), void *arg,
  192. int priority)
  193. {
  194. pthread_attr_t *rt_attributes;
  195. struct sched_param *rt_param;
  196. int retval;
  197. rt_attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  198. rt_param = (struct sched_param *) malloc (sizeof (struct sched_param));
  199. pthread_attr_init (rt_attributes);
  200. if (seteuid (0)) {
  201. cerr << "Cannot obtain root UID for RT scheduling operations"
  202. << endl;
  203. return -1;
  204. } else {
  205. if (pthread_attr_setschedpolicy (rt_attributes, SCHED_FIFO)) {
  206. cerr << "Cannot set FIFO scheduling attributes for RT thread" << endl;
  207. }
  208. if (pthread_attr_setscope (rt_attributes, PTHREAD_SCOPE_SYSTEM)) {
  209. cerr << "Cannot set scheduling scope for RT thread" << endl;
  210. }
  211. rt_param->sched_priority = priority;
  212. if (pthread_attr_setschedparam (rt_attributes, rt_param)) {
  213. cerr << "Cannot set scheduling priority for RT thread" << endl;
  214. }
  215. }
  216. retval = pthread_create (new_thread, rt_attributes, start, arg);
  217. seteuid (getuid());
  218. return retval;
  219. }