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.

Nio.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace zyncarla {
  22. using std::string;
  23. using std::set;
  24. using std::cerr;
  25. using std::endl;
  26. #ifndef IN_DEFAULT
  27. #define IN_DEFAULT "NULL"
  28. #endif
  29. #ifndef OUT_DEFAULT
  30. #define OUT_DEFAULT "NULL"
  31. #endif
  32. InMgr *in = NULL;
  33. OutMgr *out = NULL;
  34. EngineMgr *eng = NULL;
  35. string postfix;
  36. bool Nio::autoConnect = false;
  37. bool Nio::pidInClientName = false;
  38. string Nio::defaultSource = IN_DEFAULT;
  39. string Nio::defaultSink = OUT_DEFAULT;
  40. void Nio::init(const SYNTH_T &synth, const oss_devs_t& oss_devs,
  41. class Master *master)
  42. {
  43. in = &InMgr::getInstance(); //Enable input wrapper
  44. out = &OutMgr::getInstance(&synth); //Initialize the Output Systems
  45. eng = &EngineMgr::getInstance(&synth, &oss_devs); //Initialize the Engines
  46. in->setMaster(master);
  47. out->setMaster(master);
  48. }
  49. bool Nio::start()
  50. {
  51. if(eng)
  52. return eng->start();
  53. else
  54. return false;
  55. }
  56. void Nio::stop()
  57. {
  58. if(eng)
  59. eng->stop();
  60. }
  61. void Nio::setDefaultSource(string name)
  62. {
  63. std::transform(name.begin(), name.end(), name.begin(), ::toupper);
  64. defaultSource = name;
  65. }
  66. void Nio::setDefaultSink(string name)
  67. {
  68. std::transform(name.begin(), name.end(), name.begin(), ::toupper);
  69. defaultSink = name;
  70. }
  71. bool Nio::setSource(string name)
  72. {
  73. return in->setSource(name);
  74. }
  75. bool Nio::setSink(string name)
  76. {
  77. return out->setSink(name);
  78. }
  79. void Nio::setPostfix(std::string post)
  80. {
  81. postfix = post;
  82. }
  83. std::string Nio::getPostfix(void)
  84. {
  85. return postfix;
  86. }
  87. set<string> Nio::getSources(void)
  88. {
  89. set<string> sources;
  90. for(std::list<Engine *>::iterator itr = eng->engines.begin();
  91. itr != eng->engines.end(); ++itr)
  92. if(dynamic_cast<MidiIn *>(*itr))
  93. sources.insert((*itr)->name);
  94. return sources;
  95. }
  96. set<string> Nio::getSinks(void)
  97. {
  98. set<string> sinks;
  99. for(std::list<Engine *>::iterator itr = eng->engines.begin();
  100. itr != eng->engines.end(); ++itr)
  101. if(dynamic_cast<AudioOut *>(*itr))
  102. sinks.insert((*itr)->name);
  103. return sinks;
  104. }
  105. string Nio::getSource()
  106. {
  107. return in->getSource();
  108. }
  109. string Nio::getSink()
  110. {
  111. return out->getSink();
  112. }
  113. #if JACK
  114. #include <jack/jack.h>
  115. void Nio::preferedSampleRate(unsigned &rate)
  116. {
  117. #if __linux__
  118. //avoid checking in with jack if it's off
  119. FILE *ps = popen("ps aux", "r");
  120. char buffer[4096];
  121. while(fgets(buffer, sizeof(buffer), ps))
  122. if(strstr(buffer, "jack"))
  123. break;
  124. fclose(ps);
  125. if(!strstr(buffer, "jack"))
  126. return;
  127. #endif
  128. jack_client_t *client = jack_client_open("temp-client",
  129. JackNoStartServer, 0);
  130. if(client) {
  131. rate = jack_get_sample_rate(client);
  132. jack_client_close(client);
  133. }
  134. }
  135. #else
  136. void Nio::preferedSampleRate(unsigned &)
  137. {}
  138. #endif
  139. void Nio::masterSwap(Master *master)
  140. {
  141. in->setMaster(master);
  142. out->setMaster(master);
  143. }
  144. void Nio::waveNew(class WavFile *wave)
  145. {
  146. out->wave->newFile(wave);
  147. }
  148. void Nio::waveStart(void)
  149. {
  150. out->wave->Start();
  151. }
  152. void Nio::waveStop(void)
  153. {
  154. out->wave->Stop();
  155. }
  156. void Nio::waveEnd(void)
  157. {
  158. out->wave->destroyFile();
  159. }
  160. }