-
Notifications
You must be signed in to change notification settings - Fork 14
/
PowerSpectrum.cpp
170 lines (134 loc) · 4.1 KB
/
PowerSpectrum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Vamp
An API for audio analysis and feature extraction plugins.
Centre for Digital Music, Queen Mary, University of London.
Copyright 2008 QMUL.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the Centre for
Digital Music; Queen Mary, University of London; and Chris Cannam
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization.
*/
#include "PowerSpectrum.h"
using std::string;
using std::cerr;
using std::endl;
#include <math.h>
PowerSpectrum::PowerSpectrum(float inputSampleRate) :
Plugin(inputSampleRate),
m_blockSize(0)
{
}
PowerSpectrum::~PowerSpectrum()
{
}
string
PowerSpectrum::getIdentifier() const
{
return "powerspectrum";
}
string
PowerSpectrum::getName() const
{
return "Simple Power Spectrum";
}
string
PowerSpectrum::getDescription() const
{
return "Return the power spectrum of a signal";
}
string
PowerSpectrum::getMaker() const
{
return "Vamp SDK Example Plugins";
}
int
PowerSpectrum::getPluginVersion() const
{
return 2;
}
string
PowerSpectrum::getCopyright() const
{
return "Freely redistributable (BSD license)";
}
bool
PowerSpectrum::initialise(size_t channels, size_t, size_t blockSize)
{
if (channels < getMinChannelCount() ||
channels > getMaxChannelCount()) return false;
m_blockSize = blockSize;
return true;
}
void
PowerSpectrum::reset()
{
}
PowerSpectrum::OutputList
PowerSpectrum::getOutputDescriptors() const
{
OutputList list;
OutputDescriptor d;
d.identifier = "powerspectrum";
d.name = "Power Spectrum";
d.description = "Power values of the frequency spectrum bins calculated from the input signal";
d.unit = "V^2";
d.hasFixedBinCount = true;
if (m_blockSize == 0) {
// Just so as not to return "1". This is the bin count that
// would result from a block size of 1024, which is a likely
// default -- but the host should always set the block size
// before querying the bin count for certain.
d.binCount = 513;
} else {
d.binCount = m_blockSize / 2 + 1;
}
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
list.push_back(d);
return list;
}
PowerSpectrum::FeatureSet
PowerSpectrum::process(const float *const *inputBuffers, Vamp::RealTime)
{
FeatureSet fs;
if (m_blockSize == 0) {
cerr << "ERROR: PowerSpectrum::process: Not initialised" << endl;
return fs;
}
size_t n = m_blockSize / 2 + 1;
const float *fbuf = inputBuffers[0];
Feature feature;
feature.hasTimestamp = false;
feature.values.reserve(n); // optional
for (size_t i = 0; i < n; ++i) {
double real = fbuf[i * 2];
double imag = fbuf[i * 2 + 1];
feature.values.push_back(real * real + imag * imag);
}
fs[0].push_back(feature);
return fs;
}
PowerSpectrum::FeatureSet
PowerSpectrum::getRemainingFeatures()
{
return FeatureSet();
}