本文介绍了改变WAV文件(16KHz的和8位),使用n音讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换一个wav文件,以8KHz的和8位使用n音讯。

I want to change a wav file to 8KHz and 8bit with using NAudio.

            WaveFormat format1 = new WaveFormat(8000, 8, 1);
            byte[] waveByte = HelperClass.ReadFully(File.OpenRead(wavFile));
            Wave
            using (WaveFileWriter writer = new WaveFileWriter(outputFile, format1))
            {
                writer.WriteData(waveByte, 0, waveByte.Length);
            }

但是当我玩输出文件,声音是唯一的咝咝声。难道我的code是正确的或有什么不好?

but when I play the output file, the sound is only sizzle. Is my code is correct or what is wrong?

如果我设置WAVEFORMAT为WAVEFORMAT(44100,16,1),它工作正常。

If I set WaveFormat to WaveFormat(44100, 16, 1), it works fine.

感谢。

推荐答案

几个指针:

  • 您需要使用WaveFor​​matConversionStream实际上从一个采样率/比特深度转换到另一种 - 你只是把原来的音频与错误的WAVE格式的新文件
  • 您可能还需要两个步骤转换 - 首先改变采样率,然后更改位深度/通道数。这是因为底层的ACM codeCS不能总是做你想要转换的一个步骤。
  • 您应该使用WaveFileReader读取输入文件 - 你只需要在文件的实际音频数据部分地转化,但目前正在复制一切,包括RIFF块,仿佛它们是音频数据到新文件
  • 在8位PCM音频通常听起来很可怕。使用16位,或者如果你必须有8位,可使用G.711 U-法律或法
  • 下采样音频可能会导致混淆。要做到这一点,那么您需要首先实现一个低通滤波器。这不幸的是不容易的,但也有网站,帮助您生成一个切比雪夫低通滤波器的具体系数下采样,你在做什么。
  • You need to use a WaveFormatConversionStream to actually convert from one sample rate / bit depth to another - you are just putting the original audio into the new file with the wrong wave format.
  • You may also need to convert in two steps - first changing the sample rate, then changing the bit depth / channel count. This is because the underlying ACM codecs can't always do the conversion you want in a single step.
  • You should use WaveFileReader to read your input file - you only want the actual audio data part of the file to get converted, but you are currently copying everything including the RIFF chunks as though they were audio data into the new file.
  • 8 bit PCM audio usually sounds horrible. Use 16 bit, or if you must have 8 bit, use G.711 u-law or a-law
  • Downsampling audio can result in aliasing. To do it well you need to implement a low-pass filter first. This unfortunately isn't easy, but there are sites that help you generate the coefficients for a Chebyshev low pass filter for the specific downsampling you are doing.

下面是一些例子code,展示了如何从一种格式转换为另一种。请记住,你可能需要做的,这取决于你的输入文件的格式多个步骤的转换:

Here's some example code showing how to convert from one format to another. Remember that you might need to do the conversion in multiple steps depending on the format of your input file:

using (var reader = new WaveFileReader("input.wav"))
{
    var newFormat = new WaveFormat(8000, 16, 1);
    using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
    {
        WaveFileWriter.CreateWaveFile("output.wav", conversionStream);
    }
}

这篇关于改变WAV文件(16KHz的和8位),使用n音讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:47