/// <summary>
        /// 执行预处理
        /// </summary>
        /// <returns>预处理后的音频数据</returns>
        public byte[] Run(byte[] input)
        {
            if (input == null || input.Length > _frameSize * 2)
            {
                throw new Exception($"值不能为空且每次压缩的数据大小不能超过{_frameSize * 2}个字节");
            }
            input = SpeexHelper.PadArr(input, _frameSize * 2);
            short[] sShort = SpeexHelper.BytesToShorts(input);
            int     result = SpeexPreprocessApi.Speex_preprocess_run(preprocessMemAddress, sShort);

            byte[] output = SpeexHelper.ShortsToBytes(sShort);
            return(output);
        }
Beispiel #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());
        }
        /// <summary>
        /// 基于SpeexCore的具体压缩示例实现
        /// 规则 压缩:压缩数据个数+压缩数据 如:byte[42]=byte[4]+byte[38] byte[24]=byte[4]+byte[20] ...
        /// 根据质量压缩出来的数据个数的int值在内存中的占用数据+压缩数据
        /// 解压:按当前规则动态解析
        /// </summary>
        /// <param name="input">正常的全部音频数据</param>
        /// <returns>压缩后并组合的Speex数据</returns>
        public byte[] Encode(byte[] input)
        {
            input = SpeexHelper.PadArr(input, 320);//补足320 的倍数
            int i = 0;
            IEnumerable <byte> nbyte   = null;
            List <byte>        receive = new List <byte>();

            while ((nbyte = input.Skip(i).Take(320)).Count() != 0)
            {
                byte[] rec     = base.EncodeBase(nbyte.ToArray());
                byte[] lenByte = BitConverter.GetBytes(rec.Length);
                receive.AddRange(lenByte);
                receive.AddRange(rec);
                Console.WriteLine(i);
                i += 320;
            }
            return(receive.ToArray());
        }