mpd/src/pcm/PcmConvert.cxx

308 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"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include <assert.h>
#include <math.h>
const Domain pcm_convert_domain("pcm_convert");
2013-01-30 23:40:56 +01:00
PcmConvert::PcmConvert()
{
}
2013-01-30 23:40:56 +01:00
PcmConvert::~PcmConvert()
{
}
void
2013-01-30 23:40:56 +01:00
PcmConvert::Reset()
{
2013-07-29 07:56:40 +02:00
dsd.Reset();
2013-07-29 23:18:55 +02:00
resampler.Reset();
}
2013-01-30 23:40:56 +01:00
inline const int16_t *
2013-08-03 21:00:50 +02:00
PcmConvert::Convert16(const AudioFormat src_format,
2013-01-30 23:40:56 +01:00
const void *src_buffer, size_t src_size,
2013-08-03 21:00:50 +02:00
const AudioFormat dest_format, size_t *dest_size_r,
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,
2013-08-03 21:00:50 +02:00
src_format.format,
2013-01-30 23:40:56 +01:00
src_buffer, 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(src_format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
2013-08-03 21:00:50 +02:00
if (src_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,
src_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",
src_format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
2013-08-03 21:00:50 +02:00
if (src_format.sample_rate != dest_format.sample_rate) {
buf = resampler.Resample16(dest_format.channels,
src_format.sample_rate, buf, len,
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return nullptr;
}
*dest_size_r = len;
return buf;
}
2013-01-30 23:40:56 +01:00
inline const int32_t *
2013-08-03 21:00:50 +02:00
PcmConvert::Convert24(const AudioFormat src_format,
2013-01-30 23:40:56 +01:00
const void *src_buffer, size_t src_size,
2013-08-03 21:00:50 +02:00
const AudioFormat dest_format, size_t *dest_size_r,
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,
2013-08-03 21:00:50 +02:00
src_format.format,
src_buffer, 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(src_format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
2013-08-03 21:00:50 +02:00
if (src_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,
src_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",
src_format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
2013-08-03 21:00:50 +02:00
if (src_format.sample_rate != dest_format.sample_rate) {
buf = resampler.Resample24(dest_format.channels,
src_format.sample_rate, buf, len,
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return nullptr;
}
*dest_size_r = len;
return buf;
}
2013-01-30 23:40:56 +01:00
inline const int32_t *
2013-08-03 21:00:50 +02:00
PcmConvert::Convert32(const AudioFormat src_format,
2013-01-30 23:40:56 +01:00
const void *src_buffer, size_t src_size,
2013-08-03 21:00:50 +02:00
const AudioFormat dest_format, size_t *dest_size_r,
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,
2013-08-03 21:00:50 +02:00
src_format.format,
src_buffer, 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(src_format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
2013-08-03 21:00:50 +02:00
if (src_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,
src_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",
src_format.channels,
dest_format.channels);
2013-10-28 23:58:17 +01:00
return nullptr;
}
}
2013-08-03 21:00:50 +02:00
if (src_format.sample_rate != dest_format.sample_rate) {
buf = resampler.Resample32(dest_format.channels,
src_format.sample_rate, buf, len,
dest_format.sample_rate, &len,
error);
2013-10-28 23:58:17 +01:00
if (buf == nullptr)
return buf;
}
*dest_size_r = len;
return buf;
}
2013-01-30 23:40:56 +01:00
inline const float *
2013-08-03 21:00:50 +02:00
PcmConvert::ConvertFloat(const AudioFormat src_format,
2013-01-30 23:40:56 +01:00
const void *src_buffer, size_t src_size,
2013-08-03 21:00:50 +02:00
const AudioFormat dest_format, size_t *dest_size_r,
Error &error)
{
2013-01-30 23:40:56 +01:00
const float *buffer = (const float *)src_buffer;
size_t size = src_size;
2013-08-03 21:00:50 +02:00
assert(dest_format.format == SampleFormat::FLOAT);
/* convert to float now */
2013-07-29 08:10:10 +02:00
buffer = pcm_convert_to_float(format_buffer,
2013-08-03 21:00:50 +02:00
src_format.format,
buffer, 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(src_format.format));
2013-10-28 23:58:17 +01:00
return nullptr;
}
/* convert channels */
2013-08-03 21:00:50 +02:00
if (src_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,
src_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",
src_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 */
2013-08-03 21:00:50 +02:00
if (src_format.sample_rate != dest_format.sample_rate) {
buffer = resampler.ResampleFloat(dest_format.channels,
src_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;
}
*dest_size_r = size;
return buffer;
}
const void *
2013-08-03 21:00:50 +02:00
PcmConvert::Convert(AudioFormat src_format,
2013-01-30 23:40:56 +01:00
const void *src, size_t src_size,
2013-08-03 21:00:50 +02:00
const AudioFormat dest_format,
2013-01-30 23:40:56 +01:00
size_t *dest_size_r,
Error &error)
{
2013-08-03 21:00:50 +02:00
AudioFormat float_format;
if (src_format.format == SampleFormat::DSD) {
2012-03-01 00:57:33 +01:00
size_t f_size;
2013-08-03 21:00:50 +02:00
const float *f = dsd.ToFloat(src_format.channels,
2013-07-29 07:56:40 +02:00
false, (const uint8_t *)src,
src_size, &f_size);
2013-10-28 23:58:17 +01:00
if (f == nullptr) {
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-08-03 21:00:50 +02:00
float_format = src_format;
float_format.format = SampleFormat::FLOAT;
2012-03-01 00:57:33 +01:00
2013-08-03 21:00:50 +02:00
src_format = float_format;
2012-03-01 00:57:33 +01:00
src = f;
src_size = f_size;
}
2013-08-03 21:00:50 +02:00
switch (dest_format.format) {
case SampleFormat::S16:
2013-01-30 23:40:56 +01:00
return Convert16(src_format, src, src_size,
dest_format, dest_size_r,
error);
2013-08-03 21:00:50 +02:00
case SampleFormat::S24_P32:
2013-01-30 23:40:56 +01:00
return Convert24(src_format, src, src_size,
dest_format, dest_size_r,
error);
2013-08-03 21:00:50 +02:00
case SampleFormat::S32:
2013-01-30 23:40:56 +01:00
return Convert32(src_format, src, src_size,
dest_format, dest_size_r,
error);
2013-08-03 21:00:50 +02:00
case SampleFormat::FLOAT:
2013-01-30 23:40:56 +01:00
return ConvertFloat(src_format, src, src_size,
dest_format, dest_size_r,
error);
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;
}
}