Click here to Skip to main content
4.75 / 5, 37 votes

1

2

3
5 votes, 13.5%
4
32 votes, 86.5%
5

Vista Core Audio API Master Volume Control

By Ray M. | 18 May 2010
A managed wrapper for accessing the Vista Core Audio API
Screenshot - CoreAudioVolume.png

Introduction

Windows Vista features a completely new set of user-mode audio components that provide per application volume control. All legacy APIs such as the waveXxx and mixerXxx functions and DirectSound have been rebuilt on top of these new components so that without any code changes all 'old' applications support this new audio API. This is a great thing except when your application is reading or querying the master volume settings of the operating system because all of a sudden, you are no longer controlling the master volume of the operating system but only of your own application.

Core Audio API

The new API is COM based, and split into four sub APIs which roughly do the following:

  • MMDevice API - This API allows enumeration and instancing of the available audio devices in the system.
  • WASAPI - This API allows playback and recording of audio streams.
  • DeviceTopology API - This API allows access to hardware features such as bass, treble, and auto gain control.
  • EndpointVolume API - This API allows access to the Volume and Peak meters.

The MMDevice and EndpointVolume APIs are needed to control the master volume and mute settings, while the APIs themselves are a huge improvement since the legacy functions lack a nice managed interface, making working with them somehow an unpleasant experience in C#. Writing COM interop code is not for the novice and is error-prone, hence creating a wrapper to do all the plumbing for us seems a good idea.

Using the Code

You always start by creating the enumerator class, allowing you to find the device that you want, or just settle for the default device.

MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = 
  devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

After you have a reference to an MMDevice object, toggling Mute is as easy as:

defaultDevice.AudioEndpointVolume.Mute = 
             !defaultDevice.AudioEndpointVolume.Mute;

Or getting the volume of the left channel:

Console.WriteLine("Left Volume : {0}", 
      defaultDevice.AudioEndpointVolume.Channels[0].VolumeLevelScalar);

Since the wrapper implements the complete EndpointVolume API, we can also get more advanced things such as peak meters for all channels:

Console.WriteLine("Master Peak : {0}", 
      defaultDevice.AudioMeterInformation.MasterPeakValue);
Console.WriteLine("Left   Peak : {0}", 
      defaultDevice.AudioMeterInformation.PeakValues[0]);
Console.WriteLine("Right  Peak : {0}", 
      defaultDevice.AudioMeterInformation.PeakValues[1]);

If you just want to be informed when somebody changes the volume settings, you can subscribe to the OnVolumeNotification event like this:

defaultDevice.AudioEndpointVolume.OnVolumeNotification += new 
    AudioEndpointVolumeNotificationDelegate(
       AudioEndpointVolume_OnVolumeNotification);
.
.
void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
    Console.WriteLine("New Volume {0}", data.MasterVolume);
    Console.WriteLine("Muted      {0}", data.Muted);
}

Points of interest

The documentation of the volume notification states it must be non-blocking. The client should never wait on a synchronization object during an event callback. Keep that in mind when write event handlers.

Although Vista does per-application audio settings, Microsoft has not open up the APIs to enumerate Sessions. So it will not be possible to make a sndvol.exe-like application.

Right now, only the MMDevice and EndpointVolume APIs are implemented; I hope to add the WASAPI in a future update.

History

  • 2007.04.23 - Initial article
  • 2010.05.17 - Updated source and sample

License

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

About the Author

Ray M.


Web Developer

Netherlands Netherlands

Member


Sign Up to vote for this article
Add a reason or comment to your vote:

Comments and Discussions

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 73 (Total in Forum: 73) (Refresh)FirstPrevNext
QuestionRefresh device question PinmemberDan Brunner6hrs 11mins ago 
GeneralImplementing RegisterAudioSessionNotification(IAudioSessionEvents) in C# Pinmemberbwechner3:28 16 Aug '10  
GeneralInput device audio level requires active program Pinmemberwalshy00200021:38 30 Jul '10  
QuestionVB.Net event handler Pinmembermindfilter9:29 7 Mar '10  
GeneralSimilar functionality for WinXP? PinmemberJerry Jalenak5:20 9 Feb '10  
GeneralRe: Similar functionality for WinXP? PinmemberPhillip Cohen11:35 8 Jun '10  
QuestionSet level for right and left speakers separately? PinmemberHoHum19:20 3 Feb '10  
Question64-bit support? Pinmembereddycarroll12:51 2 Feb '10  
AnswerRe: 64-bit support? Pinmembereddycarroll23:35 4 Feb '10  
GeneralRe: 64-bit support? Pinmembergchudov20:16 25 Mar '10  
GeneralUsing With Windows 7 PinmemberTails8611:42 18 Dec '09  
GeneralYou're a genuis. Pinmemberstainlesssteel11:41 18 Nov '09  
GeneralRe: You're a genuis. Pinmemberunpocoloco20:08 11 Apr '10  
Generalhelp needed Pinmembertruthz033:39 19 Jul '09  
GeneralProblem - Sample Closing PinmemberMateus_ms21:18 3 Jun '09  
GeneralDeviceTopology PinmemberMark Moreno Jr1:44 8 Feb '09  
GeneralRe: DeviceTopology PinmemberMark Moreno Jr20:58 9 Feb '09  
GeneralHOW TO USE THIS EXCELLENT DLL IN VISUAL BASIC AND .NET (VERY SIMPLE) PinmemberMark Moreno Jr17:23 7 Feb '09  
GeneralRe: HOW TO USE THIS EXCELLENT DLL IN VISUAL BASIC AND .NET (VERY SIMPLE) Pinmemberhsirc8:09 18 Jun '09  
QuestionCode for WPF Pinmembercristi2go9:33 4 Feb '09  
GeneralWindows 7 64-bit PinmemberAbadi1767:17 27 Jan '09  
GeneralRe: Windows 7 64-bit PinmemberRay M.7:29 27 Jan '09  
GeneralRe: Windows 7 64-bit PinmemberAbadi1767:39 27 Jan '09  
GeneralRe: Windows 7 64-bit PinmemberYoder16:48 9 Feb '09  
GeneralRe: Windows 7 64-bit Pinmemberslaktardevil5:57 22 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2010

Copyright 2007 by Ray M.
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project