1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "media/filters/wsola_internals.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "media/base/audio_bus.h"
22 bool InInterval(int n
, Interval q
) {
23 return n
>= q
.first
&& n
<= q
.second
;
26 float MultiChannelSimilarityMeasure(const float* dot_prod_a_b
,
27 const float* energy_a
,
28 const float* energy_b
,
30 const float kEpsilon
= 1e-12f
;
31 float similarity_measure
= 0.0f
;
32 for (int n
= 0; n
< channels
; ++n
) {
33 similarity_measure
+= dot_prod_a_b
[n
] / sqrt(energy_a
[n
] * energy_b
[n
] +
36 return similarity_measure
;
39 void MultiChannelDotProduct(const AudioBus
* a
,
45 DCHECK_EQ(a
->channels(), b
->channels());
46 DCHECK_GE(frame_offset_a
, 0);
47 DCHECK_GE(frame_offset_b
, 0);
48 DCHECK_LE(frame_offset_a
+ num_frames
, a
->frames());
49 DCHECK_LE(frame_offset_b
+ num_frames
, b
->frames());
51 memset(dot_product
, 0, sizeof(*dot_product
) * a
->channels());
52 for (int k
= 0; k
< a
->channels(); ++k
) {
53 const float* ch_a
= a
->channel(k
) + frame_offset_a
;
54 const float* ch_b
= b
->channel(k
) + frame_offset_b
;
55 for (int n
= 0; n
< num_frames
; ++n
) {
56 dot_product
[k
] += *ch_a
++ * *ch_b
++;
61 void MultiChannelMovingBlockEnergies(const AudioBus
* input
,
64 int num_blocks
= input
->frames() - (frames_per_block
- 1);
65 int channels
= input
->channels();
67 for (int k
= 0; k
< input
->channels(); ++k
) {
68 const float* input_channel
= input
->channel(k
);
72 // First block of channel |k|.
73 for (int m
= 0; m
< frames_per_block
; ++m
) {
74 energy
[k
] += input_channel
[m
] * input_channel
[m
];
77 const float* slide_out
= input_channel
;
78 const float* slide_in
= input_channel
+ frames_per_block
;
79 for (int n
= 1; n
< num_blocks
; ++n
, ++slide_in
, ++slide_out
) {
80 energy
[k
+ n
* channels
] = energy
[k
+ (n
- 1) * channels
] - *slide_out
*
81 *slide_out
+ *slide_in
* *slide_in
;
86 // Fit the curve f(x) = a * x^2 + b * x + c such that
90 void CubicInterpolation(const float* y_values
,
92 float* extremum_value
) {
93 float a
= 0.5f
* (y_values
[2] + y_values
[0]) - y_values
[1];
94 float b
= 0.5f
* (y_values
[2] - y_values
[0]);
95 float c
= y_values
[1];
98 *extremum
= -b
/ (2.f
* a
);
99 *extremum_value
= a
* (*extremum
) * (*extremum
) + b
* (*extremum
) + c
;
102 int DecimatedSearch(int decimation
,
103 Interval exclude_interval
,
104 const AudioBus
* target_block
,
105 const AudioBus
* search_segment
,
106 const float* energy_target_block
,
107 const float* energy_candidate_blocks
) {
108 int channels
= search_segment
->channels();
109 int block_size
= target_block
->frames();
110 int num_candidate_blocks
= search_segment
->frames() - (block_size
- 1);
111 scoped_ptr
<float[]> dot_prod(new float[channels
]);
112 float similarity
[3]; // Three elements for cubic interpolation.
115 MultiChannelDotProduct(target_block
, 0, search_segment
, n
, block_size
,
117 similarity
[0] = MultiChannelSimilarityMeasure(
118 dot_prod
.get(), energy_target_block
,
119 &energy_candidate_blocks
[n
* channels
], channels
);
121 // Set the starting point as optimal point.
122 float best_similarity
= similarity
[0];
123 int optimal_index
= 0;
126 if (n
>= num_candidate_blocks
) {
130 MultiChannelDotProduct(target_block
, 0, search_segment
, n
, block_size
,
132 similarity
[1] = MultiChannelSimilarityMeasure(
133 dot_prod
.get(), energy_target_block
,
134 &energy_candidate_blocks
[n
* channels
], channels
);
137 if (n
>= num_candidate_blocks
) {
138 // We cannot do any more sampling. Compare these two values and return the
140 return similarity
[1] > similarity
[0] ? decimation
: 0;
143 for (; n
< num_candidate_blocks
; n
+= decimation
) {
144 MultiChannelDotProduct(target_block
, 0, search_segment
, n
, block_size
,
147 similarity
[2] = MultiChannelSimilarityMeasure(
148 dot_prod
.get(), energy_target_block
,
149 &energy_candidate_blocks
[n
* channels
], channels
);
151 if ((similarity
[1] > similarity
[0] && similarity
[1] >= similarity
[2]) ||
152 (similarity
[1] >= similarity
[0] && similarity
[1] > similarity
[2])) {
153 // A local maximum is found. Do a cubic interpolation for a better
154 // estimate of candidate maximum.
155 float normalized_candidate_index
;
156 float candidate_similarity
;
157 CubicInterpolation(similarity
, &normalized_candidate_index
,
158 &candidate_similarity
);
160 int candidate_index
= n
- decimation
+ static_cast<int>(
161 normalized_candidate_index
* decimation
+ 0.5f
);
162 if (candidate_similarity
> best_similarity
&&
163 !InInterval(candidate_index
, exclude_interval
)) {
164 optimal_index
= candidate_index
;
165 best_similarity
= candidate_similarity
;
167 } else if (n
+ decimation
>= num_candidate_blocks
&&
168 similarity
[2] > best_similarity
&&
169 !InInterval(n
, exclude_interval
)) {
170 // If this is the end-point and has a better similarity-measure than
171 // optimal, then we accept it as optimal point.
173 best_similarity
= similarity
[2];
175 memmove(similarity
, &similarity
[1], 2 * sizeof(*similarity
));
177 return optimal_index
;
180 int FullSearch(int low_limit
,
182 Interval exclude_interval
,
183 const AudioBus
* target_block
,
184 const AudioBus
* search_block
,
185 const float* energy_target_block
,
186 const float* energy_candidate_blocks
) {
187 int channels
= search_block
->channels();
188 int block_size
= target_block
->frames();
189 scoped_ptr
<float[]> dot_prod(new float[channels
]);
191 float best_similarity
= std::numeric_limits
<float>::min();
192 int optimal_index
= 0;
194 for (int n
= low_limit
; n
<= high_limit
; ++n
) {
195 if (InInterval(n
, exclude_interval
)) {
198 MultiChannelDotProduct(target_block
, 0, search_block
, n
, block_size
,
201 float similarity
= MultiChannelSimilarityMeasure(
202 dot_prod
.get(), energy_target_block
,
203 &energy_candidate_blocks
[n
* channels
], channels
);
205 if (similarity
> best_similarity
) {
206 best_similarity
= similarity
;
211 return optimal_index
;
214 int OptimalIndex(const AudioBus
* search_block
,
215 const AudioBus
* target_block
,
216 Interval exclude_interval
) {
217 int channels
= search_block
->channels();
218 DCHECK_EQ(channels
, target_block
->channels());
219 int target_size
= target_block
->frames();
220 int num_candidate_blocks
= search_block
->frames() - (target_size
- 1);
222 // This is a compromise between complexity reduction and search accuracy. I
223 // don't have a proof that down sample of order 5 is optimal. One can compute
224 // a decimation factor that minimizes complexity given the size of
225 // |search_block| and |target_block|. However, my experiments show the rate of
226 // missing the optimal index is significant. This value is chosen
227 // heuristically based on experiments.
228 const int kSearchDecimation
= 5;
230 scoped_ptr
<float[]> energy_target_block(new float[channels
]);
231 scoped_ptr
<float[]> energy_candidate_blocks(
232 new float[channels
* num_candidate_blocks
]);
234 // Energy of all candid frames.
235 MultiChannelMovingBlockEnergies(search_block
, target_size
,
236 energy_candidate_blocks
.get());
238 // Energy of target frame.
239 MultiChannelDotProduct(target_block
, 0, target_block
, 0,
240 target_size
, energy_target_block
.get());
242 int optimal_index
= DecimatedSearch(kSearchDecimation
,
243 exclude_interval
, target_block
,
244 search_block
, energy_target_block
.get(),
245 energy_candidate_blocks
.get());
247 int lim_low
= std::max(0, optimal_index
- kSearchDecimation
);
248 int lim_high
= std::min(num_candidate_blocks
- 1,
249 optimal_index
+ kSearchDecimation
);
250 return FullSearch(lim_low
, lim_high
, exclude_interval
, target_block
,
251 search_block
, energy_target_block
.get(),
252 energy_candidate_blocks
.get());
255 void GetSymmetricHanningWindow(int window_length
, float* window
) {
256 const float scale
= 2.0f
* M_PI
/ window_length
;
257 for (int n
= 0; n
< window_length
; ++n
)
258 window
[n
] = 0.5f
* (1.0f
- cosf(n
* scale
));
261 } // namespace internal