FormatConverter.cxx 2.41 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 20 21 22
 * 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 "FormatConverter.hxx"
#include "PcmFormat.hxx"
#include "util/ConstBuffer.hxx"
23
#include "util/RuntimeError.hxx"
24 25 26

#include <assert.h>

27 28
void
PcmFormatConverter::Open(SampleFormat _src_format, SampleFormat _dest_format)
29 30 31 32
{
	assert(_src_format != SampleFormat::UNDEFINED);
	assert(_dest_format != SampleFormat::UNDEFINED);

33 34 35 36 37 38 39
	switch (_dest_format) {
	case SampleFormat::UNDEFINED:
		assert(false);
		gcc_unreachable();

	case SampleFormat::S8:
	case SampleFormat::DSD:
40 41 42
		throw FormatRuntimeError("PCM conversion from %s to %s is not implemented",
					 sample_format_to_string(_src_format),
					 sample_format_to_string(_dest_format));
43 44 45 46 47 48 49 50

	case SampleFormat::S16:
	case SampleFormat::S24_P32:
	case SampleFormat::S32:
	case SampleFormat::FLOAT:
		break;
	}

51 52 53 54 55
	src_format = _src_format;
	dest_format = _dest_format;
}

void
56
PcmFormatConverter::Close() noexcept
57 58 59 60 61 62 63 64
{
#ifndef NDEBUG
	src_format = SampleFormat::UNDEFINED;
	dest_format = SampleFormat::UNDEFINED;
#endif
}

ConstBuffer<void>
65
PcmFormatConverter::Convert(ConstBuffer<void> src) noexcept
66 67 68 69 70
{
	switch (dest_format) {
	case SampleFormat::UNDEFINED:
	case SampleFormat::S8:
	case SampleFormat::DSD:
71 72
		assert(false);
		gcc_unreachable();
73 74

	case SampleFormat::S16:
75 76 77
		return pcm_convert_to_16(buffer, dither,
					 src_format,
					 src).ToVoid();
78 79

	case SampleFormat::S24_P32:
80 81 82
		return pcm_convert_to_24(buffer,
					 src_format,
					 src).ToVoid();
83 84

	case SampleFormat::S32:
85 86 87
		return pcm_convert_to_32(buffer,
					 src_format,
					 src).ToVoid();
88 89

	case SampleFormat::FLOAT:
90 91 92
		return pcm_convert_to_float(buffer,
					    src_format,
					    src).ToVoid();
93 94
	}

95 96
	assert(false);
	gcc_unreachable();
97
}