Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / Visual Basic
Article

Audio Library Part I - (Windows Mixer Control)

Rate me:
Please Sign up or sign in to vote.
4.52/5 (79 votes)
1 Oct 2006CPOL3 min read 604.4K   15.4K   163   165
Library to control Windows Mixer from C#
Sample Image - AudioLib.jpg

Introduction

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

For some time, I was trying 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 be releasing in future articles.

AudioMixer Namespace

Hierarchical Objects View

Sample screenshot

The hierarchical view shows how the classes are organized.

The main object Mixers contains two Mixer objects, Playback and Recording; those will work with default devices, but can be changed by setting the property Mixers.Playback.DeviceId or Mixers.Recording.DeviceId.

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

Every mixer is autonomous, meaning you can have Playback mixer set to one soundcard and Recording mixer to another one. Also each one contains two arrays or MixerLines (Lines, UserLines).

Lines object will contain all lines inside the mixer, for example all the lines that don't have controls associated with it or lines that are not source lines.

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

Every Line will contain a collection of controls, like Mute, Volume, Bass, Fader, etc.

If you are interested in knowing more about how Windows Mixer works, here is an excellent link with all the information about it.

Let's See Some Code

To Get the Audio Devices in the Computer

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

To Get the Input Devices

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

Change Output Mixer Device

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

Change Output Mixer Device to the Default Device

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

or

C#
mixers.Playback.DeviceId = mixers.Playback.DeviceIdDefault;

Getting Playback Speaker Master Volume

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

Setting Playback Speaker Master Volume for the Left Channel Only

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

Selecting Microphone as Default Input

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

Getting Callback Notifications When a Line has Changed

C#
/* 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, etc. 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.

x86 vs x64

Finally I got a core 2 duo and now I could compile the library under x86 and x64. At first I thought there won't be too much difference, but the big problem is that the IntPtr pointers are platform specific, so whereas a x86 takes 4 bytes, a x64 takes 8 bytes, that makes things really messy and there is no simple fix in some cases, for example in the following struct I could not find a way to make it work for x86 and x64 in C#. Looks like the .NET Framework is missing something to work with unions inside structs... I had to declare two structs and work with them separately.

Before

C#
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]
public struct MIXERCONTROLDETAILS
{
 [FieldOffset(0)] public UInt32  cbStruct;
 [FieldOffset(4)] public UInt32  dwControlID;
 [FieldOffset(8)] public UInt32  cChannels;
 /* Union start */
 [FieldOffset(12)] public IntPtr hwndOwner;
 [FieldOffset(12)] public UInt32 cMultipleItems;
 /* Union end */
 [FieldOffset(16)] public UInt32 cbDetails;
 [FieldOffset(20)] public IntPtr paDetails;
}

After

C#
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2)]
public struct MIXERCONTROLDETAILS
{
 [FieldOffset(0)]  public UInt32 cbStruct;
 [FieldOffset(4)]  public UInt32 dwControlID;
 [FieldOffset(8)]  public UInt32 cChannels;
 /* Union start */
 [FieldOffset(12)] public IntPtr hwndOwner;
 [FieldOffset(12)] public UInt32 cMultipleItems;
 /* Union end */
 [FieldOffset(16)] public UInt32 cbDetails;
 [FieldOffset(20)] public IntPtr paDetails;
}
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2)]
public struct MIXERCONTROLDETAILS64
{
 [FieldOffset(0)] public UInt32  cbStruct;
 [FieldOffset(4)] public UInt32  dwControlID;
 [FieldOffset(8)] public UInt32  cChannels;
 /* Union start */
 [FieldOffset(12)] public IntPtr hwndOwner;
 [FieldOffset(12)] public UInt32 cMultipleItems;
 /* Union end */
 [FieldOffset(20)] public UInt32 cbDetails;
 [FieldOffset(24)] public IntPtr paDetails;
}

I was hoping to declare a dynamic size for the FieldOffset and then I could have fixed the problem but it doesn't compile on .NET.

