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 "pbd/gstdio_compat.h"
25 #include <glibmm/miscutils.h>
26 #include <glibmm/fileutils.h>
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 };
114 data = new Sample[bufsize];
121 /* read from source */
123 to_read = min ((len - pos), (framecnt_t) bufsize);
125 if (src->read (data, pos, to_read, channel) != to_read) {
129 /* zero fill buffer if necessary */
131 if (to_read != bufsize) {
132 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
135 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
137 if (use_features (features, (path.empty() ? 0 : &outss))) {
141 pos += min (stepsize, to_read);
148 /* finish up VAMP plugin */
150 features = plugin->getRemainingFeatures ();
152 if (use_features (features, (path.empty() ? 0 : &outss))) {
160 g_file_set_contents (path.c_str(), outss.str().c_str(), -1, NULL);