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