SoundDlg.cpp
上传用户:hygd004
上传日期:2022-02-04
资源大小:1841k
文件大小:7k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. // SoundDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Sound.h"
  5. #include "SoundDlg.h"
  6. #include "process.h"
  7. #include "DirectSound.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CSoundDlg dialog
  15. CSoundDlg::CSoundDlg(CWnd* pParent /*=NULL*/)
  16. : CDialog(CSoundDlg::IDD, pParent)
  17. {
  18. //{{AFX_DATA_INIT(CSoundDlg)
  19. // NOTE: the ClassWizard will add member initialization here
  20. //}}AFX_DATA_INIT
  21. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  22. }
  23. void CSoundDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CSoundDlg)
  27. // NOTE: the ClassWizard will add DDX and DDV calls here
  28. //}}AFX_DATA_MAP
  29. }
  30. BEGIN_MESSAGE_MAP(CSoundDlg, CDialog)
  31. //{{AFX_MSG_MAP(CSoundDlg)
  32. ON_WM_PAINT()
  33. ON_WM_QUERYDRAGICON()
  34. ON_BN_CLICKED(IDC_Sound_Play, OnSound2)
  35. ON_BN_CLICKED(IDC_Sound_stop, OnSound1)
  36. ON_BN_CLICKED(IDC_sound_pause, OnBothSounds)
  37. ON_WM_DESTROY()
  38. ON_BN_CLICKED(IDC_BUTTON_Slow, OnBUTTONSlow)
  39. ON_BN_CLICKED(IDC_BUTTON_Fast, OnBUTTONFast)
  40. ON_BN_CLICKED(IDC_BUTTON_Normal, OnBUTTONNormal)
  41. //}}AFX_MSG_MAP
  42. //ON_BN_CLICKED(IDC_Sound_stop2, &CSoundDlg::OnBnClickedSoundstop2)
  43. ON_BN_CLICKED(IDC_Sound_open, &CSoundDlg::OnBnClickedSoundopen)
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSoundDlg message handlers
  47. BOOL CSoundDlg::OnInitDialog()
  48. {
  49. CDialog::OnInitDialog();
  50. SetIcon(m_hIcon, TRUE); // Set big icon
  51. SetIcon(m_hIcon, FALSE); // Set small icon
  52. m_hThread = 0;
  53. m_bStop = 0;
  54. m_bContine = TRUE;
  55. m_sndSound1 = new CDirectSound;
  56. return TRUE;  // return TRUE  unless you set the focus to a control
  57. }
  58. // If you add a minimize button to your dialog, you will need the code below
  59. //  to draw the icon.  For MFC applications using the document/view model,
  60. //  this is automatically done for you by the framework.
  61. void CSoundDlg::OnPaint() 
  62. {
  63. if (IsIconic())
  64. {
  65. CPaintDC dc(this); // device context for painting
  66. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  67. // Center icon in client rectangle
  68. int cxIcon = GetSystemMetrics(SM_CXICON);
  69. int cyIcon = GetSystemMetrics(SM_CYICON);
  70. CRect rect;
  71. GetClientRect(&rect);
  72. int x = (rect.Width() - cxIcon + 1) / 2;
  73. int y = (rect.Height() - cyIcon + 1) / 2;
  74. // Draw the icon
  75. dc.DrawIcon(x, y, m_hIcon);
  76. }
  77. else
  78. {
  79. CDialog::OnPaint();
  80. }
  81. }
  82. HCURSOR CSoundDlg::OnQueryDragIcon()
  83. {
  84. return (HCURSOR) m_hIcon;
  85. }
  86. unsigned int WINAPI FileReadProc(LPVOID pOwner)
  87. {
  88. CSoundDlg* pThis = (CSoundDlg*)pOwner;
  89. pThis->ReadFileProc();
  90. return 1;
  91. }
  92. void CSoundDlg::ReadFileProc()
  93. {
  94. //BYTE buf[1025];
  95. int n = 0;
  96. while(1)
  97. {
  98. BYTE buf[1025];
  99. // Sleep(0);
  100. int nlen;
  101. if(m_bStop)
  102. break;
  103.   nlen = fread(buf,1,1024,fp);
  104. if(!nlen)
  105. break;
  106. while(m_sndSound1->WriteDataToBuf(buf,nlen)==-1)
  107. {
  108. Sleep(100);
  109. }
  110. }
  111. if(fp)
  112. {
  113. fclose(fp);
  114. fp = NULL;
  115. }
  116. }
  117. typedef struct _WAVE_FORMAT
  118. {
  119.     WORD  AudioFormat;
  120.     WORD  NumChannels;
  121.     DWORD SampleRate;
  122.     DWORD ByteRate;
  123.     WORD  BlockAlign;
  124.     WORD  BitsPerSample;
  125. }WAVE_FORMAT,*PWAVE_FORMAT;
  126. void CSoundDlg::OnSound2() 
  127. {
  128. // m_sndSound1 = new CDirectSound();
  129. AUDIO_CONFIG WaveHead;
  130. WaveHead.wFormatTag = 1;
  131. WaveHead.nChannels = 2;
  132. WaveHead.nSamplesPerSec = 44100;
  133. WaveHead.nAvgBytesPerSec = 176400;
  134. WaveHead.nBlockAlign = 4;
  135. WaveHead.wBitsPerSample = 16;
  136. // WaveHead.cbSize = 24932;
  137. DWORD fdwSound =0;
  138. if(m_soundfile.IsEmpty())
  139. MessageBox("请选中播放文件!");
  140. m_bStop = 0;
  141. // m_sndSound1->SetHWnd(AfxGetMainWnd()->m_hWnd);
  142. unsigned int dwReadID;
  143. m_bContine = 1;
  144.  fp = fopen((LPCSTR)m_soundfile,"rb");
  145.     fseek(fp,20,0); //Skip previous 20 bytes RIFF header
  146.     WAVE_FORMAT waveFormat;
  147.     int nLen=0;
  148.     nLen=fread(&waveFormat,1,sizeof(WAVE_FORMAT),fp);
  149.     
  150.     WaveHead.nBlockAlign=waveFormat.BlockAlign;
  151.     WaveHead.nChannels=waveFormat.NumChannels;
  152.     WaveHead.nSamplesPerSec=waveFormat.SampleRate;
  153.     WaveHead.wBitsPerSample=waveFormat.BitsPerSample;
  154.     WaveHead.nAvgBytesPerSec=waveFormat.ByteRate;
  155.     //WaveHead.nSamplesPerSec=0;
  156.  
  157.     
  158.     m_sndSound1->CreateDSound(&WaveHead,8000);
  159.     fseek(fp,20+sizeof(WAVE_FORMAT),0);
  160.     m_hThread = (HANDLE)_beginthreadex(0,0,FileReadProc,this,0,&dwReadID);
  161. Sleep(200);
  162. m_sndSound1->Play();
  163. GetDlgItem(IDC_Sound_open)->EnableWindow(FALSE);
  164. GetDlgItem(IDC_Sound_Play)->EnableWindow(FALSE);
  165.     GetDlgItem(IDC_BUTTON_Fast)->EnableWindow(TRUE);
  166.     GetDlgItem(IDC_BUTTON_Slow)->EnableWindow(TRUE);
  167.     GetDlgItem(IDC_BUTTON_Normal)->EnableWindow(TRUE);
  168.     GetDlgItem(IDC_Sound_stop)->EnableWindow(TRUE);
  169.     GetDlgItem(IDC_sound_pause)->EnableWindow(TRUE);
  170. //ReadFileProc();
  171. }
  172. void CSoundDlg::OnSound1() 
  173. {
  174. // m_sndSound2.Play();
  175. m_bStop = 1;
  176. if(m_hThread)
  177. {
  178. WaitForSingleObject(m_hThread,2000);
  179. CloseHandle(m_hThread);
  180. m_hThread = NULL;
  181. }
  182. m_sndSound1->Stop();
  183. GetDlgItem(IDC_Sound_open)->EnableWindow(TRUE);
  184. GetDlgItem(IDC_Sound_Play)->EnableWindow(TRUE);
  185.     GetDlgItem(IDC_BUTTON_Fast)->EnableWindow(FALSE);
  186.     GetDlgItem(IDC_BUTTON_Slow)->EnableWindow(FALSE);
  187.     GetDlgItem(IDC_BUTTON_Normal)->EnableWindow(FALSE);
  188.     GetDlgItem(IDC_Sound_stop)->EnableWindow(FALSE);
  189.     GetDlgItem(IDC_sound_pause)->EnableWindow(FALSE);
  190. // delete m_sndSound1;
  191. // if(fp)
  192. // fclose(fp);
  193. }
  194. void CSoundDlg::OnBothSounds() 
  195. {
  196. // m_sndSound1.Play();
  197. // m_sndSound2.Play();
  198. if(m_bContine)
  199. {
  200. m_sndSound1->Pause();
  201. m_bContine = 0;
  202. }
  203. else
  204. {
  205. m_sndSound1->Continue();
  206. m_bContine = 1;
  207. }
  208. }
  209. void CSoundDlg::OnDestroy() 
  210. {
  211. CDialog::OnDestroy();
  212. // TODO: Add your message handler code here
  213. m_bStop = 1;
  214. if(m_hThread)
  215. {
  216. WaitForSingleObject(m_hThread,2000);
  217. CloseHandle(m_hThread);
  218. m_hThread = 0;
  219. }
  220. OnSound1();
  221. delete m_sndSound1 ;
  222. }
  223. void CSoundDlg::OnBUTTONSlow() 
  224. {
  225. // TODO: Add your control notification handler code here
  226. m_sndSound1->SetPlaySpeed(0.5);
  227. }
  228. void CSoundDlg::OnBUTTONFast() 
  229. {
  230. // TODO: Add your control notification handler code here
  231. m_sndSound1->SetPlaySpeed(2.0);
  232. }
  233. void CSoundDlg::OnBUTTONNormal() 
  234. {
  235. // TODO: Add your control notification handler code here
  236. m_sndSound1->SetPlaySpeed(1.0);
  237. }
  238. void CSoundDlg::OnBnClickedSoundstop2()
  239. {
  240. // TODO: 在此添加控件通知处理程序代码
  241. }
  242. void CSoundDlg::OnBnClickedSoundopen()
  243. {
  244. // TODO: 在此添加控件通知处理程序代码
  245.     LPCTSTR lpszFilter ="Wave File(*.wav)|*.wav|All Files|*.*||";
  246. CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,lpszFilter);
  247. if(dlg.DoModal()==IDOK)
  248. {
  249. m_soundfile = dlg.GetPathName();
  250. }
  251. GetDlgItem(IDC_Sound_open)->EnableWindow(FALSE);
  252. GetDlgItem(IDC_Sound_Play)->EnableWindow(TRUE);
  253.     GetDlgItem(IDC_BUTTON_Fast)->EnableWindow(FALSE);
  254.     GetDlgItem(IDC_BUTTON_Slow)->EnableWindow(FALSE);
  255.     GetDlgItem(IDC_BUTTON_Normal)->EnableWindow(FALSE);
  256.     GetDlgItem(IDC_Sound_stop)->EnableWindow(FALSE);
  257.     GetDlgItem(IDC_sound_pause)->EnableWindow(FALSE);
  258. }