Example #1
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();
     }
 }
Example #2
0
        internal void PlayWaveFile(AudioData audio)
        {
            // allocate some memory for the largest header
            try
            {
                // Fake a header for ALaw and ULaw
                if (!string.IsNullOrEmpty(audio._mimeType))
                {
                    WAVEFORMATEX wfx = new();

                    wfx.nChannels       = 1;
                    wfx.nSamplesPerSec  = 8000;
                    wfx.nAvgBytesPerSec = 8000;
                    wfx.nBlockAlign     = 1;
                    wfx.wBitsPerSample  = 8;
                    wfx.cbSize          = 0;

                    switch (audio._mimeType)
                    {
                    case "audio/basic":
                        wfx.wFormatTag = (short)AudioFormat.EncodingFormat.ULaw;
                        break;

                    case "audio/x-alaw-basic":
                        wfx.wFormatTag = (short)AudioFormat.EncodingFormat.ALaw;
                        break;

                    default:
                        throw new FormatException(SR.Get(SRID.UnknownMimeFormat));
                    }

                    Begin(wfx.ToBytes());
                    try
                    {
                        byte[] data = new byte[(int)audio._stream.Length];
                        audio._stream.Read(data, 0, data.Length);
                        Play(data);
                    }
                    finally
                    {
                        WaitUntilDone();
                        End();
                    }
                }
                else
                {
                    BinaryReader br = new(audio._stream);

                    try
                    {
                        byte[] wfx = GetWaveFormat(br);

                        if (wfx == null)
                        {
                            throw new FormatException(SR.Get(SRID.NotValidAudioFile, audio._uri.ToString()));
                        }

                        Begin(wfx);

                        try
                        {
                            while (true)
                            {
                                DATAHDR dataHdr = new();

                                // check for the end of file (+8 for the 2 DWORD)
                                if (audio._stream.Position + 8 >= audio._stream.Length)
                                {
                                    break;
                                }
                                dataHdr._id  = br.ReadUInt32();
                                dataHdr._len = br.ReadInt32();

                                // Is this the WAVE data?
                                if (dataHdr._id == DATA_MARKER)
                                {
                                    byte[] ab = Helpers.ReadStreamToByteArray(audio._stream, dataHdr._len);
                                    Play(ab);
                                }
                                else
                                {
                                    // Skip this RIFF fragment.
                                    audio._stream.Seek(dataHdr._len, SeekOrigin.Current);
                                }
                            }
                        }
                        finally
                        {
                            WaitUntilDone();
                            End();
                        }
                    }
                    finally
                    {
                        ((IDisposable)br).Dispose();
                    }
                }
            }
            finally
            {
                audio.Dispose();
            }
        }