HydrogenAudio

Hydrogenaudio Forum => General Audio => Topic started by: timcupery on 2021-05-11 21:19:00

Title: Possible to batch-apply average of Track Gain and Album Gain on encoding?
Post by: timcupery on 2021-05-11 21:19:00
I rip losslessly (FLAC) and encode to mp3, typically with album gain applied.

On compilation albums that weren't carefully mastered for consistent loudness, I prefer to encode each song to the average of track gain and album gain. This assumes there is some validity to track gain, and some validity to album gain, and the average is the simplest compromise.

FB2k has the option of applying album gain or track gain, but no way to average them unless I manually alter the gain of each track and then apply that while encoding. This isn't terribly time-consuming, but it would be convenient if there's a way to apply the average of gain values, perhaps using a commandline approach (I'd be doing commandline-through-frontend, but that doesn't change anything as long as I can enter the command correctly).

I considered posting this in the FB2k hosted forum, but apart from some weird manual switch setting (that I don't believe exists in the FB2k interface), this seems more like a general audio question.
Title: Re: Possible to batch-apply average of Track Gain and Album Gain on encoding?
Post by: j7n on 2021-05-12 08:01:12
You can write a new track gain computed as an average or in another way using Foobar Masstagger component. You can only write this field if it doesn't already exist. It's probably not supposed to work, as the gain is compartmentalized into a "technical" field.

1. Calculate your new gains (all four) and save them in temporary fields.
2. Remove all replaygain normally.
3. Write replaygain from the temporary fields.
4. Reload tags to make the new RG fields read in as technical.
5. Remove temporary fields.

Included a quick Masstagger action that can do 1 and 3. It probably needs tweaking.
Title: Re: Possible to batch-apply average of Track Gain and Album Gain on encoding?
Post by: timcupery on 2021-05-12 21:11:28
Thanks a lot, @j7n - this makes sense. Didn't realize masstag can do calculations.

I've been editing the album gain field for years, to function as "true gain" that I want applied to a song, back in my college days when I had time to compare songs one-by-one to a set of reference tracks across different genres. So using your approach, I only want to change the REPLAYGAIN_ALBUM_GAIN field.

I used a 4-step workflow:
1) write the average of Track and Album gain fields to "temp", using the first line of your code
2) manually blank out all album gain fields
3) copy the contents of the "temp" field to album gain field
4) manually blank out "temp" field

It would be convenient if masstag actions were completed sequentially, in which case I could save this all as one action. But doing it as four separate steps (and saving 1 and 3 as specific masstag scripts) is easy enough.
Title: Re: Possible to batch-apply average of Track Gain and Album Gain on encoding?
Post by: Porcus on 2021-05-12 22:40:52
Mp3tag can treat REPLAYGAIN_ALBUM_GAIN as just another tag, so you don't need to do multiple steps that way.

You can recycle formulae from the .mts, but if you are willing to accept some inaccuracies - I mean, already at the outset you question the accuracy of the Album Gain figure - it isn't hard to code. It isn't unlikely that you might just try something and see if it makes you happy.

* Just like foobar2000, Mp3tag's arithmetic operations are integer-only, and everything after the decimal point will be discarded. That means that simply adding the two would make for an error of
-2 to 0 if both track gain and album gain are negative
-1 to 1 if one is negative and one is positive
0 to 2 if both are positive
Then if you divide by 2, decimals will be discarded once again (disclaimer: too lazy to check!). You can throw in some decimals by concatenating in the last 5 characters of one of them (say, the Album gain).  The worst cases here will be when album gain is negative and track gain is positive and their integer'd sum is odd. But positive gain values aren't that common, so consider whether that is really an issue you need to care about. Suggestion: just try and see how annoyed you get.

Anyway, integer'd sum to odd is worst case, so for calculations example: album gain of -2.xx and track gain -5.yy; addition gives -2 -5 = -7, odd number and division by 2 will get you -3. What you wanted was something between -3.5 and -4.5: -(3.5 + 0.[avg of xx and yy]). Concatenating with the ".xx dB" gives you "-3.xx dB".
The error becomes 0.5+(.yy-.xx)/2 if odd sum, delete the 0.5 if even sum [edit: this presumes both of same sign]. You can hack it in if you think it is fun, or you can just try and listen.