public bool GetWavInfo(string strpath) { WavInfo wavInfo = new WavInfo(); FileInfo fi = new FileInfo(strpath); FileStream fs = fi.OpenRead(); if (fs.Length >= 44) { byte[] bInfo = new byte[44]; fs.Read(bInfo, 0, 44); wavInfo.groupid = Encoding.Default.GetString(bInfo, 0, 4); wavInfo.filesize = BitConverter.ToInt32(bInfo, 4); wavInfo.rifftype = Encoding.Default.GetString(bInfo, 8, 4); wavInfo.chunkid = Encoding.Default.GetString(bInfo, 12, 4); if (wavInfo.groupid == "RIFF" && wavInfo.rifftype == "WAVE" && wavInfo.chunkid == "fmt ") { wavInfo.chunksize = BitConverter.ToInt32(bInfo, 16); wavInfo.wformattag = BitConverter.ToInt16(bInfo, 20); wavInfo.wchannels = BitConverter.ToUInt16(bInfo, 22); wavInfo.dwsamplespersec = BitConverter.ToUInt32(bInfo, 24); wavInfo.dwavgbytespersec = BitConverter.ToUInt32(bInfo, 28); wavInfo.wblockalign = BitConverter.ToUInt16(bInfo, 32); wavInfo.wbitspersample = BitConverter.ToUInt16(bInfo, 34); wavInfo.datachunkid = Encoding.Default.GetString(bInfo, 36, 4); wavInfo.datasize = BitConverter.ToInt32(bInfo, 40); // 读取音频数据 int readBytes = 44, bufBytes = 0, buffSize = 0; int dataSize = (int)wavInfo.datasize; byte[] fileBuf = new byte[G722_WAVE_FRAME_SIZE]; _wav_buffer = new sbyte[G722_WAVE_FRAME_SIZE]; _dec_buffer = new sbyte[G722_VOICE_FRAME_SIZE]; //fs.Read(_buffer, 44, wavInfo.datasize); while (readBytes < dataSize) { bufBytes = dataSize - readBytes; if (bufBytes > G722_WAVE_FRAME_SIZE) { bufBytes = G722_WAVE_FRAME_SIZE; } buffSize = fs.Read(fileBuf, 0, bufBytes); for (int i = 0; i < bufBytes; i++) { if (fileBuf[i] > 127) { _wav_buffer[i] = (sbyte)(fileBuf[i] - 256); } else { _wav_buffer[i] = (sbyte)fileBuf[i]; } } HCNetSDK.NET_DVR_EncodeG722Frame(_dec_handle, _wav_buffer, _dec_buffer); readBytes += buffSize; } return(true); } } return(false); }
public void SendWavFile(object strpath) { WavInfo wavInfo = new WavInfo(); FileInfo fi = new FileInfo((string)strpath); FileStream fs = fi.OpenRead(); bool ret = false; if (fs.Length >= 44) { byte[] bInfo = new byte[44]; fs.Read(bInfo, 0, 44); wavInfo.groupid = Encoding.Default.GetString(bInfo, 0, 4); wavInfo.filesize = BitConverter.ToInt32(bInfo, 4); wavInfo.rifftype = Encoding.Default.GetString(bInfo, 8, 4); wavInfo.chunkid = Encoding.Default.GetString(bInfo, 12, 4); if (wavInfo.groupid == "RIFF" && wavInfo.rifftype == "WAVE" && wavInfo.chunkid == "fmt ") { wavInfo.chunksize = BitConverter.ToInt32(bInfo, 16); wavInfo.wformattag = BitConverter.ToInt16(bInfo, 20); wavInfo.wchannels = BitConverter.ToUInt16(bInfo, 22); wavInfo.dwsamplespersec = BitConverter.ToUInt32(bInfo, 24); wavInfo.dwavgbytespersec = BitConverter.ToUInt32(bInfo, 28); wavInfo.wblockalign = BitConverter.ToUInt16(bInfo, 32); wavInfo.wbitspersample = BitConverter.ToUInt16(bInfo, 34); wavInfo.datachunkid = Encoding.Default.GetString(bInfo, 36, 4); wavInfo.datasize = BitConverter.ToInt32(bInfo, 40); // 读取音频数据 int readBytes = 0, bufBytes = 0, buffSize = 0; int dataSize = (int)wavInfo.datasize; byte[] fileBuf = new byte[G722_WAVE_FRAME_SIZE]; byte[] _wav_buffer = new byte[G722_WAVE_FRAME_SIZE]; byte[] _dec_buffer = new byte[G722_VOICE_FRAME_SIZE]; //fs.Read(_buffer, 44, wavInfo.datasize); while (readBytes < dataSize) { bufBytes = dataSize - readBytes; if (bufBytes > G722_WAVE_FRAME_SIZE) { bufBytes = G722_WAVE_FRAME_SIZE; } buffSize = fs.Read(fileBuf, 0, bufBytes); if (buffSize == 0) { break; } ret = HCNetSDK.NET_DVR_EncodeG722Frame(_dec_handle, fileBuf, _dec_buffer); ret = HCNetSDK.NET_DVR_VoiceComSendData(_voice_handle, _dec_buffer, G722_VOICE_FRAME_SIZE); readBytes += buffSize; Thread.Sleep(40); } StopTalk(); //return true; } } // return false; }