comparison examples/PowerSpectrum.cpp @ 241:70e6826adc64

* Add Power Spectrum example plugin
author cannam
date Mon, 10 Nov 2008 17:28:54 +0000
parents
children 88ef5ffdbe8d
comparison
equal deleted inserted replaced
240:7b90fe049d04 241:70e6826adc64
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),
47 m_blockSize(0)
48 {
49 }
50
51 PowerSpectrum::~PowerSpectrum()
52 {
53 }
54
55 string
56 PowerSpectrum::getIdentifier() const
57 {
58 return "powerspectrum";
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";
71 }
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;
83 }
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 stepSize, size_t blockSize)
93 {
94 if (channels < getMinChannelCount() ||
95 channels > getMaxChannelCount()) return false;
96
97 m_blockSize = blockSize;
98
99 return true;
100 }
101
102 void
103 PowerSpectrum::reset()
104 {
105 }
106
107 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 d.binCount = m_blockSize / 2 + 1;
119 d.hasKnownExtents = false;
120 d.isQuantized = false;
121 d.sampleType = OutputDescriptor::OneSamplePerStep;
122 list.push_back(d);
123
124 return list;
125 }
126
127 PowerSpectrum::FeatureSet
128 PowerSpectrum::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
129 {
130 FeatureSet fs;
131
132 if (m_blockSize == 0) {
133 cerr << "ERROR: PowerSpectrum::process: Not initialised" << endl;
134 return fs;
135 }
136
137 size_t n = m_blockSize / 2 + 1;
138 const float *fbuf = inputBuffers[0];
139
140 Feature feature;
141 feature.hasTimestamp = false;
142 feature.values.reserve(n); // optional
143
144 for (size_t i = 0; i < n; ++i) {
145
146 double real = fbuf[i * 2];
147 double imag = fbuf[i * 2 + 1];
148
149 feature.values.push_back(real * real + imag * imag);
150 }
151
152 fs[0].push_back(feature);
153
154 return fs;
155 }
156
157 PowerSpectrum::FeatureSet
158 PowerSpectrum::getRemainingFeatures()
159 {
160 return FeatureSet();
161 }
162