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