-
Notifications
You must be signed in to change notification settings - Fork 1
/
speaking-web-server
executable file
·121 lines (110 loc) · 3.06 KB
/
speaking-web-server
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/env ruby
#
# speaking-web-server - Web server replying synthesized speech
#
# Usage:
#
# 1. Start a Web server.
#
# speaking-web-server
#
# 2. Access to the web server.
#
# Open browser and access: http://HOSTNAME_OR_IP_ADDRESS:9080
# (Change HOSTNAME_OR_IP_ADDRESS.)
#
# 3. Click 'Play' buttons.
#
require 'webrick'
require 'flite'
def start_server(html_content)
srv = WEBrick::HTTPServer.new({:Port => 9080})
srv.mount_proc('/') do |req, res|
h = req.query
if h['text']
audio_type = if h['type'] == 'mp3'
:mp3
else
:wav
end
res['Content-type'] = "audio/#{audio_type}"
res['Content-Disposition'] = %Q{attachment; filename="audio.#{audio_type}"}
voice = Flite::Voice.new(h['voice'] || 'kal')
res.body = voice.to_speech(h['text'], audio_type)
else
res['Content-type'] = 'text/html'
res.body = html_content
end
end
trap("INT"){ srv.shutdown }
srv.start
end
html_content = <<EOS
<html>
<head>
<title>Flite CGI</title>
<script>
audio_type = 'wav';
function check_audio_type() {
var audio = new Audio;
if (audio.canPlayType('audio/wav') == '') {
if (#{Flite.supported_audio_types.include? :mp3} && audio.canPlayType('audio/mp3') != '') {
audio_type = 'mp3';
} else {
var text = document.getElementById('text');
text.disabled = true;
enable_buttons(false);
alert('Cannot play audio/wav in this browser.\\nUse Chrome, Firefox, Safari or Opera.');
}
}
}
function speak(voice) {
var text = document.getElementById('text');
if (text != '') {
var status = document.getElementById('status');
status.innerHTML = 'Playing'
enable_buttons(false);
var url = '/?voice=' + voice + '&type=' + audio_type + '&text=' + encodeURIComponent(text.value);
var audio = new Audio(url);
audio.onended = function() {
status.innerHTML = 'Finished'
enable_buttons(true);
};
audio.onabort = function() {
status.innerHTML = 'Abort'
enable_buttons(true);
};
audio.onerror = function() {
status.innerHTML = 'Error'
enable_buttons(true);
};
audio.play();
}
}
function enable_buttons(bval) {
var form = document.getElementById('speech_form');
var elements = form.elements;
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.tagName == 'INPUT') {
element.disabled = !bval;
}
}
}
</script>
</head>
<body onLoad="check_audio_type();">
<form id="speech_form">
<textarea id="text" name="text" cols=80 rows=4>Hello Flite World!</textarea>
<br />
<input type="button" value="Play(voice: kal)" onClick="speak('kal');">
<input type="button" value="Play(voice: kal16)" onClick="speak('kal16');">
<input type="button" value="Play(voice: awb)" onClick="speak('awb');">
<input type="button" value="Play(voice: rms)" onClick="speak('rms');">
<input type="button" value="Play(voice: slt)" onClick="speak('slt');">
</form>
Status: <span id="status" />
</body>
</html>
EOS
start_server(html_content)