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

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