#!/bin/env ruby # # Ruby client for the MARY TTS HTTP server. # This is for Windows only, and relies on # the Win32-Sound gem to access the audio. # # require 'rubygems' require 'win32/sound' require 'net/http' require 'uri' # A fairly minimal client class for the # MARY TTS system. This uses the modern # HTTP interface to access the server. # It is limited, at the moment, to Windows # systems and relies on the Win32-Sound gem # which must be obtained in the usual way # that Rubygems are obtained. # At present, this doesn't wrap the methods # which provide documentation or lists of # voices or features. class WinMaryClient attr_accessor :host, :port attr_accessor :input_type, :output_type attr_accessor :locale, :audio, :voice # Set up the defaults for the MARY TTS # server, which is assumed to be running # on the local host, with British voices # installed. These may be modified with # the appropriate methods. # host = 127.0.0.1) # port = 59125 # input_type = "TEXT" # output_type = "AUDIO" # audio = "WAVE_FILE" # locale = "en_GB" # voice = "dfki-prudence-hsmm" def initialize @host = "127.0.0.1" # The local machine @port = 59125 @input_type = "TEXT" @output_type = "AUDIO" @locale = "en_GB" @audio = "WAVE_FILE" @voice = "dfki-prudence-hsmm" end # Process a text message, which with a # new client, will just say it. def say(message) raw_params = {"INPUT_TEXT" => message, "INPUT_TYPE" => @input_type, "OUTPUT_TYPE" => @output_type, "LOCALE" => @locale, "AUDIO" => @audio, "VOICE" => @voice, } res = Net::HTTP.post_form(URI.parse("http://#{@host}:#{@port}/process"), raw_params) res.value # Throw an exception on failure if res.get_fields('content-type')[0] =~ /^audio\//i Win32::Sound.play(res.body, Win32::Sound::MEMORY) else #puts res.body return res.body end end end # If this invoked as a program, just give a # greeting to show that it is working. if __FILE__ == $0 client = WinMaryClient.new() client.say("Hello from Mary Text to Speech with Ruby.") end