Wave.cpp
资源名称:AudioWave.rar [点击查看]
上传用户:huifengb
上传日期:2007-12-27
资源大小:334k
文件大小:5k
源码类别:
多媒体
开发平台:
Visual C++
- // Wave.cpp: implementation of the CWave class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Wave.h"
- //
- #include <afxwin.h>
- #include <mmsystem.h>
- //
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CWave::CWave()
- {
- m_nType = 0;
- m_bRecording = FALSE;
- m_bOpened = FALSE;
- m_pSoundData = NULL;
- m_wDeviceID = 0;
- }
- CWave::~CWave()
- {
- Close ();
- }
- int CWave::GetDevices (void)
- {
- return waveOutGetNumDevs ();
- }
- BOOL CWave::LoadFromDisk (CString Filename)
- {
- Close ();
- CFile File;
- if (!File.Open (Filename, CFile::modeRead))
- return FALSE;
- DWORD dwLength = File.GetLength ();
- m_pSoundData = AllocMem (dwLength);
- if (!m_pSoundData){
- File.Close ();
- return FALSE;
- }
- if (File.ReadHuge (m_pSoundData, dwLength) != dwLength){
- File.Close ();//把数据读入该内存
- return FALSE;
- }
- File.Close ();
- m_bOpened = TRUE;
- m_nType = 1;//标志:从磁盘文件读入数据
- return TRUE;
- }
- /*
- BOOL CWave::LoadFromDisk (CString Filename,DWORD BeginPoint,DWORD Length)//added by myself;
- {
- Close ();
- CFile File;
- if (!File.Open (Filename, CFile::modeRead))
- return FALSE;
- if(BeginPoint+Length>File.GetLength ())
- return FALSE;
- m_pSoundData = AllocMem (Length);
- if (!m_pSoundData)
- {
- File.Close ();
- return FALSE;
- }
- File.Seek(BeginPoint,CFile::begin);
- if (File.ReadHuge (m_pSoundData, Length) != Length)
- {
- File.Close ();//把数据读入该内存
- return FALSE;
- }
- File.Close ();
- m_bOpened = TRUE;
- m_nType = 1;//标志:从磁盘文件读入数据
- return TRUE;
- }
- */
- BOOL CWave::LoadFromRes (CString ResID, HINSTANCE hInstance)
- {
- Close ();
- m_hInstance = hInstance;
- HANDLE hResInfo;
- hResInfo = FindResource (m_hInstance, ResID, "WAVE");
- if (!hResInfo)
- return FALSE;
- m_hResHandle = LoadResource (m_hInstance, (HRSRC) hResInfo);
- if (!m_hResHandle)
- return FALSE;
- m_pSoundData = LockResource (m_hResHandle);
- if (!m_pSoundData)
- return FALSE;
- m_bOpened = TRUE;
- m_nType = 2;//标记:从资源读入数据
- return TRUE;
- }
- void CWave::Close (void)
- {
- PlaySound (NULL, NULL, NULL);
- if (m_pSoundData){
- if (m_nType == 1)//释放内存
- FreeMem (m_pSoundData);
- if (m_nType == 2){
- UnlockResource (m_hResHandle);
- FreeResource (m_hResHandle);
- }
- m_pSoundData = NULL;
- }
- m_nType = 0;
- m_bRecording = FALSE;
- m_bOpened = FALSE;
- if (m_wDeviceID)
- {
- mciSendCommand (m_wDeviceID, MCI_STOP, MCI_WAIT, NULL);
- mciSendCommand (m_wDeviceID, MCI_CLOSE, NULL, NULL);
- }
- m_wDeviceID = 0;
- }
- BOOL CWave::Play (BOOL bLoop)
- {
- if (m_bOpened)
- {
- if (m_bRecording)
- {
- Stop ();
- MCI_PLAY_PARMS PlayParms;
- mciSendCommand (m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL);
- if (mciSendCommand (m_wDeviceID, MCI_PLAY, NULL, (DWORD)(LPVOID) &PlayParms))
- return FALSE;
- else
- return TRUE;
- }
- else
- {
- DWORD dwFlags = SND_MEMORY | SND_ASYNC | SND_NODEFAULT;
- if (bLoop)
- dwFlags |= SND_LOOP;
- return PlaySound ((char *) m_pSoundData, NULL, dwFlags);
- }
- }
- return FALSE;
- }
- void CWave::Stop (void)
- {
- if (m_bRecording)
- mciSendCommand (m_wDeviceID, MCI_STOP, NULL, NULL);
- else
- PlaySound (NULL, NULL, NULL);
- }
- BOOL CWave::Record (void)
- {
- Close ();
- MCI_OPEN_PARMS OpenParms;
- OpenParms.lpstrDeviceType = "waveaudio";
- OpenParms.lpstrElementName = "";
- if (mciSendCommand (NULL, MCI_OPEN, MCI_OPEN_ELEMENT |
- MCI_OPEN_TYPE | MCI_WAIT, (DWORD)(LPVOID) &OpenParms))
- return FALSE;
- m_wDeviceID = OpenParms.wDeviceID;
- MCI_RECORD_PARMS RecordParms;
- if (mciSendCommand (m_wDeviceID, MCI_RECORD, NULL,
- (DWORD)(LPVOID) &RecordParms))
- return FALSE;
- m_bRecording = TRUE;
- m_bOpened = TRUE;
- return TRUE;
- }
- BOOL CWave::Save (CString Filename)
- {//只用于存储录音得到的数据!
- if (!m_bRecording)
- return FALSE;
- Stop ();
- MCI_SAVE_PARMS SaveParms;
- SaveParms.lpfilename = (LPCSTR) Filename;
- if (mciSendCommand (m_wDeviceID, MCI_SAVE, MCI_SAVE_FILE |
- MCI_WAIT, (DWORD)(LPVOID) &SaveParms))
- return FALSE;
- m_bRecording = FALSE;
- //LoadFromDisk (Filename);//我注释掉的/QSD
- return TRUE;
- }
- BOOL CWave::PlayFromDisk (CString Filename)
- {
- return PlaySound (Filename, NULL, SND_FILENAME | SND_SYNC | SND_NODEFAULT);
- }
- BOOL CWave::PlayFromRes (CString ResID, HINSTANCE hInstance)
- {
- return PlaySound (ResID, hInstance, SND_RESOURCE | SND_SYNC | SND_NODEFAULT);
- }
- void *CWave::AllocMem (DWORD dwSize)
- {
- HGLOBAL Global;
- HGLOBAL *pGlobal;
- unsigned char *pMem;
- Global = GlobalAlloc (GMEM_FIXED, (DWORD) dwSize + 10);
- if (!Global) return (NULL);
- pMem = (unsigned char *) GlobalLock (Global);
- pGlobal = (HGLOBAL *) pMem;
- *pGlobal = Global;
- pMem += sizeof (HGLOBAL);
- return (pMem);
- }
- void CWave::FreeMem (void far *pMem)
- {
- HGLOBAL Global;
- HGLOBAL *pGlobal;
- unsigned char *pUChar;
- pUChar = (unsigned char *) pMem;
- pUChar -= sizeof (HGLOBAL);
- pGlobal = (HGLOBAL *) pUChar;
- Global = *pGlobal;
- GlobalUnlock (Global);
- GlobalFree (Global);
- }