Windows Mixer Control in C#

Introduction

In this article, I'll show you how to use a Windows Mixer from C#.

For some time, I tried to get information about how to program the mixer from C#. I didn't have too much luck, and the few examples found were in C++. So, I took the hard and fun way... doing it myself.

This library is part of my Audio library to control Wave Audio (Playback/Recording), Mixer, Playback/Recording compressed files using ACM, basic speech recognition, and some other stuff that I'll release in future articles.

AudioMixer Namespace

Hierarchical objects view

The hierarchical view shows how the classes are organized.

A main object Mixers contains two objects: Mixer Playback and Recording. Those will work with default devices, but can be changed by setting the Mixers.Playback.DeviceId or Mixers.Recording.DeviceId properties.

I made it as simple as possible, hiding all the flat Win32 API implementation from the developer and creating a hierarchical set of objects.

Every mixer is autonomous to each other. This means that you can have a Playback mixer set to one sound card and a Recording mixer to another one. Also, each one contains two array or MixerLines (Lines, UserLines).

The Lines object contains all lines inside the mixer; for example, all the lines that don't have controls associated with them or lines that are not source lines.

UserLines will contain all lines that the developer can interact with, having controls like volume, Mute, Fadder, Bass, and so on (basically, is the same as a Lines object but a filter was applied to it).

Every Line will contain a collection of controls, such as Mute, Volume, Bass, Fader, and the like.

If you are interested in knowing more about how Windows Mixer works, here is an excellent link with all the information about it: http://www.borg.com/~jglatt/tech/mixer.htm.

Here Is Some Code

To get the audio devices in the computer:

foreach(MixerDetail mixerDetail in mMixers.Devices)
{
   ...
   ...
}

To get the input devices:

foreach(MixerDetail mixerDetail in mMixers.Recording.Devices)
{
   ...
   ...
}

To change the output mixer device:

Mixers mixers = new Mixers();
foreach(MixerDetail mixerDetail in mixers.Playback.Devices)
{
   if (mixerDetail.MixerName == "Sound Blaster Live")
       mixers.Playback.DeviceId = mixerDetail.DeviceId;
}

To change the output mixer device to the default device:

Mixers mixers = new Mixers();
mixers.Playback.DeviceId = -1;

or

mixers.Playback.DeviceId = mixers.Playback.DeviceIdDefault;

Getting the Playback Speaker Master volume:

mixers.Playback.Lines.GetMixerFirstLineByComponentType(
   MIXERLINE_COMPONENTTYPE.DST_SPEAKERS).Volume;

Setting the Playback Speaker Master volume for the left channel only:

MixerLine line = mixers.Playback.Lines.
   GetMixerFirstLineByComponentType(
   MIXERLINE_COMPONENTTYPE.DST_SPEAKERS);
line.Channel = Channel.Left;
line.Volume  = 32000;

Selecting the Microphone as the default input:

mixers.Recording.Lines.GetMixerFirstLineByComponentType(
   MIXERLINE_COMPONENTTYPE.SRC_MICROPHONE).Selected = true;

Getting callback notifications when a line has changed:

/* Initialization */
mMixers = new Mixers();
mMixers.Playback.MixerLineChanged +=
   new WaveLib.AudioMixer.Mixer.
   MixerLineChangeHandler(mMixer_MixerLineChanged);
mMixers.Recording.MixerLineChanged +=
   new WaveLib.AudioMixer.Mixer.
   MixerLineChangeHandler(mMixer_MixerLineChanged);
/* Events */
private void mMixer_MixerLineChanged(Mixer mixer, MixerLine line)
{
   Console.WriteLine("Mixer: " + mixer.DeviceDetail.MixerName);
   Console.WriteLine("Mixer Type: " + mixer.MixerType);
   Console.WriteLine("Mixer Line: " + line.Name);
}

Getting and Setting Rare Controls

