diff --git a/Makefile.am b/Makefile.am index bb29a8f19..7714c44b1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -314,9 +314,9 @@ libpcm_a_SOURCES = \ src/pcm/PcmChannels.cxx src/pcm/PcmChannels.hxx \ src/pcm/pcm_pack.c src/pcm/pcm_pack.h \ src/pcm/PcmFormat.cxx src/pcm/PcmFormat.hxx \ - src/pcm/pcm_resample.c src/pcm/pcm_resample.h \ - src/pcm/pcm_resample_fallback.c \ - src/pcm/pcm_resample_internal.h \ + src/pcm/PcmResample.cxx src/pcm/PcmResample.hxx \ + src/pcm/PcmResampleFallback.cxx \ + src/pcm/PcmResampleInternal.hxx \ src/pcm/PcmDither.cxx src/pcm/PcmDither.hxx \ src/pcm/PcmPrng.hxx \ src/pcm/PcmUtils.hxx @@ -328,7 +328,7 @@ PCM_LIBS = \ $(SAMPLERATE_LIBS) if HAVE_LIBSAMPLERATE -libpcm_a_SOURCES += src/pcm/pcm_resample_libsamplerate.c +libpcm_a_SOURCES += src/pcm/PcmResampleLibsamplerate.cxx endif # File system library diff --git a/src/Main.cxx b/src/Main.cxx index aa0a687fd..ff957c1ee 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -53,11 +53,11 @@ #include "ZeroconfGlue.hxx" #include "DecoderList.hxx" #include "AudioConfig.hxx" +#include "pcm/PcmResample.hxx" extern "C" { #include "daemon.h" #include "stats.h" -#include "pcm/pcm_resample.h" } #include "mpd_error.h" 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/pcm_resample.c b/src/pcm/PcmResample.cxx similarity index 65% rename from src/pcm/pcm_resample.c rename to src/pcm/PcmResample.cxx index 4bc057a7e..60265380e 100644 --- a/src/pcm/pcm_resample.c +++ b/src/pcm/PcmResample.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,7 +18,7 @@ */ #include "config.h" -#include "pcm_resample_internal.h" +#include "PcmResampleInternal.hxx" #ifdef HAVE_LIBSAMPLERATE #include "conf.h" @@ -56,47 +56,43 @@ pcm_resample_global_init(GError **error_r) #endif } -void pcm_resample_init(struct pcm_resample_state *state) +PcmResampler::PcmResampler() { #ifdef HAVE_LIBSAMPLERATE if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_init(state); + pcm_resample_lsr_init(this); else #endif - pcm_resample_fallback_init(state); + pcm_resample_fallback_init(this); } -void pcm_resample_deinit(struct pcm_resample_state *state) +PcmResampler::~PcmResampler() { #ifdef HAVE_LIBSAMPLERATE if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_deinit(state); + pcm_resample_lsr_deinit(this); else #endif - pcm_resample_fallback_deinit(state); + pcm_resample_fallback_deinit(this); } void -pcm_resample_reset(struct pcm_resample_state *state) +PcmResampler::Reset() { #ifdef HAVE_LIBSAMPLERATE - pcm_resample_lsr_reset(state); -#else - (void)state; + pcm_resample_lsr_reset(this); #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) +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(state, channels, + return pcm_resample_lsr_float(this, channels, src_rate, src_buffer, src_size, dest_rate, dest_size_r, error_r); @@ -108,22 +104,21 @@ pcm_resample_float(struct pcm_resample_state *state, not do any math on the sample values, so this hack is possible: */ return (const float *) - pcm_resample_fallback_32(state, channels, + pcm_resample_fallback_32(this, 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) +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(state, channels, + return pcm_resample_lsr_16(this, channels, src_rate, src_buffer, src_size, dest_rate, dest_size_r, error_r); @@ -131,21 +126,20 @@ pcm_resample_16(struct pcm_resample_state *state, (void)error_r; #endif - return pcm_resample_fallback_16(state, channels, + return pcm_resample_fallback_16(this, 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) +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(state, channels, + return pcm_resample_lsr_32(this, channels, src_rate, src_buffer, src_size, dest_rate, dest_size_r, error_r); @@ -153,7 +147,7 @@ pcm_resample_32(struct pcm_resample_state *state, (void)error_r; #endif - return pcm_resample_fallback_32(state, channels, + 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/pcm_resample_fallback.c b/src/pcm/PcmResampleFallback.cxx similarity index 78% rename from src/pcm/pcm_resample_fallback.c rename to src/pcm/PcmResampleFallback.cxx index 1d1dfdf59..724894366 100644 --- a/src/pcm/pcm_resample_fallback.c +++ b/src/pcm/PcmResampleFallback.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,52 +18,53 @@ */ #include "config.h" -#include "pcm_resample_internal.h" +#include "PcmResampleInternal.hxx" #include void -pcm_resample_fallback_init(struct pcm_resample_state *state) +pcm_resample_fallback_init(PcmResampler *state) { pcm_buffer_init(&state->buffer); } void -pcm_resample_fallback_deinit(struct pcm_resample_state *state) +pcm_resample_fallback_deinit(PcmResampler *state) { pcm_buffer_deinit(&state->buffer); } /* resampling code blatantly ripped from ESD */ const int16_t * -pcm_resample_fallback_16(struct pcm_resample_state *state, +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 src_pos, dest_pos = 0; + 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 = pcm_buffer_get(&state->buffer, dest_size); + 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) { - src_pos = dest_pos * src_rate / dest_rate; + 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) { - src_pos = dest_pos * src_rate / dest_rate; + unsigned src_pos = dest_pos * src_rate / dest_rate; src_pos &= ~1; dest_buffer[dest_pos++] = src_buffer[src_pos]; @@ -77,34 +78,35 @@ pcm_resample_fallback_16(struct pcm_resample_state *state, } const int32_t * -pcm_resample_fallback_32(struct pcm_resample_state *state, +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 src_pos, dest_pos = 0; + 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 = pcm_buffer_get(&state->buffer, dest_size); + 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) { - src_pos = dest_pos * src_rate / dest_rate; + 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) { - src_pos = dest_pos * src_rate / dest_rate; + unsigned src_pos = dest_pos * src_rate / dest_rate; src_pos &= ~1; dest_buffer[dest_pos++] = src_buffer[src_pos]; diff --git a/src/pcm/pcm_resample_internal.h b/src/pcm/PcmResampleInternal.hxx similarity index 73% rename from src/pcm/pcm_resample_internal.h rename to src/pcm/PcmResampleInternal.hxx index a0e108d4b..bae69fab4 100644 --- a/src/pcm/pcm_resample_internal.h +++ b/src/pcm/PcmResampleInternal.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -24,11 +24,11 @@ * of this header is somewhat unrelated to it. */ -#ifndef MPD_PCM_RESAMPLE_INTERNAL_H -#define MPD_PCM_RESAMPLE_INTERNAL_H +#ifndef MPD_PCM_RESAMPLE_INTERNAL_HXX +#define MPD_PCM_RESAMPLE_INTERNAL_HXX #include "check.h" -#include "pcm_resample.h" +#include "PcmResample.hxx" #ifdef HAVE_LIBSAMPLERATE @@ -36,16 +36,16 @@ bool pcm_resample_lsr_global_init(const char *converter, GError **error_r); void -pcm_resample_lsr_init(struct pcm_resample_state *state); +pcm_resample_lsr_init(PcmResampler *state); void -pcm_resample_lsr_deinit(struct pcm_resample_state *state); +pcm_resample_lsr_deinit(PcmResampler *state); void -pcm_resample_lsr_reset(struct pcm_resample_state *state); +pcm_resample_lsr_reset(PcmResampler *state); const float * -pcm_resample_lsr_float(struct pcm_resample_state *state, +pcm_resample_lsr_float(PcmResampler *state, unsigned channels, unsigned src_rate, const float *src_buffer, size_t src_size, @@ -53,7 +53,7 @@ pcm_resample_lsr_float(struct pcm_resample_state *state, GError **error_r); const int16_t * -pcm_resample_lsr_16(struct pcm_resample_state *state, +pcm_resample_lsr_16(PcmResampler *state, unsigned channels, unsigned src_rate, const int16_t *src_buffer, size_t src_size, @@ -61,7 +61,7 @@ pcm_resample_lsr_16(struct pcm_resample_state *state, GError **error_r); const int32_t * -pcm_resample_lsr_32(struct pcm_resample_state *state, +pcm_resample_lsr_32(PcmResampler *state, unsigned channels, unsigned src_rate, const int32_t *src_buffer, @@ -72,13 +72,13 @@ pcm_resample_lsr_32(struct pcm_resample_state *state, #endif void -pcm_resample_fallback_init(struct pcm_resample_state *state); +pcm_resample_fallback_init(PcmResampler *state); void -pcm_resample_fallback_deinit(struct pcm_resample_state *state); +pcm_resample_fallback_deinit(PcmResampler *state); const int16_t * -pcm_resample_fallback_16(struct pcm_resample_state *state, +pcm_resample_fallback_16(PcmResampler *state, unsigned channels, unsigned src_rate, const int16_t *src_buffer, size_t src_size, @@ -86,7 +86,7 @@ pcm_resample_fallback_16(struct pcm_resample_state *state, size_t *dest_size_r); const int32_t * -pcm_resample_fallback_32(struct pcm_resample_state *state, +pcm_resample_fallback_32(PcmResampler *state, unsigned channels, unsigned src_rate, const int32_t *src_buffer, diff --git a/src/pcm/pcm_resample_libsamplerate.c b/src/pcm/PcmResampleLibsamplerate.cxx similarity index 75% rename from src/pcm/pcm_resample_libsamplerate.c rename to src/pcm/PcmResampleLibsamplerate.cxx index f957e5155..532c79c8d 100644 --- a/src/pcm/pcm_resample_libsamplerate.c +++ b/src/pcm/PcmResampleLibsamplerate.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,7 +18,7 @@ */ #include "config.h" -#include "pcm_resample_internal.h" +#include "PcmResampleInternal.hxx" #include "conf.h" #include @@ -41,14 +41,14 @@ libsamplerate_quark(void) static bool lsr_parse_converter(const char *s) { - assert(s != NULL); + assert(s != nullptr); if (*s == 0) return true; char *endptr; long l = strtol(s, &endptr, 10); - if (*endptr == 0 && src_get_name(l) != NULL) { + if (*endptr == 0 && src_get_name(l) != nullptr) { lsr_converter = l; return true; } @@ -56,7 +56,7 @@ lsr_parse_converter(const char *s) size_t length = strlen(s); for (int i = 0;; ++i) { const char *name = src_get_name(i); - if (name == NULL) + if (name == nullptr) break; if (g_ascii_strncasecmp(s, name, length) == 0) { @@ -84,7 +84,7 @@ pcm_resample_lsr_global_init(const char *converter, GError **error_r) } void -pcm_resample_lsr_init(struct pcm_resample_state *state) +pcm_resample_lsr_init(PcmResampler *state) { memset(state, 0, sizeof(*state)); @@ -94,9 +94,9 @@ pcm_resample_lsr_init(struct pcm_resample_state *state) } void -pcm_resample_lsr_deinit(struct pcm_resample_state *state) +pcm_resample_lsr_deinit(PcmResampler *state) { - if (state->state != NULL) + if (state->state != nullptr) state->state = src_delete(state->state); pcm_buffer_deinit(&state->in); @@ -105,20 +105,17 @@ pcm_resample_lsr_deinit(struct pcm_resample_state *state) } void -pcm_resample_lsr_reset(struct pcm_resample_state *state) +pcm_resample_lsr_reset(PcmResampler *state) { - if (state->state != NULL) + if (state->state != nullptr) src_reset(state->state); } static bool -pcm_resample_set(struct pcm_resample_state *state, +pcm_resample_set(PcmResampler *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 && @@ -133,6 +130,7 @@ pcm_resample_set(struct pcm_resample_state *state, 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, @@ -141,6 +139,7 @@ pcm_resample_set(struct pcm_resample_state *state, 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); @@ -150,7 +149,7 @@ pcm_resample_set(struct pcm_resample_state *state, } static bool -lsr_process(struct pcm_resample_state *state, GError **error_r) +lsr_process(PcmResampler *state, GError **error_r) { if (state->error == 0) state->error = src_process(state->state, &state->data); @@ -164,39 +163,31 @@ lsr_process(struct pcm_resample_state *state, GError **error_r) 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, +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 NULL; + return nullptr; + - SRC_DATA *data = &state->data; data->input_frames = src_size / sizeof(*src_buffer) / channels; - data->data_in = deconst_float_buffer(src_buffer); + 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 = pcm_buffer_get(&state->out, data_out_size); + data->data_out = (float *)pcm_buffer_get(&state->out, data_out_size); if (!lsr_process(state, error_r)) - return NULL; + return nullptr; *dest_size_r = data->output_frames_gen * sizeof(*data->data_out) * channels; @@ -204,43 +195,39 @@ pcm_resample_lsr_float(struct pcm_resample_state *state, } const int16_t * -pcm_resample_lsr_16(struct pcm_resample_state *state, +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) { - 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; + if (!pcm_resample_set(state, channels, src_rate, dest_rate, + error_r)) + return nullptr; 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); + 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; - data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = pcm_buffer_get(&state->out, data_out_size); + 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 NULL; + return nullptr; + int16_t *dest_buffer; *dest_size_r = data->output_frames_gen * sizeof(*dest_buffer) * channels; - dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r); + 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); @@ -268,43 +255,39 @@ src_float_to_int_array (const float *in, int *out, int len) #endif const int32_t * -pcm_resample_lsr_32(struct pcm_resample_state *state, +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) { - 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; + if (!pcm_resample_set(state, channels, src_rate, dest_rate, + error_r)) + return nullptr; 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); + 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; - data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = pcm_buffer_get(&state->out, data_out_size); + 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 NULL; + return nullptr; + int32_t *dest_buffer; *dest_size_r = data->output_frames_gen * sizeof(*dest_buffer) * channels; - dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r); + 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); 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