Click here to Skip to main content
Licence 
First Posted 27 Oct 2001
Views 204,221
Bookmarked 46 times

A simple record and playback volume control class

By | 27 Oct 2001 | Article
A class to make setting the record and playback volume easier

Introduction

Sometimes we need to control the volume of our soundcard and Microsoft has provided an API waveOutSetVolume() to do this. Unfortunately this can only set the playback volume. When we want to set the volume of other lines such as mic or MIDI or the line-in for recording, there is NO API to help!

At first I thought that DirectMedia may provide an interface to do this, and it does, but after I coded it and tested, the result is not so good. The interface IAMAudioInputMixer can only handle the record, not the playback, and most of all:

"The name of each pin, such as "Line in" or "Microphone", reflects the type of input"

but the name is not the same on different machines!

eg:
I want to set the volume of the microphone, so I find the pin by name "Microphone" and control it, it works on my computer. When I tested it at another machine, it failed because the name of the pin is "Mic Volume"!

I tried many ways and at last I worked out how to do it using MIXER.

There may be some people that have had a headache over the same quetion, so I released my solution. I hope it helps you.

Usage

  1. include the Mixer.h and Mixer.cpp into your project;
  2. add code like this:
      CMixer mixer(MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, CMixer::Record);
    
      ......
      mixer.GetVolume();
      mixer.SetVolume(...);
    

Remarks

The constructor takes two parameters, the first is ComponentType from MIXERLINE (see MSDN for all possible values), and the second is an enum type, which can be Record or Play. The above controls the record (waveIn) volume, you can also control the playback (waveout) volume using:

CMixer mixer(MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, CMixer::Play);

Notice that there is no default constructor takes no parameters, so if you use it in a C++ class, you should code it like this:

// MyClass.h
//////////////////////////
class CMixer;
class CMyClass
{
  CMyClass();
private:
  CMixer m_mixer;
....
}

//MyClass.cpp
///////////////////////////
CMyClass::CMyClass():m_mixer(MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, CMixer::Play)
{
}
....

if you have any suggestion or improvement, plz let me know: whoo@isWhoo.com

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

About the Author

Whoo

Architect

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIs error? Pinmemberyangzmpang@20:54 22 Aug '10  
GeneralCMixer constructor error Pinmemberanandveerappan23:24 4 Mar '10  
GeneralAnother Code Project article on this topic Pinmemberdaluu9:06 5 Sep '07  
GeneralPlease, I need help Pinmemberwanhaven4:00 27 Feb '07  
GeneralConversion error Pinmembersraigiuz22:44 25 Jan '07  
GeneralThe archive file is not avaible ... Pinmemberjere44trial@yahoo.fr5:28 29 Nov '06  
QuestionHow to choose FrontPanel MIC or BackPanel MIC? Pinmemberguzeliang21:48 9 Oct '06  
AnswerRe: How to choose FrontPanel MIC or BackPanel MIC? Pinmemberknightriderjon23:21 30 May '07  
GeneralRecord Volume Control on Vista OS Pinmembervarungarg19:01 25 Jul '06  
QuestionAdjusting Record Volume Pinmember~Jabeen~20:46 22 Jun '06  
AnswerRe: Adjusting Record Volume [modified] PinmemberMartin Skibye21:14 25 Mar '07  
GeneralMixer.obj : error LNK2001 PinmemberLowFly3:21 23 Nov '05  
GeneralIt was a valiant effort, but... PinmemberProdelos5:01 15 Jun '05  
GeneralRe: It was a valiant effort, but... Pinmemberkoehler2:28 10 Feb '06  
GeneralCMixer class to controll multiple cards in one system Pinmembersujudavid0:30 2 Mar '05  
GeneralRe: CMixer class to controll multiple cards in one system PinmemberProdelos3:35 15 Jun '05  
GeneralThank you very much Pinmemberniukaimin16:45 27 Jan '05  
GeneralComawhite is right. PinmemberWhoo1:52 17 Dec '04  
QuestionHow do you SELECT the Control Pinmemberckathrani10:17 16 Dec '04  
AnswerRe: How do you SELECT the Control Pinmemberdaluu8:50 5 Sep '07  
AnswerRe: How do you SELECT the Control PinmemberMartin Skibye20:37 25 Feb '08  
One way of selecting a component in "Recording Control" is this:
 
/// CODE START ///
 
