-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
145 lines (124 loc) · 4.38 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<style>
.blabla {
font-size: 40px;
font-weight: bold;
color: yellow;
font-family: 'Courier New', Courier, monospace;
text-shadow:
-1px -1px 0 black,
1px -1px 0 black,
-1px 1px 0 black,
1px 1px 0 black;
}
.wrapper {
box-shadow: 5px 5px gray;
text-align: center;
background-color: lightgray;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
min-height: 100vh;
background-color: #00ff00;
}
.footer {
background-color: #00ff00;
padding: 10px;
text-align: center;
}
.content {
flex: 1;
/* Add styling for your content area if needed */
}
</style>
</head>
<body>
<div id="mainmain" class="wrapper">
<div id="main"></div>
</div>
<div class="content"></div>
<div id="controller" class="footer">
<button onclick="getAudio()">play</button>
</div>
<audio id="audio"></audio>
<script type="application/javascript">
const API_KEY = ""
const text = "";
const tokens = text.split(" ");
let wordsPerTime = 0;
const maxWordsPerTime = 5;
let phrase = "";
for (const i of tokens) {
if (wordsPerTime <= maxWordsPerTime) {
wordsPerTime++;
phrase += " " + i;
}
else {
wordsPerTime = 0;
document.getElementById("main").innerHTML += "<div style='display: none' class='blabla'>" + phrase + "</div>"
phrase = i
}
}
document.getElementById("main").innerHTML += "<div style='display: none' class='blabla'>" + phrase + "</div>"
let blabla = null;
function play() {
document.getElementById("audio").play();
const phrases = document.querySelectorAll('.blabla');
const readTime = 2000;
for (var i = 0; i < phrases.length; i++) {
const readTimeWithPunctuation = phrases[i].innerHTML.indexOf('.') > 0 ? readTime + 500 : readTime;
window.setTimeout((a) => {
a.style = "display: block"
//console.log(a.innerHTML + " " + readTimeWithPunctuation);
window.setTimeout((b) => {
b.style = "display: none";
}, readTimeWithPunctuation, a);
}, readTime * i + 500, phrases[i])
}
}
function getAudio() {
//document.getElementById("controller").style = "display: none;"
// if there is an audio, we don't need to call the API again, let's just replay it.
if (document.getElementById('audio').src.length > 22) {
play();
return;
}
fetch(`https://texttospeech.googleapis.com/v1/text:synthesize?key=${API_KEY}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: { text },
voice: { languageCode: 'pt-BR', name: 'pt-BR-Neural2-A' },
audioConfig: {
audioEncoding: 'MP3', "audioEncoding": "LINEAR16",
"effectsProfileId": [
"small-bluetooth-speaker-class-device"
],
//"pitch": 9.60,
"speakingRate": 1.52 //1.05
},
}),
})
.then(response => response.json())
.then(data => {
const audioContent = data.audioContent;
const audioUrl = `data:audio/mp3;base64,${audioContent}`;
document.getElementById('audio').src = audioUrl
play();
})
.catch(error => {
console.error('Error synthesizing speech:', error);
});
}
</script>
</body>
</html>