pcm_resample: convert to C++

This commit is contained in:
Max Kellermann 2013-07-29 23:18:55 +02:00
parent 46b9388bb0
commit a9d2dc6144
10 changed files with 265 additions and 320 deletions

View File

@ -314,9 +314,9 @@ libpcm_a_SOURCES = \
src/pcm/PcmChannels.cxx src/pcm/PcmChannels.hxx \ src/pcm/PcmChannels.cxx src/pcm/PcmChannels.hxx \
src/pcm/pcm_pack.c src/pcm/pcm_pack.h \ src/pcm/pcm_pack.c src/pcm/pcm_pack.h \
src/pcm/PcmFormat.cxx src/pcm/PcmFormat.hxx \ src/pcm/PcmFormat.cxx src/pcm/PcmFormat.hxx \
src/pcm/pcm_resample.c src/pcm/pcm_resample.h \ src/pcm/PcmResample.cxx src/pcm/PcmResample.hxx \
src/pcm/pcm_resample_fallback.c \ src/pcm/PcmResampleFallback.cxx \
src/pcm/pcm_resample_internal.h \ src/pcm/PcmResampleInternal.hxx \
src/pcm/PcmDither.cxx src/pcm/PcmDither.hxx \ src/pcm/PcmDither.cxx src/pcm/PcmDither.hxx \
src/pcm/PcmPrng.hxx \ src/pcm/PcmPrng.hxx \
src/pcm/PcmUtils.hxx src/pcm/PcmUtils.hxx
@ -328,7 +328,7 @@ PCM_LIBS = \
$(SAMPLERATE_LIBS) $(SAMPLERATE_LIBS)
if HAVE_LIBSAMPLERATE if HAVE_LIBSAMPLERATE
libpcm_a_SOURCES += src/pcm/pcm_resample_libsamplerate.c libpcm_a_SOURCES += src/pcm/PcmResampleLibsamplerate.cxx
endif endif
# File system library # File system library

View File

@ -53,11 +53,11 @@
#include "ZeroconfGlue.hxx" #include "ZeroconfGlue.hxx"
#include "DecoderList.hxx" #include "DecoderList.hxx"
#include "AudioConfig.hxx" #include "AudioConfig.hxx"
#include "pcm/PcmResample.hxx"
extern "C" { extern "C" {
#include "daemon.h" #include "daemon.h"
#include "stats.h" #include "stats.h"
#include "pcm/pcm_resample.h"
} }
#include "mpd_error.h" #include "mpd_error.h"

View File

