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