From a9d2dc614496a8eee4269f4032457eb8a7fb6102 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 29 Jul 2013 23:18:55 +0200 Subject: pcm_resample: convert to C++ --- src/pcm/PcmConvert.cxx | 44 ++--- src/pcm/PcmConvert.hxx | 4 +- src/pcm/PcmResample.cxx | 153 +++++++++++++++++ src/pcm/PcmResample.hxx | 138 ++++++++++++++++ src/pcm/PcmResampleFallback.cxx | 120 ++++++++++++++ src/pcm/PcmResampleInternal.hxx | 97 +++++++++++ src/pcm/PcmResampleLibsamplerate.cxx | 295 +++++++++++++++++++++++++++++++++ src/pcm/pcm_resample.c | 159 ------------------ src/pcm/pcm_resample.h | 164 ------------------ src/pcm/pcm_resample_fallback.c | 118 ------------- src/pcm/pcm_resample_internal.h | 97 ----------- src/pcm/pcm_resample_libsamplerate.c | 312 ----------------------------------- 12 files changed, 823 insertions(+), 878 deletions(-) create mode 100644 src/pcm/PcmResample.cxx create mode 100644 src/pcm/PcmResample.hxx create mode 100644 src/pcm/PcmResampleFallback.cxx create mode 100644 src/pcm/PcmResampleInternal.hxx create mode 100644 src/pcm/PcmResampleLibsamplerate.cxx delete mode 100644 src/pcm/pcm_resample.c delete mode 100644 src/pcm/pcm_resample.h delete mode 100644 src/pcm/pcm_resample_fallback.c delete mode 100644 src/pcm/pcm_resample_internal.h delete mode 100644 src/pcm/pcm_resample_libsamplerate.c (limited to 'src/pcm') diff --git a/src/pcm/PcmConvert.cxx b/src/pcm/PcmConvert.cxx index 0a3fa6816..d280101b5 100644 --- a/src/pcm/PcmConvert.cxx +++ b/src/pcm/PcmConvert.cxx @@ -34,16 +34,12 @@ PcmConvert::PcmConvert() { - pcm_resample_init(&resample); - pcm_buffer_init(&format_buffer); pcm_buffer_init(&channels_buffer); } PcmConvert::~PcmConvert() { - pcm_resample_deinit(&resample); - pcm_buffer_deinit(&format_buffer); pcm_buffer_deinit(&channels_buffer); } @@ -52,7 +48,7 @@ void PcmConvert::Reset() { dsd.Reset(); - pcm_resample_reset(&resample); + resampler.Reset(); } inline const int16_t * @@ -93,11 +89,10 @@ PcmConvert::Convert16(const audio_format *src_format, } if (src_format->sample_rate != dest_format->sample_rate) { - buf = pcm_resample_16(&resample, - dest_format->channels, - src_format->sample_rate, buf, len, - dest_format->sample_rate, &len, - error_r); + buf = resampler.Resample16(dest_format->channels, + src_format->sample_rate, buf, len, + dest_format->sample_rate, &len, + error_r); if (buf == NULL) return NULL; } @@ -143,11 +138,10 @@ PcmConvert::Convert24(const audio_format *src_format, } if (src_format->sample_rate != dest_format->sample_rate) { - buf = pcm_resample_24(&resample, - dest_format->channels, - src_format->sample_rate, buf, len, - dest_format->sample_rate, &len, - error_r); + buf = resampler.Resample24(dest_format->channels, + src_format->sample_rate, buf, len, + dest_format->sample_rate, &len, + error_r); if (buf == NULL) return NULL; } @@ -193,11 +187,10 @@ PcmConvert::Convert32(const audio_format *src_format, } if (src_format->sample_rate != dest_format->sample_rate) { - buf = pcm_resample_32(&resample, - dest_format->channels, - src_format->sample_rate, buf, len, - dest_format->sample_rate, &len, - error_r); + buf = resampler.Resample32(dest_format->channels, + src_format->sample_rate, buf, len, + dest_format->sample_rate, &len, + error_r); if (buf == NULL) return buf; } @@ -250,12 +243,11 @@ PcmConvert::ConvertFloat(const audio_format *src_format, libsamplerate */ if (src_format->sample_rate != dest_format->sample_rate) { - buffer = pcm_resample_float(&resample, - dest_format->channels, - src_format->sample_rate, - buffer, size, - dest_format->sample_rate, &size, - error_r); + buffer = resampler.ResampleFloat(dest_format->channels, + src_format->sample_rate, + buffer, size, + dest_format->sample_rate, + &size, error_r); if (buffer == NULL) return NULL; } diff --git a/src/pcm/PcmConvert.hxx b/src/pcm/PcmConvert.hxx index 62f67ed8e..1635a90c3 100644 --- a/src/pcm/PcmConvert.hxx +++ b/src/pcm/PcmConvert.hxx @@ -22,9 +22,9 @@ #include "PcmDither.hxx" #include "PcmDsd.hxx" +#include "PcmResample.hxx" extern "C" { -#include "pcm_resample.h" #include "pcm_buffer.h" } @@ -40,7 +40,7 @@ struct audio_format; class PcmConvert { PcmDsd dsd; - struct pcm_resample_state resample; + PcmResampler resampler; PcmDither dither; diff --git a/src/pcm/PcmResample.cxx b/src/pcm/PcmResample.cxx new file mode 100644 index 000000000..60265380e --- /dev/null +++ b/src/pcm/PcmResample.cxx @@ -0,0 +1,153 @@ +/* + * 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 "PcmResampleInternal.hxx" + +#ifdef HAVE_LIBSAMPLERATE +#include "conf.h" +#endif + +#include + +#ifdef HAVE_LIBSAMPLERATE +static bool lsr_enabled; +#endif + +#ifdef HAVE_LIBSAMPLERATE +static bool +pcm_resample_lsr_enabled(void) +{ + return lsr_enabled; +} +#endif + +bool +pcm_resample_global_init(GError **error_r) +{ +#ifdef HAVE_LIBSAMPLERATE + const char *converter = + config_get_string(CONF_SAMPLERATE_CONVERTER, ""); + + lsr_enabled = strcmp(converter, "internal") != 0; + if (lsr_enabled) + return pcm_resample_lsr_global_init(converter, error_r); + else + return true; +#else + (void)error_r; + return true; +#endif +} + +PcmResampler::PcmResampler() +{ +#ifdef HAVE_LIBSAMPLERATE + if (pcm_resample_lsr_enabled()) + pcm_resample_lsr_init(this); + else +#endif + pcm_resample_fallback_init(this); +} + +PcmResampler::~PcmResampler() +{ +#ifdef HAVE_LIBSAMPLERATE + if (pcm_resample_lsr_enabled()) + pcm_resample_lsr_deinit(this); + else +#endif + pcm_resample_fallback_deinit(this); +} + +void +PcmResampler::Reset() +{ +#ifdef HAVE_LIBSAMPLERATE + pcm_resample_lsr_reset(this); +#endif +} + +const float * +PcmResampler::ResampleFloat(unsigned channels, unsigned src_rate, + const float *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ +#ifdef HAVE_LIBSAMPLERATE + if (pcm_resample_lsr_enabled()) + return pcm_resample_lsr_float(this, channels, + src_rate, src_buffer, src_size, + dest_rate, dest_size_r, + error_r); +#else + (void)error_r; +#endif + + /* sizeof(float)==sizeof(int32_t); the fallback resampler does + not do any math on the sample values, so this hack is + possible: */ + return (const float *) + pcm_resample_fallback_32(this, channels, + src_rate, (const int32_t *)src_buffer, + src_size, + dest_rate, dest_size_r); +} + +const int16_t * +PcmResampler::Resample16(unsigned channels, + unsigned src_rate, const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ +#ifdef HAVE_LIBSAMPLERATE + if (pcm_resample_lsr_enabled()) + return pcm_resample_lsr_16(this, channels, + src_rate, src_buffer, src_size, + dest_rate, dest_size_r, + error_r); +#else + (void)error_r; +#endif + + return pcm_resample_fallback_16(this, channels, + src_rate, src_buffer, src_size, + dest_rate, dest_size_r); +} + +const int32_t * +PcmResampler::Resample32(unsigned channels, unsigned src_rate, + const int32_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ +#ifdef HAVE_LIBSAMPLERATE + if (pcm_resample_lsr_enabled()) + return pcm_resample_lsr_32(this, channels, + src_rate, src_buffer, src_size, + dest_rate, dest_size_r, + error_r); +#else + (void)error_r; +#endif + + return pcm_resample_fallback_32(this, channels, + src_rate, src_buffer, src_size, + dest_rate, dest_size_r); +} diff --git a/src/pcm/PcmResample.hxx b/src/pcm/PcmResample.hxx new file mode 100644 index 000000000..6aaa75014 --- /dev/null +++ b/src/pcm/PcmResample.hxx @@ -0,0 +1,138 @@ +/* + * 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_PCM_RESAMPLE_HXX +#define MPD_PCM_RESAMPLE_HXX + +#include "check.h" +#include "pcm_buffer.h" + +#include +#include +#include + +#ifdef HAVE_LIBSAMPLERATE +#include +#endif + +/** + * This object is statically allocated (within another struct), and + * holds buffer allocations and the state for the resampler. + */ +struct PcmResampler { +#ifdef HAVE_LIBSAMPLERATE + SRC_STATE *state; + SRC_DATA data; + + struct pcm_buffer in, out; + + struct { + unsigned src_rate; + unsigned dest_rate; + unsigned channels; + } prev; + + int error; +#endif + + struct pcm_buffer buffer; + + PcmResampler(); + ~PcmResampler(); + + /** + * @see pcm_convert_reset() + */ + void Reset(); + + /** + * Resamples 32 bit float data. + * + * @param channels the number of channels + * @param src_rate the source sample rate + * @param src the source PCM buffer + * @param src_size the size of #src in bytes + * @param dest_rate the requested destination sample rate + * @param dest_size_r returns the number of bytes of the destination buffer + * @return the destination buffer + */ + const float *ResampleFloat(unsigned channels, unsigned src_rate, + const float *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + + /** + * Resamples 16 bit PCM data. + * + * @param channels the number of channels + * @param src_rate the source sample rate + * @param src the source PCM buffer + * @param src_size the size of #src in bytes + * @param dest_rate the requested destination sample rate + * @param dest_size_r returns the number of bytes of the destination buffer + * @return the destination buffer + */ + const int16_t *Resample16(unsigned channels, unsigned src_rate, + const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + + /** + * Resamples 32 bit PCM data. + * + * @param channels the number of channels + * @param src_rate the source sample rate + * @param src the source PCM buffer + * @param src_size the size of #src in bytes + * @param dest_rate the requested destination sample rate + * @param dest_size_r returns the number of bytes of the destination buffer + * @return the destination buffer + */ + const int32_t *Resample32(unsigned channels, unsigned src_rate, + const int32_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + + /** + * Resamples 24 bit PCM data. + * + * @param channels the number of channels + * @param src_rate the source sample rate + * @param src the source PCM buffer + * @param src_size the size of #src in bytes + * @param dest_rate the requested destination sample rate + * @param dest_size_r returns the number of bytes of the destination buffer + * @return the destination buffer + */ + const int32_t *Resample24(unsigned channels, unsigned src_rate, + const int32_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) + { + /* reuse the 32 bit code - the resampler code doesn't care if + the upper 8 bits are actually used */ + return Resample32(channels, src_rate, src_buffer, src_size, + dest_rate, dest_size_r, error_r); + } +}; + +bool +pcm_resample_global_init(GError **error_r); + +#endif diff --git a/src/pcm/PcmResampleFallback.cxx b/src/pcm/PcmResampleFallback.cxx new file mode 100644 index 000000000..724894366 --- /dev/null +++ b/src/pcm/PcmResampleFallback.cxx @@ -0,0 +1,120 @@ +/* + * 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 "PcmResampleInternal.hxx" + +#include + +void +pcm_resample_fallback_init(PcmResampler *state) +{ + pcm_buffer_init(&state->buffer); +} + +void +pcm_resample_fallback_deinit(PcmResampler *state) +{ + pcm_buffer_deinit(&state->buffer); +} + +/* resampling code blatantly ripped from ESD */ +const int16_t * +pcm_resample_fallback_16(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, + size_t *dest_size_r) +{ + unsigned dest_pos = 0; + unsigned src_frames = src_size / channels / sizeof(*src_buffer); + unsigned dest_frames = + (src_frames * dest_rate + src_rate - 1) / src_rate; + unsigned dest_samples = dest_frames * channels; + size_t dest_size = dest_samples * sizeof(*src_buffer); + int16_t *dest_buffer = (int16_t *) + pcm_buffer_get(&state->buffer, dest_size); + + assert((src_size % (sizeof(*src_buffer) * channels)) == 0); + + switch (channels) { + case 1: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + + dest_buffer[dest_pos++] = src_buffer[src_pos]; + } + break; + case 2: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + src_pos &= ~1; + + dest_buffer[dest_pos++] = src_buffer[src_pos]; + dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; + } + break; + } + + *dest_size_r = dest_size; + return dest_buffer; +} + +const int32_t * +pcm_resample_fallback_32(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int32_t *src_buffer, size_t src_size, + unsigned dest_rate, + size_t *dest_size_r) +{ + unsigned dest_pos = 0; + unsigned src_frames = src_size / channels / sizeof(*src_buffer); + unsigned dest_frames = + (src_frames * dest_rate + src_rate - 1) / src_rate; + unsigned dest_samples = dest_frames * channels; + size_t dest_size = dest_samples * sizeof(*src_buffer); + int32_t *dest_buffer = (int32_t *) + pcm_buffer_get(&state->buffer, dest_size); + + assert((src_size % (sizeof(*src_buffer) * channels)) == 0); + + switch (channels) { + case 1: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + + dest_buffer[dest_pos++] = src_buffer[src_pos]; + } + break; + case 2: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + src_pos &= ~1; + + dest_buffer[dest_pos++] = src_buffer[src_pos]; + dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; + } + break; + } + + *dest_size_r = dest_size; + return dest_buffer; +} diff --git a/src/pcm/PcmResampleInternal.hxx b/src/pcm/PcmResampleInternal.hxx new file mode 100644 index 000000000..bae69fab4 --- /dev/null +++ b/src/pcm/PcmResampleInternal.hxx @@ -0,0 +1,97 @@ +/* + * 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. + */ + +/** \file + * + * Internal declarations for the pcm_resample library. The "internal" + * resampler is called "fallback" in the MPD source, so the file name + * of this header is somewhat unrelated to it. + */ + +#ifndef MPD_PCM_RESAMPLE_INTERNAL_HXX +#define MPD_PCM_RESAMPLE_INTERNAL_HXX + +#include "check.h" +#include "PcmResample.hxx" + +#ifdef HAVE_LIBSAMPLERATE + +bool +pcm_resample_lsr_global_init(const char *converter, GError **error_r); + +void +pcm_resample_lsr_init(PcmResampler *state); + +void +pcm_resample_lsr_deinit(PcmResampler *state); + +void +pcm_resample_lsr_reset(PcmResampler *state); + +const float * +pcm_resample_lsr_float(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const float *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + +const int16_t * +pcm_resample_lsr_16(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + +const int32_t * +pcm_resample_lsr_32(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int32_t *src_buffer, + G_GNUC_UNUSED size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r); + +#endif + +void +pcm_resample_fallback_init(PcmResampler *state); + +void +pcm_resample_fallback_deinit(PcmResampler *state); + +const int16_t * +pcm_resample_fallback_16(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, + size_t *dest_size_r); + +const int32_t * +pcm_resample_fallback_32(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int32_t *src_buffer, + G_GNUC_UNUSED size_t src_size, + unsigned dest_rate, + size_t *dest_size_r); + +#endif diff --git a/src/pcm/PcmResampleLibsamplerate.cxx b/src/pcm/PcmResampleLibsamplerate.cxx new file mode 100644 index 000000000..532c79c8d --- /dev/null +++ b/src/pcm/PcmResampleLibsamplerate.cxx @@ -0,0 +1,295 @@ +/* + * 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 "PcmResampleInternal.hxx" +#include "conf.h" + +#include + +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "pcm" + +static int lsr_converter = SRC_SINC_FASTEST; + +static inline GQuark +libsamplerate_quark(void) +{ + return g_quark_from_static_string("libsamplerate"); +} + +static bool +lsr_parse_converter(const char *s) +{ + assert(s != nullptr); + + if (*s == 0) + return true; + + char *endptr; + long l = strtol(s, &endptr, 10); + if (*endptr == 0 && src_get_name(l) != nullptr) { + lsr_converter = l; + return true; + } + + size_t length = strlen(s); + for (int i = 0;; ++i) { + const char *name = src_get_name(i); + if (name == nullptr) + break; + + if (g_ascii_strncasecmp(s, name, length) == 0) { + lsr_converter = i; + return true; + } + } + + return false; +} + +bool +pcm_resample_lsr_global_init(const char *converter, GError **error_r) +{ + if (!lsr_parse_converter(converter)) { + g_set_error(error_r, libsamplerate_quark(), 0, + "unknown samplerate converter '%s'", converter); + return false; + } + + g_debug("libsamplerate converter '%s'", + src_get_name(lsr_converter)); + + return true; +} + +void +pcm_resample_lsr_init(PcmResampler *state) +{ + memset(state, 0, sizeof(*state)); + + pcm_buffer_init(&state->in); + pcm_buffer_init(&state->out); + pcm_buffer_init(&state->buffer); +} + +void +pcm_resample_lsr_deinit(PcmResampler *state) +{ + if (state->state != nullptr) + state->state = src_delete(state->state); + + pcm_buffer_deinit(&state->in); + pcm_buffer_deinit(&state->out); + pcm_buffer_deinit(&state->buffer); +} + +void +pcm_resample_lsr_reset(PcmResampler *state) +{ + if (state->state != nullptr) + src_reset(state->state); +} + +static bool +pcm_resample_set(PcmResampler *state, + unsigned channels, unsigned src_rate, unsigned dest_rate, + GError **error_r) +{ + /* (re)set the state/ratio if the in or out format changed */ + if (channels == state->prev.channels && + src_rate == state->prev.src_rate && + dest_rate == state->prev.dest_rate) + return true; + + state->error = 0; + state->prev.channels = channels; + state->prev.src_rate = src_rate; + state->prev.dest_rate = dest_rate; + + if (state->state) + state->state = src_delete(state->state); + + int error; + state->state = src_new(lsr_converter, channels, &error); + if (!state->state) { + g_set_error(error_r, libsamplerate_quark(), state->error, + "libsamplerate initialization has failed: %s", + src_strerror(error)); + return false; + } + + SRC_DATA *data = &state->data; + data->src_ratio = (double)dest_rate / (double)src_rate; + g_debug("setting samplerate conversion ratio to %.2lf", + data->src_ratio); + src_set_ratio(state->state, data->src_ratio); + + return true; +} + +static bool +lsr_process(PcmResampler *state, GError **error_r) +{ + if (state->error == 0) + state->error = src_process(state->state, &state->data); + if (state->error) { + g_set_error(error_r, libsamplerate_quark(), state->error, + "libsamplerate has failed: %s", + src_strerror(state->error)); + return false; + } + + return true; +} + +const float * +pcm_resample_lsr_float(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const float *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ + SRC_DATA *data = &state->data; + + assert((src_size % (sizeof(*src_buffer) * channels)) == 0); + + if (!pcm_resample_set(state, channels, src_rate, dest_rate, error_r)) + return nullptr; + + + data->input_frames = src_size / sizeof(*src_buffer) / channels; + data->data_in = const_cast(src_buffer); + + data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; + size_t data_out_size = data->output_frames * sizeof(float) * channels; + data->data_out = (float *)pcm_buffer_get(&state->out, data_out_size); + + if (!lsr_process(state, error_r)) + return nullptr; + + *dest_size_r = data->output_frames_gen * + sizeof(*data->data_out) * channels; + return data->data_out; +} + +const int16_t * +pcm_resample_lsr_16(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int16_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ + SRC_DATA *data = &state->data; + + assert((src_size % (sizeof(*src_buffer) * channels)) == 0); + + if (!pcm_resample_set(state, channels, src_rate, dest_rate, + error_r)) + return nullptr; + + data->input_frames = src_size / sizeof(*src_buffer) / channels; + size_t data_in_size = data->input_frames * sizeof(float) * channels; + data->data_in = (float *)pcm_buffer_get(&state->in, data_in_size); + + data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; + size_t data_out_size = data->output_frames * sizeof(float) * channels; + data->data_out = (float *)pcm_buffer_get(&state->out, data_out_size); + + src_short_to_float_array(src_buffer, data->data_in, + data->input_frames * channels); + + if (!lsr_process(state, error_r)) + return nullptr; + + int16_t *dest_buffer; + *dest_size_r = data->output_frames_gen * + sizeof(*dest_buffer) * channels; + dest_buffer = (int16_t *)pcm_buffer_get(&state->buffer, *dest_size_r); + src_float_to_short_array(data->data_out, dest_buffer, + data->output_frames_gen * channels); + + return dest_buffer; +} + +#ifdef HAVE_LIBSAMPLERATE_NOINT + +/* libsamplerate introduced these functions in v0.1.3 */ + +static void +src_int_to_float_array(const int *in, float *out, int len) +{ + while (len-- > 0) + *out++ = *in++ / (float)(1 << (24 - 1)); +} + +static void +src_float_to_int_array (const float *in, int *out, int len) +{ + while (len-- > 0) + *out++ = *in++ * (float)(1 << (24 - 1)); +} + +#endif + +const int32_t * +pcm_resample_lsr_32(PcmResampler *state, + unsigned channels, + unsigned src_rate, + const int32_t *src_buffer, size_t src_size, + unsigned dest_rate, size_t *dest_size_r, + GError **error_r) +{ + SRC_DATA *data = &state->data; + + assert((src_size % (sizeof(*src_buffer) * channels)) == 0); + + if (!pcm_resample_set(state, channels, src_rate, dest_rate, + error_r)) + return nullptr; + + data->input_frames = src_size / sizeof(*src_buffer) / channels; + size_t data_in_size = data->input_frames * sizeof(float) * channels; + data->data_in = (float *)pcm_buffer_get(&state->in, data_in_size); + + data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; + size_t data_out_size = data->output_frames * sizeof(float) * channels; + data->data_out = (float *)pcm_buffer_get(&state->out, data_out_size); + + src_int_to_float_array(src_buffer, data->data_in, + data->input_frames * channels); + + if (!lsr_process(state, error_r)) + return nullptr; + + int32_t *dest_buffer; + *dest_size_r = data->output_frames_gen * + sizeof(*dest_buffer) * channels; + dest_buffer = (int32_t *)pcm_buffer_get(&state->buffer, *dest_size_r); + src_float_to_int_array(data->data_out, dest_buffer, + data->output_frames_gen * channels); + + return dest_buffer; +} diff --git a/src/pcm/pcm_resample.c b/src/pcm/pcm_resample.c deleted file mode 100644 index 4bc057a7e..000000000 --- a/src/pcm/pcm_resample.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "pcm_resample_internal.h" - -#ifdef HAVE_LIBSAMPLERATE -#include "conf.h" -#endif - -#include - -#ifdef HAVE_LIBSAMPLERATE -static bool lsr_enabled; -#endif - -#ifdef HAVE_LIBSAMPLERATE -static bool -pcm_resample_lsr_enabled(void) -{ - return lsr_enabled; -} -#endif - -bool -pcm_resample_global_init(GError **error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - const char *converter = - config_get_string(CONF_SAMPLERATE_CONVERTER, ""); - - lsr_enabled = strcmp(converter, "internal") != 0; - if (lsr_enabled) - return pcm_resample_lsr_global_init(converter, error_r); - else - return true; -#else - (void)error_r; - return true; -#endif -} - -void pcm_resample_init(struct pcm_resample_state *state) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_init(state); - else -#endif - pcm_resample_fallback_init(state); -} - -void pcm_resample_deinit(struct pcm_resample_state *state) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_deinit(state); - else -#endif - pcm_resample_fallback_deinit(state); -} - -void -pcm_resample_reset(struct pcm_resample_state *state) -{ -#ifdef HAVE_LIBSAMPLERATE - pcm_resample_lsr_reset(state); -#else - (void)state; -#endif -} - -const float * -pcm_resample_float(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_float(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - /* sizeof(float)==sizeof(int32_t); the fallback resampler does - not do any math on the sample values, so this hack is - possible: */ - return (const float *) - pcm_resample_fallback_32(state, channels, - src_rate, (const int32_t *)src_buffer, - src_size, - dest_rate, dest_size_r); -} - -const int16_t * -pcm_resample_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_16(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - return pcm_resample_fallback_16(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r); -} - -const int32_t * -pcm_resample_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_32(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - return pcm_resample_fallback_32(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r); -} diff --git a/src/pcm/pcm_resample.h b/src/pcm/pcm_resample.h deleted file mode 100644 index a49a24142..000000000 --- a/src/pcm/pcm_resample.h +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (C) 2003-2011 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_PCM_RESAMPLE_H -#define MPD_PCM_RESAMPLE_H - -#include "check.h" -#include "pcm_buffer.h" - -#include -#include -#include - -#ifdef HAVE_LIBSAMPLERATE -#include -#endif - -/** - * This object is statically allocated (within another struct), and - * holds buffer allocations and the state for the resampler. - */ -struct pcm_resample_state { -#ifdef HAVE_LIBSAMPLERATE - SRC_STATE *state; - SRC_DATA data; - - struct pcm_buffer in, out; - - struct { - unsigned src_rate; - unsigned dest_rate; - unsigned channels; - } prev; - - int error; -#endif - - struct pcm_buffer buffer; -}; - -bool -pcm_resample_global_init(GError **error_r); - -/** - * Initializes a pcm_resample_state object. - */ -void pcm_resample_init(struct pcm_resample_state *state); - -/** - * Deinitializes a pcm_resample_state object and frees allocated - * memory. - */ -void pcm_resample_deinit(struct pcm_resample_state *state); - -/** - * @see pcm_convert_reset() - */ -void -pcm_resample_reset(struct pcm_resample_state *state); - -/** - * Resamples 32 bit float data. - * - * @param state an initialized pcm_resample_state object - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ -const float * -pcm_resample_float(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -/** - * Resamples 16 bit PCM data. - * - * @param state an initialized pcm_resample_state object - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ -const int16_t * -pcm_resample_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -/** - * Resamples 32 bit PCM data. - * - * @param state an initialized pcm_resample_state object - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ -const int32_t * -pcm_resample_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -/** - * Resamples 24 bit PCM data. - * - * @param state an initialized pcm_resample_state object - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ -static inline const int32_t * -pcm_resample_24(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ - /* reuse the 32 bit code - the resampler code doesn't care if - the upper 8 bits are actually used */ - return pcm_resample_32(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, error_r); -} - -#endif diff --git a/src/pcm/pcm_resample_fallback.c b/src/pcm/pcm_resample_fallback.c deleted file mode 100644 index 1d1dfdf59..000000000 --- a/src/pcm/pcm_resample_fallback.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "pcm_resample_internal.h" - -#include - -void -pcm_resample_fallback_init(struct pcm_resample_state *state) -{ - pcm_buffer_init(&state->buffer); -} - -void -pcm_resample_fallback_deinit(struct pcm_resample_state *state) -{ - pcm_buffer_deinit(&state->buffer); -} - -/* resampling code blatantly ripped from ESD */ -const int16_t * -pcm_resample_fallback_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r) -{ - unsigned src_pos, dest_pos = 0; - unsigned src_frames = src_size / channels / sizeof(*src_buffer); - unsigned dest_frames = - (src_frames * dest_rate + src_rate - 1) / src_rate; - unsigned dest_samples = dest_frames * channels; - size_t dest_size = dest_samples * sizeof(*src_buffer); - int16_t *dest_buffer = pcm_buffer_get(&state->buffer, dest_size); - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - switch (channels) { - case 1: - while (dest_pos < dest_samples) { - src_pos = dest_pos * src_rate / dest_rate; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - } - break; - case 2: - while (dest_pos < dest_samples) { - src_pos = dest_pos * src_rate / dest_rate; - src_pos &= ~1; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; - } - break; - } - - *dest_size_r = dest_size; - return dest_buffer; -} - -const int32_t * -pcm_resample_fallback_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r) -{ - unsigned src_pos, dest_pos = 0; - unsigned src_frames = src_size / channels / sizeof(*src_buffer); - unsigned dest_frames = - (src_frames * dest_rate + src_rate - 1) / src_rate; - unsigned dest_samples = dest_frames * channels; - size_t dest_size = dest_samples * sizeof(*src_buffer); - int32_t *dest_buffer = pcm_buffer_get(&state->buffer, dest_size); - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - switch (channels) { - case 1: - while (dest_pos < dest_samples) { - src_pos = dest_pos * src_rate / dest_rate; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - } - break; - case 2: - while (dest_pos < dest_samples) { - src_pos = dest_pos * src_rate / dest_rate; - src_pos &= ~1; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; - } - break; - } - - *dest_size_r = dest_size; - return dest_buffer; -} diff --git a/src/pcm/pcm_resample_internal.h b/src/pcm/pcm_resample_internal.h deleted file mode 100644 index a0e108d4b..000000000 --- a/src/pcm/pcm_resample_internal.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2003-2011 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. - */ - -/** \file - * - * Internal declarations for the pcm_resample library. The "internal" - * resampler is called "fallback" in the MPD source, so the file name - * of this header is somewhat unrelated to it. - */ - -#ifndef MPD_PCM_RESAMPLE_INTERNAL_H -#define MPD_PCM_RESAMPLE_INTERNAL_H - -#include "check.h" -#include "pcm_resample.h" - -#ifdef HAVE_LIBSAMPLERATE - -bool -pcm_resample_lsr_global_init(const char *converter, GError **error_r); - -void -pcm_resample_lsr_init(struct pcm_resample_state *state); - -void -pcm_resample_lsr_deinit(struct pcm_resample_state *state); - -void -pcm_resample_lsr_reset(struct pcm_resample_state *state); - -const float * -pcm_resample_lsr_float(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -const int16_t * -pcm_resample_lsr_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -const int32_t * -pcm_resample_lsr_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, - G_GNUC_UNUSED size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r); - -#endif - -void -pcm_resample_fallback_init(struct pcm_resample_state *state); - -void -pcm_resample_fallback_deinit(struct pcm_resample_state *state); - -const int16_t * -pcm_resample_fallback_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r); - -const int32_t * -pcm_resample_fallback_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, - G_GNUC_UNUSED size_t src_size, - unsigned dest_rate, - size_t *dest_size_r); - -#endif diff --git a/src/pcm/pcm_resample_libsamplerate.c b/src/pcm/pcm_resample_libsamplerate.c deleted file mode 100644 index f957e5155..000000000 --- a/src/pcm/pcm_resample_libsamplerate.c +++ /dev/null @@ -1,312 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "pcm_resample_internal.h" -#include "conf.h" - -#include - -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "pcm" - -static int lsr_converter = SRC_SINC_FASTEST; - -static inline GQuark -libsamplerate_quark(void) -{ - return g_quark_from_static_string("libsamplerate"); -} - -static bool -lsr_parse_converter(const char *s) -{ - assert(s != NULL); - - if (*s == 0) - return true; - - char *endptr; - long l = strtol(s, &endptr, 10); - if (*endptr == 0 && src_get_name(l) != NULL) { - lsr_converter = l; - return true; - } - - size_t length = strlen(s); - for (int i = 0;; ++i) { - const char *name = src_get_name(i); - if (name == NULL) - break; - - if (g_ascii_strncasecmp(s, name, length) == 0) { - lsr_converter = i; - return true; - } - } - - return false; -} - -bool -pcm_resample_lsr_global_init(const char *converter, GError **error_r) -{ - if (!lsr_parse_converter(converter)) { - g_set_error(error_r, libsamplerate_quark(), 0, - "unknown samplerate converter '%s'", converter); - return false; - } - - g_debug("libsamplerate converter '%s'", - src_get_name(lsr_converter)); - - return true; -} - -void -pcm_resample_lsr_init(struct pcm_resample_state *state) -{ - memset(state, 0, sizeof(*state)); - - pcm_buffer_init(&state->in); - pcm_buffer_init(&state->out); - pcm_buffer_init(&state->buffer); -} - -void -pcm_resample_lsr_deinit(struct pcm_resample_state *state) -{ - if (state->state != NULL) - state->state = src_delete(state->state); - - pcm_buffer_deinit(&state->in); - pcm_buffer_deinit(&state->out); - pcm_buffer_deinit(&state->buffer); -} - -void -pcm_resample_lsr_reset(struct pcm_resample_state *state) -{ - if (state->state != NULL) - src_reset(state->state); -} - -static bool -pcm_resample_set(struct pcm_resample_state *state, - unsigned channels, unsigned src_rate, unsigned dest_rate, - GError **error_r) -{ - int error; - SRC_DATA *data = &state->data; - - /* (re)set the state/ratio if the in or out format changed */ - if (channels == state->prev.channels && - src_rate == state->prev.src_rate && - dest_rate == state->prev.dest_rate) - return true; - - state->error = 0; - state->prev.channels = channels; - state->prev.src_rate = src_rate; - state->prev.dest_rate = dest_rate; - - if (state->state) - state->state = src_delete(state->state); - - state->state = src_new(lsr_converter, channels, &error); - if (!state->state) { - g_set_error(error_r, libsamplerate_quark(), state->error, - "libsamplerate initialization has failed: %s", - src_strerror(error)); - return false; - } - - data->src_ratio = (double)dest_rate / (double)src_rate; - g_debug("setting samplerate conversion ratio to %.2lf", - data->src_ratio); - src_set_ratio(state->state, data->src_ratio); - - return true; -} - -static bool -lsr_process(struct pcm_resample_state *state, GError **error_r) -{ - if (state->error == 0) - state->error = src_process(state->state, &state->data); - if (state->error) { - g_set_error(error_r, libsamplerate_quark(), state->error, - "libsamplerate has failed: %s", - src_strerror(state->error)); - return false; - } - - return true; -} - -static float * -deconst_float_buffer(const float *in) -{ - union { - const float *in; - float *out; - } u = { .in = in }; - return u.out; -} - -const float * -pcm_resample_lsr_float(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - if (!pcm_resample_set(state, channels, src_rate, dest_rate, error_r)) - return NULL; - - SRC_DATA *data = &state->data; - data->input_frames = src_size / sizeof(*src_buffer) / channels; - data->data_in = deconst_float_buffer(src_buffer); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - size_t data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = pcm_buffer_get(&state->out, data_out_size); - - if (!lsr_process(state, error_r)) - return NULL; - - *dest_size_r = data->output_frames_gen * - sizeof(*data->data_out) * channels; - return data->data_out; -} - -const int16_t * -pcm_resample_lsr_16(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ - bool success; - SRC_DATA *data = &state->data; - size_t data_in_size; - size_t data_out_size; - int16_t *dest_buffer; - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - success = pcm_resample_set(state, channels, src_rate, dest_rate, - error_r); - if (!success) - return NULL; - - data->input_frames = src_size / sizeof(*src_buffer) / channels; - data_in_size = data->input_frames * sizeof(float) * channels; - data->data_in = pcm_buffer_get(&state->in, data_in_size); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = pcm_buffer_get(&state->out, data_out_size); - - src_short_to_float_array(src_buffer, data->data_in, - data->input_frames * channels); - - if (!lsr_process(state, error_r)) - return NULL; - - *dest_size_r = data->output_frames_gen * - sizeof(*dest_buffer) * channels; - dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r); - src_float_to_short_array(data->data_out, dest_buffer, - data->output_frames_gen * channels); - - return dest_buffer; -} - -#ifdef HAVE_LIBSAMPLERATE_NOINT - -/* libsamplerate introduced these functions in v0.1.3 */ - -static void -src_int_to_float_array(const int *in, float *out, int len) -{ - while (len-- > 0) - *out++ = *in++ / (float)(1 << (24 - 1)); -} - -static void -src_float_to_int_array (const float *in, int *out, int len) -{ - while (len-- > 0) - *out++ = *in++ * (float)(1 << (24 - 1)); -} - -#endif - -const int32_t * -pcm_resample_lsr_32(struct pcm_resample_state *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - GError **error_r) -{ - bool success; - SRC_DATA *data = &state->data; - size_t data_in_size; - size_t data_out_size; - int32_t *dest_buffer; - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - success = pcm_resample_set(state, channels, src_rate, dest_rate, - error_r); - if (!success) - return NULL; - - data->input_frames = src_size / sizeof(*src_buffer) / channels; - data_in_size = data->input_frames * sizeof(float) * channels; - data->data_in = pcm_buffer_get(&state->in, data_in_size); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = pcm_buffer_get(&state->out, data_out_size); - - src_int_to_float_array(src_buffer, data->data_in, - data->input_frames * channels); - - if (!lsr_process(state, error_r)) - return NULL; - - *dest_size_r = data->output_frames_gen * - sizeof(*dest_buffer) * channels; - dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r); - src_float_to_int_array(data->data_out, dest_buffer, - data->output_frames_gen * channels); - - return dest_buffer; -} -- cgit v1.2.3