示例#1
0
 public override void Close()
 {
     if (!_closed)
     {
         try
         {
             uint encodedSize = 0;
             if (_mInBufferPos > 0)
             {
                 if (Lame_encDll.EncodeChunk(_mHLameStream, _mInBuffer, 0, (uint)_mInBufferPos, _mOutBuffer, ref encodedSize) == Lame_encDll.BE_ERR_SUCCESSFUL)
                 {
                     if (encodedSize > 0)
                     {
                         base.Write(_mOutBuffer, 0, (int)encodedSize);
                     }
                 }
             }
             encodedSize = 0;
             if (Lame_encDll.beDeinitStream(_mHLameStream, _mOutBuffer, ref encodedSize) == Lame_encDll.BE_ERR_SUCCESSFUL)
             {
                 if (encodedSize > 0)
                 {
                     base.Write(_mOutBuffer, 0, (int)encodedSize);
                 }
             }
         }
         finally
         {
             Lame_encDll.beCloseStream(_mHLameStream);
         }
     }
     _closed = true;
     CloseStream();
 }
示例#2
0
 public override void Close()
 {
     if (!closed)
     {
         try
         {
             uint EncodedSize = 0;
             if (m_InBufferPos > 0)
             {
                 if (Lame_encDll.EncodeChunk(m_hLameStream, m_InBuffer, 0, (uint)m_InBufferPos, m_OutBuffer, ref EncodedSize) == Lame_encDll.BE_ERR_SUCCESSFUL)
                 {
                     if (EncodedSize > 0)
                     {
                         base.Write(m_OutBuffer, 0, (int)EncodedSize);
                     }
                 }
             }
             EncodedSize = 0;
             if (Lame_encDll.beDeinitStream(m_hLameStream, m_OutBuffer, ref EncodedSize) == Lame_encDll.BE_ERR_SUCCESSFUL)
             {
                 if (EncodedSize > 0)
                 {
                     base.Write(m_OutBuffer, 0, (int)EncodedSize);
                 }
             }
         }
         finally
         {
             Lame_encDll.beCloseStream(m_hLameStream);
         }
     }
     closed = true;
     CloseStream();
 }
