<!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</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 500px;
margin: 2em auto;
padding: 1em;
background: #f9f9f9;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,.1);
}
label {
display: block;
margin-top: 1em;
}
input[type=text], textarea {
width: 100%;
padding: .6em;
font-size: 1em;
box-sizing: border-box;
}
input[type=range] {
width: 100%;
}
button {
margin-top: 1em;
padding: 0.8em 1.2em;
font-size: 1em;
cursor: pointer;
border: none;
border-radius: 4px;
}
button:disabled {
opacity: 0.5;
cursor: default;
}
#notSupported {
color: red;
font-weight: bold;
margin-top: 1em;
}
#shareLink {
font-size: 0.9em;
margin-top: .5em;
word-break: break-all;
}
</style>
</head>
<body>
<h1>Voice Generator</h1>
<label for="text">Text to Speak:</label>
<textarea id="text" rows="4" placeholder="Enter text here...">Hello world, this is a test.</textarea>
<label for="speed">Speed: <span id="speedValue">1.0</span></label>
<input type="range" id="speed" min="0.1" max="3" step="0.1" value="1.0" />
<label for="pitch">Pitch: <span id="pitchValue">1.0</span></label>
<input type="range" id="pitch" min="0" max="2" step="0.1" value="1.0" />
<button id="playBtn">Play</button>
<button id="exportBtn">Export WAV</button>
<button id="shareBtn">Share</button>
<div id="notSupported" style="display: none;">Sorry, your browser does not support Speech Synthesis.</div>
<div id="shareLink" style="display:none;"></div>
<script>
// Elements:
const textEl = document.getElementById('text');
const speedEl = document.getElementById('speed');
const pitchEl = document.getElementById('pitch');
const speedVal = document.getElementById('speedValue');
const pitchVal = document.getElementById('pitchValue');
const playBtn = document.getElementById('playBtn');
const exportBtn = document.getElementById('exportBtn');
const shareBtn = document.getElementById('shareBtn');
const notSupported = document.getElementById('notSupported');
const shareLink = document.getElementById('shareLink');
// Check Support
const speechSynthesisSupported = ('speechSynthesis' in window && 'SpeechSynthesisUtterance' in window);
if (!speechSynthesisSupported) {
notSupported.style.display = 'block';
playBtn.disabled = true;
exportBtn.disabled = true;
shareBtn.disabled = true;
}
// Update UI display of values
speedEl.addEventListener('input', () => {
speedVal.textContent = speedEl.value;
});
pitchEl.addEventListener('input', () => {
pitchVal.textContent = pitchEl.value;
});
// Play speech function
function playSpeech() {
const utterance = new SpeechSynthesisUtterance(textEl.value);
utterance.rate = parseFloat(speedEl.value);
utterance.pitch = parseFloat(pitchEl.value);
speechSynthesis.cancel(); // cancel existing
speechSynthesis.speak(utterance);
}
playBtn.addEventListener('click', () => {
if (!speechSynthesisSupported) return;
if (textEl.value.trim() === '') {
alert('Please enter some text to speak.');
return;
}
playSpeech();
});
// Export WAV helper - simplified demo: Export silent WAV because real TTS recording is non-trivial
exportBtn.addEventListener('click', () => {
if (!speechSynthesisSupported) {
alert('Speech Synthesis is not supported in your browser.');
return;
}
if (textEl.value.trim() === '') {
alert('Please enter some text to export.');
return;
}
// Using Web Speech API directly cannot generate WAV, so here we simulate with silent WAV file.
// Normally, you'd need server-side TTS or Google Cloud TTS, etc.
alert('Exporting actual TTS audio is limited in browsers.\nThis demo will export a silent WAV file.');
// Generate silent 1-second WAV file (44100Hz, mono)
const sampleRate = 44100;
const durationSeconds = 1;
const numSamples = sampleRate * durationSeconds;
const buffer = new ArrayBuffer(44 + numSamples * 2);
const view = new DataView(buffer);
// Write WAV header
function writeString(view, offset, str) {
for (let i = 0; i < str.length; i++) {
view.setUint8(offset + i, str.charCodeAt(i));
}
}
writeString(view, 0, 'RIFF');
view.setUint32(4, 36 + numSamples * 2, true);
writeString(view, 8, 'WAVE');
writeString(view, 12, 'fmt ');
view.setUint32(16, 16, true); // Subchunk1Size PCM
view.setUint16(20, 1, true); // AudioFormat PCM
view.setUint16(22, 1, true); // NumChannels mono
view.setUint32(24, sampleRate, true); // SampleRate
view.setUint32(28, sampleRate * 2, true); // ByteRate
view.setUint16(32, 2, true); // BlockAlign
view.setUint16(34, 16, true); // BitsPerSample
writeString(view, 36, 'data');
view.setUint32(40, numSamples * 2, true); // Subchunk2Size
// Samples are all zero (silence)
for (let i = 44; i < 44 + numSamples * 2; i += 2) {
view.setInt16(i, 0, true);
}
const blob = new Blob([view], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'output.wav';
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
});
// Share button: copies URL with hash params
shareBtn.addEventListener('click', () => {
const params = new URLSearchParams();
params.set('text', encodeURIComponent(textEl.value));
params.set('speed', speedEl.value);
params.set('pitch', pitchEl.value);
// Location hash with ?params
const shareURL = location.origin + location.pathname + '#/?' + params.toString();
navigator.clipboard.writeText(shareURL).then(() => {
shareLink.textContent = 'Link copied to clipboard! ' + shareURL;
shareLink.style.display = 'block';
}).catch(() => {
alert('Clipboard write failed, here is the link:\n' + shareURL);
});
});
// On page load, parse hash params and populate UI
function loadFromHash() {
const hash = location.hash;
if (!hash.startsWith('#/?')) return;
const paramString = hash.slice(3);
const params = new URLSearchParams(paramString);
const text = params.get('text');
const speed = params.get('speed');
const pitch = params.get('pitch');
if (text !== null) textEl.value = decodeURIComponent(text);
if (speed !== null) {
speedEl.value = speed;
speedVal.textContent = speed;
}
if (pitch !== null) {
pitchEl.value = pitch;
pitchVal.textContent = pitch;
}
}
loadFromHash();
</script>
</body>
</html>