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.

312 lines
8.9KB

  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. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  109. CarlaEngineCVSourcePorts* CarlaEngineClient::createCVSourcePorts()
  110. {
  111. // AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  112. // CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  113. // pData->engine.pData->graph;
  114. return new CarlaEngineCVSourcePorts();
  115. }
  116. #endif
  117. const CarlaEngine& CarlaEngineClient::getEngine() const noexcept
  118. {
  119. return pData->engine;
  120. }
  121. EngineProcessMode CarlaEngineClient::getProcessMode() const noexcept
  122. {
  123. return pData->engine.getProccessMode();
  124. }
  125. uint CarlaEngineClient::getPortCount(const EnginePortType portType, const bool isInput) const noexcept
  126. {
  127. size_t ret = 0;
  128. switch (portType)
  129. {
  130. case kEnginePortTypeNull:
  131. break;
  132. case kEnginePortTypeAudio:
  133. ret = isInput ? pData->audioInList.count() : pData->audioOutList.count();
  134. break;
  135. case kEnginePortTypeCV:
  136. ret = isInput ? pData->cvInList.count() : pData->cvOutList.count();
  137. break;
  138. case kEnginePortTypeEvent:
  139. ret = isInput ? pData->eventInList.count() : pData->eventOutList.count();
  140. break;
  141. }
  142. return static_cast<uint>(ret);
  143. }
  144. const char* CarlaEngineClient::getAudioPortName(const bool isInput, const uint index) const noexcept
  145. {
  146. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  147. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  148. return portList.getAt(index);
  149. }
  150. const char* CarlaEngineClient::getCVPortName(const bool isInput, const uint index) const noexcept
  151. {
  152. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  153. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  154. return portList.getAt(index);
  155. }
  156. const char* CarlaEngineClient::getEventPortName(const bool isInput, const uint index) const noexcept
  157. {
  158. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  159. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  160. return portList.getAt(index);
  161. }
  162. void CarlaEngineClient::_addAudioPortName(const bool isInput, const char* const name)
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  165. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  166. portList.append(name);
  167. }
  168. void CarlaEngineClient::_addCVPortName(const bool isInput, const char* const name)
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  171. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  172. portList.append(name);
  173. }
  174. void CarlaEngineClient::_addEventPortName(const bool isInput, const char* const name)
  175. {
  176. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  177. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  178. portList.append(name);
  179. }
  180. static void getUniquePortName(CarlaString& sname, const CarlaStringList& list)
  181. {
  182. for (CarlaStringList::Itenerator it = list.begin2(); it.valid(); it.next())
  183. {
  184. const char* const portName(it.getValue(nullptr));
  185. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  186. // Check if unique name doesn't exist
  187. if (sname != portName)
  188. continue;
  189. // Check if string has already been modified
  190. {
  191. const std::size_t len(sname.length());
  192. // 1 digit, ex: " (2)"
  193. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  194. {
  195. const int number = sname[len-2] - '0';
  196. if (number == 9)
  197. {
  198. // next number is 10, 2 digits
  199. sname.truncate(len-4);
  200. sname += " (10)";
  201. //sname.replace(" (9)", " (10)");
  202. }
  203. else
  204. sname[len-2] = char('0' + number + 1);
  205. continue;
  206. }
  207. // 2 digits, ex: " (11)"
  208. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  209. {
  210. char n2 = sname[len-2];
  211. char n3 = sname[len-3];
  212. if (n2 == '9')
  213. {
  214. n2 = '0';
  215. n3 = static_cast<char>(n3 + 1);
  216. }
  217. else
  218. n2 = static_cast<char>(n2 + 1);
  219. sname[len-2] = n2;
  220. sname[len-3] = n3;
  221. continue;
  222. }
  223. }
  224. // Modify string if not
  225. sname += " (2)";
  226. }
  227. }
  228. const char* CarlaEngineClient::_getUniquePortName(const char* const name)
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  231. CarlaString sname;
  232. sname = name;
  233. getUniquePortName(sname, pData->audioInList);
  234. getUniquePortName(sname, pData->audioOutList);
  235. getUniquePortName(sname, pData->cvInList);
  236. getUniquePortName(sname, pData->cvOutList);
  237. getUniquePortName(sname, pData->eventInList);
  238. getUniquePortName(sname, pData->eventOutList);
  239. return sname.dup();
  240. }
  241. void CarlaEngineClient::_clearPorts()
  242. {
  243. pData->audioInList.clear();
  244. pData->audioOutList.clear();
  245. pData->cvInList.clear();
  246. pData->cvOutList.clear();
  247. pData->eventInList.clear();
  248. pData->eventOutList.clear();
  249. }
  250. // -----------------------------------------------------------------------
  251. CARLA_BACKEND_END_NAMESPACE