Help - Search - Members - Calendar
Full Version: Avoiding encoder clipping?
Hydrogenaudio Forums > Hydrogenaudio Forum > General Audio
markanini
Is there any way to avoid encoder clipping from lossy encoder?
Is reducing the voulme prior to encoding the only way? If so is there any way to predict the volume change necesarry to avoid clipping? This has been bugging me for a long time.
ChangFest
Scan the files you want to encode with ReplayGain. Then encode with an encoder that supportes replay gain, or a front end that does. I'd suggest foobar2000. ReplayGain (reducing the volume prior to encoding) is the only way I know of to avoid the clipping issue totally.
airon
Like he said, preencoding.

THe reason I like OGG so much is also its Replaygain tag and the predecoding gain you can apply in most decoders(Winamp, Foobar...).

Always add a Replaygain tag to your encodes. It should prevent clipping in almost any case.

Pulling down material before encoding will degrade the quality a little more than playback through Replaygain-capable players, because of added quantization noise.

As is the case in so many other instances of playing audio, I recommend Foobar. It's simply the most practical player for this.
XXX
Lamos use

--scale <arg> scale input (multiply PCM data) by <arg>

Something like --scale 0.90 will do you right. In theory.
rhadinocentrus
Clarification please;
Does the encoder (LAME) 1. Create a clipped encode from clipped .WAV (CDA)
2. Create a clipped encode from unclipped .WAV (CDA)

..........Now entering panic-mode

Many Thanks
XXX
A1: If Lame wants to be faithful, yes, it must do the same. Clipped is bad, but not necessarily wrong.

A2. Possible. I think the default scale is 0.98, for that reason. If my logarithms are right, scale = 0.5 would only drop you down 6 dB. The clipped waveform is still "clipped" but if it was borderline before (real close to max level), there's no chance it would be made worse (greater than max level) by the encoder. The encoder can internally deal with signals above 1.0 (unit scale) but always has to truncate (clip) to bit size. A 24-bit won't matter, since 1.0+.00001 in 24-bit is still more than 24 bits can handle (23 bits plus sign).

There are reasons to panic. Hot CDs are crap, and you bought crap. No amount of --scale will fix crap, but it won't make it worse if you use it right.
rhadinocentrus
Thank you XXX
Your implication in (A2) is that I would be better encoding from 24bit WAV files?
I do vinyl ripping so its no bother to workflow in 24bit. The final WAV files never exceed -0.3db, so hopefully the past encodes are 'safe'!
Thanks again
Jasper
Actually I think he meant that there is no point to encode (decode?) with 24bits per sample (at least in order to avoid clipping), as they also can't represent more than full-scale.
I don't exactly how the Foobar2000 mp3 decoder works, but if it decodes to floating-point directly, and you are using ReplayGain, then clipping should not happen (except for when the original already clipped or in other extreme cases, but there is a limiter available for those cases).
Jurg98
Correct me if I'm wrong, but if the source material (CD) is clipped (and a lot of CDs these days are), then how can you get an unclipped rip??? Theoretically, you should be able to mathematically restore the waveforms, but I don't think that's what ReplayGain is designed for...

A good copy of a bad sheet of paper is still a bad sheet of paper...
Jasper
Of course, but as I understand it ReplayGain CAN prevent clipping that occurs as a consequence of the encoding/decoding process.
smz
QUOTE(Jasper @ Apr 17 2004, 02:22 AM)
... as I understand it ReplayGain CAN prevent clipping that occurs as a consequence of the encoding/decoding process.

I think it is a little bit different:

A) Nothing can fix an already clipped original. (Maybe a special DSP filter that scale the signal down and try to extrapolate the missing clipped samples could alleviate this...)

B) LAME --scale <arg> can eliminate possible clipping of borderline samples introduced by math in the ENCODING phase. AFAIK and IIRC --preset standard (and maybe others presets) by default introuce a --scale 0.98%

C) Replaygain eliminate (and gives you info about) possible clipping introduced by math in the DECODING phase.


Sergio
Madrigal
1. Download and install WaveGain, available here.

2. Scan the wav files to be encoded, using "Just calculate, don't apply", and make a note of the suggested scale factor, e.g. 0.xxxx.

3. Include --scale 0.xxxx at the end of your encoder command line,
e.g. --alt-preset standard --scale 0.xxxx

Regards,
Madrigal
Jurg98
To Madrigal:

