cannam@17: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@17: cannam@17: /* cannam@17: Vamp feature extraction plugins using Paul Brossier's Aubio library. cannam@17: cannam@17: Centre for Digital Music, Queen Mary, University of London. cannam@17: This file copyright 2006 Chris Cannam. cannam@17: cannam@17: This program is free software; you can redistribute it and/or cannam@17: modify it under the terms of the GNU General Public License as cannam@17: published by the Free Software Foundation; either version 2 of the cannam@17: License, or (at your option) any later version. See the file cannam@17: COPYING included with this distribution for more information. cannam@17: */ cannam@17: cannam@17: #include cannam@17: #include "Silence.h" cannam@17: cannam@17: using std::string; cannam@17: using std::vector; cannam@17: using std::cerr; cannam@17: using std::endl; cannam@17: cannam@17: Silence::Silence(float inputSampleRate) : cannam@17: Plugin(inputSampleRate), cannam@17: m_ibuf(0), cannam@17: m_pbuf(0), cannam@17: m_tmpptrs(0), cannam@18: m_threshold(-80), cannam@17: m_prevSilent(false), cannam@17: m_first(true) cannam@17: { cannam@17: } cannam@17: cannam@17: Silence::~Silence() cannam@17: { cannam@17: if (m_ibuf) del_fvec(m_ibuf); cannam@17: if (m_pbuf) del_fvec(m_pbuf); cannam@17: if (m_tmpptrs) delete[] m_tmpptrs; cannam@17: } cannam@17: cannam@17: string cannam@17: Silence::getIdentifier() const cannam@17: { cannam@17: return "aubiosilence"; cannam@17: } cannam@17: cannam@17: string cannam@17: Silence::getName() const cannam@17: { cannam@17: return "Aubio Silence Detector"; cannam@17: } cannam@17: cannam@17: string cannam@17: Silence::getDescription() const cannam@17: { cannam@17: return "Detect levels below a certain threshold"; cannam@17: } cannam@17: cannam@17: string cannam@17: Silence::getMaker() const cannam@17: { cannam@17: return "Paul Brossier (plugin by Chris Cannam)"; cannam@17: } cannam@17: cannam@17: int cannam@17: Silence::getPluginVersion() const cannam@17: { cannam@17: return 1; cannam@17: } cannam@17: cannam@17: string cannam@17: Silence::getCopyright() const cannam@17: { cannam@17: return "GPL"; cannam@17: } cannam@17: cannam@17: bool cannam@17: Silence::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@17: { cannam@17: m_channelCount = channels; cannam@17: m_stepSize = stepSize; cannam@17: m_blockSize = blockSize; cannam@17: cannam@17: m_ibuf = new_fvec(stepSize, channels); cannam@17: m_pbuf = new_fvec(stepSize, channels); cannam@17: m_tmpptrs = new smpl_t *[channels]; cannam@17: cannam@17: return true; cannam@17: } cannam@17: cannam@17: void cannam@17: Silence::reset() cannam@17: { cannam@17: m_first = true; cannam@17: } cannam@17: cannam@17: size_t cannam@17: Silence::getPreferredStepSize() const cannam@17: { cannam@17: return 1024; cannam@17: } cannam@17: cannam@17: size_t cannam@17: Silence::getPreferredBlockSize() const cannam@17: { cannam@17: return 1024; cannam@17: } cannam@17: cannam@17: Silence::ParameterList cannam@17: Silence::getParameterDescriptors() const cannam@17: { cannam@17: ParameterList list; cannam@17: ParameterDescriptor desc; cannam@17: cannam@17: desc = ParameterDescriptor(); cannam@17: desc.identifier = "silencethreshold"; cannam@17: desc.name = "Silence Threshold"; cannam@17: desc.minValue = -120; cannam@17: desc.maxValue = 0; cannam@18: desc.defaultValue = -80; cannam@17: desc.unit = "dB"; cannam@17: desc.isQuantized = false; cannam@17: list.push_back(desc); cannam@17: cannam@17: return list; cannam@17: } cannam@17: cannam@17: float cannam@17: Silence::getParameter(std::string param) const cannam@17: { cannam@17: if (param == "silencethreshold") { cannam@17: return m_threshold; cannam@17: } else { cannam@17: return 0.0; cannam@17: } cannam@17: } cannam@17: cannam@17: void cannam@17: Silence::setParameter(std::string param, float value) cannam@17: { cannam@17: if (param == "silencethreshold") { cannam@17: m_threshold = value; cannam@17: } cannam@17: } cannam@17: cannam@17: Silence::OutputList cannam@17: Silence::getOutputDescriptors() const cannam@17: { cannam@17: OutputList list; cannam@17: cannam@17: OutputDescriptor d; cannam@17: d.identifier = "silencestart"; cannam@18: d.name = "Beginnings of Silent Regions"; cannam@17: d.description = "Return a single instant at the point where each silent region begins"; cannam@17: d.hasFixedBinCount = true; cannam@17: d.binCount = 0; cannam@17: d.sampleType = OutputDescriptor::VariableSampleRate; cannam@18: d.sampleRate = 0; cannam@17: list.push_back(d); cannam@17: cannam@17: d.identifier = "silenceend"; cannam@17: d.name = "Ends of Silent Regions"; cannam@17: d.description = "Return a single instant at the point where each silent region ends"; cannam@17: d.hasFixedBinCount = true; cannam@17: d.binCount = 0; cannam@17: d.sampleType = OutputDescriptor::VariableSampleRate; cannam@18: d.sampleRate = 0; cannam@17: list.push_back(d); cannam@17: cannam@17: d.identifier = "silencelevel"; cannam@17: d.name = "Silence Test"; cannam@17: d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends"; cannam@17: d.hasFixedBinCount = true; cannam@17: d.binCount = 1; cannam@17: d.hasKnownExtents = true; cannam@17: d.minValue = 0; cannam@17: d.maxValue = 1; cannam@17: d.isQuantized = true; cannam@17: d.quantizeStep = 1; cannam@17: d.sampleType = OutputDescriptor::VariableSampleRate; cannam@18: d.sampleRate = 0; cannam@17: list.push_back(d); cannam@17: cannam@17: return list; cannam@17: } cannam@17: cannam@17: Silence::FeatureSet cannam@17: Silence::process(const float *const *inputBuffers, cannam@17: Vamp::RealTime timestamp) cannam@17: { cannam@17: for (size_t i = 0; i < m_stepSize; ++i) { cannam@17: for (size_t j = 0; j < m_channelCount; ++j) { cannam@17: fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i); cannam@17: } cannam@17: } cannam@17: cannam@17: bool silent = aubio_silence_detection(m_ibuf, m_threshold); cannam@17: FeatureSet returnFeatures; cannam@17: cannam@17: if (m_first || m_prevSilent != silent) { cannam@17: cannam@17: Vamp::RealTime featureStamp = timestamp; cannam@17: cannam@17: if ((silent && !m_first) || !silent) { cannam@17: cannam@17: // refine our result cannam@17: cannam@17: long off = 0; cannam@17: size_t incr = 16; cannam@17: if (incr > m_stepSize/8) incr = m_stepSize/8; cannam@17: cannam@17: fvec_t vec; cannam@17: vec.length = incr * 4; cannam@17: vec.channels = m_channelCount; cannam@17: vec.data = m_tmpptrs; cannam@17: cannam@17: for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) { cannam@17: for (size_t j = 0; j < m_channelCount; ++j) { cannam@17: m_tmpptrs[j] = m_ibuf->data[j] + i; cannam@17: } cannam@17: bool subsilent = aubio_silence_detection(&vec, m_threshold); cannam@17: if (silent == subsilent) { cannam@17: off = i; cannam@17: break; cannam@17: } cannam@17: } cannam@17: cannam@17: if (silent && (off == 0)) { cannam@17: for (size_t i = 0; i < m_stepSize - incr; i += incr) { cannam@17: for (size_t j = 0; j < m_channelCount; ++j) { cannam@17: m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr; cannam@17: } cannam@17: bool subsilent = aubio_silence_detection(&vec, m_threshold); cannam@17: if (!subsilent) { cannam@17: off = -(long)i; cannam@17: break; cannam@17: } cannam@17: } cannam@17: } else { cannam@17: } cannam@17: cannam@17: featureStamp = timestamp + Vamp::RealTime::frame2RealTime cannam@17: (off, lrintf(m_inputSampleRate)); cannam@17: } cannam@17: cannam@17: Feature feature; cannam@17: feature.hasTimestamp = true; cannam@17: feature.timestamp = featureStamp; cannam@17: feature.values.push_back(silent ? 0 : 1); cannam@17: returnFeatures[2].push_back(feature); cannam@17: feature.values.clear(); cannam@17: cannam@17: if (silent) { cannam@17: returnFeatures[0].push_back(feature); cannam@17: } else { cannam@17: returnFeatures[1].push_back(feature); cannam@17: } cannam@17: cannam@17: m_prevSilent = silent; cannam@17: m_first = false; cannam@17: } cannam@17: cannam@17: // swap ibuf and pbuf data pointers, so that this block's data is cannam@17: // available in pbuf when processing the next block, without cannam@17: // having to allocate new storage for it cannam@17: smpl_t **tmpdata = m_ibuf->data; cannam@17: m_ibuf->data = m_pbuf->data; cannam@17: m_pbuf->data = tmpdata; cannam@17: cannam@17: return returnFeatures; cannam@17: } cannam@17: cannam@17: Silence::FeatureSet cannam@17: Silence::getRemainingFeatures() cannam@17: { cannam@17: return FeatureSet(); cannam@17: } cannam@17: