如何为内存流中的音频数据创建WAV文件头?

时间:2018-09-24 09:38:08

标签: c# wav naudio

我需要通过C#将2个WAV文件加入字节数组中。我收到此异常:

  

不是WAVE文件-没有RIFF标头

正如我在网上搜索时所发现的那样,问题在于字节数组的标头不是WAV格式。但是我找不到有关添加标头的任何帮助。这是我的代码:

public static void Concatenate(string outputFile, IEnumerable<byte[]> sourceFiles)
{
    byte[] buffer = new byte[1024];
    WaveFileWriter waveFileWriter = null;

    try
    {
        foreach (byte[] sourceFile in sourceFiles)
        {
            Stream streamReader = new MemoryStream(sourceFile);
            using (WaveFileReader reader = new WaveFileReader(streamReader))
            {
                if (waveFileWriter == null)
                {
                    // first time in create new Writer
                    waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                }
                else
                {
                    if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                    {
                        throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                    }
                }

                int read;
                while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    waveFileWriter.WriteData(buffer, 0, read);
                }
            }
        }
    }
    finally
    {
        if (waveFileWriter != null)
        {
            waveFileWriter.Dispose();
        }
    }
}

如何将WAV文件头添加到字节数组?还有另一个问题:我必须将标头一一添加到字节数组中,或者首先将两个字节数组并置,然后添加标头?

0 个答案:

没有答案