test_pcm_format.cxx 3.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
Max Kellermann's avatar
Max Kellermann committed
21
#include "pcm/PcmFormat.hxx"
22 23
#include "pcm/Dither.hxx"
#include "pcm/Buffer.hxx"
24
#include "pcm/SampleFormat.hxx"
25

26 27 28
#include <gtest/gtest.h>

TEST(PcmTest, Format8To16)
29
{
30
	constexpr size_t N = 509;
31 32
	const auto src = TestDataBuffer<int8_t, N>();

33
	PcmBuffer buffer;
34 35

	PcmDither dither;
36
	auto d = pcm_convert_to_16(buffer, dither, SampleFormat::S8, src);
37
	EXPECT_EQ(N, d.size);
38 39

	for (size_t i = 0; i < N; ++i)
40
		EXPECT_EQ(int(src[i]), d[i] >> 8);
41 42
}

43
TEST(PcmTest, Format16To24)
44
{
45
	constexpr size_t N = 509;
46 47
	const auto src = TestDataBuffer<int16_t, N>();

48
	PcmBuffer buffer;
49

50
	auto d = pcm_convert_to_24(buffer, SampleFormat::S16, src);
51
	EXPECT_EQ(N, d.size);
52 53

	for (size_t i = 0; i < N; ++i)
54
		EXPECT_EQ(int(src[i]), d[i] >> 8);
55 56
}

57
TEST(PcmTest, Format16To32)
58
{
59
	constexpr size_t N = 509;
60 61
	const auto src = TestDataBuffer<int16_t, N>();

62
	PcmBuffer buffer;
63

64
	auto d = pcm_convert_to_32(buffer, SampleFormat::S16, src);
65
	EXPECT_EQ(N, d.size);
66 67

	for (size_t i = 0; i < N; ++i)
68
		EXPECT_EQ(int(src[i]), d[i] >> 16);
69 70
}

71
TEST(PcmTest, FormatFloat16)
72
{
73
	constexpr size_t N = 509;
74 75
	const auto src = TestDataBuffer<int16_t, N>();

76
	PcmBuffer buffer1, buffer2;
77

78
	auto f = pcm_convert_to_float(buffer1, SampleFormat::S16, src);
79
	EXPECT_EQ(N, f.size);
80

81
	for (size_t i = 0; i != f.size; ++i) {
82 83
		EXPECT_GE(f[i], -1.);
		EXPECT_LE(f[i], 1.);
84 85 86 87
	}

	PcmDither dither;

88
	auto d = pcm_convert_to_16(buffer2, dither,
89
				   SampleFormat::FLOAT,
90
				   f.ToVoid());
91
	EXPECT_EQ(N, d.size);
92 93

	for (size_t i = 0; i < N; ++i)
94
		EXPECT_EQ(src[i], d[i]);
95 96

	/* check if clamping works */
Max Kellermann's avatar
Max Kellermann committed
97
	auto *writable = const_cast<float *>(f.data);
98 99 100 101 102 103 104 105
	*writable++ = 1.01;
	*writable++ = 10;
	*writable++ = -1.01;
	*writable++ = -10;

	d = pcm_convert_to_16(buffer2, dither,
			      SampleFormat::FLOAT,
			      f.ToVoid());
106
	EXPECT_EQ(N, d.size);
107

108 109 110 111
	EXPECT_EQ(32767, int(d[0]));
	EXPECT_EQ(32767, int(d[1]));
	EXPECT_EQ(-32768, int(d[2]));
	EXPECT_EQ(-32768, int(d[3]));
112 113

	for (size_t i = 4; i < N; ++i)
114
		EXPECT_EQ(src[i], d[i]);
115
}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

TEST(PcmTest, FormatFloat32)
{
	constexpr size_t N = 509;
	const auto src = TestDataBuffer<int32_t, N>();

	PcmBuffer buffer1, buffer2;

	auto f = pcm_convert_to_float(buffer1, SampleFormat::S32, src);
	EXPECT_EQ(N, f.size);

	for (size_t i = 0; i != f.size; ++i) {
		EXPECT_GE(f[i], -1.);
		EXPECT_LE(f[i], 1.);
	}

	auto d = pcm_convert_to_32(buffer2,
				   SampleFormat::FLOAT,
				   f.ToVoid());
	EXPECT_EQ(N, d.size);

	constexpr int error = 64;

	for (size_t i = 0; i < N; ++i)
		EXPECT_NEAR(src[i], d[i], error);

	/* check if clamping works */
Max Kellermann's avatar
Max Kellermann committed
143
	auto *writable = const_cast<float *>(f.data);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	*writable++ = 1.01;
	*writable++ = 10;
	*writable++ = -1.01;
	*writable++ = -10;

	d = pcm_convert_to_32(buffer2,
			      SampleFormat::FLOAT,
			      f.ToVoid());
	EXPECT_EQ(N, d.size);

	EXPECT_EQ(2147483647, int(d[0]));
	EXPECT_EQ(2147483647, int(d[1]));
	EXPECT_EQ(-2147483648, int(d[2]));
	EXPECT_EQ(-2147483648, int(d[3]));

	for (size_t i = 4; i < N; ++i)
		EXPECT_NEAR(src[i], d[i], error);
}