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.

272 lines
7.9KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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. #include "CarlaNative.hpp"
  18. #include "juce_audio_processors.h"
  19. #include "juce_gui_extra.h"
  20. using namespace juce;
  21. // -----------------------------------------------------------------------
  22. #include "juce-host/FilterGraph.h"
  23. #include "juce-host/InternalFilters.h"
  24. #include "juce-host/GraphEditorPanel.h"
  25. #include "juce-host/MainHostWindow.h"
  26. // -----------------------------------------------------------------------
  27. class JucePatchbayPlugin : public NativePluginClass
  28. {
  29. public:
  30. JucePatchbayPlugin(const NativeHostDescriptor* const host)
  31. : NativePluginClass(host),
  32. fFormatManager(),
  33. fGraph(fFormatManager),
  34. fAudioBuffer(1, 0),
  35. fMidiKeyState(nullptr)
  36. {
  37. PropertiesFile::Options options;
  38. options.applicationName = "Juce Audio Plugin Host";
  39. options.filenameSuffix = "settings";
  40. options.osxLibrarySubFolder = "Preferences";
  41. fAppProperties = new ApplicationProperties();
  42. fAppProperties->setStorageParameters(options);
  43. fFormatManager.addDefaultFormats();
  44. fFormatManager.addFormat(new InternalPluginFormat());
  45. fGraph.ready(fAppProperties);
  46. fGraph.getGraph().setPlayConfigDetails(2, 2, getSampleRate(), static_cast<int>(getBufferSize()));
  47. fMidiBuffer.ensureSize(512*2);
  48. fMidiBuffer.clear();
  49. }
  50. ~JucePatchbayPlugin() override
  51. {
  52. fGraph.clear();
  53. fAppProperties = nullptr;
  54. }
  55. protected:
  56. // -------------------------------------------------------------------
  57. // Plugin process calls
  58. void activate() override
  59. {
  60. fGraph.getGraph().prepareToPlay(getSampleRate(), static_cast<int>(getBufferSize()));
  61. fAudioBuffer.setSize(2, static_cast<int>(getBufferSize()));
  62. {
  63. const ScopedLock csl(fMidiKeyMutex);
  64. if (fMidiKeyState != nullptr)
  65. fMidiKeyState->reset();
  66. }
  67. }
  68. void deactivate() override
  69. {
  70. fGraph.getGraph().releaseResources();
  71. }
  72. void process(float** inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  73. {
  74. fMidiBuffer.clear();
  75. for (uint32_t i=0; i < midiEventCount; ++i)
  76. {
  77. const NativeMidiEvent* const midiEvent(&midiEvents[i]);
  78. fMidiBuffer.addEvent(midiEvent->data, midiEvent->size, midiEvent->time);
  79. }
  80. {
  81. const ScopedLock csl(fMidiKeyMutex);
  82. if (fMidiKeyState != nullptr)
  83. fMidiKeyState->processNextMidiBuffer(fMidiBuffer, 0, static_cast<int>(frames), true);
  84. }
  85. FloatVectorOperations::copy(outBuffer[0], inBuffer[0], static_cast<int>(frames));
  86. FloatVectorOperations::copy(outBuffer[1], inBuffer[1], static_cast<int>(frames));
  87. AudioSampleBuffer audioBuf(outBuffer, 2, static_cast<int>(frames));
  88. fGraph.getGraph().processBlock(audioBuf, fMidiBuffer);
  89. MidiBuffer::Iterator outBufferIterator(fMidiBuffer);
  90. const uint8_t* midiData;
  91. int numBytes;
  92. int sampleNumber;
  93. NativeMidiEvent tmpEvent;
  94. tmpEvent.port = 0;
  95. for (; outBufferIterator.getNextEvent(midiData, numBytes, sampleNumber);)
  96. {
  97. if (numBytes <= 0 || numBytes > 4)
  98. continue;
  99. tmpEvent.size = numBytes;
  100. tmpEvent.time = sampleNumber;
  101. std::memcpy(tmpEvent.data, midiData, sizeof(uint8_t)*tmpEvent.size);
  102. writeMidiEvent(&tmpEvent);
  103. }
  104. }
  105. // -------------------------------------------------------------------
  106. // Plugin UI calls
  107. void uiShow(const bool show) override
  108. {
  109. const MessageManagerLock mmLock;
  110. if (show)
  111. {
  112. if (fWindow == nullptr)
  113. {
  114. fWindow = new MainHostWindow(fFormatManager, fGraph, *fAppProperties);
  115. fWindow->setName(getUiName());
  116. }
  117. {
  118. const ScopedLock csl(fMidiKeyMutex);
  119. fMidiKeyState = fWindow->getMidiState();
  120. }
  121. fWindow->toFront(true);
  122. }
  123. else if (fWindow != nullptr)
  124. {
  125. {
  126. const ScopedLock csl(fMidiKeyMutex);
  127. fMidiKeyState = nullptr;
  128. }
  129. fWindow->setVisible(false);
  130. fWindow = nullptr;
  131. }
  132. }
  133. void uiIdle() override
  134. {
  135. if (fWindow == nullptr)
  136. return;
  137. if (fWindow->wasClosedByUser())
  138. {
  139. uiShow(false);
  140. uiClosed();
  141. }
  142. }
  143. // -------------------------------------------------------------------
  144. // Plugin state calls
  145. char* getState() const override
  146. {
  147. ScopedPointer<XmlElement> xml(fGraph.createXml());
  148. MemoryOutputStream stream;
  149. xml->writeToStream(stream, String::empty);
  150. return strdup(stream.toUTF8().toRawUTF8());
  151. }
  152. void setState(const char* const data) override
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  155. String sdata(data);
  156. XmlDocument doc(sdata);
  157. ScopedPointer<XmlElement> xml(doc.getDocumentElement());
  158. if (xml != nullptr && xml->hasTagName("FILTERGRAPH"))
  159. fGraph.restoreFromXml(*xml);
  160. }
  161. // -------------------------------------------------------------------
  162. // Plugin dispatcher calls
  163. void uiNameChanged(const char* const uiName) override
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr,);
  166. if (fWindow == nullptr)
  167. return;
  168. const MessageManagerLock mmLock;
  169. fWindow->setName(uiName);
  170. }
  171. private:
  172. AudioPluginFormatManager fFormatManager;
  173. FilterGraph fGraph;
  174. AudioSampleBuffer fAudioBuffer;
  175. MidiBuffer fMidiBuffer;
  176. ScopedPointer<ApplicationProperties> fAppProperties;
  177. ScopedPointer<MainHostWindow> fWindow;
  178. MidiKeyboardState* fMidiKeyState;
  179. CriticalSection fMidiKeyMutex;
  180. PluginClassEND(JucePatchbayPlugin)
  181. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePatchbayPlugin)
  182. };
  183. // -----------------------------------------------------------------------
  184. static const NativePluginDescriptor jucePatchbayDesc = {
  185. /* category */ PLUGIN_CATEGORY_UTILITY,
  186. /* hints */ static_cast<NativePluginHints>(PLUGIN_IS_SYNTH|PLUGIN_HAS_UI|PLUGIN_NEEDS_FIXED_BUFFERS|PLUGIN_NEEDS_UI_JUCE|PLUGIN_USES_STATE|PLUGIN_USES_TIME),
  187. /* supports */ static_cast<NativePluginSupports>(0x0),
  188. /* audioIns */ 2,
  189. /* audioOuts */ 2,
  190. /* midiIns */ 1,
  191. /* midiOuts */ 1,
  192. /* paramIns */ 0,
  193. /* paramOuts */ 0,
  194. /* name */ "Juce Patchbay",
  195. /* label */ "jucePatchbay",
  196. /* maker */ "falkTX",
  197. /* copyright */ "GNU GPL v2+",
  198. PluginDescriptorFILL(JucePatchbayPlugin)
  199. };
  200. // -----------------------------------------------------------------------
  201. CARLA_EXPORT
  202. void carla_register_native_plugin_jucePatchbay()
  203. {
  204. carla_register_native_plugin(&jucePatchbayDesc);
  205. }
  206. // -----------------------------------------------------------------------
  207. #include "juce-host/juce_MidiKeyboardComponent.h"
  208. #include "juce-host/juce_MidiKeyboardComponent.cpp"
  209. #include "juce-host/FilterGraph.cpp"
  210. #include "juce-host/InternalFilters.cpp"
  211. #include "juce-host/GraphEditorPanel.cpp"
  212. #include "juce-host/MainHostWindow.cpp"
  213. // -----------------------------------------------------------------------