2 Copyright (C) 2012 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <vamp-hostsdk/PluginLoader.h>
24 #include <glibmm/miscutils.h>
25 #include <glibmm/fileutils.h>
26 #include <glib/gstdio.h> // for g_remove()
28 #include "pbd/error.h"
29 #include "pbd/failed_constructor.h"
31 #include "ardour/audioanalyser.h"
32 #include "ardour/readable.h"
41 using namespace ARDOUR;
43 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
47 /* create VAMP plugin and initialize */
49 if (initialize_plugin (plugin_key, sample_rate)) {
50 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
51 throw failed_constructor();
55 AudioAnalyser::~AudioAnalyser ()
61 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
63 using namespace Vamp::HostExt;
65 PluginLoader* loader (PluginLoader::getInstance());
67 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL_SAFE);
70 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
74 /* we asked for the buffering adapter, so set the blocksize to
75 something that makes for efficient disk i/o
81 if (plugin->getMinChannelCount() > 1) {
86 if (!plugin->initialise (1, stepsize, bufsize)) {
95 AudioAnalyser::reset ()
103 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
106 Plugin::FeatureSet features;
110 framecnt_t len = src->readable_length();
112 float* bufs[1] = { 0 };
117 /* store data in tmp file, not the real one */
122 ofile.open (tmp_path.c_str());
128 data = new Sample[bufsize];
135 /* read from source */
137 to_read = min ((len - pos), (framecnt_t) bufsize);
139 if (src->read (data, pos, to_read, channel) != to_read) {
143 /* zero fill buffer if necessary */
145 if (to_read != bufsize) {
146 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
149 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
151 if (use_features (features, (path.empty() ? 0 : &ofile))) {
155 pos += min (stepsize, to_read);
162 /* finish up VAMP plugin */
164 features = plugin->getRemainingFeatures ();
166 if (use_features (features, (path.empty() ? &ofile : 0))) {
173 /* works even if it has not been opened */
177 g_remove (tmp_path.c_str());
178 } else if (!path.empty()) {
179 /* move the data file to the requested path */
180 g_rename (tmp_path.c_str(), path.c_str());