PcmConvert.cxx 3.91 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 54
	assert(!src_format.IsValid());
	assert(!dest_format.IsValid());
}

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

	src_format = _src_format;
	dest_format = _dest_format;

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

69 70 71 72 73 74 75 76 77 78 79 80 81 82
	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();
83
		return false;
84 85
	}

86 87
	format.format = dest_format.format;

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

99
	return true;
100 101
}

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

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

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

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

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

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

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

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

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

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

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

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

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