/// <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); }
/// <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()); }