PowerSpectrum.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     Copyright 2008 QMUL.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 #include "PowerSpectrum.h"
00038 
00039 using std::string;
00040 using std::cerr;
00041 using std::endl;
00042 
00043 #include <math.h>
00044 
00045 PowerSpectrum::PowerSpectrum(float inputSampleRate) :
00046     Plugin(inputSampleRate),
00047     m_blockSize(0)
00048 {
00049 }
00050 
00051 PowerSpectrum::~PowerSpectrum()
00052 {
00053 }
00054 
00055 string
00056 PowerSpectrum::getIdentifier() const
00057 {
00058     return "powerspectrum";
00059 }
00060 
00061 string
00062 PowerSpectrum::getName() const
00063 {
00064     return "Simple Power Spectrum";
00065 }
00066 
00067 string
00068 PowerSpectrum::getDescription() const
00069 {
00070     return "Return the power spectrum of a signal";
00071 }
00072 
00073 string
00074 PowerSpectrum::getMaker() const
00075 {
00076     return "Vamp SDK Example Plugins";
00077 }
00078 
00079 int
00080 PowerSpectrum::getPluginVersion() const
00081 {
00082     return 1;
00083 }
00084 
00085 string
00086 PowerSpectrum::getCopyright() const
00087 {
00088     return "Freely redistributable (BSD license)";
00089 }
00090 
00091 bool
00092 PowerSpectrum::initialise(size_t channels, size_t stepSize, size_t blockSize)
00093 {
00094     if (channels < getMinChannelCount() ||
00095         channels > getMaxChannelCount()) return false;
00096 
00097     m_blockSize = blockSize;
00098 
00099     return true;
00100 }
00101 
00102 void
00103 PowerSpectrum::reset()
00104 {
00105 }
00106 
00107 PowerSpectrum::OutputList
00108 PowerSpectrum::getOutputDescriptors() const
00109 {
00110     OutputList list;
00111 
00112     OutputDescriptor d;
00113     d.identifier = "powerspectrum";
00114     d.name = "Power Spectrum";
00115     d.description = "Power values of the frequency spectrum bins calculated from the input signal";
00116     d.unit = "";
00117     d.hasFixedBinCount = true;
00118     if (m_blockSize == 0) {
00119         // Just so as not to return "1".  This is the bin count that
00120         // would result from a block size of 1024, which is a likely
00121         // default -- but the host should always set the block size
00122         // before querying the bin count for certain.
00123         d.binCount = 513;
00124     } else {
00125         d.binCount = m_blockSize / 2 + 1;
00126     }
00127     d.hasKnownExtents = false;
00128     d.isQuantized = false;
00129     d.sampleType = OutputDescriptor::OneSamplePerStep;
00130     list.push_back(d);
00131 
00132     return list;
00133 }
00134 
00135 PowerSpectrum::FeatureSet
00136 PowerSpectrum::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
00137 {
00138     FeatureSet fs;
00139 
00140     if (m_blockSize == 0) {
00141         cerr << "ERROR: PowerSpectrum::process: Not initialised" << endl;
00142         return fs;
00143     }
00144 
00145     size_t n = m_blockSize / 2 + 1;
00146     const float *fbuf = inputBuffers[0];
00147 
00148     Feature feature;
00149     feature.hasTimestamp = false;
00150     feature.values.reserve(n); // optional
00151 
00152     for (size_t i = 0; i < n; ++i) {
00153 
00154         double real = fbuf[i * 2];
00155         double imag = fbuf[i * 2 + 1];
00156 
00157         feature.values.push_back(real * real + imag * imag);
00158     }
00159 
00160     fs[0].push_back(feature);
00161 
00162     return fs;
00163 }
00164 
00165 PowerSpectrum::FeatureSet
00166 PowerSpectrum::getRemainingFeatures()
00167 {
00168     return FeatureSet();
00169 }
00170 

Generated on Thu Sep 24 13:40:13 2009 for VampPluginSDK by  doxygen 1.5.8