C#
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2)]
public struct MIXERCONTROLDETAILS
{
 [FieldOffset(0)] public UInt32  cbStruct;
 [FieldOffset(4)] public UInt32  dwControlID;
 [FieldOffset(8)] public UInt32  cChannels;
 /* Union start */
 [FieldOffset(12)] public IntPtr hwndOwner;
 [FieldOffset(12)] public UInt32 cMultipleItems;
 /* Union end */
 [FieldOffset(12 + IntPtr.Size)] public UInt32 cbDetails;
 [FieldOffset(12 + IntPtr.Size + 4)] public IntPtr paDetails;
}

Notes

All the current functionality is tested and working, I tried not to 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 feedback to update the article.

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

If you have a problem with it, feel free to write me an email.

License

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


Written By
Software Developer Microsoft
United States United States
I 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, Summer87, Clipper, Fox, SQL, C/C++, Pro*C, VB3/5/6, Java, and today loving C#.

Currently working as SDE on Failover Clustering team for Microsoft.

Passion for most programming languages and my kids Aidan&Nadia.

Comments and Discussions

 
GeneralRe: Vista Pin
OmidH28-Apr-09 3:06
OmidH28-Apr-09 3:06 
GeneralRe: Vista Pin
michaelrterry19-Aug-08 10:40
michaelrterry19-Aug-08 10:40 
GeneralRe: Vista Pin
peas22-Sep-08 12:36
peas22-Sep-08 12:36 
Generalmany thanks Pin
jomojutha31-Jan-07 14:24
jomojutha31-Jan-07 14:24 
GeneralRe: many thanks Pin
HJPhilippi1-Feb-07 2:37
HJPhilippi1-Feb-07 2:37 
GeneralThanks for the library! Pin
JulianArevalo5-Jan-07 14:21
JulianArevalo5-Jan-07 14:21 
GeneralSelected exception Pin
JF Coulon20-Oct-06 4:19
JF Coulon20-Oct-06 4:19 
GeneralRe: Selected exception Pin
CastorTiu20-Oct-06 5:50
CastorTiu20-Oct-06 5:50 
QuestionLeft and Right Channel Volume [modified] Pin
DKermott3-Oct-06 9:11
DKermott3-Oct-06 9:11 
AnswerRe: Left and Right Channel Volume Pin
CastorTiu4-Oct-06 6:28
CastorTiu4-Oct-06 6:28 
GeneralRe: Left and Right Channel Volume Pin
Felix Wunderli27-May-08 1:28
Felix Wunderli27-May-08 1:28 
GeneralRe: Left and Right Channel Volume Pin
CastorTiu27-May-08 7:58
CastorTiu27-May-08 7:58 
GeneralRe: Left and Right Channel Volume Pin
Felix Wunderli28-May-08 19:48
Felix Wunderli28-May-08 19:48 
Generalpls help! microphone and multi-channel output card Pin
Taoge1-Oct-06 17:20
Taoge1-Oct-06 17:20 
Generali need help Pin
linsarella11-Sep-06 7:46
linsarella11-Sep-06 7:46 
GeneralError Message Pin
Chris Reaves21-Aug-06 7:16
Chris Reaves21-Aug-06 7:16 
GeneralRe: Error Message Pin
CastorTiu21-Aug-06 16:49
CastorTiu21-Aug-06 16:49 
GeneralRe: Error Message Pin
CastorTiu21-Aug-06 16:58
CastorTiu21-Aug-06 16:58 
GeneralRe: Error Message Pin
Chris Reaves1-Sep-06 7:46
Chris Reaves1-Sep-06 7:46 
GeneralRe: Error Message Pin
CastorTiu1-Sep-06 9:01
CastorTiu1-Sep-06 9:01 
GeneralRe: Error Message Pin
Chris Reaves18-Sep-06 10:53
Chris Reaves18-Sep-06 10:53 
AnswerRe: Error Message Pin
CastorTiu1-Oct-06 9:53
CastorTiu1-Oct-06 9:53 
QuestionMAX Values Pin
khaled_The_Prog11-Aug-06 16:33
khaled_The_Prog11-Aug-06 16:33 
AnswerRe: MAX Values Pin
CastorTiu21-Aug-06 16:53
CastorTiu21-Aug-06 16:53 
AnswerRe: MAX Values Pin
CastorTiu1-Oct-06 10:19
CastorTiu1-Oct-06 10:19 

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.