void setInputSel(long compType)
{
     HMIXER                                       hMixer;
     MMRESULT                                    result;
     MIXERLINE                                 ml               = {0};
     MIXERCONTROL                              mc               = {0};
     MIXERLINECONTROLS                     mlc               = {0};
     MIXERCONTROLDETAILS                  mcd               = {0};
 
     DWORD dwi;
     DWORD m_dwControlType, m_dwSelectControlID, m_dwMultipleItems;
 
     result = mixerOpen(&hMixer, MIXER_OBJECTF_MIXER, 0, 0, 0);
     if (result == MMSYSERR_NOERROR)
     {
          // get dwLineID
          ml.cbStruct = sizeof(MIXERLINE);
          ml.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;     //"Recording Control"
 
          result = mixerGetLineInfo((HMIXEROBJ) hMixer, &ml, MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE);
          if (result == MMSYSERR_NOERROR)
          {
               // try MUX
               m_dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
               mlc.cbStruct = sizeof(MIXERLINECONTROLS);
               mlc.dwLineID = ml.dwLineID;
               mlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
               mlc.cControls = 1;
               mlc.cbmxctrl = sizeof(MIXERCONTROL);
               mlc.pamxctrl = &mc;
 
               result = mixerGetLineControls((HMIXEROBJ) hMixer, &mlc, MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE);
               if (result != MMSYSERR_NOERROR)
               {
                    return;
               }
 
               // store dwControlID, cMultipleItems
               m_dwSelectControlID = mc.dwControlID;
               m_dwMultipleItems = mc.cMultipleItems;
 
               // get the index of the component type Select control
               MIXERCONTROLDETAILS_LISTTEXT *pmcdSelectText = new MIXERCONTROLDETAILS_LISTTEXT[m_dwMultipleItems];
 
               if (pmcdSelectText != NULL)
               {
                    MIXERCONTROLDETAILS mcd;
                    mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
                    mcd.dwControlID = m_dwSelectControlID;
                    mcd.cChannels = 1;
                    mcd.cMultipleItems = m_dwMultipleItems;
                    mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXT);
                    mcd.paDetails = pmcdSelectText;
 
                    result = mixerGetControlDetails((HMIXEROBJ) hMixer,     &mcd, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_LISTTEXT);
                    if (result == MMSYSERR_NOERROR)
                    {
                         // Run through the list of the recording components to find the array element (dwi) for the componentType
                         for (dwi = 0; dwi < m_dwMultipleItems; dwi++)
                         {
                              // get the line information
                              MIXERLINE ml;
                              ml.cbStruct = sizeof(MIXERLINE);
                              ml.dwLineID = pmcdSelectText[dwi].dwParam1;
 
                              result = mixerGetLineInfo((HMIXEROBJ) hMixer, &ml, MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_LINEID);
                              if (result == MMSYSERR_NOERROR && ml.dwComponentType == compType)
                              {
                                   // found, dwi is the index.
                                   break;
                              }
                         }
                    }
                    delete []pmcdSelectText;
               }
               // get all the values first
               MIXERCONTROLDETAILS_BOOLEAN *pmcdSelectValue = new MIXERCONTROLDETAILS_BOOLEAN[m_dwMultipleItems];
 
               if (pmcdSelectValue != NULL)
               {
                    MIXERCONTROLDETAILS mcd;
                    mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
                    mcd.dwControlID = m_dwSelectControlID;
                    mcd.cChannels = 1;
                    mcd.cMultipleItems = m_dwMultipleItems;
                    mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
                    mcd.paDetails = pmcdSelectValue;
 
                    result = mixerGetControlDetails((HMIXEROBJ) hMixer, &mcd, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_VALUE);
                    if (result == MMSYSERR_NOERROR)
                    {
                         // Check if m_dwControlType has a valid value:
                         assert(m_dwControlType == MIXERCONTROL_CONTROLTYPE_MUX);
 
                         // MUX restricts the line selection to one source line at a time.
                         ZeroMemory(pmcdSelectValue, m_dwMultipleItems * sizeof(MIXERCONTROLDETAILS_BOOLEAN));
 
                         // set the component type value
                         pmcdSelectValue[dwi].fValue = 1;
 
                         mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
                         mcd.dwControlID = m_dwSelectControlID;
                         mcd.cChannels = 1;
                         mcd.cMultipleItems = m_dwMultipleItems;
                         mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
                         mcd.paDetails = pmcdSelectValue;
 
                         result = mixerSetControlDetails((HMIXEROBJ) hMixer,     &mcd, MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE);
                         int test = 0;
                    }
                    delete []pmcdSelectValue;
               }
          }
          mixerClose(hMixer);
     }
}
 
/// CODE END ///
 
This function is to be called with the component to be selected (e.g.   MIXERLINE_COMPONENTTYPE_SRC_LINE ("Line In")).
 
Greetings,
Martin Skibye
 
Vivir con miedo es como vivir a medias!
Generalmicrophone record volume PinsussAnonymous17:41 15 Dec '04  
GeneralLink doesn't work Pinmemberramontxu2:08 6 Jul '04  
GeneralRe: Link doesn't work Pinmemberoyljerry2:07 8 Nov '04  
GeneralPlayback Pinmemberwhacker8:41 30 Jun '04  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 28 Oct 2001
Article Copyright 2001 by Whoo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid