使用 NAudio 更改 wav 文件(更改为 16KHz 和 8 位)

发布于 2024-11-19 04:38:24 字数 515 浏览 2 评论 0原文

我想使用 NAudio 将 WAV 文件更改为 8KHz 和 8bit。

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

但是当我播放输出文件时,声音只是嘶嘶声。我的代码是正确的还是有问题?

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

谢谢。

I want to change a WAV file to 8KHz and 8bit 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);
            }

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

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉徒眸意 2024-11-26 04:38:24

几点提示:

  • 您需要使用 WaveFormatConversionStream 来实际从一种采样率/位深度转换为另一种采样率/位深度 - 您只是将原始音频放入具有错误波形格式的新文件中。
  • 您可能还需要分两步进行转换 - 首先更改采样率,然后更改位深度/通道数。这是因为底层 ACM 编解码器并不总是能够一步完成您想要的转换。
  • 您应该使用 WaveFileReader 来读取输入文件 - 您只希望转换文件的实际音频数据部分,但您当前正在将包括 RIFF 块在内的所有内容复制到新文件中,就像它们是音频数据一样。
  • 8 位 PCM 音频通常听起来很糟糕。使用 16 位,或者如果必须使用 8 位,请使用 G.711 u-law 或 a-law
  • 音频下采样可能会导致混叠。为了做好它,您需要首先实现一个低通滤波器。不幸的是,这并不容易,但有些网站可以帮助您为您正在进行的特定下采样生成切比雪夫低通滤波器的系数。

下面是一些示例代码,展示了如何从一种格式转换为另一种格式。请记住,您可能需要分多个步骤进行转换,具体取决于输入文件的格式:

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

A few pointers:

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

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);
    } 
}
瞳孔里扚悲伤 2024-11-26 04:38:24

以下代码解决了我处理 G.711 Mu-Law 的问题,其中 vox 文件扩展名为 wav 文件。否则,我一直收到 WaveFileReader 的“No RIFF Header”错误。

 FileStream fileStream = new FileStream(fileName, FileMode.Open);
           var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
           var reader = new RawSourceWaveStream(fileStream, waveFormat);
            using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
            {
                WaveFileWriter.CreateWaveFile(fileName.Replace("vox", "wav"), convertedStream);
            }
           fileStream.Close();

The following code solved my problem dealing with G.711 Mu-Law with a vox file extension to wav file. I kept getting a "No RIFF Header" error with WaveFileReader otherwise.

 FileStream fileStream = new FileStream(fileName, FileMode.Open);
           var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
           var reader = new RawSourceWaveStream(fileStream, waveFormat);
            using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
            {
                WaveFileWriter.CreateWaveFile(fileName.Replace("vox", "wav"), convertedStream);
            }
           fileStream.Close();
久伴你 2024-11-26 04:38:24
            openFileDialog openFileDialog = new openFileDialog();
            openFileDialog.Filter = "Wave Files (*.wav)|*.wav|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;


            WaveFileReader reader = new NAudio.Wave.WaveFileReader(dpmFileDestPath);

            WaveFormat newFormat = new WaveFormat(8000, 16, 1);

            WaveFormatConversionStream str = new WaveFormatConversionStream(newFormat, reader);

            try
            {
                 WaveFileWriter.CreateWaveFile("C:\\Konvertierten_Dateien.wav", str);
            }
            catch (Exception ex)
            {
                 MessageBox.Show(String.Format("{0}", ex.Message));
            }
            finally
            {               
                str.Close();
            }

                 MessageBox.Show("Konvertieren ist Fertig!");
            }
            openFileDialog openFileDialog = new openFileDialog();
            openFileDialog.Filter = "Wave Files (*.wav)|*.wav|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;


            WaveFileReader reader = new NAudio.Wave.WaveFileReader(dpmFileDestPath);

            WaveFormat newFormat = new WaveFormat(8000, 16, 1);

            WaveFormatConversionStream str = new WaveFormatConversionStream(newFormat, reader);

            try
            {
                 WaveFileWriter.CreateWaveFile("C:\\Konvertierten_Dateien.wav", str);
            }
            catch (Exception ex)
            {
                 MessageBox.Show(String.Format("{0}", ex.Message));
            }
            finally
            {               
                str.Close();
            }

                 MessageBox.Show("Konvertieren ist Fertig!");
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文