From 5ba90cd8ea87cf97d64409b7b4c59033c2450c77 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 11 Nov 2013 22:31:46 +0100 Subject: pcm/PcmResampler: convert to abstract interface The PcmResampler interface is implemented by the two classes FallbackPcmResampler and LibsampleratePcmResampler. This prepares for adding more resampler libraries. --- src/pcm/PcmConvert.cxx | 62 +++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'src/pcm/PcmConvert.cxx') diff --git a/src/pcm/PcmConvert.cxx b/src/pcm/PcmConvert.cxx index 0552962aa..5501d8ddf 100644 --- a/src/pcm/PcmConvert.cxx +++ b/src/pcm/PcmConvert.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "PcmConvert.hxx" +#include "ConfiguredResampler.hxx" #include "AudioFormat.hxx" #include "util/ConstBuffer.hxx" #include "util/Error.hxx" @@ -33,7 +34,7 @@ const Domain pcm_convert_domain("pcm_convert"); bool pcm_convert_global_init(Error &error) { - return pcm_resample_global_init(error); + return pcm_resampler_global_init(error); } PcmConvert::PcmConvert() @@ -66,39 +67,51 @@ PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_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)) + enable_resampler = format.sample_rate != dest_format.sample_rate; + if (enable_resampler) { + if (!resampler.Open(format, dest_format.sample_rate, error)) + return false; + + format.format = resampler.GetOutputSampleFormat(); + format.sample_rate = dest_format.sample_rate; + } + + enable_format = format.format != dest_format.format; + if (enable_format && + !format_converter.Open(format.format, dest_format.format, error)) { + if (enable_resampler) + resampler.Close(); return false; + } + format.format = dest_format.format; - if (format.channels != dest_format.channels && + enable_channels = format.channels != dest_format.channels; + if (enable_channels && !channels_converter.Open(format.format, format.channels, dest_format.channels, error)) { - format_converter.Close(); + if (enable_format) + format_converter.Close(); + if (enable_resampler) + resampler.Close(); return false; } - if (format.sample_rate != dest_format.sample_rate && - !resampler.Open(format, dest_format.sample_rate, error)) - return false; - return true; } void PcmConvert::Close() { - if (src_format.channels != dest_format.channels) + if (enable_channels) channels_converter.Close(); - - if (src_format.format != dest_format.format) + if (enable_format) format_converter.Close(); + if (enable_resampler) + resampler.Close(); dsd.Reset(); - if (src_format.sample_rate != dest_format.sample_rate) - resampler.Close(); - #ifndef NDEBUG src_format.Clear(); dest_format.Clear(); @@ -127,28 +140,29 @@ PcmConvert::Convert(const void *src, size_t src_size, format.format = SampleFormat::FLOAT; } - if (format.format != dest_format.format) { - buffer = format_converter.Convert(buffer, error); + if (enable_resampler) { + buffer = resampler.Resample(buffer, error); if (buffer.IsNull()) return nullptr; - format.format = dest_format.format; + format.format = resampler.GetOutputSampleFormat(); + format.sample_rate = dest_format.sample_rate; } - if (format.channels != dest_format.channels) { - buffer = channels_converter.Convert(buffer, error); + if (enable_format) { + buffer = format_converter.Convert(buffer, error); if (buffer.IsNull()) return nullptr; - format.channels = dest_format.channels; + format.format = dest_format.format; } - if (format.sample_rate != dest_format.sample_rate) { - buffer = resampler.Resample(buffer, error); + if (enable_channels) { + buffer = channels_converter.Convert(buffer, error); if (buffer.IsNull()) return nullptr; - format.sample_rate = dest_format.sample_rate; + format.channels = dest_format.channels; } *dest_size_r = buffer.size; -- cgit v1.2.3