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