我已经开始使用Ruby on Rails为Siri Proxy Server制作一些插件。 我对Ruby缺乏经验,但已经掌握了基础知识。
我做了什么:
################ Commands listen_for (/show a demo to (.*)/i) do |name| show_demo request_completed end ################ Actions def show_demo(name) say "Hi #{name}, let me do a quick demo for You." say "For example if You tell me 'Turn on sidelight' I will turn the sidelights in Living room like now..." system "/usr/local/bin/tdtool --on 2" say "That was the sidelights, and now if like I can turn on the gallery for You, just tell me 'turn on gallery' like so... " system "/usr/local/bin/tdtool --on 3" say "This only part of things I can do after mod." say "Now I will turn all devices off..." system "/usr/local/bin/tdtool --off 3" system "/usr/local/bin/tdtool --off 2" say " Thank You #{name}, and goodbye." end
问题是,当我开始演示时,所有动作system "..."
都会在Siri开始说话之前执行。
我怎样才能推迟上述行动,及时将它们放在正确的位置,以便在我想要的单词后立即执行它们?
提前谢谢你。
答案 0 :(得分:0)
问题是say
不会等待Siri实际说出这些词,它只是将一个数据包发送到你的iDevice,然后继续。我能想到的最简单的方法是等待几秒钟,具体取决于文本的长度。所以首先我们需要一个方法来给我们等待的持续时间(以秒为单位)。我尝试使用OSX内置say
命令并得到以下结果:
$ time say "For example if You tell me 'Turn on sidelight' I will turn the sidelights in Living room like now..."
say 0,17s user 0,05s system 3% cpu 6,290 total
$ time say "That was the sidelights, and now if like I can turn on the gallery for You, just tell me 'turn on gallery' like so... "
say 0,17s user 0,06s system 2% cpu 8,055 total
$ time say "This only part of things I can do after mod."
say 0,13s user 0,04s system 5% cpu 2,996 total
所以这意味着我们有以下数据:
# Characters w/o whitespace | Seconds to execute
------------------------------+---------------------
77 | 6.290
87 | 8.055
34 | 2.996
这使我们每个角色的平均时间约为0.0875
秒。您可能需要自己评估场景的平均时间并使用更多样本。这个函数将包装say
,然后等到Siri说出文本:
def say_and_wait text, seconds_per_char=0.0875
say text
num_speakable_chars = text.gsub(/[^\w]/,'').size
sleep num_speakable_chars * seconds_per_char
end
其中gsub(/[^\w]/,'')
将从字符串中删除任何非单词字符。现在你可以用它来简单地说出来并等待它说出来:
say_and_wait "This is a test, just checking if 0.875 seconds per character are a good fit."
或者您也可以在特殊情况下覆盖持续时间:
say_and_wait "I will wait for ten seconds here...", 10
让我知道它是否适合你。