Skip to content

Commit

Permalink
Take opus output gain into account when checking replaygain_limit peak (
Browse files Browse the repository at this point in the history
cmus#1201)

* Add information about opus output gain to comments
* Add information about output gain to track_info struct
* Take output_gain into account when checking replaygain_limit
* Prevent output_gain comment from being overwriten by user comments
* Use keyvals_add instead of comments_add_const to avoid having to make output_gain "interesting"
* Use xmalloc0 in favor of xmalloc+memset for speed improvement
* Don't free val, since keyvals_add doesn't xstrdup it
* Initialize output_gain to 0 during track_info initialization
* Calculate peak from output_gain if missing, instead of doing a separate scale check

Co-authored-by: Rin Takanashi <[email protected]>
  • Loading branch information
Dareka826 and Dareka826 authored Oct 18, 2022
1 parent fb21589 commit fed356b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
12 changes: 11 additions & 1 deletion ip/opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,24 @@ static int opus_read_comments(struct input_plugin_data *ip_data,
GROWING_KEYVALS(c);
struct opus_private *priv;
const OpusTags *ot;
const OpusHead *head;
int i;

priv = ip_data->private;

head = op_head(priv->of, -1);
if(head != NULL) {
char *val = xmalloc0(12); // 11 max int digits + NULL

snprintf(val, 12, "%d", head->output_gain);
keyvals_add(&c, "output_gain", val);
}

ot = op_tags(priv->of, -1);
if (ot == NULL) {
d_print("ot == NULL\n");
*comments = keyvals_new(0);
keyvals_terminate(&c);
*comments = c.keyvals;
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ static void update_rg_scale(void)
return;
}
if (isnan(peak)) {
d_print("peak not available, defaulting to 1\n");
peak = 1;
d_print("peak not available, deriving from output gain\n");
peak = pow(10.0, player_info_priv.ti->output_gain / 20.0);
}
if (peak < 0.05) {
d_print("peak (%g) is too small\n", peak);
Expand Down
6 changes: 6 additions & 0 deletions track_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ struct track_info *track_info_new(const char *filename)
ti->bpm = -1;
ti->codec = NULL;
ti->codec_profile = NULL;
ti->output_gain = 0;

return ti;
}

void track_info_set_comments(struct track_info *ti, struct keyval *comments) {
long int r128_track_gain;
long int r128_album_gain;
long int output_gain;

ti->comments = comments;
ti->artist = keyvals_get_val(comments, "artist");
Expand Down Expand Up @@ -110,6 +112,10 @@ void track_info_set_comments(struct track_info *ti, struct keyval *comments) {
ti->rg_album_gain = (r128_album_gain / 256.0) + 5;
}

if (comments_get_signed_int(comments, "output_gain", &output_gain) != -1) {
ti->output_gain = (output_gain / 256.0);
}

ti->collkey_artist = u_strcasecoll_key0(ti->artist);
ti->collkey_album = u_strcasecoll_key0(ti->album);
ti->collkey_title = u_strcasecoll_key0(ti->title);
Expand Down
1 change: 1 addition & 0 deletions track_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct track_info {
double rg_track_peak;
double rg_album_gain;
double rg_album_peak;
double output_gain;
const char *artist;
const char *album;
const char *title;
Expand Down

0 comments on commit fed356b

Please sign in to comment.