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>
18 using namespace ARDOUR;
20 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
24 /* create VAMP plugin and initialize */
26 if (initialize_plugin (plugin_key, sample_rate)) {
27 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
28 throw failed_constructor();
32 AudioAnalyser::~AudioAnalyser ()
38 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
40 using namespace Vamp::HostExt;
42 PluginLoader* loader (PluginLoader::getInstance());
44 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
47 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
51 /* we asked for the buffering adapter, so set the blocksize to
52 something that makes for efficient disk i/o
58 if (plugin->getMinChannelCount() > 1) {
63 if (!plugin->initialise (1, stepsize, bufsize)) {
72 AudioAnalyser::reset ()
80 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
83 Plugin::FeatureSet features;
87 nframes64_t len = src->readable_length();
89 float* bufs[1] = { 0 };
94 /* store data in tmp file, not the real one */
99 ofile.open (tmp_path.c_str());
105 data = new Sample[bufsize];
112 /* read from source */
114 to_read = min ((len - pos), bufsize);
116 if (src->read (data, pos, to_read, channel) != to_read) {
120 /* zero fill buffer if necessary */
122 if (to_read != bufsize) {
123 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
126 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
128 if (use_features (features, (path.empty() ? 0 : &ofile))) {
132 pos += min (stepsize, to_read);
139 /* finish up VAMP plugin */
141 features = plugin->getRemainingFeatures ();
143 if (use_features (features, (path.empty() ? &ofile : 0))) {
150 /* works even if it has not been opened */
154 g_remove (tmp_path.c_str());
155 } else if (!path.empty()) {
156 /* move the data file to the requested path */
157 g_rename (tmp_path.c_str(), path.c_str());