DirectSound Method for WAVE file operations

Source: Internet
Author: User

DirectSound Method for WAVE file operations
Although the call is simple and the function is powerful, it can meet the basic needs of audio file processing, however, it also has its disadvantages, that is, it can only play one WAVE file at a time, sometimes in practical applications, when two or more WAVE files need to be played at the same time to achieve sound mixing effect, you need to use the DirectSound in Microsoft DirectX technology, this technology directly operates the underlying sound card device and enables simultaneous playback of more than eight WAV files.

The following steps are required to implement DirectSound: 1. create and initialize DirectSound; 2. sets the audio device priority of an application, generally DSSCL_NORMAL; 2. read the WAV file into the memory and find the format block, data block location, and Data Length. 3. create sound buffer; 4. load sound data; 5. play and stop:

The following functions use DirectSound technology to play a WAVE audio file (note that the project settings should contain "dsound. lib, dxguid. lib"). The Code and comments are as follows:

Void CPlaysoundView: OnPlaySound ()
{
// TODO: Add your command handler code here
LPVOID lpPtr1; // pointer 1;
LPVOID lpPtr2; // pointer 2;
HRESULT hResult;
DWORD dwLen1, dwLen2;
LPVOID m_pMemory; // memory pointer;
LPWAVEFORMATEX m_pFormat; // LPWAVEFORMATEX variable;
LPVOID m_pData; // pointer to the voice data block;
DWORD m_dwSize; // The length of the voice data block in the WAVE file;
CFile File; // Cfile object;
DWORD dwSize; // The length of the WAV file;
// Open the sound.wav file;
If (! File. Open ("d: // sound.wav", CFile: modeRead | CFile: shareDenyNone ))
Return;
DwSize = File. Seek (0, CFile: end); // obtain the length of the WAVE File;
File. Seek (0, CFile: begin); // locate the Open WAVE File header;
// Allocate memory for m_pMemory. The type is LPVOID, which is used to store data in the WAVE file;
M_pMemory = GlobalAlloc (GMEM_FIXED, dwSize );
If (File. ReadHuge (m_pMemory, dwSize )! = DwSize) // read the data in the file;
{
File. Close ();
Return;
}
File. Close ();
LPDWORD pdw, pdwEnd;
DWORD dwRiff, dwType, dwLength;
If (m_pFormat) // format block pointer
M_pFormat = NULL;
If (m_pData) // data block pointer, type: LPBYTE
M_pData = NULL;
If (m_dwSize) // 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; // determine whether the file header is a "RIFF" character;
If (dwType! = MmioFOURCC ('w', 'A', 'V', 'E '))
Return; // determine whether the file format is "WAVE ";
// Find the format block, data block location, and Data Length
PdwEnd = (DWORD *) (BYTE *) m_pMemory + dwLength-4 );
Bool m_bend = false;
While (pdw <pdwEnd )&&(! M_bend ))
// The pdw file does not indicate that the file ends and the sound data is not obtained;
{
DwType = * pdw ++;
DwLength = * pdw ++;
Switch (dwType)
{
Case mmioFOURCC ('F', 'M', 't', ''): // if it is a" fmt "flag;
If (! M_pFormat) // obtain the LPWAVEFORMATEX structure data;
{
If (dwLength <sizeof (WAVEFORMAT ))
Return;
M_pFormat = (LPWAVEFORMATEX) pdw;
 
}
Break;
Case mmioFOURCC ('D', 'A', 't', 'A'): // if it is a "data" flag;
If (! M_pData |! M_dwSize)
{
M_pData = (LPBYTE) pdw; // obtain the pointer to the sound data block;
M_dwSize = dwLength; // obtain the length of the audio data block;
If (m_pFormat)
M_bend = TRUE;
}
Break;
}
Pdw = (DWORD *) (BYTE *) pdw + (dwLength + 1 )&~ 1); // modify the pdw pointer to continue the loop;

}
DSBUFFERDESC BufferDesc; // defines the DSUBUFFERDESC structure object;
Memset (& BufferDesc, 0, sizeof (BufferDesc ));
BufferDesc. lpwfxFormat = (LPWAVEFORMATEX) m_pFormat;
BufferDesc. dwSize = sizeof (DSBUFFERDESC );
BufferDesc. dwBufferBytes = m_dwSize;
BufferDesc. dwFlags = 0;
HRESULT hRes;
LPDIRECTSOUND m_lpDirectSound;
HRes =: DirectSoundCreate (0, & m_lpDirectSound, 0); // create a DirectSound object;
If (hRes! = DS_ OK)
Return;
M_lpDirectSound-> SetCooperativeLevel (this-> GetSafeHwnd (), DSSCL_NORMAL );
// Set the sound device priority to "NORMAL ";
// Create a sound data buffer;
LPDIRECTSOUNDBUFFER m_pDSoundBuffer;
If (m_lpDirectSound-> CreateSoundBuffer (& BufferDesc, & m_pDSoundBuffer, 0) = DS_ OK)
// Load the sound data. Here we use two pointers, lpPtr1 and lpPtr2, to point to the data in the DirectSoundBuffer buffer. This is designed to process large WAVE files. DwLen1 and dwLen2 correspond to the buffer length pointed to by the two pointers respectively.

HResult = m_pDSoundBuffer-> Lock (0, m_dwSize, & lpPtr1, & dwLen1, & lpPtr2, & dwLen2, 0 );
If (hResult = DS_ OK)
{
Memcpy (lpPtr1, m_pData, dwLen1 );
If (dwLen2> 0)
{
BYTE * m_pData1 = (BYTE *) m_pData + dwLen1;
M_pData = (void *) m_pData1;
Memcpy (lpPtr2, m_pData, dwLen2 );
}
M_pDSoundBuffer-> Unlock (lpPtr1, dwLen1, lpPtr2, dwLen2 );

}
DWORD dwFlags = 0;
M_pDSoundBuffer-> Play (0, 0, dwFlags); // Play WAVE sound data;
}

To better illustrate the implementation of DiretSound programming, I use a function to implement all the operations. Of course, you can wrap the above content into a class, so as to better implement program encapsulation, I don't need to talk about how to implement it. If you really don't understand it, look for a C ++ book. If a class is defined, multiple objects can be declared at a time to achieve mixed playback of multiple WAVE audio files. Readers may find that we have introduced the PCMWAVEFORMAT structure when introducing the WAVE file format. However, we use the LPWAVEFORMATEX structure to read the data of the WAVE file in code implementation, are there any mistakes? In fact, there is no error. For the pcm wave file, the two structures are exactly the same. The LPWAVEFORMATEX structure is just used to facilitate setting the DSBUFFERDESC object.

There are many ways to operate WAVE sound files. You can use them flexibly to operate WAVE files. For detailed usage of these functions, refer to MSDN. This article only gives a superficial introduction to the operations on the WAVE file, hoping to serve readers as a reference.

From yum2006
 

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.