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.

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