annotate Test.cpp @ 28:b1bc4d045a4b vamp-plugin-tester-v1.0

* Solaris build fixes
author cannam
date Thu, 24 Sep 2009 14:11:14 +0000
parents 28097c1b3de4
children f1e8e14e9c96
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp Plugin Fuzz Tester
cannam@0 5 Chris Cannam, cannam@all-day-breakfast.com
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
cannam@0 7 Copyright 2009 QMUL.
cannam@0 8
cannam@0 9 This program loads a Vamp plugin and tests its susceptibility to a
cannam@0 10 number of common pitfalls, including handling of extremes of input
cannam@0 11 data. If you can think of any additional useful tests that are
cannam@0 12 easily added, please send them to me.
cannam@0 13
cannam@0 14 Permission is hereby granted, free of charge, to any person
cannam@0 15 obtaining a copy of this software and associated documentation
cannam@0 16 files (the "Software"), to deal in the Software without
cannam@0 17 restriction, including without limitation the rights to use, copy,
cannam@0 18 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@0 19 of the Software, and to permit persons to whom the Software is
cannam@0 20 furnished to do so, subject to the following conditions:
cannam@0 21
cannam@0 22 The above copyright notice and this permission notice shall be
cannam@0 23 included in all copies or substantial portions of the Software.
cannam@0 24
cannam@0 25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@0 26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@0 27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@0 28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@0 29 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@0 30 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@0 31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@0 32
cannam@0 33 Except as contained in this notice, the names of the Centre for
cannam@0 34 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@0 35 shall not be used in advertising or otherwise to promote the sale,
cannam@0 36 use or other dealings in this Software without prior written
cannam@0 37 authorization.
cannam@0 38 */
cannam@0 39
cannam@0 40 #include "Test.h"
cannam@0 41
cannam@0 42 #include <vamp-hostsdk/PluginLoader.h>
cannam@0 43
cannam@0 44 using namespace Vamp;
cannam@0 45 using namespace Vamp::HostExt;
cannam@0 46
cannam@20 47 #include <math.h>
cannam@1 48
cannam@28 49 #ifdef __SUNPRO_CC
cannam@28 50 #include <ieeefp.h>
cannam@28 51 #define isinf(x) (!finite(x))
cannam@28 52 #endif
cannam@28 53
cannam@0 54 Test::Test() { }
cannam@0 55 Test::~Test() { }
cannam@0 56
cannam@0 57 Plugin *
cannam@0 58 Test::load(std::string key, float rate)
cannam@0 59 {
cannam@23 60 Plugin *p = PluginLoader::getInstance()->loadPlugin
cannam@0 61 (key, rate, PluginLoader::ADAPT_ALL);
cannam@23 62 if (!p) throw FailedToLoadPlugin();
cannam@23 63 return p;
cannam@0 64 }
cannam@0 65
cannam@1 66 float **
cannam@1 67 Test::createBlock(size_t channels, size_t blocksize)
cannam@1 68 {
cannam@1 69 float **b = new float *[channels];
cannam@1 70 for (size_t c = 0; c < channels; ++c) {
cannam@1 71 b[c] = new float[blocksize];
cannam@1 72 }
cannam@1 73 return b;
cannam@1 74 }
cannam@1 75
cannam@1 76 void
cannam@1 77 Test::destroyBlock(float **blocks, size_t channels)
cannam@1 78 {
cannam@1 79 for (size_t c = 0; c < channels; ++c) {
cannam@1 80 delete[] blocks[c];
cannam@1 81 }
cannam@1 82 delete[] blocks;
cannam@1 83 }
cannam@1 84
cannam@3 85 float **
cannam@3 86 Test::createTestAudio(size_t channels, size_t blocksize, size_t blocks)
cannam@3 87 {
cannam@3 88 float **b = new float *[channels];
cannam@3 89 for (size_t c = 0; c < channels; ++c) {
cannam@3 90 b[c] = new float[blocksize * blocks];
cannam@3 91 for (int i = 0; i < int(blocksize * blocks); ++i) {
cannam@3 92 b[c][i] = sinf(float(i) / 10.f);
cannam@3 93 if (i == 5005 || i == 20002) {
cannam@3 94 b[c][i-2] = 0;
cannam@3 95 b[c][i-1] = -1;
cannam@3 96 b[c][i] = 1;
cannam@3 97 }
cannam@3 98 }
cannam@3 99 }
cannam@3 100 return b;
cannam@3 101 }
cannam@3 102
cannam@3 103 void
cannam@3 104 Test::destroyTestAudio(float **b, size_t channels)
cannam@3 105 {
cannam@3 106 for (size_t c = 0; c < channels; ++c) {
cannam@3 107 delete[] b[c];
cannam@3 108 }
cannam@3 109 delete[] b;
cannam@3 110 }
cannam@3 111
cannam@1 112 bool
cannam@1 113 Test::initDefaults(Plugin *p, size_t &channels, size_t &step, size_t &block,
cannam@1 114 Results &r)
cannam@1 115 {
cannam@1 116 channels = p->getMinChannelCount();
cannam@1 117 block = p->getPreferredBlockSize();
cannam@1 118 step = p->getPreferredStepSize();
cannam@1 119 if (block == 0) block = 1024;
cannam@1 120 if (step == 0) {
cannam@1 121 if (p->getInputDomain() == Plugin::FrequencyDomain) step = block/2;
cannam@1 122 else step = block;
cannam@1 123 }
cannam@1 124 if (!p->initialise(channels, step, block)) {
cannam@1 125 r.push_back(error("initialisation with default values failed"));
cannam@1 126 return false;
cannam@1 127 }
cannam@1 128 return true;
cannam@1 129 }
cannam@1 130
cannam@3 131 bool
cannam@3 132 Test::initAdapted(Plugin *p, size_t &channels, size_t step, size_t block,
cannam@3 133 Results &r)
cannam@3 134 {
cannam@3 135 channels = p->getMinChannelCount();
cannam@3 136 if (!p->initialise(channels, step, block)) {
cannam@3 137 r.push_back(error("initialisation failed"));
cannam@3 138 return false;
cannam@3 139 }
cannam@3 140 return true;
cannam@3 141 }
cannam@3 142
cannam@0 143 void
cannam@0 144 Test::appendFeatures(Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
cannam@0 145 {
cannam@0 146 for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
cannam@0 147 int output = i->first;
cannam@0 148 const Plugin::FeatureList &fl = i->second;
cannam@0 149 Plugin::FeatureList &target = a[output];
cannam@0 150 for (Plugin::FeatureList::const_iterator j = fl.begin(); j != fl.end(); ++j) {
cannam@0 151 target.push_back(*j);
cannam@0 152 }
cannam@0 153 }
cannam@0 154 }
cannam@0 155
cannam@0 156 bool
cannam@1 157 Test::allFeaturesValid(const Plugin::FeatureSet &b)
cannam@1 158 {
cannam@1 159 for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
cannam@1 160 for (int j = 0; j < (int)i->second.size(); ++j) {
cannam@1 161 if (i->second[j].values.empty()) continue;
cannam@1 162 for (int k = 0; k < (int)i->second[j].values.size(); ++k) {
cannam@1 163 if (isnan(i->second[j].values[k]) ||
cannam@1 164 isinf(i->second[j].values[k])) {
cannam@1 165 return false;
cannam@1 166 }
cannam@1 167 }
cannam@1 168 }
cannam@1 169 }
cannam@1 170 return true;
cannam@1 171 }
cannam@1 172
cannam@3 173 void
cannam@3 174 Test::dump(const Plugin::FeatureSet &fs)
cannam@3 175 {
cannam@3 176 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
cannam@3 177 fsi != fs.end(); ++fsi) {
cannam@3 178 int output = fsi->first;
cannam@8 179 std::cout << "Output " << output << ":" << std::endl;
cannam@3 180 const Plugin::FeatureList &fl = fsi->second;
cannam@3 181 for (int i = 0; i < (int)fl.size(); ++i) {
cannam@8 182 std::cout << " Feature " << i << ":" << std::endl;
cannam@3 183 const Plugin::Feature &f = fl[i];
cannam@8 184 std::cout << " Timestamp: " << (f.hasTimestamp ? "(none)" : f.timestamp.toText()) << std::endl;
cannam@8 185 std::cout << " Duration: " << (f.hasDuration ? "(none)" : f.duration.toText()) << std::endl;
cannam@8 186 std::cout << " Label: " << (f.label == "" ? "(none)" : f.label) << std::endl;
cannam@8 187 std::cout << " Value: " << (f.values.empty() ? "(none)" : "");
cannam@3 188 for (int j = 0; j < (int)f.values.size(); ++j) {
cannam@8 189 std::cout << f.values[j] << " ";
cannam@3 190 }
cannam@8 191 std::cout << std::endl;
cannam@3 192 }
cannam@3 193 }
cannam@3 194 }
cannam@3 195
cannam@3 196 void
cannam@3 197 Test::dump(const Result &r,
cannam@3 198 const Plugin::FeatureSet &a,
cannam@3 199 const Plugin::FeatureSet &b)
cannam@3 200 {
cannam@8 201 std::cout << r.message() << std::endl;
cannam@8 202 std::cout << "\nFirst result set:" << std::endl;
cannam@3 203 dump(a);
cannam@8 204 std::cout << "\nSecond result set:" << std::endl;
cannam@3 205 dump(b);
cannam@8 206 std::cout << std::endl;
cannam@3 207 }
cannam@3 208
cannam@1 209 bool
cannam@0 210 operator==(const Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
cannam@0 211 {
cannam@0 212 if (a.size() != b.size()) return false;
cannam@0 213 for (Plugin::FeatureSet::const_iterator ai = a.begin();
cannam@0 214 ai != a.end(); ++ai) {
cannam@0 215 int output = ai->first;
cannam@0 216 Plugin::FeatureSet::const_iterator bi = b.find(output);
cannam@0 217 if (bi == b.end()) return false;
cannam@0 218 if (!(ai->second == bi->second)) return false;
cannam@0 219 }
cannam@0 220 return true;
cannam@0 221 }
cannam@0 222
cannam@0 223 bool
cannam@0 224 operator==(const Plugin::FeatureList &a, const Plugin::FeatureList &b)
cannam@0 225 {
cannam@0 226 if (a.size() != b.size()) return false;
cannam@0 227 for (int i = 0; i < (int)a.size(); ++i) {
cannam@0 228 if (!(a[i] == b[i])) return false;
cannam@0 229 }
cannam@0 230 return true;
cannam@0 231 }
cannam@0 232
cannam@0 233 bool
cannam@0 234 operator==(const Plugin::Feature &a, const Plugin::Feature &b)
cannam@0 235 {
cannam@0 236 if (a.hasTimestamp != b.hasTimestamp) return false;
cannam@0 237 if (a.hasTimestamp && (a.timestamp != b.timestamp)) return false;
cannam@0 238 if (a.hasDuration != b.hasDuration) return false;
cannam@0 239 if (a.hasDuration && (a.duration != b.duration)) return false;
cannam@0 240 if (a.values != b.values) return false;
cannam@0 241 if (a.label != b.label) return false;
cannam@0 242 return true;
cannam@0 243 }
cannam@0 244