To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / Test.cpp @ 8:3019cb6b538d

History | View | Annotate | Download (7.29 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2

    
3
/*
4
    Vamp Plugin Fuzz Tester
5
    Chris Cannam, cannam@all-day-breakfast.com
6
    Centre for Digital Music, Queen Mary, University of London.
7
    Copyright 2009 QMUL.
8

9
    This program loads a Vamp plugin and tests its susceptibility to a
10
    number of common pitfalls, including handling of extremes of input
11
    data.  If you can think of any additional useful tests that are
12
    easily added, please send them to me.
13
  
14
    Permission is hereby granted, free of charge, to any person
15
    obtaining a copy of this software and associated documentation
16
    files (the "Software"), to deal in the Software without
17
    restriction, including without limitation the rights to use, copy,
18
    modify, merge, publish, distribute, sublicense, and/or sell copies
19
    of the Software, and to permit persons to whom the Software is
20
    furnished to do so, subject to the following conditions:
21

22
    The above copyright notice and this permission notice shall be
23
    included in all copies or substantial portions of the Software.
24

25
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
29
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32

33
    Except as contained in this notice, the names of the Centre for
34
    Digital Music; Queen Mary, University of London; and Chris Cannam
35
    shall not be used in advertising or otherwise to promote the sale,
36
    use or other dealings in this Software without prior written
37
    authorization.
38
*/
39

    
40
#include "Test.h"
41

    
42
#include <vamp-hostsdk/PluginLoader.h>
43

    
44
using namespace Vamp;
45
using namespace Vamp::HostExt;
46

    
47
#include <cmath>
48

    
49
Test::Test() { }
50
Test::~Test() { }
51

    
52
Plugin *
53
Test::load(std::string key, float rate)
54
{
55
    return PluginLoader::getInstance()->loadPlugin
56
        (key, rate, PluginLoader::ADAPT_ALL);
57
}
58

    
59
float **
60
Test::createBlock(size_t channels, size_t blocksize)
61
{
62
    float **b = new float *[channels];
63
    for (size_t c = 0; c < channels; ++c) {
64
        b[c] = new float[blocksize];
65
    }
66
    return b;
67
}
68

    
69
void
70
Test::destroyBlock(float **blocks, size_t channels)
71
{
72
    for (size_t c = 0; c < channels; ++c) {
73
        delete[] blocks[c];
74
    }
75
    delete[] blocks;
76
}
77

    
78
float **
79
Test::createTestAudio(size_t channels, size_t blocksize, size_t blocks)
80
{
81
    float **b = new float *[channels];
82
    for (size_t c = 0; c < channels; ++c) {
83
        b[c] = new float[blocksize * blocks];
84
        for (int i = 0; i < int(blocksize * blocks); ++i) {
85
            b[c][i] = sinf(float(i) / 10.f);
86
            if (i == 5005 || i == 20002) {
87
                b[c][i-2] = 0;
88
                b[c][i-1] = -1;
89
                b[c][i] = 1;
90
            }
91
        }
92
    }
93
    return b;
94
}
95

    
96
void
97
Test::destroyTestAudio(float **b, size_t channels)
98
{
99
    for (size_t c = 0; c < channels; ++c) {
100
        delete[] b[c];
101
    }
102
    delete[] b;
103
}
104

    
105
bool
106
Test::initDefaults(Plugin *p, size_t &channels, size_t &step, size_t &block,
107
                   Results &r)
108
{
109
    channels = p->getMinChannelCount();
110
    block = p->getPreferredBlockSize();
111
    step = p->getPreferredStepSize();
112
    if (block == 0) block = 1024;
113
    if (step == 0) {
114
        if (p->getInputDomain() == Plugin::FrequencyDomain) step = block/2;
115
        else step = block;
116
    }
117
    if (!p->initialise(channels, step, block)) {
118
        r.push_back(error("initialisation with default values failed"));
119
        return false;
120
    }
121
    return true;
122
}
123

    
124
bool
125
Test::initAdapted(Plugin *p, size_t &channels, size_t step, size_t block,
126
                  Results &r)
127
{
128
    channels = p->getMinChannelCount();
129
    if (!p->initialise(channels, step, block)) {
130
        r.push_back(error("initialisation failed"));
131
        return false;
132
    }
133
    return true;
134
}
135

    
136
void
137
Test::appendFeatures(Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
138
{
139
    for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
140
        int output = i->first;
141
        const Plugin::FeatureList &fl = i->second;
142
        Plugin::FeatureList &target = a[output];
143
        for (Plugin::FeatureList::const_iterator j = fl.begin(); j != fl.end(); ++j) {
144
            target.push_back(*j);
145
        }
146
    }
147
}
148

    
149
bool
150
Test::allFeaturesValid(const Plugin::FeatureSet &b)
151
{
152
    for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
153
        for (int j = 0; j < (int)i->second.size(); ++j) {
154
            if (i->second[j].values.empty()) continue;
155
            for (int k = 0; k < (int)i->second[j].values.size(); ++k) {
156
                if (isnan(i->second[j].values[k]) ||
157
                    isinf(i->second[j].values[k])) {
158
                    return false;
159
                }
160
            }
161
        }
162
    }
163
    return true;
164
}
165

    
166
void
167
Test::dump(const Plugin::FeatureSet &fs)
168
{
169
    for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
170
         fsi != fs.end(); ++fsi) {
171
        int output = fsi->first;
172
        std::cout << "Output " << output << ":" << std::endl;
173
        const Plugin::FeatureList &fl = fsi->second;
174
        for (int i = 0; i < (int)fl.size(); ++i) {
175
            std::cout << "  Feature " << i << ":" << std::endl;
176
            const Plugin::Feature &f = fl[i];
177
            std::cout << "    Timestamp: " << (f.hasTimestamp ? "(none)" : f.timestamp.toText()) << std::endl;
178
            std::cout << "    Duration: " << (f.hasDuration ? "(none)" : f.duration.toText()) << std::endl;
179
            std::cout << "    Label: " << (f.label == "" ? "(none)" : f.label) << std::endl;
180
            std::cout << "    Value: " << (f.values.empty() ? "(none)" : "");
181
            for (int j = 0; j < (int)f.values.size(); ++j) {
182
                std::cout << f.values[j] << " ";
183
            }
184
            std::cout << std::endl;
185
        }
186
    }
187
}
188

    
189
void
190
Test::dump(const Result &r,
191
           const Plugin::FeatureSet &a,
192
           const Plugin::FeatureSet &b)
193
{
194
    std::cout << r.message() << std::endl;
195
    std::cout << "\nFirst result set:" << std::endl;
196
    dump(a);
197
    std::cout << "\nSecond result set:" << std::endl;
198
    dump(b);
199
    std::cout << std::endl;
200
}
201

    
202
bool
203
operator==(const Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
204
{
205
    if (a.size() != b.size()) return false;
206
    for (Plugin::FeatureSet::const_iterator ai = a.begin();
207
         ai != a.end(); ++ai) {
208
        int output = ai->first;
209
        Plugin::FeatureSet::const_iterator bi = b.find(output);
210
        if (bi == b.end()) return false;
211
        if (!(ai->second == bi->second)) return false;
212
    }
213
    return true;
214
}
215

    
216
bool
217
operator==(const Plugin::FeatureList &a, const Plugin::FeatureList &b)
218
{
219
    if (a.size() != b.size()) return false;
220
    for (int i = 0; i < (int)a.size(); ++i) {
221
        if (!(a[i] == b[i])) return false;
222
    }
223
    return true;
224
}
225

    
226
bool
227
operator==(const Plugin::Feature &a, const Plugin::Feature &b)
228
{
229
    if (a.hasTimestamp != b.hasTimestamp) return false;
230
    if (a.hasTimestamp && (a.timestamp != b.timestamp)) return false;
231
    if (a.hasDuration != b.hasDuration) return false;
232
    if (a.hasDuration && (a.duration != b.duration)) return false;
233
    if (a.values != b.values) return false;
234
    if (a.label != b.label) return false;
235
    return true;
236
}
237