libmain.cpp, plugins/: update to GPLv3
[vamp-aubio-plugins.git] / plugins / Pitch.cpp
index 054c69c..4201508 100644 (file)
@@ -6,11 +6,20 @@
     Centre for Digital Music, Queen Mary, University of London.
     This file copyright 2006 Chris Cannam.
     
-    This program is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License as
-    published by the Free Software Foundation; either version 2 of the
-    License, or (at your option) any later version.  See the file
-    COPYING included with this distribution for more information.
+    This file is part of vamp-aubio-plugins.
+
+    vamp-aubio is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    vamp-aubio is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with aubio.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 
@@ -22,24 +31,18 @@ using std::vector;
 using std::cerr;
 using std::endl;
 
-static float
-getFrequencyForMIDIPitch(int midiPitch)
-{
-    return 440.f * powf(2.0, (float(midiPitch) - 69.0) / 12.0);
-}
-
 Pitch::Pitch(float inputSampleRate) :
     Plugin(inputSampleRate),
     m_ibuf(0),
+    m_obuf(0),
     m_pitchdet(0),
     m_pitchtype(PitchYinFFT),
-    m_minfreq(getFrequencyForMIDIPitch(32)),
-    m_maxfreq(getFrequencyForMIDIPitch(95)),
+    m_minfreq(aubio_miditofreq(32)),
+    m_maxfreq(aubio_miditofreq(95)),
     m_silence(-90),
     m_wrapRange(false),
     m_stepSize(0),
-    m_blockSize(0),
-    m_channelCount(0)
+    m_blockSize(0)
 {
 }
 
@@ -47,6 +50,7 @@ Pitch::~Pitch()
 {
     if (m_pitchdet) del_aubio_pitch(m_pitchdet);
     if (m_ibuf) del_fvec(m_ibuf);
+    if (m_obuf) del_fvec(m_obuf);
 }
 
 string
@@ -88,18 +92,18 @@ Pitch::getCopyright() const
 bool
 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
 {
-    m_channelCount = channels;
+    if (channels != 1) {
+        std::cerr << "Pitch::initialise: channels must be 1" << std::endl;
+        return false;
+    }
+
     m_stepSize = stepSize;
     m_blockSize = blockSize;
 
-    m_ibuf = new_fvec(stepSize, channels);
+    m_ibuf = new_fvec(stepSize);
+    m_obuf = new_fvec(1);
 
-    m_pitchdet = new_aubio_pitchdetection(blockSize,
-                                          stepSize,
-                                          channels,
-                                          lrintf(m_inputSampleRate),
-                                          m_pitchtype,
-                                          m_pitchmode);
+    reset();
 
     return true;
 }
@@ -107,6 +111,15 @@ Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
 void
 Pitch::reset()
 {
+    if (m_pitchdet) del_aubio_pitch(m_pitchdet);
+
+    m_pitchdet = new_aubio_pitch
+        (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
+         m_blockSize,
+         m_stepSize,
+         lrintf(m_inputSampleRate));
+
+    aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
 }
 
 size_t
@@ -129,9 +142,10 @@ Pitch::getParameterDescriptors() const
     ParameterDescriptor desc;
     desc.identifier = "pitchtype";
     desc.name = "Pitch Detection Function Type";
+    desc.description = "Type of pitch detection function to use";
     desc.minValue = 0;
     desc.maxValue = 4;
-    desc.defaultValue = (int)aubio_pitch_yinfft;
+    desc.defaultValue = (int)PitchYinFFT;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.push_back("YIN Frequency Estimator");
@@ -144,9 +158,10 @@ Pitch::getParameterDescriptors() const
     desc = ParameterDescriptor();
     desc.identifier = "minfreq";
     desc.name = "Minimum Fundamental Frequency";
+    desc.description = "Lowest frequency to look for";
     desc.minValue = 1;
     desc.maxValue = m_inputSampleRate/2;
-    desc.defaultValue = getFrequencyForMIDIPitch(32);
+    desc.defaultValue = aubio_miditofreq(32);
     desc.unit = "Hz";
     desc.isQuantized = false;
     list.push_back(desc);
@@ -154,9 +169,10 @@ Pitch::getParameterDescriptors() const
     desc = ParameterDescriptor();
     desc.identifier = "maxfreq";
     desc.name = "Maximum Fundamental Frequency";
+    desc.description = "Highest frequency to look for";
     desc.minValue = 1;
     desc.maxValue = m_inputSampleRate/2;
-    desc.defaultValue = getFrequencyForMIDIPitch(95);
+    desc.defaultValue = aubio_miditofreq(95);
     desc.unit = "Hz";
     desc.isQuantized = false;
     list.push_back(desc);
@@ -164,6 +180,7 @@ Pitch::getParameterDescriptors() const
     desc = ParameterDescriptor();
     desc.identifier = "wraprange";
     desc.name = "Fold Higher or Lower Frequencies into Range";
+    desc.description = "Frequencies detected outside the range will be transposed to higher or lower octaves";
     desc.minValue = 0;
     desc.maxValue = 1;
     desc.defaultValue = 0;
@@ -174,6 +191,7 @@ Pitch::getParameterDescriptors() const
     desc = ParameterDescriptor();
     desc.identifier = "silencethreshold";
     desc.name = "Silence Threshold";
+    desc.description = "Silence threshold, the higher the least detection";
     desc.minValue = -120;
     desc.maxValue = 0;
     desc.defaultValue = -90;
@@ -207,11 +225,11 @@ Pitch::setParameter(std::string param, float value)
 {
     if (param == "pitchtype") {
         switch (lrintf(value)) {
-        case 0: m_pitchtype = aubio_pitch_yin; break;
-        case 1: m_pitchtype = aubio_pitch_mcomb; break;
-        case 2: m_pitchtype = aubio_pitch_schmitt; break;
-        case 3: m_pitchtype = aubio_pitch_fcomb; break;
-        case 4: m_pitchtype = aubio_pitch_yinfft; break;
+        case 0: m_pitchtype = PitchYin; break;
+        case 1: m_pitchtype = PitchMComb; break;
+        case 2: m_pitchtype = PitchSchmitt; break;
+        case 3: m_pitchtype = PitchFComb; break;
+        case 4: m_pitchtype = PitchYinFFT; break;
         }
     } else if (param == "minfreq") {
         m_minfreq = value;
@@ -232,6 +250,7 @@ Pitch::getOutputDescriptors() const
     OutputDescriptor d;
     d.identifier = "frequency";
     d.name = "Fundamental Frequency";
+    d.description = "List of detected frequencies";
     d.unit = "Hz";
     d.hasFixedBinCount = true;
     d.binCount = 1;
@@ -259,12 +278,12 @@ Pitch::process(const float *const *inputBuffers,
     }
 
     for (size_t i = 0; i < m_stepSize; ++i) {
-        for (size_t j = 0; j < m_channelCount; ++j) {
-            fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
-        }
+        fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
     }
 
-    float freq = aubio_pitchdetection(m_pitchdet, m_ibuf);
+    aubio_pitch_do(m_pitchdet, m_ibuf, m_obuf);
+    
+    float freq = m_obuf->data[0];
 
     bool silent = aubio_silence_detection(m_ibuf, m_silence);
     if (silent) {