2009-01-07 18:53:36 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 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
|
|
|
*/
|
|
|
|
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "Convert.hxx"
|
2013-11-11 22:31:46 +01:00
|
|
|
#include "ConfiguredResampler.hxx"
|
2013-11-29 21:39:53 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2019-12-16 22:52:50 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
void
|
2018-07-17 23:04:26 +02:00
|
|
|
pcm_convert_global_init(const ConfigData &config)
|
2013-11-29 10:06:20 +01:00
|
|
|
{
|
2018-07-17 23:04:26 +02:00
|
|
|
pcm_resampler_global_init(config);
|
2013-11-29 10:06:20 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:55:39 +02:00
|
|
|
PcmConvert::PcmConvert(const AudioFormat _src_format,
|
2019-04-04 21:06:28 +02:00
|
|
|
const AudioFormat dest_format)
|
|
|
|
:src_format(_src_format)
|
2008-10-21 22:53:16 +02:00
|
|
|
{
|
2019-04-04 20:55:39 +02:00
|
|
|
assert(src_format.IsValid());
|
|
|
|
assert(dest_format.IsValid());
|
2013-11-11 16:15:38 +01:00
|
|
|
|
2014-10-23 22:31:54 +02:00
|
|
|
AudioFormat format = _src_format;
|
2020-01-17 19:15:28 +01:00
|
|
|
if (format.format == SampleFormat::DSD) {
|
|
|
|
#ifdef ENABLE_DSD
|
2020-01-17 19:12:16 +01:00
|
|
|
dsd2pcm_float = dest_format.format == SampleFormat::FLOAT;
|
|
|
|
format.format = dsd2pcm_float
|
|
|
|
? SampleFormat::FLOAT
|
|
|
|
: SampleFormat::S24_P32;
|
2020-01-17 19:15:28 +01:00
|
|
|
#else
|
|
|
|
throw std::runtime_error("DSD support is disabled");
|
|
|
|
#endif
|
|
|
|
}
|
2013-11-29 21:39:53 +01:00
|
|
|
|
2019-04-04 21:06:28 +02:00
|
|
|
enable_resampler = format.sample_rate != dest_format.sample_rate;
|
2013-11-11 22:31:46 +01:00
|
|
|
if (enable_resampler) {
|
2019-04-04 21:06:28 +02:00
|
|
|
resampler.Open(format, dest_format.sample_rate);
|
2013-11-11 22:31:46 +01:00
|
|
|
|
|
|
|
format.format = resampler.GetOutputSampleFormat();
|
2019-04-04 21:06:28 +02:00
|
|
|
format.sample_rate = dest_format.sample_rate;
|
2013-11-11 22:31:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 21:06:28 +02:00
|
|
|
enable_format = format.format != dest_format.format;
|
2016-09-05 12:19:20 +02:00
|
|
|
if (enable_format) {
|
|
|
|
try {
|
|
|
|
format_converter.Open(format.format,
|
2019-04-04 21:06:28 +02:00
|
|
|
dest_format.format);
|
2016-09-05 12:19:20 +02:00
|
|
|
} catch (...) {
|
|
|
|
if (enable_resampler)
|
|
|
|
resampler.Close();
|
|
|
|
throw;
|
|
|
|
}
|
2013-11-11 22:31:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 21:06:28 +02:00
|
|
|
format.format = dest_format.format;
|
2013-11-29 21:39:53 +01:00
|
|
|
|
2019-04-04 21:06:28 +02:00
|
|
|
enable_channels = format.channels != dest_format.channels;
|
2016-09-05 12:19:20 +02:00
|
|
|
if (enable_channels) {
|
|
|
|
try {
|
|
|
|
channels_converter.Open(format.format, format.channels,
|
2019-04-04 21:06:28 +02:00
|
|
|
dest_format.channels);
|
2016-09-05 12:19:20 +02:00
|
|
|
} catch (...) {
|
|
|
|
if (enable_format)
|
|
|
|
format_converter.Close();
|
|
|
|
if (enable_resampler)
|
|
|
|
resampler.Close();
|
|
|
|
throw;
|
|
|
|
}
|
2013-11-30 13:19:07 +01:00
|
|
|
}
|
2009-01-07 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:55:39 +02:00
|
|
|
PcmConvert::~PcmConvert() noexcept
|
2012-03-01 00:59:53 +01:00
|
|
|
{
|
2013-11-11 22:31:46 +01:00
|
|
|
if (enable_channels)
|
2013-11-30 13:19:07 +01:00
|
|
|
channels_converter.Close();
|
2013-11-11 22:31:46 +01:00
|
|
|
if (enable_format)
|
2013-11-29 21:39:53 +01:00
|
|
|
format_converter.Close();
|
2013-11-11 22:31:46 +01:00
|
|
|
if (enable_resampler)
|
|
|
|
resampler.Close();
|
2013-11-29 21:39:53 +01:00
|
|
|
|
2014-09-26 10:52:46 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2013-07-29 07:56:40 +02:00
|
|
|
dsd.Reset();
|
2014-09-26 10:52:46 +02:00
|
|
|
#endif
|
2012-03-01 00:59:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 15:30:30 +01:00
|
|
|
void
|
2018-01-01 19:07:33 +01:00
|
|
|
PcmConvert::Reset() noexcept
|
2017-01-11 15:30:30 +01:00
|
|
|
{
|
|
|
|
if (enable_resampler)
|
|
|
|
resampler.Reset();
|
|
|
|
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
dsd.Reset();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:36:07 +02:00
|
|
|
ConstBuffer<void>
|
2016-09-05 12:19:20 +02:00
|
|
|
PcmConvert::Convert(ConstBuffer<void> buffer)
|
2008-10-23 20:11:24 +02:00
|
|
|
{
|
2014-09-26 10:52:46 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2014-09-26 12:17:37 +02:00
|
|
|
if (src_format.format == SampleFormat::DSD) {
|
2013-11-29 22:06:14 +01:00
|
|
|
auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
|
2020-01-17 19:12:16 +01:00
|
|
|
auto d = dsd2pcm_float
|
|
|
|
? dsd.ToFloat(src_format.channels, s).ToVoid()
|
|
|
|
: dsd.ToS24(src_format.channels, s).ToVoid();
|
2016-09-05 12:19:20 +02:00
|
|
|
if (d.IsNull())
|
|
|
|
throw std::runtime_error("DSD to PCM conversion failed");
|
2012-03-01 00:57:33 +01:00
|
|
|
|
2020-01-17 19:12:16 +01:00
|
|
|
buffer = d;
|
2012-03-01 00:57:33 +01:00
|
|
|
}
|
2014-09-26 10:52:46 +02:00
|
|
|
#endif
|
2012-03-01 00:57:33 +01:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
if (enable_resampler)
|
|
|
|
buffer = resampler.Resample(buffer);
|
2013-11-29 21:39:53 +01:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
if (enable_format)
|
|
|
|
buffer = format_converter.Convert(buffer);
|
2013-11-30 13:19:07 +01:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
if (enable_channels)
|
|
|
|
buffer = channels_converter.Convert(buffer);
|
2013-11-29 22:48:23 +01:00
|
|
|
|
2014-08-12 16:36:07 +02:00
|
|
|
return buffer;
|
2008-10-23 20:11:24 +02:00
|
|
|
}
|
2018-01-02 18:22:53 +01:00
|
|
|
|
|
|
|
ConstBuffer<void>
|
|
|
|
PcmConvert::Flush()
|
|
|
|
{
|
|
|
|
if (enable_resampler) {
|
|
|
|
auto buffer = resampler.Flush();
|
|
|
|
if (!buffer.IsNull()) {
|
|
|
|
if (enable_format)
|
|
|
|
buffer = format_converter.Convert(buffer);
|
|
|
|
|
|
|
|
if (enable_channels)
|
|
|
|
buffer = channels_converter.Convert(buffer);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|