test_pcm_volume.cxx 3.21 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "test_pcm_all.hxx"
22
#include "pcm/Volume.hxx"
23 24 25
#include "pcm/Traits.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
26
#include "test_pcm_util.hxx"
27

28 29
#include <algorithm>

30 31
#include <string.h>

32 33 34 35
template<SampleFormat F, class Traits=SampleTraits<F>,
	 typename G=RandomInt<typename Traits::value_type>>
static void
TestVolume(G g=G())
36
{
37 38 39 40
	typedef typename Traits::value_type value_type;

	PcmVolume pv;
	CPPUNIT_ASSERT(pv.Open(F, IgnoreError()));
41

42
	constexpr size_t N = 509;
43 44 45
	static value_type zero[N];
	const auto _src = TestDataBuffer<value_type, N>(g);
	const ConstBuffer<void> src(_src, sizeof(_src));
46

47 48 49 50
	pv.SetVolume(0);
	auto dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero)));
51

52 53 54 55
	pv.SetVolume(PCM_VOLUME_1);
	dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size));
56

57 58 59
	pv.SetVolume(PCM_VOLUME_1 / 2);
	dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
60

61
	const auto _dest = ConstBuffer<value_type>::FromVoid(dest);
62
	for (unsigned i = 0; i < N; ++i) {
63
		const auto expected = (_src[i] + 1) / 2;
64 65
		CPPUNIT_ASSERT(_dest[i] >= expected - 4);
		CPPUNIT_ASSERT(_dest[i] <= expected + 4);
66
	}
67 68

	pv.Close();
69 70 71
}

void
72
PcmVolumeTest::TestVolume8()
73
{
74 75
	TestVolume<SampleFormat::S8>();
}
76

77 78 79 80
void
PcmVolumeTest::TestVolume16()
{
	TestVolume<SampleFormat::S16>();
81 82 83
}

void
84
PcmVolumeTest::TestVolume24()
85
{
86
	TestVolume<SampleFormat::S24_P32>(RandomInt24());
87 88 89
}

void
90
PcmVolumeTest::TestVolume32()
91
{
92
	TestVolume<SampleFormat::S32>();
93 94 95
}

void
96
PcmVolumeTest::TestVolumeFloat()
97
{
98 99
	PcmVolume pv;
	CPPUNIT_ASSERT(pv.Open(SampleFormat::FLOAT, IgnoreError()));
100

101
	constexpr size_t N = 509;
102 103 104
	static float zero[N];
	const auto _src = TestDataBuffer<float, N>(RandomFloat());
	const ConstBuffer<void> src(_src, sizeof(_src));
105

106 107 108 109
	pv.SetVolume(0);
	auto dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero)));
110

111 112 113 114
	pv.SetVolume(PCM_VOLUME_1);
	dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size));
115

116 117 118
	pv.SetVolume(PCM_VOLUME_1 / 2);
	dest = pv.Apply(src);
	CPPUNIT_ASSERT_EQUAL(src.size, dest.size);
119

120
	const auto _dest = ConstBuffer<float>::FromVoid(dest);
121
	for (unsigned i = 0; i < N; ++i)
122
		CPPUNIT_ASSERT_DOUBLES_EQUAL(_src[i] / 2, _dest[i], 1);
123 124

	pv.Close();
125
}