7b649aae896266ffb12c309565825ebe119a384c
[ardour.git] / libs / ardour / audioanalyser.cc
1 #include <cstring>
2
3 #include "vamp-hostsdk/PluginLoader.h"
4
5 #include <glibmm/miscutils.h>
6 #include <glibmm/fileutils.h>
7 #include <glib/gstdio.h> // for g_remove()
8
9 #include "pbd/error.h"
10
11 #include "ardour/audioanalyser.h"
12 #include "ardour/readable.h"
13 #include "ardour/readable.h"
14
15 #include <cstring>
16
17 #include "i18n.h"
18
19 using namespace std;
20 using namespace Vamp;
21 using namespace PBD;
22 using namespace ARDOUR;
23
24 AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
25         : sample_rate (sr)
26         , plugin_key (key)
27 {
28         /* create VAMP plugin and initialize */
29
30         if (initialize_plugin (plugin_key, sample_rate)) {
31                 error << string_compose (_("cannot load VAMP plugin \"%1\""), key) << endmsg;
32                 throw failed_constructor();
33         }
34 }
35
36 AudioAnalyser::~AudioAnalyser ()
37 {
38         delete plugin;
39 }
40
41 int
42 AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
43 {
44         using namespace Vamp::HostExt;
45
46         PluginLoader* loader (PluginLoader::getInstance());
47
48         plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
49
50         if (!plugin) {
51                 error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
52                 return -1;
53         }
54
55         /* we asked for the buffering adapter, so set the blocksize to
56            something that makes for efficient disk i/o
57         */
58
59         bufsize = 65536;
60         stepsize = bufsize;
61
62         if (plugin->getMinChannelCount() > 1) {
63                 delete plugin;
64                 return -1;
65         }
66
67         if (!plugin->initialise (1, stepsize, bufsize)) {
68                 delete plugin;
69                 return -1;
70         }
71
72         return 0;
73 }
74
75 void
76 AudioAnalyser::reset ()
77 {
78         if (plugin) {
79                 plugin->reset ();
80         }
81 }
82
83 int
84 AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
85 {
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;
95
96         if (!path.empty()) {
97
98                 /* store data in tmp file, not the real one */
99
100                 tmp_path = path;
101                 tmp_path += ".tmp";
102
103                 ofile.open (tmp_path.c_str());
104                 if (!ofile) {
105                         goto out;
106                 }
107         }
108
109         data = new Sample[bufsize];
110         bufs[0] = data;
111
112         while (!done) {
113
114                 nframes_t to_read;
115
116                 /* read from source */
117
118                 to_read = min ((len - pos), (framecnt_t) bufsize);
119
120                 if (src->read (data, pos, to_read, channel) != to_read) {
121                         goto out;
122                 }
123
124                 /* zero fill buffer if necessary */
125
126                 if (to_read != bufsize) {
127                         memset (data + to_read, 0, (bufsize - to_read) * sizeof (Sample));
128                 }
129
130                 features = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
131
132                 if (use_features (features, (path.empty() ? 0 : &ofile))) {
133                         goto out;
134                 }
135
136                 pos += min (stepsize, to_read);
137
138                 if (pos >= len) {
139                         done = true;
140                 }
141         }
142
143         /* finish up VAMP plugin */
144
145         features = plugin->getRemainingFeatures ();
146
147         if (use_features (features, (path.empty() ? &ofile : 0))) {
148                 goto out;
149         }
150
151         ret = 0;
152
153   out:
154         /* works even if it has not been opened */
155         ofile.close ();
156
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());
162         }
163
164         delete [] data;
165
166         return ret;
167 }
168