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.

177 lines
4.2KB

  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
  71. {
  72. cmd_filename = argv[1];
  73. cmd_specd = true;
  74. }
  75. }
  76. }
  77. Fl::visual(FL_DOUBLE|FL_RGB);
  78. synth=new SynthModular;
  79. // setup the synth
  80. Fl_Window* win = synth->CreateWindow();
  81. synth->LoadPlugins();
  82. win->xclass("");
  83. if (GUI) win->show(1, argv); // prevents stuff happening before the plugins have loaded
  84. // set some fltk defaults
  85. Fl_Tooltip::size(10);
  86. Fl::visible_focus(false);
  87. // spawn the audio thread
  88. int ret;
  89. if (FIFO) ret=pthread_create_realtime(&loopthread,(void*(*)(void*))audioloop,NULL,10);
  90. else ret=pthread_create(&loopthread,NULL,(void*(*)(void*))audioloop,NULL);
  91. pthread_t GUIThread = pthread_self();
  92. // do we need to load a patch on startup?
  93. if (cmd_specd) synth->LoadPatch(cmd_filename.c_str());
  94. if (!GUI)
  95. {
  96. // if there is no gui needed, there is nothing more to do here
  97. Fl::check();
  98. for (;;) sleep(1);
  99. }
  100. for (;;)
  101. {
  102. if (!Fl::check()) break;
  103. synth->UpdatePluginGUIs(); // deletes any if necc
  104. usleep(10000);
  105. }
  106. //pthread_cancel(loopthread);
  107. delete synth;
  108. return 1;
  109. }
  110. // nicked from Paul Barton-Davis' Ardour code :)
  111. int pthread_create_realtime (pthread_t *new_thread,
  112. void *(*start)(void *), void *arg,
  113. int priority)
  114. {
  115. pthread_attr_t *rt_attributes;
  116. struct sched_param *rt_param;
  117. int retval;
  118. rt_attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  119. rt_param = (struct sched_param *) malloc (sizeof (struct sched_param));
  120. pthread_attr_init (rt_attributes);
  121. if (seteuid (0)) {
  122. cerr << "Cannot obtain root UID for RT scheduling operations"
  123. << endl;
  124. return -1;
  125. } else {
  126. if (pthread_attr_setschedpolicy (rt_attributes, SCHED_FIFO)) {
  127. cerr << "Cannot set FIFO scheduling attributes for RT thread" << endl;
  128. }
  129. if (pthread_attr_setscope (rt_attributes, PTHREAD_SCOPE_SYSTEM)) {
  130. cerr << "Cannot set scheduling scope for RT thread" << endl;
  131. }
  132. rt_param->sched_priority = priority;
  133. if (pthread_attr_setschedparam (rt_attributes, rt_param)) {
  134. cerr << "Cannot set scheduling priority for RT thread" << endl;
  135. }
  136. }
  137. retval = pthread_create (new_thread, rt_attributes, start, arg);
  138. seteuid (getuid());
  139. return retval;
  140. }