Import upstream version 0.99.2
[fmit.git] / libs / Music / BubbleAlgo.h
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 // 
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 // 
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #ifndef _BubbleAlgo_h_
21 #define _BubbleAlgo_h_
22
23 #include <vector>
24 #include <deque>
25 #include <map>
26 #include <list>
27 #include <complex>
28 using namespace std;
29 #include "Algorithm.h"
30 #include "Correlation.h"
31         
32 namespace Music
33 {
34         class BubbleAlgo : public Algorithm
35         {
36                 typedef double Type;
37
38                 size_t m_wave_length;
39                 size_t m_min_length;
40                 size_t m_max_length;
41                 Type m_error_threshold;
42                 Type m_conv_threshold;
43
44                 struct Bubble
45                 {
46                         size_t s;
47                         size_t count;
48                         size_t length;
49                         Type score;
50                         Type err;
51                         complex<double> conv;
52                         size_t n;
53                         size_t f;
54                         bool todrop;
55                         size_t gcd_count;
56                         Bubble(size_t as=0) : s(as), count(0), score(0.0), err(0.0), conv(complex<double>(0.0,0.0)), n(0), todrop(false), gcd_count(0) {}
57                 };
58
59                 vector<Bubble> m_bubbles;
60                 vector< vector<complex<double> > > m_waves;
61                 vector< vector<double> > m_waves_best;
62                 map<Type,list<size_t> > m_sort;
63                 map<Type,list<size_t> > m_best_set;
64
65                 Type diff(const deque<double>& buff, size_t i, size_t j)        {return abs(buff[i] - buff[j]);}
66
67                 void setMinMaxLength(size_t min_length, size_t max_length)
68                                                                                 {m_min_length=min_length; m_max_length=max_length;}
69
70           protected:
71                 void init();
72                 virtual void AFreqChanged()                                                     {init();}
73                 virtual void samplingRateChanged()                                      {init();}
74                 virtual void semitoneBoundsChanged()                            {init();}
75
76           public:
77                 BubbleAlgo();
78
79                 virtual int getSampleAlgoLatency() const                {return 2*m_waves.back().size();}
80
81                 void apply(const deque<double>& buff);
82
83                 virtual bool hasNoteRecognized() const                  {return m_wave_length!=0;}
84                 virtual int getFondamentalWaveLength() const    {return m_wave_length;}
85                 void setErrorThreshold(double threshold)                {m_error_threshold = threshold;}
86                 void setConvolutionThreshold(double threshold)  {m_conv_threshold = threshold;}
87
88                 virtual ~BubbleAlgo() {}
89         };
90 }
91
92 #endif
93