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

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: WaveShow.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "WaveShow.h"
  14. /*------------------------------------------------------------------------------*/
  15. CWaveShow::CWaveShow()
  16. {
  17. //to fit the G729a codec,i set the audio format as below
  18. //infact the waveshow support not only this format
  19. m_crBg=0x00000000;
  20. m_crLine=0x00ffffff;
  21. m_hbBg=CreateSolidBrush(m_crBg);
  22. m_hpLine=CreatePen(PS_SOLID,1,m_crLine);
  23. m_hBmp=0;
  24. m_Format.wFormatTag = WAVE_FORMAT_PCM;
  25. m_Format.cbSize = 0;
  26. m_Format.wBitsPerSample=16;
  27. m_Format.nSamplesPerSec=8000;
  28. m_Format.nChannels=1;
  29. m_Format.nAvgBytesPerSec = m_Format.nSamplesPerSec*(m_Format.wBitsPerSample/8);
  30. m_Format.nBlockAlign = m_Format.nChannels     *(m_Format.wBitsPerSample/8);
  31. }
  32. /*------------------------------------------------------------------------------*/
  33. CWaveShow::~CWaveShow()
  34. {
  35. }
  36. /*------------------------------------------------------------------------------*/
  37. BOOL CWaveShow::Create(RECT &rc,HWND hParent)
  38. {
  39. return CWndX::Create(0,0,0,WS_CHILD|WS_VISIBLE,rc,hParent,0);
  40. }
  41. /*------------------------------------------------------------------------------*/
  42. LRESULT CWaveShow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  43. {
  44. switch(uMsg)
  45. {
  46. case WM_DESTROY:
  47. {
  48. if(m_hBmp)
  49. DeleteObject(m_hBmp);
  50. if(m_hbBg)
  51. DeleteObject(m_hbBg);
  52. if(m_hpLine)
  53. DeleteObject(m_hpLine);
  54. m_hBmp=0;
  55. m_hbBg=0;
  56. m_hpLine=0;
  57. }
  58. break;
  59. case WM_PAINT:
  60. OnPaint();
  61. return TRUE;
  62. case WM_TIMER:
  63. {
  64. KillTimer(m_hWnd,wParam);
  65. HDC hdc=GetDC(m_hWnd);
  66. Paint(hdc,0,0);
  67. ReleaseDC(m_hWnd,hdc);
  68. }
  69. break;
  70. }
  71. return CWndX::WndProc(uMsg,wParam,lParam);
  72. }
  73. /*------------------------------------------------------------------------------*/
  74. BOOL CWaveShow::Paint(HDC hdc,char* buf,unsigned uSize)
  75. {
  76. BOOL bRet=FALSE;
  77. BITMAP bmp;
  78. GetObject(m_hBmp,sizeof(bmp),&bmp);
  79. CRectX rc;
  80. GetClientRect(m_hWnd,&rc);
  81. if(m_hBmp)
  82. {
  83. if(bmp.bmHeight!=rc.Height()||bmp.bmWidth!=rc.Width())
  84. {
  85. DeleteObject(m_hBmp);
  86. m_hBmp=CreateCompatibleBitmap(hdc,rc.Width(),rc.Height());
  87. }
  88. }
  89. else
  90. {
  91. m_hBmp=CreateCompatibleBitmap(hdc,rc.Width(),rc.Height());
  92. }
  93. if(buf)
  94. {
  95. SetTimer(m_hWnd,1,150,0);
  96. switch(m_Format.wBitsPerSample)
  97. {
  98. case 8:
  99. {
  100. HDC hMemDC=CreateCompatibleDC(hdc);
  101. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hBmp);
  102. FillRect(hMemDC,&CRectX(0,0,rc.Width(),rc.Height()),m_hbBg);
  103. HPEN hop=(HPEN)SelectObject(hMemDC,m_hpLine);
  104. int iHeight=rc.Height();
  105. int iWidth=rc.Width();
  106. MoveToEx(hMemDC,-1,iHeight/2,0);
  107. if(m_Format.nChannels==1)
  108. {
  109. for(int i=0;i<iWidth;i+=2)
  110. {
  111. LineTo(hMemDC,i,((BYTE)buf[uSize*i/iWidth])*iHeight/256);
  112. }
  113. }
  114. else
  115. if(m_Format.nChannels==2)
  116. {
  117. for(int i=0;i<iWidth-1;i+=2)
  118. {
  119. int index=uSize*i/iWidth/2*2;
  120. LineTo(hMemDC,i,((BYTE)buf[index]+(BYTE)buf[index+1])*iHeight/512);
  121. }
  122. }
  123. LineTo(hMemDC,iWidth+1,iHeight/2);
  124. SelectObject(hMemDC,hop);
  125. BitBlt(hdc,rc.left,rc.top,iWidth,iHeight,hMemDC,0,0,SRCCOPY);
  126. SelectObject(hMemDC,hob);
  127. DeleteDC(hMemDC);
  128. }
  129. break;
  130. case 16:
  131. {
  132. HDC hMemDC=CreateCompatibleDC(hdc);
  133. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hBmp);
  134. FillRect(hMemDC,&CRectX(0,0,rc.Width(),rc.Height()),m_hbBg);
  135. HPEN hop=(HPEN)SelectObject(hMemDC,m_hpLine);
  136. int iHeight=rc.Height();
  137. int iHHeight=iHeight/2;
  138. int iWidth=rc.Width();
  139. MoveToEx(hMemDC,-1,iHeight/2,0);
  140. short* ps=(short*)buf;
  141. UINT uHSize=uSize/2;
  142. if(m_Format.nChannels==1)
  143. {
  144. for(int i=0;i<iWidth;i+=2)
  145. {
  146. LineTo(hMemDC,i,iHHeight+ps[uHSize*i/iWidth]*iHeight/65535);
  147. }
  148. }
  149. else
  150. if(m_Format.nChannels==2)
  151. {
  152. for(int i=0;i<iWidth-1;i+=2)
  153. {
  154. int index=uHSize*i/iWidth/2*2;
  155. LineTo(hMemDC,i,iHHeight+(ps[index]+ps[index+1])*iHHeight/65535);
  156. }
  157. }
  158. LineTo(hMemDC,iWidth+1,iHHeight);
  159. SelectObject(hMemDC,hop);
  160. BitBlt(hdc,rc.left,rc.top,iWidth,iHeight,hMemDC,0,0,SRCCOPY);
  161. SelectObject(hMemDC,hob);
  162. DeleteDC(hMemDC);
  163. }
  164. break;
  165. }
  166. }
  167. else
  168. {
  169. HDC hMemDC=CreateCompatibleDC(hdc);
  170. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hBmp);
  171. HPEN hop=(HPEN)SelectObject(hMemDC,m_hpLine);
  172. FillRect(hMemDC,&CRectX(0,0,rc.Width(),rc.Height()),m_hbBg);
  173. MoveToEx(hMemDC,0,rc.Height()/2,0);
  174. LineTo(hMemDC,rc.Width(),rc.Height()/2);
  175. BitBlt(hdc,rc.left,rc.top,rc.Width(),rc.Height(),
  176. hMemDC,0,0,SRCCOPY);
  177. SelectObject(hMemDC,hop);
  178. SelectObject(hMemDC,hob);
  179. DeleteDC(hMemDC);
  180. bRet=TRUE;
  181. }
  182. return bRet;
  183. }
  184. /*------------------------------------------------------------------------------*/
  185. //set the wave format,
  186. void CWaveShow::SetFormat(WAVEFORMATEX *pfmt)
  187. {
  188. if(pfmt)
  189. CopyMemory(&m_Format,pfmt,sizeof(m_Format));
  190. }
  191. /*------------------------------------------------------------------------------*/
  192. void CWaveShow::GetFormat(WAVEFORMATEX& fmt)
  193. {
  194. fmt=m_Format;
  195. }
  196. /*------------------------------------------------------------------------------*/
  197. void CWaveShow::SetBgColor(COLORREF cr)
  198. {
  199.     m_crBg=cr;
  200. if(m_hbBg)
  201. DeleteObject(m_hbBg);
  202. m_hbBg=CreateSolidBrush(cr);
  203. }
  204. /*------------------------------------------------------------------------------*/
  205. COLORREF CWaveShow::GetBgColor()
  206. {
  207.     return m_crBg;
  208. }
  209. /*------------------------------------------------------------------------------*/
  210. void CWaveShow::SetLineColor(COLORREF cr)
  211. {
  212. m_crLine=cr;
  213. if(m_hpLine)
  214. DeleteObject(m_hpLine);
  215. m_hpLine=CreatePen(PS_SOLID,1,cr);
  216. }
  217. /*------------------------------------------------------------------------------*/
  218. COLORREF CWaveShow::GetLineColor()
  219. {
  220. return m_crLine;
  221. }
  222. /*------------------------------------------------------------------------------*/
  223. void CWaveShow::OnPaint()
  224. {
  225. PAINTSTRUCT ps;
  226. HDC hdc=BeginPaint(m_hWnd,&ps);
  227. Paint(hdc,0,0);
  228. EndPaint(m_hWnd,&ps);
  229. }