Certainly! Below is a simple example of a web page using HTML, CSS, and JavaScript that creates a voice generator with controls for speed (rate) and pitch, uses the Web Speech API's speech synthesis to generate speech. It will also attempt to export the speech to a WAV file for download (using the Web Audio API) if possible.
Note:
Browsers do not natively support exporting the Web Speech API speech synthesis output as audio files.
To export to WAV, the code captures audio from a MediaStream generated by an OfflineAudioContext. But since speech synthesis output is not directly accessible as audio stream, this functionality is very limited or requires server-side or third-party APIs.
The following code provides:
Text input
Selectable voice (if available)
Speed and pitch controls
Play voice with speech synthesis
Download spoken phrase as WAV (via recording microphone output, causing "not supported" message if unavailable)
If your browser doesn't support download or speech synthesis, you'll see a message.
Example Code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voice Generator with Speed, Pitch, Export WAV</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 1rem;
max-width: 600px;
margin: auto;
}
input, select, button {
margin: 0.5rem 0;
width: 100%;
padding: 0.5rem;
}
label {
font-weight: bold;
}
#messages {
margin-top: 1rem;
color: red;
}
</style>
</head>
<body>
<h1>Voice Generator</h1>
<label for="voiceSelect">Choose voice:</label>
<select id="voiceSelect"></select>
<label for="textInput">Enter text:</label>
<textarea id="textInput" rows="4" placeholder="Type something...">Hello, this is a voice generator test.</textarea>
<label for="rate">Speed (Rate): <span id="rateValue">1</span></label>
<input type="range" id="rate" min="0.5" max="2" value="1" step="0.1" />
<label for="pitch">Pitch: <span id="pitchValue">1</span></label>
<input type="range" id="pitch" min="0" max="2" value="1" step="0.1" />
<button id="btnSpeak">Speak</button>
<button id="btnDownload">Download as WAV</button>
<div id="messages"></div>
<script>
const synth = window.speechSynthesis;
const voiceSelect = document.getElementById('voiceSelect');
const textInput = document.getElementById('textInput');
const rate = document.getElementById('rate');
const pitch = document.getElementById('pitch');
const btnSpeak = document.getElementById('btnSpeak');
const btnDownload = document.getElementById('btnDownload');
const messages = document.getElementById('messages');
const rateValue = document.getElementById('rateValue');
const pitchValue = document.getElementById('pitchValue');
let voices = [];
function populateVoices() {
voices = synth.getVoices();
voiceSelect.innerHTML = '';
voices.forEach((voice, i) => {
const option = document.createElement('option');
option.textContent = `${voice.name} (${voice.lang})${voice.default ? ' -- DEFAULT' : ''}`;
option.value = i;
voiceSelect.appendChild(option);
});
}
populateVoices();
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoices;
}
rate.addEventListener('input', () => {
rateValue.textContent = rate.value;
});
pitch.addEventListener('input', () => {
pitchValue.textContent = pitch.value;
});
btnSpeak.onclick = () => {
messages.textContent = '';
if (synth.speaking) {
messages.textContent = 'Already speaking...';
return;
}
if (textInput.value.trim() === '') {
messages.textContent = 'Please enter some text.';
return;
}
let utterThis = new SpeechSynthesisUtterance(textInput.value);
utterThis.voice = voices[voiceSelect.value] || null;
utterThis.rate = parseFloat(rate.value);
utterThis.pitch = parseFloat(pitch.value);
utterThis.onerror = (e) => {
messages.textContent = 'An error occurred during speech synthesis: ' + e.error;
};
synth.speak(utterThis);
};
/*
* Download as WAV functionality:
*
* Unfortunately, the Web Speech API does not provide direct access to raw audio buffers.
* One way to record TTS output is to play it through the Web Audio API and record the output.
* This example tries to record the audio output using MediaRecorder.
* However, for most browsers, recording the speech synthesis output directly is NOT supported.
*
* This button will show a "Not supported" message.
*/
btnDownload.onclick = async () => {
messages.textContent = '';
if (!('MediaRecorder' in window)) {
messages.textContent = 'Export WAV not supported in this browser.';
return;
}
if (textInput.value.trim() === '') {
messages.textContent = 'Please enter some text before downloading.';
return;
}
messages.textContent = 'Export WAV is not supported in this demo due to browser limitations.';
// Below would be a complex workaround requiring server-side or external services.
};
</script>
</body>
</html>
Explanation:
The page lists all available voices.
User types text, adjusts speed and pitch with sliders.
Clicking Speak will use speech synthesis to speak.
Download WAV is not implemented due to browser API limitations and will show a message.
The UI updates to show any error messages or warnings.
If you want native audio export from TTS, you would typically need:
Server-side TTS services that provide audio files (Google Cloud TTS, Amazon Polly, etc), or
A browser extension, or
Complex audio routing (e.g., capture system audio), which is beyond typical webpage capabilities at this time.
Let me know if you'd like me to help with a server-based audio generation or other options!