PcmDsd.cxx 2.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "PcmDsd.hxx"
22
#include "dsd2pcm/dsd2pcm.h"
23
#include "util/Macros.hxx"
24
#include "util/ConstBuffer.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26 27

#include <algorithm>

Max Kellermann's avatar
Max Kellermann committed
28
#include <assert.h>
29

Max Kellermann's avatar
Max Kellermann committed
30
PcmDsd::PcmDsd()
31
{
32
	std::fill_n(dsd2pcm, ARRAY_SIZE(dsd2pcm), nullptr);
33 34
}

Max Kellermann's avatar
Max Kellermann committed
35
PcmDsd::~PcmDsd()
36
{
37
	for (unsigned i = 0; i < ARRAY_SIZE(dsd2pcm); ++i)
Max Kellermann's avatar
Max Kellermann committed
38 39
		if (dsd2pcm[i] != nullptr)
			dsd2pcm_destroy(dsd2pcm[i]);
40 41 42
}

void
Max Kellermann's avatar
Max Kellermann committed
43
PcmDsd::Reset()
44
{
45
	for (unsigned i = 0; i < ARRAY_SIZE(dsd2pcm); ++i)
Max Kellermann's avatar
Max Kellermann committed
46 47
		if (dsd2pcm[i] != nullptr)
			dsd2pcm_reset(dsd2pcm[i]);
48 49
}

50
ConstBuffer<float>
Max Kellermann's avatar
Max Kellermann committed
51
PcmDsd::ToFloat(unsigned channels, bool lsbfirst,
52
		ConstBuffer<uint8_t> src)
53
{
54 55 56
	assert(!src.IsNull());
	assert(!src.IsEmpty());
	assert(src.size % channels == 0);
57
	assert(channels <= ARRAY_SIZE(dsd2pcm));
58

59 60
	const unsigned num_samples = src.size;
	const unsigned num_frames = src.size / channels;
61 62 63

	float *dest;
	const size_t dest_size = num_samples * sizeof(*dest);
64
	dest = (float *)buffer.Get(dest_size);
65 66

	for (unsigned c = 0; c < channels; ++c) {
Max Kellermann's avatar
Max Kellermann committed
67 68 69 70
		if (dsd2pcm[c] == nullptr) {
			dsd2pcm[c] = dsd2pcm_init();
			if (dsd2pcm[c] == nullptr)
				return nullptr;
71 72
		}

Max Kellermann's avatar
Max Kellermann committed
73
		dsd2pcm_translate(dsd2pcm[c], num_frames,
74
				  src.data + c, channels,
75 76 77
				  lsbfirst, dest + c, channels);
	}

78
	return { dest, num_samples };
79
}