summaryrefslogtreecommitdiff
path: root/libs/ardour/lua_api.cc
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2016-10-03 03:51:53 +0200
committerRobin Gareus <robin@gareus.org>2016-10-03 03:52:51 +0200
commit330e69b5652676653d4c49d33e503617ad476194 (patch)
treef6f2f6eafb849e05fa3647d276fcb18f6c91ae7a /libs/ardour/lua_api.cc
parentaf289cab6231ec8785a2e69ab78f6257626aef9f (diff)
Add Vamp-plugin Lua bindings (work in progress)
Diffstat (limited to 'libs/ardour/lua_api.cc')
-rw-r--r--libs/ardour/lua_api.cc98
1 files changed, 97 insertions, 1 deletions
diff --git a/libs/ardour/lua_api.cc b/libs/ardour/lua_api.cc
index 201ca7de0e..2f6c995851 100644
--- a/libs/ardour/lua_api.cc
+++ b/libs/ardour/lua_api.cc
@@ -17,9 +17,11 @@
*
*/
#include <cstring>
+#include <vamp-hostsdk/PluginLoader.h>
-#include "pbd/error.h"
#include "pbd/compose.h"
+#include "pbd/error.h"
+#include "pbd/failed_constructor.h"
#include "ardour/lua_api.h"
#include "ardour/luaproc.h"
@@ -27,6 +29,7 @@
#include "ardour/plugin.h"
#include "ardour/plugin_insert.h"
#include "ardour/plugin_manager.h"
+#include "ardour/readable.h"
#include "LuaBridge/LuaBridge.h"
@@ -535,3 +538,96 @@ void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
break;
}
}
+
+
+LuaAPI::Vamp::Vamp (const std::string& key, float sample_rate)
+ : _plugin (0)
+ , _sample_rate (sample_rate)
+ , _bufsize (8192)
+ , _initialized (false)
+{
+ using namespace ::Vamp::HostExt;
+
+ PluginLoader* loader (PluginLoader::getInstance());
+ _plugin = loader->loadPlugin (key, _sample_rate, PluginLoader::ADAPT_ALL_SAFE);
+
+ if (!_plugin) {
+ PBD::error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
+ throw failed_constructor ();
+ }
+}
+
+LuaAPI::Vamp::~Vamp ()
+{
+ delete _plugin;
+}
+
+void
+LuaAPI::Vamp::reset ()
+{
+ _initialized = false;
+ if (_plugin) {
+ _plugin->reset ();
+ }
+}
+
+bool
+LuaAPI::Vamp::initialize ()
+{
+ if (!_plugin || _plugin->getMinChannelCount() > 1) {
+ return false;
+ }
+ if (!_plugin->initialise (1, _bufsize, _bufsize)) {
+ return false;
+ }
+ _initialized = true;
+ return true;
+}
+
+int
+LuaAPI::Vamp::analyze (boost::shared_ptr<ARDOUR::Readable> r, uint32_t channel, luabridge::LuaRef cb)
+{
+ if (!_initialized) {
+ if (!initialize ()) {
+ return -1;
+ }
+ }
+ assert (_initialized);
+
+ ::Vamp::Plugin::FeatureSet features;
+ float* data = new float[_bufsize];
+ float* bufs[1] = { data };
+
+ framecnt_t len = r->readable_length();
+ framepos_t pos = 0;
+
+ int rv = 0;
+ while (1) {
+ framecnt_t to_read = std::min ((len - pos), _bufsize);
+ if (r->read (data, pos, to_read, channel) != to_read) {
+ rv = -1;
+ break;
+ }
+ if (to_read != _bufsize) {
+ memset (data + to_read, 0, (_bufsize - to_read) * sizeof (float));
+ }
+
+ features = _plugin->process (bufs, ::Vamp::RealTime::fromSeconds ((double) pos / _sample_rate));
+
+ if (cb.type () == LUA_TFUNCTION) {
+ /* TODO existing "features" binding fails here
+ * std::map<int, std::vector<_VampHost::Vamp::Plugin::Feature> >
+ */
+ // cb (features, pos); // XXX
+ }
+
+ pos += to_read;
+
+ if (pos >= len) {
+ break;
+ }
+ }
+
+ delete [] data;
+ return rv;
+}