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.

188 lines
5.1KB

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