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.

CarlaEngineDummy.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. carla_stdout("CarlaEngineDummy audio thread started, cycle time: " P_INT64 "ms", cycleTime / 1000);
  168. float* audioIns[2] = {
  169. (float*)std::malloc(sizeof(float)*bufferSize),
  170. (float*)std::malloc(sizeof(float)*bufferSize),
  171. };
  172. CARLA_SAFE_ASSERT_RETURN(audioIns[0] != nullptr,);
  173. CARLA_SAFE_ASSERT_RETURN(audioIns[1] != nullptr,);
  174. float* audioOuts[2] = {
  175. (float*)std::malloc(sizeof(float)*bufferSize),
  176. (float*)std::malloc(sizeof(float)*bufferSize),
  177. };
  178. CARLA_SAFE_ASSERT_RETURN(audioOuts[0] != nullptr,);
  179. CARLA_SAFE_ASSERT_RETURN(audioOuts[1] != nullptr,);
  180. carla_zeroFloats(audioIns[0], bufferSize);
  181. carla_zeroFloats(audioIns[1], bufferSize);
  182. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  183. int64_t oldTime, newTime;
  184. while (! shouldThreadExit())
  185. {
  186. oldTime = getTimeInMicroseconds();
  187. const PendingRtEventsRunner prt(this, bufferSize, true);
  188. carla_zeroFloats(audioOuts[0], bufferSize);
  189. carla_zeroFloats(audioOuts[1], bufferSize);
  190. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  191. pData->graph.process(pData, audioIns, audioOuts, bufferSize);
  192. newTime = getTimeInMicroseconds();
  193. CARLA_SAFE_ASSERT_CONTINUE(newTime >= oldTime);
  194. const int64_t remainingTime = cycleTime - (newTime - oldTime);
  195. if (remainingTime <= 0)
  196. {
  197. ++pData->xruns;
  198. carla_stdout("XRUN! remaining time: " P_INT64 ", old: " P_INT64 ", new: " P_INT64 ")",
  199. remainingTime, oldTime, newTime);
  200. }
  201. else
  202. {
  203. CARLA_SAFE_ASSERT_CONTINUE(remainingTime < 1000000); // 1 sec
  204. carla_msleep(static_cast<uint>(remainingTime / 1000));
  205. }
  206. }
  207. std::free(audioIns[0]);
  208. std::free(audioIns[1]);
  209. std::free(audioOuts[0]);
  210. std::free(audioOuts[1]);
  211. carla_stdout("CarlaEngineDummy audio thread finished with %u Xruns", pData->xruns);
  212. }
  213. // -------------------------------------------------------------------
  214. private:
  215. bool fRunning;
  216. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineDummy)
  217. };
  218. // -----------------------------------------
  219. namespace EngineInit {
  220. CarlaEngine* newDummy()
  221. {
  222. carla_debug("EngineInit::newDummy()");
  223. return new CarlaEngineDummy();
  224. }
  225. }
  226. // -----------------------------------------
  227. CARLA_BACKEND_END_NAMESPACE