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@20: This file copyright 2006-2008 Chris Cannam and QMUL.
cannam@17:
piem@112: This file is part of vamp-aubio-plugins.
piem@112:
piem@112: vamp-aubio is free software: you can redistribute it and/or modify
piem@112: it under the terms of the GNU General Public License as published by
piem@112: the Free Software Foundation, either version 3 of the License, or
piem@112: (at your option) any later version.
piem@112:
piem@112: vamp-aubio is distributed in the hope that it will be useful,
piem@112: but WITHOUT ANY WARRANTY; without even the implied warranty of
piem@112: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
piem@112: GNU General Public License for more details.
piem@112:
piem@112: You should have received a copy of the GNU General Public License
piem@112: along with aubio. If not, see .
piem@112:
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@31: Silence::Silence(float inputSampleRate) :
cannam@17: Plugin(inputSampleRate),
cannam@17: m_ibuf(0),
cannam@17: m_pbuf(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: }
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@31: return 4;
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@32: if (channels != 1) {
cannam@32: std::cerr << "Silence::initialise: channels must be 1" << std::endl;
cannam@32: return false;
cannam@32: }
cannam@32:
cannam@17: m_stepSize = stepSize;
cannam@17: m_blockSize = blockSize;
cannam@17:
cannam@32: m_ibuf = new_fvec(stepSize);
cannam@32: m_pbuf = new_fvec(stepSize);
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";
piem@66: desc.description = "Threshold for silence detection";
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:
cannam@31: d.identifier = "silent";
cannam@31: d.name = "Silent Regions";
cannam@31: d.description = "Return an interval covering each silent region";
cannam@31: d.hasFixedBinCount = true;
cannam@31: d.binCount = 0;
cannam@31: d.hasKnownExtents = false;
cannam@31: d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@31: d.sampleRate = 0;
cannam@31: d.hasDuration = true;
cannam@31: list.push_back(d);
cannam@20:
cannam@31: d.identifier = "noisy";
cannam@31: d.name = "Non-Silent Regions";
cannam@31: d.description = "Return an interval covering each non-silent region";
cannam@31: d.hasFixedBinCount = true;
cannam@31: d.binCount = 0;
cannam@31: d.hasKnownExtents = false;
cannam@31: d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@31: d.sampleRate = 0;
cannam@31: d.hasDuration = true;
cannam@31: 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) {
piem@52: fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
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:
cannam@17: for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
cannam@35: vec.data = m_ibuf->data + i;
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@35: vec.data = m_pbuf->data + m_stepSize - i - incr;
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@20:
cannam@17: feature.values.clear();
cannam@17:
cannam@31: if (!m_first) {
cannam@31: feature.timestamp = m_lastChange;
cannam@31: feature.hasDuration = true;
cannam@31: feature.duration = featureStamp - m_lastChange;
cannam@20: if (silent) {
cannam@31: // non-silent regions feature
cannam@31: // (becoming silent, so this is a non-silent region)
cannam@31: returnFeatures[1].push_back(feature);
cannam@31: } else {
cannam@31: // silent regions feature
cannam@31: // (becoming non-silent, so this is a silent region)
cannam@20: returnFeatures[0].push_back(feature);
cannam@31: }
cannam@20: }
cannam@31: m_lastChange = featureStamp;
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@35: smpl_t *tmpdata = m_ibuf->data;
cannam@17: m_ibuf->data = m_pbuf->data;
cannam@17: m_pbuf->data = tmpdata;
cannam@17:
cannam@20: m_lastTimestamp = timestamp;
cannam@20:
cannam@17: return returnFeatures;
cannam@17: }
cannam@17:
cannam@17: Silence::FeatureSet
cannam@17: Silence::getRemainingFeatures()
cannam@17: {
cannam@20: FeatureSet returnFeatures;
cannam@20:
cannam@24: // std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
cannam@24:
cannam@24: if (m_lastTimestamp > m_lastChange) {
cannam@24:
cannam@24: Feature feature;
cannam@24: feature.hasTimestamp = true;
cannam@24:
cannam@31: feature.timestamp = m_lastChange;
cannam@31: feature.hasDuration = true;
cannam@31: feature.duration = m_lastTimestamp - m_lastChange;
cannam@31: if (m_prevSilent) {
cannam@31: // silent regions feature
cannam@31: returnFeatures[0].push_back(feature);
cannam@24: } else {
cannam@31: // non-silent regions feature
cannam@31: returnFeatures[1].push_back(feature);
cannam@20: }
cannam@24:
cannam@24: if (!m_prevSilent) {
cannam@24: Feature silenceTestFeature;
cannam@24: silenceTestFeature.hasTimestamp = true;
cannam@24: silenceTestFeature.timestamp = m_lastTimestamp;
cannam@24: silenceTestFeature.values.push_back(0);
cannam@24: returnFeatures[2].push_back(silenceTestFeature);
cannam@24: }
cannam@20: }
cannam@20:
cannam@24: return returnFeatures;
cannam@17: }
cannam@17: