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 @ 23:28097c1b3de4

History | View | Annotate | Download (7.35 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 <math.h>
48

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

    
52
Plugin *
53
Test::load(std::string key, float rate)
54
{
55
    Plugin *p = PluginLoader::getInstance()->loadPlugin
56
        (key, rate, PluginLoader::ADAPT_ALL);
57
    if (!p) throw FailedToLoadPlugin();
58
    return p;
59
}
60

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

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

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

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

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

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

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

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

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

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

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

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

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