简体   繁体   中英

Changing microphone volume using Python, Windows 10

Since Microsoft Teams tends to change my audio setting, I want to write a code that will set everything back to normal. I've already figured out how to fix the application volume level but I'm stuck at the microphone volume level.

I haven't been able to find any example codes for changing mic volume using Python. I've looked into Microsoft registry if I could try to change it through that, no luck.

Any suggestions on how to change the microphone volume level in Python?

you could give a try with:

Python-sounddevice Provides bindings for the PortAudio library and a few convenience functions to play and record NumPy arrays containing audio signals. Here an example code to plot the microphone signal.

or

Pyaudio . Provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple Mac OS X / macOS.

Ok so after a long digging, I found nircmd.exe extension for command prompt, that can handle changing audio settings. With that, I wrote a code that sets Teams' master audio and microphone volume back to my desired volume.

Here is the code if you were looking for something similar:

# STAN = Set Teams Auto-adjustment to Normal

from __future__ import print_function
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
import os


def main():
    sessions = AudioUtilities.GetAllSessions()

    for session in sessions:
        volume = session._ctl.QueryInterface(ISimpleAudioVolume)

        if session.Process and session.Process.name() == "Teams.exe":
            print("Teams audio is stabilized...")

            volume.SetMasterVolume(0.18, None)
            # 18% of system master volume
            

    print("Microphone is being stabilized...")
    os.system("nircmdc.exe loop 144000 250 setsysvolume 45875 default_record")
    # nircmdc.exe loop /number of loops/ /time in ms to execute one loop/ setsysvolume /65536 == 100%/ /device/

    print("Stabilization finished")


if __name__ == "__main__":
    main()

I should add that for this code to work, you need to have nircmd.exe in the same directory as the code file.

Also, to stabilize Teams master volume you need to run this code before each meeting because the code goes into a loop and won't get out until the nircmd command is finished.

This code will stabilize your microphone volume even if you don't have teams on, so feel free to use it that way as well.

Have a peaceful teams meeting:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM