0

I'm using NAudio to open a wav file.

After I have used the SimpleCompressor class I also must do some normalizing the volume of the file to 0db, but I have no idea how to do that.

At the moment I have this:

string strCompressedFile = "";

byte[] WaveData = new byte[audio.Length];
    
SimpleCompressorStream Compressor = new SimpleCompressorStream(audio);
Compressor.Enabled = true;

if (Compressor.Read(WaveData, 0, WaveData.Length) > 0)
{
    //doing the normalizing now
}

How can I get the volume from the new byte array WaveData and how can I change it?

In WaveData is the entire wav file including the file header.

Oreo
  • 529
  • 3
  • 16
computermaus
  • 63
  • 2
  • 8

1 Answers1

0

You definitely can change individual sample value so that it fits maximum level:

string strCompressedFile = "";

byte[] WaveData = new byte[audio.Length];

SimpleCompressorStream Compressor = new SimpleCompressorStream(audio);
Compressor.Enabled = true;

byte maxLevel = 20;

if (Compressor.Read(WaveData, 0, WaveData.Length) > 0)
{
    for (int i = 0; i < audio.Length; i++)
    {
        if (WaveData[i] > maxLevel)
        {
            WaveData[i] = maxLevel;
        }
    }
}

I've added a loop which iterates through all the samples and if it's value is higher that maxLevel we set it to maxLevel.

Alex Sikilinda
  • 2,928
  • 18
  • 34
  • I'm not so familiar with wav files but Can I just set each byte to same number? Aren't there some bytes of header? In your example you start by byte 0. The other thing that I wonder: Is the value of each byte the volume? I thought it's the sound(e.g. recorded voice) itself instead... I thought for normalizing I have to get the max volume and then calculate it to the loudest value and then use the same factor for all other volume numbers. But I have no idea how to get the volume and what is the max possible value – computermaus May 23 '17 at 16:12
  • Samples are digital representation of analog signal, so each sample value is equal to analog signal amplitude in given time. If you set each byte to the same number it would represent straight line analog signal, which is basically silence. Instead, you need to somehow adjust picks. – Alex Sikilinda May 23 '17 at 16:35
  • For recording the sound I found a code piece which allowed me to implement a volume meter as a progressbar. but to be honest I don't understand everything from the piece of code: double sum = 0; for (var i = 0; i < e.BytesRecorded; i = i + 2) { double sample = BitConverter.ToInt16(e.Buffer, i) / 32768.0; sum += (sample * sample); } double rms = Math.Sqrt(sum / (e.BytesRecorded / 2)); var decibel = 20 * Math.Log10(rms); can I use that somehow too? – computermaus May 23 '17 at 17:16