2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
#include "test_pcm_util.hxx"
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "pcm/Mix.hxx"
|
|
|
|
#include "pcm/Dither.hxx"
|
2013-02-01 15:52:03 +01:00
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2013-10-19 15:05:48 +02:00
|
|
|
template<typename T, SampleFormat format, typename G=RandomInt<T>>
|
2013-10-16 21:55:00 +02:00
|
|
|
static void
|
2013-02-01 15:52:03 +01:00
|
|
|
TestPcmMix(G g=G())
|
|
|
|
{
|
2014-03-15 10:49:50 +01:00
|
|
|
constexpr unsigned N = 509;
|
2013-02-01 15:52:03 +01:00
|
|
|
const auto src1 = TestDataBuffer<T, N>(g);
|
|
|
|
const auto src2 = TestDataBuffer<T, N>(g);
|
|
|
|
|
2013-12-28 17:42:03 +01:00
|
|
|
PcmDither dither;
|
|
|
|
|
2013-02-01 15:52:03 +01:00
|
|
|
/* portion1=1.0: result must be equal to src1 */
|
|
|
|
auto result = src1;
|
2013-12-28 17:42:03 +01:00
|
|
|
bool success = pcm_mix(dither,
|
|
|
|
result.begin(), src2.begin(), sizeof(result),
|
2013-02-01 15:52:03 +01:00
|
|
|
format, 1.0);
|
2018-10-16 19:01:13 +02:00
|
|
|
ASSERT_TRUE(success);
|
2013-12-28 17:42:03 +01:00
|
|
|
AssertEqualWithTolerance(result, src1, 3);
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
/* portion1=0.0: result must be equal to src2 */
|
|
|
|
result = src1;
|
2013-12-28 17:42:03 +01:00
|
|
|
success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result),
|
2013-02-01 15:52:03 +01:00
|
|
|
format, 0.0);
|
2018-10-16 19:01:13 +02:00
|
|
|
ASSERT_TRUE(success);
|
2013-12-28 17:42:03 +01:00
|
|
|
AssertEqualWithTolerance(result, src2, 3);
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
/* portion1=0.5 */
|
|
|
|
result = src1;
|
2013-12-28 17:42:03 +01:00
|
|
|
success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result),
|
2013-02-01 15:52:03 +01:00
|
|
|
format, 0.5);
|
2018-10-16 19:01:13 +02:00
|
|
|
ASSERT_TRUE(success);
|
2013-02-01 15:52:03 +01:00
|
|
|
|
|
|
|
auto expected = src1;
|
|
|
|
for (unsigned i = 0; i < N; ++i)
|
|
|
|
expected[i] = (int64_t(src1[i]) + int64_t(src2[i])) / 2;
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
for (unsigned i = 0; i < N; ++i)
|
|
|
|
EXPECT_NEAR(result[i], expected[i], 3);
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
TEST(PcmTest, Mix8)
|
2013-02-01 15:52:03 +01:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
TestPcmMix<int8_t, SampleFormat::S8>();
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
TEST(PcmTest, Mix16)
|
2013-02-01 15:52:03 +01:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
TestPcmMix<int16_t, SampleFormat::S16>();
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
TEST(PcmTest, Mix24)
|
2013-02-01 15:52:03 +01:00
|
|
|
{
|
2013-10-19 15:05:48 +02:00
|
|
|
TestPcmMix<int32_t, SampleFormat::S24_P32>(RandomInt24());
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 19:01:13 +02:00
|
|
|
TEST(PcmTest, Mix32)
|
2013-02-01 15:52:03 +01:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
TestPcmMix<int32_t, SampleFormat::S32>();
|
2013-02-01 15:52:03 +01:00
|
|
|
}
|