Skip to content

Commit 4c39a5e

Browse files
committed
add a scratch memory test plugin
1 parent 09e5f4c commit 4c39a5e

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

plugins/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ add_library(
7575
plugs/svf/svf-plug.cc
7676
plugs/undo-test/undo-test.hh
7777
plugs/undo-test/undo-test.hxx
78+
plugs/scratch-memory/scratch-memory.cc
79+
plugs/scratch-memory/scratch-memory.hh
7880
smoothed-value.hh
7981
sample-delay.cc
8082
sample-delay.hh

plugins/clap-entry.cc

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "plugs/track-info/track-info.hh"
1919
#include "plugs/transport/transport-info.hh"
2020
#include "plugs/undo-test/undo-test.hxx"
21+
#include "plugs/scratch-memory/scratch-memory.hh"
2122

2223
struct PluginEntry {
2324
using create_func = std::function<const clap_plugin *(const clap_host &)>;
@@ -57,6 +58,7 @@ static bool clap_init(const char *plugin_path) {
5758
addPlugin<clap::UndoTest<true, true>>();
5859
addPlugin<clap::UndoTest<true, false>>();
5960
addPlugin<clap::UndoTest<false, false>>();
61+
addPlugin<clap::ScratchMemory>();
6062
return true;
6163
}
6264

plugins/core-plugin.hh

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ namespace clap {
4040
#endif
4141
{
4242
using super = PluginGlue;
43-
friend class Module;
4443

4544
static const constexpr uint32_t max_frames = 256;
4645

4746
public:
47+
friend class Module;
48+
4849
CorePlugin(std::unique_ptr<PathProvider> &&pathProvider,
4950
const clap_plugin_descriptor *desc,
5051
const clap_host &host);
@@ -53,6 +54,8 @@ namespace clap {
5354
const PathProvider &pathProvider() const noexcept { return *_pathProvider; }
5455
const TuningProvider &tuningProvider() const noexcept { return _tuningProvider; }
5556

57+
auto& host() const noexcept { return _host; }
58+
5659
protected:
5760
bool enableDraftExtensions() const noexcept override { return true; }
5861

plugins/modules/module.hh

+4-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace clap {
3737
[[nodiscard]] virtual std::unique_ptr<Module> cloneVoice() const;
3838

3939
bool activate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
40-
[[nodiscard]] virtual bool doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
40+
[[nodiscard]] virtual bool
41+
doActivate(double sampleRate, uint32_t maxFrameCount, bool isRealTime);
4142
void deactivate();
4243
virtual void doDeactivate();
4344
[[nodiscard]] bool isActive() const noexcept { return _isActive; }
@@ -63,13 +64,9 @@ namespace clap {
6364
_voiceModule = voiceModule;
6465
}
6566

66-
virtual VoiceExpanderModule *getVoiceExpander() noexcept {
67-
return nullptr;
68-
}
67+
virtual VoiceExpanderModule *getVoiceExpander() noexcept { return nullptr; }
6968

70-
virtual const VoiceExpanderModule *getVoiceExpander() const noexcept {
71-
return nullptr;
72-
}
69+
virtual const VoiceExpanderModule *getVoiceExpander() const noexcept { return nullptr; }
7370

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "../../core-plugin.hh"
4+
5+
namespace clap {
6+
class ScratchMemory final : public CorePlugin {
7+
private:
8+
using super = CorePlugin;
9+
10+
public:
11+
ScratchMemory(const std::string &pluginPath, const clap_host &host);
12+
13+
static const clap_plugin_descriptor *descriptor();
14+
};
15+
} // namespace clap

0 commit comments

Comments
 (0)