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.

197 lines
6.8KB

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