Author Topic: Detect clipping in wav file and difference to 0dB  (Read 2529 times)

metabolic

  • Posts: 41
Hi everybody,

My Problem is for example i have a wav file which is overdriven. At some point in the data there is one ore more samples which are louder than 0dB.
I would like to detect that and further i would need to know how much it was above 0dB.

I would use BASS_ChannelGetData to get the raw sample data but i dont know how to dectect that the file is overdriven and especially to get the dB value of the loudest peak to calculate the difference to 0dB.

All of my tests gave me 0dB for a file which was manually altered to have a peak at 2dB.

In another approach i tried it this way:

Code: [Select]
var streamBuffer = File.ReadAllBytes(@"C:\test peak übersteuert_1.wav");
            double peak = 0;

            for (var i = 0; i < streamBuffer.Length;  i = i+8)
            {
                var sample = BitConverter.ToDouble(streamBuffer, i);

                if (sample > peak)
                    peak = sample;
            }

            var db = 20 * Math.Log10(peak / Double.MaxValue);

This returns 0dB for a file which is overdriven. Which is logic but i just don't know how to get the amount of dB of the maxium peak so i can calculate the difference to 0dB. as far as i understand the problem this should be possible in a way.
For Example Audacity and other tools can at least detect samples in the file which are overdriven.

Thans for your help in advance,
metabolic

Bello

  • Posts: 30
Re: Detect clipping in wav file and difference to 0dB
« Reply #1 on: 1 Mar '14 - 12:23 »
This information cannot be extracted from a normal wav file, as 0dB fills up all the audio-bits.
You could estimate the hight of the peak based on the rest of the data in the file, however this might not be trivial...

Hans van Zutphen, with a Dutch company called Sound Audio Processing has developed such an algorithm called Perfect DeClipper. http://www.perfectdeclipper.com/ Maybe you can use/licence this?

3delite

  • Posts: 907
Re: Detect clipping in wav file and difference to 0dB
« Reply #2 on: 1 Mar '14 - 23:18 »
Bello is right about 8/16/24 bit WAV files, but a 32 bit float file can give you values outside +1/-1 range. If you experiment with this use 32 bit float WAV files.

Perfect DeClipper seems interesting though, I have been looking for something like this recently. So thank you for sharing this.

Rescator

  • Guest
Re: Detect clipping in wav file and difference to 0dB
« Reply #3 on: 1 Mar '14 - 23:35 »
It is not possible to detect the peak of clipped audio.
If audio is clipped then it is damaged permanently.

That being said, if a lossy encoder is used and later decoded then you may actually see peaks above 0dBFS and you can detect this during the decoding stage (but before playback stage where the audio will be clipped) provided you make sure the entire decoding/mixing stage is floating point.

Also with lossless audio that is stored as floating point the audio may have peaks above 0dBFS so you can detect that in WAVs too.
With WAV that is 16bit or 24bit the audio is clipped when saved so there is no way of knowing.

Also note that lossless format decoders that decode to 16bit (or 24bit) may cause clipping or distortion on music that are victims of the loudness war.

The guy that created that DeClipper is pretty good, I used his http://www.stereotool.com/ to "fix" some music that was badly clipped, it managed to get rid of some nasty crackling (a symptom of clipping) but it can not do real magic.

Whenever possible ask for a better master. With EBU R128 and the K-System there is no need for overly compressing stuff. 16bit audio has more than enough depth to reproduce the audio even with a RMS of -23dBFS.

One way to detect what is called "True Peak" is to oversample (not the same as upsample) by 4x this is actually mentioned in the EBU R128 docs.
It should allow you to detect peaks that would reconstruct to be above 0dBFS and hence clip during playback.


Also note that theoretically a True Peak can be as high as +11dB above 0dBFS, I created such a example which can be found on Hydrogen Audio.
In practice you will normally only see True Peaks that are 3dB above 0dBFS on average, and if you use ReplayGain or a similar technology during playback (or just reduce the volume) then such peaks (and thus the clipping that could occur) will not be an issue.

Please note that even if the volume is lowered there may be still actual clipping heard, but this is due to the loudness war and hyper compression of the audio signal itself, nothing can fix that, not even the cool DeClipper mentioned above sadly.