Skip to content

Commit

Permalink
Step 4: Loading voices from the browser to customize by language
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyfayock committed Feb 29, 2024
1 parent 739214d commit 3d514d2
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/components/Translator.tsx
@@ -1,14 +1,36 @@
"use client";

import { useState } from 'react';
import { useState, useEffect } from 'react';

const Translator = () => {
const [text, setText] = useState<string>();
const [translation, setTranslation] = useState<string>();
const [voices, setVoices] = useState<Array<SpeechSynthesisVoice>>();
const [language, setLanguage] = useState<string>('pt-BR');

const isActive = false;
const isSpeechDetected = false;
const language = 'en-US';

const availableLanguages = Array.from(new Set(voices?.map(({ lang }) => lang))).sort();
const availableVoices = voices?.filter(({ lang }) => lang === language);
const activeVoice =
availableVoices?.find(({ name }) => name.includes('Google'))
|| availableVoices?.find(({ name }) => name.includes('Luciana'))
|| availableVoices?.[0];

useEffect(() => {
const voices = window.speechSynthesis.getVoices();
if ( Array.isArray(voices) && voices.length > 0 ) {
setVoices(voices);
return;
}
if ( 'onvoiceschanged' in window.speechSynthesis ) {
window.speechSynthesis.onvoiceschanged = function() {
const voices = window.speechSynthesis.getVoices();
setVoices(voices);
}
}
}, []);

function handleOnRecord() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
Expand All @@ -30,6 +52,9 @@ const Translator = () => {
setTranslation(results.text);

const utterance = new SpeechSynthesisUtterance(results.text);
if ( activeVoice ) {
utterance.voice = activeVoice;
};
window.speechSynthesis.speak(utterance);
}

Expand Down Expand Up @@ -69,10 +94,16 @@ const Translator = () => {
<form>
<div>
<label className="block text-zinc-500 text-[.6rem] uppercase font-bold mb-1">Language</label>
<select className="w-full text-[.7rem] rounded-sm border-zinc-300 px-2 py-1 pr-7" name="language">
<option value="en-US">
English (en-US)
</option>
<select className="w-full text-[.7rem] rounded-sm border-zinc-300 px-2 py-1 pr-7" name="language" value={language} onChange={(event) => {
setLanguage(event.currentTarget.value);
}}>
{availableLanguages.map((language) => {
return (
<option key={language} value={language}>
{ language }
</option>
)
})}
</select>
</div>
</form>
Expand Down

0 comments on commit 3d514d2

Please sign in to comment.