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.

241 lines
5.9KB

  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. int main(int argc, char **argv)
  87. {
  88. srand(time(NULL));
  89. SpiralSynthModularInfo::Get()->LoadPrefs();
  90. // get args
  91. string cmd_filename="";
  92. bool cmd_specd = false;
  93. string cmd_pluginPath="";
  94. // parse the args
  95. if (argc>1)
  96. {
  97. for (int a=1; a<argc; a++)
  98. {
  99. if (!strcmp(argv[a],"--NoGUI")) GUI = false;
  100. else if (!strcmp(argv[a],"--Realtime")) FIFO = true;
  101. else if (!strcmp(argv[a],"-h"))
  102. {
  103. cout<<"usage: spiralsynthmodular [patch.ssm] [options]"<<endl<<endl
  104. <<"options list"<<endl
  105. <<"-h : help"<<endl
  106. <<"-v : print version"<<endl
  107. <<"--NoGUI : run without GUI (only useful when loading patch from command line"<<endl
  108. <<"--Realtime : spawn audio thread with FIFO scheduling (run as root)"<<endl
  109. <<"--PluginPath <PATH> : look for plugins in the specified directory"<<endl;
  110. exit(0);
  111. }
  112. else if (!strcmp(argv[a],"-v"))
  113. {
  114. cout<<VER_STRING<<endl; exit(0);
  115. }
  116. else if (!strcmp(argv[a],"--PluginPath"))
  117. {
  118. a++;
  119. cmd_pluginPath=argv[a];
  120. }
  121. else
  122. {
  123. cmd_filename = argv[1];
  124. cmd_specd = true;
  125. }
  126. }
  127. }
  128. // set some fltk defaults
  129. Fl_Tooltip::size(10);
  130. Fl::visible_focus(false);
  131. Fl::visual(FL_DOUBLE|FL_RGB);
  132. synth=new SynthModular;
  133. // setup the synth
  134. Fl_Window* win = synth->CreateWindow();
  135. synth->LoadPlugins(cmd_pluginPath);
  136. win->xclass("");
  137. if (GUI) win->show(1, argv); // prevents stuff happening before the plugins have loaded
  138. // spawn the audio thread
  139. if (FIFO)
  140. {
  141. pthread_create_realtime(&watchdogthread,(void*(*)(void*))watchdog,NULL,sched_get_priority_max(SCHED_FIFO));
  142. pthread_create_realtime(&loopthread,(void*(*)(void*))audioloop,NULL,sched_get_priority_max(SCHED_FIFO)-1);
  143. }
  144. else
  145. {
  146. pthread_create(&loopthread,NULL,(void*(*)(void*))audioloop,NULL);
  147. // reduce the priority of the gui
  148. if (setpriority(PRIO_PROCESS,0,20)) cerr<<"Could not set priority for GUI thread"<<endl;
  149. }
  150. // do we need to load a patch on startup?
  151. if (cmd_specd) synth->LoadPatch(cmd_filename.c_str());
  152. if (!GUI)
  153. {
  154. // if there is no gui needed, there is nothing more to do here
  155. Fl::check();
  156. for (;;) sleep(1);
  157. }
  158. for (;;)
  159. {
  160. if (!Fl::check()) break;
  161. synth->UpdatePluginGUIs(); // deletes any if necc
  162. usleep(10000);
  163. gui_watchdog_check=1;
  164. }
  165. //pthread_cancel(loopthread);
  166. delete synth;
  167. return 1;
  168. }
  169. // nicked from Paul Barton-Davis' Ardour code :)
  170. int pthread_create_realtime (pthread_t *new_thread,
  171. void *(*start)(void *), void *arg,
  172. int priority)
  173. {
  174. pthread_attr_t *rt_attributes;
  175. struct sched_param *rt_param;
  176. int retval;
  177. rt_attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  178. rt_param = (struct sched_param *) malloc (sizeof (struct sched_param));
  179. pthread_attr_init (rt_attributes);
  180. if (seteuid (0)) {
  181. cerr << "Cannot obtain root UID for RT scheduling operations"
  182. << endl;
  183. return -1;
  184. } else {
  185. if (pthread_attr_setschedpolicy (rt_attributes, SCHED_FIFO)) {
  186. cerr << "Cannot set FIFO scheduling attributes for RT thread" << endl;
  187. }
  188. if (pthread_attr_setscope (rt_attributes, PTHREAD_SCOPE_SYSTEM)) {
  189. cerr << "Cannot set scheduling scope for RT thread" << endl;
  190. }
  191. rt_param->sched_priority = priority;
  192. if (pthread_attr_setschedparam (rt_attributes, rt_param)) {
  193. cerr << "Cannot set scheduling priority for RT thread" << endl;
  194. }
  195. }
  196. retval = pthread_create (new_thread, rt_attributes, start, arg);
  197. seteuid (getuid());
  198. return retval;
  199. }