Specific controls like Fadder, Microphone Boost, bass, treble, and so forth can be accessed via the MixerControl object using ValueAsSigned, ValueAsUnsigned, and ValueAsBoolean properties, but they are not implemented as standard properties because they don't belong to all controls.

Notes

All the current functionality is tested and working. I tried to not include bugs, but they are there and I find them every once in a while. If you find bugs, I'll be grateful to get the feedback to update the article.

For now, I don't need anything else from the library, but if you think of something not included in it that could could make it better, just let me know and I'll try to include it.

If you have a problem with it, just feel free to write me an e-mail.



About the Author

Gustavo Franco

started with programming about 19 years ago as a teenager, from my old Commodore moving to PC/Server environment Windows/UNIX SQLServer/Oracle doing gwBasic, QBasic, Turbo Pascal, Assembler, Turbo C, BC, Clipper, Fox, SQL, C/C++, Pro*C, VB3/5/6, Java, and today loving C#. Currently working on VOIP/SIP technology. Passion for most programming languages and my son Aidan.

Downloads

Comments

  • Thanks a lot!

    Posted by NinjaSquirrel on 06/07/2013 05:44am

    You sir, are a hero, thank you very very much for this! I have been searching for a windows mixer control library for ages now and somehow I have always overlooked yours. My old WinXP machine has a TV card installed which I still use frequently but cannot get to work on Win7. Its audio signal is passed through my audio card's line-in channel. The audio card's driver in turn is not (and never was) perfect so my TV card's audio output only came through undistorted after muting Line-In and unmuting it again after every system reboot. I always wanted to automate this ... and now the penny finally dropped. Thanks again for your work (and giving it away for free), my soul is at peace now. ;)

    Reply
  • Great

    Posted by CLS on 01/03/2013 03:27pm

    Thank you very mach for this product. I found it firmly useful. The only question I have is that, why Microsoft did not implement this in the DotNet framework, and mainly why are the internal dll calls so deeply complicated?

    Reply
  • Download failure

    Posted by Lorenzo on 04/28/2012 07:09am

    Hi, download failure!

    Reply
  • giving thanks

    Posted by ipey22 on 09/12/2011 04:23am

    For me as a new C# programmer that was a great help. By the way I'm working on a simple VOIP software over LAN but I can not multicast a stream of audio. Could you please help me. thank you very much in advance.

    Reply
  • Reference from other article

    Posted by fmonroy on 09/25/2006 10:30pm

    Thank you CastorTiu, I used this information to write a windows service that saves volume control levels from users when they logon/logoff, this is at http://www.codeguru.com/csharp/csharp/cs_graphics/sound/article.php/c12643/

    • Reference from other article

      Posted by CastorTiu on 09/25/2006 11:38pm

      I'm glad it was useful. Regards.

      Reply
    Reply
  • Windows x64 .net 2.0 volume control not working

    Posted by milesj on 04/12/2006 06:04pm

    would like to get my volume control working on x64. Anyone have any headers that will work with x64? Can't get explicit to work, would even like sequential layout to work as well.

    Reply
  • Excellent

    Posted by TheFrog on 12/05/2005 11:50am

    This is the code, which I've searched for a long time. I know, that it must be a very hard work. Thanks so much.

    • Re: Excellent

      Posted by CastorTiu on 12/05/2005 12:51pm

      Your welcome, I'm glad it is useful for you. After you get the understanding of Mixer API is not so hard to do it, it is just a little confusing at the beginning. The Mixer is very good explained here http://www.borg.com/~jglatt/tech/mixer.htm Regards, Gustavo

      Reply
    Reply
  • IT Developer

    Posted by Bo3po on 11/25/2005 02:41am

    This is extremmly nice code. From a designers point of view this is 100% the way to do it I would say. A rather complex structure but with the UML class diagram there was no trouble understanding the code. The thing that impressed me is the way everyting is well coded. Nice. The GUI generation can be tricky to figure out but it's not that hard. I had no problems with it at all ... Keep it up :)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds