3 #include "vamp-hostsdk/PluginLoader.h"
5 #include <glibmm/miscutils.h>
6 #include <glibmm/fileutils.h>
7 #include <glib/gstdio.h> // for g_remove()
10 #include "pbd/failed_constructor.h"
12 #include "ardour/audioanalyser.h"
13 #include "ardour/readable.h"
22 using namespace ARDOUR
;
24 AudioAnalyser::AudioAnalyser (float sr
, AnalysisPluginKey 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 ()
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
);
51 error
<< string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key
) << endmsg
;
55 /* we asked for the buffering adapter, so set the blocksize to
56 something that makes for efficient disk i/o
62 if (plugin
->getMinChannelCount() > 1) {
67 if (!plugin
->initialise (1, stepsize
, bufsize
)) {
76 AudioAnalyser::reset ()
84 AudioAnalyser::analyse (const string
& path
, Readable
* src
, uint32_t channel
)
87 Plugin::FeatureSet features
;
91 framecnt_t len
= src
->readable_length();
93 float* bufs
[1] = { 0 };
98 /* store data in tmp file, not the real one */
103 ofile
.open (tmp_path
.c_str());
109 data
= new Sample
[bufsize
];
116 /* read from source */
118 to_read
= min ((len
- pos
), (framecnt_t
) bufsize
);
120 if (src
->read (data
, pos
, to_read
, channel
) != to_read
) {
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
))) {
136 pos
+= min (stepsize
, to_read
);
143 /* finish up VAMP plugin */
145 features
= plugin
->getRemainingFeatures ();
147 if (use_features (features
, (path
.empty() ? &ofile
: 0))) {
154 /* works even if it has not been opened */
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());