2009-01-07 18:53:36 +01:00
|
|
|
/*
|
2013-10-30 23:37:06 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-01-07 18:53:36 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 18:06:14 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 18:06:14 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-30 23:40:56 +01:00
|
|
|
#include "PcmConvert.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2013-11-29 22:06:14 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-11-29 21:39:53 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
const Domain pcm_convert_domain("pcm_convert");
|
|
|
|
|
2013-11-29 10:06:20 +01:00
|
|
|
bool
|
|
|
|
pcm_convert_global_init(Error &error)
|
|
|
|
{
|
|
|
|
return pcm_resample_global_init(error);
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
PcmConvert::PcmConvert()
|
2008-10-21 22:53:16 +02:00
|
|
|
{
|
2013-11-11 16:15:38 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
src_format.Clear();
|
|
|
|
dest_format.Clear();
|
|
|
|
#endif
|
2008-10-21 22:53:16 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
PcmConvert::~PcmConvert()
|
2009-01-07 22:20:30 +01:00
|
|
|
{
|
2013-11-11 16:15:38 +01:00
|
|
|
assert(!src_format.IsValid());
|
|
|
|
assert(!dest_format.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format,
|
2013-11-29 21:39:53 +01:00
|
|
|
Error &error)
|
2013-11-11 16:15:38 +01:00
|
|
|
{
|
|
|
|
assert(!src_format.IsValid());
|
|
|
|
assert(!dest_format.IsValid());
|
|
|
|
assert(_src_format.IsValid());
|
|
|
|
assert(_dest_format.IsValid());
|
|
|
|
|
|
|
|
src_format = _src_format;
|
|
|
|
dest_format = _dest_format;
|
|
|
|
|
2013-11-29 21:39:53 +01:00
|
|
|
AudioFormat format = src_format;
|
|
|
|
if (format.format == SampleFormat::DSD)
|
|
|
|
format.format = SampleFormat::FLOAT;
|
|
|
|
|
|
|
|
if (format.format != dest_format.format &&
|
|
|
|
!format_converter.Open(format.format, dest_format.format, error))
|
|
|
|
return false;
|
|
|
|
format.format = dest_format.format;
|
|
|
|
|
2013-11-30 13:19:07 +01:00
|
|
|
if (format.channels != dest_format.channels &&
|
|
|
|
!channels_converter.Open(format.format, format.channels,
|
|
|
|
dest_format.channels, error)) {
|
|
|
|
format_converter.Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-30 14:10:31 +01:00
|
|
|
if (format.sample_rate != dest_format.sample_rate &&
|
|
|
|
!resampler.Open(format, dest_format.sample_rate, error))
|
|
|
|
return false;
|
|
|
|
|
2013-11-11 16:15:38 +01:00
|
|
|
return true;
|
2009-01-07 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2012-03-01 00:59:53 +01:00
|
|
|
void
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Close()
|
2012-03-01 00:59:53 +01:00
|
|
|
{
|
2013-11-30 13:19:07 +01:00
|
|
|
if (src_format.channels != dest_format.channels)
|
|
|
|
channels_converter.Close();
|
|
|
|
|
2013-11-29 21:39:53 +01:00
|
|
|
if (src_format.format != dest_format.format)
|
|
|
|
format_converter.Close();
|
|
|
|
|
2013-07-29 07:56:40 +02:00
|
|
|
dsd.Reset();
|
2013-11-30 14:10:31 +01:00
|
|
|
|
|
|
|
if (src_format.sample_rate != dest_format.sample_rate)
|
|
|
|
resampler.Close();
|
2013-11-11 16:15:38 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
src_format.Clear();
|
|
|
|
dest_format.Clear();
|
|
|
|
#endif
|
2012-03-01 00:59:53 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 13:11:16 +01:00
|
|
|
const void *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Convert(const void *src, size_t src_size,
|
2013-01-30 23:40:56 +01:00
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-23 20:11:24 +02:00
|
|
|
{
|
2013-11-29 22:06:14 +01:00
|
|
|
ConstBuffer<void> buffer(src, src_size);
|
2013-11-30 13:00:41 +01:00
|
|
|
AudioFormat format = src_format;
|
2013-11-29 22:06:14 +01:00
|
|
|
|
2013-11-30 13:00:41 +01:00
|
|
|
if (format.format == SampleFormat::DSD) {
|
2013-11-29 22:06:14 +01:00
|
|
|
auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
|
2013-11-30 13:00:41 +01:00
|
|
|
auto d = dsd.ToFloat(format.channels,
|
2013-11-29 22:06:14 +01:00
|
|
|
false, s);
|
|
|
|
if (d.IsNull()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
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();
|
2013-11-30 13:00:41 +01:00
|
|
|
format.format = SampleFormat::FLOAT;
|
2012-03-01 00:57:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 21:39:53 +01:00
|
|
|
if (format.format != dest_format.format) {
|
|
|
|
buffer = format_converter.Convert(buffer, error);
|
|
|
|
if (buffer.IsNull())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
format.format = dest_format.format;
|
|
|
|
}
|
|
|
|
|
2013-11-30 13:19:07 +01:00
|
|
|
if (format.channels != dest_format.channels) {
|
|
|
|
buffer = channels_converter.Convert(buffer, error);
|
|
|
|
if (buffer.IsNull())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
format.channels = dest_format.channels;
|
|
|
|
}
|
|
|
|
|
2013-11-30 14:10:31 +01:00
|
|
|
if (format.sample_rate != dest_format.sample_rate) {
|
|
|
|
buffer = resampler.Resample(buffer, error);
|
|
|
|
if (buffer.IsNull())
|
|
|
|
return nullptr;
|
2011-10-08 15:36:29 +02:00
|
|
|
|
2013-11-30 14:10:31 +01:00
|
|
|
format.sample_rate = dest_format.sample_rate;
|
2008-10-23 20:11:24 +02:00
|
|
|
}
|
2013-11-29 22:48:23 +01:00
|
|
|
|
|
|
|
*dest_size_r = buffer.size;
|
|
|
|
return buffer.data;
|
2008-10-23 20:11:24 +02:00
|
|
|
}
|