public static void WriteWavHeader(FileStream stream, FMOD.Sound sound, int length, Object usingFmod)
        {
            FMOD.SOUND_TYPE     type   = FMOD.SOUND_TYPE.UNKNOWN;
            FMOD.SOUND_FORMAT   format = FMOD.SOUND_FORMAT.NONE;
            int                 channels = 0, bits = 0, temp1 = 0;
            float               rate = 0.0f;
            float               temp = 0.0f;

            if (sound == null)
                return;
            lock(usingFmod) {
                sound.getFormat(ref type, ref format, ref channels, ref bits);
                sound.getDefaults(ref rate, ref temp, ref temp, ref temp1);
            }

            log.Info("WaveWriter.WriteWavHeader: sound format: dataLength " + length + ", type " + type + ", format " + format +
                ", channels " + channels + ", bits " + bits + ", rate " + rate);

            stream.Seek(0, SeekOrigin.Begin);

            FmtChunk            fmtChunk  = new FmtChunk();
            DataChunk           dataChunk = new DataChunk();
            WavHeader           wavHeader = new WavHeader();
            RiffChunk           riffChunk = new RiffChunk();

            fmtChunk.chunk = new RiffChunk();
            fmtChunk.chunk.id = new char[4];
            fmtChunk.chunk.id[0]     = 'f';
            fmtChunk.chunk.id[1]     = 'm';
            fmtChunk.chunk.id[2]     = 't';
            fmtChunk.chunk.id[3]     = ' ';
            fmtChunk.chunk.size      = Marshal.SizeOf(fmtChunk) - Marshal.SizeOf(riffChunk);
            fmtChunk.wFormatTag      = 1;
            fmtChunk.nChannels       = (ushort)channels;
            fmtChunk.nSamplesPerSec  = (uint)rate;
            fmtChunk.nAvgBytesPerSec = (uint)(rate * channels * bits / 8);
            fmtChunk.nBlockAlign     = (ushort)(1 * channels * bits / 8);
            fmtChunk.wBitsPerSample  = (ushort)bits;

            dataChunk.chunk = new RiffChunk();
            dataChunk.chunk.id = new char[4];
            dataChunk.chunk.id[0]    = 'd';
            dataChunk.chunk.id[1]    = 'a';
            dataChunk.chunk.id[2]    = 't';
            dataChunk.chunk.id[3]    = 'a';
            dataChunk.chunk.size     = (int)length;

            wavHeader.chunk = new RiffChunk();
            wavHeader.chunk.id = new char[4];
            wavHeader.chunk.id[0]   = 'R';
            wavHeader.chunk.id[1]   = 'I';
            wavHeader.chunk.id[2]   = 'F';
            wavHeader.chunk.id[3]   = 'F';
            wavHeader.chunk.size    = (int)(Marshal.SizeOf(fmtChunk) + Marshal.SizeOf(riffChunk) + length);
            wavHeader.rifftype = new char[4];
            wavHeader.rifftype[0]   = 'W';
            wavHeader.rifftype[1]   = 'A';
            wavHeader.rifftype[2]   = 'V';
            wavHeader.rifftype[3]   = 'E';

            /*
                Write out the WAV header.
            */
            IntPtr wavHeaderPtr = Marshal.AllocHGlobal(Marshal.SizeOf(wavHeader));
            IntPtr fmtChunkPtr  = Marshal.AllocHGlobal(Marshal.SizeOf(fmtChunk));
            IntPtr dataChunkPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dataChunk));
            byte   []wavHeaderBytes = new byte[Marshal.SizeOf(wavHeader)];
            byte   []fmtChunkBytes  = new byte[Marshal.SizeOf(fmtChunk)];
            byte   []dataChunkBytes = new byte[Marshal.SizeOf(dataChunk)];

            Marshal.StructureToPtr(wavHeader, wavHeaderPtr, false);
            Marshal.Copy(wavHeaderPtr, wavHeaderBytes, 0, Marshal.SizeOf(wavHeader));

            Marshal.StructureToPtr(fmtChunk, fmtChunkPtr, false);
            Marshal.Copy(fmtChunkPtr, fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk));

            Marshal.StructureToPtr(dataChunk, dataChunkPtr, false);
            Marshal.Copy(dataChunkPtr, dataChunkBytes, 0, Marshal.SizeOf(dataChunk));

            stream.Write(wavHeaderBytes, 0, Marshal.SizeOf(wavHeader));
            stream.Write(fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk));
            stream.Write(dataChunkBytes, 0, Marshal.SizeOf(dataChunk));
        }
 static uint WriteHeader(FileStream SubSoundStream, FMOD.Sound SubSound, string FSBFileStr)
 {
     uint Milliseconds = 0;
     uint RAWBytes = 0;
     uint PCMBytes = 0;
     uint PCMSamples = 0;
     uint Length = 0;
     SubSound.getLength(ref Milliseconds, TIMEUNIT.MS);					// Get the length of the current wave file
     SubSound.getLength(ref RAWBytes, TIMEUNIT.RAWBYTES);					// Get the length of the current wave file
     SubSound.getLength(ref PCMSamples, TIMEUNIT.PCM);					// Get the length of the current wave file
     SubSound.getLength(ref PCMBytes, TIMEUNIT.PCMBYTES);					// Get the length of the current wave file
     // Seek to the beginning of our newly created output file
     SubSoundStream.Seek(0, SeekOrigin.Begin);
     float Frequency = 0f;			// Set some default values that aren't very
     int DefPriority = 0;			// important because they'll get overwritten
     float DefPan = 0;
     float DefVolume = 0;
     FMOD.SOUND_TYPE FModSoundType = new SOUND_TYPE();
     FMOD.SOUND_FORMAT FModSoundFormat = new SOUND_FORMAT();
     int Channels = 0;
     int BitsPerSample = 0;
     // Get the default Frequency (important), and other stuff (not important)
     SubSound.getDefaults(ref Frequency, ref DefVolume, ref DefPan, ref DefPriority);
     // Get the actual sound format. We pretty much ignore this data since we know its going to be WAV, RIFF, PCM
     SubSound.getFormat(ref FModSoundType, ref FModSoundFormat, ref Channels, ref BitsPerSample);
     //Console.WriteLine("      MS={0}\tRAWBytes={1}\tPCMSamples={2}\tPCMBytes={3}", Milliseconds, RAWBytes, PCMSamples, PCMBytes);
     //Console.WriteLine("      Freq={0}\tChan={1}", Frequency, Channels);
     if (Switches.Contains("/doublefreq")) {
         if (Channels == 1) { Frequency = Frequency * 2; }
     }
     int BlockAlign = Channels * (BitsPerSample / 8);
     float DataRate = Channels * Frequency * (BitsPerSample / 8);
     PCMSamples = Convert.ToUInt32(Milliseconds * Frequency / 1000);
     // This is more of a sloppy hack. I don't know if I am just reading the file incorrectly, or using the wrong
     // version of the API, or the FSB files are incorrectly structured, but the metadata that comes from the file
     // seems to be incorrect. According to the WAV/RIFF specification and the FMOD API Documentation the
     // following line should be the correct computation. If you use just the following line you get a random
     // blip at the end of the sounds for the voice overs and the SFX don't play at all. A annoying
     // trial-and-error process revealed that the logic below works properly. I wish there was a better way to
     // identify which file we're parsing other than "SFX" in the name.
     // "Correct" Computation: Length = Convert.ToUInt32(PCMSamples * Channels * (BitsPerSample / 8));
     if (Path.GetFileName(FSBFileStr).Contains("VOBank")) {
         // If we're parsing the VOBank FSB
         if (Channels == 2) {					// Works for the Character Speech FSBs
             Length = Convert.ToUInt32(PCMSamples * Channels * (BitsPerSample / 8));
         } else {
             Length = PCMBytes * 2;
         }
     } else {
         // If we're parsing a SFX FSB
         if (Channels == 2) {					// Works for the SFX FSBs
             Length = PCMBytes;
         } else {
             //Length = Convert.ToUInt32(PCMSamples * Channels * (BitsPerSample / 8));
             //Console.WriteLine("      CustomLen={0}\tPCMBytes={1}\tDiff={2}", Convert.ToUInt32(PCMSamples * Channels * (BitsPerSample / 8)), PCMBytes, (Convert.ToUInt32(PCMSamples * Channels * (BitsPerSample / 8)) - PCMBytes) * 1.0 / PCMBytes * 1.0);
             if (Switches.Contains("/doublelen")) {
                 Length = PCMBytes * 2;
             } else {
                 Length = PCMBytes;
             }
         }
     }
     // Some debugging outputs:
     //Console.WriteLine("      MS={0}\tRAWBytes={1}\tPCMSamples={2}\tPCMBytes={3}", Milliseconds, RAWBytes, PCMSamples, PCMBytes);
     //Console.WriteLine("      Freq={0}\tChan={1}\tBits/Sam={2}\tDataRate={3}", Frequency, Channels, BitsPerSample, DataRate);
     // WAVE RIFF Format: http://www.topherlee.com/software/pcm-tut-wavformat.html
     // Write the actual specification to the header of the file.
     SubSoundStream.Write(BitConverter.GetBytes('R'), 0, 1);					// 0 - Letter R
     SubSoundStream.Write(BitConverter.GetBytes('I'), 0, 1);					// 1 - Letter I
     SubSoundStream.Write(BitConverter.GetBytes('F'), 0, 1);					// 2 - Letter F
     SubSoundStream.Write(BitConverter.GetBytes('F'), 0, 1);					// 3 - Letter F
     SubSoundStream.Write(BitConverter.GetBytes((long)0), 0, 4);		// 4,5,6,7 - Length of entire file minus 8
     SubSoundStream.Write(BitConverter.GetBytes('W'), 0, 1);					// 8 - Letter W
     SubSoundStream.Write(BitConverter.GetBytes('A'), 0, 1);					// 9 - Letter A
     SubSoundStream.Write(BitConverter.GetBytes('V'), 0, 1);					// 10 - Letter V
     SubSoundStream.Write(BitConverter.GetBytes('E'), 0, 1);					// 11 - Letter E
     SubSoundStream.Write(BitConverter.GetBytes('f'), 0, 1);					// 12 - Letter f
     SubSoundStream.Write(BitConverter.GetBytes('m'), 0, 1);					// 13 - Letter m
     SubSoundStream.Write(BitConverter.GetBytes('t'), 0, 1);					// 14 - Letter t
     SubSoundStream.Write(BitConverter.GetBytes(' '), 0, 1);					// 15 - Space
     SubSoundStream.Write(BitConverter.GetBytes((long)16), 0, 4);			// 16,17,18,19 - Size of following subchunk
     SubSoundStream.Write(BitConverter.GetBytes((int)1), 0, 2);				// 20,21 - Format code (PCM)
     SubSoundStream.Write(BitConverter.GetBytes((int)Channels), 0, 2);		// 22,23 - Number of channels
     SubSoundStream.Write(BitConverter.GetBytes((long)Frequency), 0, 4);			// 24,25,26,27 - Sampling Rate (Blocks Per Second)
     SubSoundStream.Write(BitConverter.GetBytes((int)DataRate), 0, 4);	// 28,29,30,31 - Data rate
     SubSoundStream.Write(BitConverter.GetBytes(BlockAlign), 0, 2);				// 32,33 - Data block size (bytes)
     SubSoundStream.Write(BitConverter.GetBytes(BitsPerSample), 0, 2);				// 34,35 - Bits per sample
     SubSoundStream.Write(BitConverter.GetBytes('d'), 0, 1);					// 36 - Letter d
     SubSoundStream.Write(BitConverter.GetBytes('a'), 0, 1);					// 37 - Letter a
     SubSoundStream.Write(BitConverter.GetBytes('t'), 0, 1);					// 38 - Letter t
     SubSoundStream.Write(BitConverter.GetBytes('a'), 0, 1);					// 39 - Letter a
     SubSoundStream.Write(BitConverter.GetBytes((long)Length), 0, 4);		// 40,41,42,43 - Size of Data Section
     return Length;
 }