Click here to Skip to main content
15,879,613 members
Articles / Desktop Programming / MFC
Article

A simple record and playback volume control class

Rate me:
Please Sign up or sign in to vote.
3.64/5 (11 votes)
27 Oct 20012 min read 292.3K   3.7K   48   62
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


Written By
Architect
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Playback Pin
Comawhite11-Oct-04 14:03
Comawhite11-Oct-04 14:03 
GeneralRe: Playback Pin
fritzpas24-Aug-05 10:21
fritzpas24-Aug-05 10:21 
GeneralA poorly documented article Pin
Atif Mushtaq16-Jun-04 20:09
Atif Mushtaq16-Jun-04 20:09 
GeneralRe: A poorly documented article Pin
Ravi Bhavnani16-Jun-04 20:52
professionalRavi Bhavnani16-Jun-04 20:52 
GeneralRe: A poorly documented article Pin
coldembrace30-Jun-04 20:42
coldembrace30-Jun-04 20:42 
GeneralRe: A poorly documented article Pin
coldembrace30-Jun-04 20:43
coldembrace30-Jun-04 20:43 
GeneralRe: A poorly documented article Pin
Ravi Bhavnani1-Jul-04 3:22
professionalRavi Bhavnani1-Jul-04 3:22 
GeneralI cannot control mic "play" volume on XP Pin
Anonymous13-Apr-04 11:38
Anonymous13-Apr-04 11:38 
GeneralRe: I cannot control mic "play" volume on XP Pin
Anonymous15-Apr-04 16:49
Anonymous15-Apr-04 16:49 
GeneralRe: I cannot control mic "play" volume on XP Pin
Anonymous2-Mar-05 17:50
Anonymous2-Mar-05 17:50 
GeneralRecording from Soundcard Pin
Anonymous5-Nov-03 17:59
Anonymous5-Nov-03 17:59 
GeneralRe: Recording from Soundcard Pin
Whoo15-Apr-04 17:07
Whoo15-Apr-04 17:07 
Questionwhat about paning? (n.t.) Pin
DonGuitar30-Sep-03 14:20
DonGuitar30-Sep-03 14:20 
Generalgetting slider value Pin
tmf690228-Aug-03 8:12
tmf690228-Aug-03 8:12 
GeneralRe: getting slider value Pin
Whoo28-Aug-03 19:35
Whoo28-Aug-03 19:35 
GeneralRe: getting slider value Pin
tmf690229-Aug-03 8:25
tmf690229-Aug-03 8:25 
GeneralRe: getting slider value Pin
Whoo29-Aug-03 18:11
Whoo29-Aug-03 18:11 
GeneralRe: getting slider value Pin
SohailB15-Jun-05 21:14
SohailB15-Jun-05 21:14 
GeneralRecord Sound Volume Control Pin
Member 4657467-Jul-03 6:45
Member 4657467-Jul-03 6:45 
GeneralRe: Record Sound Volume Control Pin
Whoo8-Jul-03 1:15
Whoo8-Jul-03 1:15 
GeneralRe: Record Sound Volume Control Pin
Member 4657469-Jul-03 9:35
Member 4657469-Jul-03 9:35 
GeneralRe: Record Sound Volume Control Pin
Whoo15-Apr-04 16:58
Whoo15-Apr-04 16:58 
GeneralMixer.obj : error LNK2001: unresolved external symbol __imp__mixerGetLineControlsA@12 Pin
tvhead802-Apr-03 2:56
tvhead802-Apr-03 2:56 
GeneralRe: Add Winmm.lib.:laugh: Pin
tvhead802-Apr-03 3:01
tvhead802-Apr-03 3:01 
GeneralRe:How to Add Winmm.lib? Pin
severeight1-Apr-04 17:16
severeight1-Apr-04 17:16 

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.