HydrogenAudio

Lossy Audio Compression => MP3 => MP3 - General => Topic started by: h.tuehn on 2003-08-01 04:30:11

Title: Clipping switch for LAME
Post by: h.tuehn on 2003-08-01 04:30:11
I've read previous posts on scale and MP3Gain/global gain.  I'm looking for a more precise method to prevent clipping on MP3s.  Something like what MAD does.

Would it be possible to add a switch to LAME that would automatically scale down a frame it detects will clip, and only that frame (and any other clipping frames), not the entire file?  Something along the lines of a limiter using frame-specific gain or scale.

By clipping, I mean those that exceed whatever bit-depth dynamic range is being used, usually 16.  So, if someone is using it for 20 or 24 bit encodes there wouldn't be a similar clipping problem to post-process with MP3Gain or a decoder that's limited to use when playing via an updatable PC.
Title: Clipping switch for LAME
Post by: boojum on 2003-08-01 05:16:05
How about Replaygain in fb2k or MP3Gain after encoding?  Would that work?   

Ooops;
i see you rejected that.
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-01 10:17:32
You could do this as a post encoding process, by altering the global gain field in selected frames.

It would probably sound worse than the clipping itself. In some of the worst cases of loud, overcompressed music, mp3 encoding can double the peak amplitude, and do so throughout most of the music. If you're going to halve the volume for a moment to prevent every clip, the music is going to be quite obviously damaged! It would sound terrible.

Where it may help is if there are only 1 or 2 moments of clipping - you can reduce the amplitude of these peaks, and keep the overall level higher without clipping. This is the job of a peak limiter - you can run this as a pre-process on any audio file, then encode it, and then use mp3gain or something similar to bring the gain up to maximum no clip gain. However, this approach is effectively re-mastering the track to add a touch of dynamic range compression (by squashing the peaks) - this is fine if it's what you want to do, but it's the opposite of transparent audio coding, where is aim is not to change the sound.


If you really want to keep the music as loud as possible, you might as well let it clip. Or reduce the level overall and remove the clipping.

Cheers,
David.
Title: Clipping switch for LAME
Post by: kennedyb4 on 2003-08-01 11:24:01
Wouldn't this be a form of dynamic compression (read "distortion")?
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-01 13:45:31
Quote
...this approach is effectively re-mastering the track to add a touch of dynamic range compression...

yup!
Title: Clipping switch for LAME
Post by: h.tuehn on 2003-08-01 14:11:21
Quote
Quote
...this approach is effectively re-mastering the track to add a touch of dynamic range compression...

yup!

Take the distortion of your choice.
1.  Clipping.
2.  Compressing the dynamics of the entire track and reducing to silence some ambient sounds, undertones, etc., and decreasing the signal to noise ratio (which increases hiss and MP3 encoding distortions).
3.  Compress the clipped waves only (my pick).  No clipping and the rest of the track is left alone.

It's just a question of what kind of and how much distortion you want.  In terms of dynamics, it's no actual distortion because it's getting clipped the same level any way (you just get the clipping effect as an added distorted benefit).  With MP3Gain, you distort the dynamics of the entire track (or worse, album) and silence some audio.
Title: Clipping switch for LAME
Post by: music_man_mpc on 2003-08-01 14:48:16
Quote
3.  Compress the clipped waves only (my pick).  No clipping and the rest of the track is left alone.

This would be a far worse option then replay gain for music that clips heavily, would it not?
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-01 15:07:14
Quote
2.  Compressing the dynamics of the entire track and reducing to silence some ambient sounds, undertones, etc., and decreasing the signal to noise ratio (which increases hiss and MP3 encoding distortions).

1. Reducing the volume isn't compressing the dynamics. What you are suggesting compresses thge dynamics. Replay gain (or your volume control) does not.

