I'm new in naudio. And I wanna increase volume by X db. I've written this piece of code:
public static void IncreaseVolume(string inputPath, string outputPath, double db)
{
double linearScalingRatio = Math.Pow(10d, db / 10d);
using (WaveFileReader reader = new WaveFileReader(inputPath))
{
VolumeWaveProvider16 volumeProvider = new VolumeWaveProvider16(reader);
using (WaveFileWriter writer = new WaveFileWriter(outputPath, reader.WaveFormat))
{
while (true)
{
var frame = reader.ReadNextSampleFrame();
if (frame == null)
break;
writer.WriteSample(frame[0] * (float)linearScalingRatio);
}
}
}
}
Ok, this works, but how can I find by how many decibels I've increased each sample? May anyone explain this moment for me and provide any examples?
UPDATE:
using (WaveFileReader reader = new WaveFileReader(inFile))
{
float Sum = 0f;
for (int i = 0; i < reader.SampleCount; i++)
{
var sample = reader.ReadNextSampleFrame();
Sum += sample[0] * sample[0];
}
var db = 20 * Math.Log10(Math.Sqrt(Sum / reader.SampleCount) / 1);
Console.WriteLine(db);
Console.ReadLine();
}
Your code looks good. To measure the average sound level of an audio sample you need to calculate the RMS (root mean square) of this sound level:
RMS := Sqrt( Sum(x_i*x_i)/N)
with x_i being the i-th sample and N the number of samples. The RMS is the average amplitude of your signal. Use
RMS_dB = 20*log(RMS/ref)
(with ref being 1.0 or 32767.0)
to convert it to a decibel value.
You may calculate this RMS value before and after you change the volume. The difference should be erxactly the dB
you used in your IncreaseVolume()