PcmConvert.cxx 3.7 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
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 25
#include <stdexcept>

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

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

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

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

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

56
	AudioFormat format = _src_format;
57 58 59
	if (format.format == SampleFormat::DSD)
		format.format = SampleFormat::FLOAT;

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

		format.format = resampler.GetOutputSampleFormat();
65
		format.sample_rate = _dest_format.sample_rate;
66 67
	}

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

80
	format.format = _dest_format.format;
81

82
	enable_channels = format.channels != _dest_format.channels;
83 84 85 86 87 88 89 90 91 92 93
	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;
		}
94 95
	}

96 97
	src_format = _src_format;
	dest_format = _dest_format;
98 99
}

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

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

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

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

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

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

141
		buffer = d.ToVoid();
142
	}
143
#endif
144

145 146
	if (enable_resampler)
		buffer = resampler.Resample(buffer);
147

148 149
	if (enable_format)
		buffer = format_converter.Convert(buffer);
150

151 152
	if (enable_channels)
		buffer = channels_converter.Convert(buffer);
153

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

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;
}