Normalizing a Wave File in VB/C#

Viewed 148

I currently have a project that grabs raw data files and produces wave files by incorporating a wave header. However, the file needs to be normalized. When importing into Audacity and using the normalize effect, the file plays as expected. However I'm not sure how to do this in the code. The code is written in VB.net.

I have found this example: https://markheath.net/post/normalize-audio-naudio Which does what I am looking for, but I don't have NAudio incorporated and would like to avoid the dependency if I can.

1 Answers

As that example states:

It’s simply amplifying every sample in the file by the largest amount possible without causing any clipping.

Clipping happens when an amplified (i.e., scaled) sample (i.e., value) becomes lesser/greater than the minimum/maximum value representable in the targeted wave format. Assuming the format is 16-bit (signed) samples, the minimum and maximum values are −32,768 and 32,767. (In the example, the values are converted to floating point numbers, presumably, between -1.0 and 1.0.)

So, you must loop over the samples in a raw data file to find the sample with the greatest absolute value v, and, then, multiply all of the samples by 32,767/v.

Related