示例#3
0
        /// <summary>
        /// Send to the compressor an array of bytes.
        /// </summary>
        /// <param name="buffer">Input buffer</param>
        /// <param name="index">Start position</param>
        /// <param name="count">Bytes to process. The optimal size, to avoid buffer copy, is a multiple of <see cref="yeti.mmedia.utils.AudioFileWriter.OptimalBufferSize"/></param>
        public override void Write(byte[] buffer, int index, int count)
        {
            int  ToCopy      = 0;
            uint EncodedSize = 0;
            uint LameResult;

            while (count > 0)
            {
                if (m_InBufferPos > 0)
                {
                    ToCopy = Math.Min(count, m_InBuffer.Length - m_InBufferPos);
                    Buffer.BlockCopy(buffer, index, m_InBuffer, m_InBufferPos, ToCopy);
                    m_InBufferPos += ToCopy;
                    index         += ToCopy;
                    count         -= ToCopy;
                    if (m_InBufferPos >= m_InBuffer.Length)
                    {
                        m_InBufferPos = 0;
                        if ((LameResult = Lame_encDll.EncodeChunk(m_hLameStream, m_InBuffer, m_OutBuffer, ref EncodedSize)) == Lame_encDll.BE_ERR_SUCCESSFUL)
                        {
                            if (EncodedSize > 0)
                            {
                                base.Write(m_OutBuffer, 0, (int)EncodedSize);
                            }
                        }
                        else
                        {
                            throw new ApplicationException(string.Format("Lame_encDll.EncodeChunk failed with the error code {0}", LameResult));
                        }
                    }
                }
                else
                {
                    if (count >= m_InBuffer.Length)
                    {
                        if ((LameResult = Lame_encDll.EncodeChunk(m_hLameStream, buffer, index, (uint)m_InBuffer.Length, m_OutBuffer, ref EncodedSize)) == Lame_encDll.BE_ERR_SUCCESSFUL)
                        {
                            if (EncodedSize > 0)
                            {
                                base.Write(m_OutBuffer, 0, (int)EncodedSize);
                            }
                        }
                        else
                        {
                            throw new ApplicationException(string.Format("Lame_encDll.EncodeChunk failed with the error code {0}", LameResult));
                        }
                        count -= m_InBuffer.Length;
                        index += m_InBuffer.Length;
                    }
                    else
                    {
                        Buffer.BlockCopy(buffer, index, m_InBuffer, 0, count);
                        m_InBufferPos = count;
                        index        += count;
                        count         = 0;
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Send to the compressor an array of bytes.
        /// </summary>
        /// <param name="buffer">Input buffer</param>
        /// <param name="index">Start position</param>
        /// <param name="count">Bytes to process. The optimal size, to avoid buffer copy, is a multiple</param>
        public override void Write(byte[] buffer, int index, int count)
        {
            uint encodedSize = 0;

            while (count > 0)
            {
                uint lameResult;
                if (_mInBufferPos > 0)
                {
                    int toCopy = Math.Min(count, _mInBuffer.Length - _mInBufferPos);
                    Buffer.BlockCopy(buffer, index, _mInBuffer, _mInBufferPos, toCopy);
                    _mInBufferPos += toCopy;
                    index         += toCopy;
                    count         -= toCopy;
                    if (_mInBufferPos >= _mInBuffer.Length)
                    {
                        _mInBufferPos = 0;
                        if ((lameResult = Lame_encDll.EncodeChunk(_mHLameStream, _mInBuffer, _mOutBuffer, ref encodedSize)) == Lame_encDll.BE_ERR_SUCCESSFUL)
                        {
                            if (encodedSize > 0)
                            {
                                base.Write(_mOutBuffer, 0, (int)encodedSize);
                            }
                        }
                        else
                        {
                            throw new ApplicationException(string.Format("Lame_encDll.EncodeChunk failed with the error code {0}", lameResult));
                        }
                    }
                }
                else
                {
                    if (count >= _mInBuffer.Length)
                    {
                        if ((lameResult = Lame_encDll.EncodeChunk(_mHLameStream, buffer, index, (uint)_mInBuffer.Length, _mOutBuffer, ref encodedSize)) == Lame_encDll.BE_ERR_SUCCESSFUL)
                        {
                            if (encodedSize > 0)
                            {
                                base.Write(_mOutBuffer, 0, (int)encodedSize);
                            }
                        }
                        else
                        {
                            throw new ApplicationException(string.Format("Lame_encDll.EncodeChunk failed with the error code {0}", lameResult));
                        }
                        count -= _mInBuffer.Length;
                        index += _mInBuffer.Length;
                    }
                    else
                    {
                        Buffer.BlockCopy(buffer, index, _mInBuffer, 0, count);
                        _mInBufferPos = count;
                        index        += count;
                        count         = 0;
                    }
                }
            }
        }
示例#5
0
 /// <summary>
 /// Create a Mp3Writer with specific MP3 format
 /// </summary>
 /// <param name="output">Stream that will hold the MP3 resulting data</param>
 /// <param name="inputDataFormat">PCM format of input data</param>
 /// <param name="mp3Config">Desired MP3 config</param>
 /// <param name="isStreamOwner"> </param>
 public Mp3Writer(Stream output, WaveFormat inputDataFormat, BE_CONFIG mp3Config, bool isStreamOwner)
     : base(output, inputDataFormat)
 {
     IsStreamOwner = isStreamOwner;
     try
     {
         _mMp3Config = mp3Config;
         uint lameResult = Lame_encDll.beInitStream(_mMp3Config, ref _mInputSamples, ref _mOutBufferSize, ref _mHLameStream);
         if (lameResult != Lame_encDll.BE_ERR_SUCCESSFUL)
         {
             throw new ApplicationException(string.Format("Lame_encDll.beInitStream failed with the error code {0}", lameResult));
         }
         _mInBuffer  = new byte[_mInputSamples * 2]; //Input buffer is expected as short[]
         _mOutBuffer = new byte[_mOutBufferSize];
     }
     catch
     {
         CloseStream();
         throw;
     }
 }
示例#6
0
 /// <summary>
 /// Create a Mp3Writer with specific MP3 format
 /// </summary>
 /// <param name="Output">Stream that will hold the MP3 resulting data</param>
 /// <param name="InputDataFormat">PCM format of input data</param>
 /// <param name="Mp3Config">Desired MP3 config</param>
 public Mp3Writer(Stream Output, WaveFormat InputDataFormat, BE_CONFIG Mp3Config, bool isStreamOwner)
     : base(Output, InputDataFormat)
 {
     IsStreamOwner = isStreamOwner;
     try
     {
         m_Mp3Config = Mp3Config;
         uint LameResult = Lame_encDll.beInitStream(m_Mp3Config, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
         if (LameResult != Lame_encDll.BE_ERR_SUCCESSFUL)
         {
             throw new ApplicationException(string.Format("Lame_encDll.beInitStream failed with the error code {0}", LameResult));
         }
         m_InBuffer  = new byte[m_InputSamples * 2]; //Input buffer is expected as short[]
         m_OutBuffer = new byte[m_OutBufferSize];
     }
     catch
     {
         CloseStream();
         throw;
     }
 }