wav 标头错误 NAudio c# [英] wav header error NAudio c#

查看:91
本文介绍了wav 标头错误 NAudio c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我更改 wav 文件的 waveFormat 时,数据"标识符向右移动.表示 da 块开头的数据标识符必须位于 0x24 位置,但在我使用 NAudio 库更改 waveFormat 后,它会移动到 0x26 位置.

When I change the waveFormat of a wav file the "data" identifier shifts to the right. The data identifier which represents the beginning of the da chunk has to be at the 0x24 position but after I change the waveFormat with the NAudio library it shiffts to the 0x26 position.

WAV 格式

这是我用来更改 waveFormat 的代码:

This is the code I use to change the waveFormat:

private void TurnTo16bitsAudio(string path)
        {
            NAudio.Wave.WaveFileReader wave = new NAudio.Wave.WaveFileReader(@path);
            System.Console.WriteLine(wave.WaveFormat);
            if (wave.WaveFormat.BitsPerSample >= 16 && wave.WaveFormat.Channels<2)
            {
                wave.Dispose();
                return;
            }

            var newFormat = new WaveFormat(44100, 16, 1);
            var conversionStream = new WaveFormatConversionStream(newFormat, wave);
            WaveFileWriter.CreateWaveFile(@path + '1', conversionStream);
            wave.Dispose();
            wave = new NAudio.Wave.WaveFileReader(@path + '1');
            conversionStream = new WaveFormatConversionStream(newFormat, wave);
            WaveFileWriter.CreateWaveFile(@path, conversionStream);
            wave.Dispose();
            conversionStream.Dispose();
            System.IO.File.Delete(@path + '1');

        }

有可能使用 NAudio 更改 wav 标头或更改 waveFormat 而不移动数据"标识符

There is any possibility to change the wav header with NAudio or to change the waveFormat without shiffting the "data" identifier

推荐答案

WaveFormat 类中的方法 Serialize 永远不会在 Subchunk1Size 字段中输出 0x10.最小值为 0x12.

Method Serialize in WaveFormat class never outputs 0x10 in Subchunk1Size field. Minimum value is 0x12.

public virtual void Serialize(BinaryWriter writer)
{
    writer.Write((int)(18 + extraSize)); // wave format length
    writer.Write((short)Encoding);
    writer.Write((short)Channels);
    writer.Write((int)SampleRate);
    writer.Write((int)AverageBytesPerSecond);
    writer.Write((short)BlockAlign);
    writer.Write((short)BitsPerSample);
    writer.Write((short)extraSize);
}

额外的 2 个字节用于 ExtraParamSize,当编码设置为 WaveFormatEncoding.Pcm(其值 == 0)时会被忽略.这与引入 WAVEFORMATEX 时 Microsoft(Windows 2000 及更高版本)对 WAV 格式的更改有关.

Additional 2 bytes are for ExtraParamSize which is ignored when encoding is set to WaveFormatEncoding.Pcm (its value == 0). This is related to change in the WAV format done by Microsoft (Windows 2000 and later) when WAVEFORMATEX was introduced.

在这里您可以找到 subchunk1size 16/18 不同大小问题的答案:C++读取wav文件,subchunk1size = 18

Here you can find answers on subchunk1size 16/18 different size question: C++ read wav file, subchunk1size = 18

WAVEFORMATEX 结构的详细信息:https://msdn.microsoft.com/library/windows/hardware/ff538799

Detailed information about WAVEFORMATEX structure: https://msdn.microsoft.com/library/windows/hardware/ff538799

如果你真的想在没有 extraSize 字段的情况下强制创建 wav 头,你可以扩展 WaveFormat 类.

If you really want to force creation of wav header without extraSize field you can extend WaveFormat class.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 2)]
class CustomWaveFormat : WaveFormat
{
    public CustomWaveFormat(int rate, int bits, int channels) 
        : base(rate, bits, channels)
    {
        extraSize = 0;
    }

    public override void Serialize(BinaryWriter writer)
    {
        writer.Write((int)16); // wave format length
        writer.Write((short)Encoding);
        writer.Write((short)Channels);
        writer.Write((int)SampleRate);
        writer.Write((int)AverageBytesPerSecond);
        writer.Write((short)BlockAlign);
        writer.Write((short)BitsPerSample);
    }
}

用法:

...
    var newFormat = new CustomWaveFormat(44100, 16, 1);

    using (var conversionStream = new WaveFormatConversionStream(newFormat, waveFileReader))
    {
        WaveFileWriter.CreateWaveFile("example_new.wav", conversionStream);
    }
... 

这篇关于wav 标头错误 NAudio c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