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
5.0KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021 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 3 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 LICENSE file.
  16. */
  17. #pragma once
  18. #include <audio.hpp>
  19. #include <context.hpp>
  20. #ifdef NDEBUG
  21. # undef DEBUG
  22. #endif
  23. #include "DistrhoPlugin.hpp"
  24. #include "extra/Mutex.hpp"
  25. START_NAMESPACE_DISTRHO
  26. // -----------------------------------------------------------------------------------------------------------
  27. class CardinalBasePlugin : public Plugin {
  28. public:
  29. CardinalBasePlugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
  30. : Plugin(parameterCount, programCount, stateCount) {}
  31. ~CardinalBasePlugin() override {}
  32. virtual bool isActive() const noexcept = 0;
  33. virtual bool canAssignDevice() const noexcept = 0;
  34. virtual void assignDevice(rack::audio::Device* dev) noexcept = 0;
  35. virtual bool clearDevice(rack::audio::Device* dev) noexcept = 0;
  36. // ensure context validity through UI and setState
  37. Mutex contextMutex;
  38. };
  39. // -----------------------------------------------------------------------------------------------------------
  40. struct CardinalPluginContext : rack::Context {
  41. CardinalBasePlugin* const plugin;
  42. CardinalPluginContext(CardinalBasePlugin* const p)
  43. : plugin(p) {}
  44. };
  45. // -----------------------------------------------------------------------------------------------------------
  46. struct CardinalAudioDevice : rack::audio::Device {
  47. CardinalBasePlugin* const fPlugin;
  48. CardinalAudioDevice(CardinalBasePlugin* const plugin)
  49. : fPlugin(plugin) {}
  50. std::string getName() override
  51. {
  52. return "Cardinal";
  53. }
  54. int getNumInputs() override
  55. {
  56. return DISTRHO_PLUGIN_NUM_INPUTS;
  57. }
  58. int getNumOutputs() override
  59. {
  60. return DISTRHO_PLUGIN_NUM_OUTPUTS;
  61. }
  62. int getBlockSize() override
  63. {
  64. return fPlugin->getBufferSize();
  65. }
  66. float getSampleRate() override
  67. {
  68. return fPlugin->getSampleRate();
  69. }
  70. std::set<int> getBlockSizes() override
  71. {
  72. return std::set<int>({ getBlockSize() });
  73. }
  74. std::set<float> getSampleRates() override
  75. {
  76. return std::set<float>({ getSampleRate() });
  77. }
  78. void setBlockSize(int) override {}
  79. void setSampleRate(float) override {}
  80. };
  81. // -----------------------------------------------------------------------------------------------------------
  82. struct CardinalAudioDriver : rack::audio::Driver {
  83. CardinalAudioDriver() {}
  84. std::string getName() override
  85. {
  86. return "Plugin Driver";
  87. }
  88. std::vector<int> getDeviceIds() override
  89. {
  90. return std::vector<int>({ 0 });
  91. }
  92. int getDefaultDeviceId() override
  93. {
  94. return 0;
  95. }
  96. std::string getDeviceName(int) override
  97. {
  98. return "Plugin Device";
  99. }
  100. int getDeviceNumInputs(int) override
  101. {
  102. return DISTRHO_PLUGIN_NUM_INPUTS;
  103. }
  104. int getDeviceNumOutputs(int) override
  105. {
  106. return DISTRHO_PLUGIN_NUM_OUTPUTS;
  107. }
  108. rack::audio::Device* subscribe(int, rack::audio::Port* const port) override
  109. {
  110. CardinalPluginContext* const pluginContext = reinterpret_cast<CardinalPluginContext*>(port->context);
  111. DISTRHO_SAFE_ASSERT_RETURN(pluginContext != nullptr, nullptr);
  112. CardinalBasePlugin* const plugin = pluginContext->plugin;
  113. DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr, nullptr);
  114. if (! plugin->canAssignDevice())
  115. throw rack::Exception("Plugin driver only allows one audio device to be used simultaneously");
  116. CardinalAudioDevice* const device = new CardinalAudioDevice(plugin);
  117. device->subscribe(port);
  118. if (plugin->isActive())
  119. device->onStartStream();
  120. plugin->assignDevice(device);
  121. return device;
  122. }
  123. void unsubscribe(int, rack::audio::Port* const port) override
  124. {
  125. CardinalAudioDevice* const device = reinterpret_cast<CardinalAudioDevice*>(port->device);
  126. DISTRHO_SAFE_ASSERT_RETURN(device != nullptr,);
  127. CardinalPluginContext* const pluginContext = reinterpret_cast<CardinalPluginContext*>(port->context);
  128. DISTRHO_SAFE_ASSERT_RETURN(pluginContext != nullptr,);
  129. CardinalBasePlugin* const plugin = pluginContext->plugin;
  130. DISTRHO_SAFE_ASSERT_RETURN(plugin != nullptr,);
  131. if (plugin->clearDevice(device))
  132. {
  133. device->onStopStream();
  134. device->unsubscribe(port);
  135. delete device;
  136. }
  137. }
  138. };
  139. // -----------------------------------------------------------------------------------------------------------
  140. END_NAMESPACE_DISTRHO