RecordAudioDlg.cpp
上传用户:shenwh911
上传日期:2013-07-04
资源大小:54k
文件大小:8k
源码类别:

Audio

开发平台:

Visual C++

  1. // RecordAudioDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "RecordAudio.h"
  5. #include "RecordAudioDlg.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. #define WM_MAPER_NOTIFY  WM_USER+1001
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CRecordAudioDlg dialog
  51. CRecordAudioDlg::CRecordAudioDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CRecordAudioDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CRecordAudioDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CRecordAudioDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CRecordAudioDlg)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CRecordAudioDlg, CDialog)
  68. //{{AFX_MSG_MAP(CRecordAudioDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BUTTON1, OnRecord)
  73. ON_BN_CLICKED(IDCANCEL, OnStop)
  74. ON_CBN_SELCHANGE(IDC_CMB_IN_NAMES, OnSelchangeCmbInNames)
  75. ON_WM_CLOSE()
  76. ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_RECORD, OnReleasedcaptureSliderRecord)
  77. ON_BN_CLICKED(IDC_STOP, OnStop)
  78. ON_BN_CLICKED(IDC_BUTTON2, OnPlay)
  79. //}}AFX_MSG_MAP
  80. ON_MESSAGE(WM_MAPER_NOTIFY, OnMixerInfo)
  81. END_MESSAGE_MAP()
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CRecordAudioDlg message handlers
  84. BOOL CALLBACK InputLineProc (UINT uLineIndex, MIXERLINE* pLineInfo, DWORD dwUserValue)
  85. {
  86. ::SendMessage((HWND)dwUserValue, WM_MAPER_NOTIFY, (WPARAM)uLineIndex, (LPARAM)pLineInfo->szName);
  87. return TRUE;
  88. }
  89. BOOL CRecordAudioDlg::OnInitDialog()
  90. {
  91. CDialog::OnInitDialog();
  92. // Add "About..." menu item to system menu.
  93. // IDM_ABOUTBOX must be in the system command range.
  94. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  95. ASSERT(IDM_ABOUTBOX < 0xF000);
  96. CMenu* pSysMenu = GetSystemMenu(FALSE);
  97. if (pSysMenu != NULL)
  98. {
  99. CString strAboutMenu;
  100. strAboutMenu.LoadString(IDS_ABOUTBOX);
  101. if (!strAboutMenu.IsEmpty())
  102. {
  103. pSysMenu->AppendMenu(MF_SEPARATOR);
  104. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  105. }
  106. }
  107. // Set the icon for this dialog.  The framework does this automatically
  108. //  when the application's main window is not a dialog
  109. SetIcon(m_hIcon, TRUE); // Set big icon
  110. SetIcon(m_hIcon, FALSE); // Set small icon
  111. char szDevName[100] = {0};
  112. UINT uiDevs = m_AudioCtrl.GetWaveInDevs();
  113. for(UINT i=0; i<uiDevs; i++)
  114. {
  115. strcpy(szDevName, m_AudioCtrl.GetWaveInName(i));
  116. GetDevsName(szDevName);
  117. }
  118. UINT uiAvailabeDev = m_AudioCtrl.GetWaveInAvailableDev();
  119. SetCurDevIndex(uiAvailabeDev);
  120. CVolumeCtrl::EnumerateInputLines(uiAvailabeDev, InputLineProc, (DWORD)this->m_hWnd);
  121. SetCurMixerIndex();
  122. DWORD dwMinVolume = 0, dwMaxVolume = 0;
  123. DWORD dwCurVolume = m_AudioCtrl.SetMixerDevs(0, &dwMinVolume, &dwMaxVolume);
  124. ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_RECORD))->SetRange(dwMinVolume, dwMaxVolume);
  125. ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_RECORD))->SetPos(dwCurVolume);
  126. CRect rc;
  127. GetDlgItem(IDC_SATC_GRAPH)->GetWindowRect(&rc);
  128. ScreenToClient(&rc);
  129. if (!m_AudioCtrl.InitCtrl(((CComboBox*)GetDlgItem(IDC_CMB_DEVS_NAME))->GetCurSel(), 1024*4, GetSafeHwnd(), rc, NULL, (WPARAM)this))
  130. {
  131. MessageBox ( "Initialize callback device failed" );
  132. }
  133. return TRUE;  // return TRUE  unless you set the focus to a control
  134. }
  135. LRESULT CRecordAudioDlg::OnMixerInfo(WPARAM wParam, LPARAM lParam)
  136. {
  137. CComboBox* pCmb = (CComboBox*)GetDlgItem(IDC_CMB_IN_NAMES);
  138. if (pCmb)
  139. {
  140. pCmb->AddString(LPCTSTR(lParam));
  141. }
  142. return S_OK;
  143. }
  144. void CRecordAudioDlg::OnSysCommand(UINT nID, LPARAM lParam)
  145. {
  146. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  147. {
  148. CAboutDlg dlgAbout;
  149. dlgAbout.DoModal();
  150. }
  151. else
  152. {
  153. CDialog::OnSysCommand(nID, lParam);
  154. }
  155. }
  156. // If you add a minimize button to your dialog, you will need the code below
  157. //  to draw the icon.  For MFC applications using the document/view model,
  158. //  this is automatically done for you by the framework.
  159. void CRecordAudioDlg::OnPaint() 
  160. {
  161. if (IsIconic())
  162. {
  163. CPaintDC dc(this); // device context for painting
  164. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  165. // Center icon in client rectangle
  166. int cxIcon = GetSystemMetrics(SM_CXICON);
  167. int cyIcon = GetSystemMetrics(SM_CYICON);
  168. CRect rect;
  169. GetClientRect(&rect);
  170. int x = (rect.Width() - cxIcon + 1) / 2;
  171. int y = (rect.Height() - cyIcon + 1) / 2;
  172. // Draw the icon
  173. dc.DrawIcon(x, y, m_hIcon);
  174. }
  175. else
  176. {
  177. CDialog::OnPaint();
  178. }
  179. }
  180. // The system calls this to obtain the cursor to display while the user drags
  181. //  the minimized window.
  182. HCURSOR CRecordAudioDlg::OnQueryDragIcon()
  183. {
  184. return (HCURSOR) m_hIcon;
  185. }
  186. void CRecordAudioDlg::GetDevsName(char *pszName)
  187. {
  188. CComboBox* pCmbox = (CComboBox*)GetDlgItem(IDC_CMB_DEVS_NAME);
  189. if (pCmbox)
  190. {
  191. pCmbox->AddString(pszName);
  192. }
  193. }
  194. void CRecordAudioDlg::SetCurDevIndex(UINT uiIndex)
  195. {
  196. CComboBox* pCmbox = (CComboBox*)GetDlgItem(IDC_CMB_DEVS_NAME);
  197. if (pCmbox)
  198. {
  199. pCmbox->SetCurSel(uiIndex);
  200. }
  201. }
  202. void CRecordAudioDlg::SetCurMixerIndex()
  203. {
  204. CComboBox* pCmbox = (CComboBox*)GetDlgItem(IDC_CMB_IN_NAMES);
  205. if (pCmbox)
  206. {
  207. pCmbox->SetCurSel(0);
  208. }
  209. }
  210. void CRecordAudioDlg::OnRecord() 
  211. {
  212. CComboBox* pCmb = (CComboBox*)GetDlgItem(IDC_CMB_IN_NAMES);
  213. m_AudioCtrl.Record(pCmb->GetCurSel(), 11025*2, 16);
  214. m_AudioCtrl.SetRecordAudioFile(0, "text.wav");
  215. }
  216. void CRecordAudioDlg::OnStop() 
  217. {
  218. m_AudioCtrl.Stop();
  219. }
  220. void CRecordAudioDlg::SetRecordDev(UINT uiIndex)
  221. {
  222. m_AudioCtrl.SetMixerDevs(uiIndex);
  223. }
  224. void CRecordAudioDlg::OnSelchangeCmbInNames() 
  225. {
  226. m_AudioCtrl.SetMixerDevs(((CComboBox*)GetDlgItem(IDC_CMB_IN_NAMES))->GetCurSel());
  227. }
  228. void CRecordAudioDlg::OnOK() 
  229. {
  230. CDialog::OnOK();
  231. }
  232. void CRecordAudioDlg::OnClose() 
  233. {
  234. OnOK();
  235. }
  236. void CRecordAudioDlg::SetRecordVolumeRand(DWORD dwMin, DWORD dwMax)
  237. {
  238. }
  239. void CRecordAudioDlg::OnReleasedcaptureSliderRecord(NMHDR* pNMHDR, LRESULT* pResult) 
  240. {
  241. CSliderCtrl* pRecord = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_RECORD);
  242. if (pRecord)
  243. {
  244. m_AudioCtrl.SetCurVolume(pRecord->GetPos());
  245. }
  246. *pResult = 0;
  247. }
  248. void CRecordAudioDlg::OnPlay() 
  249. {
  250. if (!m_AudioCtrl.IsPlay())
  251. {
  252. CString csFileName = GetFileName ( TRUE, "打开文件来播放", NULL, "Wave files(*.wav)|*.wav||" );
  253. if (csFileName.IsEmpty())
  254. {
  255. return;
  256. }
  257. m_AudioCtrl.Play(csFileName.GetBuffer(csFileName.GetLength()));
  258. }
  259. }
  260. CString CRecordAudioDlg::GetFileName(BOOL bOpen, LPCTSTR lpszTitle, LPCTSTR lpszFileName, char *szFilter)
  261. {
  262. char szCurDir[MAX_PATH] = {0};
  263. DWORD dwRet = ::GetModuleFileName(NULL,(LPTSTR)szCurDir,sizeof(szCurDir));
  264. if ( szCurDir[dwRet-1] != '\' )
  265. strncat ( szCurDir, "\", sizeof(szCurDir) );
  266. CFileDialog FileDlg ( bOpen, ".wav", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL );
  267. FileDlg.m_ofn.lpstrTitle = LPSTR ( lpszTitle );
  268. if ( FileDlg.DoModal () != IDOK )
  269. return "";
  270. return FileDlg.GetPathName();
  271. }