But does that eliminate clipping?? Don't think so... What you might avoid is decoder clipping (let's call it output clipping), since the tracks will be ripped at a lower volume. But you'll still have the same cut-off waveforms (they won't be at the top of the scale though, but a notch lower)...

Again, correct me if I'm wrong... smile.gif
Madrigal
@Jurg98:

The initial post wanted to avoid encoder clipping.

The method I suggested will encode the tracks at or close to the "normal" 89dB reference level, which is adequate to insure against encoder clipping in the vast majority of cases.

Attempting to simulate the restoration of cut-off waveforms is a separate matter altogether, into which I have never really delved.

edit: See also this thread.

Regards,
Madrigal
Jurg98
You've got me there... smile.gif

But, err, if you encode a clipped WAV, I agree you don't necessary have a clipped rip, but you'll definitely hear something's fishy (= the orginal clipping), right? Or wrong? smile.gif

Hence my point you can 't really eliminate clipping...
Madrigal
@Jurg98:

If the source track (be it CD, cassette, vinyl, or whatever) is compressed to the extent that extracted waveforms are cut off, then that portion which has been cut off is missing, and therefore lost and beyond accurate restoration.

But given a good source track, encoder clipping can definitely be avoided.

Regards,
Madrigal
rhadinocentrus
QUOTE
B) LAME --scale <arg> can eliminate possible clipping of borderline samples introduced by math in the ENCODING phase. AFAIK and IIRC --preset standard (and maybe others presets) by default introuce a --scale 0.98%

blink.gif : Is this reduction of Original WAV occur in LAME without adding '--scale 0.98%' in the command-line? ( I use EAC front end with external LAME encoder)

QUOTE
The method I suggested will encode the tracks at or close to the "normal" 89dB reference level, which is adequate to insure against encoder clipping in the vast majority of cases

blink.gif What is ' "normal" 89dB reference level ' refered to?

To clarify : Like the original poster, I am trying to prevent encoder clipping - not repair clipped music.

crying.gif Off topic: What does 'warn 0%' mean on the bottom of my posts?


Just did the following test.
LAME 3.93.1;High quality;192VBR
Single 5khz 24bit pulse: WAV to mp3 back to WAV (using soundforge) no scaling
1: 0% : returns 0% (0) no clipping
2: -0.17% : returns -0.1% (0.9806) sf doesn't show the second decimal point
3: -0.3% : returns -0.3% (0.9661)

As above with '--scale 0.98%'
0% returns 0.9661/-1.2db

Appears that (a)3.93.1 needs the command-line (b) LAME encodes 'true' (at least for test pulse used)
Madrigal
@rhadinocentrus:

89dB is an arbitrary level, adopted by ReplayGain, Mp3Gain, WaveGain and others, as a "normal" target gain level at which the majority of audio tracks will not clip. This may be a somewhat oversimplified answer, but it should suffice for the present topic of discussion.

edit: The following is an extract from the Mp3Gain help file:

"The Target 'Normal' Volume is the average volume you want each of your
mp3 files (in Track mode) or each of your mp3 collections (in Album
mode) to have."

"The default is 89.0 dB because most mp3s will not have clipping at this
volume level.
"

"('Clipping' means that when the mp3 file is decoded by your player, some
of the sound samples will be too loud. The player will 'clip' these
samples so that they do not exceed the maximum allowable value. This
clipping creates a sort of rough, 'scratchy' sound during loud parts of
the song.)"


Regards,
Madrigal
Pio2001
Replaygain comes with a clipping prevention option. The target level doesn't matter once clipping prevention is enabled.

The problem is that clipping prevention can't be activated without using replaygain target level, neither in Winamp 2, nor in Foobar.

So, personally, I just use Foobar without replaygain, and the volume DSP set at -2 db. Only the worst CDs still clip from time to time, but this is negligible compared to the havoc that the audio must have suffered in order to still clip after a -2 db scaling.
Note that the Foobar DSP volume is special. I tested it with MPC only, and it acts during the decoding, which allows it to prevent clipping.
Winamp, the soundcard, or Windows volume setting won't help anything.
markanini
I have got original wav files that don't clip but peak at 0 dB.
When I encode these files wth lame or ogg for example and afterwords scan replaygain values the files seem to peak above 0 db.
So the solution whould be to apply replaygain to the original wav?
Which method of applying replaygain to wav is the best?

