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.

193 lines
4.8KB

  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 "SpiralSynthModular.h"
  27. pthread_t loopthread;
  28. SynthModular *synth;
  29. int pthread_create_realtime (pthread_t *new_thread,
  30. void *(*start)(void *), void *arg,
  31. int priority);
  32. bool CallbackOnly = false;
  33. void audioloop(void* o)
  34. {
  35. while(1)
  36. {
  37. if (!synth->CallbackMode())
  38. {
  39. // do funky stuff
  40. synth->Update();
  41. // slow down this thread if we are not going to be using the
  42. // oss plugin. prevents maxing the CPU out for no reason.
  43. if (CallbackOnly) usleep(100);
  44. }
  45. else
  46. {
  47. // the engine is currently in callback mode, so we don't
  48. // need to do anything unless we are switched back
  49. usleep(1000000);
  50. }
  51. }
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. srand(time(NULL));
  56. SpiralSynthModularInfo::Get()->LoadPrefs();
  57. bool GUI = true;
  58. bool FIFO = false;
  59. // get args
  60. string cmd_filename="";
  61. bool cmd_specd = false;
  62. // parse the args
  63. if (argc>1)
  64. {
  65. for (int a=1; a<argc; a++)
  66. {
  67. if (!strcmp(argv[a],"--NoGUI")) GUI = false;
  68. else if (!strcmp(argv[a],"--SHED_FIFO")) FIFO = true;
  69. else if (!strcmp(argv[a],"--CallbackOnly")) CallbackOnly = true;
  70. else if (!strcmp(argv[a],"-h"))
  71. {
  72. cout<<"usage: spiralsynthmodular [options] [patch.ssm]"<<endl<<endl
  73. <<"options list"<<endl
  74. <<"-h : help"<<endl
  75. <<"-v : print version"<<endl
  76. <<"--NoGUI : run without GUI (only useful when loading patch from command line"<<endl
  77. <<"--SHED_FIFO : spawn audio thread with FIFO sheduling (run as root)"<<endl
  78. <<"--CallbackOnly : prevents 100% cpu usage when idle, but makes OSS output unsable"<<endl;
  79. exit(0);
  80. }
  81. else if (!strcmp(argv[a],"-v"))
  82. {
  83. cout<<VER_STRING<<endl; exit(0);
  84. }
  85. else
  86. {
  87. cmd_filename = argv[1];
  88. cmd_specd = true;
  89. }
  90. }
  91. }
  92. // set some fltk defaults
  93. Fl_Tooltip::size(10);
  94. Fl::visible_focus(false);
  95. //Fl::set_font(FL_HELVETICA,FL_SCREEN);
  96. Fl::visual(FL_DOUBLE|FL_RGB);
  97. synth=new SynthModular;
  98. // setup the synth
  99. Fl_Window* win = synth->CreateWindow();
  100. synth->LoadPlugins();
  101. win->xclass("");
  102. if (GUI) win->show(1, argv); // prevents stuff happening before the plugins have loaded
  103. // spawn the audio thread
  104. int ret;
  105. if (FIFO) ret=pthread_create_realtime(&loopthread,(void*(*)(void*))audioloop,NULL,10);
  106. else ret=pthread_create(&loopthread,NULL,(void*(*)(void*))audioloop,NULL);
  107. pthread_t GUIThread = pthread_self();
  108. // do we need to load a patch on startup?
  109. if (cmd_specd) synth->LoadPatch(cmd_filename.c_str());
  110. if (!GUI)
  111. {
  112. // if there is no gui needed, there is nothing more to do here
  113. Fl::check();
  114. for (;;) sleep(1);
  115. }
  116. for (;;)
  117. {
  118. if (!Fl::check()) break;
  119. synth->UpdatePluginGUIs(); // deletes any if necc
  120. usleep(10000);
  121. }
  122. //pthread_cancel(loopthread);
  123. delete synth;
  124. return 1;
  125. }
  126. // nicked from Paul Barton-Davis' Ardour code :)
  127. int pthread_create_realtime (pthread_t *new_thread,
  128. void *(*start)(void *), void *arg,
  129. int priority)
  130. {
  131. pthread_attr_t *rt_attributes;
  132. struct sched_param *rt_param;
  133. int retval;
  134. rt_attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  135. rt_param = (struct sched_param *) malloc (sizeof (struct sched_param));
  136. pthread_attr_init (rt_attributes);
  137. if (seteuid (0)) {
  138. cerr << "Cannot obtain root UID for RT scheduling operations"
  139. << endl;
  140. return -1;
  141. } else {
  142. if (pthread_attr_setschedpolicy (rt_attributes, SCHED_FIFO)) {
  143. cerr << "Cannot set FIFO scheduling attributes for RT thread" << endl;
  144. }
  145. if (pthread_attr_setscope (rt_attributes, PTHREAD_SCOPE_SYSTEM)) {
  146. cerr << "Cannot set scheduling scope for RT thread" << endl;
  147. }
  148. rt_param->sched_priority = priority;
  149. if (pthread_attr_setschedparam (rt_attributes, rt_param)) {
  150. cerr << "Cannot set scheduling priority for RT thread" << endl;
  151. }
  152. }
  153. retval = pthread_create (new_thread, rt_attributes, start, arg);
  154. seteuid (getuid());
  155. return retval;
  156. }