2. There is an often quoted rule around here that says that, if you think something may be audible, but that no one has yet proven that it is, it's helpful to go away and find an example of audio where it actually is audible, and bring it to everyone's attention.

3. If the track is so loud as to clip, it either has
a) a very wide dynamic range - i.e. a very very loud part and a very very quiet part, or
B) it's just very very loud throughout

In case (a), there's a possible issue with reducing the gain. You won't silence or lose anything, given correct dither - and, to be honest, even without correct dither there will be nothing lost with most real world signals! What you do do is reduce the signal to noise ratio. Slightly. But no signal components magically vanish.

In case (B) you're simply never going to notice the difference. Unless you have a portable player with a low power output, and you notice the actual decrease in volume.

Cheers,
David.
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-01 15:10:23
Quote
Quote
3.  Compress the clipped waves only (my pick).  No clipping and the rest of the track is left alone.

This would be a far worse option then replay gain for music that clips heavily, would it not?

Yes, much.

You can simulate it.

1. Find a track which clips heavily when mp3 encoded.

2. mp3gain it so that it doesn't clip.

3. Decode it, and load it into Cool Edit Pro

4. Apply the hard limiter. Set the gain increase to be the opposite of the gain reduction applied by mp3gain

5. Listen to the result!

The hard limiter squashes all the peaks down to prevent clipping. Compare it to the original (clipped) mp3 - and pick your poison!

Cheers,
David.
Title: Clipping switch for LAME
Post by: h.tuehn on 2003-08-01 21:02:33
Quote
You can simulate it.

4. Apply the hard limiter. Set the gain increase to be the opposite of the gain reduction applied by mp3gain

5. Listen to the result!

The hard limiter squashes all the peaks down to prevent clipping. Compare it to the original (clipped) mp3 - and pick your poison!

I can simulate it with a frame editor that allows gain adjustment per frame, but not this way.  If I can actually simulate it, I'll post something to ABX.

As to why the simulation you suggest fails.:
1.  If you decrease the gain on a track by 3dB, save it, open it tomorrow, and increase the gain by 3dB, is that a lossless operation?  Or will you get distortion compared to the original?
2.  As you point out, the hard limiter squashes peaks.  If you've already squashed them with MP3Gain, you're squashing them a second time, introducing more distortion.  What I'm recommending doesn't require 2 rounds of squashing, so it's unfair to make a comparison with a simulation that does.
Title: Clipping switch for LAME
Post by: AstralStorm on 2003-08-01 21:21:32
Digital gain scaling does introduce losses... but the analog part shouldn't, if you have at least adequate equipment.
Quality loss for ReplayGain is neglible or even non-existant. (in >(put-file-bitdepth-here)bit decoder, like foobar2000)

And you can change it losslessly. WaveGain is a different story though.
It actually changes PCM data in the file.
Title: Clipping switch for LAME
Post by: music_man_mpc on 2003-08-01 21:49:43
Quote
1.  If you decrease the gain on a track by 3dB, save it, open it tomorrow, and increase the gain by 3dB, is that a lossless operation?  Or will you get distortion compared to the original?

Correct me if I am wrong, but mp3gain dosen't affect the mp3 file at all, does it?  Thus you cannot "save the changes".  I know that replaygain for musepack files doesn't change the file information itself.
Title: Clipping switch for LAME
Post by: AstralStorm on 2003-08-01 22:05:41
MP3Gain doesn't affect the data in the file - it can be changed back losslessly.
Title: Clipping switch for LAME
Post by: music_man_mpc on 2003-08-01 22:24:50
I was pretty certain that was the case.
Title: Clipping switch for LAME
Post by: h.tuehn on 2003-08-06 01:32:04
A quick note while setting up a listening test.  mp3DirectCut is not the answer.  At least not at 1.32 or 1.33.  It does not accurately display where the potential clipping problems are because it merges left and right channels, among other things, averaging the volume.  As the its FAQ also notes:
Quote
The main MP3 graph does not show the correct audio level. But this is not an error! The graph contains only higher frequencies. It can also be too high or too low over the whole file. In rare cases the display doesn't show anything


