You need to sign in or sign up before continuing.
PcmConvert.cxx 3.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/ConstBuffer.hxx"
26 27
#include "util/Error.hxx"
#include "util/Domain.hxx"
28
#include "util/ConstBuffer.hxx"
29 30 31

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

33 34 35
bool
pcm_convert_global_init(Error &error)
{
36
	return pcm_resampler_global_init(error);
37 38
}

39
PcmConvert::PcmConvert()
40
{
41 42 43 44
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
45 46
}

47
PcmConvert::~PcmConvert()
48
{
49 50 51 52 53
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
}

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

62
	AudioFormat format = _src_format;
63 64 65
	if (format.format == SampleFormat::DSD)
		format.format = SampleFormat::FLOAT;

66
	enable_resampler = format.sample_rate != _dest_format.sample_rate;
67
	if (enable_resampler) {
68
		if (!resampler.Open(format, _dest_format.sample_rate, error))
69 70 71
			return false;

		format.format = resampler.GetOutputSampleFormat();
72
		format.sample_rate = _dest_format.sample_rate;
73 74
	}

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

84
	format.format = _dest_format.format;
85

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

97 98 99
	src_format = _src_format;
	dest_format = _dest_format;

100
	return true;
101 102
}

103
void
104
PcmConvert::Close()
105
{
106
	if (enable_channels)
107
		channels_converter.Close();
108
	if (enable_format)
109
		format_converter.Close();
110 111
	if (enable_resampler)
		resampler.Close();
112

113
#ifdef ENABLE_DSD
Max Kellermann's avatar
Max Kellermann committed
114
	dsd.Reset();
115
#endif
116

117 118 119 120
#ifndef NDEBUG
	src_format.Clear();
	dest_format.Clear();
#endif
121 122
}

123 124
ConstBuffer<void>
PcmConvert::Convert(ConstBuffer<void> buffer, Error &error)
125
{
126
#ifdef ENABLE_DSD
127
	if (src_format.format == SampleFormat::DSD) {
128
		auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
129
		auto d = dsd.ToFloat(src_format.channels, s);
130
		if (d.IsNull()) {
131
			error.Set(pcm_domain,
132
				  "DSD to PCM conversion failed");
133
			return nullptr;
134 135
		}

136
		buffer = d.ToVoid();
137
	}
138
#endif
139

140 141
	if (enable_resampler) {
		buffer = resampler.Resample(buffer, error);
142 143 144 145
		if (buffer.IsNull())
			return nullptr;
	}

146 147
	if (enable_format) {
		buffer = format_converter.Convert(buffer, error);
148 149 150 151
		if (buffer.IsNull())
			return nullptr;
	}

152 153
	if (enable_channels) {
		buffer = channels_converter.Convert(buffer, error);
154 155
		if (buffer.IsNull())
			return nullptr;
156
	}
157

158
	return buffer;
159
}