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.

EngineMgr.cpp 3.9KB

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