PcmDsd.cxx 1.79 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "PcmDsd.hxx"
21
#include "dsd2pcm/dsd2pcm.h"
22
#include "util/ConstBuffer.hxx"
Max Kellermann's avatar
Max Kellermann committed
23

Max Kellermann's avatar
Max Kellermann committed
24
#include <assert.h>
25

Max Kellermann's avatar
Max Kellermann committed
26
PcmDsd::PcmDsd() noexcept
27
{
28
	dsd2pcm.fill(nullptr);
29 30
}

Max Kellermann's avatar
Max Kellermann committed
31
PcmDsd::~PcmDsd() noexcept
32
{
33 34 35
	for (auto i : dsd2pcm)
		if (i != nullptr)
			dsd2pcm_destroy(i);
36 37 38
}

void
Max Kellermann's avatar
Max Kellermann committed
39
PcmDsd::Reset() noexcept
40
{
41 42 43
	for (auto i : dsd2pcm)
		if (i != nullptr)
			dsd2pcm_reset(i);
44 45
}

46
ConstBuffer<float>
Max Kellermann's avatar
Max Kellermann committed
47
PcmDsd::ToFloat(unsigned channels, ConstBuffer<uint8_t> src) noexcept
48
{
49
	assert(!src.IsNull());
50
	assert(!src.empty());
51
	assert(src.size % channels == 0);
52
	assert(channels <= dsd2pcm.max_size());
53

54 55
	const unsigned num_samples = src.size;
	const unsigned num_frames = src.size / channels;
56

57
	float *dest = buffer.GetT<float>(num_samples);
58 59

	for (unsigned c = 0; c < channels; ++c) {
Max Kellermann's avatar
Max Kellermann committed
60 61 62 63
		if (dsd2pcm[c] == nullptr) {
			dsd2pcm[c] = dsd2pcm_init();
			if (dsd2pcm[c] == nullptr)
				return nullptr;
64 65
		}

Max Kellermann's avatar
Max Kellermann committed
66
		dsd2pcm_translate(dsd2pcm[c], num_frames,
67
				  src.data + c, channels,
68
				  false, dest + c, channels);
69 70
	}

71
	return { dest, num_samples };
72
}