c# - Normalize volume in a 32 bit PCM Wav with NAudio -


is there way normalize volume in 32-bit wav file using naudio?

if volume clipping, want downward normalize , vice versa if volume low.

there no built-in feature, if use audiofilereader can examine values of samples find max absolute sample value. can work out how original file can amplified without clipping.

then can use volume property of audiofilereader amplify audio, , use wavefilewriter.createwavefile write out new (ieee floating point) wav file. wavefilewriter.createwavefile16 used if wanted 16 bit output after normalizing.

here's simple sample code

var inpath = @"e:\audio\wav\input.wav"; var outpath = @"e:\audio\wav\normalized.wav"; float max = 0;  using (var reader = new audiofilereader(inpath)) {     // find max peak     float[] buffer = new float[reader.waveformat.samplerate];     int read;         {         read = reader.read(buffer, 0, buffer.length);         (int n = 0; n < read; n++)         {             var abs = math.abs(buffer[n]);             if (abs > max) max = abs;         }     } while (read > 0);     console.writeline($"max sample value: {max}");      if (max == 0 || max > 1.0f)         throw new invalidoperationexception("file cannot normalized");      // rewind , amplify     reader.position = 0;     reader.volume = 1.0f / max;      // write out new wav file     wavefilewriter.createwavefile(outpath, reader); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -