Here's how you can create a text-to-voice website using HTML, JavaScript, and CSS:
- Create three files:
index.html, styles.css, and script.js.
- Copy the following code into each file:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text to Voice</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Text to Voice</h1>
<textarea id="textInput" placeholder="Enter text here..."></textarea>
<button id="speakButton">Speak</button>
<div class="controls">
<label for="rate">Rate:</label>
<input type="range" id="rate" min="0.5" max="2" value="1" step="0.1">
<label for="pitch">Pitch:</label>
<input type="range" id="pitch" min="0" max="2" value="1" step="0.1">
<label for="volume">Volume:</label>
<input type="range" id="volume" min="0" max="1" value="1" step="0.1">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
styles.css:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 600px;
text-align: center;
}
h1 {
color: #333;
}
textarea {
width: 100%;
height: 150px;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
resize: vertical;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.controls {
margin-top: 20px;
}
.controls label {
display: inline-block;
width: 80px;
text-align: right;
margin-right: 10px;
}
.controls input {
width: 200px;
}
script.js:
document.addEventListener('DOMContentLoaded', function() {
const speakButton = document.getElementById('speakButton');
const textInput = document.getElementById('textInput');
const rateInput = document.getElementById('rate');
const pitchInput = document.getElementById('pitch');
const volumeInput = document.getElementById('volume');
speakButton.addEventListener('click', function() {
const text = textInput.value;
if (text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = parseFloat(rateInput.value);
utterance.pitch = parseFloat(pitchInput.value);
utterance.volume = parseFloat(volumeInput.value);
window.speechSynthesis.speak(utterance);
} else {
alert('Please enter some text to speak.');
}
});
});
- Open the
index.html file in a web browser.
This code creates a simple text-to-voice website where users can input text, adjust the speech rate, pitch, and volume, and then click the "Speak" button to have the text read aloud. The Web Speech API is used for the text-to-speech functionality, which is supported in most modern browsers.
Make sure to test this in a browser that supports the Web Speech API, such as Google Chrome or Mozilla Firefox. Some browsers may require user interaction to initiate speech synthesis.
This should give you a functional text-to-voice website. If you need any further customization or have any questions, feel free to ask!