Making your computer talk with Ruby.
Published on March 01, 2015 by Mike Chilson
Ruby Text-To-Speech Code Example
3 min READ
My favorite projects are when I get to find a solution to small problems for clients and solve them quickly for them. I had a client that needed some Text To Speech (TTS) capabilities on a small Linux system in their shipping department. Easy enough right? After a little research, I found a really simple open-source code example of TTS that someone had posted that would work on Windows, Linux, and a Mac and written in Ruby.
While this post is old, I have updated the example code to work with Ruby 2.7. I did a bit of refactoring and customizing ended up with a pretty cool little command-line utility that will read any text you feed it from the command prompt.
First, we need to be able to identify which operating system we are running on. So let’s create a module file called operating_system.rb
module OperatingSystem
require 'rbconfig'
module_function
def operating_system
case RbConfig::CONFIG['host_os']
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
else
nil
end
end
def linux?; operating_system == :linux; end
def windows?; operating_system == :windows; end
def mac?; operating_system == :macosx; end
end
This code will allow us to cleanly determine which OS we are running our main program file on. Next, if you are running on Windows you will need to add a Gemfile to your project and add the win32-sapi gem.
source 'https://rubygems.org'
# Uncomment gem definition below if you are running this on windows
#gem 'win32-sapi'
Also, if you will be running this application on Windows don’t forget to run gem install win32-sapi at the command prompt to install the gem.
One more thing… If you are going to run this sample on Linux you need to install the espeak utility using your distributions software manager. You can learn more about espeak by visiting their website.
Now let’s get to the meat and potatoes of this app. Create a file called speakup.rb and type or paste the following code:
load 'operating_system.rb'
def speak(text)
if OperatingSystem.windows?
require 'win32/sapi5'
v = Win32::SpVoice.new
v.Speak(text)
elsif OperatingSystem.mac?
IO.popen(['say'], 'w') {|pipe| pipe.puts text}
else
#Try to run "espeak". No OperatingSystem check: "espeak" is
#for Linux but is also an optional package for BSD.
IO.popen(["espeak", "-stdin"], "w") {|pipe| pipe.puts text}
end
end
ARGV.each do|a|
speak a
end
Once saved, at the command prompt simply type ruby speak.rb “This is a test” and you should hear the TTS engine speaking. Also, note you can pass multiple string arguments like this: ruby speak.rb “This is a test” “Can you hear me?” and the TTS will speak all of the string arguments.
I hope you found this small utility handy and interesting as I did. It is a lot of fun and a great stepping stone to more complex command line Ruby applications. You can download the sample code from my GitLab repository.