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.

199 lines
6.9KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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 "CarlaThread.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. // External Graph stuff
  32. enum ExternalGraphGroupIds {
  33. kExternalGraphGroupNull = 0,
  34. kExternalGraphGroupCarla = 1,
  35. kExternalGraphGroupAudioIn = 2,
  36. kExternalGraphGroupAudioOut = 3,
  37. kExternalGraphGroupMidiIn = 4,
  38. kExternalGraphGroupMidiOut = 5,
  39. kExternalGraphGroupMax = 6
  40. };
  41. enum ExternalGraphCarlaPortIds {
  42. kExternalGraphCarlaPortNull = 0,
  43. kExternalGraphCarlaPortAudioIn1 = 1,
  44. kExternalGraphCarlaPortAudioIn2 = 2,
  45. kExternalGraphCarlaPortAudioOut1 = 3,
  46. kExternalGraphCarlaPortAudioOut2 = 4,
  47. kExternalGraphCarlaPortMidiIn = 5,
  48. kExternalGraphCarlaPortMidiOut = 6,
  49. kExternalGraphCarlaPortMax = 7
  50. };
  51. enum ExternalGraphConnectionType {
  52. kExternalGraphConnectionNull = 0,
  53. kExternalGraphConnectionAudioIn1 = 1,
  54. kExternalGraphConnectionAudioIn2 = 2,
  55. kExternalGraphConnectionAudioOut1 = 3,
  56. kExternalGraphConnectionAudioOut2 = 4,
  57. kExternalGraphConnectionMidiInput = 5,
  58. kExternalGraphConnectionMidiOutput = 6
  59. };
  60. struct ExternalGraphPorts {
  61. LinkedList<PortNameToId> ins;
  62. LinkedList<PortNameToId> outs;
  63. const char* getName(const bool isInput, const uint portId) const noexcept;
  64. uint getPortId(const bool isInput, const char portName[], bool* const ok = nullptr) const noexcept;
  65. ExternalGraphPorts() noexcept;
  66. CARLA_PREVENT_HEAP_ALLOCATION
  67. CARLA_DECLARE_NON_COPY_CLASS(ExternalGraphPorts)
  68. };
  69. struct ExternalGraph {
  70. PatchbayConnectionList connections;
  71. ExternalGraphPorts audioPorts, midiPorts;
  72. mutable CharStringListPtr retCon;
  73. ExternalGraph(CarlaEngine* const engine) noexcept;
  74. void clear() noexcept;
  75. bool connect(const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback) noexcept;
  76. bool disconnect(const uint connectionId) noexcept;
  77. void refresh(const char* const deviceName);
  78. const char* const* getConnections() const noexcept;
  79. bool getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept;
  80. CarlaEngine* const kEngine;
  81. CARLA_PREVENT_HEAP_ALLOCATION
  82. CARLA_DECLARE_NON_COPY_CLASS(ExternalGraph)
  83. };
  84. // -----------------------------------------------------------------------
  85. // RackGraph
  86. struct RackGraph {
  87. ExternalGraph extGraph;
  88. const uint32_t inputs;
  89. const uint32_t outputs;
  90. bool isOffline;
  91. struct Buffers {
  92. CarlaRecursiveMutex mutex;
  93. LinkedList<uint> connectedIn1;
  94. LinkedList<uint> connectedIn2;
  95. LinkedList<uint> connectedOut1;
  96. LinkedList<uint> connectedOut2;
  97. float* inBuf[2];
  98. float* inBufTmp[2];
  99. float* outBuf[2];
  100. float* unusedBuf;
  101. Buffers() noexcept;
  102. ~Buffers() noexcept;
  103. void setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept;
  104. CARLA_PREVENT_HEAP_ALLOCATION
  105. CARLA_DECLARE_NON_COPY_CLASS(Buffers)
  106. } audioBuffers;
  107. RackGraph(CarlaEngine* const engine, const uint32_t inputs, const uint32_t outputs) noexcept;
  108. ~RackGraph() noexcept;
  109. void setBufferSize(const uint32_t bufferSize) noexcept;
  110. void setOffline(const bool offline) noexcept;
  111. bool connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept;
  112. bool disconnect(const uint connectionId) noexcept;
  113. void refresh(const char* const deviceName);
  114. const char* const* getConnections() const noexcept;
  115. bool getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept;
  116. // the base, where plugins run
  117. void process(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames);
  118. // extended, will call process() in the middle
  119. void processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames);
  120. CarlaEngine* const kEngine;
  121. CARLA_DECLARE_NON_COPY_CLASS(RackGraph)
  122. };
  123. // -----------------------------------------------------------------------
  124. // PatchbayGraph
  125. class PatchbayGraph : private CarlaThread {
  126. public:
  127. PatchbayConnectionList connections;
  128. AudioProcessorGraph graph;
  129. AudioSampleBuffer audioBuffer;
  130. MidiBuffer midiBuffer;
  131. const uint32_t inputs;
  132. const uint32_t outputs;
  133. mutable CharStringListPtr retCon;
  134. bool usingExternal;
  135. ExternalGraph extGraph;
  136. PatchbayGraph(CarlaEngine* const engine, const uint32_t inputs, const uint32_t outputs);
  137. ~PatchbayGraph();
  138. void setBufferSize(const uint32_t bufferSize);
  139. void setSampleRate(const double sampleRate);
  140. void setOffline(const bool offline);
  141. void addPlugin(CarlaPlugin* const plugin);
  142. void replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin);
  143. void renamePlugin(CarlaPlugin* const plugin, const char* const newName);
  144. void removePlugin(CarlaPlugin* const plugin);
  145. void removeAllPlugins();
  146. bool connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback);
  147. bool disconnect(const uint connectionId);
  148. void disconnectInternalGroup(const uint groupId) noexcept;
  149. void refresh(const char* const deviceName);
  150. const char* const* getConnections(const bool external) const;
  151. bool getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const;
  152. void process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames);
  153. private:
  154. void run() override;
  155. CarlaEngine* const kEngine;
  156. CARLA_DECLARE_NON_COPY_CLASS(PatchbayGraph)
  157. };
  158. // -----------------------------------------------------------------------
  159. CARLA_BACKEND_END_NAMESPACE
  160. #endif // CARLA_ENGINE_GRAPH_HPP_INCLUDED