2013-11-10 19:17:16 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2013-11-10 19:17:16 +01:00
|
|
|
* 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"
|
2015-01-21 20:42:41 +01:00
|
|
|
#include "config/Block.hxx"
|
2016-09-05 12:19:20 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-11-10 19:17:16 +01:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
|
|
|
|
#include <soxr.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2020-07-29 23:07:16 +02:00
|
|
|
#include <cmath>
|
2020-03-12 23:20:59 +01:00
|
|
|
|
2014-08-16 08:25:06 +02:00
|
|
|
#include <string.h>
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
static constexpr Domain soxr_domain("soxr");
|
|
|
|
|
2015-01-21 19:56:36 +01:00
|
|
|
static constexpr unsigned long SOXR_DEFAULT_RECIPE = SOXR_HQ;
|
|
|
|
|
2015-01-21 19:55:48 +01:00
|
|
|
/**
|
|
|
|
* Special value for "invalid argument".
|
|
|
|
*/
|
|
|
|
static constexpr unsigned long SOXR_INVALID_RECIPE = -1;
|
|
|
|
|
2020-07-29 23:07:16 +02:00
|
|
|
/**
|
|
|
|
* Special value for the recipe selection for custom recipe.
|
|
|
|
*/
|
|
|
|
static constexpr unsigned long SOXR_CUSTOM_RECIPE = -2;
|
|
|
|
|
|
|
|
static soxr_io_spec_t soxr_io_custom_recipe;
|
2015-01-21 19:51:59 +01:00
|
|
|
static soxr_quality_spec_t soxr_quality;
|
2015-01-21 20:29:20 +01:00
|
|
|
static soxr_runtime_spec_t soxr_runtime;
|
2020-07-29 23:07:16 +02:00
|
|
|
static bool soxr_use_custom_recipe;
|
|
|
|
|
2014-01-21 19:34:06 +01:00
|
|
|
|
2015-01-25 10:01:50 +01:00
|
|
|
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" },
|
2020-07-29 23:07:16 +02:00
|
|
|
{ SOXR_CUSTOM_RECIPE, "custom" },
|
2015-01-25 10:01:50 +01:00
|
|
|
{ SOXR_INVALID_RECIPE, nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
gcc_const
|
2014-01-21 19:34:06 +01:00
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
soxr_quality_name(unsigned long recipe) noexcept
|
2014-01-21 19:34:06 +01:00
|
|
|
{
|
2015-01-25 10:01:50 +01:00
|
|
|
for (const auto *i = soxr_quality_table;; ++i) {
|
|
|
|
assert(i->name != nullptr);
|
2014-01-21 19:34:06 +01:00
|
|
|
|
2015-01-25 10:01:50 +01:00
|
|
|
if (i->recipe == recipe)
|
|
|
|
return i->name;
|
|
|
|
}
|
2014-01-21 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 19:55:48 +01:00
|
|
|
gcc_pure
|
|
|
|
static unsigned long
|
2017-05-08 14:44:49 +02:00
|
|
|
soxr_parse_quality(const char *quality) noexcept
|
2014-01-21 19:34:06 +01:00
|
|
|
{
|
2015-01-21 20:42:41 +01:00
|
|
|
if (quality == nullptr)
|
2015-01-21 19:55:48 +01:00
|
|
|
return SOXR_DEFAULT_RECIPE;
|
2014-01-21 19:34:06 +01:00
|
|
|
|
2015-01-25 10:01:50 +01:00
|
|
|
for (const auto *i = soxr_quality_table; i->name != nullptr; ++i)
|
2015-06-20 15:25:00 +02:00
|
|
|
if (strcmp(i->name, quality) == 0)
|
2015-01-25 10:01:50 +01:00
|
|
|
return i->recipe;
|
|
|
|
|
|
|
|
return SOXR_INVALID_RECIPE;
|
2014-01-21 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:07:16 +02:00
|
|
|
static unsigned
|
|
|
|
SoxrParsePrecision(unsigned value) {
|
|
|
|
switch (value) {
|
|
|
|
case 16:
|
|
|
|
case 20:
|
|
|
|
case 24:
|
|
|
|
case 28:
|
|
|
|
case 32:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter invalid precision : %d [16|20|24|28|32]", value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
SoxrParsePhaseResponse(unsigned value) {
|
|
|
|
if (value > 100) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter invalid phase_respons : %d (0-100)", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return double(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
SoxrParsePassbandEnd(const char *svalue) {
|
|
|
|
char *endptr;
|
|
|
|
double value = strtod(svalue, &endptr);
|
|
|
|
if (svalue == endptr || *endptr != 0) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter passband_end value not a number: %s", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 1 || value > 100) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter invalid passband_end : %s (1-100%%)", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value / 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
SoxrParseStopbandBegin(const char *svalue) {
|
|
|
|
char *endptr;
|
|
|
|
double value = strtod(svalue, &endptr);
|
|
|
|
if (svalue == endptr || *endptr != 0) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter stopband_begin value not a number: %s", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 100 || value > 199) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter invalid stopband_begin : %s (100-150%%)", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value / 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
SoxrParseAttenuation(const char *svalue) {
|
|
|
|
char *endptr;
|
|
|
|
double value = strtod(svalue, &endptr);
|
|
|
|
if (svalue == endptr || *endptr != 0) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter attenuation value not a number: %s", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0 || value > 30) {
|
|
|
|
throw FormatInvalidArgument(
|
|
|
|
"soxr converter invalid attenuation : %s (0-30dB))", svalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1 / std::pow(10, value / 10.0);
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
void
|
|
|
|
pcm_resample_soxr_global_init(const ConfigBlock &block)
|
2014-01-21 19:34:06 +01:00
|
|
|
{
|
2015-01-21 20:42:41 +01:00
|
|
|
const char *quality_string = block.GetBlockValue("quality");
|
|
|
|
unsigned long recipe = soxr_parse_quality(quality_string);
|
2020-07-29 23:07:16 +02:00
|
|
|
soxr_use_custom_recipe = recipe == SOXR_CUSTOM_RECIPE;
|
|
|
|
|
2015-01-21 19:51:59 +01:00
|
|
|
if (recipe == SOXR_INVALID_RECIPE) {
|
2015-01-21 20:42:41 +01:00
|
|
|
assert(quality_string != nullptr);
|
2016-09-05 12:19:20 +02:00
|
|
|
throw FormatRuntimeError("unknown quality setting '%s' in line %d",
|
|
|
|
quality_string, block.line);
|
2020-07-29 23:07:16 +02:00
|
|
|
} else if (recipe == SOXR_CUSTOM_RECIPE) {
|
|
|
|
// used to preset possible internal flags, like SOXR_RESET_ON_CLEAR
|
|
|
|
soxr_quality = soxr_quality_spec(SOXR_DEFAULT_RECIPE, 0);
|
|
|
|
soxr_io_custom_recipe = soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I);
|
2014-01-21 19:34:06 +01:00
|
|
|
|
2020-07-29 23:07:16 +02:00
|
|
|
soxr_quality.precision =
|
|
|
|
SoxrParsePrecision(block.GetBlockValue("precision", SOXR_HQ));
|
|
|
|
soxr_quality.phase_response =
|
|
|
|
SoxrParsePhaseResponse(block.GetBlockValue("phase_response", 50));
|
|
|
|
soxr_quality.passband_end =
|
|
|
|
SoxrParsePassbandEnd(block.GetBlockValue("passband_end", "95.0"));
|
|
|
|
soxr_quality.stopband_begin = SoxrParseStopbandBegin(
|
|
|
|
block.GetBlockValue("stopband_begin", "100.0"));
|
|
|
|
// see soxr.h soxr_quality_spec.flags
|
|
|
|
soxr_quality.flags = (soxr_quality.flags & 0xFFFFFFC0) |
|
|
|
|
(block.GetBlockValue("flags", 0) & 0x3F);
|
|
|
|
soxr_io_custom_recipe.scale =
|
|
|
|
SoxrParseAttenuation(block.GetBlockValue("attenuation", "0"));
|
|
|
|
} else {
|
|
|
|
soxr_quality = soxr_quality_spec(recipe, 0);
|
|
|
|
}
|
2015-01-21 19:51:59 +01:00
|
|
|
|
2014-01-21 19:34:06 +01:00
|
|
|
FormatDebug(soxr_domain,
|
|
|
|
"soxr converter '%s'",
|
2015-01-21 19:51:59 +01:00
|
|
|
soxr_quality_name(recipe));
|
2014-01-21 19:34:06 +01:00
|
|
|
|
2015-01-21 20:33:13 +01:00
|
|
|
const unsigned n_threads = block.GetBlockValue("threads", 1);
|
2015-01-21 20:29:20 +01:00
|
|
|
soxr_runtime = soxr_runtime_spec(n_threads);
|
2014-01-21 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 19:17:16 +01:00
|
|
|
AudioFormat
|
2016-09-05 12:19:20 +02:00
|
|
|
SoxrPcmResampler::Open(AudioFormat &af, unsigned new_sample_rate)
|
2013-11-10 19:17:16 +01:00
|
|
|
{
|
|
|
|
assert(af.IsValid());
|
|
|
|
assert(audio_valid_sample_rate(new_sample_rate));
|
|
|
|
|
|
|
|
soxr_error_t e;
|
2020-07-29 23:07:16 +02:00
|
|
|
soxr_io_spec_t* p_soxr_io = nullptr;
|
|
|
|
if(soxr_use_custom_recipe) {
|
|
|
|
p_soxr_io = & soxr_io_custom_recipe;
|
|
|
|
}
|
2013-11-10 19:17:16 +01:00
|
|
|
soxr = soxr_create(af.sample_rate, new_sample_rate,
|
|
|
|
af.channels, &e,
|
2020-07-29 23:07:16 +02:00
|
|
|
p_soxr_io, &soxr_quality, &soxr_runtime);
|
2016-09-05 12:19:20 +02:00
|
|
|
if (soxr == nullptr)
|
|
|
|
throw FormatRuntimeError("soxr initialization has failed: %s",
|
|
|
|
e);
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
FormatDebug(soxr_domain, "soxr engine '%s'", soxr_engine(soxr));
|
2020-07-29 23:07:16 +02:00
|
|
|
if (soxr_use_custom_recipe)
|
|
|
|
FormatDebug(soxr_domain,
|
|
|
|
"soxr precision=%0.0f, phase_response=%0.2f, "
|
|
|
|
"passband_end=%0.2f, stopband_begin=%0.2f scale=%0.2f",
|
|
|
|
soxr_quality.precision, soxr_quality.phase_response,
|
|
|
|
soxr_quality.passband_end, soxr_quality.stopband_begin,
|
|
|
|
soxr_io_custom_recipe.scale);
|
|
|
|
else
|
|
|
|
FormatDebug(soxr_domain,
|
|
|
|
"soxr precision=%0.0f, phase_response=%0.2f, "
|
|
|
|
"passband_end=%0.2f, stopband_begin=%0.2f",
|
|
|
|
soxr_quality.precision, soxr_quality.phase_response,
|
|
|
|
soxr_quality.passband_end, soxr_quality.stopband_begin);
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
channels = af.channels;
|
|
|
|
|
|
|
|
ratio = float(new_sample_rate) / float(af.sample_rate);
|
|
|
|
FormatDebug(soxr_domain,
|
|
|
|
"samplerate conversion ratio to %.2lf",
|
2020-03-16 07:33:21 +01:00
|
|
|
double(ratio));
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
/* libsoxr works with floating point samples */
|
|
|
|
af.format = SampleFormat::FLOAT;
|
|
|
|
|
|
|
|
AudioFormat result = af;
|
|
|
|
result.sample_rate = new_sample_rate;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-01 19:07:33 +01:00
|
|
|
SoxrPcmResampler::Close() noexcept
|
2013-11-10 19:17:16 +01:00
|
|
|
{
|
|
|
|
soxr_delete(soxr);
|
|
|
|
}
|
|
|
|
|
2018-01-02 19:07:45 +01:00
|
|
|
void
|
2018-01-05 10:10:17 +01:00
|
|
|
SoxrPcmResampler::Reset() noexcept
|
2018-01-02 19:07:45 +01:00
|
|
|
{
|
|
|
|
#if SOXR_THIS_VERSION >= SOXR_VERSION(0,1,2)
|
|
|
|
soxr_clear(soxr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-10 19:17:16 +01:00
|
|
|
ConstBuffer<void>
|
2016-09-05 12:19:20 +02:00
|
|
|
SoxrPcmResampler::Resample(ConstBuffer<void> src)
|
2013-11-10 19:17:16 +01:00
|
|
|
{
|
|
|
|
const size_t frame_size = channels * sizeof(float);
|
|
|
|
assert(src.size % frame_size == 0);
|
|
|
|
|
|
|
|
const size_t n_frames = src.size / frame_size;
|
|
|
|
|
2014-11-10 22:52:17 +01:00
|
|
|
/* always round up: worst case output buffer size */
|
|
|
|
const size_t o_frames = size_t(n_frames * ratio) + 1;
|
2013-11-10 19:17:16 +01:00
|
|
|
|
2020-02-01 13:55:08 +01:00
|
|
|
auto *output_buffer = (float *)buffer.Get(o_frames * frame_size);
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
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);
|
2016-09-05 12:19:20 +02:00
|
|
|
if (e != nullptr)
|
|
|
|
throw FormatRuntimeError("soxr error: %s", e);
|
2013-11-10 19:17:16 +01:00
|
|
|
|
|
|
|
return { output_buffer, o_done * frame_size };
|
|
|
|
}
|
2018-01-02 18:22:53 +01:00
|
|
|
|
|
|
|
ConstBuffer<void>
|
|
|
|
SoxrPcmResampler::Flush()
|
|
|
|
{
|
|
|
|
const size_t frame_size = channels * sizeof(float);
|
|
|
|
const size_t o_frames = 1024;
|
|
|
|
|
2020-02-01 13:55:08 +01:00
|
|
|
auto *output_buffer = (float *)buffer.Get(o_frames * frame_size);
|
2018-01-02 18:22:53 +01:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|