PcmConvert.cxx 3.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "PcmConvert.hxx"
22
#include "ConfiguredResampler.hxx"
23
#include "util/ConstBuffer.hxx"
24 25

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

27 28
void
pcm_convert_global_init()
29
{
30
	pcm_resampler_global_init();
31 32
}

33
PcmConvert::PcmConvert()
34
{
35 36 37 38
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
39 40
}

41
PcmConvert::~PcmConvert()
42
{
43 44 45 46
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
}

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

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

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

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

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

79
	format.format = _dest_format.format;
80

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

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

99
void
100
PcmConvert::Close()
101
{
102
	if (enable_channels)
103
		channels_converter.Close();
104
	if (enable_format)
105
		format_converter.Close();
106 107
	if (enable_resampler)
		resampler.Close();
108

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

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

119 120 121 122 123 124 125 126 127 128 129
void
PcmConvert::Reset()
{
	if (enable_resampler)
		resampler.Reset();

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

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

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

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

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

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

153
	return buffer;
154
}