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