Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Implementing replaygain (Read 1985 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Implementing replaygain

Hi guys,

the replaygain site seems to be unavailable, so I'll just post it here:

I've written my own MP3 player (based on FMOD) for my own HTPC use. All is good and all is well, I simply use a Foobar playlist for all my info including replaygain. The only thing remaining is enabling replaygain. The question is, how.

How do I translate the xx DB value to the (linear) decoder volume settings?

Implementing replaygain

Reply #1
HA mods: the ReplayGain website is hosted by HA - where has it gone?!

BoomSmurf: This is the relevant info (taken from my thesis, not the website, but it's basically the same)...


K.7.1.2   Scaling by the Replay Gain Adjustment

Changing the level of an audio signal simply means multiplying each sample value by a con-stant value. This constant is given by:
      scale=10^(replay_gain_adjustment/20)
Or, in words: ten to the power of (the Replay Gain Adjustment divided by 20).

After any such operation, it is good practice to dither the result. If this calculation and the pre-amp are implemented separately, then dither should only be added to the final result, just be-fore the result is truncated back to 16 bits. The bit-depth of the output should be equal to that of the sound card, not the original file. For example, after Replay Gain Adjustment, an 8-bit file should be sent to a 16-bit soundcard at 16-bit resolution.

K.7.1.3   No Replay Gain information

Disabling Replay Gain control for tracks without Replay Gain information would cause these tracks to be louder than the others, thus bringing back the original problem. If neither "Radio" or "Audiophile" Gain Adjustments are set, or if the track does not contain Replay Gain infor-mation, then the player should use an average of the previous ten Replay Gain Adjustments. This represents the typical loudness of tracks in the users music collection, and is a much bet-ter estimate of the likely Replay Gain than 0 dB, or no adjustment at all.

If the file only contains one of the Replay Gain Adjustments (e.g. Audiophile) but the user has requested the other (Radio), then the player should use the one that is available (in this case, Audiophile).

K.7.2   Player Pre-amp

The SMPTE calibration level suggests that the average level of an audio track should be 20 dB below full scale. Some pop music is dynamically compressed to peak at 0 dB and average around –3 dB. Hence, when the Replay Gain is correctly set, the level of such tracks will be reduced by 17 dB. If the user is listening to a mixture of highly compressed and not com-pressed tracks, then Replay Gain Adjustment will make the listening experience more pleasur-able, by bringing the level of the compressed tracks down into line with the others. However, if the user is only listening to highly compressed music, then they are likely to complain that all their files are now too quiet.

To solve this problem, a Pre-amp should be incorporated into the player. This should take the form of an adjustment to the scale factor calculated in the previous section. The default value should be +6 dB, though some manufacturers may choose +9, +12 or +15 dB. A positive value is chosen so that casual users will find little change to the loudness of their compressed pop music (except that the occasional "problem" quiet track will now be as loud as the rest), while power users and audiophiles can reduce the Pre-amp gain to enjoy all their music without dy-namic compression.

If the Pre-amp gain is set to a high value whilst listening to classical music (or nicely produced pop music), then the peaks will be compressed. However, this is the consistent policy of most radio stations, and many listeners like this sound.

K.7.2.1   Implementation

If Replay Gain Adjustment is enabled, the player should read the user selected pre-amp gain, and scale the audio signal by the appropriate amount. For example, a +6 dB gain requires a scale of 10(6/20), which is approximately 2. The Replay Gain and Pre-amp scale factors can be multiplied together for simplicity and ease of processing.

K.7.3   Clipping Prevention

The signal may clip for three reasons.
1. In coded audio (e.g. mp3 files) a file that was hard-limited to digital full scale before encod-ing will often be pushed over the limit by the psychoacoustic compression. A decoder with headroom can recover the signal above full scale by reducing the gain (for example [Leslie, WEB]). Typical decoders simply allow the audio to clip.
2. Replay Gain will reduce the level of loud dynamically compressed tracks, and increase the level of quiet dynamically uncompressed tracks. The average levels will then be similar, but the quiet tracks will have higher peaks. If the user pushes the pre-amp gain to maximum (which would take highly compressed pop music back to its original level), then the peaks of the (originally) quieter material will be pushed well over full scale.
3. If a track has a very wide dynamic range, then the Replay Gain Adjustment may instruct the player to increase the level of the track such that it will clip. This will occur for tracks with very low average energy, but very high peak amplitude. The author would be interested to hear of any recordings which cause clipping following Replay Gain Adjustment, with the pre-amp gain set at zero.

There are two solutions to the problem of clipping. In situation 2 above, the user apparently wants all the music to sound very loud. To give them their wish, any signal which would peak above digital full scale should be limited at just below digital full scale. This is also useful at lower pre-amp gains, where it allows the average level of classical music to be raised to that of pop music, without introduction clipping distortion. This could be useful when making tapes for use in an automobile. The exact type of limiting/compression is not defined here, but some-thing similar to the Hard Limiter found in Cool Edit Pro [Syntrillium, 2000] may be appropri-ate, especially for popular music.

The audiophile user will not want any compression or limiting to be applied to the signal. In this case, the only option is to reduce the pre-amp gain (so that the scaling of the digital signal is lower than that suggested by the Replay Gain Adjustment). In order to maintain the consis-tency of level between tracks, the pre-amp gain should remain at this reduced level for subse-quent tracks.

K.7.3.1   Implementation

If the Peak Level is stored in the header of the file, it is trivial to calculate whether the signal will clip following the Replay Gain Adjustment and Pre-amp gain. If the signal will not clip, then no further action is necessary. If the signal will clip, then either the hard limiter should be enabled, or the pre-amp gain should be reduced accordingly before playing the track.


Hope this helps.

Cheers,
David.

Implementing replaygain

Reply #2
Thank you for the info 2Bdecided. Seems I will have to write a DSP module for it. If I still have questions, I will get back to it, but I think I'll manage with this info.