PcmConvert.cxx 3.67 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "PcmConvert.hxx"
21
#include "ConfiguredResampler.hxx"
22
#include "util/ConstBuffer.hxx"
23 24

#include <assert.h>
Warren Dukes's avatar
Warren Dukes committed
25

26
void
27
pcm_convert_global_init(const ConfigData &config)
28
{
29
	pcm_resampler_global_init(config);
30 31
}

Max Kellermann's avatar
Max Kellermann committed
32
PcmConvert::PcmConvert() noexcept
33
{
34 35 36 37
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
38 39
}

Max Kellermann's avatar
Max Kellermann committed
40
PcmConvert::~PcmConvert() noexcept
41
{
42 43 44 45
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
}

46 47
void
PcmConvert::Open(const AudioFormat _src_format, const AudioFormat _dest_format)
48 49 50 51 52 53
{
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
	assert(_src_format.IsValid());
	assert(_dest_format.IsValid());

54
	AudioFormat format = _src_format;
55 56 57
	if (format.format == SampleFormat::DSD)
		format.format = SampleFormat::FLOAT;

58
	enable_resampler = format.sample_rate != _dest_format.sample_rate;
59
	if (enable_resampler) {
60
		resampler.Open(format, _dest_format.sample_rate);
61 62

		format.format = resampler.GetOutputSampleFormat();
63
		format.sample_rate = _dest_format.sample_rate;
64 65
	}

66
	enable_format = format.format != _dest_format.format;
67 68 69 70 71 72 73 74 75
	if (enable_format) {
		try {
			format_converter.Open(format.format,
					      _dest_format.format);
		} catch (...) {
			if (enable_resampler)
				resampler.Close();
			throw;
		}
76 77
	}

78
	format.format = _dest_format.format;
79

80
	enable_channels = format.channels != _dest_format.channels;
81 82 83 84 85 86 87 88 89 90 91
	if (enable_channels) {
		try {
			channels_converter.Open(format.format, format.channels,
						_dest_format.channels);
		} catch (...) {
			if (enable_format)
				format_converter.Close();
			if (enable_resampler)
				resampler.Close();
			throw;
		}
92 93
	}

94 95
	src_format = _src_format;
	dest_format = _dest_format;
96 97
}

98
void
Max Kellermann's avatar
Max Kellermann committed
99
PcmConvert::Close() noexcept
100
{
101
	if (enable_channels)
102
		channels_converter.Close();
103
	if (enable_format)
104
		format_converter.Close();
105 106
	if (enable_resampler)
		resampler.Close();
107

108
#ifdef ENABLE_DSD
Max Kellermann's avatar
Max Kellermann committed
109
	dsd.Reset();
110
#endif
111

112 113 114 115
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
116 117
}

118
void
Max Kellermann's avatar
Max Kellermann committed
119
PcmConvert::Reset() noexcept
120 121 122 123 124 125 126 127 128
{
	if (enable_resampler)
		resampler.Reset();

#ifdef ENABLE_DSD
	dsd.Reset();
#endif
}

129
ConstBuffer<void>
130
PcmConvert::Convert(ConstBuffer<void> buffer)
131
{
132
#ifdef ENABLE_DSD
133
	if (src_format.format == SampleFormat::DSD) {
134
		auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
135
		auto d = dsd.ToFloat(src_format.channels, s);
136 137
		if (d.IsNull())
			throw std::runtime_error("DSD to PCM conversion failed");
138

139
		buffer = d.ToVoid();
140
	}
141
#endif
142

143 144
	if (enable_resampler)
		buffer = resampler.Resample(buffer);
145

146 147
	if (enable_format)
		buffer = format_converter.Convert(buffer);
148

149 150
	if (enable_channels)
		buffer = channels_converter.Convert(buffer);
151

152
	return buffer;
153
}
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

ConstBuffer<void>
PcmConvert::Flush()
{
	if (enable_resampler) {
		auto buffer = resampler.Flush();
		if (!buffer.IsNull()) {
			if (enable_format)
				buffer = format_converter.Convert(buffer);

			if (enable_channels)
				buffer = channels_converter.Convert(buffer);

			return buffer;
		}
	}

	return nullptr;
}