Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / audioanalyser.cc
blobd477026a24df8998be326335c5d710176111ddac
1 #include <cstring>
3 #include "vamp-hostsdk/PluginLoader.h"
5 #include <glibmm/miscutils.h>
6 #include <glibmm/fileutils.h>
7 #include <glib/gstdio.h> // for g_remove()
9 #include "pbd/error.h"
10 #include "pbd/failed_constructor.h"
12 #include "ardour/audioanalyser.h"
13 #include "ardour/readable.h"
15 #include <cstring>
17 #include "i18n.h"
19 using namespace std;
20 using namespace Vamp;
21 using namespace PBD;
22 using namespace ARDOUR;
24 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
25 : sample_rate (sr)
26 , plugin_key (key)
28 /* create VAMP plugin and initialize */
30 if (initialize_plugin (plugin_key, sample_rate)) {
31 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
32 throw failed_constructor();
36 AudioAnalyser::~AudioAnalyser ()
38 delete plugin;
41 int
42 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
44 using namespace Vamp::HostExt;
46 PluginLoader* loader (PluginLoader::getInstance());
48 plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
50 if (!plugin) {
51 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
52 return -1;
55 /* we asked for the buffering adapter, so set the blocksize to
56 something that makes for efficient disk i/o
59 bufsize = 65536;
60 stepsize = bufsize;
62 if (plugin->getMinChannelCount() > 1) {
63 delete plugin;
64 return -1;
67 if (!plugin->initialise (1, stepsize, bufsize)) {
68 delete plugin;
69 return -1;
72 return 0;
75 void
76 AudioAnalyser::reset ()
78 if (plugin) {
79 plugin->reset ();
83 int
84 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
86 ofstream ofile;
87 Plugin::FeatureSet features;
88 int ret = -1;
89 bool done = false;
90 Sample* data = 0;
91 framecnt_t len = src->readable_length();
92 framepos_t pos = 0;
93 float* bufs[1] = { 0 };
94 string tmp_path;
96 if (!path.empty()) {
98 /* store data in tmp file, not the real one */
100 tmp_path = path;
101 tmp_path += ".tmp";
103 ofile.open (tmp_path.c_str());
104 if (!ofile) {
105 goto out;
109 data = new Sample[bufsize];
110 bufs[0] = data;
112 while (!done) {
114 framecnt_t to_read;
116 /* read from source */
118 to_read = min ((len - pos), (framecnt_t) bufsize);
120 if (src->read (data, pos, to_read, channel) != to_read) {
121 goto out;
124 /* zero fill buffer if necessary */
126 if (to_read != bufsize) {
127 memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
130 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
132 if (use_features (features, (path.empty() ? 0 : &ofile))) {
133 goto out;
136 pos += min (stepsize, to_read);
138 if (pos >= len) {
139 done = true;
143 /* finish up VAMP plugin */
145 features = plugin->getRemainingFeatures ();
147 if (use_features (features, (path.empty() ? &ofile : 0))) {
148 goto out;
151 ret = 0;
153 out:
154 /* works even if it has not been opened */
155 ofile.close ();
157 if (ret) {
158 g_remove (tmp_path.c_str());
159 } else if (!path.empty()) {
160 /* move the data file to the requested path */
161 g_rename (tmp_path.c_str(), path.c_str());
164 delete [] data;
166 return ret;