Exemplo n.º 1
0
        internal byte[] WaveFormat(byte[] targetWaveFormat)
        {
            // Get the Wave header if it has not been set by the user
            if (targetWaveFormat == null && _waveFormat == null)
            {
                // The registry values contains a default rate
                if (VoiceInfo.SupportedAudioFormats.Count > 0)
                {
                    // Create the array of bytes containing the format
                    targetWaveFormat = VoiceInfo.SupportedAudioFormats[0].WaveFormat;
                }
            }

            // No input specified and we already got the default
            if (targetWaveFormat == null && _waveFormat != null)
            {
                return(_waveFormat);
            }

            // New waveFormat provided?
            if (_waveFormat == null || !Array.Equals(targetWaveFormat, _waveFormat))
            {
                IntPtr   waveFormat   = IntPtr.Zero;
                GCHandle targetFormat = new();

                if (targetWaveFormat != null)
                {
                    targetFormat = GCHandle.Alloc(targetWaveFormat, GCHandleType.Pinned);
                }
                try
                {
                    waveFormat = _engine.GetOutputFormat(targetWaveFormat != null ? targetFormat.AddrOfPinnedObject() : IntPtr.Zero);
                }
                finally
                {
                    if (targetWaveFormat != null)
                    {
                        targetFormat.Free();
                    }
                }

                if (waveFormat != IntPtr.Zero)
                {
                    _waveFormat = WAVEFORMATEX.ToBytes(waveFormat);

                    // Free the buffer
                    Marshal.FreeCoTaskMem(waveFormat);
                }
                else
                {
                    _waveFormat = WAVEFORMATEX.Default.ToBytes();
                }
            }
            return(_waveFormat);
        }
Exemplo n.º 2
0
        internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long position, int cData)
        {
            RIFFHDR  riff    = new(0);
            BLOCKHDR block   = new(0);
            DATAHDR  dataHdr = new(0);

            int cRiff    = Marshal.SizeOf <RIFFHDR>();
            int cBlock   = Marshal.SizeOf <BLOCKHDR>();
            int cWaveEx  = waveEx.Length;// Marshal.SizeOf (waveEx); // The CLR automatically pad the waveEx structure to dword boundary. Force 16.
            int cDataHdr = Marshal.SizeOf <DATAHDR>();

            int total = cRiff + cBlock + cWaveEx + cDataHdr;

            using (MemoryStream memStream = new())
            {
                BinaryWriter bw = new(memStream);
                try
                {
                    // Write the RIFF section
                    riff._len = total + cData - 8 /* - cRiff*/; // for the "WAVE" 4 characters
                    bw.Write(riff._id);
                    bw.Write(riff._len);
                    bw.Write(riff._type);

                    // Write the wave header section
                    block._len = cWaveEx;
                    bw.Write(block._id);
                    bw.Write(block._len);

                    // Write the FormatEx structure
                    bw.Write(waveEx.ToBytes());
                    //bw.Write (waveEx.cbSize);

                    // Write the data section
                    dataHdr._len = cData;
                    bw.Write(dataHdr._id);
                    bw.Write(dataHdr._len);

                    stream.Seek(position, SeekOrigin.Begin);
                    stream.Write(memStream.GetBuffer(), 0, (int)memStream.Length);
                }
                finally
                {
                    ((IDisposable)bw).Dispose();
                }
            }
        }
Exemplo n.º 3
0
 internal byte[] WaveFormat(byte[] targetWaveFormat)
 {
     if (targetWaveFormat == null && _waveFormat == null && VoiceInfo.SupportedAudioFormats.Count > 0)
     {
         targetWaveFormat = VoiceInfo.SupportedAudioFormats[0].WaveFormat;
     }
     if (targetWaveFormat == null && _waveFormat != null)
     {
         return(_waveFormat);
     }
     if (_waveFormat == null || !object.Equals(targetWaveFormat, _waveFormat))
     {
         IntPtr   intPtr   = IntPtr.Zero;
         GCHandle gCHandle = default(GCHandle);
         if (targetWaveFormat != null)
         {
             gCHandle = GCHandle.Alloc(targetWaveFormat, GCHandleType.Pinned);
         }
         try
         {
             intPtr = _engine.GetOutputFormat((targetWaveFormat != null) ? gCHandle.AddrOfPinnedObject() : IntPtr.Zero);
         }
         finally
         {
             if (targetWaveFormat != null)
             {
                 gCHandle.Free();
             }
         }
         if (intPtr != IntPtr.Zero)
         {
             _waveFormat = WAVEFORMATEX.ToBytes(intPtr);
             Marshal.FreeCoTaskMem(intPtr);
         }
         else
         {
             _waveFormat = WAVEFORMATEX.Default.ToBytes();
         }
     }
     return(_waveFormat);
 }
Exemplo n.º 4
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();
                }
            }
        }
Exemplo n.º 5
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();
     }
 }