From 7c29bdb0e21d86ede81f42b9b54d9606bed55b74 Mon Sep 17 00:00:00 2001 From: SundaraRaman R Date: Fri, 20 Apr 2018 20:44:07 +0530 Subject: [PATCH 1/2] stop the which command from emitting stderr messages --- lib/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/user.rb b/lib/user.rb index c6eb9b1..0320ce8 100755 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,6 +1,6 @@ module User def self.has_command?(command) - response = `which #{ command }` + response = `which #{ command } 2>/dev/null` response != "" end From 6c869248cef3a976ee161e4f988c5452756817f9 Mon Sep 17 00:00:00 2001 From: SundaraRaman R Date: Fri, 20 Apr 2018 20:45:30 +0530 Subject: [PATCH 2/2] check and manage text-to-speech program used for speech --- lib/config.rb | 48 ++++++++++++++++++++++++++++++++++--------- lib/fun.rb | 12 ++++++++--- main.rb | 57 +++++++++++++++++---------------------------------- 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/lib/config.rb b/lib/config.rb index 2c50e5c..cf14505 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -2,7 +2,7 @@ module BettyConfig require 'yaml' @@config = {} - @@default_config = {"name" => "Betty","speech"=>false,"web"=>false,"chat"=>false} + @@default_config = {"name" => "Betty","speech"=>false,"speaker"=>"","web"=>false,"chat"=>false} def self.config_object @@config.inspect @@ -38,10 +38,22 @@ def self.interpret(command) # todo: merge all turn ... on|off commands if command.match(/^(turn|switch|the|\s)*speech\s+on$/i) || command.match(/^speak\s+to\s+me$/) - responses << { - :call_before => lambda { self.set("speech", true) }, - :say => "Speech ON", - } + if self.get("speaker").empty? + speaker_available = self.set_speaker + else + speaker_available = true + end + + if speaker_available + responses << { + :call_before => lambda { self.set("speech", true) }, + :say => "Speech ON", + } + else + responses << { + :say => "Something's wrong with my throat, I can't speak (the program 'say' would help me fix this)." + } + end end if command.match(/^(turn|switch|the|\s)*speech\s+off$/i) || command.match(/^stop\s+speak(ing)?\s+to\s+me$/) @@ -79,12 +91,17 @@ def self.interpret(command) } end - if command.match(/^(list\s(your\s)?voices)/i) - responses << { - :command => 'say -v "?"', - :explanation => 'List the availables voices for text-to-speech.' - } + if User.has_command?('say') + responses << { + :command => 'say -v "?"', + :explanation => 'List the availables voices for text-to-speech.' + } + else + responses << { + :say => "I don't seem to have a voice at all (the program 'say' would help me get some)." + } + end end if command.match(/^(?:set|change|make)\s+(?:your|betty\'?s?)\s+voice\s+to\s+(.+)$/i) @@ -132,6 +149,17 @@ def self.help } commands end + + def self.set_speaker + success = false + ["say", "spd-say", "mpg123", "afplay"].each do |speaker| + if User.has_command?(speaker) + self.set("speaker", speaker) + success = true + end + end + success + end end $executors << BettyConfig diff --git a/lib/fun.rb b/lib/fun.rb index 1744e76..27aed8c 100755 --- a/lib/fun.rb +++ b/lib/fun.rb @@ -53,9 +53,15 @@ def self.interpret(command) end if command.match(/sing (.*)/) - responses << { - :command => "say -v cello #{$~}" - } + if User.has_command?('say') + responses << { + :command => "say -v cello #{$~}" + } + else + responses << { + :say => "I don't have my singing voice today (the program 'say' would help me get one)." + } + end end responses diff --git a/main.rb b/main.rb index f33ba46..c1e2a40 100755 --- a/main.rb +++ b/main.rb @@ -123,47 +123,28 @@ def sanitize(text) end def speak(text) - if User.has_command?('say') - say = 'say' + return if text.empty? + speaker = BettyConfig.get("speaker") + if speaker == "say" voice=BettyConfig.get("voice") - say += " -v '#{voice}'" if voice - system("#{say} \"#{ text }\"") # formerly mpg123 -q - else - has_afplay = User.has_command?('afplay') - has_mpg123 = User.has_command?('mpg123') - has_spd_say = User.has_command?('spd-say') - - if Internet.connection_enable? - - if has_afplay || has_mpg123 - require 'open-uri' - text = sanitize(text) - speech_path = '/tmp/betty_speech.mp3' - - if text != '' - url = 'http://translate.google.com/translate_tts?tl=en&q=' + text - open(speech_path, 'wb') do |file| - file << open(url).read - end + speaker += " -v '#{voice}'" if voice + system("#{ speaker } \"#{ text }\"") # formerly mpg123 -q + elsif speaker == "spd-say" + system("spd-say -t female2 -m some -r 5 -p -25 -s \"#{text}\"") + elsif Internet.connection_enable? && (speaker == "afplay" || speaker == "mpg123") + require 'open-uri' + text = sanitize(text) + speech_path = '/tmp/betty_speech.mp3' + + url = 'http://translate.google.com/translate_tts?tl=en&q=' + text + open(speech_path, 'wb') do |file| + file << open(url).read + end - if has_afplay - system("afplay #{ speech_path }") - elsif has_mpg123 - system("mpg123 -q #{ speech_path }") - end + speaker += " -q " if speaker == "mpg123" + system("#{ speaker } #{ speech_path }") - system("rm #{ speech_path }") - end - else - if has_spd_say - system("spd-say -t female2 -m some -r 5 -p -25 -s \"#{text}\"") - end - end - else - if has_spd_say - system("spd-say -t female2 -m some -r 5 -p -25 -s \"#{text}\"") - end - end + system("rm #{ speech_path }") end end