简体   繁体   English

使用 Python、Windows 10 更改麦克风音量

[英]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.由于 Microsoft Teams 倾向于更改我的音频设置,因此我想编写一个代码来让一切恢复正常。 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.我找不到任何使用 Python 更改麦克风音量的示例代码。 I've looked into Microsoft registry if I could try to change it through that, no luck.如果我可以尝试通过它进行更改,我已经查看了 Microsoft 注册表,但没有运气。

Any suggestions on how to change the microphone volume level in Python?有关如何更改 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. Python-sounddevice提供 PortAudio 库的绑定和一些方便的函数来播放和录制包含音频信号的 NumPy arrays。 Here an example code to plot the microphone signal. 这里的示例代码为 plot 麦克风信号。

or或者

Pyaudio .皮奥迪奥 Provides Python bindings for PortAudio, the cross-platform audio I/O library.为跨平台音频 I/O 库 PortAudio 提供 Python 绑定。 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.借助 PyAudio,您可以轻松使用 Python 在各种平台上播放和录制音频,例如 GNU/Linux、Microsoft Windows 和 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.好的,经过长时间的挖掘,我找到了命令提示符的 nircmd.exe 扩展名,它可以处理更改音频设置。 With that, I wrote a code that sets Teams' master audio and microphone volume back to my desired volume.有了这个,我编写了一个代码,将 Teams 的主音频和麦克风音量设置回我想要的音量。

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.我应该补充一点,要使此代码正常工作,您需要将 nircmd.exe 与代码文件放在同一目录中。

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.此外,为了稳定 Teams 主卷,您需要在每次会议之前运行此代码,因为代码会进入循环并且在 nircmd 命令完成之前不会退出。

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:)有一个和平的团队会议:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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