Search code examples
c#.netaudiowavnaudio

How do I write RIFF chunk data to a WAV file that was made with NAudio?


How do I write RIFF chunk data to the end of a WAV file? Yes, that's right, to the end of a WAV file. Why? Because that's the way the old program I'm replacing did it and the integrating programs are prepared for it that way and they cannot be modified.


Solution

  • Ok, so in the end it wasn't really a standard format. We found that out by leveraging the CueWaveFileWriter like Mark Heath stated. The format was actually something very custom and looked more like this:

    LIST - this is a one-time blurb of text.
    Tag Name (e.g. ICOP for copyright)
    4 bytes indicating the length of the following value + 1. The 1 was a (byte)0 terminator.
    Value
    

    so an example might look like this:

    ICOP<bh:15><bh:00><bh:00><bh:00>4/5/2013 10:48:40 AM<bh:00>
    

    so, to get this done, we just created a BinaryWriter and wrote them out directly like this:

    using (BinaryWriter bw = new BinaryWriter(writer))
    {
        foreach (var item in RIFFChunks)
        {
            if (!(bw.BaseStream.Position % 2 == 0))
            {
                bw.Write((byte)0);
            }
    
            bw.Write(Encoding.Default.GetBytes(item.Key));
            bw.Write(item.Value.Length + 1);
            bw.Write(Encoding.Default.GetBytes(item.Value));
            bw.Write((byte)0);
        }
    }
    

    and you may want to take note to this line if (!(bw.BaseStream.Position % 2 == 0)), that needed to exist because each tag name needed to start on an even offset. I honestly don't know why, I just know it did.

    I have to say though, there's a good chance I can now reverse engineer just about any file format. But really, what good is that?!?