private void Form1_Load(object sender, EventArgs e) { btnDo.Enabled = false; btnLogin.Enabled = true; btnLogout.Enabled = false; //以下初始化音频采集 for (int i = 0; i < MAXRECBUFFER; i++) { var hdr = new PcmAudio.WaveHdr { lpData = Marshal.AllocHGlobal(RECBUFFER), dwBufferLength = (UInt32)RECBUFFER }; globalhdr[i] = hdr; } lpFormat.wFormatTag = 1; //声音格式为PCM lpFormat.nChannels = 1; //采样声道数,对于单声道音频设置为1,立体声设置为2 lpFormat.wBitsPerSample = 16; //采样比特 16bits/次 lpFormat.cbSize = 0; //一般为0 lpFormat.nSamplesPerSec = 8000; //采样率 8000 次/秒 lpFormat.nBlockAlign = 2; //一个块的大小,采样bit的字节数乘以声道数 lpFormat.nAvgBytesPerSec = 16000; //每秒的数据率,就是每秒能采集多少字节的数据 ////初始化音频格式结构体 //lpFormatPlay.wFormatTag = 1;//声音格式为PCM //lpFormatPlay.nChannels = 1; //lpFormatPlay.wBitsPerSample = 16; //lpFormatPlay.cbSize = 0; //lpFormatPlay.nSamplesPerSec = 8000; //lpFormatPlay.nAvgBytesPerSec = 16000; //lpFormatPlay.nBlockAlign = 2; //以下建立服务器连接 _voiceSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _voiceSocket.Connect(new IPEndPoint(IPAddress.Parse(txtIP.Text), POC_NET_UDP_PORT)); _voiceSocket.ReceiveTimeout = 30 * 1000; _voiceSocket.SendTimeout = 30 * 1000; //tcptimer = new System.Windows.Forms.Timer(); //tcptimer.Tick += Tcptimer_Tick; //tcptimer.Interval = 20 * 1000; //tcptimer.Enabled = true; //tcptimer.Start(); udptimer = new System.Windows.Forms.Timer(); udptimer.Tick += Udptimer_Tick; udptimer.Interval = 6 * 1000; udptimer.Enabled = true; udptimer.Start(); }
/// <summary> /// 喊话处理 /// </summary> /// <param name="hdr"></param> private void OnSoundData(PcmAudio.WaveHdr hdr) { if (!IsRecoding) { return; } if (hdr.dwBytesRecorded == 0) { return; } //使采集过程,直到此buffer已经沾满,不能再填充 PcmAudio.waveInUnprepareHeader(hRecord, ref hdr, (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); //将采集到的声音发送给播放线程 if (hdr.lpData != IntPtr.Zero) { DataProc(hdr.dwBytesRecorded, hdr.lpData); } }
private unsafe void btnTest_Click(object sender, EventArgs e) { try { #region 测试:PCM音频采集 + evrc编码 + evrc解码 + PCM音频播放 //hPlay = IntPtr.Zero; //PcmAudio.WaveHdr hdr; //var plyrts = PcmAudio.waveOutOpen(out hPlay, -1, ref lpFormatPlay, _wdPlay, 0, 0x00030000); //if (plyrts == 0)//打开成功 //{ // plyrts = PcmAudio.waveOutSetVolume(hPlay, 0xffffffff);//设置音量 // if (plyrts == 0) // { // //evrc编码后数据存放:EvrcBuff // for (var i = 0; i < EvrcBuff.Length; i++) // { // //二维数组复制到一维数组 // var tmp = new byte[AUDIOPACKLENGTH]; // for (var j = 0; j < AUDIOPACKLENGTH; j++) // { // tmp[j] = EvrcBuff[i, j]; // } // //数组存buffer转为Intptr // //var dec = Marshal.AllocHGlobal(RECBUFFER); // hdr = new PcmAudio.WaveHdr // { // lpData = Marshal.AllocHGlobal(RECBUFFER), // dwBufferLength = (UInt32)RECBUFFER // }; // //var buffer = Marshal.AllocHGlobal(AUDIOPACKLENGTH); // //Marshal.Copy(tmp, 0, buffer, AUDIOPACKLENGTH); // EvrcProtocal.evrc_decode(tmp, ref hdr.lpData);//解码得到byte[] // //将要输出的数据写入buffer // PcmAudio.WaveHdr hdrcopy = hdr;//todo:将最后一帧播放出来 // plyrts = PcmAudio.waveOutPrepareHeader(hPlay, ref hdrcopy, (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); // if (plyrts == 0)//播放成功 // { // plyrts = PcmAudio.waveOutWrite(hPlay, ref hdrcopy, (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); // } // } // ShowMsg("播放成功"); // } //} //int i; //byte[] pDat = new byte[RECBUFFER]; //int uiDatLen = 16; //byte[] ucEvrcDat = new byte[AUDIOPACKLENGTH]; //byte* pPcm; //for (int j = 0; j < globalhdr.Length; j++) //{ // Marshal.Copy(globalhdr[j].lpData, pDat, 0, RECBUFFER); // byte ucFrameNum = pDat[12]; // if (ucFrameNum <= 0x0a) // { // for (i = 0; i < ucFrameNum; i++) // { // switch ((pDat[13 + (i >> 2)] >> (6 - (i % 4) * 2)) & 0x03) // { // case 0x02: // ucEvrcDat[0] = 3; // memcpy(&ucEvrcDat[1], &pDat[uiDatLen], 10); // pPcm = (uint8*)malloc(PCM_PRE_FRAME_SIZE); // evrc_decode(ucEvrcDat, pPcm);//解码 // m_pPlaySound->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK, (WPARAM)PCM_PRE_FRAME_SIZE, (LPARAM)pPcm);//触发播放事件 // uiDatLen += 10; // break; // case 0x03: // ucEvrcDat[0] = 4; // memcpy(&ucEvrcDat[1], &pDat[uiDatLen], 22); // pPcm = (uint8*)malloc(PCM_PRE_FRAME_SIZE); // evrc_decode(ucEvrcDat, pPcm); // m_pPlaySound->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK, (WPARAM)PCM_PRE_FRAME_SIZE, (LPARAM)pPcm); // uiDatLen += 22; // break; // default: // //_DEBUG("OutWrite unknow FrameRate!\r\n"); // break; // } // } // } //} #endregion #region 测试:PCM音频采集 + PCM音频播放 hPlay = IntPtr.Zero; var plyrts = PcmAudio.waveOutOpen(out hPlay, -1, ref lpFormatPlay, _wdPlay, 0, 0x00030000); if (plyrts == 0) //打开成功 { plyrts = PcmAudio.waveOutSetVolume(hPlay, 0xffffffff); //设置音量 if (plyrts == 0) { pcmcache = new PcmAudio.WaveHdr { dwBufferLength = (UInt32)RECBUFFER }; for (int i = 0; i < globalhdr.Length; i++) { pcmcache.lpData = globalhdr[i].lpData; //将要输出的数据写入buffer plyrts = PcmAudio.waveOutPrepareHeader(hPlay, ref pcmcache, (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); if (plyrts == 0)//播放成功 { plyrts = PcmAudio.waveOutWrite(hPlay, ref pcmcache, (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr))); } } } } ShowMsg("播放成功"); #endregion #region 测试:编码数据传输 + APP音频解析(通过) //Func1(); //string arr = // "C0 6F 00 19 00 00 00 00 00 00 10 6A 0A AA AA A0 4C F9 E5 00 0C 1C E2 6E 1D 07 23 C5 2C C0 D3 5C 27 70 5A 17 32 96 F9 68 C2 98 7E 2C 38 56 EA 98 48 B0 3C 1C 7F F6 96 FB 3A 94 F9 37 79 73 35 99 17 5D BC 15 D9 01 60 76 DF D8 83 7D DC E8 48 FB 88 F1 CA B7 73 BA 94 E2 59 51 DE AC BA BA 00 CC 95 00 D9 4F 90 77 E9 79 CD FD AD C3 D9 37 41 77 BF 9D FC 9F "; //var arrsnd = HexStringToByteArray(arr); //SendUdpPacket(_voiceSocket, txtIP.Text, POC_NET_UDP_PORT, arrsnd, arrsnd.Length); //Func2(); #endregion } catch (Exception exception) { ShowMsg("测试按钮:" + exception.Message); } }