-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml5_app.rb
101 lines (65 loc) · 2.89 KB
/
html5_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
# html5_app.rb
# Copyright 2013 Robert Jones ([email protected]) Craic Computing LLC
# This code and associated data files are distributed freely under the terms of the MIT license
require 'open-uri'
require 'base64'
require 'json'
require 'aws-sdk'
require 'bing_translator'
$:.unshift File.join(File.dirname(__FILE__))
class Html5App < Sinatra::Base
set :root, File.dirname(__FILE__)
set :static, true
# Set up AWS and Bing credentials --- DO NOT MAKE THESE AVAILABLE TO OTHERS
aws_access_key_id = "YOUR KEY GOES HERE"
aws_secret_access_key = "YOUR KEY GOES HERE"
aws_s3_bucket = "YOUR S3 BUCKET GOES HERE"
bing_client_id = "YOUR BING CLIENT ID GOES HERE"
bing_client_secret = "YOUR BING CLIENT SECRET GOES HERE"
AWS.config({"access_key_id" => aws_access_key_id, "secret_access_key" => aws_secret_access_key})
s3 = AWS::S3.new
bucket = s3.buckets[aws_s3_bucket]
get '/' do
File.read(File.join('public', 'index.html'))
end
# Simplified version that uses web audio API (not available on Firefox)
get '/text_to_speech_web_audio_api' do
query = params['query']
language = params['language']
translator = BingTranslator.new(bing_client_id, bing_client_secret)
# Fetch the WAV audio for this phrase
# audio tag in Firefox does not play mp3 files ! - due to licensing issues...
audio_stream = translator.speak query, :language => language, :format => 'audio/wav', :options => 'MaxQuality'
response["Access-Control-Allow-Origin"] = "*"
content_type 'audio/wav'
audio_stream
end
# Complicated version that stores files on AWS and uses a proxy (works on Firefox)
get '/text_to_speech_audio_tag' do
query = params['query']
language = params['language']
translator = BingTranslator.new(bing_client_id, bing_client_secret)
# Fetch the WAV audio for this phrase
# audio tag in Firefox does not play mp3 files ! - due to licensing issues...
audio_stream = translator.speak query, :language => language, :format => 'audio/wav', :options => 'MaxQuality'
# create a unique digest with the text query and the current time
digest = Digest::SHA1.hexdigest "#{query} #{Time.now.to_s}"
# Write to a file on Amazon S3
filename = "#{language}_#{digest}.wav"
s3obj = bucket.objects[filename].write(audio_stream, :acl => :public_read)
# return the filename as json
response["Access-Control-Allow-Origin"] = "*"
content_type 'application/json', :charset => 'utf-8'
{ 'url' => filename }.to_json
end
# Given the filename for an audio file of AWS S3, fetch it and echo the data to the client
# This is necessary to get around S3 not supporting the Access-Control-Allow-Origin header
get '/proxy' do
filename = params['file']
s3obj = bucket.objects[filename]
response["Access-Control-Allow-Origin"] = "*"
content_type 'audio/wav'
s3obj.read
end
end