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.

301 lines
8.5KB

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