Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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 Speech Application</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
      line-height: 1.6;
    }
    h1 {
      text-align: center;
      color: #333;
    }
    .container {
      background-color: #f9f9f9;
      border-radius: 10px;
      padding: 20px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }
    textarea {
      width: 100%;
      height: 150px;
      padding: 10px;
      margin-bottom: 15px;
      border-radius: 5px;
      border: 1px solid #ddd;
      resize: vertical;
    }
    .controls {
      display: flex;
      flex-wrap: wrap;
      gap: 15px;
      margin-bottom: 15px;
    }
    .controls > div {
      flex: 1;
      min-width: 200px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    select, input {
      width: 100%;
      padding: 8px;
      border-radius: 5px;
      border: 1px solid #ddd;
    }
    .buttons {
      display: flex;
      gap: 10px;
    }
    button {
      flex: 1;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    button:hover {
      background-color: #45a049;
    }
    button#pauseBtn {
      background-color: #f39c12;
    }
    button#pauseBtn:hover {
      background-color: #e67e22;
    }
    button#stopBtn {
      background-color: #e74c3c;
    }
    button#stopBtn:hover {
      background-color: #c0392b;
    }
    .status {
      text-align: center;
      margin-top: 15px;
      font-style: italic;
      color: #666;
    }
  </style>
</head>
<body>
  <h1>Text to Speech Converter</h1>
  
  <div class="container">
    <textarea id="textInput" placeholder="Enter text to convert to speech...">Hello! This is a text to speech demo. You can change my voice, rate, and pitch using the controls below.</textarea>
    
    <div class="controls">
      <div>
        <label for="voiceSelect">Voice:</label>
        <select id="voiceSelect"></select>
      </div>
      
      <div>
        <label for="rate">Rate: <span id="rateValue">1</span></label>
        <input type="range" id="rate" min="0.5" max="2" value="1" step="0.1">
      </div>
      
      <div>
        <label for="pitch">Pitch: <span id="pitchValue">1</span></label>
        <input type="range" id="pitch" min="0.5" max="2" value="1" step="0.1">
      </div>
    </div>
    
    <div class="buttons">
      <button id="speakBtn">Speak</button>
      <button id="pauseBtn">Pause/Resume</button>
      <button id="stopBtn">Stop</button>
    </div>
    
    <p class="status" id="statusText">Ready</p>
  </div>

  <script>
    // Initialize speech synthesis
    const synth = window.speechSynthesis;
    let voices = [];
    let currentUtterance = null;
    let isPaused = false;

    // DOM elements
    const textInput = document.getElementById('textInput');
    const voiceSelect = document.getElementById('voiceSelect');
    const rate = document.getElementById('rate');
    const rateValue = document.getElementById('rateValue');
    const pitch = document.getElementById('pitch');
    const pitchValue = document.getElementById('pitchValue');
    const speakBtn = document.getElementById('speakBtn');
    const pauseBtn = document.getElementById('pauseBtn');
    const stopBtn = document.getElementById('stopBtn');
    const statusText = document.getElementById('statusText');

    // Populate voice list
    function populateVoiceList() {
      voices = synth.getVoices();
      
      // Clear existing options
      voiceSelect.innerHTML = '';
      
      // Add voices to select element
      voices.forEach((voice, index) => {
        const option = document.createElement('option');
        option.textContent = `${voice.name} (${voice.lang})`;
        option.setAttribute('data-lang', voice.lang);
        option.setAttribute('data-name', voice.name);
        option.value = index;
        voiceSelect.appendChild(option);
      });
    }

    // Check if voices are already loaded (Chrome loads asynchronously)
    if (synth.onvoiceschanged !== undefined) {
      synth.onvoiceschanged = populateVoiceList;
    } else {
      // For browsers that don't fire onvoiceschanged
      populateVoiceList();
    }

    // Update rate and pitch displays
    rate.addEventListener('input', () => {
      rateValue.textContent = rate.value;
    });

    pitch.addEventListener('input', () => {
      pitchValue.textContent = pitch.value;
    });

    // Speak function
    function speak() {
      // Cancel any ongoing speech
      if (synth.speaking) {
        synth.cancel();
      }

      const text = textInput.value.trim();
      
      // Check if there's text to speak
      if (text === '') {
        statusText.textContent = 'Please enter some text to speak.';
        return;
      }

      // Create speech utterance
      currentUtterance = new SpeechSynthesisUtterance(text);
      
      // Set selected voice
      if (voices.length > 0) {
        const selectedVoice = voices[voiceSelect.value];
        currentUtterance.voice = selectedVoice;
      }
      
      // Set rate and pitch
      currentUtterance.rate = parseFloat(rate.value);
      currentUtterance.pitch = parseFloat(pitch.value);
      
      // Add event listeners
      currentUtterance.onstart = () => {
        statusText.textContent = 'Speaking...';
        speakBtn.disabled = true;
      };
      
      currentUtterance.onend = () => {
        statusText.textContent = 'Finished speaking.';
        speakBtn.disabled = false;
        isPaused = false;
      };
      
      currentUtterance.onerror = (event) => {
        statusText.textContent = `Error occurred: ${event.error}`;
        speakBtn.disabled = false;
      };
      
      // Start speaking
      synth.speak(currentUtterance);
    }

    // Event listeners for buttons
    speakBtn.addEventListener('click', speak);
    
    pauseBtn.addEventListener('click', () => {
      if (!synth.speaking) {
        statusText.textContent = 'Nothing to pause.';
        return;
      }
      
      if (isPaused) {
        synth.resume();
        isPaused = false;
        statusText.textContent = 'Resumed speaking.';
      } else {
        synth.pause();
        isPaused = true;
        statusText.textContent = 'Paused speaking.';
      }
    });
    
    stopBtn.addEventListener('click', () => {
      if (synth.speaking) {
        synth.cancel();
        isPaused = false;
        statusText.textContent = 'Stopped speaking.';
        speakBtn.disabled = false;
      } else {
        statusText.textContent = 'Nothing to stop.';
      }
    });
  </script>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console