3

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();
            }
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
Alexander
  • 691
  • 1
  • 11
  • 29
  • Surely the decibel increase is dependent upon the volume of your output? Say you double the volume in the file, for some users that will be an increase of X db, for other users that will be an increase of Y db for a single frame, for a user who has muted, it will be an increase of 0 db, no matter what you do. – Lukazoid Jun 05 '15 at 15:54
  • @Lukazoid wrong: As long as somthing arrives at the speaker, the (change in) dB value will be the same for all users/hardware – DrKoch Jun 05 '15 at 16:17
  • Simply multiply the samples by Math.Pow(10.0, decibel / 20.0). Watch out for clipping. – Hans Passant Jun 05 '15 at 17:45
  • Does this answer your question? [Calculate decibels](https://stackoverflow.com/questions/4152201/calculate-decibels) – JHBonarius Aug 03 '22 at 08:01
  • Next time tag Naudio. The accepted answer is now very general, this not specific for naudio. Naudio has its own methods and adapters to the audio stream. – JHBonarius Aug 03 '22 at 08:04

2 Answers2

3

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()

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • Try `double linearScalingRatio = Math.Pow(10d, db / 20.0);` in your code, then everything should be consistent. – DrKoch Jun 05 '15 at 20:34
  • This is nice theory, but the OP is using NAudio. NAudio provides its own interfaces. Do you have an NAudio specific answer? – JHBonarius Aug 03 '22 at 08:03
0

Just adding a comment for people The input db in line is decibel and you need to convert it into amplitude. double linearScalingRatio = Math.Pow(10d, db / 10d);

The table is as follows- https://blog.demofox.org/2015/04/14/decibels-db-and-amplitude/

so you need to provide value as 6 in db, to make it twice as load. Another point already mentioned it should be double linearScalingRatio = Math.Pow(10d, db / 20d);

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32397243) – Hoppeduppeanut Aug 09 '22 at 04:33