annotate src/PiperAdapter.h @ 66:08d80862b69c

-ffast-math is not a great idea as a default flag, because some plugins won't work correctly with it. Also allow people to override these
author Chris Cannam
date Sat, 12 Nov 2016 09:58:24 +0000
parents 02efb2bbdaf7
children
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@43 4 Piper Vamp JSON Adapter
Chris@0 5
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7 Copyright 2015-2016 QMUL.
Chris@0 8
Chris@0 9 Permission is hereby granted, free of charge, to any person
Chris@0 10 obtaining a copy of this software and associated documentation
Chris@0 11 files (the "Software"), to deal in the Software without
Chris@0 12 restriction, including without limitation the rights to use, copy,
Chris@0 13 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@0 14 of the Software, and to permit persons to whom the Software is
Chris@0 15 furnished to do so, subject to the following conditions:
Chris@0 16
Chris@0 17 The above copyright notice and this permission notice shall be
Chris@0 18 included in all copies or substantial portions of the Software.
Chris@0 19
Chris@0 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@0 21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@0 22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@0 23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@0 24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@0 25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@0 26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@0 27
Chris@0 28 Except as contained in this notice, the names of the Centre for
Chris@0 29 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@0 30 shall not be used in advertising or otherwise to promote the sale,
Chris@0 31 use or other dealings in this Software without prior written
Chris@0 32 authorization.
Chris@0 33 */
Chris@0 34
Chris@43 35 #ifndef PIPER_ADAPTER_H
Chris@43 36 #define PIPER_ADAPTER_H
Chris@0 37
Chris@46 38 #include "vamp-support/PluginStaticData.h"
Chris@46 39 #include "vamp-support/PluginConfiguration.h"
Chris@46 40 #include "vamp-support/RequestResponse.h"
Chris@0 41
Chris@0 42 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
Chris@0 43 #include <vamp-hostsdk/PluginBufferingAdapter.h>
Chris@0 44 #include <vamp-hostsdk/PluginChannelAdapter.h>
Chris@0 45
Chris@46 46 namespace piper_vamp_js { //!!! not a good name for this namespace
Chris@0 47
Chris@43 48 class PiperAdapterInterface
Chris@0 49 {
Chris@0 50 public:
Chris@47 51 virtual std::string getLibraryName() const = 0;
Chris@46 52 virtual piper_vamp::PluginStaticData getStaticData() const = 0;
Chris@46 53 virtual piper_vamp::LoadResponse loadPlugin(piper_vamp::LoadRequest r) const = 0;
Chris@19 54 virtual Vamp::Plugin *createPlugin(float inputSampleRate) const = 0;
Chris@0 55 };
Chris@0 56
Chris@0 57 template <typename P>
Chris@43 58 class PiperAdapterBase : public PiperAdapterInterface
Chris@0 59 {
Chris@0 60 const int adaptInputDomain = 0x01;
Chris@0 61 const int adaptChannelCount = 0x02;
Chris@0 62 const int adaptBufferSize = 0x04;
Chris@19 63
Chris@19 64 protected:
Chris@43 65 PiperAdapterBase(std::string libname) : m_soname(libname) { }
Chris@0 66
Chris@0 67 public:
Chris@47 68 virtual std::string getLibraryName() const override {
Chris@47 69 return m_soname;
Chris@47 70 }
Chris@0 71
Chris@47 72 virtual piper_vamp::PluginStaticData getStaticData() const override {
Chris@19 73 Vamp::Plugin *p = createPlugin(44100.f);
Chris@46 74 auto data = piper_vamp::PluginStaticData::fromPlugin
Chris@19 75 (m_soname + ":" + p->getIdentifier(),
Chris@0 76 {}, //!!! todo: category - tricky one that
Chris@19 77 p);
Chris@19 78 delete p;
Chris@19 79 return data;
Chris@0 80 }
Chris@0 81
Chris@47 82 virtual piper_vamp::LoadResponse loadPlugin(piper_vamp::LoadRequest r)
Chris@47 83 const override {
Chris@0 84
Chris@0 85 // We assume the caller has guaranteed that the request is for
Chris@0 86 // the correct plugin (so we don't have to check the plugin
Chris@0 87 // key field here)
Chris@0 88
Chris@19 89 Vamp::Plugin *p = createPlugin(r.inputSampleRate);
Chris@0 90
Chris@0 91 if (r.adapterFlags & adaptInputDomain) {
Chris@0 92 if (p->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
Chris@0 93 p = new Vamp::HostExt::PluginInputDomainAdapter(p);
Chris@0 94 }
Chris@0 95 }
Chris@0 96
Chris@0 97 if (r.adapterFlags & adaptBufferSize) {
Chris@0 98 p = new Vamp::HostExt::PluginBufferingAdapter(p);
Chris@0 99 }
Chris@0 100
Chris@0 101 if (r.adapterFlags & adaptChannelCount) {
Chris@0 102 p = new Vamp::HostExt::PluginChannelAdapter(p);
Chris@0 103 }
Chris@0 104
Chris@46 105 piper_vamp::LoadResponse response;
Chris@0 106 response.plugin = p;
Chris@0 107
Chris@46 108 response.staticData = piper_vamp::PluginStaticData::fromPlugin
Chris@0 109 (m_soname + ":" + p->getIdentifier(),
Chris@0 110 {}, //!!! todo: category - tricky one that
Chris@0 111 p);
Chris@0 112
Chris@0 113 int defaultChannels = 0;
Chris@0 114 if (p->getMinChannelCount() == p->getMaxChannelCount()) {
Chris@0 115 defaultChannels = p->getMinChannelCount();
Chris@0 116 }
Chris@64 117
Chris@64 118 int defaultBlockSize = p->getPreferredBlockSize();
Chris@64 119 int defaultStepSize = p->getPreferredStepSize();
Chris@64 120
Chris@64 121 if (defaultBlockSize == 0) {
Chris@64 122 defaultBlockSize = 1024;
Chris@64 123 }
Chris@64 124 if (defaultStepSize == 0) {
Chris@64 125 if (p->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
Chris@64 126 defaultStepSize = defaultBlockSize / 2;
Chris@64 127 } else {
Chris@64 128 defaultStepSize = defaultBlockSize;
Chris@64 129 }
Chris@64 130 }
Chris@64 131
Chris@64 132 response.defaultConfiguration =
Chris@64 133 piper_vamp::PluginConfiguration::fromPlugin
Chris@0 134 (p,
Chris@0 135 defaultChannels,
Chris@64 136 defaultStepSize,
Chris@64 137 defaultBlockSize);
Chris@0 138
Chris@0 139 return response;
Chris@0 140 }
Chris@0 141
Chris@0 142 private:
Chris@0 143 std::string m_soname;
Chris@0 144 };
Chris@0 145
Chris@19 146 template <typename P>
Chris@43 147 class PiperAdapter : public PiperAdapterBase<P>
Chris@19 148 {
Chris@19 149 public:
Chris@43 150 PiperAdapter(std::string libname) : PiperAdapterBase<P>(libname) { }
Chris@19 151
Chris@47 152 virtual Vamp::Plugin *createPlugin(float inputSampleRate) const override {
Chris@19 153 return new P(inputSampleRate);
Chris@19 154 }
Chris@19 155 };
Chris@19 156
Chris@0 157 }
Chris@0 158
Chris@0 159 #endif
Chris@0 160