Skip to main content
Email Password   helpLost your password?

Introduction

This article is a brief explanation of how to use the mixerSetControlDetails API for setting the master volume of the speakers. Sometimes it is not enough to merely set the volume of the waveform-audio (or MIDI) output device.

Setting the volume

MMRESULT result;
result = waveOutSetVolume(0, 0x48444844);

If only it was that easy! Sometimes, the easiest-sounding task does not always equate to the most straight-forward code. While this does alter the speaker's volume, it does so in the context of the waveform-audio output device. What we want is the ability to set the master volume regardless of the output device.

The first thing we need to do is obtain a handle to the mixer device.

MMRESULT result;
HMIXER hMixer;
result = mixerOpen(&hMixer, MIXER_OBJECTF_MIXER, 0, 0, 0);

Next, we need to get the speaker line of the mixer device.

MIXERLINE ml = {0};
ml.cbStruct = sizeof(MIXERLINE);
ml.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
result = mixerGetLineInfo((HMIXEROBJ) hMixer, 
         &ml, MIXER_GETLINEINFOF_COMPONENTTYPE);

Next, we need to get the volume control of the speaker line.

MIXERLINECONTROLS mlc = {0};
MIXERCONTROL mc = {0};
mlc.cbStruct = sizeof(MIXERLINECONTROLS);
mlc.dwLineID = ml.dwLineID;
mlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mlc.cControls = 1;
mlc.pamxctrl = &mc;
mlc.cbmxctrl = sizeof(MIXERCONTROL);
result = mixerGetLineControls((HMIXEROBJ) hMixer, 
           &mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

We now have everything identified. All that's left is to actually set the volume level.

MIXERCONTROLDETAILS mcd = {0};
MIXERCONTROLDETAILS_UNSIGNED mcdu = {0};
mcdu.dwValue = 18500; // the volume is a number between 0 and 65535

mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
mcd.hwndOwner = 0;
mcd.dwControlID = mc.dwControlID;
mcd.paDetails = &mcdu;
mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mcd.cChannels = 1;
result = mixerSetControlDetails((HMIXEROBJ) hMixer, 
               &mcd, MIXER_SETCONTROLDETAILSF_VALUE);

That's it! If you want to mute the speakers, simply set the value to 0. If you want to test the elasticity of the speaker cones, set the value to 65535!

Obtaining the current speaker volume is just as easy, or at least it uses most of the same code! Simply call mixerGetControlDetails() instead and do not assign a value to the dwValue member of the MIXERCONTROLDETAILS_UNSIGNED structure. It will contain the current speaker volume upon successful return from mixerGetControlDetails().

Mute me, baby!

Using the above code, you can mute the master speakers by using a value of 0 for mcdu.dwValue. However, there is another method to do it correctly. The control type used is MIXERCONTROL_CONTROLTYPE_MUTE instead of MIXERCONTROL_CONTROLTYPE_VOLUME. The structure used will be MIXERCONTROLDETAILS_BOOLEAN instead of MIXERCONTROLDETAILS_UNSIGNED. The value of the fValue member is 1 to mute the speakers and 0 to unmute them. The code looks like:

mlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
result = mixerGetLineControls((HMIXEROBJ) hMixer, 
               &mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

MIXERCONTROLDETAILS_BOOLEAN mcb = {0};
mcb.fValue    = true; 
mcd.paDetails = &mcb;
mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
result = mixerSetControlDetails((HMIXEROBJ) hMixer, 
             &mcd, MIXER_SETCONTROLDETAILSF_VALUE);

Notes

The code was not tested for all possible audio combinations, but is merely showing how to use a particular API. My development machine has only the default sound card, and some $19 speakers! Not exactly an audio mixer's dream!

There are a few other CP articles that attack this problem differently, or that provide different levels of detail. I was not aware of them when I wrote this article, and simply searched CP for mixerSetControlDetails only to find no matches.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDoes this work on Vista ? Pin
Jeffrey Buxton
8:22 1 Apr '09  
GeneralRe: Does this work on Vista ? Pin
DavidCrow
8:24 1 Apr '09  
GeneralRe: Does this work on Vista ? Pin
Jeffrey Buxton
14:53 1 Apr '09  
GeneralRe: Does this work on Vista ? Pin
DavidCrow
3:54 2 Apr '09  
Questionmixeropen fails in DLL Pin
Sreejithg1984
0:47 15 Oct '08  
QuestionRe: mixeropen fails in DLL Pin
DavidCrow
4:10 15 Oct '08  
GeneralThanks! Pin
deoren
9:15 13 Sep '08  
GeneralMute affects other applications Pin
kundan kumar das
0:37 16 Oct '07  
Questioni want to play two sound file same time Pin
rajneshmalik
3:36 30 Aug '07  
Questionhow can i balance volume of left and rigt head speaker by your application Pin
rajneshmalik
20:50 5 Aug '07  
QuestionRe: how can i balance volume of left and rigt head speaker by your application Pin
DavidCrow
3:55 6 Aug '07  
AnswerRe: how can i balance volume of left and rigt head speaker by your application [modified] Pin
rajneshmalik
5:17 6 Aug '07  
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
DavidCrow
5:30 6 Aug '07  
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
rajneshmalik
4:28 7 Aug '07  
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
DavidCrow
4:35 7 Aug '07  
Generalhow can i control the sound of left and right speaker Pin
rajneshmalik
22:40 1 Aug '07  
QuestionRe: how can i control the sound of left and right speaker Pin
DavidCrow
3:43 2 Aug '07  
AnswerRe: how can i control the sound of left and right speaker Pin
rajneshmalik
21:33 2 Aug '07  
QuestionRe: how can i control the sound of left and right speaker Pin
DavidCrow
3:47 3 Aug '07  
AnswerRe: how can i control the sound of left and right speaker [modified] Pin
rajneshmalik
20:49 3 Aug '07  
GeneralAudio in vista Pin
Bayni
21:24 12 Mar '07  
GeneralThank you Pin
Apollo8
11:08 29 Jan '07  
Generalmlc.pamxctrl = &mc Pin
sraigiuz
5:20 26 Jan '07  
QuestionRe: mlc.pamxctrl = &mc Pin
DavidCrow
5:42 26 Jan '07  
GeneralSmall Error in the Code Pin
dcymbala
7:46 13 Sep '06  


Last Updated 19 Sep 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009