简体   繁体   English

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

[英]Increase volume by X decibels and rewrite audio file

I'm new in naudio.我是新手。 And I wanna increase volume by X db.我想将音量增加 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(均方根):

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

with x_i being the i-th sample and N the number of samples. x_i是第i个样本,N是样本数。 The RMS is the average amplitude of your signal. RMS是信号的平均幅度。 Use 采用

RMS_dB = 20*log(RMS/ref)

(with ref being 1.0 or 32767.0) (参考是1.0或32767.0)

to convert it to a decibel value. 将其转换为分贝值。

You may calculate this RMS value before and after you change the volume. 您可以在更改音量之前和之后计算此RMS值。 The difference should be erxactly the dB you used in your IncreaseVolume() 差异应该恰好是您在GrowthVolume IncreaseVolume()使用的dB

Just adding a comment for people The input db in line is decibel and you need to convert it into amplitude.只是为人们添加评论在线输入db是分贝,您需要将其转换为幅度。 double linearScalingRatio = Math.Pow(10d, db / 10d);双线性缩放比率 = Math.Pow(10d, db / 10d);

The table is as follows- https://blog.demofox.org/2015/04/14/decibels-db-and-amplitude/该表如下- 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.所以你需要在 db 中提供 6 的值,使它的负载增加一倍。 Another point already mentioned it should be double linearScalingRatio = Math.Pow(10d, db / 20d);已经提到的另一点应该是 double linearScalingRatio = Math.Pow(10d, db / 20d);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM