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.

303 lines
8.9KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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 "CarlaEngineClient.hpp"
  18. #include "CarlaEngineUtils.hpp"
  19. #include "CarlaString.hpp"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------------------------------------
  22. // Carla Engine Client
  23. CarlaEngineClient::CarlaEngineClient(ProtectedData* const p)
  24. : pData(p)
  25. {
  26. carla_debug("CarlaEngineClient::CarlaEngineClient()");
  27. }
  28. CarlaEngineClient::~CarlaEngineClient() noexcept
  29. {
  30. carla_debug("CarlaEngineClient::~CarlaEngineClient()");
  31. }
  32. void CarlaEngineClient::activate() noexcept
  33. {
  34. CARLA_SAFE_ASSERT(! pData->active);
  35. carla_debug("CarlaEngineClient::activate()");
  36. pData->active = true;
  37. }
  38. void CarlaEngineClient::deactivate() noexcept
  39. {
  40. CARLA_SAFE_ASSERT(pData->active);
  41. carla_debug("CarlaEngineClient::deactivate()");
  42. pData->active = false;
  43. }
  44. bool CarlaEngineClient::isActive() const noexcept
  45. {
  46. return pData->active;
  47. }
  48. bool CarlaEngineClient::isOk() const noexcept
  49. {
  50. return true;
  51. }
  52. uint32_t CarlaEngineClient::getLatency() const noexcept
  53. {
  54. return pData->latency;
  55. }
  56. void CarlaEngineClient::setLatency(const uint32_t samples) noexcept
  57. {
  58. pData->latency = samples;
  59. }
  60. CarlaEnginePort* CarlaEngineClient::addPort(const EnginePortType portType, const char* const name, const bool isInput, const uint32_t indexOffset)
  61. {
  62. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  63. carla_debug("CarlaEngineClient::addPort(%i:%s, \"%s\", %s, %u)", portType, EnginePortType2Str(portType), name, bool2str(isInput), indexOffset);
  64. switch (portType)
  65. {
  66. case kEnginePortTypeNull:
  67. break;
  68. case kEnginePortTypeAudio:
  69. _addAudioPortName(isInput, name);
  70. return new CarlaEngineAudioPort(*this, isInput, indexOffset);
  71. case kEnginePortTypeCV:
  72. _addCVPortName(isInput, name);
  73. return new CarlaEngineCVPort(*this, isInput, indexOffset);
  74. case kEnginePortTypeEvent:
  75. _addEventPortName(isInput, name);
  76. return new CarlaEngineEventPort(*this, isInput, indexOffset);
  77. }
  78. carla_stderr("CarlaEngineClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  79. return nullptr;
  80. }
  81. bool CarlaEngineClient::removePort(const EnginePortType portType, const char* const name, const bool isInput)
  82. {
  83. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', false);
  84. carla_debug("CarlaEngineClient::removePort(%i:%s, \"%s\", %s)", portType, EnginePortType2Str(portType), name, bool2str(isInput));
  85. switch (portType)
  86. {
  87. case kEnginePortTypeNull:
  88. break;
  89. case kEnginePortTypeAudio: {
  90. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  91. portList.append(name);
  92. return portList.removeOne(name);
  93. }
  94. case kEnginePortTypeCV: {
  95. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  96. return portList.removeOne(name);
  97. }
  98. case kEnginePortTypeEvent: {
  99. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  100. return portList.removeOne(name);
  101. }
  102. }
  103. return false;
  104. }
  105. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  106. CarlaEngineCVSourcePorts* CarlaEngineClient::createCVSourcePorts()
  107. {
  108. pData->cvSourcePorts.setGraphAndPlugin(pData->egraph.getPatchbayGraph(), pData->plugin);
  109. return &pData->cvSourcePorts;
  110. }
  111. #endif
  112. const CarlaEngine& CarlaEngineClient::getEngine() const noexcept
  113. {
  114. return pData->engine;
  115. }
  116. EngineProcessMode CarlaEngineClient::getProcessMode() const noexcept
  117. {
  118. return pData->engine.getProccessMode();
  119. }
  120. uint CarlaEngineClient::getPortCount(const EnginePortType portType, const bool isInput) const noexcept
  121. {
  122. size_t ret = 0;
  123. switch (portType)
  124. {
  125. case kEnginePortTypeNull:
  126. break;
  127. case kEnginePortTypeAudio:
  128. ret = isInput ? pData->audioInList.count() : pData->audioOutList.count();
  129. break;
  130. case kEnginePortTypeCV:
  131. ret = isInput ? pData->cvInList.count() : pData->cvOutList.count();
  132. break;
  133. case kEnginePortTypeEvent:
  134. ret = isInput ? pData->eventInList.count() : pData->eventOutList.count();
  135. break;
  136. }
  137. return static_cast<uint>(ret);
  138. }
  139. const char* CarlaEngineClient::getAudioPortName(const bool isInput, const uint index) const noexcept
  140. {
  141. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  142. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  143. return portList.getAt(index);
  144. }
  145. const char* CarlaEngineClient::getCVPortName(const bool isInput, const uint index) const noexcept
  146. {
  147. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  148. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  149. return portList.getAt(index);
  150. }
  151. const char* CarlaEngineClient::getEventPortName(const bool isInput, const uint index) const noexcept
  152. {
  153. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  154. CARLA_SAFE_ASSERT_RETURN(index < portList.count(), nullptr);
  155. return portList.getAt(index);
  156. }
  157. void CarlaEngineClient::_addAudioPortName(const bool isInput, const char* const name)
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  160. CarlaStringList& portList(isInput ? pData->audioInList : pData->audioOutList);
  161. portList.append(name);
  162. }
  163. void CarlaEngineClient::_addCVPortName(const bool isInput, const char* const name)
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  166. CarlaStringList& portList(isInput ? pData->cvInList : pData->cvOutList);
  167. portList.append(name);
  168. }
  169. void CarlaEngineClient::_addEventPortName(const bool isInput, const char* const name)
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  172. CarlaStringList& portList(isInput ? pData->eventInList : pData->eventOutList);
  173. portList.append(name);
  174. }
  175. static void getUniquePortName(CarlaString& sname, const CarlaStringList& list)
  176. {
  177. for (CarlaStringList::Itenerator it = list.begin2(); it.valid(); it.next())
  178. {
  179. const char* const portName(it.getValue(nullptr));
  180. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  181. // Check if unique name doesn't exist
  182. if (sname != portName)
  183. continue;
  184. // Check if string has already been modified
  185. {
  186. const std::size_t len(sname.length());
  187. // 1 digit, ex: " (2)"
  188. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  189. {
  190. const int number = sname[len-2] - '0';
  191. if (number == 9)
  192. {
  193. // next number is 10, 2 digits
  194. sname.truncate(len-4);
  195. sname += " (10)";
  196. //sname.replace(" (9)", " (10)");
  197. }
  198. else
  199. sname[len-2] = char('0' + number + 1);
  200. continue;
  201. }
  202. // 2 digits, ex: " (11)"
  203. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  204. {
  205. char n2 = sname[len-2];
  206. char n3 = sname[len-3];
  207. if (n2 == '9')
  208. {
  209. n2 = '0';
  210. n3 = static_cast<char>(n3 + 1);
  211. }
  212. else
  213. n2 = static_cast<char>(n2 + 1);
  214. sname[len-2] = n2;
  215. sname[len-3] = n3;
  216. continue;
  217. }
  218. }
  219. // Modify string if not
  220. sname += " (2)";
  221. }
  222. }
  223. const char* CarlaEngineClient::_getUniquePortName(const char* const name)
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  226. CarlaString sname;
  227. sname = name;
  228. getUniquePortName(sname, pData->audioInList);
  229. getUniquePortName(sname, pData->audioOutList);
  230. getUniquePortName(sname, pData->cvInList);
  231. getUniquePortName(sname, pData->cvOutList);
  232. getUniquePortName(sname, pData->eventInList);
  233. getUniquePortName(sname, pData->eventOutList);
  234. return sname.dup();
  235. }
  236. void CarlaEngineClient::_clearPorts()
  237. {
  238. pData->audioInList.clear();
  239. pData->audioOutList.clear();
  240. pData->cvInList.clear();
  241. pData->cvOutList.clear();
  242. pData->eventInList.clear();
  243. pData->eventOutList.clear();
  244. }
  245. // -----------------------------------------------------------------------
  246. CARLA_BACKEND_END_NAMESPACE