VC under the implementation of DirectSound

Source: Internet
Author: User

Adding music is the simplest of all methods to enhance application functionality. Music can make users happy, and playing the right music on the right occasions can make the programmer and his VC program glow.

----The Microsoft Windows Multimedia API provides three ways to play WAV files:

PlaySound () function. It can be encoded in a single line to play the sound of the wave format. This function has two limitations: the sound data must be fully loaded into physical memory, and the data format must be supported by an audio drive that is configured. Based on experience, PlaySound () applies to files under 100K, MCI (the Media control Interface). MCI function is powerful, not only can realize WAV file playback, but also can play MIDI and CD audio.

Low-level wave audio device. These devices allow you to run application files that fully control wave data.

----The first two methods are simple to use, but can not achieve two and more than two WAV file playback; The third approach is very complex and difficult for non-professional professionals to complete. DirectSound can achieve eight and more than eight WAV files at the same time play, make multimedia programs more vivid, and easy to use, but it is only suitable for Windows 95 program, also does not support the recording and storage of WAV files "" This is the only place that regrets. The following steps are required to implement DirectSound:

1. Create and initialize DirectSound

LPDIRECTSOUND m_pDirectObject;
if(DirectSoundCreate(NULL,&m_pDirectObject,NULL)==DS_OK)
file://设定应用程序的声音设备优先级别方式,一般为DSSCL_NORMAL
m_pDirectObject->SetCooperativeLevel
(this- >m_hWnd,DSSCL_NORMAL);
else
AfxMessageBox("DirectSound Create failed");

2. Read the WAV file into memory, find the format block, data block location and data length.

The

M_pmemory,m_pformat,m_pdata,m_dwsize is declared in the header file.
BOOL cdirectwave::loadfile (CString Filename) {
CFile File;
DWORD dwsize;
if (! File.Open (Filename,
Cfile::moderead | Cfile::sharedenynone))
return FALSE;
dwsize = File.seek (0, cfile::end);
File.seek (0, Cfile::begin);
File://m_pMemory memory block pointer, type: LPVOID
M_pmemory = GlobalAlloc (gmem_fixed, dwsize);
If File.readhuge (m_ Pmemory, dwsize)!= dwsize) {
File.close ();
return FALSE;
}
File.close ();
Lpdword Pdw,pdwend;
DWORD Dwriff,dwtype, dwlength, dwlength;
if (m_pformat) file://format block pointer, type: Lpwaveformatex
M_pformat = NULL;
if (m_pdata) file://data block pointer, type: LPBYTE
m_ PData = NULL;
if (m_dwsize) file://data length, type: DWORD
m_dwsize = 0;
PDW = (DWORD *) m_pmemory;
Dwriff = *pdw++;
Dwlength = *pdw++;
dwtype = *pdw++;
if (Dwriff!= mmiofourcc (' R ', ' I ', ' f ', ' F ')
return FALSE;
if (dwtype!= mmiofourcc (' W ', ' A ', ' V ', ' E ')) return FALSE;
file://looking for format blocks, data block location and data length
Pdwend = (DWORD *) ((BYTE *) PDW + dwLength-4);
while (PDW < pdwend) {
Dwtype = *pdw++;
Dwlength = *pdw++;
Switch (dwtype) {
Case MMIOFOURCC (' f ', ' m ', ' t ', '):
if (!m_pformat) {
if (Dwlength < sizeof (waveform at)
return FALSE;
M_pformat = (Lpwaveformatex) PDW;
if (m_pdata && m_dwsize)
return TRUE;
}
break;
Case MMIOFOURCC (' d ', ' a ', ' t ', ' a '):
if (!m_pdata | |!m_dwsize) {
M_pdata = (lpbyte) PDW;
M_dwsize = Dwlen Gth
if (m_pformat)
return TRUE;
}
break;
}
PDW = (DWORD *) ((BYTE *) PDW +
((dwlength + 1) &~ 1);
}
file://not found, returns false
return false;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.