Exemplo n.º 1
0
        /// <summary>
        /// 解压Speex音频数据流
        /// </summary>
        /// <param name="input">需要解压的数据流</param>
        /// <returns>解压后的正常音频数据</returns>
        public byte[] DecodeBase(byte[] input)
        {
            SpeexBitsApi.Speex_bits_reset(ref _decodeSpeexBits);
            SpeexBitsApi.Speex_bits_read_from(ref _decodeSpeexBits, input, input.Length);
            //int i = SpeexBitsApi.Speex_bits_nbytes(ref _decodeSpeexBits);
            short[] buffer = new short[160];
            int     len    = SpeexDecodeApi.Speex_decode_int(_decodeStateIntptr, ref _decodeSpeexBits, buffer);

            return(SpeexHelper.ShortsToBytes(buffer));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 压缩音频数据
        /// </summary>
        /// <param name="input">需要压缩的音频字节流</param>
        /// <returns>压缩后的Speex数据流</returns>
        public byte[] EncodeBase(byte[] input)
        {
            if (input == null || input.Length > 320)
            {
                throw new Exception("值不能为空且每次压缩的数据大小不能超过320个字节");
            }
            input = SpeexHelper.PadArr(input, 320);
            SpeexBitsApi.Speex_bits_reset(ref _encodeSpeexBits);
            short[] sShort = SpeexHelper.BytesToShorts(input);
            //float[] sInput = SpeexHelper.ShortsToFloats(sShort);
            int i   = SpeexEncodeApi.Speex_encode_int(_encodeStateIntptr, sShort, ref _encodeSpeexBits);
            int len = SpeexBitsApi.Speex_bits_nbytes(ref _encodeSpeexBits);

            byte[] outBuffer = new byte[len];
            int    outResult = SpeexBitsApi.Speex_bits_write(ref _encodeSpeexBits, outBuffer, outBuffer.Length);

            return(outBuffer.Take(outResult).ToArray());
        }