Skip to content

Instantly share code, notes, and snippets.

@wktk
Created July 13, 2018 14:48
Show Gist options
  • Save wktk/fbd3043c826ea8cf44e1322c2d7e4cf2 to your computer and use it in GitHub Desktop.
Save wktk/fbd3043c826ea8cf44e1322c2d7e4cf2 to your computer and use it in GitHub Desktop.
Web UI to call the `say` command on Mac
require 'rack'
voices = `say -v ?`.each_line.map { |l| l.gsub(/ #.*/, '').split(/\s{2,}/) }.to_h
html = DATA.read.sub(
'{VOICES}',
voices.map { |k, v| "<option value='#{k}'>#{k} (#{v})</option>" }.join
)
app = -> (env) do
params = URI.decode_www_form(env['rack.input'].read).to_h
if params['text'] && voices.keys.include?(params['voice'])
Thread.new { system 'say', '-v', params['voice'], params['text'] }
end
[200, { 'content-type' => 'text/html' }, [html]]
end
Rack::Handler::WEBrick.run(app, Host: '0.0.0.0')
__END__
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Say</title>
<meta name="viewport" content="user-scalable=no,initial-scale=1,width=device-width">
</head>
<body>
<form action="/" method="POST">
<textarea name="text"></textarea><br>
<select name="voice">{VOICES}</select><br>
<input type="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment