cannam@150: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
cannam@150: /*
cannam@150:   Piper C++
cannam@150: 
cannam@150:   An API for audio analysis and feature extraction plugins.
cannam@150: 
cannam@150:   Centre for Digital Music, Queen Mary, University of London.
cannam@150:   Copyright 2006-2016 Chris Cannam and QMUL.
cannam@150:   
cannam@150:   Permission is hereby granted, free of charge, to any person
cannam@150:   obtaining a copy of this software and associated documentation
cannam@150:   files (the "Software"), to deal in the Software without
cannam@150:   restriction, including without limitation the rights to use, copy,
cannam@150:   modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@150:   of the Software, and to permit persons to whom the Software is
cannam@150:   furnished to do so, subject to the following conditions:
cannam@150: 
cannam@150:   The above copyright notice and this permission notice shall be
cannam@150:   included in all copies or substantial portions of the Software.
cannam@150: 
cannam@150:   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@150:   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@150:   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@150:   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@150:   ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@150:   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@150:   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@150: 
cannam@150:   Except as contained in this notice, the names of the Centre for
cannam@150:   Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@150:   shall not be used in advertising or otherwise to promote the sale,
cannam@150:   use or other dealings in this Software without prior written
cannam@150:   authorization.
cannam@150: */
cannam@150: 
cannam@150: #include "ProcessQtTransport.h"
cannam@150: #include "CapnpRRClient.h"
cannam@208: #include "PiperAutoPlugin.h"
cannam@150: 
cannam@150: #include <stdexcept>
cannam@150: 
cannam@150: using std::cerr;
cannam@150: using std::endl;
cannam@150: using std::exception;
cannam@150: using std::vector;
cannam@150: 
cannam@150: int main(int argc, char **argv)
cannam@150: {
cannam@150:     if (argc != 2) {
cannam@150:         cerr << "Usage: " << argv[0] << " <server-executable-path>" << endl;
cannam@150:         return 2;
cannam@150:     }
cannam@150: 
cannam@150:     try {
cannam@150:         piper_vamp::client::ProcessQtTransport transport(argv[1], "capnp", nullptr);
cannam@150:         if (!transport.isOK()) {
cannam@150:             cerr << "ERROR: Transport failed to start" << endl;
cannam@150:             return 1;
cannam@150:         }
cannam@150:             
cannam@150:         piper_vamp::client::CapnpRRClient client(&transport, nullptr);
cannam@150: 
cannam@207:         piper_vamp::ListResponse lr = client.list({});
cannam@150:         cerr << "Plugins available:" << endl;
cannam@150:         int i = 1;
cannam@150:         for (const auto &p: lr.available) {
cannam@150:             cerr << i++ << ". [" << p.pluginKey << "] " << p.basic.name << endl;
cannam@150:         }
cannam@150:     
cannam@150:         piper_vamp::LoadRequest req;
cannam@150:         req.pluginKey = "vamp-example-plugins:zerocrossing";
cannam@150:         req.inputSampleRate = 16;
cannam@207:         piper_vamp::LoadResponse resp = client.load(req);
cannam@150:         Vamp::Plugin *plugin = resp.plugin;
cannam@150:     
cannam@150:         if (!plugin->initialise(1, 4, 4)) {
cannam@150:             cerr << "initialisation failed" << endl;
cannam@150:         } else {
cannam@150:             vector<float> buf = { 1.0, -1.0, 1.0, -1.0 };
cannam@150:             float *bd = buf.data();
cannam@150:             Vamp::Plugin::FeatureSet features = plugin->process
cannam@150:                 (&bd, Vamp::RealTime::zeroTime);
cannam@150:             cerr << "results for output 0:" << endl;
cannam@150:             auto fl(features[0]);
cannam@150:             for (const auto &f: fl) {
cannam@150:                 cerr << f.values[0] << endl;
cannam@150:             }
cannam@150:         }
cannam@150: 
cannam@150:         (void)plugin->getRemainingFeatures();
cannam@150: 
cannam@150:         cerr << "calling reset..." << endl;
cannam@150:         plugin->reset();
cannam@150:         cerr << "...round 2!" << endl;
cannam@150: 
cannam@150:         vector<float> buf = { 1.0, -1.0, 1.0, -1.0 };
cannam@150:         float *bd = buf.data();
cannam@150:         Vamp::Plugin::FeatureSet features = plugin->process
cannam@150:             (&bd, Vamp::RealTime::zeroTime);
cannam@150:         cerr << "results for output 0:" << endl;
cannam@150:         auto fl(features[0]);
cannam@150:         for (const auto &f: fl) {
cannam@150:             cerr << f.values[0] << endl;
cannam@150:         }
cannam@150:     
cannam@150:         (void)plugin->getRemainingFeatures();
cannam@150: 
cannam@150:         delete plugin;
cannam@150: 
cannam@208:         // Let's try a crazy PiperAutoPlugin
cannam@150: 
cannam@208:         piper_vamp::client::PiperAutoPlugin ap
cannam@150:             (argv[1], "vamp-example-plugins:zerocrossing", 16, 0, nullptr);
cannam@150:     
cannam@150:         if (!ap.isOK()) {
cannam@208:             cerr << "PiperAutoPlugin creation failed" << endl;
cannam@150:         } else {
cannam@150:             if (!ap.initialise(1, 4, 4)) {
cannam@150:                 cerr << "initialisation failed" << endl;
cannam@150:             } else {
cannam@150:                 vector<float> buf = { 1.0, -1.0, 1.0, -1.0 };
cannam@150:                 float *bd = buf.data();
cannam@150:                 Vamp::Plugin::FeatureSet features = ap.process
cannam@150:                     (&bd, Vamp::RealTime::zeroTime);
cannam@150:                 cerr << "results for output 0:" << endl;
cannam@150:                 auto fl(features[0]);
cannam@150:                 for (const auto &f: fl) {
cannam@150:                     cerr << f.values[0] << endl;
cannam@150:                 }
cannam@150:             }
cannam@150:         }
cannam@150:     } catch (const exception &e) {
cannam@150:         cerr << "ERROR: Exception caught: " << e.what() << endl;
cannam@150:         return 1;
cannam@150:     }
cannam@150: 
cannam@150:     return 0;
cannam@150: }
cannam@150: