Skip to content

Commit da90c72

Browse files
completed the project
1 parent 01002b1 commit da90c72

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

medium/text_to_speech/app.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
let speech = new SpeechSynthesisUtterance();
2+
speech.lang = "en";
3+
4+
let voices = [];
5+
window.speechSynthesis.onvoiceschanged = () => {
6+
voices = window.speechSynthesis.getVoices();
7+
speech.voice = voices[0];
8+
let voiceSelect = document.querySelector("#voices");
9+
voices.forEach((voice, i) => (voiceSelect.options[i] = new Option(voice.name, i)));
10+
};
11+
12+
document.querySelector("#rate").addEventListener("input", () => {
13+
const rate = document.querySelector("#rate").value;
14+
speech.rate = rate;
15+
document.querySelector("#rate-label").innerHTML = rate;
16+
});
17+
18+
document.querySelector("#volume").addEventListener("input", () => {
19+
const volume = document.querySelector("#volume").value;
20+
speech.volume = volume;
21+
document.querySelector("#volume-label").innerHTML = volume;
22+
});
23+
24+
document.querySelector("#pitch").addEventListener("input", () => {
25+
const pitch = document.querySelector("#pitch").value;
26+
speech.pitch = pitch;
27+
document.querySelector("#pitch-label").innerHTML = pitch;
28+
});
29+
30+
document.querySelector("#voices").addEventListener("change", () => {
31+
speech.voice = voices[document.querySelector("#voices").value];
32+
});
33+
34+
document.querySelector("#start").addEventListener("click", () => {
35+
speech.text = document.querySelector("textarea").value;
36+
window.speechSynthesis.speak(speech);
37+
});
38+
39+
document.querySelector("#pause").addEventListener("click", () => {
40+
window.speechSynthesis.pause();
41+
});
42+
43+
document.querySelector("#resume").addEventListener("click", () => {
44+
window.speechSynthesis.resume();
45+
});
46+
47+
document.querySelector("#cancel").addEventListener("click", () => {
48+
window.speechSynthesis.cancel();
49+
});

medium/text_to_speech/index.html

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,39 @@
44
<meta charset="UTF-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>Document</title>
8-
</head>
9-
<body>
10-
11-
</body>
7+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" />
8+
<link rel="stylesheet" href="style.css" />
9+
<title>Text to Speech</title>
10+
</head>
11+
<body class="container mt-5 bg-dark">
12+
<h1 class="text-light">Text to Speech</h1>
13+
<p class="lead text-light mt-4">Select Voice</p>
14+
<select id="voices" class="form-select bg-secondary text-light"></select>
15+
<div class="d-flex mt-4 text-light">
16+
<div>
17+
<p class="lead">Volume</p>
18+
<input type="range" min="0" max="1" value="1" step="0.1" id="volume" />
19+
<span id="volume-label" class="ms-2">1</span>
20+
</div>
21+
<div class="mx-5">
22+
<p class="lead">Rate</p>
23+
<input type="range" min="0.1" max="10" value="1" id="rate" step="0.1" />
24+
<span id="rate-label" class="ms-2">1</span>
25+
</div>
26+
<div>
27+
<p class="lead">Pitch</p>
28+
<input type="range" min="0" max="2" value="1" step="0.1" id="pitch" />
29+
<span id="pitch-label" class="ms-2">1</span>
30+
</div>
31+
</div>
32+
<textarea class="form-control bg-dark text-light mt-5" cols="30" rows="10" placeholder="Type here..."></textarea>
33+
<div class="mb-5">
34+
<button id="start" class="btn btn-success mt-5 me-3">Start</button>
35+
<button id="pause" class="btn btn-warning mt-5 me-3">Pause</button>
36+
<button id="resume" class="btn btn-info mt-5 me-3">Resume</button>
37+
<button id="cancel" class="btn btn-danger mt-5 me-3">Cancel</button>
38+
</div>
39+
</body>
40+
<!-- script tag goes here -->
41+
<script src="./app.js"></script>
1242
</html>

0 commit comments

Comments
 (0)