PcmResample.cxx 3.57 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "PcmResampleInternal.hxx"
22

23
#ifdef HAVE_LIBSAMPLERATE
24 25
#include "ConfigGlobal.hxx"
#include "ConfigOption.hxx"
26 27
#endif

28 29
#include <string.h>

30 31 32 33
#ifdef HAVE_LIBSAMPLERATE
static bool lsr_enabled;
#endif

34 35 36 37
#ifdef HAVE_LIBSAMPLERATE
static bool
pcm_resample_lsr_enabled(void)
{
38
	return lsr_enabled;
39 40 41
}
#endif

42
bool
43
pcm_resample_global_init(Error &error)
44 45 46 47 48 49 50
{
#ifdef HAVE_LIBSAMPLERATE
	const char *converter =
		config_get_string(CONF_SAMPLERATE_CONVERTER, "");

	lsr_enabled = strcmp(converter, "internal") != 0;
	if (lsr_enabled)
51
		return pcm_resample_lsr_global_init(converter, error);
52 53 54
	else
		return true;
#else
55
	(void)error;
56 57 58 59
	return true;
#endif
}

60
PcmResampler::PcmResampler()
61
{
62
#ifdef HAVE_LIBSAMPLERATE
63
	if (pcm_resample_lsr_enabled())
64
		pcm_resample_lsr_init(this);
65
#endif
66
}
67

68
PcmResampler::~PcmResampler()
69 70
{
#ifdef HAVE_LIBSAMPLERATE
71
	if (pcm_resample_lsr_enabled())
72
		pcm_resample_lsr_deinit(this);
73 74 75
#endif
}

76
void
77
PcmResampler::Reset()
78 79
{
#ifdef HAVE_LIBSAMPLERATE
80
	pcm_resample_lsr_reset(this);
81 82 83
#endif
}

84
const float *
85 86 87
PcmResampler::ResampleFloat(unsigned channels, unsigned src_rate,
			    const float *src_buffer, size_t src_size,
			    unsigned dest_rate, size_t *dest_size_r,
88
			    Error &error_r)
89 90 91
{
#ifdef HAVE_LIBSAMPLERATE
	if (pcm_resample_lsr_enabled())
92
		return pcm_resample_lsr_float(this, channels,
93 94 95 96 97 98 99 100 101 102 103
					      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 *)
104
		pcm_resample_fallback_32(this, channels,
105 106 107 108 109
					 src_rate, (const int32_t *)src_buffer,
					 src_size,
					 dest_rate, dest_size_r);
}

110
const int16_t *
111 112 113
PcmResampler::Resample16(unsigned channels,
			 unsigned src_rate, const int16_t *src_buffer, size_t src_size,
			 unsigned dest_rate, size_t *dest_size_r,
114
			 Error &error_r)
115 116
{
#ifdef HAVE_LIBSAMPLERATE
117
	if (pcm_resample_lsr_enabled())
118
		return pcm_resample_lsr_16(this, channels,
119
					   src_rate, src_buffer, src_size,
120 121 122 123
					   dest_rate, dest_size_r,
					   error_r);
#else
	(void)error_r;
124 125
#endif

126
	return pcm_resample_fallback_16(this, channels,
127 128 129 130 131
					src_rate, src_buffer, src_size,
					dest_rate, dest_size_r);
}

const int32_t *
132 133 134
PcmResampler::Resample32(unsigned channels, unsigned src_rate,
			 const int32_t *src_buffer, size_t src_size,
			 unsigned dest_rate, size_t *dest_size_r,
135
			 Error &error_r)
136 137
{
#ifdef HAVE_LIBSAMPLERATE
138
	if (pcm_resample_lsr_enabled())
139
		return pcm_resample_lsr_32(this, channels,
140
					   src_rate, src_buffer, src_size,
141 142 143 144
					   dest_rate, dest_size_r,
					   error_r);
#else
	(void)error_r;
145 146
#endif

147
	return pcm_resample_fallback_32(this, channels,
148 149 150
					src_rate, src_buffer, src_size,
					dest_rate, dest_size_r);
}