Exemplo n.º 1
0
 /// <summary>
 /// 结束喊话
 /// </summary>
 private void StopCollectSound()
 {
     try
     {
         if (!IsRecoding)
         {
             return;
         }
         //停止音频采集
         var rtn = PcmAudio.waveInStop(hRecord);
         if (rtn == 0) //停止采集成功,立即重置设备,重置设备将会导致所有的buffer反馈给程序
         {
             IsRecoding = false;
             ShowMsg("停止采集音频数据。");
             rtn = PcmAudio.waveInReset(hRecord);  //重置设备
         }
         Thread.Sleep(500);
         if (rtn == 0)                            //重置设备成功,立即关闭设备
         {
             rtn = PcmAudio.waveInClose(hRecord); //关闭设备
         }
         //GC.Collect();
     }
     catch (Exception ex)
     {
         ShowMsg(ex.Message);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 开始喊话
        /// </summary>
        private void StartCollectSound()
        {
            try
            {
                if (IsRecoding)
                {
                    return;
                }

                ////初始化音频编码解码
                //EvrcProtocal.InitEncoder();
                //EvrcProtocal.InitDecoder();
                EvrcProtocal.StartSpeech();
                var rtn = 0;
                hRecord = IntPtr.Zero;
                rtn     = PcmAudio.waveInOpen(out hRecord, -1, ref lpFormat, _wd, 0, 0x00030000);

                if (rtn == 0)//成功返回0
                {
                    //将准备好的buffer提供给音频输入设备
                    for (int i = 0; i < MAXRECBUFFER; i++)
                    {
                        //准备一个bufrer给输入设备
                        PcmAudio.waveInPrepareHeader(hRecord, ref globalhdr[i], (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr)));
                        //发送一个buffer给指定的输入设备,当buffer填满将会通知程序
                        PcmAudio.waveInAddBuffer(hRecord, ref globalhdr[i], (UInt32)Marshal.SizeOf(typeof(PcmAudio.WaveHdr)));
                    }
                    //开启指定的输入采集设备
                    rtn = PcmAudio.waveInStart(hRecord);
                    if (rtn == 0)
                    {
                        IsRecoding = true;
                        ShowMsg("正在采集音频数据...");
                    }
                    else
                    {
                        ShowMsg("采集音频失败");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }
Exemplo n.º 3
0
 /// <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);
     }
 }
Exemplo n.º 4
0
        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);
            }
        }