1 #include <vamp-sdk/hostext/PluginLoader.h>
2 #include <glibmm/miscutils.h>
3 #include <glibmm/fileutils.h>
4 #include <glib/gstdio.h> // for g_remove()
8 #include <ardour/audioanalyser.h>
9 #include <ardour/readable.h>
10 #include <ardour/readable.h>
17 using namespace ARDOUR;
19 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
23 /* create VAMP plugin and initialize */
25 if (initialize_plugin (plugin_key, sample_rate)) {
26 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
27 throw failed_constructor();
31 AudioAnalyser::~AudioAnalyser ()
37 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
39 using namespace Vamp::HostExt;
41 PluginLoader* loader (PluginLoader::getInstance());
43 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
46 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
50 /* we asked for the buffering adapter, so set the blocksize to
51 something that makes for efficient disk i/o
57 if (plugin->getMinChannelCount() > 1) {
62 if (!plugin->initialise (1, stepsize, bufsize)) {
71 AudioAnalyser::reset ()
79 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
82 Plugin::FeatureSet features;
86 nframes64_t len = src->readable_length();
88 float* bufs[1] = { 0 };
93 /* store data in tmp file, not the real one */
98 ofile.open (tmp_path.c_str());
104 data = new Sample[bufsize];
111 /* read from source */
113 to_read = min ((len - pos), bufsize);
115 if (src->read (data, pos, to_read, channel) != to_read) {
119 /* zero fill buffer if necessary */
121 if (to_read != bufsize) {
122 memset (data + to_read, 0, (bufsize - to_read));
125 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
127 if (use_features (features, (path.empty() ? 0 : &ofile))) {
138 /* finish up VAMP plugin */
140 features = plugin->getRemainingFeatures ();
142 if (use_features (features, (path.empty() ? &ofile : 0))) {
149 /* works even if it has not been opened */
153 g_remove (tmp_path.c_str());
154 } else if (!path.empty()) {
155 /* move the data file to the requested path */
156 g_rename (tmp_path.c_str(), path.c_str());