Audio plugin host https://kx.studio/carla
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.

163 lines
4.2KB

  1. #include "EngineMgr.h"
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cassert>
  5. #include "Nio.h"
  6. #include "InMgr.h"
  7. #include "OutMgr.h"
  8. #include "AudioOut.h"
  9. #include "MidiIn.h"
  10. #include "NulEngine.h"
  11. #if OSS
  12. #include "OssEngine.h"
  13. #include "OssMultiEngine.h"
  14. #endif
  15. #if ALSA
  16. #include "AlsaEngine.h"
  17. #endif
  18. #if JACK
  19. #include "JackEngine.h"
  20. #include "JackMultiEngine.h"
  21. #endif
  22. #if PORTAUDIO
  23. #include "PaEngine.h"
  24. #endif
  25. using namespace std;
  26. EngineMgr &EngineMgr::getInstance(const SYNTH_T *synth)
  27. {
  28. static EngineMgr instance(synth);
  29. return instance;
  30. }
  31. EngineMgr::EngineMgr(const SYNTH_T *synth)
  32. {
  33. assert(synth);
  34. Engine *defaultEng = new NulEngine(*synth);
  35. //conditional compiling mess (but contained)
  36. engines.push_back(defaultEng);
  37. #if OSS
  38. engines.push_back(new OssEngine(*synth));
  39. engines.push_back(new OssMultiEngine(*synth));
  40. #endif
  41. #if ALSA
  42. engines.push_back(new AlsaEngine(*synth));
  43. #endif
  44. #if JACK
  45. engines.push_back(new JackEngine(*synth));
  46. engines.push_back(new JackMultiEngine(*synth));
  47. #endif
  48. #if PORTAUDIO
  49. engines.push_back(new PaEngine(*synth));
  50. #endif
  51. defaultOut = dynamic_cast<AudioOut *>(defaultEng);
  52. defaultIn = dynamic_cast<MidiIn *>(defaultEng);
  53. //Accept command line/compile time options
  54. if(!Nio::defaultSink.empty())
  55. setOutDefault(Nio::defaultSink);
  56. if(!Nio::defaultSource.empty())
  57. setInDefault(Nio::defaultSource);
  58. }
  59. EngineMgr::~EngineMgr()
  60. {
  61. for(list<Engine *>::iterator itr = engines.begin();
  62. itr != engines.end(); ++itr)
  63. delete *itr;
  64. }
  65. Engine *EngineMgr::getEng(string name)
  66. {
  67. transform(name.begin(), name.end(), name.begin(), ::toupper);
  68. for(list<Engine *>::iterator itr = engines.begin();
  69. itr != engines.end(); ++itr)
  70. if((*itr)->name == name)
  71. return *itr;
  72. return NULL;
  73. }
  74. bool EngineMgr::start()
  75. {
  76. bool expected = true;
  77. if(!(defaultOut && defaultIn)) {
  78. cerr << "ERROR: It looks like someone broke the Nio Output\n"
  79. << " Attempting to recover by defaulting to the\n"
  80. << " Null Engine." << endl;
  81. defaultOut = dynamic_cast<AudioOut *>(getEng("NULL"));
  82. defaultIn = dynamic_cast<MidiIn *>(getEng("NULL"));
  83. }
  84. OutMgr::getInstance(). currentOut = defaultOut;
  85. InMgr::getInstance(). current = defaultIn;
  86. //open up the default output(s)
  87. cout << "Starting Audio: " << defaultOut->name << endl;
  88. defaultOut->setAudioEn(true);
  89. if(defaultOut->getAudioEn())
  90. cout << "Audio Started" << endl;
  91. else {
  92. expected = false;
  93. cerr << "ERROR: The default audio output failed to open!" << endl;
  94. OutMgr::getInstance(). currentOut =
  95. dynamic_cast<AudioOut *>(getEng("NULL"));
  96. OutMgr::getInstance(). currentOut->setAudioEn(true);
  97. }
  98. cout << "Starting MIDI: " << defaultIn->name << endl;
  99. defaultIn->setMidiEn(true);
  100. if(defaultIn->getMidiEn())
  101. cout << "MIDI Started" << endl;
  102. else { //recover
  103. expected = false;
  104. cerr << "ERROR: The default MIDI input failed to open!" << endl;
  105. InMgr::getInstance(). current = dynamic_cast<MidiIn *>(getEng("NULL"));
  106. InMgr::getInstance(). current->setMidiEn(true);
  107. }
  108. //Show if expected drivers were booted
  109. return expected;
  110. }
  111. void EngineMgr::stop()
  112. {
  113. for(list<Engine *>::iterator itr = engines.begin();
  114. itr != engines.end(); ++itr)
  115. (*itr)->Stop();
  116. }
  117. bool EngineMgr::setInDefault(string name)
  118. {
  119. MidiIn *chosen;
  120. if((chosen = dynamic_cast<MidiIn *>(getEng(name)))) { //got the input
  121. defaultIn = chosen;
  122. return true;
  123. }
  124. //Warn user
  125. cerr << "Error: " << name << " is not a recognized MIDI input source"
  126. << endl;
  127. cerr << " Defaulting to the NULL input source" << endl;
  128. return false;
  129. }
  130. bool EngineMgr::setOutDefault(string name)
  131. {
  132. AudioOut *chosen;
  133. if((chosen = dynamic_cast<AudioOut *>(getEng(name)))) { //got the output
  134. defaultOut = chosen;
  135. return true;
  136. }
  137. //Warn user
  138. cerr << "Error: " << name << " is not a recognized audio backend" << endl;
  139. cerr << " Defaulting to the NULL audio backend" << endl;
  140. return false;
  141. }