Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text-to-speech management (fixes #197, #170) #202

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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$/)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
12 changes: 9 additions & 3 deletions lib/fun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module User
def self.has_command?(command)
response = `which #{ command }`
response = `which #{ command } 2>/dev/null`
response != ""
end

Expand Down
57 changes: 19 additions & 38 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down