Hoof
: A basic pastebin type thing
Snip
ruby webrick based web server for text to speech using espeak
#!/usr/bin/env ruby require 'webrick' #create the html of the 'index' page @index_html="
Time Talky Thing
Text to Speak:
Delay Minutes:
" @index_html += "
" (0..100).each do |i| @index_html += "
#{i}
" end @index_html += "
" @index_html+="
" #function to run when / is requested def index (req, resp) resp['Content-Type'] = 'text/html' resp.body = @index_html end #function to run when /speak is requested def speak (req, resp) #get data from the request text = req.query['text'] delay = req.query['delay_minutes'].to_i * 60 resp['Content-Type'] = 'text/plain' resp.body = "OK" speak_string( text, delay) end def speak_string( text, delay = 0 ) #build a command cmd = "" cmd += "sleep #{delay} && " if delay cmd += "espeak -a150 -s150 -g7 -ven-us+f3 \"#{text}\"" if text #if there is a command, run it in a new thread unless cmd.empty? Thread.new do IO.popen(cmd) end end end # Initialize our WEBrick server if $0 == __FILE__ config = {:Port => 8000} server = WEBrick::HTTPServer.new(config) #what urls do we need to mount? server.mount_proc('/') do |req, resp| index( req, resp ) end server.mount_proc('/speak') do |req, resp| speak( req, resp ) end trap "INT" do server.shutdown end server.start end