-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.html.experimental
175 lines (156 loc) · 8.86 KB
/
stt.html.experimental
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Browser speech recognition</title>
<link rel="stylesheet" href="./speech-to-text.css" />
</head>
<body>
<header>
<h1>Pelajaran bahasa inggris warna buat anak TK</h1>
</header>
<main>
<div id="textToSynth">
<!-- <input autofocus size="23" type="text" id="textEntry" value="It's very good to meet you." /> -->
<button class="btn default" id="speaks" style="display:none" onClick="speakText()">Synthesize</button>
<!-- <p id="result">Enter text above then click Synthesize</p> -->
</div>
<audio id="audioPlayback" controls autoplay>
<source id="audioSource" type="audio/mp3" src="">
</audio>
<button id="button">Start listening</button>
<div id="result"></div>
<p id="message" hidden aria-hidden="true">
Your browser doesn't support Speech Recognition. Sorry.
</p>
</main>
<footer>
<p>Built by <a href="#!">alfian</a></p>
</footer>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.763.0.min.js"></script>
<script>
window.addEventListener("DOMContentLoaded", () => {
const button = document.getElementById("button");
const result = document.getElementById("result");
const main = document.getElementsByTagName("main")[0];
let listening = false;
//I want to select and change the color of the body, but this could be any HTML element on your page
let body = document.querySelector("body")
let cap_css_colors = ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "Darkorange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue", "FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod", "Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenRodYellow", "LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "SlateGrey", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen"];
const CSS_COLORS = cap_css_colors.map(color => {
//I need to change all color names to lower case, because comparison between words will be case sensitive
return color.toLowerCase()
})
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
if (typeof SpeechRecognition !== "undefined") {
const recognition = new SpeechRecognition();
// recognition.lang = "id";
const stop = () => {
main.classList.remove("speaking");
recognition.stop();
button.textContent = "Start listening";
};
const start = () => {
main.classList.add("speaking");
recognition.start();
button.textContent = "Stop listening";
};
var i = 0;
const onResult = event => {
result.innerHTML = "";
for (const res of event.results) {
const text = document.createTextNode(res[0].transcript);
const p = document.createElement("p");
if (res.isFinal) {
p.classList.add("final");
}
p.appendChild(text);
result.appendChild(p);
const transcript = Array.from(event.results)
.map(result => result[0])
.map(result => result.transcript)
.join("")
// p.innerText = transcript
//for each result, map through all color names and check if current result (transcript) contains that color
//i.e. see if a person said any color name you know
CSS_COLORS.forEach(function (color) {
//if find a match, change your background color to that color
if (transcript.includes(color)) {
// console.log(transcript)
color = lastColor(transcript)
// console.log(color);
body.style.backgroundColor = color;
}
})
}
i++;
console.log(i)
};
// setTimeout(function () {
// document.getElementById('speaks').click()
// }, 500);
recognition.continuous = true;
recognition.interimResults = true;
recognition.addEventListener("result", onResult);
button.addEventListener("click", event => {
listening ? stop() : start();
listening = !listening;
});
} else {
button.remove();
const message = document.getElementById("message");
message.removeAttribute("hidden");
message.setAttribute("aria-hidden", "false");
}
});
function lastColor(words) {
var n = words.split(" ");
return n[n.length - 1];
}
</script>
<script type="text/javascript">
// Initialize the Amazon Cognito credentials provider
// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'ap-southeast-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'ap-southeast-1:dd7aef1f-b489-4633-82e1-3221f2a2e975',
});
// Function invoked by button click
function speakText() {
// Create the JSON parameters for getSynthesizeSpeechUrl
var speechParams = {
OutputFormat: "mp3",
SampleRate: "16000",
Text: "",
TextType: "text",
VoiceId: "Amy",
};
// speechParams.Text = document.getElementById("textEntry").value;
speechParams.Text = removeTags(document.getElementById('result').innerHTML);
// Create the Polly service object and presigner object
var polly = new AWS.Polly({ apiVersion: '2016-06-10' });
var signer = new AWS.Polly.Presigner(speechParams, polly)
// Create presigned URL of synthesized speech file
signer.getSynthesizeSpeechUrl(speechParams, function (error, url) {
if (error) {
document.getElementById('result').innerHTML = error;
} else {
document.getElementById('audioSource').src = url;
document.getElementById('audioPlayback').load();
// document.getElementById('result').innerHTML = "Speech ready to play.";
}
});
}
function removeTags(str) {
if ((str === null) || (str === ''))
return false;
else
str = str.toString();
return str.replace(/(<([^>]+)>)/ig, '');
}
</script>
</body>
</html>