Debian Codegraph @

libs/Music/CombedFT.cpp

raw
1 // Copyright 2007 "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 #include "CombedFT.h"
20
21 #include <assert.h>
22 #include <iostream>
23 using namespace std;
24 using namespace Math;
25 #include "Music.h"
26 #include "SPWindow.h"
27 #include "FreqAnalysis.h"
28
29 #include <QTextStream>
30
31 namespace Music
32 {
33 CombedFT::CombedFT()
34 {
35 m_use_audibility_treshold = false;
36 m_audib_ratio = 0.1;
37
38 m_zp_factor = 1.0;
39 m_window_factor = 1.0;
40
41 init();
42 }
43 void CombedFT::setZeroPaddingFactor(double zp)
44 {
45 if(zp!=m_zp_factor)
46 {
47 m_zp_factor = zp;
48 init();
49 }
50 }
51 void CombedFT::setWindowFactor(double wf)
52 {
53 if(wf!=m_window_factor)
54 {
55 m_window_factor = wf;
56 init();
57 }
58 }
59
60 void CombedFT::init()
61 {
62 if(GetSamplingRate()<=0) return;
63
64 m_f0 = 0.0;
65
66 int win_size = int(m_window_factor*GetSamplingRate()/h2f(GetSemitoneMin()));// at least m_window_factor period of the lowest freq
67
68 int best_size = 2;
69 while(best_size<int(win_size))
70 best_size *= 2;
71
72 m_win = hann(best_size);
73
74 while(best_size<int(m_zp_factor*win_size))
75 best_size *= 2;
76
77 cout << "CombedFT: INFO: window size=" << win_size << " FFT size=" << best_size << " window size factor=" << m_window_factor << " zero padding factor=" << m_zp_factor << endl;
78
79 m_plan.resize(best_size);
80 m_components.resize(m_plan.size()/2);
81 m_comb.resize(m_plan.size()/2);
82 }
83 int CombedFT::getSampleAlgoLatency() const
84 {
85 return 1000*int(m_win.size())/GetSamplingRate();
86 }
87 int CombedFT::getMinSize() const
88 {
89 return int(m_win.size());
90 }
91 double CombedFT::getFondamentalFreq() const
92 {
93 return m_f0;
94 }
95
96 void CombedFT::apply(const deque<double>& buff)
97 {
98 // cout << getAmplitudeTreshold() << " " << getComponentTreshold() << " " << m_audib_ratio << endl;
99
100 if(int(buff.size())<getMinSize() || m_win.empty()) return;
101
102 m_max_amplitude = 0.0;
103 for(size_t i=0; i<m_win.size(); i++)
104 {
105 m_plan.in[i] = buff[i]*m_win[i];
106
107 if(abs(buff[i])>m_max_amplitude)
108 m_max_amplitude = abs(buff[i]);
109 }
110 for(int i=int(m_win.size()); i<int(m_plan.size()); i++) // padd with zeros
111 m_plan.in[i] = 0.0;
112
113 m_plan.execute();
114
115 for(size_t i=0; i<m_comb.in.size() && i<m_plan.out.size(); i++)
116 m_comb.in[i] = mod(m_plan.out[i]);
117
118 // compute max with respect of the bounds
119 m_components_max = 0.0;
120 int max_index = -1;
121 double fmin = h2f(GetSemitoneMin());
122 double fmax = h2f(GetSemitoneMax());
123 for(size_t i=0; i<m_components.size(); i++)
124 {
125 double fi = i*double(GetSamplingRate())/m_plan.size();
126 if(fmin<=fi && fi<=fmax
127 && m_comb.in[i]>m_components_max)
128 {
129 max_index = i;
130 m_components_max = m_comb.in[i];
131 }
132 }
133
134 m_f0 = 0.0;
135
136 if(m_components_max>getComponentTreshold())
137 {
138 m_f0 = PeakRefinementLogParabola(m_plan.out, max_index)*double(GetSamplingRate())/m_plan.size();
139
140 // TODO TEST *win[i]; // me semble qu'une trans de harm signal n'est pas trop discontinue aux extrémités
141 m_comb.execute();
142
143 for(size_t i=0; i<m_components.size(); i++)
144 m_components[i] = 0.0;
145
146 for(size_t i=0; i<m_comb.out.size()/2; i++)
147 m_components[i] = real(m_comb.out[i]);
148
149 // avg is not interesting
150 m_components[0] = 0.0;
151
152 // frequencies between 1 and g_res_factor are virtuals
153 // for(int i=m_comb.out.size()-(g_res_factor-1); i<m_comb.out.size(); i++)
154 // m_components[i] = 0.0;
155
156 double step = (m_plan.size()/max_index)/2;
157 // hyp: the fund freq is not greater than the max amplitude harmonic
158 // keep only multiples of the max amplitude harmonic
159 for(int i=1; i<int(m_components.size())/step; i++)
160 m_components[i] = m_components[int(step*i)];
161 for(int i=int(m_components.size()/step); i<int(m_components.size()); i++)
162 m_components[i] = 0.0;
163
164 if(m_use_audibility_treshold)
165 {
166 for(int i=1; i<int(m_components.size()/step); i++)
167 m_components[i] /= (1+m_audib_ratio*(i-1));
168 }
169 else
170 {
171 vector<double> temp_comp(int(m_components.size()/step), 0.0);
172 for(int i=1; i<int(temp_comp.size()); i++)
173 temp_comp[i] = (m_components[i]-m_components[i-1]) +
174 (m_components[i]-m_components[i+1]);
175 for(int i=1; i<int(temp_comp.size()); i++)
176 m_components[i] = temp_comp[i];
177 }
178
179 // find the max
180 double max_amp = 0.0;
181 max_index = -1;
182 for(size_t i=0; i<m_components.size(); i++)
183 {
184 // double fi = i*double(GetSamplingRate())/m_win.size();
185 if(m_components[i]>max_amp)
186 {
187 max_index = i;
188 max_amp = m_components[i];
189 }
190 }
191
192 if(max_index>0)
193 m_f0 /= max_index;
194 }
195
196 // cout << " final: " << GetSamplingRate() << ":" << m_f0 << endl;
197 }
198 CombedFT::~CombedFT()
199 {
200 }
201 }