简体   繁体   English

如何处理WAV文件的增益?

[英]How do I manipulate gain on a wav file?

I have a wav file, and I want to play around with the gain on the sound clip (boost in softer areas, reduce at others. And I need to do this to 1k+ files) and save it back to disk. 我有一个wav文件,我想试一下声音片段的增益(在较软的区域中增强,在其他区域中减小。我需要将其保存为1k +个文件)并将其保存回磁盘。

.NET doesn't seem to have a sound library, and none of the audio libraries I can find seem to have controls for this (Accord, Naudio, SDL2, SharpDX; though I could just not be seeing what I'm looking for). .NET似乎没有声音库,而且我找不到的音频库似乎都没有对此的控件(Accord,Naudio,SDL2,SharpDX;尽管我只是看不到自己在寻找什么) 。 I've also looked into the WAV format to try and do it directly to the byte stream, but I can't find anything more specific than the data block is left-right pair samples. 我还研究了WAV格式,尝试直接对字节流进行处理,但是除了数据块是左右对样本之外,我找不到其他更具体的东西。

So, with access to a .wav file, how can I adjust the loudness on a per sample basis? 因此,通过访问.wav文件,如何在每个样本的基础上调整响度? If possible, I'd also like a solution that would allow me to seamlessly work with other sound file formats as well. 如果可能的话,我还希望有一个解决方案,让我也可以无缝处理其他声音文件格式。

NAudio seems to provide what you are looking for NAudio似乎提供您想要的东西

ISampleProvider ISampleProvider

The strength of IWaveProvider is that it can be used to represent audio in any format. IWaveProvider的优势在于它可以用来表示任何格式的音频。 It can be used for 16,24 or 32 bit PCM audio, and even for compressed audio (MP3, G.711 etc). 它可用于16,24或32位PCM音频,甚至可用于压缩音频(MP3,G.711等)。 But if you are performing any kind of signal processing or analysis on the audio, it is very likely that you want the audio to be in 32 bit IEEE floating point format. 但是,如果您要对音频执行任何类型的信号处理或分析,则很有可能希望音频采用32位IEEE浮点格式。 And it can be a pain to try to read floating point values out of a byte[] in C#. 尝试从C#中的byte []中读取浮点值可能会很痛苦。

So ISampleProvider defines an interface where the samples are all 32 bit floating point 因此ISampleProvider定义了一个接口,其中的样本都是32位浮点

public interface ISampleProvider { 
    WaveFormat WaveFormat { get; } 
    int Read(float[] buffer, int offset, int.count); 
}

https://github.com/naudio/NAudio/blob/master/Docs/WaveProviders.md#isampleprovider https://github.com/naudio/NAudio/blob/master/Docs/WaveProviders.md#isampleprovider

Create custom SampleProvider that takes an input SampleProvider in your constructor. 创建自定义SampleProvider,在构造函数中采用输入SampleProvider。 Use input Sample Provider to get samples, multiply by your gain: Output sample = gain x input sample 使用输入样本提供者获取样本,乘以您的增益:输出样本=增益x输入样本

Use WaveFileWriter.CreateWaveFile feeding customSampleProvider.ToWaveProvider() to the appropriate argument, to output back to wav file. 使用WaveFileWriter.CreateWaveFile将customSampleProvider.ToWaveProvider()输入适当的参数,以输出回wav文件。

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

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