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"
25 #include <Music/Music.h>
26 using namespace Music;
27 #include "LatencyMonoQuantizer.h"
29 LatencyMonoQuantizer::LatencyMonoQuantizer()
31 m_latency = 125; // ms
36 void LatencyMonoQuantizer::quantize(double freq)
38 // cerr << "LatencyMonoQuantizer::quantize " << freq << " m_states=" << m_states.size() << endl;
40 double current_time = m_time.elapsed();
42 m_states.push_front(State(current_time, freq));
44 double current_center_freq = f2cf(freq);
47 map<double,Note> densities;
48 for(int i=0; i<m_states.size(); i++)
50 double cf = f2cf(m_states[i].m_freq);
51 map<double,Note>::iterator it=densities.find(cf);
52 if(it==densities.end())
53 densities.insert(make_pair(cf,Note(m_states[i].m_freq)));
56 it->second.m_avg_freq *= it->second.m_dens;
57 it->second.m_avg_freq += m_states[i].m_freq;
59 it->second.m_avg_freq /= it->second.m_dens;
60 if(it->second.m_dens>max_dens)
62 max_dens = it->second.m_dens;
63 current_center_freq = it->first;
64 m_avg_freq = it->second.m_avg_freq;
69 // cerr << m_current_center_freq << ", " << current_center_freq << ", " << m_avg_freq << ", max_dens=" << max_dens << ", m_states.size()=" << m_states.size() << endl;
71 m_confidence = double(max_dens)/m_states.size();
73 // if a density is strong enough
74 if(m_confidence>m_min_confidence)
76 if(m_current_center_freq==0.0)
78 if(current_center_freq>0.0)
80 m_current_center_freq = current_center_freq;
82 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
83 m_duration.addMSecs(int(lag));
84 emit(noteStarted(m_current_center_freq, -lag));
89 if(current_center_freq==0.0)
91 m_current_center_freq = current_center_freq;
92 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
93 emit(noteFinished(m_current_center_freq, -lag));
95 else if(m_current_center_freq != current_center_freq)
97 double lag = (current_time-m_states.back().m_time)/2.0; // TODO pas forcément a fait 2 ~bruit
98 emit(noteFinished(m_current_center_freq, -lag));
99 m_current_center_freq = current_center_freq;
101 m_duration.addMSecs(int(lag));
102 emit(noteStarted(m_current_center_freq, -lag));
106 else if(m_current_center_freq>0.0)
108 m_current_center_freq = 0.0;
109 double lag = (current_time-m_states.back().m_time)*m_min_confidence;
110 emit(noteFinished(m_current_center_freq, -lag));
113 while(!m_states.empty() && (current_time-m_states.back().m_time>m_latency))
117 void LatencyMonoQuantizer::reset()