|
| 1 | +#include <cstring> |
| 2 | + |
| 3 | +#include "scratch-memory.hh" |
| 4 | + |
| 5 | +namespace clap { |
| 6 | + static constexpr size_t SCRATCH_SIZE = 16 * 1024; |
| 7 | + |
| 8 | + class ScratchMemoryModule final : public Module { |
| 9 | + public: |
| 10 | + ScratchMemoryModule(ScratchMemory &plugin) : Module(plugin, "", 0) {} |
| 11 | + |
| 12 | + [[nodiscard]] virtual bool |
| 13 | + doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime) override { |
| 14 | + auto &h = _plugin.host(); |
| 15 | + |
| 16 | + _didReserveScratchMemory = false; |
| 17 | + if (h.canUseScratchMemory()) |
| 18 | + _didReserveScratchMemory = h.scratchMemoryReserve(SCRATCH_SIZE, 0); |
| 19 | + |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + clap_process_status process(const Context &c, uint32_t numFrames) noexcept override { |
| 24 | + auto &h = _plugin.host(); |
| 25 | + if (_didReserveScratchMemory) { |
| 26 | + auto scratchPtr = h.scratchMemoryAccess(); |
| 27 | + if (scratchPtr) |
| 28 | + std::memset(scratchPtr, 0, SCRATCH_SIZE); |
| 29 | + } |
| 30 | + return CLAP_PROCESS_CONTINUE; |
| 31 | + } |
| 32 | + |
| 33 | + bool _didReserveScratchMemory = false; |
| 34 | + }; |
| 35 | + |
| 36 | + const clap_plugin_descriptor *ScratchMemory::descriptor() { |
| 37 | + static const char *features[] = { |
| 38 | + CLAP_PLUGIN_FEATURE_UTILITY, CLAP_PLUGIN_FEATURE_ANALYZER, nullptr}; |
| 39 | + |
| 40 | + static const clap_plugin_descriptor desc = {CLAP_VERSION, |
| 41 | + "com.github.free-audio.clap.scratch-memory-test", |
| 42 | + "Scratch Memory Test", |
| 43 | + "clap", |
| 44 | + "https://github.com/free-audio/clap", |
| 45 | + nullptr, |
| 46 | + nullptr, |
| 47 | + "1.0", |
| 48 | + "Tests scratch memory", |
| 49 | + features}; |
| 50 | + |
| 51 | + return &desc; |
| 52 | + } |
| 53 | + |
| 54 | + enum { |
| 55 | + kParamIdOffset = 0, |
| 56 | + }; |
| 57 | + |
| 58 | + ScratchMemory::ScratchMemory(const std::string &pluginPath, const clap_host &host) |
| 59 | + : CorePlugin(PathProvider::create(pluginPath, "transport-info"), descriptor(), host) { |
| 60 | + _rootModule = std::make_unique<ScratchMemoryModule>(*this); |
| 61 | + } |
| 62 | +} // namespace clap |
0 commit comments