PcmConvert.cxx 3.94 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "AudioFormat.hxx"
24
#include "util/ConstBuffer.hxx"
25 26
#include "util/Error.hxx"
#include "util/Domain.hxx"
27
#include "util/ConstBuffer.hxx"
28 29 30

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

32 33
const Domain pcm_convert_domain("pcm_convert");

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

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

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

bool
PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format,
56
		 Error &error)
57 58 59 60 61 62 63 64 65
{
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
	assert(_src_format.IsValid());
	assert(_dest_format.IsValid());

	src_format = _src_format;
	dest_format = _dest_format;

66 67 68 69
	AudioFormat format = src_format;
	if (format.format == SampleFormat::DSD)
		format.format = SampleFormat::FLOAT;

70 71 72 73 74 75 76 77 78 79 80 81 82 83
	enable_resampler = format.sample_rate != dest_format.sample_rate;
	if (enable_resampler) {
		if (!resampler.Open(format, dest_format.sample_rate, error))
			return false;

		format.format = resampler.GetOutputSampleFormat();
		format.sample_rate = dest_format.sample_rate;
	}

	enable_format = format.format != dest_format.format;
	if (enable_format &&
	    !format_converter.Open(format.format, dest_format.format, error)) {
		if (enable_resampler)
			resampler.Close();
84
		return false;
85 86
	}

87 88
	format.format = dest_format.format;

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

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

Max Kellermann's avatar
Max Kellermann committed
113
	dsd.Reset();
114

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

121
const void *
122
PcmConvert::Convert(const void *src, size_t src_size,
123
		    size_t *dest_size_r,
124
		    Error &error)
125
{
126
	ConstBuffer<void> buffer(src, src_size);
127
	AudioFormat format = src_format;
128

129
	if (format.format == SampleFormat::DSD) {
130
		auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
131
		auto d = dsd.ToFloat(format.channels,
132 133
				     false, s);
		if (d.IsNull()) {
134 135
			error.Set(pcm_convert_domain,
				  "DSD to PCM conversion failed");
136
			return nullptr;
137 138
		}

139
		buffer = d.ToVoid();
140
		format.format = SampleFormat::FLOAT;
141 142
	}

143 144
	if (enable_resampler) {
		buffer = resampler.Resample(buffer, error);
145 146 147
		if (buffer.IsNull())
			return nullptr;

148 149
		format.format = resampler.GetOutputSampleFormat();
		format.sample_rate = dest_format.sample_rate;
150 151
	}

152 153
	if (enable_format) {
		buffer = format_converter.Convert(buffer, error);
154 155 156
		if (buffer.IsNull())
			return nullptr;

157
		format.format = dest_format.format;
158 159
	}

160 161
	if (enable_channels) {
		buffer = channels_converter.Convert(buffer, error);
162 163
		if (buffer.IsNull())
			return nullptr;
164

165
		format.channels = dest_format.channels;
166
	}
167 168 169

	*dest_size_r = buffer.size;
	return buffer.data;
170
}