2013-02-01 15:52:03 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2013-02-01 15:52:03 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
}
|