From 8f7e1920ad6fe6bf783d0f9a8c3b1705c6b7f863 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Thu, 23 Jan 2025 16:35:31 +0100 Subject: [PATCH] Add systematic integration test for cubeb_resampler, with typical block sizes and sample-rates This might be a bit much, runtime is 25s debug ASAN, 6s opt, but should be thorough. --- test/test_resampler.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/test_resampler.cpp b/test/test_resampler.cpp index adbcfd8c..ba5e4333 100644 --- a/test/test_resampler.cpp +++ b/test/test_resampler.cpp @@ -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" @@ -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(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 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