G729OutDlg.cpp
Upload User: sz_zxd888
Upload Date: 2021-03-25
Package Size: 2126k
Code Size: 7k
Category:

Voice Compress

Development Platform:

C#

  1. // G729OutDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "G729Out.h"
  5. #include "G729OutDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CG729OutDlg dialog
  50. CG729OutDlg::CG729OutDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CG729OutDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CG729OutDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. hwO = NULL;
  59. memset(&wfx,0x00,sizeof(WAVEFORMATEX));
  60. wfx.wFormatTag = WAVE_FORMAT_PCM;
  61. wfx.nChannels = 1;
  62. wfx.wBitsPerSample = 16;
  63. wfx.cbSize = 0;
  64. wfx.nSamplesPerSec = 8000;//44100;//8000;//20050;
  65. wfx.nAvgBytesPerSec = //16000
  66. wfx.nSamplesPerSec*(wfx.wBitsPerSample/8);
  67. wfx.nBlockAlign = //2
  68. (wfx.wBitsPerSample/8)*wfx.nChannels;
  69. // WaveHdrOut.lpData = WaveOutBuffer;
  70. lBufferCounter = 0;
  71. for(int i=0;i<BUFFER_SIZE;i++)
  72. {
  73. ZeroMemory(&lBufferpHdr[i], sizeof(WAVEHDR));
  74. lBufferpHdr[i].lpData = NULL;
  75. }
  76. }
  77. void CG729OutDlg::DoDataExchange(CDataExchange* pDX)
  78. {
  79. CDialog::DoDataExchange(pDX);
  80. //{{AFX_DATA_MAP(CG729OutDlg)
  81. // NOTE: the ClassWizard will add DDX and DDV calls here
  82. //}}AFX_DATA_MAP
  83. }
  84. BEGIN_MESSAGE_MAP(CG729OutDlg, CDialog)
  85. //{{AFX_MSG_MAP(CG729OutDlg)
  86. ON_WM_SYSCOMMAND()
  87. ON_WM_PAINT()
  88. ON_WM_QUERYDRAGICON()
  89. ON_BN_CLICKED(IDC_PLAY, OnPlay)
  90. ON_BN_CLICKED(IDC_STOP, OnStop)
  91. ON_WM_TIMER()
  92. //}}AFX_MSG_MAP
  93. ON_MESSAGE(MM_WOM_DONE,OnWomDone)
  94. END_MESSAGE_MAP()
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CG729OutDlg message handlers
  97. BOOL CG729OutDlg::OnInitDialog()
  98. {
  99. CDialog::OnInitDialog();
  100. // Add "About..." menu item to system menu.
  101. // IDM_ABOUTBOX must be in the system command range.
  102. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  103. ASSERT(IDM_ABOUTBOX < 0xF000);
  104. CMenu* pSysMenu = GetSystemMenu(FALSE);
  105. if (pSysMenu != NULL)
  106. {
  107. CString strAboutMenu;
  108. strAboutMenu.LoadString(IDS_ABOUTBOX);
  109. if (!strAboutMenu.IsEmpty())
  110. {
  111. pSysMenu->AppendMenu(MF_SEPARATOR);
  112. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  113. }
  114. }
  115. // Set the icon for this dialog.  The framework does this automatically
  116. //  when the application's main window is not a dialog
  117. SetIcon(m_hIcon, TRUE); // Set big icon
  118. SetIcon(m_hIcon, FALSE); // Set small icon
  119. // TODO: Add extra initialization here
  120. return TRUE;  // return TRUE  unless you set the focus to a control
  121. }
  122. void CG729OutDlg::OnSysCommand(UINT nID, LPARAM lParam)
  123. {
  124. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  125. {
  126. CAboutDlg dlgAbout;
  127. dlgAbout.DoModal();
  128. }
  129. else
  130. {
  131. CDialog::OnSysCommand(nID, lParam);
  132. }
  133. }
  134. // If you add a minimize button to your dialog, you will need the code below
  135. //  to draw the icon.  For MFC applications using the document/view model,
  136. //  this is automatically done for you by the framework.
  137. void CG729OutDlg::OnPaint() 
  138. {
  139. if (IsIconic())
  140. {
  141. CPaintDC dc(this); // device context for painting
  142. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  143. // Center icon in client rectangle
  144. int cxIcon = GetSystemMetrics(SM_CXICON);
  145. int cyIcon = GetSystemMetrics(SM_CYICON);
  146. CRect rect;
  147. GetClientRect(&rect);
  148. int x = (rect.Width() - cxIcon + 1) / 2;
  149. int y = (rect.Height() - cyIcon + 1) / 2;
  150. // Draw the icon
  151. dc.DrawIcon(x, y, m_hIcon);
  152. }
  153. else
  154. {
  155. CDialog::OnPaint();
  156. }
  157. }
  158. // The system calls this to obtain the cursor to display while the user drags
  159. //  the minimized window.
  160. HCURSOR CG729OutDlg::OnQueryDragIcon()
  161. {
  162. return (HCURSOR) m_hIcon;
  163. }
  164. void CG729OutDlg::OnPlay() 
  165. {
  166. // TODO: Add your control notification handler code here
  167. if(hwO == NULL)
  168. {
  169. if(waveOutOpen(&hwO,WAVE_MAPPER,&wfx,(long)GetSafeHwnd(),0,CALLBACK_WINDOW) 
  170. != MMSYSERR_NOERROR)
  171. {
  172. return ;
  173. }
  174. }
  175. fp = fopen("d:\debug\1.pcm","rb");
  176. SetTimer(1,20,NULL);
  177. }
  178. void CG729OutDlg::OnStop() 
  179. {
  180. // TODO: Add your control notification handler code here
  181. if(hwO)
  182. {
  183. // waveOutUnprepareHeader(hwO,&WaveHdrOut,sizeof(WAVEHDR));
  184.  //    ClearOutBuffer();
  185. waveOutClose(hwO);//关声音输出设备
  186. hwO = NULL;
  187. }
  188. for(int i=0;i<BUFFER_SIZE;i++)
  189. {
  190. if(lBufferpHdr[i].lpData)
  191. {
  192. free(lBufferpHdr[i].lpData);
  193. lBufferpHdr[i].lpData = NULL;
  194. }
  195. }
  196. }
  197. LRESULT CG729OutDlg::OnWomDone(WPARAM wParam, LPARAM lParam)
  198. {
  199. if(hwO)
  200. {
  201. LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;
  202. ::waveOutUnprepareHeader(hwO, lpHdr, sizeof(WAVEHDR));
  203. free(lpHdr->lpData);
  204. lpHdr->lpData = NULL;
  205. // fclose(fp);
  206. // fp = NULL;
  207. // KillTimer(1);
  208. }
  209. return true;
  210. }
  211. void CG729OutDlg::OnTimer(UINT nIDEvent) 
  212. {
  213. // TODO: Add your message handler code here and/or call default
  214. switch(nIDEvent)
  215. {
  216. case 1:
  217. {
  218. if(fp)
  219. {
  220. PlayWave();
  221. }
  222. break;
  223. }
  224. }
  225. CDialog::OnTimer(nIDEvent);
  226. }
  227. void CG729OutDlg::PlayWave()
  228. {
  229. long DataLen = 320;
  230. if(lBufferpHdr[lBufferCounter].lpData)
  231. {
  232. ::waveOutUnprepareHeader(hwO, &lBufferpHdr[lBufferCounter], sizeof(WAVEHDR));
  233. free(lBufferpHdr[lBufferCounter].lpData);
  234. lBufferpHdr[lBufferCounter].lpData = NULL;
  235. }
  236. lBufferpHdr[lBufferCounter].dwBufferLength = DataLen;
  237. lBufferpHdr[lBufferCounter].lpData = (char *)calloc(DataLen,sizeof(char));
  238. fread(lBufferpHdr[lBufferCounter].lpData,sizeof(char),DataLen,fp);
  239. // memcpy(lBufferpHdr[lBufferCounter].lpData,WaveData.pbVal,DataLen);
  240. waveOutPrepareHeader(hwO, &lBufferpHdr[lBufferCounter], sizeof(WAVEHDR));
  241. //播放该语音数据
  242. waveOutWrite(hwO, &lBufferpHdr[lBufferCounter], sizeof(WAVEHDR));
  243. // waveOutUnprepareHeader(hwO,&WaveHdrOut,sizeof(WAVEHDR));
  244. lBufferCounter ++;
  245. lBufferCounter = lBufferCounter % BUFFER_SIZE;
  246. }