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

Windows Audio Mixer API Wrapper Library

Rate me:
Please Sign up or sign in to vote.
4.11/5 (12 votes)
20 Oct 2008CPOL5 min read 121.7K   5K   27   28
A small wrapper library to programmatically interact with standard audio controls

Introduction

Adding a volume control to an application seems like it should be an easy thing to do, enter the Windows Multimedia Audio API that enables a programmer to add audio services to an application. The audio mixer functions of this API are used to set an audio device's volume but there is a steep learning curve penalty waiting for the first time user. Although Vista provides a new and somewhat easier to use set of user-mode audio components, Microsoft has stated that it does not plan to make the Vista core audio APIs available for use with earlier versions of Windows such as XP. Since there is a large population of users still using XP and older Windows operating systems, a solution is needed that applies to all of these older systems as well.  

Background

To address this problem, I created an API in the form of a small wrapper library that provides a straightforward way to programmatically interact with standard audio controls such as volume, balance, and mute.  Using this API still requires some understanding of the terminology and basic concepts used in the mixer documentation.

Mixer: A Mixer combines (mixes), routes, and changes the levels and other characteristics of audio signal data. A system can have zero to several mixers.

Destination: Each mixer has one or more destinations, and each destination has zero to several controls and one or more connections.

Connection: A connection supplies audio data to a destination and has zero to several controls. There are also one or more channels per connection, if a connection has two channels, the volume of each channel may be controlled independently (balance).

Control: A control effects a change in the characteristics of audio signal data such as volume. Each control associated with a mixer has a system assigned unique ID, 0...N.  A mixer with one destination that has two connections will have nine control IDs assigned to it numbered zero to eight assuming each connection and destination has three controls each. If there are three mixers, there will be three sets of control ID numbers, each set starting at zero.

The concept of a destination is somewhat confusing, but I find it useful to think of a destination as a receiver or consumer of data coming from the connections associated with it. The confusing part of this is that a destination may be an output device, such as a speaker, or an input device, such as a wave input device. A speaker is clearly an output device but a wave input device is clearly not an output device. The introductory part of the SDK documentation uses the term "audio line" to refer to both destinations and connections but the usage of this term is not consistent throughout the remainder of the documentation so I have avoided using it here. 

The wrapper library mixer class, CMixer, enumerates all destinations, connections, and controls associated with a mixer. It provides access to control labels and bounds data and read and write access to state data for each control. The following chart provides a clearer view of the relationships between mixer class components:

Mixer                   A Mixer has a collection of Destinations
   Destination        A Destination has a collection of Connections and Controls
      Connection     A Connection has a collection of Controls
         Control       A Control has a collection of channels, typically two

The CMixer class draws on the support of two other classes, CMixerDestination and CMixerControl. The CMixerDestination class manages data associated with a destination while the CMixerControl class provides access to the state and metrics data of a control.

Using the Code 

To fully use the CMixer class, you will need to familiarize yourself with three SDK defined structures used by the class to provide information about mixer components:

  • The MIXERCAPS structure describes the capabilities of a mixer device.
  • The MIXERLINE structure describes the state and metrics of a connection or destination.
  • The MIXERCONTROL structure describes the state and metrics of a single control for a connection or destination.  

If you are interested in associating a Windows control with a mixer control, you will only need to refer to the MIXERCONTROL structure definition in the SDK documentation as this structure contains the ID number, type, name, and bounds data for a control. The included VolumeControl sample application is a simple MFC dialog application with a single volume control that is synchronized with the first volume control of the first mixer found in the system. In the case where a specific audio device, such as a wave in/out, MIDI in/out, or auxiliary device has already been opened, the CMixer instance may be initialized using the device’s ID index value or opened handle.

The AudioDialog sample MFC dialog application has been included to address the situation where more extensive mixer information is required. It enumerates all mixers in the system and allows the user to display data associated with any of the mixers, destinations, connections, and controls found. It also presents a collection of volume, balance, and mute check box control groups based on the user selected mixer and destination similar to the system provided Volume Control utility.

