How to Use Wave Audio Volume Control APIs

Summary

The functions waveOutGetVolume() and waveOutSetVolume() can be used to set and retrieve a wave output device's volume. The DWORD passed or retrieved contains the left channel's volume in the low-order word and the right channel's volume in the high-order word. In this article, the use of these functions is illustrated through a single function that can be added to the Generic sample.

More Information

Although the Mixer APIs included in the Win32 SDK offer more extensive control, quick and simple audio volume control can be obtained by using the waveOutGetVolume() and waveOutSetVolume() functions.


By adding the code for the ShowVolume() function (given in this article) to the Generic sample, you can cause it to output message boxes about the state of the volume control. While the sample is running, the WSS Audio Conrol's Volume Control (or a given sound card's mixer control that displays wave output volume) undergoes changes in its slider positions as the values for right and left channel volume are changed by the ShowVolume() function.


As the sample runs, the first message box reports the number of wave output devices. The second and third message boxes report a left channel volume of 8192 and a right channel volume of 16384 (out of a maximum of 65535). The horizontal slider in the Wave column of the Volume Control is the balance slider, and it is set to the right because the right volume is greater now. Also, the vertical slider in the Wave column, which is the volume slider, is set to the 1/4 position, tracking the larger volume setting of the two channels.


The fourth and fifth messages boxes report a left and right volume of 32768 and 16384 respectively. Now the balance slider is to the left, and the wave volume is about 1/2, again tracking the larger value.

ShowVolume() Code to Be Added to the Generic Sample

// Link with mmsystem.lib for 16-bit applications and
// link with winmm.lib for 32-bit applications.

#include <mmsystem.h>
#include <stdlib.h>

void ShowVolume(void); //Prototype the function early in the app

void ShowVolume(void)
{
// This is the function that can be added to the Generic Sample to
// illustrate the use of waveOutGetVolume() and waveOutSetVolume().

char buffer[40];
char printbuf[80];
UINT uRetVal, uNumDevs;
DWORD volume;
long lLeftVol, lRightVol;

WAVEOUTCAPS waveCaps;

// Make sure there is at least one
// wave output device to work with.
if (uNumDevs = waveOutGetNumDevs())
{
itoa((int)uNumDevs, buffer, 10);
wsprintf(printbuf, "Number of devices is %s\n", (LPSTR)buffer);
MessageBox(GetFocus(), printbuf, "NumDevs", MB_OK);
}

// This sample uses a hard-coded 0 as the device ID, but retail
// applications should loop on devices 0 through N-1, where N is the
// number of devices returned by waveOutGetNumDevs().
if (!waveOutGetDevCaps(0,(LPWAVEOUTCAPS)&waveCaps,
sizeof(WAVEOUTCAPS)))

{
// Verify the device supports volume changes
if(waveCaps.dwSupport & WAVECAPS_VOLUME)
{
// The low word is the left volume, the high word is the right.
// Set left channel: 2000h is one-eighth volume (8192 base ten).
// Set right channel: 4000h is quarter volume (16384 base ten).
uRetVal = waveOutSetVolume(0, (DWORD)0x40002000UL);

// Now get and display the volumes.
uRetVal = waveOutGetVolume(0, (LPDWORD)&volume);

lLeftVol = (long)LOWORD(volume);
lRightVol = (long)HIWORD(volume);

ltoa(lLeftVol, buffer, 10);
wsprintf(printbuf, "Left Volume is %s\n", (LPSTR)buffer);
MessageBox(GetFocus(), printbuf, "Left Volume", MB_OK);

ltoa(lRightVol, buffer, 10);
wsprintf(printbuf, "Right Volume is %s\n", (LPSTR)buffer);
MessageBox(GetFocus(), printbuf, "Right Volume", MB_OK);

// The low word is the left volume, the high word is the right.
// Set left channel: 8000h is half volume (32768 base ten).
// Set right channel: 4000h is quarter volume (16384 base ten).
uRetVal = waveOutSetVolume(0, (DWORD)0x40008000UL);

// Now get and display the volumes.
uRetVal = waveOutGetVolume(0, (LPDWORD)&volume);

lLeftVol = (long)LOWORD(volume);
lRightVol = (long)HIWORD(volume);

ltoa(lLeftVol, buffer, 10);
wsprintf(printbuf, "Left Volume is %s\n", (LPSTR)buffer);
MessageBox(GetFocus(), printbuf, "Left Volume", MB_OK);

ltoa(lRightVol, buffer, 10);
wsprintf(printbuf, "Right Volume is %s\n", (LPSTR)buffer);
MessageBox(GetFocus(), printbuf, "Right Volume", MB_OK);

}
}
}
Lastnosti

ID članka: 139098 – Zadnji pregled: 6. mar. 1999 – Revizija: 1

Povratne informacije