cannam@241: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@241: cannam@241: /* cannam@241: Vamp cannam@241: cannam@241: An API for audio analysis and feature extraction plugins. cannam@241: cannam@241: Centre for Digital Music, Queen Mary, University of London. cannam@241: Copyright 2008 QMUL. cannam@241: cannam@241: Permission is hereby granted, free of charge, to any person cannam@241: obtaining a copy of this software and associated documentation cannam@241: files (the "Software"), to deal in the Software without cannam@241: restriction, including without limitation the rights to use, copy, cannam@241: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@241: of the Software, and to permit persons to whom the Software is cannam@241: furnished to do so, subject to the following conditions: cannam@241: cannam@241: The above copyright notice and this permission notice shall be cannam@241: included in all copies or substantial portions of the Software. cannam@241: cannam@241: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@241: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@241: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@241: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@241: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@241: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@241: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@241: cannam@241: Except as contained in this notice, the names of the Centre for cannam@241: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@241: shall not be used in advertising or otherwise to promote the sale, cannam@241: use or other dealings in this Software without prior written cannam@241: authorization. cannam@241: */ cannam@241: cannam@241: #include "PowerSpectrum.h" cannam@241: cannam@241: using std::string; cannam@241: using std::cerr; cannam@241: using std::endl; cannam@241: cannam@241: #include cannam@241: cannam@241: PowerSpectrum::PowerSpectrum(float inputSampleRate) : cannam@241: Plugin(inputSampleRate), cannam@241: m_blockSize(0) cannam@241: { cannam@241: } cannam@241: cannam@241: PowerSpectrum::~PowerSpectrum() cannam@241: { cannam@241: } cannam@241: cannam@241: string cannam@241: PowerSpectrum::getIdentifier() const cannam@241: { cannam@241: return "powerspectrum"; cannam@241: } cannam@241: cannam@241: string cannam@241: PowerSpectrum::getName() const cannam@241: { cannam@241: return "Simple Power Spectrum"; cannam@241: } cannam@241: cannam@241: string cannam@241: PowerSpectrum::getDescription() const cannam@241: { cannam@241: return "Return the power spectrum of a signal"; cannam@241: } cannam@241: cannam@241: string cannam@241: PowerSpectrum::getMaker() const cannam@241: { cannam@241: return "Vamp SDK Example Plugins"; cannam@241: } cannam@241: cannam@241: int cannam@241: PowerSpectrum::getPluginVersion() const cannam@241: { cannam@241: return 1; cannam@241: } cannam@241: cannam@241: string cannam@241: PowerSpectrum::getCopyright() const cannam@241: { cannam@241: return "Freely redistributable (BSD license)"; cannam@241: } cannam@241: cannam@241: bool cannam@241: PowerSpectrum::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@241: { cannam@241: if (channels < getMinChannelCount() || cannam@241: channels > getMaxChannelCount()) return false; cannam@241: cannam@241: m_blockSize = blockSize; cannam@241: cannam@241: return true; cannam@241: } cannam@241: cannam@241: void cannam@241: PowerSpectrum::reset() cannam@241: { cannam@241: } cannam@241: cannam@241: PowerSpectrum::OutputList cannam@241: PowerSpectrum::getOutputDescriptors() const cannam@241: { cannam@241: OutputList list; cannam@241: cannam@241: OutputDescriptor d; cannam@241: d.identifier = "powerspectrum"; cannam@241: d.name = "Power Spectrum"; cannam@241: d.description = "Power values of the frequency spectrum bins calculated from the input signal"; cannam@241: d.unit = ""; cannam@241: d.hasFixedBinCount = true; cannam@255: if (m_blockSize == 0) { cannam@255: // Just so as not to return "1". This is the bin count that cannam@255: // would result from a block size of 1024, which is a likely cannam@255: // default -- but the host should always set the block size cannam@255: // before querying the bin count for certain. cannam@255: d.binCount = 513; cannam@255: } else { cannam@255: d.binCount = m_blockSize / 2 + 1; cannam@255: } cannam@241: d.hasKnownExtents = false; cannam@241: d.isQuantized = false; cannam@241: d.sampleType = OutputDescriptor::OneSamplePerStep; cannam@241: list.push_back(d); cannam@241: cannam@241: return list; cannam@241: } cannam@241: cannam@241: PowerSpectrum::FeatureSet cannam@241: PowerSpectrum::process(const float *const *inputBuffers, Vamp::RealTime timestamp) cannam@241: { cannam@241: FeatureSet fs; cannam@241: cannam@241: if (m_blockSize == 0) { cannam@241: cerr << "ERROR: PowerSpectrum::process: Not initialised" << endl; cannam@241: return fs; cannam@241: } cannam@241: cannam@241: size_t n = m_blockSize / 2 + 1; cannam@241: const float *fbuf = inputBuffers[0]; cannam@241: cannam@241: Feature feature; cannam@241: feature.hasTimestamp = false; cannam@241: feature.values.reserve(n); // optional cannam@241: cannam@241: for (size_t i = 0; i < n; ++i) { cannam@241: cannam@241: double real = fbuf[i * 2]; cannam@241: double imag = fbuf[i * 2 + 1]; cannam@241: cannam@241: feature.values.push_back(real * real + imag * imag); cannam@241: } cannam@241: cannam@241: fs[0].push_back(feature); cannam@241: cannam@241: return fs; cannam@241: } cannam@241: cannam@241: PowerSpectrum::FeatureSet cannam@241: PowerSpectrum::getRemainingFeatures() cannam@241: { cannam@241: return FeatureSet(); cannam@241: } cannam@241: