| 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 "MultiCumulativeDiffAlgo.h" |
| 21 |
|
| 22 |
#include <cassert> |
| 23 |
#include <cmath> |
| 24 |
#include <iostream> |
| 25 |
#include <algorithm> |
| 26 |
#include <limits> |
| 27 |
using namespace std; |
| 28 |
#include <CppAddons/CAMath.h> |
| 29 |
using namespace Math; |
| 30 |
|
| 31 |
#include "Music.h" |
| 32 |
|
| 33 |
//#define MUSIC_DEBUG |
| 34 |
#ifdef MUSIC_DEBUG |
| 35 |
#define LOG(a) a |
| 36 |
#else |
| 37 |
#define LOG(a) |
| 38 |
#endif |
| 39 |
|
| 40 |
namespace Music |
| 41 |
{ |
| 42 |
void MultiCumulativeDiffAlgo::init() |
| 43 |
{ |
| 44 |
for(size_t i=0; i<size(); i++) |
| 45 |
{ |
| 46 |
if(m_diffs[i]!=NULL) delete m_diffs[i]; |
| 47 |
|
| 48 |
m_diffs[i] = new CumulativeDiff(1, i+GetSemitoneMin()); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
MultiCumulativeDiffAlgo::MultiCumulativeDiffAlgo(int latency_factor, double test_complexity) |
| 53 |
{ |
| 54 |
(void)latency_factor; |
| 55 |
|
| 56 |
assert(GetSamplingRate()>0); |
| 57 |
|
| 58 |
m_test_complexity = test_complexity; |
| 59 |
|
| 60 |
m_diffs.resize(size()); |
| 61 |
for(size_t i=0; i<size(); i++) |
| 62 |
m_diffs[i] = NULL; |
| 63 |
init(); |
| 64 |
} |
| 65 |
bool MultiCumulativeDiffAlgo::is_minima(int ih) |
| 66 |
{ |
| 67 |
if(ih+1>=0 && ih+1<int(size())) |
| 68 |
if(m_components[ih+1]<=m_components[ih]) |
| 69 |
return false; |
| 70 |
if(ih-1>=0 && ih-1<int(size())) |
| 71 |
if(m_components[ih-1]<=m_components[ih]) |
| 72 |
return false; |
| 73 |
return true; |
| 74 |
} |
| 75 |
double MultiCumulativeDiffAlgo::getFondamentalWaveLength() const |
| 76 |
{ |
| 77 |
return int(GetSamplingRate()/h2f(m_first_fond+GetSemitoneMin(), GetAFreq())); |
| 78 |
} |
| 79 |
void MultiCumulativeDiffAlgo::apply(const deque<double>& buff) |
| 80 |
{ |
| 81 |
assert(GetSamplingRate()>0); |
| 82 |
for(size_t i=0; i<size(); i++) |
| 83 |
{ |
| 84 |
m_components[i] = 1.0; |
| 85 |
m_is_fondamental[i] = false; |
| 86 |
} |
| 87 |
|
| 88 |
m_first_fond = -1; |
| 89 |
|
| 90 |
if(buff.empty() || buff.size()<(m_test_complexity+1)*m_diffs[0]->m_s) |
| 91 |
return; |
| 92 |
|
| 93 |
double v = 0.0; |
| 94 |
for(size_t i=0; i<buff.size() && v<=getAmplitudeTreshold() && i<m_diffs[0]->m_s; i++) |
| 95 |
v = std::max(v, abs(buff[i])); |
| 96 |
|
| 97 |
if(v>getAmplitudeTreshold()) |
| 98 |
{ |
| 99 |
// compute all components |
| 100 |
m_components_max = 0.0; |
| 101 |
// double min_comp = 1000000; |
| 102 |
double max_sum = 0.0; |
| 103 |
for(int ih=int(size())-1; ih>=0; ih--) |
| 104 |
{ |
| 105 |
m_diffs[ih]->receive(buff, 0); |
| 106 |
m_components[ih] = m_diffs[ih]->m_error; |
| 107 |
m_components_max = std::max(m_components_max, m_components[ih]); |
| 108 |
} |
| 109 |
|
| 110 |
// test components |
| 111 |
for(int ih=int(size())-1; ih>=0; ih--) |
| 112 |
{ |
| 113 |
bool ok = true; |
| 114 |
|
| 115 |
// bool crit_min = true; |
| 116 |
// criteria: the fond and his first harmonics are minimas |
| 117 |
if(ok) ok = |
| 118 |
is_minima(ih) && |
| 119 |
is_minima(ih-12) && |
| 120 |
(is_minima(ih-19) || is_minima(ih-19-1) || is_minima(ih-19+1)) && |
| 121 |
is_minima(ih-24); |
| 122 |
|
| 123 |
// crit_min = ok; |
| 124 |
|
| 125 |
bool crit_small_enough = true; |
| 126 |
if(ok) |
| 127 |
{ |
| 128 |
if(m_components[ih]/m_components_max>getComponentTreshold()) |
| 129 |
crit_small_enough = false; |
| 130 |
ok = crit_small_enough; |
| 131 |
} |
| 132 |
|
| 133 |
// bool crit_cross_zero = true; |
| 134 |
/* |
| 135 |
// criteria: wave should cross the zero value |
| 136 |
if(ok) |
| 137 |
{ |
| 138 |
bool cross = true; |
| 139 |
for(int s=0; cross && s<int(m_diffs[ih]->m_s); s++) |
| 140 |
{ |
| 141 |
double sg = Math::sgn(buff[s]); |
| 142 |
bool same_side = true; |
| 143 |
for(int i=0; same_side && i<int(m_diffs[ih]->m_s); i++) |
| 144 |
same_side = Math::sgn(buff[s+i])==sg; |
| 145 |
|
| 146 |
cross = !same_side; |
| 147 |
} |
| 148 |
|
| 149 |
ok = crit_cross_zero = cross; |
| 150 |
} |
| 151 |
*/ |
| 152 |
|
| 153 |
// bool crit_integ_cst = true; |
| 154 |
// criteria: integral should be nearly constant while shifting |
| 155 |
// TODO |
| 156 |
// (the previous criteria seems sufficient to remove high comp.) |
| 157 |
|
| 158 |
// LOG(if(crit_min) |
| 159 |
// cout << "ih=" << ih << |
| 160 |
// " harm_min=(("<<is_minima(ih-12)<<","<<is_minima(ih-12-1)<<","<<is_minima(ih-12+1)<<"),("<< |
| 161 |
// is_minima(ih-19)<<","<<is_minima(ih-19-1)<<","<<is_minima(ih-19+1)<<"),("<< |
| 162 |
// is_minima(ih-24)<<","<<is_minima(ih-24-1)<<","<<is_minima(ih-24+1)<<"))"<< crit_min << |
| 163 |
// " increase=" << crit_incr_err << |
| 164 |
// " cross_zero=" << crit_cross_zero << |
| 165 |
// " integ_cst=" << crit_integ_cst << |
| 166 |
// " ok=" << ok << " c=" << m_components[ih] << endl;) |
| 167 |
|
| 168 |
// if all criteria are ok |
| 169 |
if(ok) |
| 170 |
{ |
| 171 |
double sum = 0.0; |
| 172 |
int n=0; |
| 173 |
int i=0; |
| 174 |
double wh = 1.0; |
| 175 |
sum += wh*(m_components_max-m_components[ih]); n++; |
| 176 |
i=12; if(ih-i>=0) {sum+=wh*(m_components_max-m_components[ih-i]); n++;} |
| 177 |
i=19; if(ih-i>=0) {sum+=wh*(m_components_max-m_components[ih-i]); n++;} |
| 178 |
i=24; if(ih-i>=0) {sum+=wh*(m_components_max-m_components[ih-i]); n++;} |
| 179 |
|
| 180 |
LOG(cout << "ih=" << ih << " sum=" << sum << endl;) |
| 181 |
|
| 182 |
// get the "best" |
| 183 |
if(sum>max_sum) |
| 184 |
{ |
| 185 |
size_t step = size_t(m_diffs[ih]->m_s/m_test_complexity); |
| 186 |
if(step<1) step = 1; |
| 187 |
for(size_t s=0; ok && s<m_diffs[ih]->m_s; s+=step) |
| 188 |
{ |
| 189 |
if(ih-1>=0){ |
| 190 |
m_diffs[ih-1]->receive(buff, s); |
| 191 |
m_components[ih-1] = m_diffs[ih-1]->m_error; |
| 192 |
} |
| 193 |
if(ih+1<int(size())){ |
| 194 |
m_diffs[ih+1]->receive(buff, s); |
| 195 |
m_components[ih+1] = m_diffs[ih+1]->m_error; |
| 196 |
} |
| 197 |
m_diffs[ih]->receive(buff, s); |
| 198 |
m_components[ih] = m_diffs[ih]->m_error; |
| 199 |
ok = is_minima(ih); |
| 200 |
} |
| 201 |
|
| 202 |
if(ok) |
| 203 |
{ |
| 204 |
max_sum = sum; |
| 205 |
// min_comp = m_components[ih]; |
| 206 |
m_first_fond = ih; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// cout << "ff: " << m_first_fond << endl; |
| 213 |
|
| 214 |
if(m_first_fond!=-1) |
| 215 |
m_is_fondamental[m_first_fond] = true; |
| 216 |
|
| 217 |
LOG(cout << "m_first_fond=" << m_first_fond << endl;) |
| 218 |
} |
| 219 |
} |
| 220 |
MultiCumulativeDiffAlgo::~MultiCumulativeDiffAlgo() |
| 221 |
{ |
| 222 |
for(size_t i=0; i<m_diffs.size(); i++) |
| 223 |
delete m_diffs[i]; |
| 224 |
} |
| 225 |
} |