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.

184 lines
4.3KB

  1. /*
  2. * Carla Plugin Engine
  3. * Copyright (C) 2012-2013 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. #ifdef BUILD_BRIDGE
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "CarlaBridge.hpp"
  22. #include "CarlaShmUtils.hpp"
  23. CARLA_BACKEND_START_NAMESPACE
  24. #if 0
  25. } // Fix editor indentation
  26. #endif
  27. // -----------------------------------------
  28. class CarlaEngineBridge : public CarlaEngine
  29. {
  30. public:
  31. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName)
  32. : CarlaEngine()
  33. {
  34. carla_debug("CarlaEngineBridge::CarlaEngineBridge()");
  35. fShmAudioPool.filename = "/carla-bridge_shm_" + audioBaseName;
  36. fShmControl.filename = "/carla-bridge_shc_" + controlBaseName;
  37. }
  38. ~CarlaEngineBridge()
  39. {
  40. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  41. }
  42. // -------------------------------------
  43. // CarlaEngine virtual calls
  44. bool init(const char* const clientName)
  45. {
  46. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  47. char tmpFileBase[60];
  48. // SHM Audio Pool
  49. {
  50. fShmAudioPool.shm = carla_shm_attach((const char*)fShmAudioPool.filename);
  51. if (! carla_is_shm_valid(fShmAudioPool.shm))
  52. {
  53. _cleanup();
  54. carla_stdout("Failed to open or create shared memory file #1");
  55. return false;
  56. }
  57. }
  58. // SHM Control
  59. {
  60. fShmControl.shm = carla_shm_attach((const char*)fShmControl.filename);
  61. if (! carla_is_shm_valid(fShmControl.shm))
  62. {
  63. _cleanup();
  64. carla_stdout("Failed to open or create shared memory file #2");
  65. return false;
  66. }
  67. if (! carla_shm_map<ShmControl>(fShmControl.shm, fShmControl.data))
  68. {
  69. _cleanup();
  70. carla_stdout("Failed to mmap shared memory file");
  71. return false;
  72. }
  73. }
  74. CarlaEngine::init(fName);
  75. return true;
  76. }
  77. bool close()
  78. {
  79. carla_debug("CarlaEnginePlugin::close()");
  80. CarlaEngine::close();
  81. _cleanup();
  82. return true;
  83. }
  84. bool isRunning() const
  85. {
  86. return true;
  87. }
  88. bool isOffline() const
  89. {
  90. return false;
  91. }
  92. EngineType type() const
  93. {
  94. return kEngineTypeBridge;
  95. }
  96. private:
  97. struct BridgeAudioPool {
  98. CarlaString filename;
  99. float* data;
  100. size_t size;
  101. shm_t shm;
  102. BridgeAudioPool()
  103. : data(nullptr),
  104. size(0)
  105. {
  106. carla_shm_init(shm);
  107. }
  108. } fShmAudioPool;
  109. struct BridgeControl {
  110. CarlaString filename;
  111. ShmControl* data;
  112. shm_t shm;
  113. BridgeControl()
  114. : data(nullptr)
  115. {
  116. carla_shm_init(shm);
  117. }
  118. } fShmControl;
  119. void _cleanup()
  120. {
  121. if (fShmAudioPool.filename.isNotEmpty())
  122. fShmAudioPool.filename.clear();
  123. if (fShmControl.filename.isNotEmpty())
  124. fShmControl.filename.clear();
  125. // delete data
  126. fShmAudioPool.data = nullptr;
  127. fShmAudioPool.size = 0;
  128. // and again
  129. fShmControl.data = nullptr;
  130. if (carla_is_shm_valid(fShmAudioPool.shm))
  131. carla_shm_close(fShmAudioPool.shm);
  132. if (carla_is_shm_valid(fShmControl.shm))
  133. carla_shm_close(fShmControl.shm);
  134. }
  135. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  136. };
  137. // -----------------------------------------
  138. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  139. {
  140. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  141. }
  142. CARLA_BACKEND_END_NAMESPACE
  143. #endif // BUILD_BRIDGE