Exemple #1
0
        internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long position, int cData)
        {
            RIFFHDR  rIFFHDR  = new RIFFHDR(0);
            BLOCKHDR bLOCKHDR = new BLOCKHDR(0);
            DATAHDR  dATAHDR  = new DATAHDR(0);
            int      num      = Marshal.SizeOf((object)rIFFHDR);
            int      num2     = Marshal.SizeOf((object)bLOCKHDR);
            int      length   = waveEx.Length;
            int      num3     = Marshal.SizeOf((object)dATAHDR);
            int      num4     = num + num2 + length + num3;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
                try
                {
                    rIFFHDR._len = num4 + cData - 8;
                    binaryWriter.Write(rIFFHDR._id);
                    binaryWriter.Write(rIFFHDR._len);
                    binaryWriter.Write(rIFFHDR._type);
                    bLOCKHDR._len = length;
                    binaryWriter.Write(bLOCKHDR._id);
                    binaryWriter.Write(bLOCKHDR._len);
                    binaryWriter.Write(waveEx.ToBytes());
                    dATAHDR._len = cData;
                    binaryWriter.Write(dATAHDR._id);
                    binaryWriter.Write(dATAHDR._len);
                    stream.Seek(position, SeekOrigin.Begin);
                    stream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                }
                finally
                {
                    ((IDisposable)binaryWriter).Dispose();
                }
            }
        }
Exemple #2
0
 internal void PlayWaveFile(AudioData audio)
 {
     try
     {
         if (!string.IsNullOrEmpty(audio._mimeType))
         {
             WAVEFORMATEX wAVEFORMATEX = default(WAVEFORMATEX);
             wAVEFORMATEX.nChannels       = 1;
             wAVEFORMATEX.nSamplesPerSec  = 8000;
             wAVEFORMATEX.nAvgBytesPerSec = 8000;
             wAVEFORMATEX.nBlockAlign     = 1;
             wAVEFORMATEX.wBitsPerSample  = 8;
             wAVEFORMATEX.cbSize          = 0;
             string mimeType = audio._mimeType;
             if (!(mimeType == "audio/basic"))
             {
                 if (!(mimeType == "audio/x-alaw-basic"))
                 {
                     throw new FormatException(SR.Get(SRID.UnknownMimeFormat));
                 }
                 wAVEFORMATEX.wFormatTag = 6;
             }
             else
             {
                 wAVEFORMATEX.wFormatTag = 7;
             }
             Begin(wAVEFORMATEX.ToBytes());
             try
             {
                 byte[] array = new byte[(int)audio._stream.Length];
                 audio._stream.Read(array, 0, array.Length);
                 Play(array);
             }
             finally
             {
                 WaitUntilDone();
                 End();
             }
         }
         else
         {
             BinaryReader binaryReader = new BinaryReader(audio._stream);
             try
             {
                 byte[] waveFormat = GetWaveFormat(binaryReader);
                 if (waveFormat == null)
                 {
                     throw new FormatException(SR.Get(SRID.NotValidAudioFile, audio._uri.ToString()));
                 }
                 Begin(waveFormat);
                 try
                 {
                     while (true)
                     {
                         DATAHDR dATAHDR = default(DATAHDR);
                         if (audio._stream.Position + 8 >= audio._stream.Length)
                         {
                             break;
                         }
                         dATAHDR._id  = binaryReader.ReadUInt32();
                         dATAHDR._len = binaryReader.ReadInt32();
                         if (dATAHDR._id == 1635017060)
                         {
                             byte[] buffer = Helpers.ReadStreamToByteArray(audio._stream, dATAHDR._len);
                             Play(buffer);
                         }
                         else
                         {
                             audio._stream.Seek(dATAHDR._len, SeekOrigin.Current);
                         }
                     }
                 }
                 finally
                 {
                     WaitUntilDone();
                     End();
                 }
             }
             finally
             {
                 ((IDisposable)binaryReader).Dispose();
             }
         }
     }
     finally
     {
         audio.Dispose();
     }
 }