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.

CarlaEngineClient.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaEngineUtils.hpp"
  18. #include "CarlaString.hpp"
  19. #include "CarlaStringList.hpp"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------------------------------------
  22. // Carla Engine client (Abstract)
  23. struct CarlaEngineClient::ProtectedData {
  24. const CarlaEngine& engine;
  25. bool active;
  26. uint32_t latency;
  27. CarlaStringList audioInList;
  28. CarlaStringList audioOutList;
  29. CarlaStringList cvInList;
  30. CarlaStringList cvOutList;
  31. CarlaStringList eventInList;
  32. CarlaStringList eventOutList;
  33. ProtectedData(const CarlaEngine& eng) noexcept
  34. : engine(eng),
  35. active(false),
  36. latency(0),
  37. audioInList(),
  38. audioOutList(),
  39. cvInList(),
  40. cvOutList(),
  41. eventInList(),
  42. eventOutList() {}
  43. #ifdef CARLA_PROPER_CPP11_SUPPORT
  44. ProtectedData() = delete;
  45. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  46. #endif
  47. };
  48. CarlaEngineClient::CarlaEngineClient(const CarlaEngine& engine)
  49. : pData(new ProtectedData(engine))
  50. {
  51. carla_debug("CarlaEngineClient::CarlaEngineClient()");
  52. }
  53. CarlaEngineClient::~CarlaEngineClient() noexcept
  54. {
  55. CARLA_SAFE_ASSERT(! pData->active);
  56. carla_debug("CarlaEngineClient::~CarlaEngineClient()");
  57. delete pData;
  58. }
  59. void CarlaEngineClient::activate() noexcept
  60. {
  61. CARLA_SAFE_ASSERT(! pData->active);
  62. carla_debug("CarlaEngineClient::activate()");
  63. pData->active = true;
  64. }
  65. void CarlaEngineClient::deactivate() noexcept
  66. {
  67. CARLA_SAFE_ASSERT(pData->active);
  68. carla_debug("CarlaEngineClient::deactivate()");
  69. pData->active = false;
  70. }
  71. bool CarlaEngineClient::isActive() const noexcept
  72. {
  73. return pData->active;
  74. }
  75. bool CarlaEngineClient::isOk() const noexcept
  76. {
  77. return true;
  78. }
  79. uint32_t CarlaEngineClient::getLatency() const noexcept
  80. {
  81. return pData->latency;
  82. }
  83. void CarlaEngineClient::setLatency(const uint32_t samples) noexcept
  84. {
  85. pData->latency = samples;
  86. }
  87. CarlaEnginePort* CarlaEngineClient::addPort(const EnginePortType portType, const char* const name, const bool isInput, const uint32_t indexOffset)
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  90. carla_debug("CarlaEngineClient::addPort(%i:%s, \"%s\", %s)", portType, EnginePortType2Str(portType), name, bool2str(isInput));
  91. switch (portType)
  92. {
  93. case kEnginePortTypeNull:
  94. break;
  95. case kEnginePortTypeAudio:
  96. _addAudioPortName(isInput, name);
  97. return new CarlaEngineAudioPort(*this, isInput, indexOffset);
  98. case kEnginePortTypeCV:
  99. _addCVPortName(isInput, name);
  100. return new CarlaEngineCVPort(*this, isInput, indexOffset);
  101. case kEnginePortTypeEvent:
  102. _addEventPortName(isInput, name);
  103. return new CarlaEngineEventPort(*this, isInput, indexOffset);
  104. }
  105. carla_stderr("CarlaEngineClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  106. return nullptr;
  107. }
  108. const CarlaEngine& CarlaEngineClient::getEngine() const noexcept
  109. {
  110. return pData->engine;
  111. }
  112. EngineProcessMode CarlaEngineClient::getProcessMode() const noexcept
  113. {
  114. return pData->engine.getProccessMode();
  115. }
  116. const char* CarlaEngineClient::getAudioPortName(const bool isInput, const uint index) const noexcept
  117. {
  118. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  119. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  120. return portList.getAt(index, nullptr);
  121. }
  122. const char* CarlaEngineClient::getCVPortName(const bool isInput, const uint index) const noexcept
  123. {
  124. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  125. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  126. return portList.getAt(index, nullptr);
  127. }
  128. const char* CarlaEngineClient::getEventPortName(const bool isInput, const uint index) const noexcept
  129. {
  130. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  131. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  132. return portList.getAt(index, nullptr);
  133. }
  134. void CarlaEngineClient::_addAudioPortName(const bool isInput, const char* const name)
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  137. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  138. portList.append(name);
  139. }
  140. void CarlaEngineClient::_addCVPortName(const bool isInput, const char* const name)
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  143. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  144. portList.append(name);
  145. }
  146. void CarlaEngineClient::_addEventPortName(const bool isInput, const char* const name)
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  149. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  150. portList.append(name);
  151. }
  152. static void getUniquePortName(CarlaString& sname, const CarlaStringList& list)
  153. {
  154. for (CarlaStringList::Itenerator it = list.begin2(); it.valid(); it.next())
  155. {
  156. const char* const portName(it.getValue(nullptr));
  157. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  158. // Check if unique name doesn't exist
  159. if (sname != portName)
  160. continue;
  161. // Check if string has already been modified
  162. {
  163. const std::size_t len(sname.length());
  164. // 1 digit, ex: " (2)"
  165. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  166. {
  167. const int number = sname[len-2] - '0';
  168. if (number == 9)
  169. {
  170. // next number is 10, 2 digits
  171. sname.truncate(len-4);
  172. sname += " (10)";
  173. //sname.replace(" (9)", " (10)");
  174. }
  175. else
  176. sname[len-2] = char('0' + number + 1);
  177. continue;
  178. }
  179. // 2 digits, ex: " (11)"
  180. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  181. {
  182. char n2 = sname[len-2];
  183. char n3 = sname[len-3];
  184. if (n2 == '9')
  185. {
  186. n2 = '0';
  187. n3 = static_cast<char>(n3 + 1);
  188. }
  189. else
  190. n2 = static_cast<char>(n2 + 1);
  191. sname[len-2] = n2;
  192. sname[len-3] = n3;
  193. continue;
  194. }
  195. }
  196. // Modify string if not
  197. sname += " (2)";
  198. }
  199. }
  200. const char* CarlaEngineClient::_getUniquePortName(const char* const name)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  203. CarlaString sname;
  204. sname = name;
  205. getUniquePortName(sname, pData->audioInList);
  206. getUniquePortName(sname, pData->audioOutList);
  207. getUniquePortName(sname, pData->cvInList);
  208. getUniquePortName(sname, pData->cvOutList);
  209. getUniquePortName(sname, pData->eventInList);
  210. getUniquePortName(sname, pData->eventOutList);
  211. return sname.dup();
  212. }
  213. void CarlaEngineClient::_clearPorts()
  214. {
  215. pData->audioInList.clear();
  216. pData->audioOutList.clear();
  217. pData->cvInList.clear();
  218. pData->cvOutList.clear();
  219. pData->eventInList.clear();
  220. pData->eventOutList.clear();
  221. }
  222. // -----------------------------------------------------------------------
  223. CARLA_BACKEND_END_NAMESPACE