Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting individual windows application current volume output level as visualized in audio Mixer

I am trying to write a C# code that outputs the current audio output level from each of the windows application accessing the sound output (as shown with constantly changing green bars of the Volume mixer).

The program will check every 10 ms, and outputs sth like this: Windows Media Player: 30, Mozilla Firefox: 0, Adobe Flash Player: 35 (as per the figure)

I am using Windows 7, and trying it in C# (as Java cannot achieve this).

I have found ways to get and set the Master Volume (the handle bar which shows 65% for Windows Media Player) for a running application, is there a way to get the green fluctuating level data?

Thank you!

Audio Mixer

like image 992
mio Avatar asked Jan 18 '14 06:01

mio


People also ask

How do I change the Apps on my volume mixer?

Right-click [Speakers icon] on the taskbar⑤, and then select [Open volume mixer]⑥. In the Apps filed, by dragging the slider to adjust volume for each app⑦.

How do I make everything in my volume mixer the same?

There is a simple fix: manually drag all of them to 100% and the drag the master all the way to 0. Drag the master up again. You'll notice they're all in sync now. This happens if you modiy the volumes for programs individually to different levels and you attempt to modify them all use the master slider.

How do you normalize a volume mixer?

This is very handy if you have a particular application that's louder than others, letting you normalize volume manually. Right-click the speaker icon on your taskbar and select “Open Volume mixer”. Move the sliders to adjust the volume of each application to suit your preferences, with higher naturally being louder.


2 Answers

You can use CSCore. There is a wrapper for the CoreAudioAPI-Audiosessions. Use something like that (for more details take a look at the unittests: AudioSession-UnitTests):

private static void Main(string[] args)
{
    using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
    {
        using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
        {
            foreach (var session in sessionEnumerator)
            {
                using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                {
                    Console.WriteLine(audioMeterInformation.GetPeakValue());
                }
            }
        }
    }

    Console.ReadKey();
}

private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
{
    using (var enumerator = new MMDeviceEnumerator())
    {
        using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
        {
            Debug.WriteLine("DefaultDevice: " + device.FriendlyName);
            var sessionManager = AudioSessionManager2.FromMMDevice(device);
            return sessionManager;
        }
    }
}

To control an applications volume, take a look at the unit-tests here.

like image 184
Florian Avatar answered Sep 29 '22 00:09

Florian


Here is a sample application which displays the audio levels from running applications in a graph. There are two versions, one in WPF and one in Windows.Forms. They use the method from Florian's answer to get the audio levels.

https://github.com/jeske/SoundLevelMonitor

enter image description here

like image 41
David Jeske Avatar answered Sep 28 '22 23:09

David Jeske