PowerSpectrum.cpp
Go to the documentation of this file.
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 
52 {
53 }
54 
55 string
57 {
58  return "powerspectrum";
59 }
60 
61 string
63 {
64  return "Simple Power Spectrum";
65 }
66 
67 string
69 {
70  return "Return the power spectrum of a signal";
71 }
72 
73 string
75 {
76  return "Vamp SDK Example Plugins";
77 }
78 
79 int
81 {
82  return 1;
83 }
84 
85 string
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() ||
95  channels > getMaxChannelCount()) return false;
96 
97  m_blockSize = blockSize;
98 
99  return true;
100 }
101 
102 void
104 {
105 }
106 
109 {
110  OutputList list;
111 
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;
130  list.push_back(d);
131 
132  return list;
133 }
134 
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 
167 {
168  return FeatureSet();
169 }
170 
std::vector< OutputDescriptor > OutputList
virtual ~PowerSpectrum()
std::string getCopyright() const
Get the copyright statement or licensing summary for the plugin.
bool hasFixedBinCount
True if the output has the same number of values per sample for every output sample.
std::vector< float > values
Results for a single sample of this feature.
OutputList getOutputDescriptors() const
Get the outputs of this plugin.
std::map< int, FeatureList > FeatureSet
std::string getName() const
Get a human-readable name or title of the plugin.
std::string description
A human-readable short text describing the output.
std::string identifier
The name of the output, in computer-usable form.
FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp)
Process a single block of input data.
std::string getIdentifier() const
Get the computer-usable name of the plugin.
std::string unit
The unit of the output, in human-readable form.
std::string getDescription() const
Get a human-readable description for the plugin, typically a line of text that may optionally be disp...
std::string name
The human-readable name of the output.
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
bool hasTimestamp
True if an output feature has its own timestamp.
bool initialise(size_t channels, size_t stepSize, size_t blockSize)
Initialise a plugin to prepare it for use with the given number of input channels, step size (window increment, in sample frames) and block size (window size, in sample frames).
FeatureSet getRemainingFeatures()
After all blocks have been processed, calculate and return any remaining features derived from the co...
Results from each process() align with that call&#39;s block start.
virtual size_t getMaxChannelCount() const
Get the maximum supported number of input channels.
size_t binCount
The number of values per result of the output.
std::string getMaker() const
Get the name of the author or vendor of the plugin in human-readable form.
void reset()
Reset the plugin after use, to prepare it for another clean run.
bool isQuantized
True if the output values are quantized to a particular resolution.
int getPluginVersion() const
Get the version number of the plugin.
size_t m_blockSize
Definition: PowerSpectrum.h:76
virtual size_t getMinChannelCount() const
Get the minimum supported number of input channels.
PowerSpectrum(float inputSampleRate)
SampleType sampleType
Positioning in time of the output results.
bool hasKnownExtents
True if the results in each output bin fall within a fixed numeric range (minimum and maximum values)...