mpd/src/pcm/PcmConvert.cxx

318 lines
7.7 KiB
C++
Raw Normal View History

/*
2013-10-30 23:37:06 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* 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.
*/
#include "config.h"
2013-01-30 23:40:56 +01:00
#include "PcmConvert.hxx"
2013-01-31 20:33:26 +01:00
#include "PcmChannels.hxx"
#include "PcmFormat.hxx"
2013-08-03 21:00:50 +02:00
#include "AudioFormat.hxx"
2013-11-29 22:06:14 +01:00
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include <assert.h>
#include <math.h>
const Domain pcm_convert_domain("pcm_convert");
bool
pcm_convert_global_init(Error &error)
{
return pcm_resample_global_init(error);
}
2013-01-30 23:40:56 +01:00
PcmConvert::PcmConvert()
{
#ifndef NDEBUG
src_format.Clear();
dest_format.Clear();
#endif
}
2013-01-30 23:40:56 +01:00
PcmConvert::~PcmConvert()
{
assert(!src_format.IsValid());
assert(!dest_format.IsValid());
}
bool
PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format,
gcc_unused Error &error)
{
assert(!src_format.IsValid());
assert(!dest_format.IsValid());
assert(_src_format.IsValid());
assert(_dest_format.IsValid());
src_format = _src_format;
dest_format = _dest_format;
return true;
}
void
PcmConvert::Close()
{
2013-07-29 07:56:40 +02:00
dsd.Reset();
2013-07-29 23:18:55 +02:00
resampler.Reset();
#ifndef NDEBUG
src_format.Clear();
dest_format.Clear();
#endif
}
inline ConstBuffer<int16_t>
PcmConvert::Convert16(ConstBuffer<void> src, AudioFormat format, Error &error)
{
const int16_t *buf;
size_t len;
2013-08-03 21:00:50 +02:00
assert(dest_format.format == SampleFormat::S16);
2013-07-29 08:10:10 +02:00
buf = pcm_convert_to_16(format_buffer, dither,
format.format,
src.data, src.size,
&len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %s to 16 bit is not implemented",
sample_format_to_string(format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
if (format.channels != dest_format.channels) {
2013-07-29 08:10:10 +02:00
buf = pcm_convert_channels_16(channels_buffer,
2013-08-03 21:00:50 +02:00
dest_format.channels,
format.channels,
buf, len, &len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %u to %u channels "
"is not implemented",
format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
if (format.sample_rate != dest_format.sample_rate) {
2013-08-03 21:00:50 +02:00
buf = resampler.Resample16(dest_format.channels,
format.sample_rate, buf, len,
2013-08-03 21:00:50 +02:00
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return nullptr;
}
return ConstBuffer<int16_t>::FromVoid({buf, len});
}
inline ConstBuffer<int32_t>
PcmConvert::Convert24(ConstBuffer<void> src, AudioFormat format, Error &error)
{
const int32_t *buf;
size_t len;
2013-08-03 21:00:50 +02:00
assert(dest_format.format == SampleFormat::S24_P32);
2013-07-29 08:10:10 +02:00
buf = pcm_convert_to_24(format_buffer,
format.format,
src.data, src.size, &len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %s to 24 bit is not implemented",
sample_format_to_string(format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
if (format.channels != dest_format.channels) {
2013-07-29 08:10:10 +02:00
buf = pcm_convert_channels_24(channels_buffer,
2013-08-03 21:00:50 +02:00
dest_format.channels,
format.channels,
buf, len, &len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %u to %u channels "
"is not implemented",
format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
if (format.sample_rate != dest_format.sample_rate) {
2013-08-03 21:00:50 +02:00
buf = resampler.Resample24(dest_format.channels,
format.sample_rate, buf, len,
2013-08-03 21:00:50 +02:00
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return nullptr;
}
return ConstBuffer<int32_t>::FromVoid({buf, len});
}
inline ConstBuffer<int32_t>
PcmConvert::Convert32(ConstBuffer<void> src, AudioFormat format, Error &error)
{
const int32_t *buf;
size_t len;
2013-08-03 21:00:50 +02:00
assert(dest_format.format == SampleFormat::S32);
2013-07-29 08:10:10 +02:00
buf = pcm_convert_to_32(format_buffer,
format.format,
src.data, src.size, &len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %s to 32 bit is not implemented",
sample_format_to_string(format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
if (format.channels != dest_format.channels) {
2013-07-29 08:10:10 +02:00
buf = pcm_convert_channels_32(channels_buffer,
2013-08-03 21:00:50 +02:00
dest_format.channels,
format.channels,
buf, len, &len);
2013-10-28 23:58:17 +01:00
if (buf == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %u to %u channels "
"is not implemented",
format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
if (format.sample_rate != dest_format.sample_rate) {
2013-08-03 21:00:50 +02:00
buf = resampler.Resample32(dest_format.channels,
format.sample_rate, buf, len,
2013-08-03 21:00:50 +02:00
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return nullptr;
}
return ConstBuffer<int32_t>::FromVoid({buf, len});
}
inline ConstBuffer<float>
PcmConvert::ConvertFloat(ConstBuffer<void> src, AudioFormat format,
Error &error)
{
2013-08-03 21:00:50 +02:00
assert(dest_format.format == SampleFormat::FLOAT);
/* convert to float now */
size_t size;
const float *buffer = pcm_convert_to_float(format_buffer,
format.format,
src.data, src.size, &size);
2013-10-28 23:58:17 +01:00
if (buffer == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %s to float is not implemented",
sample_format_to_string(format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
/* convert channels */
if (format.channels != dest_format.channels) {
2013-07-29 08:10:10 +02:00
buffer = pcm_convert_channels_float(channels_buffer,
2013-08-03 21:00:50 +02:00
dest_format.channels,
format.channels,
buffer, size, &size);
2013-10-28 23:58:17 +01:00
if (buffer == nullptr) {
error.Format(pcm_convert_domain,
"Conversion from %u to %u channels "
"is not implemented",
format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
/* resample with float, because this is the best format for
libsamplerate */
if (format.sample_rate != dest_format.sample_rate) {
2013-08-03 21:00:50 +02:00
buffer = resampler.ResampleFloat(dest_format.channels,
format.sample_rate,
2013-07-29 23:18:55 +02:00
buffer, size,
2013-08-03 21:00:50 +02:00
dest_format.sample_rate,
&size, error);
2013-10-28 23:58:17 +01:00
if (buffer == nullptr)
return nullptr;
}
return ConstBuffer<float>::FromVoid({buffer, size});
}
const void *
PcmConvert::Convert(const void *src, size_t src_size,
2013-01-30 23:40:56 +01:00
size_t *dest_size_r,
Error &error)
{
2013-11-29 22:06:14 +01:00
ConstBuffer<void> buffer(src, src_size);
AudioFormat format = src_format;
2013-11-29 22:06:14 +01:00
if (format.format == SampleFormat::DSD) {
2013-11-29 22:06:14 +01:00
auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
auto d = dsd.ToFloat(format.channels,
2013-11-29 22:06:14 +01:00
false, s);
if (d.IsNull()) {
error.Set(pcm_convert_domain,
"DSD to PCM conversion failed");
2013-10-28 23:58:17 +01:00
return nullptr;
2012-03-01 00:57:33 +01:00
}
2013-11-29 22:06:14 +01:00
buffer = d.ToVoid();
format.format = SampleFormat::FLOAT;
2012-03-01 00:57:33 +01:00
}
2013-08-03 21:00:50 +02:00
switch (dest_format.format) {
case SampleFormat::S16:
buffer = Convert16(buffer, format, error).ToVoid();
break;
2013-08-03 21:00:50 +02:00
case SampleFormat::S24_P32:
buffer = Convert24(buffer, format, error).ToVoid();
break;
2013-08-03 21:00:50 +02:00
case SampleFormat::S32:
buffer = Convert32(buffer, format, error).ToVoid();
break;
2013-08-03 21:00:50 +02:00
case SampleFormat::FLOAT:
buffer = ConvertFloat(buffer, format, error).ToVoid();
break;
default:
error.Format(pcm_convert_domain,
"PCM conversion to %s is not implemented",
sample_format_to_string(dest_format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
*dest_size_r = buffer.size;
return buffer.data;
}