1 // Copyright 2005 "Gilles Degottex"
3 // This file is part of "Music"
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.
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.
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
20 #include "MonoQuantizer.h"
24 #include <Music/Music.h>
25 using namespace Music;
26 #include "LatencyMonoQuantizer.h"
28 LatencyMonoQuantizer::LatencyMonoQuantizer()
30 m_latency = 125; // ms
35 void LatencyMonoQuantizer::quantize(double freq)
37 // cerr << "LatencyMonoQuantizer::quantize " << freq << " m_states=" << m_states.size() << endl;
39 double current_time = m_time.elapsed();
41 m_states.push_front(State(current_time, freq));
43 double current_center_freq = h2f(f2h(freq));
46 map<double,Note> densities;
47 for(int i=0; i<m_states.size(); i++)
49 double cf = h2f(f2h(m_states[i].m_freq));
50 map<double,Note>::iterator it=densities.find(cf);
51 if(it==densities.end())
52 densities.insert(make_pair(cf,Note(m_states[i].m_freq)));
55 it->second.m_avg_freq *= it->second.m_dens;
56 it->second.m_avg_freq += m_states[i].m_freq;
58 it->second.m_avg_freq /= it->second.m_dens;
59 if(it->second.m_dens>max_dens)
61 max_dens = it->second.m_dens;
62 current_center_freq = it->first;
63 m_avg_freq = it->second.m_avg_freq;
68 // cerr << m_current_center_freq << ", " << current_center_freq << ", " << m_avg_freq << ", max_dens=" << max_dens << ", m_states.size()=" << m_states.size() << endl;
70 m_confidence = double(max_dens)/m_states.size();
72 // if a density is strong enough
73 if(m_confidence>m_min_confidence)
75 if(m_current_center_freq==0.0)
77 if(current_center_freq>0.0)
79 m_current_center_freq = current_center_freq;
81 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
82 m_duration.addMSecs(int(lag));
83 emit(noteStarted(m_current_center_freq, -lag));
88 if(current_center_freq==0.0)
90 m_current_center_freq = current_center_freq;
91 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
92 emit(noteFinished(m_current_center_freq, -lag));
94 else if(m_current_center_freq != current_center_freq)
96 double lag = (current_time-m_states.back().m_time)/2.0; // TODO pas forcément a fait 2 ~bruit
97 emit(noteFinished(m_current_center_freq, -lag));
98 m_current_center_freq = current_center_freq;
100 m_duration.addMSecs(int(lag));
101 emit(noteStarted(m_current_center_freq, -lag));
105 else if(m_current_center_freq>0.0)
107 m_current_center_freq = 0.0;
108 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
109 emit(noteFinished(m_current_center_freq, -lag));
112 while(!m_states.empty() && (current_time-m_states.back().m_time>m_latency))
116 void LatencyMonoQuantizer::reset()