PcmConvert.cxx 3.53 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "Domain.hxx"
23
#include "ConfiguredResampler.hxx"
24
#include "AudioFormat.hxx"
25
#include "util/Error.hxx"
26
#include "util/ConstBuffer.hxx"
27 28

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

30 31 32
bool
pcm_convert_global_init(Error &error)
{
33
	return pcm_resampler_global_init(error);
34 35
}

36
PcmConvert::PcmConvert()
37
{
38 39 40 41
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
42 43
}

44
PcmConvert::~PcmConvert()
45
{
46 47 48 49 50
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
}

bool
51
PcmConvert::Open(const AudioFormat _src_format, const AudioFormat _dest_format,
52
		 Error &error)
53 54 55 56 57 58
{
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
	assert(_src_format.IsValid());
	assert(_dest_format.IsValid());

59
	AudioFormat format = _src_format;
60 61 62
	if (format.format == SampleFormat::DSD)
		format.format = SampleFormat::FLOAT;

63
	enable_resampler = format.sample_rate != _dest_format.sample_rate;
64
	if (enable_resampler) {
65
		if (!resampler.Open(format, _dest_format.sample_rate, error))
66 67 68
			return false;

		format.format = resampler.GetOutputSampleFormat();
69
		format.sample_rate = _dest_format.sample_rate;
70 71
	}

72
	enable_format = format.format != _dest_format.format;
73
	if (enable_format &&
74 75
	    !format_converter.Open(format.format, _dest_format.format,
				   error)) {
76 77
		if (enable_resampler)
			resampler.Close();
78
		return false;
79 80
	}

81
	format.format = _dest_format.format;
82

83
	enable_channels = format.channels != _dest_format.channels;
84
	if (enable_channels &&
85
	    !channels_converter.Open(format.format, format.channels,
86
				     _dest_format.channels, error)) {
87 88 89 90
		if (enable_format)
			format_converter.Close();
		if (enable_resampler)
			resampler.Close();
91 92 93
		return false;
	}

94 95 96
	src_format = _src_format;
	dest_format = _dest_format;

97
	return true;
98 99
}

100
void
101
PcmConvert::Close()
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 121
ConstBuffer<void>
PcmConvert::Convert(ConstBuffer<void> buffer, Error &error)
122
{
123
#ifdef ENABLE_DSD
124
	if (src_format.format == SampleFormat::DSD) {
125
		auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
126
		auto d = dsd.ToFloat(src_format.channels, s);
127
		if (d.IsNull()) {
128
			error.Set(pcm_domain,
129
				  "DSD to PCM conversion failed");
130
			return nullptr;
131 132
		}

133
		buffer = d.ToVoid();
134
	}
135
#endif
136

137 138
	if (enable_resampler) {
		buffer = resampler.Resample(buffer, error);
139 140 141 142
		if (buffer.IsNull())
			return nullptr;
	}

143 144
	if (enable_format) {
		buffer = format_converter.Convert(buffer, error);
145 146 147 148
		if (buffer.IsNull())
			return nullptr;
	}

149 150
	if (enable_channels) {
		buffer = channels_converter.Convert(buffer, error);
151 152
		if (buffer.IsNull())
			return nullptr;
153
	}
154

155
	return buffer;
156
}