Will this allways prevent clipping?
Let's say that there is a file with a replaygain vaue of 0 dB but it peaks 0 dB, wont it clip anyway?
Jasper
In general you apply REPLAYGain on playback. ReplayGain sets a few values in the (encoded) file that specify things like peak level and how much gain the song SHOULD be given in order to reach a certain target loudness (actually I don't know whether it stores the gain or the current loudness, but the result is more or less the same). Then when the file is played back the output is modified according to this information, so the audio content of your files is never modified by ReplayGain, it only changes the data in memory when it is played back. This also means a player has to support ReplayGain in order to make use of it.
To be able to use ReplayGain in Foobar2000 you first encode your songs, then use Foobar2000 (or some other tool) to scan the files and put the ReplayGain information in their tags. Then you should make sure Foobar2000 is set up to make use of ReplayGain (in the playback section). Now your files should all come out at approximately the same level, with very loud tracks being quieter. I don't know the internals of Foobar2000, but Pio2001's posts seem to suggest that at least some formats will clip a lot less when using this (as it would be similar to lowering the volume, only dependent on the actual content of the song).
I have no experience with ReplayGain in other programs, so I can't comment on that.

The --scale parameter is used during encoding and WaveGain is used on the original wav (before encoding). I don't know how --scale works exactly, but WaveGain simply scales the original wav (using the ReplayGain algorithms), so it doesn't need any tags or settings in the player, but it also isn't lossless (although the quality is most likely quite good).
Pio2001
QUOTE(Jasper @ Apr 20 2004, 10:17 AM)
Pio2001's posts seem to suggest that at least some formats will clip a lot less when using this (as it would be similar to lowering the volume, only dependent on the actual content of the song).

I didn't suggest such a thing.
Replaygain adjusts the perceptual level so that it is 14 db below the full scale. Since this is below the recording level of many CDs, it helps preventing clipping as a side effect.
But clipping prevention is also an optional function of replaygain. Turn it on and it ensures that the decoded never clips the signal.

The DSP volume setting of which I spoke has nothing to do with replaygain. It is the standard volume setting of Foobar.
Jasper
QUOTE
I didn't suggest such a thing.
Replaygain adjusts the perceptual level so that it is 14 db below the full scale. Since this is below the recording level of many CDs, it helps preventing clipping as a side effect.

I merely meant that since your post suggests that altering the volume (using the volume DSP) has an effect on the decoded signal BEFORE possible clipping occurs that it is plausible that ReplayGain would also be applied before any possible clipping (due to the decoding process) occurs (so before it is converted to an integer value to send to the soundcard), which means ReplayGain can prevent clipping by adjusting the level of the signal (similar to what you do with the volume DSP). My wording could have been a bit more clear on this.
Madrigal
QUOTE(Madrigal @ Apr 17 2004, 08:20 AM)
1. Download and install WaveGain, available here.

2. Scan the wav files to be encoded, using "Just calculate, don't apply", and make a note of the suggested scale factor, e.g. 0.xxxx.

3. Include --scale 0.xxxx at the end of your encoder command line,
e.g. --alt-preset standard --scale 0.xxxx

Regards,
Madrigal

@ markanini:

The --scale method as outlined above is by far the best for your needs, as it is lossless and player-independent. As long as your original wavs do not clip, as you indicated, the compressed files encoded as above should not clip either. In the unlikely event that they do, simply check the "Extra gain" box in WaveGain, punch in some negative value such as -1 or -2 dB, scan the wavs again and re-encode with the new, lower scale factor.

I am not familiar with Vorbis at all, but hopefully the --scale switch or equivalent is available for encoding ogg files as well.

Regards,
Madrigal
Jebus
QUOTE(Jurg98 @ Apr 17 2004, 06:33 AM)
To Madrigal:

But does that eliminate clipping?? Don't think so... What you might avoid is decoder clipping (let's call it output clipping), since the tracks will be ripped at a lower volume. But you'll still have the same cut-off waveforms (they won't be at the top of the scale though, but a notch lower)...

Again, correct me if I'm wrong... smile.gif

Well, technically this is the process for normalizing mp3s, but in my experience it removes clipping from 100% of my tracks. Try it - run MP3gain clipping analysis afterwards and see if you have any clipping tracks.

If you are using MP3 and aren't interested in normalizing, i would recommend encoding first and then just running a "max noclip gain adjustment" in mp3gain.

Understand that clipped samples created during encoding are not actually deleted, they just don't get played back. Lowering the gain restores them, so this will work perfectly well. Clipping introduced in the original mastering process cannot be restored.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.