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.

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