Skip to content

Commit

Permalink
Add systematic integration test for cubeb_resampler, with typical blo…
Browse files Browse the repository at this point in the history
…ck sizes and sample-rates

This might be a bit much, runtime is 25s debug ASAN, 6s opt, but should
be thorough.
  • Loading branch information
padenot committed Jan 23, 2025
1 parent 2054244 commit 8f7e192
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/test_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/
#ifndef NOMINMAX
#define NOMINMAX
#include "cubeb/cubeb.h"
#include "cubeb_log.h"
#include "cubeb_resampler.h"
#endif // NOMINMAX
// #define ENABLE_NORMAL_LOG
// #define ENABLE_VERBOSE_LOG
#include "common.h"
#include "cubeb_resampler_internal.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -1160,5 +1165,61 @@ TEST(cubeb, individual_methods)
ASSERT_EQ(frames_needed2, 0u);
}

long
data_cb(cubeb_stream * stream, void * user_ptr, void const * input_buffer,
void * output_buffer, long nframes)
{
LOGV("%ld frames requested\n", nframes);
float * out = static_cast<float *>(output_buffer);
// never drain, fill with silence
for (int i = 0; i < nframes * 2; i++) {
out[i] = 0.0;
}
return nframes;
}

TEST(cubeb, resampler_typical_uses)
{
// Source and target sample-rates in Hz, typical values.
const int rates[] = {16000, 32000, 44100, 48000, 96000, 192000, 384000};
// Block size in frames, except the first element, that is in millisecond
// Power of two are typical on Windows WASAPI IAudioClient3, macOS,
// Linux Pipewire and Jack. 10ms is typical on Windows IAudioClient and
// IAudioClient2. 96, 192 are not uncommon on some Android devices.
const int block_sizes[] = {10, 96, 128, 192, 256, 512, 1024, 2048};
cubeb * ctx;
common_init(&ctx, "Cubeb resampler test");
for (int source_rate : rates) {
for (int target_rate : rates) {
for (int block_size : block_sizes) {
// special case: Windows/WASAPI works in blocks of 10ms
if (block_size == 10) {
block_size = target_rate / 100;
}
cubeb_stream_params out_params = {};
out_params.channels = 2;
out_params.rate = target_rate;
out_params.format = CUBEB_SAMPLE_FLOAT32NE;

cubeb_resampler * resampler = cubeb_resampler_create(
nullptr, nullptr, &out_params, source_rate, data_cb, nullptr,
CUBEB_RESAMPLER_QUALITY_DEFAULT, CUBEB_RESAMPLER_RECLOCK_NONE);
ASSERT_NE(resampler, nullptr);

std::vector<float> data(block_size * out_params.channels);
int i = 100;
while (i--) {
long got = cubeb_resampler_fill(resampler, nullptr, 0, data.data(),
block_size);
ASSERT_EQ(got, block_size);
}

cubeb_resampler_destroy(resampler);
}
}
}
cubeb_destroy(ctx);
}

#undef NOMINMAX
#undef DUMP_ARRAYS

0 comments on commit 8f7e192

Please sign in to comment.