-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web/TypeScript : Web Speech API Practice (#39)
* Create WebSpeech.html * Create WebSpeech.ts * Upload WebSpeech.js * Upload WebSpeechAPIPractice.PNG * Update README.md * Update /README.md * Update links.json
- Loading branch information
Showing
7 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
|
||
<html lang="ko"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta name="author" content="kimpro82"> | ||
<meta name="date" content="2024-11-14"> | ||
<title>Web Speech API Practice</title> | ||
<script defer src="WebSpeech.js"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>한글 TTS 예제</h1> | ||
<input type="text" id="textInput" placeholder="텍스트를 입력하세요"> | ||
<button id="speakButton">읽기</button> | ||
</body> | ||
|
||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Web Speech API Practice | ||
// Author: kimpro82 | ||
// Date: 2024.11.14 | ||
|
||
/** | ||
* TextToSpeech class for converting text to speech using Web Speech API | ||
*/ | ||
class TextToSpeech { | ||
private synth: SpeechSynthesis; | ||
private utterance: SpeechSynthesisUtterance; | ||
|
||
/** | ||
* Initializes the TextToSpeech instance | ||
*/ | ||
constructor() { | ||
this.synth = window.speechSynthesis; | ||
this.utterance = new SpeechSynthesisUtterance(); | ||
} | ||
|
||
/** | ||
* Converts the given text to speech | ||
* @param text - The text to be spoken | ||
*/ | ||
speak(text: string): void { | ||
// Check if speech is already in progress | ||
if (this.synth.speaking) { | ||
console.error('Speech is already in progress.'); | ||
return; | ||
} | ||
|
||
// Configure the utterance | ||
this.utterance.text = text; | ||
this.utterance.lang = 'ko-KR'; // Set language to Korean | ||
this.utterance.rate = 1; // Set speech rate (1 is normal speed) | ||
this.utterance.pitch = 1; // Set pitch (1 is normal pitch) | ||
|
||
// Start speaking | ||
this.synth.speak(this.utterance); | ||
} | ||
} | ||
|
||
// Wait for the DOM to be fully loaded before executing | ||
document.addEventListener('DOMContentLoaded', () => { | ||
// Get references to DOM elements | ||
const textInput = document.getElementById('textInput') as HTMLInputElement; | ||
const speakButton = document.getElementById('speakButton') as HTMLButtonElement; | ||
|
||
// Create an instance of TextToSpeech | ||
const tts = new TextToSpeech(); | ||
|
||
// Add click event listener to the speak button | ||
speakButton.addEventListener('click', () => { | ||
const text = textInput.value.trim(); | ||
if (text) { | ||
tts.speak(text); // Speak the input text | ||
} else { | ||
alert('Please enter some text.'); // Alert if no text is entered | ||
} | ||
}); | ||
}); |