File: MultiCumulativeDiffAlgo.cpp

package info (click to toggle)
fmit 1.3.2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,356 kB
  • sloc: cpp: 9,515; sh: 69; makefile: 12
file content (225 lines) | stat: -rw-r--r-- 6,073 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2004 "Gilles Degottex"

// This file is part of "Music"

// "Music" is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// "Music" is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


#include "MultiCumulativeDiffAlgo.h"

#include <cassert>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
#include <CppAddons/CAMath.h>
using namespace Math;

#include "Music.h"

//#define MUSIC_DEBUG
#ifdef MUSIC_DEBUG
#define LOG(a)	a
#else
#define LOG(a)
#endif

namespace Music
{
	void MultiCumulativeDiffAlgo::init()
	{
		for(size_t i=0; i<size(); i++)
		{
			if(m_diffs[i]!=NULL)	delete m_diffs[i];

			m_diffs[i] = new CumulativeDiff(1, i+GetSemitoneMin());
		}
	}

	MultiCumulativeDiffAlgo::MultiCumulativeDiffAlgo(int latency_factor, double test_complexity)
	{
        (void)latency_factor;

		assert(GetSamplingRate()>0);

		m_test_complexity = test_complexity;

		m_diffs.resize(size());
		for(size_t i=0; i<size(); i++)
			m_diffs[i] = NULL;
		init();
	}
	bool MultiCumulativeDiffAlgo::is_minima(int ih)
	{
		if(ih+1>=0 && ih+1<int(size()))
			if(m_components[ih+1]<=m_components[ih])
				return false;
		if(ih-1>=0 && ih-1<int(size()))
			if(m_components[ih-1]<=m_components[ih])
				return false;
		return true;
	}
	double MultiCumulativeDiffAlgo::getFondamentalWaveLength() const
	{
		return int(GetSamplingRate()/h2f(m_first_fond+GetSemitoneMin(), GetAFreq()));
	}
	void MultiCumulativeDiffAlgo::apply(const deque<double>& buff)
	{
		assert(GetSamplingRate()>0);
		for(size_t i=0; i<size(); i++)
		{
			m_components[i] = 1.0;
			m_is_fondamental[i] = false;
		}

		m_first_fond = -1;

		if(buff.empty() || buff.size()<(m_test_complexity+1)*m_diffs[0]->m_s)
			return;

		double v = 0.0;
		for(size_t i=0; i<buff.size() && v<=getAmplitudeTreshold() && i<m_diffs[0]->m_s; i++)
            v = std::max(v, abs(buff[i]));

		if(v>getAmplitudeTreshold())
		{
			// compute all components
			m_components_max = 0.0;
//            double min_comp = 1000000;
			double max_sum = 0.0;
			for(int ih=int(size())-1; ih>=0; ih--)
			{
				m_diffs[ih]->receive(buff, 0);
				m_components[ih] = m_diffs[ih]->m_error;
                m_components_max = std::max(m_components_max, m_components[ih]);
			}

			// test components
			for(int ih=int(size())-1; ih>=0; ih--)
			{
				bool ok = true;

//				bool crit_min = true;
				// criteria: the fond and his first harmonics are minimas
				if(ok)	ok =
							is_minima(ih) &&
							is_minima(ih-12) &&
							(is_minima(ih-19) || is_minima(ih-19-1) || is_minima(ih-19+1)) &&
							is_minima(ih-24);

//				crit_min = ok;

				bool crit_small_enough = true;
				if(ok)
				{
					if(m_components[ih]/m_components_max>getComponentTreshold())
						crit_small_enough = false;
					ok = crit_small_enough;
				}

//				bool crit_cross_zero = true;
				/*
				// criteria: wave should cross the zero value
				if(ok)
				{
					bool cross = true;
					for(int s=0; cross && s<int(m_diffs[ih]->m_s); s++)
					{
						double sg = Math::sgn(buff[s]);
						bool same_side = true;
						for(int i=0; same_side && i<int(m_diffs[ih]->m_s); i++)
							same_side = Math::sgn(buff[s+i])==sg;

						cross = !same_side;
					}

					ok = crit_cross_zero = cross;
				}
				*/

//				bool crit_integ_cst = true;
				// criteria: integral should be nearly constant while shifting
				// TODO
				// (the previous criteria seems sufficient to remove high comp.)

//				LOG(if(crit_min)
//					cout << "ih=" << ih <<
//					" harm_min=(("<<is_minima(ih-12)<<","<<is_minima(ih-12-1)<<","<<is_minima(ih-12+1)<<"),("<<
//						is_minima(ih-19)<<","<<is_minima(ih-19-1)<<","<<is_minima(ih-19+1)<<"),("<<
//						is_minima(ih-24)<<","<<is_minima(ih-24-1)<<","<<is_minima(ih-24+1)<<"))"<< crit_min <<
//					" increase=" << crit_incr_err <<
//					" cross_zero=" << crit_cross_zero <<
//					" integ_cst=" << crit_integ_cst <<
//					" ok=" << ok << " c=" << m_components[ih] << endl;)

				// if all criteria are ok
				if(ok)
				{
					double sum = 0.0;
					int n=0;
					int i=0;
					double wh = 1.0;
					sum += wh*(m_components_max-m_components[ih]); n++;
					i=12;	if(ih-i>=0)	{sum+=wh*(m_components_max-m_components[ih-i]);	n++;}
					i=19;	if(ih-i>=0)	{sum+=wh*(m_components_max-m_components[ih-i]);	n++;}
					i=24;	if(ih-i>=0)	{sum+=wh*(m_components_max-m_components[ih-i]);	n++;}

                    LOG(cout << "ih=" << ih << " sum=" << sum << endl;)

					// get the "best"
					if(sum>max_sum)
					{
						size_t step = size_t(m_diffs[ih]->m_s/m_test_complexity);
						if(step<1)	step = 1;
						for(size_t s=0; ok && s<m_diffs[ih]->m_s; s+=step)
						{
							if(ih-1>=0){
								m_diffs[ih-1]->receive(buff, s);
								m_components[ih-1] = m_diffs[ih-1]->m_error;
							}
							if(ih+1<int(size())){
								m_diffs[ih+1]->receive(buff, s);
								m_components[ih+1] = m_diffs[ih+1]->m_error;
							}
							m_diffs[ih]->receive(buff, s);
							m_components[ih] = m_diffs[ih]->m_error;
							ok = is_minima(ih);
						}

						if(ok)
						{
							max_sum = sum;
//							min_comp = m_components[ih];
							m_first_fond = ih;
						}
					}
				}
			}

// 			cout << "ff: " << m_first_fond << endl;

			if(m_first_fond!=-1)
				m_is_fondamental[m_first_fond] = true;

            LOG(cout << "m_first_fond=" << m_first_fond << endl;)
		}
	}
	MultiCumulativeDiffAlgo::~MultiCumulativeDiffAlgo()
	{
		for(size_t i=0; i<m_diffs.size(); i++)
			delete m_diffs[i];
	}
}