2 #include <vamp-sdk/hostext/PluginLoader.h>
3 #include <glibmm/miscutils.h>
4 #include <glibmm/fileutils.h>
5 #include <glib/gstdio.h> // for g_remove()
9 #include <ardour/audioanalyser.h>
10 #include <ardour/readable.h>
11 #include <ardour/readable.h>
20 using namespace ARDOUR;
22 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
26 /* create VAMP plugin and initialize */
28 if (initialize_plugin (plugin_key, sample_rate)) {
29 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
30 throw failed_constructor();
34 AudioAnalyser::~AudioAnalyser ()
40 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
42 using namespace Vamp::HostExt;
44 PluginLoader* loader (PluginLoader::getInstance());
46 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
49 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
53 /* we asked for the buffering adapter, so set the blocksize to
54 something that makes for efficient disk i/o
60 if (plugin->getMinChannelCount() > 1) {
65 if (!plugin->initialise (1, stepsize, bufsize)) {
74 AudioAnalyser::reset ()
82 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
85 Plugin::FeatureSet features;
89 nframes64_t len = src->readable_length();
91 float* bufs[1] = { 0 };
96 /* store data in tmp file, not the real one */
101 ofile.open (tmp_path.c_str());
107 data = new Sample[bufsize];
114 /* read from source */
116 to_read = min ((len - pos), bufsize);
118 if (src->read (data, pos, to_read, channel) != to_read) {
122 /* zero fill buffer if necessary */
124 if (to_read != bufsize) {
125 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
128 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
130 if (use_features (features, (path.empty() ? 0 : &ofile))) {
134 pos += min (stepsize, to_read);
141 /* finish up VAMP plugin */
143 features = plugin->getRemainingFeatures ();
145 if (use_features (features, (path.empty() ? &ofile : 0))) {
152 /* works even if it has not been opened */
156 g_remove (tmp_path.c_str());
157 } else if (!path.empty()) {
158 /* move the data file to the requested path */
159 g_rename (tmp_path.c_str(), path.c_str());