Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / MFC
Article

mixerSetControlDetails

Rate me:
Please Sign up or sign in to vote.
4.83/5 (22 votes)
19 Sep 20052 min read 227.5K   5K   54   47
How to set the master volume of the speakers

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

Comments and Discussions

 
GeneralMy vote of 5 Pin
ZoinZoin10-Jun-16 8:42
ZoinZoin10-Jun-16 8:42 
QuestionAn well written article written for programmers. Pin
Vaclav_10-Dec-11 5:19
Vaclav_10-Dec-11 5:19 
QuestionDoes this work on Vista ? Pin
Jeffrey Buxton1-Apr-09 7:22
Jeffrey Buxton1-Apr-09 7:22 
AnswerRe: Does this work on Vista ? Pin
David Crow1-Apr-09 7:24
David Crow1-Apr-09 7:24 
GeneralRe: Does this work on Vista ? Pin
Jeffrey Buxton1-Apr-09 13:53
Jeffrey Buxton1-Apr-09 13:53 
GeneralRe: Does this work on Vista ? Pin
David Crow2-Apr-09 2:54
David Crow2-Apr-09 2:54 
Questionmixeropen fails in DLL Pin
Sreejithg198414-Oct-08 23:47
Sreejithg198414-Oct-08 23:47 
Hi,

The code you have added works fine in a dialog application, however when I used the same in a DLL (MFC Shared) the mixerOpen API returned MMSYSERR_BADDEVICEID [It returned MMSYSERR_NOERROR in the dialog application though]. What could be the problem?

Thanks in advance

Sreejith
QuestionRe: mixeropen fails in DLL Pin
David Crow15-Oct-08 3:10
David Crow15-Oct-08 3:10 
GeneralThanks! Pin
deoren13-Sep-08 8:15
deoren13-Sep-08 8:15 
GeneralMute affects other applications Pin
kundan kumar das15-Oct-07 23:37
kundan kumar das15-Oct-07 23:37 
Questioni want to play two sound file same time Pin
rajneshmalik30-Aug-07 2:36
rajneshmalik30-Aug-07 2:36 
Questionhow can i balance volume of left and rigt head speaker by your application Pin
rajneshmalik5-Aug-07 19:50
rajneshmalik5-Aug-07 19:50 
QuestionRe: how can i balance volume of left and rigt head speaker by your application Pin
David Crow6-Aug-07 2:55
David Crow6-Aug-07 2:55 
AnswerRe: how can i balance volume of left and rigt head speaker by your application [modified] Pin
rajneshmalik6-Aug-07 4:17
rajneshmalik6-Aug-07 4:17 
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
David Crow6-Aug-07 4:30
David Crow6-Aug-07 4:30 
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
rajneshmalik7-Aug-07 3:28
rajneshmalik7-Aug-07 3:28 
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pin
David Crow7-Aug-07 3:35
David Crow7-Aug-07 3:35 
Questionhow can i control the sound of left and right speaker Pin
rajneshmalik1-Aug-07 21:40
rajneshmalik1-Aug-07 21:40 
AnswerRe: how can i control the sound of left and right speaker Pin
David Crow2-Aug-07 2:43
David Crow2-Aug-07 2:43 
AnswerRe: how can i control the sound of left and right speaker Pin
rajneshmalik2-Aug-07 20:33
rajneshmalik2-Aug-07 20:33 
QuestionRe: how can i control the sound of left and right speaker Pin
David Crow3-Aug-07 2:47
David Crow3-Aug-07 2:47 
AnswerRe: how can i control the sound of left and right speaker [modified] Pin
rajneshmalik3-Aug-07 19:49
rajneshmalik3-Aug-07 19:49 
GeneralAudio in vista Pin
Bayni12-Mar-07 20:24
Bayni12-Mar-07 20:24 
GeneralThank you Pin
Apollo829-Jan-07 10:08
Apollo829-Jan-07 10:08 
Generalmlc.pamxctrl = &mc Pin
sraigiuz26-Jan-07 4:20
sraigiuz26-Jan-07 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.