Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio: allow specifying pitch correction filter #12479

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1846,10 +1846,20 @@ Video
Audio
-----

``--audio-pitch-correction=<yes|no>``
If this is enabled (default), playing with a speed different from normal
automatically inserts the ``scaletempo2`` audio filter. For details, see
audio filter section.
``--audio-pitch-correction=<no|<filter>|yes>``
If this is set to "yes" (default), playing with a speed different from
normal automatically inserts the ``scaletempo2`` audio filter. Aditionally,
a ``<filter>`` can be one of the following:

:scaletempo: The original pitch correction filter used in mpv, similar to
and generally worse than scaletempo2 in every use case.
:scaletempo2: This algorithm is ported from chromium. Also aliased to "yes"
(Default)
:rubberband: High quality pitch correction with librubberband. Uses more
CPU cycles than the others, but often achieves higher quality
with its R3 engine.

For details, see audio filter section.

``--audio-device=<name>``
Use the given audio device. This consists of the audio output name, e.g.
Expand Down
10 changes: 8 additions & 2 deletions filters/f_auto_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ struct aspeed_priv {
struct mp_subfilter sub;
double cur_speed, cur_speed_drop;
int current_filter;
const char *name;
};

static void aspeed_process(struct mp_filter *f)
Expand Down Expand Up @@ -340,9 +341,9 @@ static void aspeed_process(struct mp_filter *f)

if (req_filter) {
if (req_filter == 1) {
MP_VERBOSE(f, "adding scaletempo2\n");
MP_VERBOSE(f, "adding %s\n", p->name);
p->sub.filter = mp_create_user_filter(f, MP_OUTPUT_CHAIN_AUDIO,
"scaletempo2", NULL);
p->name, NULL);
} else if (req_filter == 2) {
MP_VERBOSE(f, "adding drop\n");
p->sub.filter = mp_create_user_filter(f, MP_OUTPUT_CHAIN_AUDIO,
Expand Down Expand Up @@ -372,6 +373,11 @@ static bool aspeed_command(struct mp_filter *f, struct mp_filter_command *cmd)
{
struct aspeed_priv *p = f->priv;

if (cmd->type == MP_FILTER_COMMAND_TEXT) {
p->name = cmd->cmd;
return true;
}

if (cmd->type == MP_FILTER_COMMAND_SET_SPEED) {
p->cur_speed = cmd->speed;
return true;
Expand Down
17 changes: 16 additions & 1 deletion filters/f_output_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,21 @@ static void set_speed_any(struct mp_user_filter **filters, int num_filters,
}
}

static void set_speed_params_any(struct mp_user_filter **filters, int num_filters,
int command, const char *name)
{
for (int n = num_filters - 1; n >= 0; n--) {
struct mp_filter_command cmd = {
.type = command,
.cmd = name,
};
mp_filter_command(filters[n]->f, &cmd);
}
}

void mp_output_chain_set_audio_speed(struct mp_output_chain *c,
double speed, double resample, double drop)
double speed, double resample, double drop,
const char *name)
{
struct chain *p = c->f->priv;

Expand All @@ -491,6 +504,8 @@ void mp_output_chain_set_audio_speed(struct mp_output_chain *c,
// otherwise use the builtin ones.
set_speed_any(p->user_filters, p->num_user_filters,
MP_FILTER_COMMAND_SET_SPEED, &speed);
set_speed_params_any(p->post_filters, p->num_post_filters,
MP_FILTER_COMMAND_TEXT, name);
set_speed_any(p->post_filters, p->num_post_filters,
MP_FILTER_COMMAND_SET_SPEED, &speed);
set_speed_any(p->user_filters, p->num_user_filters,
Expand Down
4 changes: 2 additions & 2 deletions filters/f_output_chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ bool mp_output_chain_update_filters(struct mp_output_chain *p,

// Desired audio speed, with resample being strict resampling.
void mp_output_chain_set_audio_speed(struct mp_output_chain *p,
double speed, double resample, double drop);

double speed, double resample, double drop,
const char *name);
// Total delay incurred by the filter chain, as measured by the recent filtered
// frames. The intention is that this sums the measured delays for each filter,
// so if a filter is removed, the caller can estimate how much audio is missing
Expand Down
6 changes: 4 additions & 2 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,9 @@ static const m_option_t mp_opts[] = {
{"audio-format", OPT_AUDIOFORMAT(audio_output_format), .flags = UPDATE_AUDIO},
{"speed", OPT_DOUBLE(playback_speed), M_RANGE(0.01, 100.0)},

{"audio-pitch-correction", OPT_BOOL(pitch_correction)},
{"audio-pitch-correction", OPT_CHOICE(pitch_correction,
{"no", 0}, {"yes", 1},
{"scaletempo2", 1}, {"rubberband", 2}, {"scaletempo", 3})},

// set a-v distance
{"audio-delay", OPT_FLOAT(audio_delay)},
Expand Down Expand Up @@ -948,7 +950,7 @@ static const struct MPOpts mp_default_opts = {
.audio_display = 1,
.audio_output_format = 0, // AF_FORMAT_UNKNOWN
.playback_speed = 1.,
.pitch_correction = true,
.pitch_correction = 1,
.audiofile_auto = -1,
.coverart_whitelist = true,
.osd_bar_visible = true,
Expand Down
2 changes: 1 addition & 1 deletion options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ typedef struct MPOpts {
int audio_output_format;
int force_srate;
double playback_speed;
bool pitch_correction;
int pitch_correction;
struct m_obj_settings *vf_settings;
struct m_obj_settings *af_settings;
struct filter_opts *filter_opts;
Expand Down
8 changes: 7 additions & 1 deletion player/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ static void update_speed_filters(struct MPContext *mpctx)
double speed = mpctx->opts->playback_speed;
double resample = mpctx->speed_factor_a;
double drop = 1.0;
char *pitch_correction_filters[] = {NULL,
"scaletempo2",
"rubberband",
"scaletempo",
};
char *name = pitch_correction_filters[mpctx->opts->pitch_correction];

if (!mpctx->opts->pitch_correction) {
resample *= speed;
Expand All @@ -76,7 +82,7 @@ static void update_speed_filters(struct MPContext *mpctx)
}
}

mp_output_chain_set_audio_speed(ao_c->filter, speed, resample, drop);
mp_output_chain_set_audio_speed(ao_c->filter, speed, resample, drop, name);
}

static int recreate_audio_filters(struct MPContext *mpctx)
Expand Down