Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for me to see how much volume an application is outputting?

Tags:

python

I'm trying to make a computer application which will look at the volume that Spotify and discord are outputting, and balance it accordingly so that I can hear my friends, but when they're not talking, my music is louder.

This is for a windows 10 computer, I've used pycaw to get the master volume as well as modify the master volume; however, I could not find an option to get the current volume being outputted.

from __future__ import print_function
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume, IAudioEndpointVolume, IAudioEndpointVolumeCallback


def main():
    sessions = AudioUtilities.GetAllSessions()
    for session in sessions:
        volume = session._ctl.QueryInterface(ISimpleAudioVolume)
        if session.Process and session.Process.name() == "Discord.exe":
            print("volume.GetMasterVolume(): %s" % volume.GetMasterVolume())


if __name__ == "__main__":
    main()

By doing this, I can get the maximum volume for discord (for example 1.0). However, I want to get the level of audio discord is currently outputting (for example 0.3). What would I need to replace

volume.GetMasterVolume()

to achieve this? Thanks.

like image 941
Penguronik Avatar asked Jun 08 '19 22:06

Penguronik


People also ask

Does Macos have a volume mixer?

Volume Mixer for Mac lives in your menu bar. With simple features like muting apps by double clicking and detaching an app from the master volume by right clicking, Volume Mixer is easy to learn and simple to use.

How do I control application volume on Mac?

To change the volume on your Mac, click the Sound control in the menu bar or Control Center, then drag the slider to adjust the volume (or use the Control Strip). If the Sound control isn't in the menu bar, choose Apple menu > System Preferences, then click Sound .

How do I see audio Output on Android?

Swipe down from the top of the screen to see notifications. Swipe down a second time. Tap the small button at the top right of the player notification tile. In the media player pop-up, you'll see a list of connected audio devices.


1 Answers

This is not a full answer, but I figured it may help someone on their way.

You should look at the Windows Core Audio API. Part of this API are 'audio sessions' https://docs.microsoft.com/en-us/windows/win32/coreaudio/audio-sessions, which are the audio sessions for the applications as they appear in the Volume Mixer, which I think are the volumes you're referring to.

There's an existing work-in-progress Python library called pyWinCoreAudio, but it appears to be incomplete and doesn't currently cover audio sessions (or in no way that I could fine). However, you can have a look at the open source (GPL) source code and it should be relatively straightforward to add the functionality to query the audio sessions and set their.

For example, with the part that is already working in pyWinCoreAudio, this is how you'd set the volume on an existing device:

import pyWinCoreAudio as wca

wca.AudioDevices[3].render_endpoints[0].volume.channel_levels = (-10, -10)

In this case, AudioDevices[3] happens to be my headset, and setting the volume to (-10, -10) sets it 10dB below max (which would be (0, 0) on both channels.

To list all the audio devices and their outputs on your system, you'd do something like:

import pyWinCoreAudio as wca

for n, d in enumerate(wca.AudioDevices):
    for rep in d.render_endpoints:
        print(f'render endpoint {n}: {rep.name} is a {rep.form_factor}')

But this is in relation to the actual devices, not the applications producing the audio (i.e. the audio sessions), which the library doesn't appear to expose yet.

like image 147
Grismar Avatar answered Nov 15 '22 01:11

Grismar