Click here to Skip to main content
Click here to Skip to main content
Go to top

mixerSetControlDetails

, 19 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

DavidCrow
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

 
QuestionAn well written article written for programmers. PinmemberVaclav_Sal10-Dec-11 5:19 
QuestionDoes this work on Vista ? PinmemberJeffrey Buxton1-Apr-09 7:22 
AnswerRe: Does this work on Vista ? PinmvpDavidCrow1-Apr-09 7:24 
GeneralRe: Does this work on Vista ? PinmemberJeffrey Buxton1-Apr-09 13:53 
GeneralRe: Does this work on Vista ? PinmvpDavidCrow2-Apr-09 2:54 
Questionmixeropen fails in DLL PinmemberSreejithg198414-Oct-08 23:47 
QuestionRe: mixeropen fails in DLL PinmvpDavidCrow15-Oct-08 3:10 
GeneralThanks! Pinmemberdeoren13-Sep-08 8:15 
GeneralMute affects other applications Pinmemberkundan kumar das15-Oct-07 23:37 
Questioni want to play two sound file same time Pinmemberrajneshmalik30-Aug-07 2:36 
Questionhow can i balance volume of left and rigt head speaker by your application Pinmemberrajneshmalik5-Aug-07 19:50 
QuestionRe: how can i balance volume of left and rigt head speaker by your application PinmvpDavidCrow6-Aug-07 2:55 
AnswerRe: how can i balance volume of left and rigt head speaker by your application [modified] Pinmemberrajneshmalik6-Aug-07 4:17 
GeneralRe: how can i balance volume of left and rigt head speaker by your application PinmvpDavidCrow6-Aug-07 4:30 
GeneralRe: how can i balance volume of left and rigt head speaker by your application Pinmemberrajneshmalik7-Aug-07 3:28 
GeneralRe: how can i balance volume of left and rigt head speaker by your application PinmvpDavidCrow7-Aug-07 3:35 
Questionhow can i control the sound of left and right speaker Pinmemberrajneshmalik1-Aug-07 21:40 
AnswerRe: how can i control the sound of left and right speaker PinmvpDavidCrow2-Aug-07 2:43 
AnswerRe: how can i control the sound of left and right speaker Pinmemberrajneshmalik2-Aug-07 20:33 
QuestionRe: how can i control the sound of left and right speaker PinmvpDavidCrow3-Aug-07 2:47 
AnswerRe: how can i control the sound of left and right speaker [modified] Pinmemberrajneshmalik3-Aug-07 19:49 
GeneralAudio in vista PinmemberBayni12-Mar-07 20:24 
GeneralThank you PinmemberApollo829-Jan-07 10:08 
Generalmlc.pamxctrl = &mc Pinmembersraigiuz26-Jan-07 4:20 
QuestionRe: mlc.pamxctrl = &mc PinmvpDavidCrow26-Jan-07 4:42 
GeneralSmall Error in the Code Pinmemberdcymbala13-Sep-06 6:46 
GeneralRe: Small Error in the Code Pinmemberquin281014-Nov-06 6:18 
Questionhow to select a recording device? Pinmemberhaitham hamed housin26-May-06 7:59 
AnswerRe: how to select a recording device? PinmemberDavidCrow26-May-06 8:27 
JokeRe: how to select a recording device? Pinmemberhaitham hamed housin26-May-06 9:27 
QuestionHow do I mute all output... PinmemberDevil Panther3-Mar-06 10:45 
QuestionHow to set Microphone Volume to spkr PinmemberBobboots27-Oct-04 17:00 
AnswerRe: How to set Microphone Volume to spkr PinmemberDavidCrow28-Oct-04 5:03 
GeneralRe: How to set Microphone Volume to spkr PinmemberBobboots28-Oct-04 5:08 
QuestionHow to change LineIn Volume? PinmemberP_JAYAPRAKASH27-Oct-04 16:52 
AnswerRe: How to change LineIn Volume? PinmemberDivineAnarchy14-Jul-10 10:33 
Generalsound just disappears... :( Pinmemberscoroop23-May-04 4:47 
GeneralRe: sound just disappears... :( PinmemberDavidCrow24-May-04 8:28 
GeneralRe: sound just disappears... :( Pinmemberscoroop25-May-04 6:53 
GeneralRe: sound just disappears... :( PinmemberDavidCrow25-May-04 10:22 
GeneralRe: sound just disappears... :( Pinmemberscoroop25-May-04 10:49 
GeneralRe: sound just disappears... :( PinmemberDavidCrow25-May-04 10:54 
GeneralRe: sound just disappears... :( Pinmemberscoroop25-May-04 11:32 
Generalerror in MMsystem.h Pinmemberdietercools11-Feb-04 10:18 
GeneralRe: error in MMsystem.h PinmemberDavidCrow11-Feb-04 10:23 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 19 Sep 2005
Article Copyright 2003 by DavidCrow
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid