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.

186 lines
3.6KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Nio.cpp - IO Wrapper Layer
  4. Copyright (C) 2016 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #include "Nio.h"
  11. #include "OutMgr.h"
  12. #include "InMgr.h"
  13. #include "EngineMgr.h"
  14. #include "MidiIn.h"
  15. #include "AudioOut.h"
  16. #include "WavEngine.h"
  17. #include "../Misc/Config.h"
  18. #include <cstring>
  19. #include <iostream>
  20. #include <algorithm>
  21. using std::string;
  22. using std::set;
  23. using std::cerr;
  24. using std::endl;
  25. #ifndef IN_DEFAULT
  26. #define IN_DEFAULT "NULL"
  27. #endif
  28. #ifndef OUT_DEFAULT
  29. #define OUT_DEFAULT "NULL"
  30. #endif
  31. InMgr *in = NULL;
  32. OutMgr *out = NULL;
  33. EngineMgr *eng = NULL;
  34. string postfix;
  35. bool Nio::autoConnect = false;
  36. bool Nio::pidInClientName = false;
  37. string Nio::defaultSource = IN_DEFAULT;
  38. string Nio::defaultSink = OUT_DEFAULT;
  39. void Nio::init(const SYNTH_T &synth, const oss_devs_t& oss_devs,
  40. class Master *master)
  41. {
  42. in = &InMgr::getInstance(); //Enable input wrapper
  43. out = &OutMgr::getInstance(&synth); //Initialize the Output Systems
  44. eng = &EngineMgr::getInstance(&synth, &oss_devs); //Initialize the Engines
  45. in->setMaster(master);
  46. out->setMaster(master);
  47. }
  48. bool Nio::start()
  49. {
  50. if(eng)
  51. return eng->start();
  52. else
  53. return false;
  54. }
  55. void Nio::stop()
  56. {
  57. if(eng)
  58. eng->stop();
  59. }
  60. void Nio::setDefaultSource(string name)
  61. {
  62. std::transform(name.begin(), name.end(), name.begin(), ::toupper);
  63. defaultSource = name;
  64. }
  65. void Nio::setDefaultSink(string name)
  66. {
  67. std::transform(name.begin(), name.end(), name.begin(), ::toupper);
  68. defaultSink = name;
  69. }
  70. bool Nio::setSource(string name)
  71. {
  72. return in->setSource(name);
  73. }
  74. bool Nio::setSink(string name)
  75. {
  76. return out->setSink(name);
  77. }
  78. void Nio::setPostfix(std::string post)
  79. {
  80. postfix = post;
  81. }
  82. std::string Nio::getPostfix(void)
  83. {
  84. return postfix;
  85. }
  86. set<string> Nio::getSources(void)
  87. {
  88. set<string> sources;
  89. for(std::list<Engine *>::iterator itr = eng->engines.begin();
  90. itr != eng->engines.end(); ++itr)
  91. if(dynamic_cast<MidiIn *>(*itr))
  92. sources.insert((*itr)->name);
  93. return sources;
  94. }
  95. set<string> Nio::getSinks(void)
  96. {
  97. set<string> sinks;
  98. for(std::list<Engine *>::iterator itr = eng->engines.begin();
  99. itr != eng->engines.end(); ++itr)
  100. if(dynamic_cast<AudioOut *>(*itr))
  101. sinks.insert((*itr)->name);
  102. return sinks;
  103. }
  104. string Nio::getSource()
  105. {
  106. return in->getSource();
  107. }
  108. string Nio::getSink()
  109. {
  110. return out->getSink();
  111. }
  112. #if JACK
  113. #include <jack/jack.h>
  114. void Nio::preferedSampleRate(unsigned &rate)
  115. {
  116. #if __linux__
  117. //avoid checking in with jack if it's off
  118. FILE *ps = popen("ps aux", "r");
  119. char buffer[4096];
  120. while(fgets(buffer, sizeof(buffer), ps))
  121. if(strstr(buffer, "jack"))
  122. break;
  123. fclose(ps);
  124. if(!strstr(buffer, "jack"))
  125. return;
  126. #endif
  127. jack_client_t *client = jack_client_open("temp-client",
  128. JackNoStartServer, 0);
  129. if(client) {
  130. rate = jack_get_sample_rate(client);
  131. jack_client_close(client);
  132. }
  133. }
  134. #else
  135. void Nio::preferedSampleRate(unsigned &)
  136. {}
  137. #endif
  138. void Nio::masterSwap(Master *master)
  139. {
  140. in->setMaster(master);
  141. out->setMaster(master);
  142. }
  143. void Nio::waveNew(class WavFile *wave)
  144. {
  145. out->wave->newFile(wave);
  146. }
  147. void Nio::waveStart(void)
  148. {
  149. out->wave->Start();
  150. }
  151. void Nio::waveStop(void)
  152. {
  153. out->wave->Stop();
  154. }
  155. void Nio::waveEnd(void)
  156. {
  157. out->wave->destroyFile();
  158. }