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.

153 lines
4.5KB

  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. #include <midi.hpp>
  21. #ifdef NDEBUG
  22. # undef DEBUG
  23. #endif
  24. #include "DistrhoPlugin.hpp"
  25. #include "extra/Mutex.hpp"
  26. #ifndef HEADLESS
  27. # include "DistrhoUI.hpp"
  28. #endif
  29. START_NAMESPACE_DISTRHO
  30. // -----------------------------------------------------------------------------------------------------------
  31. static constexpr const uint kModuleParameters = 24;
  32. // -----------------------------------------------------------------------------------------------------------
  33. struct CardinalPluginContext : rack::Context {
  34. uint32_t bufferSize;
  35. double sampleRate;
  36. float parameters[kModuleParameters];
  37. bool playing, reset, bbtValid, loadedHostCV;
  38. int32_t bar, beat, beatsPerBar, beatType;
  39. uint64_t frame;
  40. double barStartTick, beatsPerMinute;
  41. double tick, tickClock, ticksPerBeat, ticksPerClock, ticksPerFrame;
  42. uintptr_t nativeWindowId;
  43. uint32_t dataFrame;
  44. const float** dataIns;
  45. float** dataOuts;
  46. Plugin* const plugin;
  47. #ifndef HEADLESS
  48. UI* ui;
  49. #endif
  50. CardinalPluginContext(Plugin* const p)
  51. : bufferSize(p->getBufferSize()),
  52. sampleRate(p->getSampleRate()),
  53. playing(false),
  54. reset(false),
  55. bbtValid(false),
  56. loadedHostCV(false),
  57. bar(1),
  58. beat(1),
  59. beatsPerBar(4),
  60. beatType(4),
  61. frame(0),
  62. barStartTick(0.0),
  63. beatsPerMinute(120.0),
  64. tick(0.0),
  65. tickClock(0.0),
  66. ticksPerBeat(0.0),
  67. ticksPerClock(0.0),
  68. ticksPerFrame(0.0),
  69. nativeWindowId(0),
  70. dataFrame(0),
  71. dataIns(nullptr),
  72. dataOuts(nullptr),
  73. plugin(p)
  74. #ifndef HEADLESS
  75. , ui(nullptr)
  76. #endif
  77. {
  78. std::memset(parameters, 0, sizeof(parameters));
  79. }
  80. #ifndef HEADLESS
  81. bool addIdleCallback(IdleCallback* cb);
  82. void removeIdleCallback(IdleCallback* cb);
  83. #endif
  84. };
  85. #ifndef HEADLESS
  86. void handleHostParameterDrag(CardinalPluginContext* pcontext, uint index, bool started);
  87. #endif
  88. // -----------------------------------------------------------------------------------------------------------
  89. struct CardinalAudioDevice;
  90. struct CardinalMidiInputDevice;
  91. struct CardinalMidiOutputDevice;
  92. CardinalPluginContext* getRackContextFromPlugin(void* ptr);
  93. class CardinalBasePlugin : public Plugin {
  94. public:
  95. CardinalPluginContext* const context;
  96. CardinalBasePlugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
  97. : Plugin(parameterCount, programCount, stateCount),
  98. context(new CardinalPluginContext(this)) {}
  99. ~CardinalBasePlugin() override {}
  100. virtual bool isActive() const noexcept = 0;
  101. virtual bool canAssignAudioDevice() const noexcept = 0;
  102. virtual bool canAssignMidiInputDevice() const noexcept = 0;
  103. virtual bool canAssignMidiOutputDevice() const noexcept = 0;
  104. virtual void assignAudioDevice(CardinalAudioDevice* dev) noexcept = 0;
  105. virtual void assignMidiInputDevice(CardinalMidiInputDevice* dev) noexcept = 0;
  106. virtual void assignMidiOutputDevice(CardinalMidiOutputDevice* dev) noexcept = 0;
  107. virtual bool clearAudioDevice(CardinalAudioDevice* dev) noexcept = 0;
  108. virtual bool clearMidiInputDevice(CardinalMidiInputDevice* dev) noexcept = 0;
  109. virtual bool clearMidiOutputDevice(CardinalMidiOutputDevice* dev) noexcept = 0;
  110. };
  111. #ifndef HEADLESS
  112. class CardinalBaseUI : public UI {
  113. public:
  114. CardinalPluginContext* const context;
  115. bool saving;
  116. CardinalBaseUI(const uint width, const uint height)
  117. : UI(width, height),
  118. context(getRackContextFromPlugin(getPluginInstancePointer())),
  119. saving(false)
  120. {
  121. context->ui = this;
  122. }
  123. ~CardinalBaseUI() override
  124. {
  125. context->ui = nullptr;
  126. }
  127. };
  128. #endif
  129. // -----------------------------------------------------------------------------------------------------------
  130. END_NAMESPACE_DISTRHO