如何使用speech modu优化脚本,使其在单词之间有1秒的间隔

2024-03-29 00:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是,当我运行脚本时,它在发出下一个命令之前花费的时间比预期的时间长1秒。我想这跟语音命令有关。我能做些什么来优化这个?你知道吗

编辑:链接到sppech模块https://pypi.python.org/pypi/speech/0.5.2

edit2:每个请求我只使用datetime来测量睡眠时间。 2016-06-29 18:39:42.953000 2016-06-29 18:39:43.954000 我发现它相当准确

edit3:我尝试了导入win32的内部版本com客户端而且也没用

import speech
import time
import os 

def exercise1():
    speech.say("exercise1")
    time.sleep(0.5)
    for n in range(0, rep*2):
        speech.say("1")
        t   ime.sleep(1)
        speech.say("2")
        time.sleep(1)
        speech.say("3")
        time.sleep(1)
        speech.say("switch")

Tags: import命令脚本pypi编辑time链接时间
2条回答

参考这里的帖子How accurate is python's time.sleep()?

上面写着:

"The accuracy of the time.sleep function depends on the accuracy of your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms."

正如你在评论中所说,睡眠(1)相当准确地说是1s

你要做的是让每一部分用1秒,是时间的“说”电话,然后等待剩余的时间填写第二。像这样:

start = time.time()
speech.say("whatever")
end = time.time()
sleep(1 - (end - start)) # Wait however long will bring the time up to 1 second total

相关问题 更多 >