| 1 |
/* |
| 2 |
* Copyright © 2016 Mozilla Foundation |
| 3 |
* |
| 4 |
* This program is made available under an ISC-style license. See the |
| 5 |
* accompanying file LICENSE for details. |
| 6 |
*/ |
| 7 |
#ifndef NOMINMAX |
| 8 |
#define NOMINMAX |
| 9 |
#endif // NOMINMAX |
| 10 |
#include "common.h" |
| 11 |
#include "cubeb_resampler_internal.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
#include <algorithm> |
| 14 |
#include <iostream> |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
/* Windows cmath USE_MATH_DEFINE thing... */ |
| 18 |
const float PI = 3.14159265359f; |
| 19 |
|
| 20 |
/* Testing all sample rates is very long, so if THOROUGH_TESTING is not defined, |
| 21 |
* only part of the test suite is ran. */ |
| 22 |
#ifdef THOROUGH_TESTING |
| 23 |
/* Some standard sample rates we're testing with. */ |
| 24 |
const uint32_t sample_rates[] = {8000, 16000, 32000, 44100, |
| 25 |
48000, 88200, 96000, 192000}; |
| 26 |
/* The maximum number of channels we're resampling. */ |
| 27 |
const uint32_t max_channels = 2; |
| 28 |
/* The minimum an maximum number of milliseconds we're resampling for. This is |
| 29 |
* used to simulate the fact that the audio stream is resampled in chunks, |
| 30 |
* because audio is delivered using callbacks. */ |
| 31 |
const uint32_t min_chunks = 10; /* ms */ |
| 32 |
const uint32_t max_chunks = 30; /* ms */ |
| 33 |
const uint32_t chunk_increment = 1; |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
const uint32_t sample_rates[] = { |
| 38 |
8000, |
| 39 |
44100, |
| 40 |
48000, |
| 41 |
}; |
| 42 |
const uint32_t max_channels = 2; |
| 43 |
const uint32_t min_chunks = 10; /* ms */ |
| 44 |
const uint32_t max_chunks = 30; /* ms */ |
| 45 |
const uint32_t chunk_increment = 10; |
| 46 |
#endif |
| 47 |
|
| 48 |
// #define DUMP_ARRAYS |
| 49 |
#ifdef DUMP_ARRAYS |
| 50 |
/** |
| 51 |
* Files produced by dump(...) can be converted to .wave files using: |
| 52 |
* |
| 53 |
* sox -c <channel_count> -r <rate> -e float -b 32 file.raw file.wav |
| 54 |
* |
| 55 |
* for floating-point audio, or: |
| 56 |
* |
| 57 |
* sox -c <channel_count> -r <rate> -e unsigned -b 16 file.raw file.wav |
| 58 |
* |
| 59 |
* for 16bit integer audio. |
| 60 |
*/ |
| 61 |
|
| 62 |
/* Use the correct implementation of fopen, depending on the platform. */ |
| 63 |
void |
| 64 |
fopen_portable(FILE ** f, const char * name, const char * mode) |
| 65 |
{ |
| 66 |
#ifdef WIN32 |
| 67 |
fopen_s(f, name, mode); |
| 68 |
#else |
| 69 |
*f = fopen(name, mode); |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
template <typename T> |
| 74 |
void |
| 75 |
dump(const char * name, T * frames, size_t count) |
| 76 |
{ |
| 77 |
FILE * file; |
| 78 |
fopen_portable(&file, name, "wb"); |
| 79 |
|
| 80 |
if (!file) { |
| 81 |
fprintf(stderr, "error opening %s\n", name); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (count != fwrite(frames, sizeof(T), count, file)) { |
| 86 |
fprintf(stderr, "error writing to %s\n", name); |
| 87 |
} |
| 88 |
fclose(file); |
| 89 |
} |
| 90 |
#else |
| 91 |
template <typename T> |
| 92 |
void |
| 93 |
dump(const char * name, T * frames, size_t count) |
| 94 |
{ |
| 95 |
} |
| 96 |
#endif |
| 97 |
|
| 98 |
// The more the ratio is far from 1, the more we accept a big error. |
| 99 |
float |
| 100 |
epsilon_tweak_ratio(float ratio) |
| 101 |
{ |
| 102 |
return ratio >= 1 ? ratio : 1 / ratio; |
| 103 |
} |
| 104 |
|
| 105 |
// Epsilon values for comparing resampled data to expected data. |
| 106 |
// The bigger the resampling ratio is, the more lax we are about errors. |
| 107 |
template <typename T> |
| 108 |
T |
| 109 |
epsilon(float ratio); |
| 110 |
|
| 111 |
template <> |
| 112 |
float |
| 113 |
epsilon(float ratio) |
| 114 |
{ |
| 115 |
return 0.08f * epsilon_tweak_ratio(ratio); |
| 116 |
} |
| 117 |
|
| 118 |
template <> |
| 119 |
int16_t |
| 120 |
epsilon(float ratio) |
| 121 |
{ |
| 122 |
return static_cast<int16_t>(10 * epsilon_tweak_ratio(ratio)); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
test_delay_lines(uint32_t delay_frames, uint32_t channels, uint32_t chunk_ms) |
| 127 |
{ |
| 128 |
const size_t length_s = 2; |
| 129 |
const size_t rate = 44100; |
| 130 |
const size_t length_frames = rate * length_s; |
| 131 |
delay_line<float> delay(delay_frames, channels, rate); |
| 132 |
auto_array<float> input; |
| 133 |
auto_array<float> output; |
| 134 |
uint32_t chunk_length = channels * chunk_ms * rate / 1000; |
| 135 |
uint32_t output_offset = 0; |
| 136 |
uint32_t channel = 0; |
| 137 |
|
| 138 |
/** Generate diracs every 100 frames, and check they are delayed. */ |
| 139 |
input.push_silence(length_frames * channels); |
| 140 |
for (uint32_t i = 0; i < input.length() - 1; i += 100) { |
| 141 |
input.data()[i + channel] = 0.5; |
| 142 |
channel = (channel + 1) % channels; |
| 143 |
} |
| 144 |
dump("input.raw", input.data(), input.length()); |
| 145 |
while (input.length()) { |
| 146 |
uint32_t to_pop = |
| 147 |
std::min<uint32_t>(input.length(), chunk_length * channels); |
| 148 |
float * in = delay.input_buffer(to_pop / channels); |
| 149 |
input.pop(in, to_pop); |
| 150 |
delay.written(to_pop / channels); |
| 151 |
output.push_silence(to_pop); |
| 152 |
delay.output(output.data() + output_offset, to_pop / channels); |
| 153 |
output_offset += to_pop; |
| 154 |
} |
| 155 |
|
| 156 |
// Check the diracs have been shifted by `delay_frames` frames. |
| 157 |
for (uint32_t i = 0; i < output.length() - delay_frames * channels + 1; |
| 158 |
i += 100) { |
| 159 |
ASSERT_EQ(output.data()[i + channel + delay_frames * channels], 0.5); |
| 160 |
channel = (channel + 1) % channels; |
| 161 |
} |
| 162 |
|
| 163 |
dump("output.raw", output.data(), output.length()); |
| 164 |
} |
| 165 |
/** |
| 166 |
* This takes sine waves with a certain `channels` count, `source_rate`, and |
| 167 |
* resample them, by chunk of `chunk_duration` milliseconds, to `target_rate`. |
| 168 |
* Then a sample-wise comparison is performed against a sine wave generated at |
| 169 |
* the correct rate. |
| 170 |
*/ |
| 171 |
template <typename T> |
| 172 |
void |
| 173 |
test_resampler_one_way(uint32_t channels, uint32_t source_rate, |
| 174 |
uint32_t target_rate, float chunk_duration) |
| 175 |
{ |
| 176 |
size_t chunk_duration_in_source_frames = |
| 177 |
static_cast<uint32_t>(ceil(chunk_duration * source_rate / 1000.)); |
| 178 |
float resampling_ratio = static_cast<float>(source_rate) / target_rate; |
| 179 |
cubeb_resampler_speex_one_way<T> resampler(channels, source_rate, target_rate, |
| 180 |
3); |
| 181 |
auto_array<T> source(channels * source_rate * 10); |
| 182 |
auto_array<T> destination(channels * target_rate * 10); |
| 183 |
auto_array<T> expected(channels * target_rate * 10); |
| 184 |
uint32_t phase_index = 0; |
| 185 |
uint32_t offset = 0; |
| 186 |
const uint32_t buf_len = 2; /* seconds */ |
| 187 |
|
| 188 |
// generate a sine wave in each channel, at the source sample rate |
| 189 |
source.push_silence(channels * source_rate * buf_len); |
| 190 |
while (offset != source.length()) { |
| 191 |
float p = phase_index++ / static_cast<float>(source_rate); |
| 192 |
for (uint32_t j = 0; j < channels; j++) { |
| 193 |
source.data()[offset++] = 0.5 * sin(440. * 2 * PI * p); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
dump("input.raw", source.data(), source.length()); |
| 198 |
|
| 199 |
expected.push_silence(channels * target_rate * buf_len); |
| 200 |
// generate a sine wave in each channel, at the target sample rate. |
| 201 |
// Insert silent samples at the beginning to account for the resampler |
| 202 |
// latency. |
| 203 |
offset = resampler.latency() * channels; |
| 204 |
for (uint32_t i = 0; i < offset; i++) { |
| 205 |
expected.data()[i] = 0.0f; |
| 206 |
} |
| 207 |
phase_index = 0; |
| 208 |
while (offset != expected.length()) { |
| 209 |
float p = phase_index++ / static_cast<float>(target_rate); |
| 210 |
for (uint32_t j = 0; j < channels; j++) { |
| 211 |
expected.data()[offset++] = 0.5 * sin(440. * 2 * PI * p); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
dump("expected.raw", expected.data(), expected.length()); |
| 216 |
|
| 217 |
// resample by chunk |
| 218 |
uint32_t write_offset = 0; |
| 219 |
destination.push_silence(channels * target_rate * buf_len); |
| 220 |
while (write_offset < destination.length()) { |
| 221 |
size_t output_frames = static_cast<uint32_t>( |
| 222 |
floor(chunk_duration_in_source_frames / resampling_ratio)); |
| 223 |
uint32_t input_frames = resampler.input_needed_for_output(output_frames); |
| 224 |
resampler.input(source.data(), input_frames); |
| 225 |
source.pop(nullptr, input_frames * channels); |
| 226 |
resampler.output( |
| 227 |
destination.data() + write_offset, |
| 228 |
std::min(output_frames, |
| 229 |
(destination.length() - write_offset) / channels)); |
| 230 |
write_offset += output_frames * channels; |
| 231 |
} |
| 232 |
|
| 233 |
dump("output.raw", destination.data(), expected.length()); |
| 234 |
|
| 235 |
// compare, taking the latency into account |
| 236 |
bool fuzzy_equal = true; |
| 237 |
for (uint32_t i = resampler.latency() + 1; i < expected.length(); i++) { |
| 238 |
float diff = fabs(expected.data()[i] - destination.data()[i]); |
| 239 |
if (diff > epsilon<T>(resampling_ratio)) { |
| 240 |
fprintf(stderr, "divergence at %d: %f %f (delta %f)\n", i, |
| 241 |
expected.data()[i], destination.data()[i], diff); |
| 242 |
fuzzy_equal = false; |
| 243 |
} |
| 244 |
} |
| 245 |
ASSERT_TRUE(fuzzy_equal); |
| 246 |
} |
| 247 |
|
| 248 |
template <typename T> |
| 249 |
cubeb_sample_format |
| 250 |
cubeb_format(); |
| 251 |
|
| 252 |
template <> |
| 253 |
cubeb_sample_format |
| 254 |
cubeb_format<float>() |
| 255 |
{ |
| 256 |
return CUBEB_SAMPLE_FLOAT32NE; |
| 257 |
} |
| 258 |
|
| 259 |
template <> |
| 260 |
cubeb_sample_format |
| 261 |
cubeb_format<short>() |
| 262 |
{ |
| 263 |
return CUBEB_SAMPLE_S16NE; |
| 264 |
} |
| 265 |
|
| 266 |
struct osc_state { |
| 267 |
osc_state() |
| 268 |
: input_phase_index(0), output_phase_index(0), output_offset(0), |
| 269 |
input_channels(0), output_channels(0) |
| 270 |
{ |
| 271 |
} |
| 272 |
uint32_t input_phase_index; |
| 273 |
uint32_t max_output_phase_index; |
| 274 |
uint32_t output_phase_index; |
| 275 |
uint32_t output_offset; |
| 276 |
uint32_t input_channels; |
| 277 |
uint32_t output_channels; |
| 278 |
uint32_t output_rate; |
| 279 |
uint32_t target_rate; |
| 280 |
auto_array<float> input; |
| 281 |
auto_array<float> output; |
| 282 |
}; |
| 283 |
|
| 284 |
uint32_t |
| 285 |
fill_with_sine(float * buf, uint32_t rate, uint32_t channels, uint32_t frames, |
| 286 |
uint32_t initial_phase) |
| 287 |
{ |
| 288 |
uint32_t offset = 0; |
| 289 |
for (uint32_t i = 0; i < frames; i++) { |
| 290 |
float p = initial_phase++ / static_cast<float>(rate); |
| 291 |
for (uint32_t j = 0; j < channels; j++) { |
| 292 |
buf[offset++] = 0.5 * sin(440. * 2 * PI * p); |
| 293 |
} |
| 294 |
} |
| 295 |
return initial_phase; |
| 296 |
} |
| 297 |
|
| 298 |
long |
| 299 |
data_cb_resampler(cubeb_stream * /*stm*/, void * user_ptr, |
| 300 |
const void * input_buffer, void * output_buffer, |
| 301 |
long frame_count) |
| 302 |
{ |
| 303 |
osc_state * state = reinterpret_cast<osc_state *>(user_ptr); |
| 304 |
const float * in = reinterpret_cast<const float *>(input_buffer); |
| 305 |
float * out = reinterpret_cast<float *>(output_buffer); |
| 306 |
|
| 307 |
state->input.push(in, frame_count * state->input_channels); |
| 308 |
|
| 309 |
/* Check how much output frames we need to write */ |
| 310 |
uint32_t remaining = |
| 311 |
state->max_output_phase_index - state->output_phase_index; |
| 312 |
uint32_t to_write = std::min<uint32_t>(remaining, frame_count); |
| 313 |
state->output_phase_index = |
| 314 |
fill_with_sine(out, state->target_rate, state->output_channels, to_write, |
| 315 |
state->output_phase_index); |
| 316 |
|
| 317 |
return to_write; |
| 318 |
} |
| 319 |
|
| 320 |
template <typename T> |
| 321 |
bool |
| 322 |
array_fuzzy_equal(const auto_array<T> & lhs, const auto_array<T> & rhs, T epsi) |
| 323 |
{ |
| 324 |
uint32_t len = std::min(lhs.length(), rhs.length()); |
| 325 |
|
| 326 |
for (uint32_t i = 0; i < len; i++) { |
| 327 |
if (fabs(lhs.at(i) - rhs.at(i)) > epsi) { |
| 328 |
std::cout << "not fuzzy equal at index: " << i << " lhs: " << lhs.at(i) |
| 329 |
<< " rhs: " << rhs.at(i) |
| 330 |
<< " delta: " << fabs(lhs.at(i) - rhs.at(i)) |
| 331 |
<< " epsilon: " << epsi << std::endl; |
| 332 |
return false; |
| 333 |
} |
| 334 |
} |
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
template <typename T> |
| 339 |
void |
| 340 |
test_resampler_duplex(uint32_t input_channels, uint32_t output_channels, |
| 341 |
uint32_t input_rate, uint32_t output_rate, |
| 342 |
uint32_t target_rate, float chunk_duration) |
| 343 |
{ |
| 344 |
cubeb_stream_params input_params; |
| 345 |
cubeb_stream_params output_params; |
| 346 |
osc_state state; |
| 347 |
|
| 348 |
input_params.format = output_params.format = cubeb_format<T>(); |
| 349 |
state.input_channels = input_params.channels = input_channels; |
| 350 |
state.output_channels = output_params.channels = output_channels; |
| 351 |
input_params.rate = input_rate; |
| 352 |
state.output_rate = output_params.rate = output_rate; |
| 353 |
state.target_rate = target_rate; |
| 354 |
input_params.prefs = output_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 355 |
long got; |
| 356 |
|
| 357 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 358 |
(cubeb_stream *)nullptr, &input_params, &output_params, target_rate, |
| 359 |
data_cb_resampler, (void *)&state, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 360 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 361 |
|
| 362 |
long latency = cubeb_resampler_latency(resampler); |
| 363 |
|
| 364 |
const uint32_t duration_s = 2; |
| 365 |
int32_t duration_frames = duration_s * target_rate; |
| 366 |
uint32_t input_array_frame_count = |
| 367 |
ceil(chunk_duration * input_rate / 1000) + |
| 368 |
ceilf(static_cast<float>(input_rate) / target_rate) * 2; |
| 369 |
uint32_t output_array_frame_count = chunk_duration * output_rate / 1000; |
| 370 |
auto_array<float> input_buffer(input_channels * input_array_frame_count); |
| 371 |
auto_array<float> output_buffer(output_channels * output_array_frame_count); |
| 372 |
auto_array<float> expected_resampled_input(input_channels * duration_frames); |
| 373 |
auto_array<float> expected_resampled_output(output_channels * output_rate * |
| 374 |
duration_s); |
| 375 |
|
| 376 |
state.max_output_phase_index = duration_s * target_rate; |
| 377 |
|
| 378 |
expected_resampled_input.push_silence(input_channels * duration_frames); |
| 379 |
expected_resampled_output.push_silence(output_channels * output_rate * |
| 380 |
duration_s); |
| 381 |
|
| 382 |
/* expected output is a 440Hz sine wave at 16kHz */ |
| 383 |
fill_with_sine(expected_resampled_input.data() + latency, target_rate, |
| 384 |
input_channels, duration_frames - latency, 0); |
| 385 |
/* expected output is a 440Hz sine wave at 32kHz */ |
| 386 |
fill_with_sine(expected_resampled_output.data() + latency, output_rate, |
| 387 |
output_channels, output_rate * duration_s - latency, 0); |
| 388 |
|
| 389 |
while (state.output_phase_index != state.max_output_phase_index) { |
| 390 |
uint32_t leftover_samples = input_buffer.length() * input_channels; |
| 391 |
input_buffer.reserve(input_array_frame_count); |
| 392 |
state.input_phase_index = fill_with_sine( |
| 393 |
input_buffer.data() + leftover_samples, input_rate, input_channels, |
| 394 |
input_array_frame_count - leftover_samples, state.input_phase_index); |
| 395 |
long input_consumed = input_array_frame_count; |
| 396 |
input_buffer.set_length(input_array_frame_count); |
| 397 |
|
| 398 |
got = cubeb_resampler_fill(resampler, input_buffer.data(), &input_consumed, |
| 399 |
output_buffer.data(), output_array_frame_count); |
| 400 |
|
| 401 |
/* handle leftover input */ |
| 402 |
if (input_array_frame_count != static_cast<uint32_t>(input_consumed)) { |
| 403 |
input_buffer.pop(nullptr, input_consumed * input_channels); |
| 404 |
} else { |
| 405 |
input_buffer.clear(); |
| 406 |
} |
| 407 |
|
| 408 |
state.output.push(output_buffer.data(), got * state.output_channels); |
| 409 |
} |
| 410 |
|
| 411 |
dump("input_expected.raw", expected_resampled_input.data(), |
| 412 |
expected_resampled_input.length()); |
| 413 |
dump("output_expected.raw", expected_resampled_output.data(), |
| 414 |
expected_resampled_output.length()); |
| 415 |
dump("input.raw", state.input.data(), state.input.length()); |
| 416 |
dump("output.raw", state.output.data(), state.output.length()); |
| 417 |
|
| 418 |
// This is disabled because the latency estimation in the resampler code is |
| 419 |
// slightly off so we can generate expected vectors. |
| 420 |
// See https://github.com/kinetiknz/cubeb/issues/93 |
| 421 |
// ASSERT_TRUE(array_fuzzy_equal(state.input, expected_resampled_input, |
| 422 |
// epsilon<T>(input_rate/target_rate))); |
| 423 |
// ASSERT_TRUE(array_fuzzy_equal(state.output, expected_resampled_output, |
| 424 |
// epsilon<T>(output_rate/target_rate))); |
| 425 |
|
| 426 |
cubeb_resampler_destroy(resampler); |
| 427 |
} |
| 428 |
|
| 429 |
#define array_size(x) (sizeof(x) / sizeof(x[0])) |
| 430 |
|
| 431 |
TEST(cubeb, resampler_one_way) |
| 432 |
{ |
| 433 |
/* Test one way resamplers */ |
| 434 |
for (uint32_t channels = 1; channels <= max_channels; channels++) { |
| 435 |
for (uint32_t source_rate = 0; source_rate < array_size(sample_rates); |
| 436 |
source_rate++) { |
| 437 |
for (uint32_t dest_rate = 0; dest_rate < array_size(sample_rates); |
| 438 |
dest_rate++) { |
| 439 |
for (uint32_t chunk_duration = min_chunks; chunk_duration < max_chunks; |
| 440 |
chunk_duration += chunk_increment) { |
| 441 |
fprintf(stderr, |
| 442 |
"one_way: channels: %d, source_rate: %d, dest_rate: %d, " |
| 443 |
"chunk_duration: %d\n", |
| 444 |
channels, sample_rates[source_rate], sample_rates[dest_rate], |
| 445 |
chunk_duration); |
| 446 |
test_resampler_one_way<float>(channels, sample_rates[source_rate], |
| 447 |
sample_rates[dest_rate], |
| 448 |
chunk_duration); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
TEST(cubeb, DISABLED_resampler_duplex) |
| 456 |
{ |
| 457 |
for (uint32_t input_channels = 1; input_channels <= max_channels; |
| 458 |
input_channels++) { |
| 459 |
for (uint32_t output_channels = 1; output_channels <= max_channels; |
| 460 |
output_channels++) { |
| 461 |
for (uint32_t source_rate_input = 0; |
| 462 |
source_rate_input < array_size(sample_rates); source_rate_input++) { |
| 463 |
for (uint32_t source_rate_output = 0; |
| 464 |
source_rate_output < array_size(sample_rates); |
| 465 |
source_rate_output++) { |
| 466 |
for (uint32_t dest_rate = 0; dest_rate < array_size(sample_rates); |
| 467 |
dest_rate++) { |
| 468 |
for (uint32_t chunk_duration = min_chunks; |
| 469 |
chunk_duration < max_chunks; |
| 470 |
chunk_duration += chunk_increment) { |
| 471 |
fprintf(stderr, |
| 472 |
"input channels:%d output_channels:%d input_rate:%d " |
| 473 |
"output_rate:%d target_rate:%d chunk_ms:%d\n", |
| 474 |
input_channels, output_channels, |
| 475 |
sample_rates[source_rate_input], |
| 476 |
sample_rates[source_rate_output], sample_rates[dest_rate], |
| 477 |
chunk_duration); |
| 478 |
test_resampler_duplex<float>(input_channels, output_channels, |
| 479 |
sample_rates[source_rate_input], |
| 480 |
sample_rates[source_rate_output], |
| 481 |
sample_rates[dest_rate], |
| 482 |
chunk_duration); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
TEST(cubeb, resampler_delay_line) |
| 492 |
{ |
| 493 |
for (uint32_t channel = 1; channel <= 2; channel++) { |
| 494 |
for (uint32_t delay_frames = 4; delay_frames <= 40; |
| 495 |
delay_frames += chunk_increment) { |
| 496 |
for (uint32_t chunk_size = 10; chunk_size <= 30; chunk_size++) { |
| 497 |
fprintf(stderr, "channel: %d, delay_frames: %d, chunk_size: %d\n", |
| 498 |
channel, delay_frames, chunk_size); |
| 499 |
test_delay_lines(delay_frames, channel, chunk_size); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
long |
| 506 |
test_output_only_noop_data_cb(cubeb_stream * /*stm*/, void * /*user_ptr*/, |
| 507 |
const void * input_buffer, void * output_buffer, |
| 508 |
long frame_count) |
| 509 |
{ |
| 510 |
EXPECT_TRUE(output_buffer); |
| 511 |
EXPECT_TRUE(!input_buffer); |
| 512 |
return frame_count; |
| 513 |
} |
| 514 |
|
| 515 |
TEST(cubeb, resampler_output_only_noop) |
| 516 |
{ |
| 517 |
cubeb_stream_params output_params; |
| 518 |
int target_rate; |
| 519 |
|
| 520 |
output_params.rate = 44100; |
| 521 |
output_params.channels = 1; |
| 522 |
output_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 523 |
target_rate = output_params.rate; |
| 524 |
|
| 525 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 526 |
(cubeb_stream *)nullptr, nullptr, &output_params, target_rate, |
| 527 |
test_output_only_noop_data_cb, nullptr, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 528 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 529 |
const long out_frames = 128; |
| 530 |
float out_buffer[out_frames]; |
| 531 |
long got; |
| 532 |
|
| 533 |
got = |
| 534 |
cubeb_resampler_fill(resampler, nullptr, nullptr, out_buffer, out_frames); |
| 535 |
|
| 536 |
ASSERT_EQ(got, out_frames); |
| 537 |
|
| 538 |
cubeb_resampler_destroy(resampler); |
| 539 |
} |
| 540 |
|
| 541 |
long |
| 542 |
test_drain_data_cb(cubeb_stream * /*stm*/, void * user_ptr, |
| 543 |
const void * input_buffer, void * output_buffer, |
| 544 |
long frame_count) |
| 545 |
{ |
| 546 |
EXPECT_TRUE(output_buffer); |
| 547 |
EXPECT_TRUE(!input_buffer); |
| 548 |
auto cb_count = static_cast<int *>(user_ptr); |
| 549 |
(*cb_count)++; |
| 550 |
return frame_count - 1; |
| 551 |
} |
| 552 |
|
| 553 |
TEST(cubeb, resampler_drain) |
| 554 |
{ |
| 555 |
cubeb_stream_params output_params; |
| 556 |
int target_rate; |
| 557 |
|
| 558 |
output_params.rate = 44100; |
| 559 |
output_params.channels = 1; |
| 560 |
output_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 561 |
target_rate = 48000; |
| 562 |
int cb_count = 0; |
| 563 |
|
| 564 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 565 |
(cubeb_stream *)nullptr, nullptr, &output_params, target_rate, |
| 566 |
test_drain_data_cb, &cb_count, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 567 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 568 |
|
| 569 |
const long out_frames = 128; |
| 570 |
float out_buffer[out_frames]; |
| 571 |
long got; |
| 572 |
|
| 573 |
do { |
| 574 |
got = cubeb_resampler_fill(resampler, nullptr, nullptr, out_buffer, |
| 575 |
out_frames); |
| 576 |
} while (got == out_frames); |
| 577 |
|
| 578 |
/* The callback should be called once but not again after returning < |
| 579 |
* frame_count. */ |
| 580 |
ASSERT_EQ(cb_count, 1); |
| 581 |
|
| 582 |
cubeb_resampler_destroy(resampler); |
| 583 |
} |
| 584 |
|
| 585 |
// gtest does not support using ASSERT_EQ and friend in a function that returns |
| 586 |
// a value. |
| 587 |
void |
| 588 |
check_output(const void * input_buffer, void * output_buffer, long frame_count) |
| 589 |
{ |
| 590 |
ASSERT_EQ(input_buffer, nullptr); |
| 591 |
ASSERT_EQ(frame_count, 256); |
| 592 |
ASSERT_TRUE(!!output_buffer); |
| 593 |
} |
| 594 |
|
| 595 |
long |
| 596 |
cb_passthrough_resampler_output(cubeb_stream * /*stm*/, void * /*user_ptr*/, |
| 597 |
const void * input_buffer, void * output_buffer, |
| 598 |
long frame_count) |
| 599 |
{ |
| 600 |
check_output(input_buffer, output_buffer, frame_count); |
| 601 |
return frame_count; |
| 602 |
} |
| 603 |
|
| 604 |
TEST(cubeb, resampler_passthrough_output_only) |
| 605 |
{ |
| 606 |
// Test that the passthrough resampler works when there is only an output |
| 607 |
// stream. |
| 608 |
cubeb_stream_params output_params; |
| 609 |
|
| 610 |
const size_t output_channels = 2; |
| 611 |
output_params.channels = output_channels; |
| 612 |
output_params.rate = 44100; |
| 613 |
output_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 614 |
int target_rate = output_params.rate; |
| 615 |
|
| 616 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 617 |
(cubeb_stream *)nullptr, nullptr, &output_params, target_rate, |
| 618 |
cb_passthrough_resampler_output, nullptr, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 619 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 620 |
|
| 621 |
float output_buffer[output_channels * 256]; |
| 622 |
|
| 623 |
long got; |
| 624 |
for (uint32_t i = 0; i < 30; i++) { |
| 625 |
got = cubeb_resampler_fill(resampler, nullptr, nullptr, output_buffer, 256); |
| 626 |
ASSERT_EQ(got, 256); |
| 627 |
} |
| 628 |
|
| 629 |
cubeb_resampler_destroy(resampler); |
| 630 |
} |
| 631 |
|
| 632 |
// gtest does not support using ASSERT_EQ and friend in a function that returns |
| 633 |
// a value. |
| 634 |
void |
| 635 |
check_input(const void * input_buffer, void * output_buffer, long frame_count) |
| 636 |
{ |
| 637 |
ASSERT_EQ(output_buffer, nullptr); |
| 638 |
ASSERT_EQ(frame_count, 256); |
| 639 |
ASSERT_TRUE(!!input_buffer); |
| 640 |
} |
| 641 |
|
| 642 |
long |
| 643 |
cb_passthrough_resampler_input(cubeb_stream * /*stm*/, void * /*user_ptr*/, |
| 644 |
const void * input_buffer, void * output_buffer, |
| 645 |
long frame_count) |
| 646 |
{ |
| 647 |
check_input(input_buffer, output_buffer, frame_count); |
| 648 |
return frame_count; |
| 649 |
} |
| 650 |
|
| 651 |
TEST(cubeb, resampler_passthrough_input_only) |
| 652 |
{ |
| 653 |
// Test that the passthrough resampler works when there is only an output |
| 654 |
// stream. |
| 655 |
cubeb_stream_params input_params; |
| 656 |
|
| 657 |
const size_t input_channels = 2; |
| 658 |
input_params.channels = input_channels; |
| 659 |
input_params.rate = 44100; |
| 660 |
input_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 661 |
int target_rate = input_params.rate; |
| 662 |
|
| 663 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 664 |
(cubeb_stream *)nullptr, &input_params, nullptr, target_rate, |
| 665 |
cb_passthrough_resampler_input, nullptr, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 666 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 667 |
|
| 668 |
float input_buffer[input_channels * 256]; |
| 669 |
|
| 670 |
long got; |
| 671 |
for (uint32_t i = 0; i < 30; i++) { |
| 672 |
long int frames = 256; |
| 673 |
got = cubeb_resampler_fill(resampler, input_buffer, &frames, nullptr, 0); |
| 674 |
ASSERT_EQ(got, 256); |
| 675 |
} |
| 676 |
|
| 677 |
cubeb_resampler_destroy(resampler); |
| 678 |
} |
| 679 |
|
| 680 |
template <typename T> |
| 681 |
long |
| 682 |
seq(T * array, int stride, long start, long count) |
| 683 |
{ |
| 684 |
uint32_t output_idx = 0; |
| 685 |
for (int i = 0; i < count; i++) { |
| 686 |
for (int j = 0; j < stride; j++) { |
| 687 |
array[output_idx + j] = static_cast<T>(start + i); |
| 688 |
} |
| 689 |
output_idx += stride; |
| 690 |
} |
| 691 |
return start + count; |
| 692 |
} |
| 693 |
|
| 694 |
template <typename T> |
| 695 |
void |
| 696 |
is_seq(T * array, int stride, long count, long expected_start) |
| 697 |
{ |
| 698 |
uint32_t output_index = 0; |
| 699 |
for (long i = 0; i < count; i++) { |
| 700 |
for (int j = 0; j < stride; j++) { |
| 701 |
ASSERT_EQ(array[output_index + j], expected_start + i); |
| 702 |
} |
| 703 |
output_index += stride; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
template <typename T> |
| 708 |
void |
| 709 |
is_not_seq(T * array, int stride, long count, long expected_start) |
| 710 |
{ |
| 711 |
uint32_t output_index = 0; |
| 712 |
for (long i = 0; i < count; i++) { |
| 713 |
for (int j = 0; j < stride; j++) { |
| 714 |
ASSERT_NE(array[output_index + j], expected_start + i); |
| 715 |
} |
| 716 |
output_index += stride; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
struct closure { |
| 721 |
int input_channel_count; |
| 722 |
}; |
| 723 |
|
| 724 |
// gtest does not support using ASSERT_EQ and friend in a function that returns |
| 725 |
// a value. |
| 726 |
template <typename T> |
| 727 |
void |
| 728 |
check_duplex(const T * input_buffer, T * output_buffer, long frame_count, |
| 729 |
int input_channel_count) |
| 730 |
{ |
| 731 |
ASSERT_EQ(frame_count, 256); |
| 732 |
// Silence scan-build warning. |
| 733 |
ASSERT_TRUE(!!output_buffer); |
| 734 |
assert(output_buffer); |
| 735 |
ASSERT_TRUE(!!input_buffer); |
| 736 |
assert(input_buffer); |
| 737 |
|
| 738 |
int output_index = 0; |
| 739 |
int input_index = 0; |
| 740 |
for (int i = 0; i < frame_count; i++) { |
| 741 |
// output is two channels, input one or two channels. |
| 742 |
if (input_channel_count == 1) { |
| 743 |
output_buffer[output_index] = output_buffer[output_index + 1] = |
| 744 |
input_buffer[i]; |
| 745 |
} else if (input_channel_count == 2) { |
| 746 |
output_buffer[output_index] = input_buffer[input_index]; |
| 747 |
output_buffer[output_index + 1] = input_buffer[input_index + 1]; |
| 748 |
} |
| 749 |
output_index += 2; |
| 750 |
input_index += input_channel_count; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
long |
| 755 |
cb_passthrough_resampler_duplex(cubeb_stream * /*stm*/, void * user_ptr, |
| 756 |
const void * input_buffer, void * output_buffer, |
| 757 |
long frame_count) |
| 758 |
{ |
| 759 |
closure * c = reinterpret_cast<closure *>(user_ptr); |
| 760 |
check_duplex<float>(static_cast<const float *>(input_buffer), |
| 761 |
static_cast<float *>(output_buffer), frame_count, |
| 762 |
c->input_channel_count); |
| 763 |
return frame_count; |
| 764 |
} |
| 765 |
|
| 766 |
TEST(cubeb, resampler_passthrough_duplex_callback_reordering) |
| 767 |
{ |
| 768 |
// Test that when pre-buffering on resampler creation, we can survive an input |
| 769 |
// callback being delayed. |
| 770 |
|
| 771 |
cubeb_stream_params input_params; |
| 772 |
cubeb_stream_params output_params; |
| 773 |
|
| 774 |
const int input_channels = 1; |
| 775 |
const int output_channels = 2; |
| 776 |
|
| 777 |
input_params.channels = input_channels; |
| 778 |
input_params.rate = 44100; |
| 779 |
input_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 780 |
|
| 781 |
output_params.channels = output_channels; |
| 782 |
output_params.rate = input_params.rate; |
| 783 |
output_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 784 |
|
| 785 |
int target_rate = input_params.rate; |
| 786 |
|
| 787 |
closure c; |
| 788 |
c.input_channel_count = input_channels; |
| 789 |
|
| 790 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 791 |
(cubeb_stream *)nullptr, &input_params, &output_params, target_rate, |
| 792 |
cb_passthrough_resampler_duplex, &c, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 793 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 794 |
|
| 795 |
const long BUF_BASE_SIZE = 256; |
| 796 |
float input_buffer_prebuffer[input_channels * BUF_BASE_SIZE * 2]; |
| 797 |
float input_buffer_glitch[input_channels * BUF_BASE_SIZE * 2]; |
| 798 |
float input_buffer_normal[input_channels * BUF_BASE_SIZE]; |
| 799 |
float output_buffer[output_channels * BUF_BASE_SIZE]; |
| 800 |
|
| 801 |
long seq_idx = 0; |
| 802 |
long output_seq_idx = 0; |
| 803 |
|
| 804 |
long prebuffer_frames = |
| 805 |
ARRAY_LENGTH(input_buffer_prebuffer) / input_params.channels; |
| 806 |
seq_idx = |
| 807 |
seq(input_buffer_prebuffer, input_channels, seq_idx, prebuffer_frames); |
| 808 |
|
| 809 |
long got = |
| 810 |
cubeb_resampler_fill(resampler, input_buffer_prebuffer, &prebuffer_frames, |
| 811 |
output_buffer, BUF_BASE_SIZE); |
| 812 |
|
| 813 |
output_seq_idx += BUF_BASE_SIZE; |
| 814 |
|
| 815 |
// prebuffer_frames will hold the frames used by the resampler. |
| 816 |
ASSERT_EQ(prebuffer_frames, BUF_BASE_SIZE); |
| 817 |
ASSERT_EQ(got, BUF_BASE_SIZE); |
| 818 |
|
| 819 |
for (uint32_t i = 0; i < 300; i++) { |
| 820 |
long int frames = BUF_BASE_SIZE; |
| 821 |
// Simulate that sometimes, we don't have the input callback on time |
| 822 |
if (i != 0 && (i % 100) == 0) { |
| 823 |
long zero = 0; |
| 824 |
got = |
| 825 |
cubeb_resampler_fill(resampler, input_buffer_normal /* unused here */, |
| 826 |
&zero, output_buffer, BUF_BASE_SIZE); |
| 827 |
is_seq(output_buffer, 2, BUF_BASE_SIZE, output_seq_idx); |
| 828 |
output_seq_idx += BUF_BASE_SIZE; |
| 829 |
} else if (i != 0 && (i % 100) == 1) { |
| 830 |
// if this is the case, the on the next iteration, we'll have twice the |
| 831 |
// amount of input frames |
| 832 |
seq_idx = |
| 833 |
seq(input_buffer_glitch, input_channels, seq_idx, BUF_BASE_SIZE * 2); |
| 834 |
frames = 2 * BUF_BASE_SIZE; |
| 835 |
got = cubeb_resampler_fill(resampler, input_buffer_glitch, &frames, |
| 836 |
output_buffer, BUF_BASE_SIZE); |
| 837 |
is_seq(output_buffer, 2, BUF_BASE_SIZE, output_seq_idx); |
| 838 |
output_seq_idx += BUF_BASE_SIZE; |
| 839 |
} else { |
| 840 |
// normal case |
| 841 |
seq_idx = |
| 842 |
seq(input_buffer_normal, input_channels, seq_idx, BUF_BASE_SIZE); |
| 843 |
long normal_input_frame_count = 256; |
| 844 |
got = cubeb_resampler_fill(resampler, input_buffer_normal, |
| 845 |
&normal_input_frame_count, output_buffer, |
| 846 |
BUF_BASE_SIZE); |
| 847 |
is_seq(output_buffer, 2, BUF_BASE_SIZE, output_seq_idx); |
| 848 |
output_seq_idx += BUF_BASE_SIZE; |
| 849 |
} |
| 850 |
ASSERT_EQ(got, BUF_BASE_SIZE); |
| 851 |
} |
| 852 |
|
| 853 |
cubeb_resampler_destroy(resampler); |
| 854 |
} |
| 855 |
|
| 856 |
// Artificially simulate output thread underruns, |
| 857 |
// by building up artificial delay in the input. |
| 858 |
// Check that the frame drop logic kicks in. |
| 859 |
TEST(cubeb, resampler_drift_drop_data) |
| 860 |
{ |
| 861 |
for (uint32_t input_channels = 1; input_channels < 3; input_channels++) { |
| 862 |
cubeb_stream_params input_params; |
| 863 |
cubeb_stream_params output_params; |
| 864 |
|
| 865 |
const int output_channels = 2; |
| 866 |
const int sample_rate = 44100; |
| 867 |
|
| 868 |
input_params.channels = input_channels; |
| 869 |
input_params.rate = sample_rate; |
| 870 |
input_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 871 |
|
| 872 |
output_params.channels = output_channels; |
| 873 |
output_params.rate = sample_rate; |
| 874 |
output_params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 875 |
|
| 876 |
int target_rate = input_params.rate; |
| 877 |
|
| 878 |
closure c; |
| 879 |
c.input_channel_count = input_channels; |
| 880 |
|
| 881 |
cubeb_resampler * resampler = cubeb_resampler_create( |
| 882 |
(cubeb_stream *)nullptr, &input_params, &output_params, target_rate, |
| 883 |
cb_passthrough_resampler_duplex, &c, CUBEB_RESAMPLER_QUALITY_VOIP, |
| 884 |
CUBEB_RESAMPLER_RECLOCK_NONE); |
| 885 |
|
| 886 |
const long BUF_BASE_SIZE = 256; |
| 887 |
|
| 888 |
// The factor by which the deadline is missed. This is intentionally |
| 889 |
// kind of large to trigger the frame drop quickly. In real life, multiple |
| 890 |
// smaller under-runs would accumulate. |
| 891 |
const long UNDERRUN_FACTOR = 10; |
| 892 |
// Number buffer used for pre-buffering, that some backends do. |
| 893 |
const long PREBUFFER_FACTOR = 2; |
| 894 |
|
| 895 |
std::vector<float> input_buffer_prebuffer(input_channels * BUF_BASE_SIZE * |
| 896 |
PREBUFFER_FACTOR); |
| 897 |
std::vector<float> input_buffer_glitch(input_channels * BUF_BASE_SIZE * |
| 898 |
UNDERRUN_FACTOR); |
| 899 |
std::vector<float> input_buffer_normal(input_channels * BUF_BASE_SIZE); |
| 900 |
std::vector<float> output_buffer(output_channels * BUF_BASE_SIZE); |
| 901 |
|
| 902 |
long seq_idx = 0; |
| 903 |
long output_seq_idx = 0; |
| 904 |
|
| 905 |
long prebuffer_frames = |
| 906 |
input_buffer_prebuffer.size() / input_params.channels; |
| 907 |
seq_idx = seq(input_buffer_prebuffer.data(), input_channels, seq_idx, |
| 908 |
prebuffer_frames); |
| 909 |
|
| 910 |
long got = cubeb_resampler_fill(resampler, input_buffer_prebuffer.data(), |
| 911 |
&prebuffer_frames, output_buffer.data(), |
| 912 |
BUF_BASE_SIZE); |
| 913 |
|
| 914 |
output_seq_idx += BUF_BASE_SIZE; |
| 915 |
|
| 916 |
// prebuffer_frames will hold the frames used by the resampler. |
| 917 |
ASSERT_EQ(prebuffer_frames, BUF_BASE_SIZE); |
| 918 |
ASSERT_EQ(got, BUF_BASE_SIZE); |
| 919 |
|
| 920 |
for (uint32_t i = 0; i < 300; i++) { |
| 921 |
long int frames = BUF_BASE_SIZE; |
| 922 |
if (i != 0 && (i % 100) == 1) { |
| 923 |
// Once in a while, the output thread misses its deadline. |
| 924 |
// The input thread still produces data, so it ends up accumulating. |
| 925 |
// Simulate this by providing a much bigger input buffer. Check that the |
| 926 |
// sequence is now unaligned, meaning we've dropped data to keep |
| 927 |
// everything in sync. |
| 928 |
seq_idx = seq(input_buffer_glitch.data(), input_channels, seq_idx, |
| 929 |
BUF_BASE_SIZE * UNDERRUN_FACTOR); |
| 930 |
frames = BUF_BASE_SIZE * UNDERRUN_FACTOR; |
| 931 |
got = |
| 932 |
cubeb_resampler_fill(resampler, input_buffer_glitch.data(), &frames, |
| 933 |
output_buffer.data(), BUF_BASE_SIZE); |
| 934 |
is_seq(output_buffer.data(), 2, BUF_BASE_SIZE, output_seq_idx); |
| 935 |
output_seq_idx += BUF_BASE_SIZE; |
| 936 |
} else if (i != 0 && (i % 100) == 2) { |
| 937 |
// On the next iteration, the sequence should be broken |
| 938 |
seq_idx = seq(input_buffer_normal.data(), input_channels, seq_idx, |
| 939 |
BUF_BASE_SIZE); |
| 940 |
long normal_input_frame_count = 256; |
| 941 |
got = cubeb_resampler_fill(resampler, input_buffer_normal.data(), |
| 942 |
&normal_input_frame_count, |
| 943 |
output_buffer.data(), BUF_BASE_SIZE); |
| 944 |
is_not_seq(output_buffer.data(), output_channels, BUF_BASE_SIZE, |
| 945 |
output_seq_idx); |
| 946 |
// Reclock so that we can use is_seq again. |
| 947 |
output_seq_idx = output_buffer[BUF_BASE_SIZE * output_channels - 1] + 1; |
| 948 |
} else { |
| 949 |
// normal case |
| 950 |
seq_idx = seq(input_buffer_normal.data(), input_channels, seq_idx, |
| 951 |
BUF_BASE_SIZE); |
| 952 |
long normal_input_frame_count = 256; |
| 953 |
got = cubeb_resampler_fill(resampler, input_buffer_normal.data(), |
| 954 |
&normal_input_frame_count, |
| 955 |
output_buffer.data(), BUF_BASE_SIZE); |
| 956 |
is_seq(output_buffer.data(), output_channels, BUF_BASE_SIZE, |
| 957 |
output_seq_idx); |
| 958 |
output_seq_idx += BUF_BASE_SIZE; |
| 959 |
} |
| 960 |
ASSERT_EQ(got, BUF_BASE_SIZE); |
| 961 |
} |
| 962 |
|
| 963 |
cubeb_resampler_destroy(resampler); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
static long |
| 968 |
passthrough_resampler_fill_eq_input(cubeb_stream * stream, void * user_ptr, |
| 969 |
void const * input_buffer, |
| 970 |
void * output_buffer, long nframes) |
| 971 |
{ |
| 972 |
// gtest does not support using ASSERT_EQ and friends in a |
| 973 |
// function that returns a value. |
| 974 |
[nframes, input_buffer]() { |
| 975 |
ASSERT_EQ(nframes, 32); |
| 976 |
const float * input = static_cast<const float *>(input_buffer); |
| 977 |
for (int i = 0; i < 64; ++i) { |
| 978 |
ASSERT_FLOAT_EQ(input[i], 0.01 * i); |
| 979 |
} |
| 980 |
}(); |
| 981 |
return nframes; |
| 982 |
} |
| 983 |
|
| 984 |
TEST(cubeb, passthrough_resampler_fill_eq_input) |
| 985 |
{ |
| 986 |
uint32_t channels = 2; |
| 987 |
uint32_t sample_rate = 44100; |
| 988 |
passthrough_resampler<float> resampler = |
| 989 |
passthrough_resampler<float>(nullptr, passthrough_resampler_fill_eq_input, |
| 990 |
nullptr, channels, sample_rate); |
| 991 |
|
| 992 |
long input_frame_count = 32; |
| 993 |
long output_frame_count = 32; |
| 994 |
float input[64] = {}; |
| 995 |
float output[64] = {}; |
| 996 |
for (uint32_t i = 0; i < input_frame_count * channels; ++i) { |
| 997 |
input[i] = 0.01 * i; |
| 998 |
} |
| 999 |
long got = |
| 1000 |
resampler.fill(input, &input_frame_count, output, output_frame_count); |
| 1001 |
ASSERT_EQ(got, output_frame_count); |
| 1002 |
// Input frames used must be equal to output frames. |
| 1003 |
ASSERT_EQ(input_frame_count, output_frame_count); |
| 1004 |
} |
| 1005 |
|
| 1006 |
static long |
| 1007 |
passthrough_resampler_fill_short_input(cubeb_stream * stream, void * user_ptr, |
| 1008 |
void const * input_buffer, |
| 1009 |
void * output_buffer, long nframes) |
| 1010 |
{ |
| 1011 |
// gtest does not support using ASSERT_EQ and friends in a |
| 1012 |
// function that returns a value. |
| 1013 |
[nframes, input_buffer]() { |
| 1014 |
ASSERT_EQ(nframes, 32); |
| 1015 |
const float * input = static_cast<const float *>(input_buffer); |
| 1016 |
// First part contains the input |
| 1017 |
for (int i = 0; i < 32; ++i) { |
| 1018 |
ASSERT_FLOAT_EQ(input[i], 0.01 * i); |
| 1019 |
} |
| 1020 |
// missing part contains silence |
| 1021 |
for (int i = 32; i < 64; ++i) { |
| 1022 |
ASSERT_FLOAT_EQ(input[i], 0.0); |
| 1023 |
} |
| 1024 |
}(); |
| 1025 |
return nframes; |
| 1026 |
} |
| 1027 |
|
| 1028 |
TEST(cubeb, passthrough_resampler_fill_short_input) |
| 1029 |
{ |
| 1030 |
uint32_t channels = 2; |
| 1031 |
uint32_t sample_rate = 44100; |
| 1032 |
passthrough_resampler<float> resampler = passthrough_resampler<float>( |
| 1033 |
nullptr, passthrough_resampler_fill_short_input, nullptr, channels, |
| 1034 |
sample_rate); |
| 1035 |
|
| 1036 |
long input_frame_count = 16; |
| 1037 |
long output_frame_count = 32; |
| 1038 |
float input[64] = {}; |
| 1039 |
float output[64] = {}; |
| 1040 |
for (uint32_t i = 0; i < input_frame_count * channels; ++i) { |
| 1041 |
input[i] = 0.01 * i; |
| 1042 |
} |
| 1043 |
long got = |
| 1044 |
resampler.fill(input, &input_frame_count, output, output_frame_count); |
| 1045 |
ASSERT_EQ(got, output_frame_count); |
| 1046 |
// Input frames used are less than the output frames due to glitch. |
| 1047 |
ASSERT_EQ(input_frame_count, output_frame_count - 16); |
| 1048 |
} |
| 1049 |
|
| 1050 |
static long |
| 1051 |
passthrough_resampler_fill_input_left(cubeb_stream * stream, void * user_ptr, |
| 1052 |
void const * input_buffer, |
| 1053 |
void * output_buffer, long nframes) |
| 1054 |
{ |
| 1055 |
// gtest does not support using ASSERT_EQ and friends in a |
| 1056 |
// function that returns a value. |
| 1057 |
int iteration = *static_cast<int *>(user_ptr); |
| 1058 |
if (iteration == 1) { |
| 1059 |
[nframes, input_buffer]() { |
| 1060 |
ASSERT_EQ(nframes, 32); |
| 1061 |
const float * input = static_cast<const float *>(input_buffer); |
| 1062 |
for (int i = 0; i < 64; ++i) { |
| 1063 |
ASSERT_FLOAT_EQ(input[i], 0.01 * i); |
| 1064 |
} |
| 1065 |
}(); |
| 1066 |
} else if (iteration == 2) { |
| 1067 |
[nframes, input_buffer]() { |
| 1068 |
ASSERT_EQ(nframes, 32); |
| 1069 |
const float * input = static_cast<const float *>(input_buffer); |
| 1070 |
for (int i = 0; i < 32; ++i) { |
| 1071 |
// First part contains the reamaining input samples from previous |
| 1072 |
// iteration (since they were more). |
| 1073 |
ASSERT_FLOAT_EQ(input[i], 0.01 * (i + 64)); |
| 1074 |
// next part contains the new buffer |
| 1075 |
ASSERT_FLOAT_EQ(input[i + 32], 0.01 * i); |
| 1076 |
} |
| 1077 |
}(); |
| 1078 |
} else if (iteration == 3) { |
| 1079 |
[nframes, input_buffer]() { |
| 1080 |
ASSERT_EQ(nframes, 32); |
| 1081 |
const float * input = static_cast<const float *>(input_buffer); |
| 1082 |
for (int i = 0; i < 32; ++i) { |
| 1083 |
// First part (16 frames) contains the reamaining input samples |
| 1084 |
// from previous iteration (since they were more). |
| 1085 |
ASSERT_FLOAT_EQ(input[i], 0.01 * (i + 32)); |
| 1086 |
} |
| 1087 |
for (int i = 0; i < 16; ++i) { |
| 1088 |
// next part (8 frames) contains the new input buffer. |
| 1089 |
ASSERT_FLOAT_EQ(input[i + 32], 0.01 * i); |
| 1090 |
// last part (8 frames) contains silence. |
| 1091 |
ASSERT_FLOAT_EQ(input[i + 32 + 16], 0.0); |
| 1092 |
} |
| 1093 |
}(); |
| 1094 |
} |
| 1095 |
return nframes; |
| 1096 |
} |
| 1097 |
|
| 1098 |
TEST(cubeb, passthrough_resampler_fill_input_left) |
| 1099 |
{ |
| 1100 |
const uint32_t channels = 2; |
| 1101 |
const uint32_t sample_rate = 44100; |
| 1102 |
int iteration = 0; |
| 1103 |
passthrough_resampler<float> resampler = passthrough_resampler<float>( |
| 1104 |
nullptr, passthrough_resampler_fill_input_left, &iteration, channels, |
| 1105 |
sample_rate); |
| 1106 |
|
| 1107 |
long input_frame_count = 48; // 32 + 16 |
| 1108 |
const long output_frame_count = 32; |
| 1109 |
float input[96] = {}; |
| 1110 |
float output[64] = {}; |
| 1111 |
for (uint32_t i = 0; i < input_frame_count * channels; ++i) { |
| 1112 |
input[i] = 0.01 * i; |
| 1113 |
} |
| 1114 |
|
| 1115 |
// 1st iteration, add the extra input. |
| 1116 |
iteration = 1; |
| 1117 |
long got = |
| 1118 |
resampler.fill(input, &input_frame_count, output, output_frame_count); |
| 1119 |
ASSERT_EQ(got, output_frame_count); |
| 1120 |
// Input frames used must be equal to output frames. |
| 1121 |
ASSERT_EQ(input_frame_count, output_frame_count); |
| 1122 |
|
| 1123 |
// 2st iteration, use the extra input from previous iteration, |
| 1124 |
// 16 frames are remaining in the input buffer. |
| 1125 |
input_frame_count = 32; // we need 16 input frames but we get more; |
| 1126 |
iteration = 2; |
| 1127 |
got = resampler.fill(input, &input_frame_count, output, output_frame_count); |
| 1128 |
ASSERT_EQ(got, output_frame_count); |
| 1129 |
// Input frames used must be equal to output frames. |
| 1130 |
ASSERT_EQ(input_frame_count, output_frame_count); |
| 1131 |
|
| 1132 |
// 3rd iteration, use the extra input from previous iteration. |
| 1133 |
// 16 frames are remaining in the input buffer. |
| 1134 |
input_frame_count = 16 - 8; // We need 16 more input frames but we only get 8. |
| 1135 |
iteration = 3; |
| 1136 |
got = resampler.fill(input, &input_frame_count, output, output_frame_count); |
| 1137 |
ASSERT_EQ(got, output_frame_count); |
| 1138 |
// Input frames used are less than the output frames due to glitch. |
| 1139 |
ASSERT_EQ(input_frame_count, output_frame_count - 8); |
| 1140 |
} |
| 1141 |
|
| 1142 |
TEST(cubeb, individual_methods) |
| 1143 |
{ |
| 1144 |
const uint32_t channels = 2; |
| 1145 |
const uint32_t sample_rate = 44100; |
| 1146 |
const uint32_t frames = 256; |
| 1147 |
|
| 1148 |
delay_line<float> dl(10, channels, sample_rate); |
| 1149 |
uint32_t frames_needed1 = dl.input_needed_for_output(0); |
| 1150 |
ASSERT_EQ(frames_needed1, 0u); |
| 1151 |
|
| 1152 |
cubeb_resampler_speex_one_way<float> one_way( |
| 1153 |
channels, sample_rate, sample_rate, CUBEB_RESAMPLER_QUALITY_DEFAULT); |
| 1154 |
float buffer[channels * frames] = {0.0}; |
| 1155 |
// Add all frames in the resampler's internal buffer. |
| 1156 |
one_way.input(buffer, frames); |
| 1157 |
// Ask for less than the existing frames, this would create a uint overlflow |
| 1158 |
// without the fix. |
| 1159 |
uint32_t frames_needed2 = one_way.input_needed_for_output(0); |
| 1160 |
ASSERT_EQ(frames_needed2, 0u); |
| 1161 |
} |
| 1162 |
|
| 1163 |
#undef NOMINMAX |
| 1164 |
#undef DUMP_ARRAYS |