AudioRec.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:3k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: AidopRec.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "WaveShow.h"
  14. #include "UDPSocket.h"
  15. #include "WaveIn.h"
  16. #include "AudioRec.h"
  17. #include "stdio.h"
  18. /*------------------------------------------------------------------------------*/
  19. CAudioRec::CAudioRec()
  20. {
  21. }
  22. /*------------------------------------------------------------------------------*/
  23. CAudioRec::~CAudioRec()
  24. {
  25. Destroy();
  26. }
  27. /*------------------------------------------------------------------------------*/
  28. //开始录音
  29. BOOL CAudioRec::Start()
  30. {
  31. BOOL bRet=FALSE;
  32. if(!m_hIn)
  33. goto RET;
  34. if(!AllocBuffer())
  35. goto RET;
  36. m_mmr=waveInStart(m_hIn);
  37. if(m_mmr)
  38. goto RET;
  39. bRet=TRUE;
  40. RET:
  41. return bRet;
  42. }
  43. /*------------------------------------------------------------------------------*/
  44. //停止录音
  45. BOOL CAudioRec::Stop()
  46. {
  47. BOOL bRet=FALSE;
  48. if(!m_hIn)
  49. goto RET;
  50. m_mmr=waveInReset(m_hIn);
  51. if(m_mmr)
  52. goto RET;
  53. if(!FreeBuffer())
  54. goto RET;
  55. bRet=TRUE;
  56. RET:
  57. return TRUE;
  58. }
  59. /*------------------------------------------------------------------------------*/
  60. //为录音分配一组内存
  61. BOOL CAudioRec::AllocBuffer()
  62. {
  63. BOOL bRet=FALSE;
  64. UINT i;
  65. m_pHdr=new WAVEHDR[NUM_BUF];
  66. //为了使录音连续,采用多个缓冲区
  67. for(i=0;i<NUM_BUF;i++)
  68. {
  69. ZeroMemory(&m_pHdr[i],sizeof(WAVEHDR));
  70. m_pHdr[i].lpData=new char[m_nBufSize];
  71. m_pHdr[i].dwBufferLength=m_nBufSize;
  72. m_mmr=waveInPrepareHeader(m_hIn,&m_pHdr[i],sizeof(WAVEHDR));
  73. if(m_mmr)
  74. goto RET;
  75. m_mmr=waveInAddBuffer(m_hIn,&m_pHdr[i],sizeof(WAVEHDR));
  76. if(m_mmr)
  77. goto RET;
  78. }
  79. bRet=TRUE;
  80. RET:
  81. return bRet;
  82. }
  83. /*------------------------------------------------------------------------------*/
  84. //释放内存
  85. BOOL CAudioRec::FreeBuffer()
  86. {
  87. BOOL bRet=FALSE;
  88. UINT i;
  89. if(!m_pHdr)
  90. goto RET;
  91. for(i=0;i<NUM_BUF;i++)
  92. {
  93. waveInUnprepareHeader(m_hIn,&m_pHdr[i],sizeof(WAVEHDR));
  94. if(m_pHdr[i].lpData)
  95. delete []m_pHdr[i].lpData;
  96. }
  97. delete []m_pHdr;
  98. bRet=TRUE;
  99. RET:
  100. return bRet;
  101. }
  102. /*------------------------------------------------------------------------------*/
  103. //打开录音设备
  104. BOOL CAudioRec::Create(WAVEFORMATEX *pwf,DWORD dwCallBack,DWORD dwInst,DWORD fdwOpen ,DWORD dwBufSize)
  105. {
  106. BOOL bRet=FALSE;
  107. if(m_hIn)
  108. {
  109. bRet=TRUE;
  110. goto RET;
  111. }
  112. if(!OpenDev(pwf,dwCallBack,dwInst,fdwOpen))
  113. goto RET;
  114. m_nBufSize=dwBufSize;
  115. bRet=TRUE;
  116. RET:
  117. return bRet;
  118. }
  119. /*------------------------------------------------------------------------------*/
  120. BOOL CAudioRec::Destroy()
  121. {
  122. BOOL bRet=FALSE;
  123. if(!CloseDev())
  124. goto RET;
  125. bRet=TRUE;
  126. RET:
  127. return bRet;
  128. }