增加音量X分贝,重写音频文件

本文关键字:重写 音频 文件 分贝 增加 | 更新日期: 2023-09-27 17:53:34

我是naudio新手。我想让体积增加X db。我写了这段代码:

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);
            }
        }
    }
}

好的,这是有效的,但是我怎么知道每个样本增加了多少分贝呢?谁能给我解释一下这个时刻,举个例子?

更新:

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

增加音量X分贝,重写音频文件

您的代码看起来不错。要测量音频样本的平均声级,您需要计算该声级的RMS(均方根):

RMS := Sqrt( Sum(x_i*x_i)/N)

,其中x_i为第i个样本,N为样本个数。均方根值是信号的平均振幅。使用

RMS_dB = 20*log(RMS/ref)

(ref为1.0或32767.0)

将其转换为分贝值。

您可以在更改音量之前和之后计算此RMS值。差异应该是您在IncreaseVolume()

中使用的dB

只是为人们添加评论输入的db是分贝,你需要把它转换成幅度。double linearScalingRatio =数学。w(10d, db/10d);

表格如下-https://blog.demofox.org/2015/04/14/decibels-db-and-amplitude/

所以你需要提供值为6 in db,使其两倍的负载。另外一点已经提到了double linearScalingRatio =数学。