summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-11-30 14:10:31 +0100
committerMax Kellermann <max@duempel.org>2013-11-30 14:10:31 +0100
commite9127523db55a267f67532fd61e913f2879324fc (patch)
tree24f65d0ce209b63137e59b8368436cc4ab5ceaf4 /src
parent92004f2e7e6ec8479805313b0bdac7258d53c74e (diff)
pcm/PcmConvert: move code to new class GluePcmResampler
Diffstat (limited to 'src')
-rw-r--r--src/pcm/GlueResampler.cxx94
-rw-r--r--src/pcm/GlueResampler.hxx44
-rw-r--r--src/pcm/PcmConvert.cxx134
-rw-r--r--src/pcm/PcmConvert.hxx19
4 files changed, 152 insertions, 139 deletions
diff --git a/src/pcm/GlueResampler.cxx b/src/pcm/GlueResampler.cxx
new file mode 100644
index 000000000..8aee98f38
--- /dev/null
+++ b/src/pcm/GlueResampler.cxx
@@ -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 };
+}
diff --git a/src/pcm/GlueResampler.hxx b/src/pcm/GlueResampler.hxx
new file mode 100644
index 000000000..a7e0a84f2
--- /dev/null
+++ b/src/pcm/GlueResampler.hxx
@@ -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
diff --git a/src/pcm/PcmConvert.cxx b/src/pcm/PcmConvert.cxx
index bc289460d..0552962aa 100644
--- a/src/pcm/PcmConvert.cxx
+++ b/src/pcm/PcmConvert.cxx
@@ -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;
-
- 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;
+ if (format.sample_rate != dest_format.sample_rate) {
+ buffer = resampler.Resample(buffer, error);
+ if (buffer.IsNull())
+ return nullptr;
- 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;
diff --git a/src/pcm/PcmConvert.hxx b/src/pcm/PcmConvert.hxx
index d6e113915..9835045d6 100644
--- a/src/pcm/PcmConvert.hxx
+++ b/src/pcm/PcmConvert.hxx
@@ -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;