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.

181 lines
4.9KB

  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. kCardinalVariantSynth,
  36. };
  37. // -----------------------------------------------------------------------------------------------------------
  38. struct CardinalPluginContext : rack::Context {
  39. uint32_t bufferSize, processCounter;
  40. double sampleRate;
  41. float parameters[kModuleParameters];
  42. CardinalVariant variant;
  43. bool bypassed, playing, reset, bbtValid;
  44. int32_t bar, beat, beatsPerBar, beatType;
  45. uint64_t frame;
  46. double barStartTick, beatsPerMinute;
  47. double tick, tickClock, ticksPerBeat, ticksPerClock, ticksPerFrame;
  48. uintptr_t nativeWindowId;
  49. const float* const* dataIns;
  50. float** dataOuts;
  51. const MidiEvent* midiEvents;
  52. uint32_t midiEventCount;
  53. Plugin* const plugin;
  54. #ifndef HEADLESS
  55. UI* ui;
  56. #endif
  57. CardinalPluginContext(Plugin* const p)
  58. : bufferSize(p->getBufferSize()),
  59. processCounter(0),
  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. bypassed(false),
  71. playing(false),
  72. reset(false),
  73. bbtValid(false),
  74. bar(1),
  75. beat(1),
  76. beatsPerBar(4),
  77. beatType(4),
  78. frame(0),
  79. barStartTick(0.0),
  80. beatsPerMinute(120.0),
  81. tick(0.0),
  82. tickClock(0.0),
  83. ticksPerBeat(0.0),
  84. ticksPerClock(0.0),
  85. ticksPerFrame(0.0),
  86. nativeWindowId(0),
  87. dataIns(nullptr),
  88. dataOuts(nullptr),
  89. midiEvents(nullptr),
  90. midiEventCount(0),
  91. plugin(p)
  92. #ifndef HEADLESS
  93. , ui(nullptr)
  94. #endif
  95. {
  96. std::memset(parameters, 0, sizeof(parameters));
  97. }
  98. void writeMidiMessage(const rack::midi::Message& message, uint8_t channel);
  99. #ifndef HEADLESS
  100. bool addIdleCallback(IdleCallback* cb) const;
  101. void removeIdleCallback(IdleCallback* cb) const;
  102. #endif
  103. };
  104. #ifndef HEADLESS
  105. void handleHostParameterDrag(const CardinalPluginContext* pcontext, uint index, bool started);
  106. #endif
  107. // -----------------------------------------------------------------------------------------------------------
  108. CardinalPluginContext* getRackContextFromPlugin(void* ptr);
  109. class CardinalBasePlugin : public Plugin {
  110. public:
  111. CardinalPluginContext* const context;
  112. CardinalBasePlugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount)
  113. : Plugin(parameterCount, programCount, stateCount),
  114. context(new CardinalPluginContext(this)) {}
  115. ~CardinalBasePlugin() override {}
  116. };
  117. #ifndef HEADLESS
  118. struct WasmPatchStorageLoadingDialog;
  119. class CardinalBaseUI : public UI {
  120. public:
  121. CardinalPluginContext* const context;
  122. bool saving;
  123. bool savingUncompressed;
  124. #ifdef DISTRHO_OS_WASM
  125. WasmPatchStorageLoadingDialog* psDialog;
  126. #endif
  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. #ifdef DISTRHO_OS_WASM
  136. psDialog(nullptr),
  137. #endif
  138. filebrowseraction(),
  139. filebrowserhandle(nullptr)
  140. {
  141. context->ui = this;
  142. }
  143. ~CardinalBaseUI() override
  144. {
  145. if (filebrowserhandle != nullptr)
  146. fileBrowserClose(filebrowserhandle);
  147. context->ui = nullptr;
  148. }
  149. };
  150. #endif
  151. // -----------------------------------------------------------------------------------------------------------
  152. END_NAMESPACE_DISTRHO