Fixup prev commit (LV2 X11 UI) -- #7837
[ardour.git] / libs / ardour / audioanalyser.cc
1 /*
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>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <cstring>
23
24 #include <vamp-hostsdk/PluginLoader.h>
25
26 #include "pbd/gstdio_compat.h"
27 #include <glibmm/miscutils.h>
28 #include <glibmm/fileutils.h>
29
30 #include "pbd/error.h"
31 #include "pbd/failed_constructor.h"
32
33 #include "ardour/audioanalyser.h"
34 #include "ardour/readable.h"
35
36 #include <cstring>
37
38 #include "pbd/i18n.h"
39
40 using namespace std;
41 using namespace Vamp;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
46         : sample_rate (sr)
47         , plugin_key (key)
48 {
49         /* create VAMP plugin and initialize */
50
51         if (initialize_plugin (plugin_key, sample_rate)) {
52                 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
53                 throw failed_constructor();
54         }
55 }
56
57 AudioAnalyser::~AudioAnalyser ()
58 {
59         delete plugin;
60 }
61
62 int
63 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
64 {
65         using namespace Vamp::HostExt;
66
67         PluginLoader* loader (PluginLoader::getInstance());
68
69         plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL_SAFE);
70
71         if (!plugin) {
72                 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
73                 return -1;
74         }
75
76         /* we asked for the buffering adapter, so set the blocksize to
77            something that makes for efficient disk i/o
78         */
79
80         bufsize = 1024;
81         stepsize = 512;
82
83         if (plugin->getMinChannelCount() > 1) {
84                 delete plugin;
85                 return -1;
86         }
87
88         if (!plugin->initialise (1, stepsize, bufsize)) {
89                 delete plugin;
90                 return -1;
91         }
92
93         return 0;
94 }
95
96 void
97 AudioAnalyser::reset ()
98 {
99         if (plugin) {
100                 plugin->reset ();
101         }
102 }
103
104 int
105 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
106 {
107         stringstream outss;
108         Plugin::FeatureSet features;
109         int ret = -1;
110         bool done = false;
111         Sample* data = 0;
112         samplecnt_t len = src->readable_length();
113         samplepos_t pos = 0;
114         float* bufs[1] = { 0 };
115
116         data = new Sample[bufsize];
117         bufs[0] = data;
118
119         while (!done) {
120
121                 samplecnt_t to_read;
122
123                 /* read from source */
124
125                 to_read = min ((len - pos), (samplecnt_t) bufsize);
126
127                 if (src->read (data, pos, to_read, channel) != to_read) {
128                         goto out;
129                 }
130
131                 /* zero fill buffer if necessary */
132
133                 if (to_read != bufsize) {
134                         memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
135                 }
136
137                 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
138
139                 if (use_features (features, (path.empty() ? 0 : &outss))) {
140                         goto out;
141                 }
142
143                 pos += min (stepsize, to_read);
144
145                 if (pos >= len) {
146                         done = true;
147                 }
148         }
149
150         /* finish up VAMP plugin */
151
152         features = plugin->getRemainingFeatures ();
153
154         if (use_features (features, (path.empty() ? 0 : &outss))) {
155                 goto out;
156         }
157
158         ret = 0;
159
160   out:
161         if (!ret) {
162                 g_file_set_contents (path.c_str(), outss.str().c_str(), -1, NULL);
163         }
164
165         delete [] data;
166
167         return ret;
168 }
169