2 * Copyright (C) 2008-2010 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2008-2010 David Robillard <d@drobilla.net>
4 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <vamp-hostsdk/PluginLoader.h>
26 #include "pbd/gstdio_compat.h"
27 #include <glibmm/miscutils.h>
28 #include <glibmm/fileutils.h>
30 #include "pbd/error.h"
31 #include "pbd/failed_constructor.h"
33 #include "ardour/audioanalyser.h"
34 #include "ardour/readable.h"
43 using namespace ARDOUR;
45 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
49 /* create VAMP plugin and initialize */
51 if (initialize_plugin (plugin_key, sample_rate)) {
52 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
53 throw failed_constructor();
57 AudioAnalyser::~AudioAnalyser ()
63 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
65 using namespace Vamp::HostExt;
67 PluginLoader* loader (PluginLoader::getInstance());
69 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL_SAFE);
72 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
76 /* we asked for the buffering adapter, so set the blocksize to
77 something that makes for efficient disk i/o
83 if (plugin->getMinChannelCount() > 1) {
88 if (!plugin->initialise (1, stepsize, bufsize)) {
97 AudioAnalyser::reset ()
105 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
108 Plugin::FeatureSet features;
112 samplecnt_t len = src->readable_length();
114 float* bufs[1] = { 0 };
116 data = new Sample[bufsize];
123 /* read from source */
125 to_read = min ((len - pos), (samplecnt_t) bufsize);
127 if (src->read (data, pos, to_read, channel) != to_read) {
131 /* zero fill buffer if necessary */
133 if (to_read != bufsize) {
134 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
137 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
139 if (use_features (features, (path.empty() ? 0 : &outss))) {
143 pos += min (stepsize, to_read);
150 /* finish up VAMP plugin */
152 features = plugin->getRemainingFeatures ();
154 if (use_features (features, (path.empty() ? 0 : &outss))) {
162 g_file_set_contents (path.c_str(), outss.str().c_str(), -1, NULL);