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.

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