The volume control panel applet in Windows CE controls the volume by dividing the possible volume by 5 and letting the user index between those settings. So it doesn’t allow for adjustment of to all of the possible volume levels. This makes sense because really the difference between a volume level of 60,000 and 60,001 is not really perceivable by the user.
I thought that it would be interesting to replicate this behavior in a function. This function demonstrates how to get the current volume setting using waveOutGetVolume() and setting the volume using waveOutSetVolume(). The function below, IncrementVolume() takes a delta value which should be a value between –NUM_VOLUME_DIVISIONS and NUM_VOLUME_DIVISIONS, but if it is out of range, that will be handled. So this function can handle negative delta values, which decreases the volume.
#define NUM_VOLUME_DIVISIONS 10
#define MAX_VOLUME 0x0ffff // for 16 bit volume control, single channel
void IncrementVolume( int Delta )
{
UINT ReturnValue;
LONG newVolume;
// Get current volume from the default device
waveOutGetVolume(0, &newVolume);
// This function assumes that both right and left volume are equal
// or at least they will be when we are done, so wack off just
// the least significant bits of the volume
newVolume &= MAX_VOLUME;
// Adjust the volume
newVolume += (AdjustValue * (MAX_VOLUME/NUM_VOLUME_DIVISIONS));
// Bounds check
if( newVolume > MAX_VOLUME )
newVolume = MAX_VOLUME;
if( newVolume < 0 && -newVolume > (LONG)volume )
newVolume = 0;
volume = (ULONG)newVolume ;
// Now update the volume both left and rigth set to the same value
newVolume = MAKELONG(LOWORD((ULONG)newVolume), LOWORD((ULONG)newVolume));
if ((ReturnValue = waveOutSetVolume(0, newVolume)))
RETAILMSG(1,(L"IncrementVolume: Failed to set volume. Error code (%d)\n", ReturnValue));
}
The control panel applet plays a sound after changing the volume. That is a nice feature because the user gets an audible response that lets him hear the change that they made. I will leave playing a sound for another day though.
Copyright © 2008 – Bruce Eitman
All Rights Reserved