CLAM-Development  1.4.0
SemitoneCenterFinder.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2006 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef SemitoneCenterFinder_hxx
23 #define SemitoneCenterFinder_hxx
24 #include <list>
25 #include <vector>
26 #include <cmath>
27 
28 namespace Simac
29 {
30 
31 
33 {
34 public:
35  typedef std::vector<std::pair<double, double> > PeakList;
36 private:
37  unsigned _histogramSize;
38  unsigned * _histogram;
39  unsigned _binsPerSemitone;
40 public:
42  {
43  _binsPerSemitone = 3;
44  _histogramSize = 30;
45  _histogram = new unsigned[_histogramSize];
46  for (unsigned int i=0; i<_histogramSize; i++)
47  _histogram[i]=0;
48  }
50  {
51  delete [] _histogram;
52  }
53  void doIt(unsigned int nPeaks, const double * peakPositions, const double * peakValues)
54  {
55  for (unsigned int i=0; i<nPeaks; i++)
56  {
57  double semitonePosition = std::fmod(peakPositions[i],_binsPerSemitone);
58  unsigned histogramBin=semitonePosition*_histogramSize/_binsPerSemitone+0.5;
59  _histogram[histogramBin]++;
60  }
61  }
62  double output()
63  {
64  unsigned maxPos=0;
65  unsigned maxOcurrences=0;
66  for (unsigned int i=0; i<_histogramSize; i++)
67  {
68  if (_histogram[i]<=maxOcurrences) continue;
69  maxOcurrences=_histogram[i];
70  maxPos=i;
71  }
72  return maxPos*_binsPerSemitone/float(_histogramSize);
73  }
74 };
75 
76 } // namespace Simac
77 
78 #endif// SemitoneCenterFinder_hxx
79