Use the underlying device interface function to play back the sound

Source: Internet
Author: User

I picked it out from the books borrowed from the library. It's sour to be a typo.,:-)

Use the underlying device interface function to play back the sound

Sampling Point:It is a sample data of the sound, which may occupy 1, 2, and 4 bytes depending on the number of sampling digits. When the sampling data is 16 bits, a sampling point is a short integer, which occupies two bytes. The value range is-32768 ~ 32767. When the sampling data is 8 bits or less, a sampling point is an unsigned dense data with a value ranging from 0 to 255.

The stereo sound has two channels: left and right.
Single-channel sound has only one channel.

Sample frame:The first sampling point of the left sound channel and the first sampling point of the right sound channel constitute a sampling frame. Similarly, their second sampling point forms a sampling frame. For a single channel, a sampling point is a sampling frame.

The function used must contain the header file mmsystem. H, and add the winmm. Lib file to the database during the compilation connection.

1. Enable the audio device
With waveoutopen, you must specify the following parameters during the call:
Device ID, typically using wave_mapper, that is, a standard audio device;
Sample data format, such as sampling rate and number of sampling digits;
Callback method.
If the call succeeds, we will get a device handle, which will be required for future function calls.
When you open an audio device, you can pass a function address or an event or window handle to the system.

2. Transmit sampling data
First, use waveoutprepareheader to prepare the sample data, and then use waveoutwrite to send the prepared data to the device for playback. After the playback is complete, use waveoutunprepareheader to cancel the preparation of the sample data.

3. Disable the device
Use the waveoutclose function.

The following is a piece of code that uses underlying function playback. Assume that the sample data has been taken out of the wav file and placed in the memory pointed to by the lpwavedata pointer. The size is wavesize, and the sound format is PCM and stereo, sampling frequency 44.1 kHz, sampling bit 16:

// Enable the audio device
Hwaveout hwave;
Pcmwaveformat FMt = {1, // PCM
2, // number of channels
44100, // sampling frequency
4*44100 // data volume per second (bytes)
4}, // sample frame size
16}; // sampling bit
Handle hevent = createevent (null, false, false, null );
Waveoutopen (& hwave, wave_mapper, (lpwaveformatex) & FMT, (DWORD) hevent, 0, callback_event );

// Prepare data
Wavehdr whdr;
Memset (& whdr, 0, sizeof (wavehdr ));
Whdr. lpdata = lpwavedata;
Whdr. dwbufferlength = wavesize;
Waveoutprepareheader (hwave, & whdr, sizeof (wavehdr ));

// Output
Waveoutwrite (hwave, & whdr, sizeof (wavehdr ));

// Wait until playback is complete
Waitforsingleobject (hevent, infinite );

// Cancel preparation
Waveoutunprepareheader (hwave, & whdr, sizeof (wavehdr ));

// Close the device
Waveoutclose (hwave );
Waitforsingleobject (hevent, infinite );
Closehandle (hevent );

 

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.