pcm/PcmConvert: move code to new class GluePcmResampler
This commit is contained in:
parent
92004f2e7e
commit
e9127523db
@ -339,6 +339,7 @@ libpcm_a_SOURCES = \
|
||||
src/pcm/PcmFormat.cxx src/pcm/PcmFormat.hxx \
|
||||
src/pcm/FormatConverter.cxx src/pcm/FormatConverter.hxx \
|
||||
src/pcm/ChannelsConverter.cxx src/pcm/ChannelsConverter.hxx \
|
||||
src/pcm/GlueResampler.cxx src/pcm/GlueResampler.hxx \
|
||||
src/pcm/PcmResample.cxx src/pcm/PcmResample.hxx \
|
||||
src/pcm/PcmResampleFallback.cxx \
|
||||
src/pcm/PcmResampleInternal.hxx \
|
||||
|
94
src/pcm/GlueResampler.cxx
Normal file
94
src/pcm/GlueResampler.cxx
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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"
|
||||
#include "GlueResampler.hxx"
|
||||
#include "PcmConvert.hxx"
|
||||
#include "PcmFormat.hxx"
|
||||
#include "util/ConstBuffer.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
bool
|
||||
GluePcmResampler::Open(AudioFormat _src_format, unsigned _new_sample_rate,
|
||||
gcc_unused Error &error)
|
||||
{
|
||||
src_format = _src_format;
|
||||
new_sample_rate = _new_sample_rate;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
GluePcmResampler::Close()
|
||||
{
|
||||
resampler.Reset();
|
||||
}
|
||||
|
||||
ConstBuffer<void>
|
||||
GluePcmResampler::Resample(ConstBuffer<void> src, Error &error)
|
||||
{
|
||||
const void *result;
|
||||
size_t size;
|
||||
|
||||
switch (src_format.format) {
|
||||
case SampleFormat::S16:
|
||||
result = resampler.Resample16(src_format.channels,
|
||||
src_format.sample_rate,
|
||||
(const int16_t *)src.data,
|
||||
src.size,
|
||||
new_sample_rate, &size,
|
||||
error);
|
||||
break;
|
||||
|
||||
case SampleFormat::S24_P32:
|
||||
result = resampler.Resample24(src_format.channels,
|
||||
src_format.sample_rate,
|
||||
(const int32_t *)src.data,
|
||||
src.size,
|
||||
new_sample_rate, &size,
|
||||
error);
|
||||
break;
|
||||
|
||||
case SampleFormat::S32:
|
||||
result = resampler.Resample24(src_format.channels,
|
||||
src_format.sample_rate,
|
||||
(const int32_t *)src.data,
|
||||
src.size,
|
||||
new_sample_rate, &size,
|
||||
error);
|
||||
break;
|
||||
|
||||
case SampleFormat::FLOAT:
|
||||
result = resampler.ResampleFloat(src_format.channels,
|
||||
src_format.sample_rate,
|
||||
(const float *)src.data,
|
||||
src.size,
|
||||
new_sample_rate, &size,
|
||||
error);
|
||||
break;
|
||||
|
||||
default:
|
||||
error.Format(pcm_convert_domain,
|
||||
"Resampling %s is not implemented",
|
||||
sample_format_to_string(src_format.format));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return { result, size };
|
||||
}
|
44
src/pcm/GlueResampler.hxx
Normal file
44
src/pcm/GlueResampler.hxx
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MPD_GLUE_RESAMPLER_HXX
|
||||
#define MPD_GLUE_RESAMPLER_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "AudioFormat.hxx"
|
||||
#include "PcmResample.hxx"
|
||||
|
||||
class Error;
|
||||
template<typename T> struct ConstBuffer;
|
||||
|
||||
class GluePcmResampler {
|
||||
PcmResampler resampler;
|
||||
|
||||
AudioFormat src_format;
|
||||
unsigned new_sample_rate;
|
||||
|
||||
public:
|
||||
bool Open(AudioFormat src_format, unsigned new_sample_rate,
|
||||
Error &error);
|
||||
void Close();
|
||||
|
||||
ConstBuffer<void> Resample(ConstBuffer<void> src, Error &error);
|
||||
};
|
||||
|
||||
#endif
|
@ -78,6 +78,10 @@ PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (format.sample_rate != dest_format.sample_rate &&
|
||||
!resampler.Open(format, dest_format.sample_rate, error))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -91,7 +95,9 @@ PcmConvert::Close()
|
||||
format_converter.Close();
|
||||
|
||||
dsd.Reset();
|
||||
resampler.Reset();
|
||||
|
||||
if (src_format.sample_rate != dest_format.sample_rate)
|
||||
resampler.Close();
|
||||
|
||||
#ifndef NDEBUG
|
||||
src_format.Clear();
|
||||
@ -99,102 +105,6 @@ PcmConvert::Close()
|
||||
#endif
|
||||
}
|
||||
|
||||
inline ConstBuffer<int16_t>
|
||||
PcmConvert::Convert16(ConstBuffer<int16_t> src, AudioFormat format,
|
||||
Error &error)
|
||||
{
|
||||
assert(format.format == SampleFormat::S16);
|
||||
assert(dest_format.format == SampleFormat::S16);
|
||||
assert(format.channels == dest_format.channels);
|
||||
|
||||
auto buf = src.data;
|
||||
size_t len = src.size * sizeof(*src.data);
|
||||
|
||||
if (format.sample_rate != dest_format.sample_rate) {
|
||||
buf = resampler.Resample16(dest_format.channels,
|
||||
format.sample_rate, buf, len,
|
||||
dest_format.sample_rate, &len,
|
||||
error);
|
||||
if (buf == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConstBuffer<int16_t>::FromVoid({buf, len});
|
||||
}
|
||||
|
||||
inline ConstBuffer<int32_t>
|
||||
PcmConvert::Convert24(ConstBuffer<int32_t> src, AudioFormat format,
|
||||
Error &error)
|
||||
{
|
||||
assert(format.format == SampleFormat::S24_P32);
|
||||
assert(dest_format.format == SampleFormat::S24_P32);
|
||||
assert(format.channels == dest_format.channels);
|
||||
|
||||
auto buf = src.data;
|
||||
size_t len = src.size * sizeof(*src.data);
|
||||
|
||||
if (format.sample_rate != dest_format.sample_rate) {
|
||||
buf = resampler.Resample24(dest_format.channels,
|
||||
format.sample_rate, buf, len,
|
||||
dest_format.sample_rate, &len,
|
||||
error);
|
||||
if (buf == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConstBuffer<int32_t>::FromVoid({buf, len});
|
||||
}
|
||||
|
||||
inline ConstBuffer<int32_t>
|
||||
PcmConvert::Convert32(ConstBuffer<int32_t> src, AudioFormat format,
|
||||
Error &error)
|
||||
{
|
||||
assert(format.format == SampleFormat::S32);
|
||||
assert(dest_format.format == SampleFormat::S32);
|
||||
assert(format.channels == dest_format.channels);
|
||||
|
||||
auto buf = src.data;
|
||||
size_t len = src.size * sizeof(*src.data);
|
||||
|
||||
if (format.sample_rate != dest_format.sample_rate) {
|
||||
buf = resampler.Resample32(dest_format.channels,
|
||||
format.sample_rate, buf, len,
|
||||
dest_format.sample_rate, &len,
|
||||
error);
|
||||
if (buf == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConstBuffer<int32_t>::FromVoid({buf, len});
|
||||
}
|
||||
|
||||
inline ConstBuffer<float>
|
||||
PcmConvert::ConvertFloat(ConstBuffer<float> src, AudioFormat format,
|
||||
Error &error)
|
||||
{
|
||||
assert(format.format == SampleFormat::FLOAT);
|
||||
assert(dest_format.format == SampleFormat::FLOAT);
|
||||
assert(format.channels == dest_format.channels);
|
||||
|
||||
auto buffer = src.data;
|
||||
size_t size = src.size * sizeof(*src.data);
|
||||
|
||||
/* resample with float, because this is the best format for
|
||||
libsamplerate */
|
||||
|
||||
if (format.sample_rate != dest_format.sample_rate) {
|
||||
buffer = resampler.ResampleFloat(dest_format.channels,
|
||||
format.sample_rate,
|
||||
buffer, size,
|
||||
dest_format.sample_rate,
|
||||
&size, error);
|
||||
if (buffer == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ConstBuffer<float>::FromVoid({buffer, size});
|
||||
}
|
||||
|
||||
const void *
|
||||
PcmConvert::Convert(const void *src, size_t src_size,
|
||||
size_t *dest_size_r,
|
||||
@ -233,32 +143,12 @@ PcmConvert::Convert(const void *src, size_t src_size,
|
||||
format.channels = dest_format.channels;
|
||||
}
|
||||
|
||||
switch (dest_format.format) {
|
||||
case SampleFormat::S16:
|
||||
buffer = Convert16(ConstBuffer<int16_t>::FromVoid(buffer),
|
||||
format, error).ToVoid();
|
||||
break;
|
||||
if (format.sample_rate != dest_format.sample_rate) {
|
||||
buffer = resampler.Resample(buffer, error);
|
||||
if (buffer.IsNull())
|
||||
return nullptr;
|
||||
|
||||
case SampleFormat::S24_P32:
|
||||
buffer = Convert24(ConstBuffer<int32_t>::FromVoid(buffer),
|
||||
format, error).ToVoid();
|
||||
break;
|
||||
|
||||
case SampleFormat::S32:
|
||||
buffer = Convert32(ConstBuffer<int32_t>::FromVoid(buffer),
|
||||
format, error).ToVoid();
|
||||
break;
|
||||
|
||||
case SampleFormat::FLOAT:
|
||||
buffer = ConvertFloat(ConstBuffer<float>::FromVoid(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));
|
||||
return nullptr;
|
||||
format.sample_rate = dest_format.sample_rate;
|
||||
}
|
||||
|
||||
*dest_size_r = buffer.size;
|
||||
|
@ -21,10 +21,10 @@
|
||||
#define PCM_CONVERT_HXX
|
||||
|
||||
#include "PcmDsd.hxx"
|
||||
#include "PcmResample.hxx"
|
||||
#include "PcmBuffer.hxx"
|
||||
#include "FormatConverter.hxx"
|
||||
#include "ChannelsConverter.hxx"
|
||||
#include "GlueResampler.hxx"
|
||||
#include "AudioFormat.hxx"
|
||||
|
||||
#include <stddef.h>
|
||||
@ -41,10 +41,9 @@ class Domain;
|
||||
class PcmConvert {
|
||||
PcmDsd dsd;
|
||||
|
||||
PcmResampler resampler;
|
||||
|
||||
PcmFormatConverter format_converter;
|
||||
PcmChannelsConverter channels_converter;
|
||||
GluePcmResampler resampler;
|
||||
|
||||
AudioFormat src_format, dest_format;
|
||||
|
||||
@ -79,20 +78,6 @@ public:
|
||||
const void *Convert(const void *src, size_t src_size,
|
||||
size_t *dest_size_r,
|
||||
Error &error);
|
||||
|
||||
private:
|
||||
ConstBuffer<int16_t> Convert16(ConstBuffer<int16_t> src,
|
||||
AudioFormat format,
|
||||
Error &error);
|
||||
ConstBuffer<int32_t> Convert24(ConstBuffer<int32_t> src,
|
||||
AudioFormat format,
|
||||
Error &error);
|
||||
ConstBuffer<int32_t> Convert32(ConstBuffer<int32_t> src,
|
||||
AudioFormat format,
|
||||
Error &error);
|
||||
ConstBuffer<float> ConvertFloat(ConstBuffer<float> src,
|
||||
AudioFormat format,
|
||||
Error &error);
|
||||
};
|
||||
|
||||
extern const Domain pcm_convert_domain;
|
||||
|
Loading…
Reference in New Issue
Block a user