summaryrefslogtreecommitdiff
path: root/libs/ardour/audioanalyser.cc
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2008-02-02 03:57:35 +0000
committerDavid Robillard <d@drobilla.net>2008-02-02 03:57:35 +0000
commit9f63ab9931e6478472853bdda58da47ea29ac125 (patch)
tree7edfb1d16f580e93501c24fa9f9648fe415f3745 /libs/ardour/audioanalyser.cc
parent85ea9028b52eefb34184deb0fbd4d3c7632a2c38 (diff)
Merge with trunk R2978.
git-svn-id: svn://localhost/ardour2/branches/3.0@2988 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/ardour/audioanalyser.cc')
-rw-r--r--libs/ardour/audioanalyser.cc157
1 files changed, 157 insertions, 0 deletions
diff --git a/libs/ardour/audioanalyser.cc b/libs/ardour/audioanalyser.cc
new file mode 100644
index 0000000000..4cc99a5d5e
--- /dev/null
+++ b/libs/ardour/audioanalyser.cc
@@ -0,0 +1,157 @@
+#include <vamp-sdk/hostext/PluginLoader.h>
+#include <glibmm/miscutils.h>
+#include <glibmm/fileutils.h>
+#include <glib/gstdio.h> // for g_remove()
+
+#include <pbd/error.h>
+
+#include <ardour/audioanalyser.h>
+#include <ardour/readable.h>
+#include <ardour/readable.h>
+
+#include "i18n.h"
+
+using namespace std;
+using namespace Vamp;
+using namespace PBD;
+using namespace ARDOUR;
+
+AudioAnalyser::AudioAnalyser (float sr, AnalysisPluginKey key)
+ : sample_rate (sr)
+ , plugin (0)
+ , plugin_key (key)
+{
+}
+
+AudioAnalyser::~AudioAnalyser ()
+{
+}
+
+int
+AudioAnalyser::initialize_plugin (AnalysisPluginKey key, float sr)
+{
+ using namespace Vamp::HostExt;
+
+ PluginLoader* loader (PluginLoader::getInstance());
+
+ plugin = loader->loadPlugin (key, sr, PluginLoader::ADAPT_ALL);
+
+ if (!plugin) {
+ error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
+ return -1;
+ }
+
+ /* we asked for the buffering adapter, so set the blocksize to
+ something that makes for efficient disk i/o
+ */
+
+ bufsize = 65536;
+ stepsize = bufsize;
+
+ if (plugin->getMinChannelCount() > 1) {
+ delete plugin;
+ return -1;
+ }
+
+ if (!plugin->initialise (1, stepsize, bufsize)) {
+ delete plugin;
+ return -1;
+ }
+
+ return 0;
+}
+
+void
+AudioAnalyser::reset ()
+{
+ if (plugin) {
+ plugin->reset ();
+ }
+}
+
+int
+AudioAnalyser::analyse (const string& path, Readable* src, uint32_t channel)
+{
+ ofstream ofile;
+ Plugin::FeatureSet onsets;
+ int ret = -1;
+ bool done = false;
+ Sample* data = 0;
+ nframes64_t len = src->readable_length();
+ nframes64_t pos = 0;
+ float* bufs[1] = { 0 };
+
+ if (!path.empty()) {
+ ofile.open (path.c_str());
+ if (!ofile) {
+ goto out;
+ }
+ }
+
+ /* create VAMP percussion onset plugin and initialize */
+
+ if (plugin == 0) {
+ if (initialize_plugin (plugin_key, sample_rate)) {
+ goto out;
+ }
+ }
+
+ data = new Sample[bufsize];
+ bufs[0] = data;
+
+ while (!done) {
+
+ nframes64_t to_read;
+
+ /* read from source */
+
+ to_read = min ((len - pos), bufsize);
+
+ if (src->read (data, pos, to_read, channel) != to_read) {
+ cerr << "bad read\n";
+ goto out;
+ }
+
+ /* zero fill buffer if necessary */
+
+ if (to_read != bufsize) {
+ memset (data + to_read, 0, (bufsize - to_read));
+ }
+
+ onsets = plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
+
+ if (use_features (onsets, (path.empty() ? &ofile : 0))) {
+ goto out;
+ }
+
+ pos += stepsize;
+
+ if (pos >= len) {
+ done = true;
+ }
+ }
+
+ /* finish up VAMP plugin */
+
+ onsets = plugin->getRemainingFeatures ();
+
+ if (use_features (onsets, (path.empty() ? &ofile : 0))) {
+ goto out;
+ }
+
+ ret = 0;
+
+ out:
+ /* works even if it has not been opened */
+ ofile.close ();
+
+ if (ret) {
+ g_remove (path.c_str());
+ }
+ if (data) {
+ delete data;
+ }
+
+ return ret;
+}
+