SoxrResampler.cxx 4.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "SoxrResampler.hxx"
#include "AudioFormat.hxx"
22
#include "config/Block.hxx"
23
#include "util/RuntimeError.hxx"
24 25 26 27 28
#include "util/Domain.hxx"
#include "Log.hxx"

#include <soxr.h>

29 30
#include <cassert>

31
#include <string.h>
32 33 34

static constexpr Domain soxr_domain("soxr");

35 36
static constexpr unsigned long SOXR_DEFAULT_RECIPE = SOXR_HQ;

37 38 39 40 41
/**
 * Special value for "invalid argument".
 */
static constexpr unsigned long SOXR_INVALID_RECIPE = -1;

42
static soxr_quality_spec_t soxr_quality;
43
static soxr_runtime_spec_t soxr_runtime;
44

45 46 47 48 49 50 51 52 53 54 55 56 57
static constexpr struct {
	unsigned long recipe;
	const char *name;
} soxr_quality_table[] = {
	{ SOXR_VHQ, "very high" },
	{ SOXR_HQ, "high" },
	{ SOXR_MQ, "medium" },
	{ SOXR_LQ, "low" },
	{ SOXR_QQ, "quick" },
	{ SOXR_INVALID_RECIPE, nullptr }
};

gcc_const
58
static const char *
59
soxr_quality_name(unsigned long recipe) noexcept
60
{
61 62
	for (const auto *i = soxr_quality_table;; ++i) {
		assert(i->name != nullptr);
63

64 65 66
		if (i->recipe == recipe)
			return i->name;
	}
67 68
}

69 70
gcc_pure
static unsigned long
71
soxr_parse_quality(const char *quality) noexcept
72
{
73
	if (quality == nullptr)
74
		return SOXR_DEFAULT_RECIPE;
75

76
	for (const auto *i = soxr_quality_table; i->name != nullptr; ++i)
77
		if (strcmp(i->name, quality) == 0)
78 79 80
			return i->recipe;

	return SOXR_INVALID_RECIPE;
81 82
}

83 84
void
pcm_resample_soxr_global_init(const ConfigBlock &block)
85
{
86 87
	const char *quality_string = block.GetBlockValue("quality");
	unsigned long recipe = soxr_parse_quality(quality_string);
88
	if (recipe == SOXR_INVALID_RECIPE) {
89 90
		assert(quality_string != nullptr);

91 92
		throw FormatRuntimeError("unknown quality setting '%s' in line %d",
					 quality_string, block.line);
93 94
	}

95 96
	soxr_quality = soxr_quality_spec(recipe, 0);

97 98
	FormatDebug(soxr_domain,
		    "soxr converter '%s'",
99
		    soxr_quality_name(recipe));
100

101
	const unsigned n_threads = block.GetBlockValue("threads", 1);
102
	soxr_runtime = soxr_runtime_spec(n_threads);
103 104
}

105
AudioFormat
106
SoxrPcmResampler::Open(AudioFormat &af, unsigned new_sample_rate)
107 108 109 110 111 112 113
{
	assert(af.IsValid());
	assert(audio_valid_sample_rate(new_sample_rate));

	soxr_error_t e;
	soxr = soxr_create(af.sample_rate, new_sample_rate,
			   af.channels, &e,
114
			   nullptr, &soxr_quality, &soxr_runtime);
115 116 117
	if (soxr == nullptr)
		throw FormatRuntimeError("soxr initialization has failed: %s",
					 e);
118 119 120 121 122 123 124 125

	FormatDebug(soxr_domain, "soxr engine '%s'", soxr_engine(soxr));

	channels = af.channels;

	ratio = float(new_sample_rate) / float(af.sample_rate);
	FormatDebug(soxr_domain,
		    "samplerate conversion ratio to %.2lf",
Rosen Penev's avatar
Rosen Penev committed
126
		    double(ratio));
127 128 129 130 131 132 133 134 135 136

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

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

void
Max Kellermann's avatar
Max Kellermann committed
137
SoxrPcmResampler::Close() noexcept
138 139 140 141
{
	soxr_delete(soxr);
}

142
void
143
SoxrPcmResampler::Reset() noexcept
144 145 146 147 148 149
{
#if SOXR_THIS_VERSION >= SOXR_VERSION(0,1,2)
	soxr_clear(soxr);
#endif
}

150
ConstBuffer<void>
151
SoxrPcmResampler::Resample(ConstBuffer<void> src)
152 153 154 155 156 157
{
	const size_t frame_size = channels * sizeof(float);
	assert(src.size % frame_size == 0);

	const size_t n_frames = src.size / frame_size;

158 159
	/* always round up: worst case output buffer size */
	const size_t o_frames = size_t(n_frames * ratio) + 1;
160

Max Kellermann's avatar
Max Kellermann committed
161
	auto *output_buffer = (float *)buffer.Get(o_frames * frame_size);
162 163 164 165

	size_t i_done, o_done;
	soxr_error_t e = soxr_process(soxr, src.data, n_frames, &i_done,
				      output_buffer, o_frames, &o_done);
166 167
	if (e != nullptr)
		throw FormatRuntimeError("soxr error: %s", e);
168 169 170

	return { output_buffer, o_done * frame_size };
}
171 172 173 174 175 176 177

ConstBuffer<void>
SoxrPcmResampler::Flush()
{
	const size_t frame_size = channels * sizeof(float);
	const size_t o_frames = 1024;

Max Kellermann's avatar
Max Kellermann committed
178
	auto *output_buffer = (float *)buffer.Get(o_frames * frame_size);
179 180 181 182 183 184 185 186 187 188 189 190 191

	size_t o_done;
	soxr_error_t e = soxr_process(soxr, nullptr, 0, nullptr,
				      output_buffer, o_frames, &o_done);
	if (e != nullptr)
		throw FormatRuntimeError("soxr error: %s", e);

	if (o_done == 0)
		/* flush complete */
		output_buffer = nullptr;

	return { output_buffer, o_done * frame_size };
}