commit 91f9a918c1e57e92f1e102ab25b4f324e785ee6f
parent 88c1867aff536cf4a4f3527cb757519a163fcbc4
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date: Mon, 24 May 2021 01:43:56 +0200
More work
Diffstat:
6 files changed, 99 insertions(+), 28 deletions(-)
diff --git a/examples/.clang-format b/examples/.clang-format
@@ -38,9 +38,9 @@ BraceWrapping:
BeforeCatch: true
BeforeElse: true
IndentBraces: false
- SplitEmptyFunction: false
- SplitEmptyRecord: false
- SplitEmptyNamespace: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: true
+ SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
diff --git a/examples/plugins/clap-entry.cc b/examples/plugins/clap-entry.cc
@@ -1,15 +1,35 @@
#include <clap/all.h>
+#include <cstring>
#include <exception>
+#include <functional>
#include <sstream>
#include <vector>
#include "gain/gain.hh"
-static std::vector<const clap_plugin_descriptor *> g_plugins;
+struct PluginEntry {
+ using create_func = std::function<clap_plugin *(clap_host *)>;
+
+ PluginEntry(const clap_plugin_descriptor *d, create_func &&func)
+ : desc(d), create(std::move(func)) {}
+
+ const clap_plugin_descriptor * desc;
+ std::function<clap_plugin *(clap_host *)> create;
+};
+
+static std::vector<PluginEntry> g_plugins;
+
+template <typename T>
+static void addPlugin() {
+ g_plugins.emplace_back(T::descriptor(), [](clap_host *host) -> clap_plugin * {
+ auto plugin = new T(host);
+ return plugin->clapPlugin();
+ });
+}
static bool clap_init(const char *plugin_path) {
- g_plugins.push_back(&g_gain_plugin_descriptor);
+ addPlugin<Gain>();
return true;
}
@@ -24,10 +44,15 @@ static const clap_plugin_descriptor *clap_get_plugin_descriptor(int32_t index) {
throw std::invalid_argument(msg.str());
}
- return g_plugins[index];
+ return g_plugins[index].desc;
}
-static clap_plugin *clap_create_plugin(clap_host *host, const char *plugin_id) { return nullptr; }
+static clap_plugin *clap_create_plugin(clap_host *host, const char *plugin_id) {
+ for (auto &entry : g_plugins)
+ if (!strcmp(entry.desc->id, plugin_id))
+ return entry.create(host);
+ return nullptr;
+}
CLAP_EXPORT const struct clap_plugin_entry clap_plugin_entry = {
clap_init,
diff --git a/examples/plugins/gain/gain.cc b/examples/plugins/gain/gain.cc
@@ -1,15 +1,51 @@
#include "gain.hh"
-const clap_plugin_descriptor g_gain_plugin_descriptor = {
- CLAP_VERSION,
- "com.github.free-audio.clap.gain",
- "gain",
- "clap",
- "https://github.com/free-audio/clap",
- nullptr,
- nullptr,
- "0.1",
- "example gain plugin",
- "mix;gain",
- CLAP_PLUGIN_AUDIO_EFFECT
-};
-\ No newline at end of file
+const clap_plugin_descriptor *Gain::descriptor() {
+ static const clap_plugin_descriptor desc = {
+
+ CLAP_VERSION,
+ "com.github.free-audio.clap.gain",
+ "gain",
+ "clap",
+ "https://github.com/free-audio/clap",
+ nullptr,
+ nullptr,
+ "0.1",
+ "example gain plugin",
+ "mix;gain",
+ CLAP_PLUGIN_AUDIO_EFFECT
+
+ };
+ return &desc;
+}
+
+Gain::Gain(clap_host *host) : Plugin(descriptor(), host) {}
+
+bool Gain::init() {
+ if (canUseTrackInfo()) {
+ clap_track_info info;
+ if (hostTrackInfo_->get_track_info(host_, &info))
+ {
+ channelCount_ = info.channel_count;
+ channelMap_ = info.channel_map;
+ }
+ }
+
+ return true;
+}
+
+clap_process_status Gain::process(const clap_process *process)
+{
+ auto **in = process->audio_inputs[0].data32;
+ auto **out = process->audio_outputs[0].data32;
+
+ float k = 1;
+
+ for (int i = 0; i < process->frames_count; ++i)
+ {
+ for (int j = 0; j < channelCount_; ++j)
+ out[j][i] = k * in[j][i];
+ }
+
+ return CLAP_PROCESS_CONTINUE_IF_NOT_QUIET;
+}
+\ No newline at end of file
diff --git a/examples/plugins/gain/gain.hh b/examples/plugins/gain/gain.hh
@@ -4,10 +4,16 @@
#include "../plugin.hh"
-class Gain : public Plugin
-{
+class Gain : public Plugin {
public:
- Gain(clap_host *host);
-};
+ Gain(clap_host *host);
-extern const clap_plugin_descriptor g_gain_plugin_descriptor;
-\ No newline at end of file
+ static const clap_plugin_descriptor *descriptor();
+
+ bool init() override;
+ clap_process_status process(const clap_process *process) override;
+
+private:
+ int channelCount_ = 2;
+ int channelMap_ = CLAP_CHMAP_STEREO;
+};
+\ No newline at end of file
diff --git a/examples/plugins/plugin.hh b/examples/plugins/plugin.hh
@@ -6,6 +6,9 @@
#include <clap/all.h>
class Plugin {
+public:
+ clap_plugin *clapPlugin() noexcept { return &plugin_; }
+
protected:
Plugin(const clap_plugin_descriptor *desc, clap_host *host);
virtual ~Plugin() = default;
@@ -50,6 +53,7 @@ protected:
/////////////////////////////////
bool canUseHostLog() const noexcept;
bool canUseThreadCheck() const noexcept;
+ bool canUseTrackInfo() const noexcept;
/////////////////////
// Thread Checking //
diff --git a/include/clap/ext/draft/track-info.h b/include/clap/ext/draft/track-info.h
@@ -23,7 +23,7 @@ typedef struct clap_track_info {
typedef struct clap_plugin_track_info {
// [main-thread]
- void (*set_track_info)(clap_plugin *plugin, const clap_track_info *info);
+ void (*track_info_changed)(clap_plugin *plugin);
} clap_plugin_track_info;
typedef struct clap_host_track_info {