说话时使计算机静音(不起作用)

时间:2019-03-15 02:28:28

标签: python audio python-multithreading playsound gtts

我正在使用豪猪热词检测和gtts语音模块制作一个供个人使用的虚拟助手。我试图在gtts模块读取mp3文件时检测到“静音”热门单词,但看来我在修改的playound模块中创建的线程在讲话时无法正常工作。有什么建议么?这是代码:

这是语音代码:

from gtts import gTTS
import sys, playsound
def speak(text):
    tts = gTTS(text=str(text), lang='en')
    tts.save(r"D:\\Sounds\\speak.mp3")
    playsound.playsound(r"D:\\Sounds\\speak.mp3")

这是playsound模块的修改代码:

class PlaysoundException(Exception):
    pass
import threading, os, pyautogui, time

def mute():
    pyautogui.keyDown("volumemute")
    time.sleep(0.2)
    pyautogui.keyUp("volumemute")
def go_sleep():
    os.system(r'python D:\\Porcupine\demo\\python\\porcupine_demo.py -- 
    keyword_file_paths ' + os.getcwd() + r'\\Hotwords\\muting_windows.ppn --sensitivities 0.02')
    print("muting")
    mute()
def start_thread():
    thread = threading.Thread(target=go_sleep)
    thread.start()
def _playsoundWin(sound):
    from ctypes import c_buffer, windll
    from random import random
    from time import sleep
    from sys import getfilesystemencoding
    import datetime
    def winCommand(*command):
        buf = c_buffer(255)
        command = ' '.join(command).encode(getfilesystemencoding())
        errorCode = int(windll.winmm.mciSendStringA(command, buf, 254, 0))
        if errorCode:
            errorBuffer = c_buffer(255)
            windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
            exceptionMessage = ('\n    Error ' + str(errorCode) + ' for 
command:'
                            '\n        ' + command.decode() +
                            '\n    ' + errorBuffer.value.decode())
            raise PlaysoundException(exceptionMessage)
        return buf.value
    alias = 'playsound_' + str(random())
    winCommand('open "' + sound + '" alias', alias)
    winCommand('set', alias, 'time format milliseconds')
    durationInMS = winCommand('status', alias, 'length')
    now = datetime.datetime.now()
    milli = float(durationInMS.decode())
    date = now + datetime.timedelta(milliseconds=milli)
    start_thread()
    while now <= date:
        winCommand('play', alias, 'from 0 to', durationInMS.decode())
        now = datetime.datetime.now()

playsound = _playsoundWin

如前所述,该代码仅适用于线程热字检测。请让我知道建议,也许我在做错什么。预先感谢。

0 个答案:

没有答案