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.8KB

  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())
  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 hasIdleOnMainThread() const noexcept override
  90. {
  91. return true;
  92. }
  93. bool isRunning() const noexcept override
  94. {
  95. return fRunning;
  96. }
  97. bool isOffline() const noexcept override
  98. {
  99. return false;
  100. }
  101. EngineType getType() const noexcept override
  102. {
  103. return kEngineTypeDummy;
  104. }
  105. const char* getCurrentDriverName() const noexcept override
  106. {
  107. return "Dummy";
  108. }
  109. // -------------------------------------------------------------------
  110. // Patchbay
  111. bool patchbayRefresh(const bool sendHost, const bool sendOSC, const bool) override
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  114. RackGraph* const graph = pData->graph.getRackGraph();
  115. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  116. ExternalGraph& extGraph(graph->extGraph);
  117. // ---------------------------------------------------------------
  118. // clear last ports
  119. extGraph.clear();
  120. // ---------------------------------------------------------------
  121. // fill in new ones
  122. {
  123. PortNameToId portNameToId;
  124. portNameToId.setData(kExternalGraphGroupAudioIn, 1, "capture_1", "");
  125. extGraph.audioPorts.ins.append(portNameToId);
  126. }
  127. {
  128. PortNameToId portNameToId;
  129. portNameToId.setData(kExternalGraphGroupAudioIn, 2, "capture_2", "");
  130. extGraph.audioPorts.ins.append(portNameToId);
  131. }
  132. {
  133. PortNameToId portNameToId;
  134. portNameToId.setData(kExternalGraphGroupAudioOut, 1, "playback_1", "");
  135. extGraph.audioPorts.outs.append(portNameToId);
  136. }
  137. {
  138. PortNameToId portNameToId;
  139. portNameToId.setData(kExternalGraphGroupAudioOut, 2, "playback_2", "");
  140. extGraph.audioPorts.outs.append(portNameToId);
  141. }
  142. // ---------------------------------------------------------------
  143. // now refresh
  144. if (sendHost || sendOSC)
  145. graph->refresh(sendHost, sendOSC, false, "Dummy");
  146. return true;
  147. }
  148. // -------------------------------------------------------------------
  149. protected:
  150. static int64_t getTimeInMicroseconds() noexcept
  151. {
  152. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  153. struct timeval tv;
  154. gettimeofday(&tv, nullptr);
  155. return (tv.tv_sec * 1000000) + tv.tv_usec;
  156. #else
  157. struct timespec ts;
  158. # ifdef CLOCK_MONOTONIC_RAW
  159. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  160. # else
  161. clock_gettime(CLOCK_MONOTONIC, &ts);
  162. # endif
  163. return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
  164. #endif
  165. }
  166. void run() override
  167. {
  168. const uint32_t bufferSize = pData->bufferSize;
  169. const int64_t cycleTime = static_cast<int64_t>(
  170. static_cast<double>(bufferSize) / pData->sampleRate * 1000000 + 0.5);
  171. int delay = 0;
  172. if (const char* const delaystr = std::getenv("CARLA_BRIDGE_DUMMY"))
  173. if ((delay = atoi(delaystr)) == 1)
  174. delay = 0;
  175. carla_stdout("CarlaEngineDummy audio thread started, cycle time: " P_INT64 "ms, delay %ds",
  176. cycleTime / 1000, delay);
  177. float* audioIns[2] = {
  178. (float*)std::malloc(sizeof(float)*bufferSize),
  179. (float*)std::malloc(sizeof(float)*bufferSize),
  180. };
  181. CARLA_SAFE_ASSERT_RETURN(audioIns[0] != nullptr,);
  182. CARLA_SAFE_ASSERT_RETURN(audioIns[1] != nullptr,);
  183. float* audioOuts[2] = {
  184. (float*)std::malloc(sizeof(float)*bufferSize),
  185. (float*)std::malloc(sizeof(float)*bufferSize),
  186. };
  187. CARLA_SAFE_ASSERT_RETURN(audioOuts[0] != nullptr,);
  188. CARLA_SAFE_ASSERT_RETURN(audioOuts[1] != nullptr,);
  189. carla_zeroFloats(audioIns[0], bufferSize);
  190. carla_zeroFloats(audioIns[1], bufferSize);
  191. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  192. int64_t oldTime, newTime;
  193. while (! shouldThreadExit())
  194. {
  195. if (delay > 0)
  196. carla_sleep(static_cast<uint>(delay));
  197. oldTime = getTimeInMicroseconds();
  198. const PendingRtEventsRunner prt(this, bufferSize, true);
  199. carla_zeroFloats(audioOuts[0], bufferSize);
  200. carla_zeroFloats(audioOuts[1], bufferSize);
  201. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  202. pData->graph.process(pData, audioIns, audioOuts, bufferSize);
  203. newTime = getTimeInMicroseconds();
  204. CARLA_SAFE_ASSERT_CONTINUE(newTime >= oldTime);
  205. const int64_t remainingTime = cycleTime - (newTime - oldTime);
  206. if (remainingTime <= 0)
  207. {
  208. ++pData->xruns;
  209. carla_stdout("XRUN! remaining time: " P_INT64 ", old: " P_INT64 ", new: " P_INT64 ")",
  210. remainingTime, oldTime, newTime);
  211. }
  212. else if (remainingTime >= 1000)
  213. {
  214. CARLA_SAFE_ASSERT_CONTINUE(remainingTime < 1000000); // 1 sec
  215. carla_msleep(static_cast<uint>(remainingTime / 1000));
  216. }
  217. }
  218. std::free(audioIns[0]);
  219. std::free(audioIns[1]);
  220. std::free(audioOuts[0]);
  221. std::free(audioOuts[1]);
  222. carla_stdout("CarlaEngineDummy audio thread finished with %u Xruns", pData->xruns);
  223. }
  224. // -------------------------------------------------------------------
  225. private:
  226. bool fRunning;
  227. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineDummy)
  228. };
  229. // -----------------------------------------
  230. namespace EngineInit {
  231. CarlaEngine* newDummy()
  232. {
  233. carla_debug("EngineInit::newDummy()");
  234. return new CarlaEngineDummy();
  235. }
  236. }
  237. // -----------------------------------------
  238. CARLA_BACKEND_END_NAMESPACE