I'll probably use another cutter/merger along with MP3Gain to adjust specific frames.  So don't expect huge samples. 
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-06 12:30:08
Quote
2.  As you point out, the hard limiter squashes peaks.  If you've already squashed them with MP3Gain, you're squashing them a second time, introducing more distortion.

Please don't take offense at this, because none is meant, but I can't think of another way to say it:

  I'm beginning to doubt that you actually know what you're talking about.


Maybe you do, and it's just a language and terminology thing. But you're not squashing anything when you increase or decrease the volume, either in analogue or digitla domains. You're scaling everything by the same amount. "Squashing the peaks" suggests that you're not reducing the level of the rest of the signal. But you are.

Until we all understand and use the same words, there's little point trying to argue which is better/worse/different!


Quote
1. If you decrease the gain on a track by 3dB, save it, open it tomorrow, and increase the gain by 3dB, is that a lossless operation? Or will you get distortion compared to the original?


If you use mp3gain (and some of the software you've mentioned) the process is entirely lossless. The final file will be bit identical to the original file. That's not true with an integer (fixed point) .wav file, but can be true with a floating point .wav file.


To clarify: mp3gain does change the data in an mp3 file, but the change is perfectly reversible because it's a numerical subtraction, with no rounding loss. It can can be reversed simply by adding the same number that was subtracted.

However, in a wav file, changing the volume involves Dividing every sample by the same number. Division almost always increases the number of decimal (or binary!) places, and so introduces rounding errors with fixed point numbers, and is hence a lossy process. If you divide by a number, round the result to store it in a fixed point file, and then multiply by the same number, the rounding means that you do not get the exact same result back.

Cheers,
David.
Title: Clipping switch for LAME
Post by: h.tuehn on 2003-08-07 05:43:15
Cheery David, let me begin by thanking you for admitting that adjusting the gain down then back up is lossy, thus making your suggested simulation faulty (which you did not admit, but I'm willing to help).

That aside, I never questioned, suggested, or wished to  express that the gain adjustments by MP3Gain on MP3s are not lossless.  They are lossless.  That fact also has nothing to do with this topic.  Please just let it go, as I was going to do when the earlier respondents, apparently lacking sufficient attention span, failed to understand the context of my comments.

As to...
Quote
Maybe you do, and it's just a language and terminology thing. But you're not squashing anything when you increase or decrease the volume, either in analogue or digitla domains. You're scaling everything by the same amount. "Squashing the peaks" suggests that you're not reducing the level of the rest of the signal. But you are.

To quote, let's see, oh, you, using this phrase first...
Quote
this approach is effectively re-mastering the track to add a touch of dynamic range compression (by squashing the peaks)

I'm simply working with your chosen terms.  In any event, I agree.  Adjusting the gain on an MP3 scales the entire signal within that frame.

Moving along, maybe eventually back to the topic.

While the gain method applied by MP3Gain is lossless, this doesn't change that, after applying MP3Gain, you end up with a waveform even more distorted compared to a waveform that only had gain adjusted on the specific frames that clipped.

Also, and this applies to both decoded MP3s processed by MP3Gain and wavs, fixed or floating, if you take a track that peaks at no higher than 3 dB above the floor (e.g., -93 on a 16 bit file), with a brief exception where it jumps to 0 dB (causing a 3 dB clip when encoded), what happens to the rest of the track at -93 dB and under?  (BTW, for anyone actually experimenting with this, you may need to increase the peak by several dB before you anything is audible).  In pure theory, rounding isn't even relevant.  The sound of the rest of the track is silenced.  No amount of volume adjustment can regain it.  Yes, you could undo it if using MP3Gain on an MP3, but that's not what we're discussing.  We're discussing whether a gain adjusted track loses sound that can't be recovered by increasing the volume (turning the knob, sliding the slider, etc.).

So what's the point?  Since isolat9[/QUOTE]
I'm simply working with your chosen terms.  In any event, I agree.  Adjusting the gain on an MP3 scales the entire signal within that frame.

Moving along, maybe eventually back to the topic.

While the gain method applied by MP3Gain is lossless, this doesn't change that, after applying MP3Gain, you end up with a waveform even more distorted compared to a waveform that only had gain adjusted on the specific frames that clipped.

Also, and this applies to both decoded MP3s processed by MP3Gain and wavs, fixed or floating, if you take a track that peaks at no higher than 3 dB above the floor (e.g., -93 on a 16 bit file), with a brief exception where it jumps to 0 dB (causing a 3 dB clip when encoded), what happens to the rest of the track at -93 dB and under?  (BTW, for anyone actually experimenting with this, you may need to increase the peak by several dB before you anything is audible).  In pure theory, rounding isn't even relevant.  The sound of the rest of the track is silenced.  No amount of volume adjustment can regain it.  Yes, you could undo it if using MP3Gain on an MP3, but that's not what we're discussing.  We're discussing whether a gain adjusted track loses sound that can't be recovered by increasing the volume (turning the knob, sliding the slider, etc.).

So what's the point?  Since isolating and reducing gain on specific frames is proving to be impossible (or too error prone) with the tools available, I'm going to focus the ABX on quiet sounds and the impact on them when gain is adjusted globally.
Title: Clipping switch for LAME
Post by: Jebus on 2003-08-07 05:58:45
So, you are willing to make the really loud clipping stuff compressed, and even lower the volume on individual frames (which would sound REALLY wonky, with the volume jumping all over the place), but you are concerned with the possibility of tiny little noises being lost below the threshold of hearing?

Put it this way: do you listen to your music at 100dB just so that you don't miss anything below the threshold of hearing? In doing so, are you willing to let your speakers distort the loudest passages as a trade-off?

Priorities, my friend!
Title: Clipping switch for LAME
Post by: Gabriel on 2003-08-07 08:18:29
Quote
While the gain method applied by MP3Gain is lossless, this doesn't change that, after applying MP3Gain, you end up with a waveform even more distorted compared to a waveform that only had gain adjusted on the specific frames that clipped.


I do not agree. The waveform is just scaled, not distorted. It could become distorted if your decoder has a limited resolution, but then the problem is in your decoder.
Title: Clipping switch for LAME
Post by: 2Bdecided on 2003-08-07 11:10:12
ditto what Gabriel said.

h.tuehn,

your use of the word distortion is incorrect.

as for the -93dB > silence argument - read something about dither, and then come back and make the same argument ;-)

http://mp3decoders.mp3-tech.org/24bit2.html (http://mp3decoders.mp3-tech.org/24bit2.html)
http://mp3decoders.mp3-tech.org/24bit.html (http://mp3decoders.mp3-tech.org/24bit.html)
(not the best references, but I like linking to my own sites, and I'm sure you can use Google as well as me to find the best result for audio dither)

Cheers,
David.
Title: Re: Clipping switch for LAME
Post by: Kraeved on 2024-02-26 16:23:12
A lot of water has passed under the bridge since 2003.

I often come across 44 kHz 16 bit lossless songs with True Peak between -0.5dB and 0dB, which is calculated using the recommended EBUR128 of Foobar2000 player. When converting to MP3 using LAME, I see that True Peak changes noticeably, it becomes between+0.5dB and +1.5dB, sometimes even higher. By trial and error, I found that using a preamp of -1.5dB in the conversion chain helps keeping True Peak in MP3 files around 0. But it's a manual method, more suitable when all songs to be converted have no headroom to spare, and if any song has enough headroom on its own, its volume seems to be reduced for nothing. So what is the best course of action here, or am I worrying in vain?