It sounds like you want to create a simple web-based voice generator that uses HTML, CSS, and JavaScript, allowing users to input text and adjust voice parameters like speed and pitch. You also want to handle cases when voice synthesis is not supported, provide an option to export the generated speech as a WAV file, and include a sharing feature. Plus, you mention something about "#/" — possibly a hash-based routing or UI state indicator.
Below is a complete example implementing these features:
Text input for speech text
Controls for speed and pitch
Button to play speech
Button to export/save as WAV
Notification if speech synthesis is unsupported
Simple share option (copy shareable URL with current text/pitch/speed encoded via hash #/)
Basic styling with CSS
Complete Example
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, Share</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 2rem auto;
padding: 1rem;
background: #f3f4f6;
}
label {
display: block;
margin-top: 1rem;
}
input[type="range"] {
width: 100%;
}
textarea {
width: 100%;
height: 100px;
font-size: 1rem;
margin-top: 0.5rem;
padding: 0.5rem;
}
button {
margin-top: 1rem;
margin-right: 0.5rem;
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
}
#status {
margin-top: 1rem;
color: #e53e3e;
}
#shareLink {
width: 100%;
margin-top: 0.5rem;
font-size: 0.9rem;
padding: 0.5rem;
}
</style>
</head>
<body>
<h1>Voice Generator</h1>
<label for="textInput">Text to Speak:</label>
<textarea id="textInput" placeholder="Enter text here..."></textarea>
<label for="rate">Speed (Rate): <span id="rateValue">1</span></label>
<input type="range" id="rate" min="0.5" max="2" step="0.1" value="1" />
<label for="pitch">Pitch: <span id="pitchValue">1</span></label>
<input type="range" id="pitch" min="0" max="2" step="0.1" value="1" />
<div>
<button id="speakBtn">Speak</button>
<button id="exportBtn">Export WAV</button>
<button id="shareBtn">Copy Share URL</button>
</div>
<input type="text" id="shareLink" readonly placeholder="Share URL will appear here..." />
<div id="status"></div>
<script>
// Check for speech synthesis support
if (!('speechSynthesis' in window)) {
document.getElementById('status').textContent = 'Sorry, your browser does not support Speech Synthesis.';
}
// Elements
const textInput = document.getElementById('textInput');
const rateInput = document.getElementById('rate');
const pitchInput = document.getElementById('pitch');
const speakBtn = document.getElementById('speakBtn');
const exportBtn = document.getElementById('exportBtn');
const shareBtn = document.getElementById('shareBtn');
const status = document.getElementById('status');
const rateValue = document.getElementById('rateValue');
const pitchValue = document.getElementById('pitchValue');
const shareLink = document.getElementById('shareLink');
rateInput.addEventListener('input', () => {
rateValue.textContent = rateInput.value;
updateHash();
});
pitchInput.addEventListener('input', () => {
pitchValue.textContent = pitchInput.value;
updateHash();
});
textInput.addEventListener('input', updateHash);
// Speak text using Web Speech API
function speakText(text, rate, pitch) {
if (!('speechSynthesis' in window)) {
status.textContent = 'Speech synthesis not supported.';
return;
}
if (speechSynthesis.speaking) {
speechSynthesis.cancel(); // Cancel if currently speaking
}
if (text.length > 0) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = rate;
utterance.pitch = pitch;
utterance.onend = () => (status.textContent = 'Speech finished.');
utterance.onerror = e => (status.textContent = 'Speech error: ' + e.error);
status.textContent = 'Speaking...';
speechSynthesis.speak(utterance);
}
}
// Export speech to WAV (approximation via recording audio output)
// Unfortunately, SpeechSynthesis API doesn't give raw audio streams,
// so we need to do a workaround: use Web Audio API + SpeechSynthesisUtterance won't output audio blob.
// Alternative: Use Speech Synthesis to play speech and record audio output from speakers with MediaStream and AudioContext.
// Note: This method requires user microphone permission or loopback, which typically is not trivial or possible in most browsers.
// Instead, we can synthesize speech with Google's Text-To-Speech or other APIs offline, but that's beyond pure client side.
// So, here is a workaround using Google Cloud TTS or similar is needed for real WAV export.
// Alternatively, we create WAV from synthesized speech audio buffer using SpeechSynthesis API + Web Audio API is not feasible directly.
// As a demo: We create a WAV with our own text to speech using offline processing (using external library) - but it's complex.
// For this example, we will just create silent WAV with text echoed in console.
// This button will alert that export is not supported for now.
// To do better: use external services or record audio output externally.
// Sharing URL with hash encoding state
function updateHash() {
const text = encodeURIComponent(textInput.value);
const rate = rateInput.value;
const pitch = pitchInput.value;
location.hash = `#/text=${text}&rate=${rate}&pitch=${pitch}`;
shareLink.value = location.href;
}
// Parse hash and restore UI state
function parseHash() {
const hash = location.hash;
if (hash.startsWith('#/')) {
const params = hash.slice(2).split('&');
const state = {};
params.forEach(param => {
const [key, val] = param.split('=');
if (key && val !== undefined) {
state[key] = decodeURIComponent(val);
}
});
if (state.text !== undefined) textInput.value = state.text;
if (state.rate !== undefined) rateInput.value = state.rate;
if (state.pitch !== undefined) pitchInput.value = state.pitch;
rateValue.textContent = rateInput.value;
pitchValue.textContent = pitchInput.value;
shareLink.value = location.href;
}
}
// Call parseHash on load
window.addEventListener('load', parseHash);
// Update hash when inputs change
window.addEventListener('hashchange', parseHash);
speakBtn.addEventListener('click', () => {
status.textContent = '';
speakText(textInput.value, rateInput.value, pitchInput.value);
});
exportBtn.addEventListener('click', () => {
alert('Exporting speech as WAV is not supported directly in this demo due to browser limitations.');
});
shareBtn.addEventListener('click', () => {
if (navigator.clipboard) {
navigator.clipboard.writeText(shareLink.value)
.then(() => {
status.textContent = 'Share URL copied to clipboard!';
})
.catch(err => {
status.textContent = 'Failed to copy: ' + err;
});
} else {
status.textContent = 'Clipboard API not supported.';
}
});
// Initialize share link on load
updateHash();
</script>
</body>
</html>
Explanation:
Basic Voice Generation: Uses the standard Web Speech API SpeechSynthesisUtterance.
Speed and Pitch: Controlled by range sliders updating utterance properties.
Export WAV: The Web Speech API does not provide raw audio data, so exporting WAV client-side is not straightforward without an external TTS API or audio recording. The demo alerts users that this is not supported.
Share URL: Encodes text, pitch, and rate into the URL hash (#/text=...&rate=...&pitch=...) so users can share the current state. When loading the page, it parses the hash and restores previous settings.
Support Check: Warns if the browser does not support speech synthesis.
Copy to Clipboard: Uses Clipboard API to copy the generated share URL.
How You Can Extend
For real WAV exporting of TTS voices, you would need server-side TTS or third-party APIs that return audio files, or use libraries such as tts.js with pre-recorded samples.
You could implement audio recording from the output device using the Web Audio API if the browser and OS allow loopback capture (rare and often requires special permissions).
You could allow voice selection if you want (listing available voices).
If you want me to provide code for some of those extensions or help with something else related, feel free to ask!