Directsound Study Notes (5): Using WAV data

Source: Internet
Author: User

Use WAV data

In a Windows Driver Model (WDM), The directsound hardware buffer can play any uncompressed or compressed sound formats which can be described by a waveformatex or waveformatextensible structure and supported by hardware. The software buffer and non-WDM hardware buffer only support 8-bit and 16-bit uncompressed formats.

Waveform (WAV) data is usually stored in files or resources in the Resource Exchange file format (riff. The data includes a WAV format description, including parameters such as the sampling rate and number of output channels.

Multi-band WAV format

In a WDM drive, the directsound buffer supports two or more output channels in WAV format. For speaker configuration such as 5.1, it has speakers at the left, center, right, left, right, and low frequency enhancement devices of the foreground.

The waveformatextensible Structure describes a multi-channel waveform format. This constructor is an extension of waveformatex and configures additional bytes that have been supported by cbsize members in waveformatex. When the location of waveformatex is required, the waveformatextensible structure can be converted to waveformatex.

If the number of physical speakers configured by the system is less than the number of channels specified in the multi-band WAV file, the audio data will be properly mixed and output to the existing speaker. Directsound does not support 3D processing and effects of multi-band buffers. An attempt to create a buffer with the dsbcaps_ctrl3d or dsbcaps_ctrlfx ID and a multi-band WAV format will fail.

Read WAV data

A wav file is a resource exchange file format (riff). It consists of several names that contain header information (such as audio sampling format) or data (sampling itself. Win32 API provides functions such as opening and disabling riff files and exploring blocks. These function names start with "mmio.

To store WAV sounds in an executable way, You can import wav files as resources and name them. Note that the cwavefile class expects these resources to be of the wave or WAV type and is an executable module rather than a DLL.

The directsound API does not include the method for loading sound data. However, the dsutil. cpp file is used by many SDK examples.ProgramThese classes can be used to create sound buffers from files, resources, or a memory address.

The steps for using the example class to initialize directsound and create a buffer to load include:
1. Create a csoundmanager EXAMPLE Class Object.
2. Call csoundmanager: Initialize to create a device object.
3. Pass a file or resource name to csoundmanager: Create or send a memory address to csoundmanager: createfrommemory. These methods return the object of a csound example class, which indicates that one or more static buffers can accommodate data. (You can create multiple buffers to play multiple audio instances at the same time) or pass a file or resource name to the cstreamingsound example class. This method returns the object of a cstreamingsound example class, which represents a separate stream buffer.
4. Call the fillbufferwithsound method of the object obtained in the previous step. It reads data from files, resources, or memory addresses into the buffer zone. For a stream buffer, it fills the buffer with the maximum amount of data that can be accommodated, and uses cstreamingsound: handlewavestreamnotification to update data while playing the buffer.
Note that the actual data is read by a cwavefile object, which is a protected member of the csound or cstreamingsound object. You do not need to use the cwavefile class directly. However, you can refer to the implementation of this class to obtain information for analyzing WAV data.

Calculate the duration of a WAV sound

The duration of a waveform to be played depends on the data size and format. The data size and format can be obtained by calling the cwavefile: getsize and cwavefile: getformat methods in the directsound example framework.
The following example function does not use the example class and returns the duration of a WAV file, in milliseconds:

DWORD getsoundlength (lpstr strfilename)
{
Cwavefile * Pwav;
DWORD dwlen = 0 ;
DWORD dwsize;
Waveformatex * WFX;
 
Pwav = New Cwavefile ();
If (Succeeded (pwav -> Open (strfilename, null, wavefile_read )))
{
WFX = Pwav -> Getformat ();
Dwsize = Pwav -> Getsize ();
Dwlen = (DWORD )( 1000 * Dwsize / WFX -> Navgbytespersec );
Pwav -> Close ();
}
If (Pwav) delete pwav;
Return Dwlen;
}

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.