Skip to content

Commit c011175

Browse files
authored
Merge pull request fac29b#46 from fac29b/text-to-speech-feature
Text to speech feature
2 parents f90bb67 + d48e0c2 commit c011175

3 files changed

Lines changed: 87 additions & 5 deletions

File tree

public/index.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@
1414
width: 100vw;
1515
}
1616

17+
.recording {
18+
/* position: fixed; */
19+
width: 50%;
20+
height: fit-content;
21+
display: flex;
22+
justify-content: space-evenly;
23+
flex-direction: row;
24+
margin: auto;
25+
}
26+
27+
28+
29+
30+
.speed-wrapper {
31+
display: flex;
32+
}
33+
34+
#speed {
35+
margin-left: 10px;
36+
37+
}
38+
1739
.main-element {
1840
overflow: scroll;
1941
min-height: 100vh;
@@ -58,6 +80,10 @@ main {
5880
transform: translate(-50%,-50%);
5981
}
6082

83+
.fa-microphone {
84+
height: 30px;
85+
width: 30px;
86+
}
6187

6288

6389
#background-img {

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/>
1919
</head>
2020
<body>
21+
2122
<div aria-label="parent container" class="parent-container">
2223
<img src="" alt="" id="background-img" />
2324
<main class="main-element">

public/index.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const mainElement = document.querySelector(".main-element");
22
const test = document.querySelector(".test");
3-
console.log(test);
4-
test.addEventListener("click", () => {
5-
console.log("test");
6-
});
73
const backgroundImg = document.querySelector("#background-img");
84
const gptResponseElement = document.querySelector(".gpt-response");
95
const headline = document.querySelector(".headline");
@@ -33,6 +29,7 @@ let textContent;
3329
let imageUrl;
3430
let isLactoseIntolerant;
3531
let dishOriginCountry;
32+
let currentChar
3633
const defaultRecipe = `
3734
Apologies, but our AI Recipe-Making expert is unavailable. Please try again later. In the meantime, please find one of our favourite recipes below.
3835
@@ -265,13 +262,71 @@ recipeButtons.forEach((button) => {
265262
.then(() => {
266263
console.log("image loaded:", Promise.all.status);
267264
textContent = data.text.choices[0].message.content;
268-
gptResponseElement.innerHTML = `${textContent}`;
265+
gptResponseElement.innerHTML = `
266+
<div class="recording">
267+
<i class="fa-solid fa-microphone"></i>
268+
<i class="fa-solid fa-pause"></i>
269+
<i class="fa-solid fa-stop"></i>
270+
<div class="speed-wrapper">
271+
<label for="speed">Speed</label>
272+
<input type="number" name="speed" id="speed" min="0.25" max="2" step="0.25" value="1">
273+
</div>
274+
</div>
275+
${textContent}`;
269276
removeElements([headline, allergies, ...recipeButtons]);
270277
displayElements([
271278
userWantAnotherRecipe,
272279
gptResponseElement,
273280
sendRecipeToUserInboxBtn,
274281
]);
282+
283+
const utterance = new SpeechSynthesisUtterance();
284+
285+
function readRecipe(recipe) {
286+
if(speechSynthesis.paused && speechSynthesis.speaking) {
287+
return speechSynthesis.resume();
288+
}
289+
if(speechSynthesis.speaking) return
290+
utterance.text = recipe;
291+
utterance.rate = speedBtn.value || 1;
292+
speechSynthesis.speak(utterance);
293+
}
294+
function pauseReading() {
295+
if (speechSynthesis.speaking) speechSynthesis.pause();
296+
}
297+
298+
function stopREeading() {
299+
speechSynthesis.resume();
300+
speechSynthesis.cancel();
301+
}
302+
303+
const microphoneBtn = document.querySelector(".fa-microphone");
304+
const pauseBtn = document.querySelector(".fa-pause");
305+
const stopBtn = document.querySelector(".fa-stop");
306+
const speedBtn = document.querySelector("#speed");
307+
308+
console.log(speedBtn);
309+
310+
// speedBtn.addEventListener("input", () => {
311+
// stopREeading();
312+
// readRecipe(utterance.text.substring(currentChar));
313+
314+
// console.log("speed has been incremented")
315+
// })
316+
317+
// utterance.addEventListener("boundary", (e) => {
318+
// currentChar = e.charIndex;
319+
// })
320+
321+
stopBtn.addEventListener("click", stopREeading);
322+
323+
pauseBtn.addEventListener("click", pauseReading);
324+
325+
326+
327+
microphoneBtn.addEventListener("click", () => {
328+
readRecipe(`${textContent}`);
329+
});
275330
})
276331
.catch((error) => {
277332
console.error("Error:", error);

0 commit comments

Comments
 (0)