* Update along with latest Vamp API change
[vamp-aubio-plugins.git] / plugins / Pitch.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Vamp feature extraction plugins using Paul Brossier's Aubio library.
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam.
8     
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14
15 */
16
17 #include <math.h>
18 #include "Pitch.h"
19
20 using std::string;
21 using std::vector;
22 using std::cerr;
23 using std::endl;
24
25 Pitch::Pitch(float inputSampleRate) :
26     Plugin(inputSampleRate),
27     m_ibuf(0),
28     m_pitchdet(0),
29     m_pitchtype(aubio_pitch_yinfft),
30     m_pitchmode(aubio_pitchm_freq)
31 {
32 }
33
34 Pitch::~Pitch()
35 {
36     if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
37     if (m_ibuf) del_fvec(m_ibuf);
38 }
39
40 string
41 Pitch::getIdentifier() const
42 {
43     return "aubiopitch";
44 }
45
46 string
47 Pitch::getName() const
48 {
49     return "Aubio Pitch Detector";
50 }
51
52 string
53 Pitch::getDescription() const
54 {
55     return "Track estimated note pitches";
56 }
57
58 string
59 Pitch::getMaker() const
60 {
61     return "Paul Brossier (plugin by Chris Cannam)";
62 }
63
64 int
65 Pitch::getPluginVersion() const
66 {
67     return 1;
68 }
69
70 string
71 Pitch::getCopyright() const
72 {
73     return "GPL";
74 }
75
76 bool
77 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
78 {
79     m_channelCount = channels;
80     m_stepSize = stepSize;
81     m_blockSize = blockSize;
82
83     m_ibuf = new_fvec(stepSize, channels);
84
85     m_pitchdet = new_aubio_pitchdetection(blockSize,
86                                           stepSize,
87                                           channels,
88                                           lrintf(m_inputSampleRate),
89                                           m_pitchtype,
90                                           m_pitchmode);
91
92     return true;
93 }
94
95 void
96 Pitch::reset()
97 {
98 }
99
100 size_t
101 Pitch::getPreferredStepSize() const
102 {
103     return 512;
104 }
105
106 size_t
107 Pitch::getPreferredBlockSize() const
108 {
109     return 2048;
110 }
111
112 Pitch::ParameterList
113 Pitch::getParameterDescriptors() const
114 {
115     ParameterList list;
116     
117     ParameterDescriptor desc;
118     desc.identifier = "pitchtype";
119     desc.name = "Pitch Detection Function Type";
120     desc.minValue = 0;
121     desc.maxValue = 4;
122     desc.defaultValue = (int)aubio_pitch_yinfft;
123     desc.isQuantized = true;
124     desc.quantizeStep = 1;
125     desc.valueNames.push_back("YIN Frequency Estimator");
126     desc.valueNames.push_back("Spectral Comb");
127     desc.valueNames.push_back("Schmitt");
128     desc.valueNames.push_back("Fast Harmonic Comb");
129     desc.valueNames.push_back("YIN with FFT");
130     list.push_back(desc);
131
132     return list;
133 }
134
135 float
136 Pitch::getParameter(std::string param) const
137 {
138     if (param == "pitchtype") {
139         return m_pitchtype;
140     } else {
141         return 0.0;
142     }
143 }
144
145 void
146 Pitch::setParameter(std::string param, float value)
147 {
148     if (param == "pitchtype") {
149         switch (lrintf(value)) {
150         case 0: m_pitchtype = aubio_pitch_yin; break;
151         case 1: m_pitchtype = aubio_pitch_mcomb; break;
152         case 2: m_pitchtype = aubio_pitch_schmitt; break;
153         case 3: m_pitchtype = aubio_pitch_fcomb; break;
154         case 4: m_pitchtype = aubio_pitch_yinfft; break;
155         }
156     }
157 }
158
159 Pitch::OutputList
160 Pitch::getOutputDescriptors() const
161 {
162     OutputList list;
163
164     OutputDescriptor d;
165     d.identifier = "frequency";
166     d.name = "Frequency";
167     d.unit = "Hz";
168     d.hasFixedBinCount = true;
169     d.binCount = 1;
170     d.hasKnownExtents = false;
171     d.isQuantized = false;
172     d.sampleType = OutputDescriptor::OneSamplePerStep;
173     list.push_back(d);
174
175     return list;
176 }
177
178 Pitch::FeatureSet
179 Pitch::process(const float *const *inputBuffers,
180                Vamp::RealTime /* timestamp */)
181 {
182     for (size_t i = 0; i < m_stepSize; ++i) {
183         for (size_t j = 0; j < m_channelCount; ++j) {
184             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
185         }
186     }
187
188     float pitch = aubio_pitchdetection(m_pitchdet, m_ibuf);
189
190     Feature feature;
191     feature.hasTimestamp = false;
192     feature.values.push_back(pitch);
193
194     FeatureSet returnFeatures;
195     returnFeatures[0].push_back(feature);
196     return returnFeatures;
197 }
198
199 Pitch::FeatureSet
200 Pitch::getRemainingFeatures()
201 {
202     return FeatureSet();
203 }
204