Import fmit upstream version 0.97.6
[fmit.git] / libs / Music / CombedFT.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 _CombedFT_h_
21 #define _CombedFT_h_
22
23 #include <vector>
24 #include <deque>
25 #include <complex>
26 using namespace std;
27 #include <CppAddons/CAMath.h>
28
29 #include "Music.h"
30 #include "CFFTW3.h"
31 #include "Algorithm.h"
32
33 namespace Music
34 {
35         /*! Combed FT for one voice
36          * Hypothesis: the highest energy peak takes part of the instrument sound
37          * O(2*nln(n))
38          */
39         class CombedFT : public Transform
40         {
41                 protected:
42                         vector<double> m_win;
43                         CFFTW3 m_comb;
44
45                         double m_f0;
46                         double m_audib_ratio;
47                         bool m_use_audibility_treshold;
48                         double m_zp_factor;
49                         double m_window_factor;
50
51                         virtual void init();
52                         virtual void AFreqChanged()                                                     {init();}
53                         virtual void samplingRateChanged()                                      {init();}
54                         virtual void semitoneBoundsChanged()                            {init();}
55
56                 public:
57                         CombedFT();
58
59                         CFFTW3 m_plan;
60
61                         void setZeroPaddingFactor(double zp);
62                         double getZeroPaddingFactor() const                                     {return m_zp_factor;}
63
64                         void setWindowFactor(double wf);
65                         double getWindowFactor() const                                          {return m_window_factor;}
66
67                         /*! set audibility ratio of low harmonics against higher ones
68                          * decrease influences of low harmonics in the evaluation of the fundamental.
69                          * (avoid many too-low evaluation in high frequencies)
70                          * Sometimes in high pitch notes (eg. G3 with bari-sax) a "disturbing" component appears
71                          * at frequency f=G2 and so the fundamental is badly evaluated.
72                          * By "disturbing" I mean: present in the spectral representation, but not audible or weak to much to change the impression of the pitch.
73                          * Actually, that's a physiological setting, there is no "true" choice for a such situation.
74                          * in [0, inf], 0 mean no correction, a good value seems to be 0.1
75                          */
76                         void useAudibilityRatio(bool use)                                       {m_use_audibility_treshold=use;}
77                         void setAudibilityRatio(double audib_ratio=0.1)         {m_audib_ratio=audib_ratio;}
78                         double getAudibilityRatio()                                                     {return m_audib_ratio;}
79
80                         virtual int getSampleAlgoLatency() const;
81                         virtual double getFondamentalFreq() const;
82                         virtual bool hasNoteRecognized() const                          {return m_f0>0.0;}
83                         virtual int getMinSize() const;
84                         virtual void apply(const deque<double>& buff);
85
86                         ~CombedFT();
87         };
88 }
89
90 #endif