简体   繁体   中英

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.

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

So, with access to a .wav file, how can I adjust the loudness on a per sample basis? 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

ISampleProvider

The strength of IWaveProvider is that it can be used to represent audio in any format. It can be used for 16,24 or 32 bit PCM audio, and even for compressed audio (MP3, G.711 etc). 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. And it can be a pain to try to read floating point values out of a byte[] in C#.

So ISampleProvider defines an interface where the samples are all 32 bit floating point

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

Create custom SampleProvider that takes an input SampleProvider in your constructor. Use input Sample Provider to get samples, multiply by your gain: Output sample = gain x input sample

Use WaveFileWriter.CreateWaveFile feeding customSampleProvider.ToWaveProvider() to the appropriate argument, to output back to wav file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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