@ -34,16 +34,12 @@
PcmConvert::PcmConvert() PcmConvert::PcmConvert()
{ {
pcm_resample_init(&resample);
pcm_buffer_init(&format_buffer); pcm_buffer_init(&format_buffer);
pcm_buffer_init(&channels_buffer); pcm_buffer_init(&channels_buffer);
} }
PcmConvert::~PcmConvert() PcmConvert::~PcmConvert()
{ {
pcm_resample_deinit(&resample);
pcm_buffer_deinit(&format_buffer); pcm_buffer_deinit(&format_buffer);
pcm_buffer_deinit(&channels_buffer); pcm_buffer_deinit(&channels_buffer);
} }
@ -52,7 +48,7 @@ void
PcmConvert::Reset() PcmConvert::Reset()
{ {
dsd.Reset(); dsd.Reset();
pcm_resample_reset(&resample); resampler.Reset();
} }
inline const int16_t * inline const int16_t *
@ -93,8 +89,7 @@ PcmConvert::Convert16(const audio_format *src_format,
} }
if (src_format->sample_rate != dest_format->sample_rate) { if (src_format->sample_rate != dest_format->sample_rate) {
buf = pcm_resample_16(&resample, buf = resampler.Resample16(dest_format->channels,
dest_format->channels,
src_format->sample_rate, buf, len, src_format->sample_rate, buf, len,
dest_format->sample_rate, &len, dest_format->sample_rate, &len,
error_r); error_r);
@ -143,8 +138,7 @@ PcmConvert::Convert24(const audio_format *src_format,
} }
if (src_format->sample_rate != dest_format->sample_rate) { if (src_format->sample_rate != dest_format->sample_rate) {
buf = pcm_resample_24(&resample, buf = resampler.Resample24(dest_format->channels,
dest_format->channels,
src_format->sample_rate, buf, len, src_format->sample_rate, buf, len,
dest_format->sample_rate, &len, dest_format->sample_rate, &len,
error_r); error_r);
@ -193,8 +187,7 @@ PcmConvert::Convert32(const audio_format *src_format,
} }
if (src_format->sample_rate != dest_format->sample_rate) { if (src_format->sample_rate != dest_format->sample_rate) {
buf = pcm_resample_32(&resample, buf = resampler.Resample32(dest_format->channels,
dest_format->channels,
src_format->sample_rate, buf, len, src_format->sample_rate, buf, len,
dest_format->sample_rate, &len, dest_format->sample_rate, &len,
error_r); error_r);
@ -250,12 +243,11 @@ PcmConvert::ConvertFloat(const audio_format *src_format,
libsamplerate */ libsamplerate */
if (src_format->sample_rate != dest_format->sample_rate) { if (src_format->sample_rate != dest_format->sample_rate) {
buffer = pcm_resample_float(&resample, buffer = resampler.ResampleFloat(dest_format->channels,
dest_format->channels,
src_format->sample_rate, src_format->sample_rate,
buffer, size, buffer, size,
dest_format->sample_rate, &size, dest_format->sample_rate,
error_r); &size, error_r);
if (buffer == NULL) if (buffer == NULL)
return NULL; return NULL;
} }

View File

@ -22,9 +22,9 @@
#include "PcmDither.hxx" #include "PcmDither.hxx"
#include "PcmDsd.hxx" #include "PcmDsd.hxx"
#include "PcmResample.hxx"
extern "C" { extern "C" {
#include "pcm_resample.h"
#include "pcm_buffer.h" #include "pcm_buffer.h"
} }
@ -40,7 +40,7 @@ struct audio_format;
class PcmConvert { class PcmConvert {
PcmDsd dsd; PcmDsd dsd;
struct pcm_resample_state resample; PcmResampler resampler;
PcmDither dither; PcmDither dither;

View File

@ -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 * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,7 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "pcm_resample_internal.h" #include "PcmResampleInternal.hxx"
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
#include "conf.h" #include "conf.h"
@ -56,47 +56,43 @@ pcm_resample_global_init(GError **error_r)
#endif #endif
} }
void pcm_resample_init(struct pcm_resample_state *state) PcmResampler::PcmResampler()
{ {
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
if (pcm_resample_lsr_enabled()) if (pcm_resample_lsr_enabled())
pcm_resample_lsr_init(state); pcm_resample_lsr_init(this);
else else
#endif #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 #ifdef HAVE_LIBSAMPLERATE
if (pcm_resample_lsr_enabled()) if (pcm_resample_lsr_enabled())
pcm_resample_lsr_deinit(state); pcm_resample_lsr_deinit(this);
else else
#endif #endif
pcm_resample_fallback_deinit(state); pcm_resample_fallback_deinit(this);
} }
void void
pcm_resample_reset(struct pcm_resample_state *state) PcmResampler::Reset()
{ {
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
pcm_resample_lsr_reset(state); pcm_resample_lsr_reset(this);
#else
(void)state;
#endif #endif
} }
const float * const float *
pcm_resample_float(struct pcm_resample_state *state, PcmResampler::ResampleFloat(unsigned channels, unsigned src_rate,
unsigned channels,
unsigned src_rate,
const float *src_buffer, size_t src_size, const float *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
if (pcm_resample_lsr_enabled()) 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, src_rate, src_buffer, src_size,
dest_rate, dest_size_r, dest_rate, dest_size_r,
error_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 not do any math on the sample values, so this hack is
possible: */ possible: */
return (const float *) return (const float *)
pcm_resample_fallback_32(state, channels, pcm_resample_fallback_32(this, channels,
src_rate, (const int32_t *)src_buffer, src_rate, (const int32_t *)src_buffer,
src_size, src_size,
dest_rate, dest_size_r); dest_rate, dest_size_r);
} }
const int16_t * const int16_t *
pcm_resample_16(struct pcm_resample_state *state, PcmResampler::Resample16(unsigned channels,
unsigned channels,
unsigned src_rate, const int16_t *src_buffer, size_t src_size, unsigned src_rate, const int16_t *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
if (pcm_resample_lsr_enabled()) 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, src_rate, src_buffer, src_size,
dest_rate, dest_size_r, dest_rate, dest_size_r,
error_r); error_r);
@ -131,21 +126,20 @@ pcm_resample_16(struct pcm_resample_state *state,
(void)error_r; (void)error_r;
#endif #endif
return pcm_resample_fallback_16(state, channels, return pcm_resample_fallback_16(this, channels,
src_rate, src_buffer, src_size, src_rate, src_buffer, src_size,
dest_rate, dest_size_r); dest_rate, dest_size_r);
} }
const int32_t * const int32_t *
pcm_resample_32(struct pcm_resample_state *state, PcmResampler::Resample32(unsigned channels, unsigned src_rate,
unsigned channels, const int32_t *src_buffer, size_t src_size,
unsigned src_rate, const int32_t *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
if (pcm_resample_lsr_enabled()) 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, src_rate, src_buffer, src_size,
dest_rate, dest_size_r, dest_rate, dest_size_r,
error_r); error_r);
@ -153,7 +147,7 @@ pcm_resample_32(struct pcm_resample_state *state,
(void)error_r; (void)error_r;
#endif #endif
return pcm_resample_fallback_32(state, channels, return pcm_resample_fallback_32(this, channels,
src_rate, src_buffer, src_size, src_rate, src_buffer, src_size,
dest_rate, dest_size_r); dest_rate, dest_size_r);
} }

138
src/pcm/PcmResample.hxx Normal file
View File

@ -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 <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifdef HAVE_LIBSAMPLERATE
#include <samplerate.h>
#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

View File

@ -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 * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,52 +18,53 @@
*/ */
#include "config.h" #include "config.h"
#include "pcm_resample_internal.h" #include "PcmResampleInternal.hxx"
#include <assert.h> #include <assert.h>
void void
pcm_resample_fallback_init(struct pcm_resample_state *state) pcm_resample_fallback_init(PcmResampler *state)
{ {
pcm_buffer_init(&state->buffer); pcm_buffer_init(&state->buffer);
} }
void void
pcm_resample_fallback_deinit(struct pcm_resample_state *state) pcm_resample_fallback_deinit(PcmResampler *state)
{ {
pcm_buffer_deinit(&state->buffer); pcm_buffer_deinit(&state->buffer);
} }
/* resampling code blatantly ripped from ESD */ /* resampling code blatantly ripped from ESD */
const int16_t * const int16_t *
pcm_resample_fallback_16(struct pcm_resample_state *state, pcm_resample_fallback_16(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int16_t *src_buffer, size_t src_size, const int16_t *src_buffer, size_t src_size,
unsigned dest_rate, unsigned dest_rate,
size_t *dest_size_r) 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 src_frames = src_size / channels / sizeof(*src_buffer);
unsigned dest_frames = unsigned dest_frames =
(src_frames * dest_rate + src_rate - 1) / src_rate; (src_frames * dest_rate + src_rate - 1) / src_rate;
unsigned dest_samples = dest_frames * channels; unsigned dest_samples = dest_frames * channels;
size_t dest_size = dest_samples * sizeof(*src_buffer); 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); assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
switch (channels) { switch (channels) {
case 1: case 1:
while (dest_pos < dest_samples) { 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]; dest_buffer[dest_pos++] = src_buffer[src_pos];
} }
break; break;
case 2: case 2:
while (dest_pos < dest_samples) { 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; src_pos &= ~1;
dest_buffer[dest_pos++] = src_buffer[src_pos]; dest_buffer[dest_pos++] = src_buffer[src_pos];
@ -77,34 +78,35 @@ pcm_resample_fallback_16(struct pcm_resample_state *state,
} }
const int32_t * const int32_t *
pcm_resample_fallback_32(struct pcm_resample_state *state, pcm_resample_fallback_32(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int32_t *src_buffer, size_t src_size, const int32_t *src_buffer, size_t src_size,
unsigned dest_rate, unsigned dest_rate,
size_t *dest_size_r) 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 src_frames = src_size / channels / sizeof(*src_buffer);
unsigned dest_frames = unsigned dest_frames =
(src_frames * dest_rate + src_rate - 1) / src_rate; (src_frames * dest_rate + src_rate - 1) / src_rate;
unsigned dest_samples = dest_frames * channels; unsigned dest_samples = dest_frames * channels;
size_t dest_size = dest_samples * sizeof(*src_buffer); 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); assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
switch (channels) { switch (channels) {
case 1: case 1:
while (dest_pos < dest_samples) { 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]; dest_buffer[dest_pos++] = src_buffer[src_pos];
} }
break; break;
case 2: case 2:
while (dest_pos < dest_samples) { 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; src_pos &= ~1;
dest_buffer[dest_pos++] = src_buffer[src_pos]; dest_buffer[dest_pos++] = src_buffer[src_pos];

View File

@ -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 * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -24,11 +24,11 @@
* of this header is somewhat unrelated to it. * of this header is somewhat unrelated to it.
*/ */
#ifndef MPD_PCM_RESAMPLE_INTERNAL_H #ifndef MPD_PCM_RESAMPLE_INTERNAL_HXX
#define MPD_PCM_RESAMPLE_INTERNAL_H #define MPD_PCM_RESAMPLE_INTERNAL_HXX
#include "check.h" #include "check.h"
#include "pcm_resample.h" #include "PcmResample.hxx"
#ifdef HAVE_LIBSAMPLERATE #ifdef HAVE_LIBSAMPLERATE
@ -36,16 +36,16 @@ bool
pcm_resample_lsr_global_init(const char *converter, GError **error_r); pcm_resample_lsr_global_init(const char *converter, GError **error_r);
void void
pcm_resample_lsr_init(struct pcm_resample_state *state); pcm_resample_lsr_init(PcmResampler *state);
void void
pcm_resample_lsr_deinit(struct pcm_resample_state *state); pcm_resample_lsr_deinit(PcmResampler *state);
void void
pcm_resample_lsr_reset(struct pcm_resample_state *state); pcm_resample_lsr_reset(PcmResampler *state);
const float * const float *
pcm_resample_lsr_float(struct pcm_resample_state *state, pcm_resample_lsr_float(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const float *src_buffer, size_t src_size, const float *src_buffer, size_t src_size,
@ -53,7 +53,7 @@ pcm_resample_lsr_float(struct pcm_resample_state *state,
GError **error_r); GError **error_r);
const int16_t * const int16_t *
pcm_resample_lsr_16(struct pcm_resample_state *state, pcm_resample_lsr_16(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int16_t *src_buffer, size_t src_size, 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); GError **error_r);
const int32_t * const int32_t *
pcm_resample_lsr_32(struct pcm_resample_state *state, pcm_resample_lsr_32(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int32_t *src_buffer, const int32_t *src_buffer,
@ -72,13 +72,13 @@ pcm_resample_lsr_32(struct pcm_resample_state *state,
#endif #endif
void void
pcm_resample_fallback_init(struct pcm_resample_state *state); pcm_resample_fallback_init(PcmResampler *state);
void void
pcm_resample_fallback_deinit(struct pcm_resample_state *state); pcm_resample_fallback_deinit(PcmResampler *state);
const int16_t * const int16_t *
pcm_resample_fallback_16(struct pcm_resample_state *state, pcm_resample_fallback_16(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int16_t *src_buffer, size_t src_size, 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); size_t *dest_size_r);
const int32_t * const int32_t *
pcm_resample_fallback_32(struct pcm_resample_state *state, pcm_resample_fallback_32(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int32_t *src_buffer, const int32_t *src_buffer,

View File

@ -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 * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,7 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "pcm_resample_internal.h" #include "PcmResampleInternal.hxx"
#include "conf.h" #include "conf.h"
#include <glib.h> #include <glib.h>
@ -41,14 +41,14 @@ libsamplerate_quark(void)
static bool static bool
lsr_parse_converter(const char *s) lsr_parse_converter(const char *s)
{ {
assert(s != NULL); assert(s != nullptr);
if (*s == 0) if (*s == 0)
return true; return true;
char *endptr; char *endptr;
long l = strtol(s, &endptr, 10); 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; lsr_converter = l;
return true; return true;
} }
@ -56,7 +56,7 @@ lsr_parse_converter(const char *s)
size_t length = strlen(s); size_t length = strlen(s);
for (int i = 0;; ++i) { for (int i = 0;; ++i) {
const char *name = src_get_name(i); const char *name = src_get_name(i);
if (name == NULL) if (name == nullptr)
break; break;
if (g_ascii_strncasecmp(s, name, length) == 0) { if (g_ascii_strncasecmp(s, name, length) == 0) {
@ -84,7 +84,7 @@ pcm_resample_lsr_global_init(const char *converter, GError **error_r)
} }
void void
pcm_resample_lsr_init(struct pcm_resample_state *state) pcm_resample_lsr_init(PcmResampler *state)
{ {
memset(state, 0, sizeof(*state)); memset(state, 0, sizeof(*state));
@ -94,9 +94,9 @@ pcm_resample_lsr_init(struct pcm_resample_state *state)
} }
void 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); state->state = src_delete(state->state);
pcm_buffer_deinit(&state->in); pcm_buffer_deinit(&state->in);
@ -105,20 +105,17 @@ pcm_resample_lsr_deinit(struct pcm_resample_state *state)
} }
void 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); src_reset(state->state);
} }
static bool static bool
pcm_resample_set(struct pcm_resample_state *state, pcm_resample_set(PcmResampler *state,
unsigned channels, unsigned src_rate, unsigned dest_rate, unsigned channels, unsigned src_rate, unsigned dest_rate,
GError **error_r) GError **error_r)
{ {
int error;
SRC_DATA *data = &state->data;
/* (re)set the state/ratio if the in or out format changed */ /* (re)set the state/ratio if the in or out format changed */
if (channels == state->prev.channels && if (channels == state->prev.channels &&
src_rate == state->prev.src_rate && src_rate == state->prev.src_rate &&
@ -133,6 +130,7 @@ pcm_resample_set(struct pcm_resample_state *state,
if (state->state) if (state->state)
state->state = src_delete(state->state); state->state = src_delete(state->state);
int error;
state->state = src_new(lsr_converter, channels, &error); state->state = src_new(lsr_converter, channels, &error);
if (!state->state) { if (!state->state) {
g_set_error(error_r, libsamplerate_quark(), state->error, g_set_error(error_r, libsamplerate_quark(), state->error,
@ -141,6 +139,7 @@ pcm_resample_set(struct pcm_resample_state *state,
return false; return false;
} }
SRC_DATA *data = &state->data;
data->src_ratio = (double)dest_rate / (double)src_rate; data->src_ratio = (double)dest_rate / (double)src_rate;
g_debug("setting samplerate conversion ratio to %.2lf", g_debug("setting samplerate conversion ratio to %.2lf",
data->src_ratio); data->src_ratio);
@ -150,7 +149,7 @@ pcm_resample_set(struct pcm_resample_state *state,
} }
static bool static bool
lsr_process(struct pcm_resample_state *state, GError **error_r) lsr_process(PcmResampler *state, GError **error_r)
{ {
if (state->error == 0) if (state->error == 0)
state->error = src_process(state->state, &state->data); state->error = src_process(state->state, &state->data);
@ -164,39 +163,31 @@ lsr_process(struct pcm_resample_state *state, GError **error_r)
return true; return true;
} }
static float *
deconst_float_buffer(const float *in)
{
union {
const float *in;
float *out;
} u = { .in = in };
return u.out;
}
const float * const float *
pcm_resample_lsr_float(struct pcm_resample_state *state, pcm_resample_lsr_float(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const float *src_buffer, size_t src_size, const float *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
SRC_DATA *data = &state->data;
assert((src_size % (sizeof(*src_buffer) * channels)) == 0); assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
if (!pcm_resample_set(state, channels, src_rate, dest_rate, error_r)) 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->input_frames = src_size / sizeof(*src_buffer) / channels;
data->data_in = deconst_float_buffer(src_buffer); data->data_in = const_cast<float *>(src_buffer);
data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate;
size_t data_out_size = data->output_frames * sizeof(float) * channels; 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)) if (!lsr_process(state, error_r))
return NULL; return nullptr;
*dest_size_r = data->output_frames_gen * *dest_size_r = data->output_frames_gen *
sizeof(*data->data_out) * channels; sizeof(*data->data_out) * channels;
@ -204,43 +195,39 @@ pcm_resample_lsr_float(struct pcm_resample_state *state,
} }
const int16_t * const int16_t *
pcm_resample_lsr_16(struct pcm_resample_state *state, pcm_resample_lsr_16(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int16_t *src_buffer, size_t src_size, const int16_t *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
bool success;
SRC_DATA *data = &state->data; 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); assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
success = pcm_resample_set(state, channels, src_rate, dest_rate, if (!pcm_resample_set(state, channels, src_rate, dest_rate,
error_r); error_r))
if (!success) return nullptr;
return NULL;
data->input_frames = src_size / sizeof(*src_buffer) / channels; data->input_frames = src_size / sizeof(*src_buffer) / channels;
data_in_size = data->input_frames * sizeof(float) * channels; size_t data_in_size = data->input_frames * sizeof(float) * channels;
data->data_in = pcm_buffer_get(&state->in, data_in_size); 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->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate;
data_out_size = data->output_frames * sizeof(float) * channels; 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);
src_short_to_float_array(src_buffer, data->data_in, src_short_to_float_array(src_buffer, data->data_in,
data->input_frames * channels); data->input_frames * channels);
if (!lsr_process(state, error_r)) if (!lsr_process(state, error_r))
return NULL; return nullptr;
int16_t *dest_buffer;
*dest_size_r = data->output_frames_gen * *dest_size_r = data->output_frames_gen *
sizeof(*dest_buffer) * channels; 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, src_float_to_short_array(data->data_out, dest_buffer,
data->output_frames_gen * channels); data->output_frames_gen * channels);
@ -268,43 +255,39 @@ src_float_to_int_array (const float *in, int *out, int len)
#endif #endif
const int32_t * const int32_t *
pcm_resample_lsr_32(struct pcm_resample_state *state, pcm_resample_lsr_32(PcmResampler *state,
unsigned channels, unsigned channels,
unsigned src_rate, unsigned src_rate,
const int32_t *src_buffer, size_t src_size, const int32_t *src_buffer, size_t src_size,
unsigned dest_rate, size_t *dest_size_r, unsigned dest_rate, size_t *dest_size_r,
GError **error_r) GError **error_r)
{ {
bool success;
SRC_DATA *data = &state->data; 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); assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
success = pcm_resample_set(state, channels, src_rate, dest_rate, if (!pcm_resample_set(state, channels, src_rate, dest_rate,
error_r); error_r))
if (!success) return nullptr;
return NULL;
data->input_frames = src_size / sizeof(*src_buffer) / channels; data->input_frames = src_size / sizeof(*src_buffer) / channels;
data_in_size = data->input_frames * sizeof(float) * channels; size_t data_in_size = data->input_frames * sizeof(float) * channels;
data->data_in = pcm_buffer_get(&state->in, data_in_size); 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->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate;
data_out_size = data->output_frames * sizeof(float) * channels; 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);
src_int_to_float_array(src_buffer, data->data_in, src_int_to_float_array(src_buffer, data->data_in,
data->input_frames * channels); data->input_frames * channels);
if (!lsr_process(state, error_r)) if (!lsr_process(state, error_r))
return NULL; return nullptr;
int32_t *dest_buffer;
*dest_size_r = data->output_frames_gen * *dest_size_r = data->output_frames_gen *
sizeof(*dest_buffer) * channels; 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, src_float_to_int_array(data->data_out, dest_buffer,
data->output_frames_gen * channels); data->output_frames_gen * channels);

View File

@ -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 <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifdef HAVE_LIBSAMPLERATE
#include <samplerate.h>
#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