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 #include "CumulativeDiff.h"
21 
22 #include <iostream>
23 #include "Music.h"
24 using namespace Music;
25 
CumulativeDiff(double latency_factor,int ht)26 CumulativeDiff::CumulativeDiff(double latency_factor, int ht)
27 : m_ht(ht)
28 , m_freq(h2f(m_ht))
29 , m_s(size_t(GetSamplingRate()/m_freq))
30 , m_latency_factor(latency_factor)
31 {
32 	m_error = 0.0;
33 
34 //	cout << "CumulativeDiff::CumulativeDiff ht=" << ht << " AFreq="<<GetAFreq() << " freq=" << m_freq << endl;
35 }
36 
receive(const deque<double> & buff,size_t start)37 void CumulativeDiff::receive(const deque<double>& buff, size_t start)
38 {
39 	if(buff.size()<start+(m_latency_factor+1)*m_s)	return;
40 
41 	m_error = 0.0;
42 
43 	for(size_t i=0; i<m_latency_factor*m_s; i++)
44 		m_error += abs(buff[start+i] - buff[start+i+m_s]);
45 }
46 
RangedCumulativeDiff(double pitch_tolerance,double latency_factor,int ht)47 RangedCumulativeDiff::RangedCumulativeDiff(double pitch_tolerance, double latency_factor, int ht)
48 : m_ht(ht)
49 , m_freq(h2f(m_ht))
50 , m_pitch_tolerance(pitch_tolerance)
51 , m_smin(size_t(GetSamplingRate()/(m_freq + m_pitch_tolerance*(h2f(m_ht+1)-m_freq))))
52 , m_smax(size_t(GetSamplingRate()/(m_freq + m_pitch_tolerance*(h2f(m_ht-1)-m_freq))))
53 , m_latency_factor(latency_factor)
54 {
55 	m_error = 0.0;
56 
57 //	cout << "CumulativeDiff::CumulativeDiff ht=" << ht << " pitch_tol=" << m_pitch_tolerance << " seek_factor=" << m_seek_factor << " s=" << m_s << " smin=" << m_smin << " smax=" << m_smax << endl;
58 }
59 
receive(const deque<double> & buff,size_t start)60 void RangedCumulativeDiff::receive(const deque<double>& buff, size_t start)
61 {
62 	if(buff.size()<start+m_latency_factor*m_smax+m_smax)	return;
63 
64 	m_error = 0.0;
65 	m_min_error = 1000.0;
66 //	int m=0;
67 	for(size_t s=m_smin; s<=m_smax; s++)
68 	{
69 		double err = 0.0;
70 //		size_t n = 0;
71 
72 		for(size_t i=0; i<m_latency_factor*s; i++)
73 		{
74 			err += abs(buff[start+i] - buff[start+i+s]);
75 //			n++;
76 		}
77 
78 //		err /= n;
79 
80 		if(err<m_min_error)
81 		{
82 			m_min_wave_length = s;
83 			m_min_error = err;
84 		}
85 //		m_error = max(m_error, err);
86 		m_error = m_min_error;
87 //		cout << m_error << endl;
88 //		m_error += err;
89 //		m++;
90 	}
91 
92 //	m_error /= m;
93 }
94 
GetMinWaveLength(double pitch_tolerance,int ht,double & error,double min_wave_length)95 void RangedCumulativeDiff::GetMinWaveLength(double pitch_tolerance, int ht, double& error, double min_wave_length)
96 {
97     (void)pitch_tolerance;
98     (void)ht;
99     (void)error;
100     (void)min_wave_length;
101 }
102 
103