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.

232 lines
8.1KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2022 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 doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_ENGINE_GRAPH_HPP_INCLUDED
  18. #define CARLA_ENGINE_GRAPH_HPP_INCLUDED
  19. #include "CarlaEngine.hpp"
  20. #include "CarlaMutex.hpp"
  21. #include "CarlaPatchbayUtils.hpp"
  22. #include "CarlaStringList.hpp"
  23. #include "CarlaRunner.hpp"
  24. #include "water/processors/AudioProcessorGraph.h"
  25. #include "water/text/StringArray.h"
  26. using water::AudioProcessorGraph;
  27. using water::AudioSampleBuffer;
  28. using water::MidiBuffer;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  31. struct PatchbayPosition {
  32. bool active;
  33. int x1, y1, x2, y2;
  34. };
  35. // -----------------------------------------------------------------------
  36. // External Graph stuff
  37. enum ExternalGraphGroupIds {
  38. kExternalGraphGroupNull = 0,
  39. kExternalGraphGroupCarla = 1,
  40. kExternalGraphGroupAudioIn = 2,
  41. kExternalGraphGroupAudioOut = 3,
  42. kExternalGraphGroupMidiIn = 4,
  43. kExternalGraphGroupMidiOut = 5,
  44. kExternalGraphGroupMax = 6
  45. };
  46. enum ExternalGraphCarlaPortIds {
  47. kExternalGraphCarlaPortNull = 0,
  48. kExternalGraphCarlaPortAudioIn1 = 1,
  49. kExternalGraphCarlaPortAudioIn2 = 2,
  50. kExternalGraphCarlaPortAudioOut1 = 3,
  51. kExternalGraphCarlaPortAudioOut2 = 4,
  52. kExternalGraphCarlaPortMidiIn = 5,
  53. kExternalGraphCarlaPortMidiOut = 6,
  54. kExternalGraphCarlaPortMax = 7
  55. };
  56. enum ExternalGraphConnectionType {
  57. kExternalGraphConnectionNull = 0,
  58. kExternalGraphConnectionAudioIn1 = 1,
  59. kExternalGraphConnectionAudioIn2 = 2,
  60. kExternalGraphConnectionAudioOut1 = 3,
  61. kExternalGraphConnectionAudioOut2 = 4,
  62. kExternalGraphConnectionMidiInput = 5,
  63. kExternalGraphConnectionMidiOutput = 6
  64. };
  65. struct ExternalGraphPorts {
  66. LinkedList<PortNameToId> ins;
  67. LinkedList<PortNameToId> outs;
  68. const char* getName(bool isInput, uint portId) const noexcept;
  69. uint getPortIdFromName(bool isInput, const char name[], bool* ok = nullptr) const noexcept;
  70. uint getPortIdFromIdentifier(bool isInput, const char identifier[], bool* ok = nullptr) const noexcept;
  71. ExternalGraphPorts() noexcept;
  72. CARLA_PREVENT_HEAP_ALLOCATION
  73. CARLA_DECLARE_NON_COPYABLE(ExternalGraphPorts)
  74. };
  75. struct ExternalGraph {
  76. PatchbayConnectionList connections;
  77. ExternalGraphPorts audioPorts, midiPorts;
  78. PatchbayPosition positions[kExternalGraphGroupMax];
  79. mutable CharStringListPtr retCon;
  80. ExternalGraph(CarlaEngine* engine) noexcept;
  81. void clear() noexcept;
  82. bool connect(bool sendHost, bool sendOSC,
  83. uint groupA, uint portA, uint groupB, uint portB) noexcept;
  84. bool disconnect(bool sendHost, bool sendOSC,
  85. uint connectionId) noexcept;
  86. void setGroupPos(bool sendHost, bool sendOSC,
  87. uint groupId, int x1, int y1, int x2, int y2);
  88. void refresh(bool sendHost, bool sendOSC,
  89. const char* deviceName);
  90. const char* const* getConnections() const noexcept;
  91. bool getGroupFromName(const char* groupName, uint& groupId) const noexcept;
  92. bool getGroupAndPortIdFromFullName(const char* fullPortName, uint& groupId, uint& portId) const noexcept;
  93. CarlaEngine* const kEngine;
  94. CARLA_PREVENT_HEAP_ALLOCATION
  95. CARLA_DECLARE_NON_COPYABLE(ExternalGraph)
  96. };
  97. // -----------------------------------------------------------------------
  98. // RackGraph
  99. struct RackGraph {
  100. ExternalGraph extGraph;
  101. const uint32_t inputs;
  102. const uint32_t outputs;
  103. bool isOffline;
  104. struct Buffers {
  105. CarlaRecursiveMutex mutex;
  106. LinkedList<uint> connectedIn1;
  107. LinkedList<uint> connectedIn2;
  108. LinkedList<uint> connectedOut1;
  109. LinkedList<uint> connectedOut2;
  110. float* inBuf[2];
  111. float* inBufTmp[2];
  112. float* outBuf[2];
  113. float* unusedBuf;
  114. Buffers() noexcept;
  115. ~Buffers() noexcept;
  116. void setBufferSize(uint32_t bufferSize, bool createBuffers) noexcept;
  117. CARLA_PREVENT_HEAP_ALLOCATION
  118. CARLA_DECLARE_NON_COPYABLE(Buffers)
  119. } audioBuffers;
  120. RackGraph(CarlaEngine* engine, uint32_t inputs, uint32_t outputs) noexcept;
  121. ~RackGraph() noexcept;
  122. void setBufferSize(uint32_t bufferSize) noexcept;
  123. void setOffline(bool offline) noexcept;
  124. bool connect(uint groupA, uint portA, uint groupB, uint portB) noexcept;
  125. bool disconnect(uint connectionId) noexcept;
  126. void refresh(bool sendHost, bool sendOsc, bool ignored, const char* deviceName);
  127. const char* const* getConnections() const noexcept;
  128. bool getGroupAndPortIdFromFullName(const char* fullPortName, uint& groupId, uint& portId) const noexcept;
  129. // the base, where plugins run
  130. void process(CarlaEngine::ProtectedData* data, const float* inBuf[2], float* outBuf[2], uint32_t frames);
  131. // extended, will call process() in the middle
  132. void processHelper(CarlaEngine::ProtectedData* data, const float* const* inBuf, float* const* outBuf, uint32_t frames);
  133. CarlaEngine* const kEngine;
  134. CARLA_DECLARE_NON_COPYABLE(RackGraph)
  135. };
  136. // -----------------------------------------------------------------------
  137. // PatchbayGraph
  138. class PatchbayGraph : private CarlaRunner {
  139. public:
  140. PatchbayConnectionList connections;
  141. AudioProcessorGraph graph;
  142. AudioSampleBuffer audioBuffer;
  143. AudioSampleBuffer cvInBuffer;
  144. AudioSampleBuffer cvOutBuffer;
  145. MidiBuffer midiBuffer;
  146. const uint32_t numAudioIns;
  147. const uint32_t numAudioOuts;
  148. const uint32_t numCVIns;
  149. const uint32_t numCVOuts;
  150. mutable CharStringListPtr retCon;
  151. bool usingExternalHost;
  152. bool usingExternalOSC;
  153. ExternalGraph extGraph;
  154. PatchbayGraph(CarlaEngine* engine,
  155. uint32_t audioIns, uint32_t audioOuts,
  156. uint32_t cvIns, uint32_t cvOuts,
  157. bool withMidiIn, bool withMidiOut);
  158. ~PatchbayGraph();
  159. void setBufferSize(uint32_t bufferSize);
  160. void setSampleRate(double sampleRate);
  161. void setOffline(bool offline);
  162. void addPlugin(CarlaPluginPtr plugin);
  163. void replacePlugin(CarlaPluginPtr oldPlugin, CarlaPluginPtr newPlugin);
  164. void renamePlugin(CarlaPluginPtr plugin, const char* newName);
  165. void switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB);
  166. void reconfigureForCV(CarlaPluginPtr plugin, const uint portIndex, bool added);
  167. void reconfigurePlugin(CarlaPluginPtr plugin, bool portsAdded);
  168. void removePlugin(CarlaPluginPtr plugin);
  169. void removeAllPlugins(bool aboutToClose);
  170. bool connect(bool external, uint groupA, uint portA, uint groupB, uint portB);
  171. bool disconnect(bool external, uint connectionId);
  172. void disconnectInternalGroup(uint groupId) noexcept;
  173. void setGroupPos(bool sendHost, bool sendOsc, bool external, uint groupId, int x1, int y1, int x2, int y2);
  174. void refresh(bool sendHost, bool sendOsc, bool external, const char* deviceName);
  175. const char* const* getConnections(bool external) const;
  176. const CarlaEngine::PatchbayPosition* getPositions(bool external, uint& count) const;
  177. bool getGroupFromName(bool external, const char* groupName, uint& groupId) const;
  178. bool getGroupAndPortIdFromFullName(bool external, const char* fullPortName, uint& groupId, uint& portId) const;
  179. void process(CarlaEngine::ProtectedData* data,
  180. const float* const* inBuf,
  181. float* const* outBuf,
  182. uint32_t frames);
  183. private:
  184. bool run() override;
  185. CarlaEngine* const kEngine;
  186. CARLA_DECLARE_NON_COPYABLE(PatchbayGraph)
  187. };
  188. // -----------------------------------------------------------------------
  189. CARLA_BACKEND_END_NAMESPACE
  190. #endif // CARLA_ENGINE_GRAPH_HPP_INCLUDED