Skip to content

Commit

Permalink
add a scratch memory test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Dec 20, 2024
1 parent 09e5f4c commit 4c39a5e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 8 deletions.
2 changes: 2 additions & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ add_library(
plugs/svf/svf-plug.cc
plugs/undo-test/undo-test.hh
plugs/undo-test/undo-test.hxx
plugs/scratch-memory/scratch-memory.cc
plugs/scratch-memory/scratch-memory.hh
smoothed-value.hh
sample-delay.cc
sample-delay.hh
Expand Down
2 changes: 2 additions & 0 deletions plugins/clap-entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "plugs/track-info/track-info.hh"
#include "plugs/transport/transport-info.hh"
#include "plugs/undo-test/undo-test.hxx"
#include "plugs/scratch-memory/scratch-memory.hh"

struct PluginEntry {
using create_func = std::function<const clap_plugin *(const clap_host &)>;
Expand Down Expand Up @@ -57,6 +58,7 @@ static bool clap_init(const char *plugin_path) {
addPlugin<clap::UndoTest<true, true>>();
addPlugin<clap::UndoTest<true, false>>();
addPlugin<clap::UndoTest<false, false>>();
addPlugin<clap::ScratchMemory>();
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion plugins/core-plugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ namespace clap {
#endif
{
using super = PluginGlue;
friend class Module;

static const constexpr uint32_t max_frames = 256;

public:
friend class Module;

CorePlugin(std::unique_ptr<PathProvider> &&pathProvider,
const clap_plugin_descriptor *desc,
const clap_host &host);
Expand All @@ -53,6 +54,8 @@ namespace clap {
const PathProvider &pathProvider() const noexcept { return *_pathProvider; }
const TuningProvider &tuningProvider() const noexcept { return _tuningProvider; }

auto& host() const noexcept { return _host; }

protected:
bool enableDraftExtensions() const noexcept override { return true; }

Expand Down
11 changes: 4 additions & 7 deletions plugins/modules/module.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace clap {
[[nodiscard]] virtual std::unique_ptr<Module> cloneVoice() const;

bool activate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
[[nodiscard]] virtual bool doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
[[nodiscard]] virtual bool
doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
void deactivate();
virtual void doDeactivate();
[[nodiscard]] bool isActive() const noexcept { return _isActive; }
Expand All @@ -63,13 +64,9 @@ namespace clap {
_voiceModule = voiceModule;
}

virtual VoiceExpanderModule *getVoiceExpander() noexcept {
return nullptr;
}
virtual VoiceExpanderModule *getVoiceExpander() noexcept { return nullptr; }

virtual const VoiceExpanderModule *getVoiceExpander() const noexcept {
return nullptr;
}
virtual const VoiceExpanderModule *getVoiceExpander() const noexcept { return nullptr; }

virtual uint32_t latency() const noexcept { return 0; }

Expand Down
62 changes: 62 additions & 0 deletions plugins/plugs/scratch-memory/scratch-memory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <cstring>

#include "scratch-memory.hh"

namespace clap {
static constexpr size_t SCRATCH_SIZE = 16 * 1024;

class ScratchMemoryModule final : public Module {
public:
ScratchMemoryModule(ScratchMemory &plugin) : Module(plugin, "", 0) {}

[[nodiscard]] virtual bool
doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime) override {
auto &h = _plugin.host();

_didReserveScratchMemory = false;
if (h.canUseScratchMemory())
_didReserveScratchMemory = h.scratchMemoryReserve(SCRATCH_SIZE, 0);

return true;
}

clap_process_status process(const Context &c, uint32_t numFrames) noexcept override {
auto &h = _plugin.host();
if (_didReserveScratchMemory) {
auto scratchPtr = h.scratchMemoryAccess();
if (scratchPtr)
std::memset(scratchPtr, 0, SCRATCH_SIZE);
}
return CLAP_PROCESS_CONTINUE;
}

bool _didReserveScratchMemory = false;
};

const clap_plugin_descriptor *ScratchMemory::descriptor() {
static const char *features[] = {
CLAP_PLUGIN_FEATURE_UTILITY, CLAP_PLUGIN_FEATURE_ANALYZER, nullptr};

static const clap_plugin_descriptor desc = {CLAP_VERSION,
"com.github.free-audio.clap.scratch-memory-test",
"Scratch Memory Test",
"clap",
"https://github.com/free-audio/clap",
nullptr,
nullptr,
"1.0",
"Tests scratch memory",
features};

return &desc;
}

enum {
kParamIdOffset = 0,
};

ScratchMemory::ScratchMemory(const std::string &pluginPath, const clap_host &host)
: CorePlugin(PathProvider::create(pluginPath, "transport-info"), descriptor(), host) {
_rootModule = std::make_unique<ScratchMemoryModule>(*this);
}
} // namespace clap
15 changes: 15 additions & 0 deletions plugins/plugs/scratch-memory/scratch-memory.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "../../core-plugin.hh"

namespace clap {
class ScratchMemory final : public CorePlugin {
private:
using super = CorePlugin;

public:
ScratchMemory(const std::string &pluginPath, const clap_host &host);

static const clap_plugin_descriptor *descriptor();
};
} // namespace clap

0 comments on commit 4c39a5e

Please sign in to comment.