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.

307 lines
8.7KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 GPL.txt file
  16. */
  17. #include "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInit.hpp"
  19. #include "CarlaEngineInternal.hpp"
  20. #include <ctime>
  21. #include <sys/time.h>
  22. CARLA_BACKEND_START_NAMESPACE
  23. // -------------------------------------------------------------------------------------------------------------------
  24. // Dummy Engine
  25. class CarlaEngineDummy : public CarlaEngine,
  26. public CarlaThread
  27. {
  28. public:
  29. CarlaEngineDummy()
  30. : CarlaEngine(),
  31. CarlaThread("CarlaEngineDummy"),
  32. fRunning(false)
  33. {
  34. carla_debug("CarlaEngineDummy::CarlaEngineDummy()");
  35. // just to make sure
  36. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  37. }
  38. ~CarlaEngineDummy() override
  39. {
  40. carla_debug("CarlaEngineDummy::~CarlaEngineDummy()");
  41. }
  42. // -------------------------------------
  43. bool init(const char* const clientName) override
  44. {
  45. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  46. carla_debug("CarlaEngineDummy::init(\"%s\")", clientName);
  47. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  48. {
  49. setLastError("Invalid process mode");
  50. return false;
  51. }
  52. fRunning = true;
  53. if (! pData->init(clientName))
  54. {
  55. close();
  56. setLastError("Failed to init internal data");
  57. return false;
  58. }
  59. pData->bufferSize = pData->options.audioBufferSize;
  60. pData->sampleRate = pData->options.audioSampleRate;
  61. pData->initTime(pData->options.transportExtra);
  62. pData->graph.create(2, 2, 0, 0);
  63. if (! startThread(true))
  64. {
  65. close();
  66. setLastError("Failed to start dummy audio thread");
  67. return false;
  68. }
  69. patchbayRefresh(true, false, false);
  70. callback(true, true,
  71. ENGINE_CALLBACK_ENGINE_STARTED,
  72. 0,
  73. pData->options.processMode,
  74. pData->options.transportMode,
  75. static_cast<int>(pData->bufferSize),
  76. static_cast<float>(pData->sampleRate),
  77. getCurrentDriverName());
  78. return true;
  79. }
  80. bool close() override
  81. {
  82. carla_debug("CarlaEngineDummy::close()");
  83. fRunning = false;
  84. stopThread(-1);
  85. CarlaEngine::close();
  86. pData->graph.destroy();
  87. return true;
  88. }
  89. bool isRunning() const noexcept override
  90. {
  91. return fRunning;
  92. }
  93. bool isOffline() const noexcept override
  94. {
  95. return false;
  96. }
  97. EngineType getType() const noexcept override
  98. {
  99. return kEngineTypeDummy;
  100. }
  101. const char* getCurrentDriverName() const noexcept override
  102. {
  103. return "Dummy";
  104. }
  105. // -------------------------------------------------------------------
  106. // Patchbay
  107. bool patchbayRefresh(const bool sendHost, const bool sendOSC, const bool) override
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  110. RackGraph* const graph = pData->graph.getRackGraph();
  111. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  112. ExternalGraph& extGraph(graph->extGraph);
  113. // ---------------------------------------------------------------
  114. // clear last ports
  115. extGraph.clear();
  116. // ---------------------------------------------------------------
  117. // fill in new ones
  118. {
  119. PortNameToId portNameToId;
  120. portNameToId.setData(kExternalGraphGroupAudioIn, 1, "capture_1", "");
  121. extGraph.audioPorts.ins.append(portNameToId);
  122. }
  123. {
  124. PortNameToId portNameToId;
  125. portNameToId.setData(kExternalGraphGroupAudioIn, 2, "capture_2", "");
  126. extGraph.audioPorts.ins.append(portNameToId);
  127. }
  128. {
  129. PortNameToId portNameToId;
  130. portNameToId.setData(kExternalGraphGroupAudioOut, 1, "playback_1", "");
  131. extGraph.audioPorts.outs.append(portNameToId);
  132. }
  133. {
  134. PortNameToId portNameToId;
  135. portNameToId.setData(kExternalGraphGroupAudioOut, 2, "playback_2", "");
  136. extGraph.audioPorts.outs.append(portNameToId);
  137. }
  138. // ---------------------------------------------------------------
  139. // now refresh
  140. if (sendHost || sendOSC)
  141. graph->refresh(sendHost, sendOSC, false, "Dummy");
  142. return true;
  143. }
  144. // -------------------------------------------------------------------
  145. protected:
  146. static int64_t getTimeInMicroseconds() noexcept
  147. {
  148. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  149. struct timeval tv;
  150. gettimeofday(&tv, nullptr);
  151. return (tv.tv_sec * 1000000) + tv.tv_usec;
  152. #else
  153. struct timespec ts;
  154. # ifdef CLOCK_MONOTONIC_RAW
  155. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  156. # else
  157. clock_gettime(CLOCK_MONOTONIC, &ts);
  158. # endif
  159. return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
  160. #endif
  161. }
  162. void run() override
  163. {
  164. const uint32_t bufferSize = pData->bufferSize;
  165. const int64_t cycleTime = static_cast<int64_t>(
  166. static_cast<double>(bufferSize) / pData->sampleRate * 1000000 + 0.5);
  167. int delay = 0;
  168. if (const char* const delaystr = std::getenv("CARLA_BRIDGE_DUMMY"))
  169. if ((delay = atoi(delaystr)) == 1)
  170. delay = 0;
  171. carla_stdout("CarlaEngineDummy audio thread started, cycle time: " P_INT64 "ms, delay %ds",
  172. cycleTime / 1000, delay);
  173. float* audioIns[2] = {
  174. (float*)std::malloc(sizeof(float)*bufferSize),
  175. (float*)std::malloc(sizeof(float)*bufferSize),
  176. };
  177. CARLA_SAFE_ASSERT_RETURN(audioIns[0] != nullptr,);
  178. CARLA_SAFE_ASSERT_RETURN(audioIns[1] != nullptr,);
  179. float* audioOuts[2] = {
  180. (float*)std::malloc(sizeof(float)*bufferSize),
  181. (float*)std::malloc(sizeof(float)*bufferSize),
  182. };
  183. CARLA_SAFE_ASSERT_RETURN(audioOuts[0] != nullptr,);
  184. CARLA_SAFE_ASSERT_RETURN(audioOuts[1] != nullptr,);
  185. carla_zeroFloats(audioIns[0], bufferSize);
  186. carla_zeroFloats(audioIns[1], bufferSize);
  187. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  188. int64_t oldTime, newTime;
  189. while (! shouldThreadExit())
  190. {
  191. if (delay > 0)
  192. carla_sleep(static_cast<uint>(delay));
  193. oldTime = getTimeInMicroseconds();
  194. const PendingRtEventsRunner prt(this, bufferSize, true);
  195. carla_zeroFloats(audioOuts[0], bufferSize);
  196. carla_zeroFloats(audioOuts[1], bufferSize);
  197. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  198. pData->graph.process(pData, audioIns, audioOuts, bufferSize);
  199. newTime = getTimeInMicroseconds();
  200. CARLA_SAFE_ASSERT_CONTINUE(newTime >= oldTime);
  201. const int64_t remainingTime = cycleTime - (newTime - oldTime);
  202. if (remainingTime <= 0)
  203. {
  204. ++pData->xruns;
  205. carla_stdout("XRUN! remaining time: " P_INT64 ", old: " P_INT64 ", new: " P_INT64 ")",
  206. remainingTime, oldTime, newTime);
  207. }
  208. else if (remainingTime >= 1000)
  209. {
  210. CARLA_SAFE_ASSERT_CONTINUE(remainingTime < 1000000); // 1 sec
  211. carla_msleep(static_cast<uint>(remainingTime / 1000));
  212. }
  213. }
  214. std::free(audioIns[0]);
  215. std::free(audioIns[1]);
  216. std::free(audioOuts[0]);
  217. std::free(audioOuts[1]);
  218. carla_stdout("CarlaEngineDummy audio thread finished with %u Xruns", pData->xruns);
  219. }
  220. // -------------------------------------------------------------------
  221. private:
  222. bool fRunning;
  223. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineDummy)
  224. };
  225. // -----------------------------------------
  226. namespace EngineInit {
  227. CarlaEngine* newDummy()
  228. {
  229. carla_debug("EngineInit::newDummy()");
  230. return new CarlaEngineDummy();
  231. }
  232. }
  233. // -----------------------------------------
  234. CARLA_BACKEND_END_NAMESPACE