Build Notes

Both of the sample applications were built with Visual Studio 2005 as I felt that most people would at least have this version and that updating a project to a newer version of VS is much simpler than converting one to a previous version. Both applications were tested with Vista, XP, and 2K. The AudioDialog application should be built first as it creates the mixer API library file used by the VolumeControl application.

History

  • 10.20.08 Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 102659309-Oct-13 18:41
Member 102659309-Oct-13 18:41 
GeneralDoesn't seem to cleanly compile under MS VS 2010 Pin
dc_200011-Jul-10 16:17
dc_200011-Jul-10 16:17 
QuestionIs there any way to control application specific volume ? Pin
fMark4-Mar-10 13:02
fMark4-Mar-10 13:02 
GeneralDo not work on Win7 x64! Pin
Member 11447023-Jan-10 22:11
Member 11447023-Jan-10 22:11 
GeneralRe: Do not work on Win7 x64! Pin
sglorz10-Feb-10 23:52
sglorz10-Feb-10 23:52 
GeneralRe: Do not work on Win7 x64! Pin
John Plsek13-Dec-10 23:12
John Plsek13-Dec-10 23:12 
GeneralShould set NULL pointer after deleting allocated memory in CMixerControl::CleanupHeap() function Pin
NSHAN25-Nov-09 14:08
NSHAN25-Nov-09 14:08 
QuestionCan not work on Vista? Pin
AchillesTangYan1-Sep-09 20:42
AchillesTangYan1-Sep-09 20:42 
QuestionLooks Good!! Pin
ezcodez26-Jul-09 19:15
ezcodez26-Jul-09 19:15 
GeneralMy vote of 2 Pin
Alexandre GRANVAUD30-Jun-09 21:50
Alexandre GRANVAUD30-Jun-09 21:50 
Questionhow to list record dev and playback dev Pin
tangerwendy15-Jan-09 18:48
tangerwendy15-Jan-09 18:48 
GeneralZip-File is empty Pin
QuantixVoom4-Dec-08 1:28
QuantixVoom4-Dec-08 1:28 
AnswerRe: Zip-File is empty Pin
Member 34690368-Dec-08 8:31
Member 34690368-Dec-08 8:31 
GeneralRe: Zip-File is empty Pin
QuantixVoom18-Dec-08 9:52
QuantixVoom18-Dec-08 9:52 
GeneralDebug error.... Pin
Jeffrey_YH3-Dec-08 21:58
Jeffrey_YH3-Dec-08 21:58 
AnswerRe: Debug error.... Pin
Member 34690368-Dec-08 8:25
Member 34690368-Dec-08 8:25 
GeneralRe: Debug error.... Pin
Jeffrey_YH8-Dec-08 16:53
Jeffrey_YH8-Dec-08 16:53 
AnswerRe: Debug error.... Pin
Member 346903610-Dec-08 14:55
Member 346903610-Dec-08 14:55 
GeneralRe: Debug error.... Pin
Jeffrey_YH10-Dec-08 21:37
Jeffrey_YH10-Dec-08 21:37 
AnswerRe: Debug error.... Pin
Member 346903613-Dec-08 9:38
Member 346903613-Dec-08 9:38 
GeneralRe: Debug error.... Pin
Jeffrey_YH14-Dec-08 16:08
Jeffrey_YH14-Dec-08 16:08 
AnswerRe: Debug error.... Pin
Member 346903617-Dec-08 10:10
Member 346903617-Dec-08 10:10 
GeneralRe: Debug error.... Pin
Jeffrey_YH17-Dec-08 16:35
Jeffrey_YH17-Dec-08 16:35 
AnswerRe: Debug error.... Pin
Member 346903618-Dec-08 7:50
Member 346903618-Dec-08 7:50 
GeneralRe: Debug error.... Pin
Jeffrey_YH18-Dec-08 21:22
Jeffrey_YH18-Dec-08 21:22 

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.