public byte[] Decode(byte[] data) { if (!_inited) { uint samplerate = 0; byte channels = 0; lock (_lock) { var r = NeAACDecInit(_handle, data, (uint)data.Length, ref samplerate, ref channels); if (r == 0) { _inited = true; } } } if (_inited) { NeAACDecFrameInfo info = new NeAACDecFrameInfo(); var pcm = NeAACDecDecode(_handle, ref info, data, data.Length); try { int bufferLenth = info.samples * info.channels; if (bufferLenth > 0 && bufferLenth <= 4096) { byte[] pcm_data = FunctionEx.IntPtrToBytes(pcm, 0, (info.samples * info.channels)); byte[] frame_mono = new byte[2048]; if (info.channels == 1) { return(pcm_data); } else if (info.channels == 2) { //从双声道的数据中提取单通道 for (int i = 0, j = 0; i < 4096 && j < 2048; i += 4, j += 2) { frame_mono[j] = pcm_data[i]; frame_mono[j + 1] = pcm_data[i + 1]; } return(frame_mono); } } } catch (Exception ex) { } } return(new byte[0]); }
//void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder, // NeAACDecFrameInfo *hInfo, // unsigned char *buffer, // unsigned long buffer_size); private extern static IntPtr NeAACDecDecode(IntPtr hDecoder, ref NeAACDecFrameInfo hInfo, byte[] buffer, int buffer_size);