2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-23 20:00:51 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-23 20:00:51 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-03-14 15:26:28 +01:00
|
|
|
#include "pcm_resample_internal.h"
|
2008-10-23 20:00:51 +02:00
|
|
|
#include "conf.h"
|
|
|
|
|
2008-12-29 17:28:49 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-23 20:01:12 +02:00
|
|
|
#include <assert.h>
|
2008-10-23 20:00:51 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-29 17:28:49 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "pcm"
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
static int lsr_converter = SRC_SINC_FASTEST;
|
|
|
|
|
2009-07-23 12:01:03 +02:00
|
|
|
static inline GQuark
|
|
|
|
libsamplerate_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("libsamplerate");
|
|
|
|
}
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
static bool
|
|
|
|
lsr_parse_converter(const char *s)
|
2009-01-07 22:20:30 +01:00
|
|
|
{
|
2011-10-08 13:14:29 +02:00
|
|
|
assert(s != NULL);
|
2009-01-07 22:20:30 +01:00
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
if (*s == 0)
|
|
|
|
return true;
|
2009-01-07 22:20:30 +01:00
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
char *endptr;
|
|
|
|
long l = strtol(s, &endptr, 10);
|
|
|
|
if (*endptr == 0 && src_get_name(l) != NULL) {
|
|
|
|
lsr_converter = l;
|
|
|
|
return true;
|
2008-10-23 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
size_t length = strlen(s);
|
|
|
|
for (int i = 0;; ++i) {
|
|
|
|
const char *name = src_get_name(i);
|
|
|
|
if (name == NULL)
|
2008-10-23 20:00:51 +02:00
|
|
|
break;
|
2011-10-08 13:14:29 +02:00
|
|
|
|
|
|
|
if (g_ascii_strncasecmp(s, name, length) == 0) {
|
|
|
|
lsr_converter = i;
|
|
|
|
return true;
|
2008-10-23 20:00:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-08 12:40:03 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
void
|
|
|
|
pcm_resample_lsr_deinit(struct pcm_resample_state *state)
|
|
|
|
{
|
|
|
|
if (state->state != NULL)
|
|
|
|
state->state = src_delete(state->state);
|
2008-10-23 20:00:51 +02:00
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
pcm_buffer_deinit(&state->in);
|
|
|
|
pcm_buffer_deinit(&state->out);
|
|
|
|
pcm_buffer_deinit(&state->buffer);
|
2008-10-23 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
2009-07-23 12:01:03 +02:00
|
|
|
static bool
|
2008-10-23 20:02:09 +02:00
|
|
|
pcm_resample_set(struct pcm_resample_state *state,
|
2011-10-19 22:06:39 +02:00
|
|
|
unsigned channels, unsigned src_rate, unsigned dest_rate,
|
2009-07-23 12:01:03 +02:00
|
|
|
GError **error_r)
|
2008-10-23 20:02:09 +02:00
|
|
|
{
|
|
|
|
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)
|
2009-07-23 12:01:03 +02:00
|
|
|
return true;
|
2008-10-23 20:02:09 +02:00
|
|
|
|
2009-07-23 12:01:03 +02:00
|
|
|
state->error = 0;
|
2008-10-23 20:02:09 +02:00
|
|
|
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);
|
|
|
|
|
2011-10-08 13:14:29 +02:00
|
|
|
state->state = src_new(lsr_converter, channels, &error);
|
2008-10-23 20:02:09 +02:00
|
|
|
if (!state->state) {
|
2009-07-23 12:01:03 +02:00
|
|
|
g_set_error(error_r, libsamplerate_quark(), state->error,
|
|
|
|
"libsamplerate initialization has failed: %s",
|
|
|
|
src_strerror(error));
|
|
|
|
return false;
|
2008-10-23 20:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data->src_ratio = (double)dest_rate / (double)src_rate;
|
2008-12-29 17:28:49 +01:00
|
|
|
g_debug("setting samplerate conversion ratio to %.2lf",
|
|
|
|
data->src_ratio);
|
2008-10-23 20:02:09 +02:00
|
|
|
src_set_ratio(state->state, data->src_ratio);
|
2009-07-23 12:01:03 +02:00
|
|
|
|
|
|
|
return true;
|
2008-10-23 20:02:09 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 14:06:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
const int16_t *
|
2009-03-14 15:26:28 +01:00
|
|
|
pcm_resample_lsr_16(struct pcm_resample_state *state,
|
2011-10-19 22:06:39 +02:00
|
|
|
unsigned channels,
|
2009-03-14 15:26:28 +01:00
|
|
|
unsigned src_rate,
|
|
|
|
const int16_t *src_buffer, size_t src_size,
|
2009-07-23 12:01:03 +02:00
|
|
|
unsigned dest_rate, size_t *dest_size_r,
|
|
|
|
GError **error_r)
|
2008-10-23 20:00:51 +02:00
|
|
|
{
|
2009-07-23 12:01:03 +02:00
|
|
|
bool success;
|
2008-10-23 20:00:51 +02:00
|
|
|
SRC_DATA *data = &state->data;
|
|
|
|
size_t data_in_size;
|
|
|
|
size_t data_out_size;
|
2009-01-08 00:46:38 +01:00
|
|
|
int16_t *dest_buffer;
|
2008-10-23 20:00:51 +02:00
|
|
|
|
2008-10-23 20:01:12 +02:00
|
|
|
assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
|
|
|
|
|
2009-07-23 12:01:03 +02:00
|
|
|
success = pcm_resample_set(state, channels, src_rate, dest_rate,
|
|
|
|
error_r);
|
|
|
|
if (!success)
|
|
|
|
return NULL;
|
2008-10-23 20:00:51 +02:00
|
|
|
|
2008-10-23 20:01:08 +02:00
|
|
|
data->input_frames = src_size / sizeof(*src_buffer) / channels;
|
2008-10-23 20:00:51 +02:00
|
|
|
data_in_size = data->input_frames * sizeof(float) * channels;
|
2009-01-08 00:47:04 +01:00
|
|
|
data->data_in = pcm_buffer_get(&state->in, data_in_size);
|
2008-10-23 20:00:51 +02:00
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate;
|
2008-10-23 20:00:51 +02:00
|
|
|
data_out_size = data->output_frames * sizeof(float) * channels;
|
2009-01-08 00:47:04 +01:00
|
|
|
data->data_out = pcm_buffer_get(&state->out, data_out_size);
|
2008-10-23 20:00:51 +02:00
|
|
|
|
|
|
|
src_short_to_float_array(src_buffer, data->data_in,
|
|
|
|
data->input_frames * channels);
|
|
|
|
|
2011-10-08 14:06:24 +02:00
|
|
|
if (!lsr_process(state, error_r))
|
2009-03-14 14:37:31 +01:00
|
|
|
return NULL;
|
2008-10-23 20:00:51 +02:00
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
*dest_size_r = data->output_frames_gen *
|
|
|
|
sizeof(*dest_buffer) * channels;
|
|
|
|
dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r);
|
2008-10-23 20:00:51 +02:00
|
|
|
src_float_to_short_array(data->data_out, dest_buffer,
|
|
|
|
data->output_frames_gen * channels);
|
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
return dest_buffer;
|
2008-10-23 20:00:51 +02:00
|
|
|
}
|
2008-10-23 20:02:51 +02:00
|
|
|
|
2008-10-24 08:41:34 +02:00
|
|
|
#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
|
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
const int32_t *
|
2009-03-14 15:26:28 +01:00
|
|
|
pcm_resample_lsr_32(struct pcm_resample_state *state,
|
2011-10-19 22:06:39 +02:00
|
|
|
unsigned channels,
|
2009-03-14 15:26:28 +01:00
|
|
|
unsigned src_rate,
|
|
|
|
const int32_t *src_buffer, size_t src_size,
|
2009-07-23 12:01:03 +02:00
|
|
|
unsigned dest_rate, size_t *dest_size_r,
|
|
|
|
GError **error_r)
|
2008-10-23 20:02:51 +02:00
|
|
|
{
|
2009-07-23 12:01:03 +02:00
|
|
|
bool success;
|
2008-10-23 20:02:51 +02:00
|
|
|
SRC_DATA *data = &state->data;
|
|
|
|
size_t data_in_size;
|
|
|
|
size_t data_out_size;
|
2009-01-08 00:46:38 +01:00
|
|
|
int32_t *dest_buffer;
|
2008-10-23 20:02:51 +02:00
|
|
|
|
|
|
|
assert((src_size % (sizeof(*src_buffer) * channels)) == 0);
|
|
|
|
|
2009-07-23 12:01:03 +02:00
|
|
|
success = pcm_resample_set(state, channels, src_rate, dest_rate,
|
|
|
|
error_r);
|
|
|
|
if (!success)
|
|
|
|
return NULL;
|
2008-10-23 20:02:51 +02:00
|
|
|
|
|
|
|
data->input_frames = src_size / sizeof(*src_buffer) / channels;
|
|
|
|
data_in_size = data->input_frames * sizeof(float) * channels;
|
2009-01-08 00:47:04 +01:00
|
|
|
data->data_in = pcm_buffer_get(&state->in, data_in_size);
|
2008-10-23 20:02:51 +02:00
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate;
|
2008-10-23 20:02:51 +02:00
|
|
|
data_out_size = data->output_frames * sizeof(float) * channels;
|
2009-01-08 00:47:04 +01:00
|
|
|
data->data_out = pcm_buffer_get(&state->out, data_out_size);
|
2008-10-23 20:02:51 +02:00
|
|
|
|
|
|
|
src_int_to_float_array(src_buffer, data->data_in,
|
|
|
|
data->input_frames * channels);
|
|
|
|
|
2011-10-08 14:06:24 +02:00
|
|
|
if (!lsr_process(state, error_r))
|
2009-03-14 14:37:31 +01:00
|
|
|
return NULL;
|
2008-10-23 20:02:51 +02:00
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
*dest_size_r = data->output_frames_gen *
|
|
|
|
sizeof(*dest_buffer) * channels;
|
|
|
|
dest_buffer = pcm_buffer_get(&state->buffer, *dest_size_r);
|
2008-10-23 20:02:51 +02:00
|
|
|
src_float_to_int_array(data->data_out, dest_buffer,
|
|
|
|
data->output_frames_gen * channels);
|
|
|
|
|
2009-01-08 00:46:38 +01:00
|
|
|
return dest_buffer;
|
2008-10-23 20:02:51 +02:00
|
|
|
}
|