LibsamplerateResampler.cxx 3.81 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "LibsamplerateResampler.hxx"
22
#include "config/Block.hxx"
23
#include "util/ASCII.hxx"
24
#include "util/RuntimeError.hxx"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "util/Domain.hxx"
#include "Log.hxx"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

static constexpr Domain libsamplerate_domain("libsamplerate");

static int lsr_converter = SRC_SINC_FASTEST;

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 (StringEqualsCaseASCII(s, name, length)) {
			lsr_converter = i;
			return true;
		}
	}

	return false;
}

66 67
void
pcm_resample_lsr_global_init(const ConfigBlock &block)
68
{
69
	const char *converter = block.GetBlockValue("type", "2");
70 71 72
	if (!lsr_parse_converter(converter))
		throw FormatRuntimeError("unknown samplerate converter '%s'",
					 converter);
73 74 75 76 77 78 79

	FormatDebug(libsamplerate_domain,
		    "libsamplerate converter '%s'",
		    src_get_name(lsr_converter));
}

AudioFormat
80
LibsampleratePcmResampler::Open(AudioFormat &af, unsigned new_sample_rate)
81 82 83 84 85 86 87 88 89 90 91 92 93
{
	assert(af.IsValid());
	assert(audio_valid_sample_rate(new_sample_rate));

	src_rate = af.sample_rate;
	dest_rate = new_sample_rate;
	channels = af.channels;

	/* libsamplerate works with floating point samples */
	af.format = SampleFormat::FLOAT;

	int src_error;
	state = src_new(lsr_converter, channels, &src_error);
94 95 96
	if (!state)
		throw FormatRuntimeError("libsamplerate initialization has failed: %s",
					 src_strerror(src_error));
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	memset(&data, 0, sizeof(data));

	data.src_ratio = double(new_sample_rate) / double(af.sample_rate);
	FormatDebug(libsamplerate_domain,
		    "setting samplerate conversion ratio to %.2lf",
		    data.src_ratio);
	src_set_ratio(state, data.src_ratio);

	AudioFormat result = af;
	result.sample_rate = new_sample_rate;
	return result;
}

void
Max Kellermann's avatar
Max Kellermann committed
112
LibsampleratePcmResampler::Close() noexcept
113 114 115 116
{
	state = src_delete(state);
}

117
void
Max Kellermann's avatar
Max Kellermann committed
118
LibsampleratePcmResampler::Reset() noexcept
119 120 121 122
{
	src_reset(state);
}

123
inline ConstBuffer<float>
124
LibsampleratePcmResampler::Resample2(ConstBuffer<float> src)
125 126 127 128 129 130 131 132 133 134 135 136 137
{
	assert(src.size % channels == 0);

	const unsigned src_frames = src.size / channels;
	const unsigned dest_frames =
		(src_frames * dest_rate + src_rate - 1) / src_rate;
	size_t data_out_size = dest_frames * sizeof(float) * channels;

	data.data_in = const_cast<float *>(src.data);
	data.data_out = (float *)buffer.Get(data_out_size);
	data.input_frames = src_frames;
	data.output_frames = dest_frames;

138 139 140 141
	int result = src_process(state, &data);
	if (result != 0)
		throw FormatRuntimeError("libsamplerate has failed: %s",
					 src_strerror(result));
142 143 144 145 146 147

	return ConstBuffer<float>(data.data_out,
				  data.output_frames_gen * channels);
}

ConstBuffer<void>
148
LibsampleratePcmResampler::Resample(ConstBuffer<void> src)
149
{
150
	return Resample2(ConstBuffer<float>::FromVoid(src)).ToVoid();
151
}