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.

175 lines
4.8KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2022 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. # include "extra/FileBrowserDialog.hpp"
  29. #endif
  30. START_NAMESPACE_DISTRHO
  31. // -----------------------------------------------------------------------------------------------------------
  32. static constexpr const uint kModuleParameters = 24;
  33. enum CardinalVariant {
  34. kCardinalVariantMain,
  35. kCardinalVariantFX,
  36. kCardinalVariantSynth,
  37. };
  38. // -----------------------------------------------------------------------------------------------------------
  39. struct CardinalPluginContext : rack::Context {
  40. uint32_t bufferSize;
  41. double sampleRate;
  42. float parameters[kModuleParameters];
  43. CardinalVariant variant;
  44. bool playing, reset, bbtValid;
  45. int32_t bar, beat, beatsPerBar, beatType;
  46. uint64_t frame;
  47. double barStartTick, beatsPerMinute;
  48. double tick, tickClock, ticksPerBeat, ticksPerClock, ticksPerFrame;
  49. uintptr_t nativeWindowId;
  50. const float* const* dataIns;
  51. float** dataOuts;
  52. const MidiEvent* midiEvents;
  53. uint32_t midiEventCount;
  54. Plugin* const plugin;
  55. #ifndef HEADLESS
  56. UI* ui;
  57. #endif
  58. CardinalPluginContext(Plugin* const p)
  59. : bufferSize(p->getBufferSize()),
  60. sampleRate(p->getSampleRate()),
  61. #if CARDINAL_VARIANT_MAIN
  62. variant(kCardinalVariantMain),
  63. #elif CARDINAL_VARIANT_FX
  64. variant(kCardinalVariantFX),
  65. #elif CARDINAL_VARIANT_SYNTH
  66. variant(kCardinalVariantSynth),
  67. #else
  68. #error cardinal variant not set
  69. #endif
  70. playing(false),
  71. reset(false),
  72. bbtValid(false),
  73. bar(1),
  74. beat(1),
  75. beatsPerBar(4),
  76. beatType(4),
  77. frame(0),
  78. barStartTick(0.0),
  79. beatsPerMinute(120.0),
  80. tick(0.0),
  81. tickClock(0.0),
  82. ticksPerBeat(0.0),
  83. ticksPerClock(0.0),
  84. ticksPerFrame(0.0),
  85. nativeWindowId(0),
  86. dataIns(nullptr),
  87. dataOuts(nullptr),
  88. midiEvents(nullptr),
  89. midiEventCount(0),
  90. plugin(p)
  91. #ifndef HEADLESS
  92. , ui(nullptr)
  93. #endif
  94. {
  95. std::memset(parameters, 0, sizeof(parameters));
  96. }
  97. void writeMidiMessage(const rack::midi::Message& message, uint8_t channel);
  98. #ifndef HEADLESS
  99. bool addIdleCallback(IdleCallback* cb) const;
  100. void removeIdleCallback(IdleCallback* cb) const;
  101. #endif
  102. };
  103. #ifndef HEADLESS
  104. void handleHostParameterDrag(const CardinalPluginContext* pcontext, uint index, bool started);
  105. #endif
  106. // -----------------------------------------------------------------------------------------------------------
  107. struct CardinalAudioDevice;
  108. struct CardinalMidiInputDevice;
  109. struct CardinalMidiOutputDevice;
  110. CardinalPluginContext* getRackContextFromPlugin(void* ptr);
  111. class CardinalBasePlugin : public Plugin {
  112. public:
  113. CardinalPluginContext* const context;
  114. CardinalBasePlugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
  115. : Plugin(parameterCount, programCount, stateCount),
  116. context(new CardinalPluginContext(this)) {}
  117. ~CardinalBasePlugin() override {}
  118. };
  119. #ifndef HEADLESS
  120. class CardinalBaseUI : public UI {
  121. public:
  122. CardinalPluginContext* const context;
  123. bool saving;
  124. bool savingUncompressed;
  125. // for 3rd party modules
  126. std::function<void(char* path)> filebrowseraction;
  127. FileBrowserHandle filebrowserhandle;
  128. CardinalBaseUI(const uint width, const uint height)
  129. : UI(width, height),
  130. context(getRackContextFromPlugin(getPluginInstancePointer())),
  131. saving(false),
  132. savingUncompressed(false),
  133. filebrowseraction(),
  134. filebrowserhandle(nullptr)
  135. {
  136. context->ui = this;
  137. }
  138. ~CardinalBaseUI() override
  139. {
  140. if (filebrowserhandle != nullptr)
  141. fileBrowserClose(filebrowserhandle);
  142. context->ui = nullptr;
  143. }
  144. };
  145. #endif
  146. // -----------------------------------------------------------------------------------------------------------
  147. END_NAMESPACE_DISTRHO