include math.h, add onset timestamps, fix default block and step sizes, switch to...
[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::getName() const
42 {
43     return "aubiopitch";
44 }
45
46 string
47 Pitch::getDescription() const
48 {
49     return "Aubio Pitch Detector";
50 }
51
52 string
53 Pitch::getMaker() const
54 {
55     return "Paul Brossier (plugin by Chris Cannam)";
56 }
57
58 int
59 Pitch::getPluginVersion() const
60 {
61     return 1;
62 }
63
64 string
65 Pitch::getCopyright() const
66 {
67     return "GPL";
68 }
69
70 bool
71 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
72 {
73     m_channelCount = channels;
74     m_stepSize = stepSize;
75     m_blockSize = blockSize;
76
77     m_ibuf = new_fvec(stepSize, channels);
78
79     m_pitchdet = new_aubio_pitchdetection(blockSize,
80                                           stepSize,
81                                           channels,
82                                           lrintf(m_inputSampleRate),
83                                           m_pitchtype,
84                                           m_pitchmode);
85
86     return true;
87 }
88
89 void
90 Pitch::reset()
91 {
92 }
93
94 size_t
95 Pitch::getPreferredStepSize() const
96 {
97     return 512;
98 }
99
100 size_t
101 Pitch::getPreferredBlockSize() const
102 {
103     return 2048;
104 }
105
106 Pitch::ParameterList
107 Pitch::getParameterDescriptors() const
108 {
109     ParameterList list;
110     
111     ParameterDescriptor desc;
112     desc.name = "pitchtype";
113     desc.description = "Pitch Detection Function Type";
114     desc.minValue = 0;
115     desc.maxValue = 4;
116     desc.defaultValue = (int)aubio_pitch_yinfft;
117     desc.isQuantized = true;
118     desc.quantizeStep = 1;
119     desc.valueNames.push_back("YIN Frequency Estimator");
120     desc.valueNames.push_back("Spectral Comb");
121     desc.valueNames.push_back("Schmitt");
122     desc.valueNames.push_back("Fast Harmonic Comb");
123     desc.valueNames.push_back("YIN with FFT");
124     list.push_back(desc);
125
126     return list;
127 }
128
129 float
130 Pitch::getParameter(std::string param) const
131 {
132     if (param == "pitchtype") {
133         return m_pitchtype;
134     } else {
135         return 0.0;
136     }
137 }
138
139 void
140 Pitch::setParameter(std::string param, float value)
141 {
142     if (param == "pitchtype") {
143         switch (lrintf(value)) {
144         case 0: m_pitchtype = aubio_pitch_yin; break;
145         case 1: m_pitchtype = aubio_pitch_mcomb; break;
146         case 2: m_pitchtype = aubio_pitch_schmitt; break;
147         case 3: m_pitchtype = aubio_pitch_fcomb; break;
148         case 4: m_pitchtype = aubio_pitch_yinfft; break;
149         }
150     }
151 }
152
153 Pitch::OutputList
154 Pitch::getOutputDescriptors() const
155 {
156     OutputList list;
157
158     OutputDescriptor d;
159     d.name = "frequency";
160     d.unit = "Hz";
161     d.description = "Frequency";
162     d.hasFixedBinCount = true;
163     d.binCount = 1;
164     d.hasKnownExtents = false;
165     d.isQuantized = false;
166     d.sampleType = OutputDescriptor::OneSamplePerStep;
167     list.push_back(d);
168
169     return list;
170 }
171
172 Pitch::FeatureSet
173 Pitch::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
174 {
175     for (size_t i = 0; i < m_stepSize; ++i) {
176         for (size_t j = 0; j < m_channelCount; ++j) {
177             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
178         }
179     }
180
181     float pitch = aubio_pitchdetection(m_pitchdet, m_ibuf);
182
183     Feature feature;
184     feature.hasTimestamp = false;
185     feature.values.push_back(pitch);
186
187     FeatureSet returnFeatures;
188     returnFeatures[0].push_back(feature);
189     return returnFeatures;
190 }
191
192 Pitch::FeatureSet
193 Pitch::getRemainingFeatures()
194 {
195     return FeatureSet();
196 }
197