Sling Academy
Home/JavaScript/Convert Spoken Words to Text Using JavaScript Web Speech

Convert Spoken Words to Text Using JavaScript Web Speech

Last updated: December 14, 2024

In today's technology-driven world, the ability to convert spoken words to text has become a vital feature for many applications. This is particularly true for user interfaces that require hands-free control. Thankfully, modern browsers have built-in support for speech recognition through the Web Speech API, which allows developers to integrate speech-to-text capabilities directly into web applications using JavaScript. In this article, we'll explore how to leverage the Web Speech API to convert spoken words to text.

Understanding the Web Speech API

The Web Speech API is composed of two interfaces: SpeechRecognition and SpeechSynthesis. For our purpose of converting speech to text, we will focus on the SpeechRecognition interface. This interface allows web applications to receive real-time results of speech recognition, making it possible to transcribe spoken words instantly.

Getting Started with Speech Recognition in JavaScript

Before delving into code, it's important to recognize that the Web Speech API is supported in several browsers, including Google Chrome and Microsoft Edge. However, it's wise always to check for compatibility before using it in production environments. To begin working with the API, we must create an instance of the SpeechRecognition object:

if (window.SpeechRecognition || window.webkitSpeechRecognition) {
  const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  const recognition = new SpeechRecognition();
}

Here, we first check for availability of the API, since this feature may not be present in older or unsupported browsers (such as those running on iOS). We then create an instance of SpeechRecognition. Note the use of window.webkitSpeechRecognition, which provides webkit-prefixed support for the API in some cases.

Configuring Your Speech Recognition Instance

With our instance ready, we can set several properties to customize the behavior of the SpeechRecognition object. For example:

recognition.continuous = true;  // Continue recognizing speech until stopped
recognition.interimResults = true;  // Capture interim results
recognition.lang = 'en-US';  // Set the language to English (United States)

continuous allows the recognition service to keep listening until we manually stop it. interimResults enables us to capture partial hypotheses of the speech recognition. The lang property allows us to define the language to be recognized.

Handling the Speech Recognition Events

The SpeechRecognition object dispatches several events, which you can listen for to handle various states of recognition. The most commonly used events include:

  • start: Fired when the recognition service starts listening to incoming audio.
  • end: Fired after the recognition service stops listening.
  • result: Fired when a result is generated and available for further processing.
  • error: Fired when an error occurs during the recognition process.
recognition.onstart = function() {
  console.log('Speech recognition started. Please speak into the microphone.');
};

recognition.onresult = function(event) {
  const transcript = event.results[0][0].transcript;
  console.log("Recognized text: " + transcript);
};

recognition.onerror = function(event) {
  console.error('Speech recognition error detected: ' + event.error);
};

recognition.onend = function() {
  console.log('Speech recognition service disconnected.');
};

Starting and Stopping Speech Recognition

To start listening, you simply call the start method on your recognition instance:

recognition.start();

Stopping is just as simple with the use of:

recognition.stop();

Practical Use Cases of Speech Recognition

Speech recognition can be used in various applications such as voice-driven navigation, hands-free text input, real-time transcription services, and more. Let's look at a simple HTML interface integrated with our JavaScript code to input text into a text field through voice commands:


Start Recognition


  // Existing JavaScript recognition code...
  document.getElementById('start-btn').onclick = function() {
    recognition.start();
  };

  recognition.onresult = function(event) {
    document.getElementById('text-input').value = event.results[0][0].transcript;
  };

By evolving the simple interface, you can easily create a web application that inputs spoken words as text.

Conclusion

Converting spoken words to text using the JavaScript Web Speech API is an excellent way to enhance your application with voice-recognition capabilities. By following the guidelines and examples outlined in this article, you can create innovative and accessible user experiences. Explore additional options and features provided by the Web Speech API as you integrate it into your applications. Keep in mind the security and privacy concerns while handling user audio input by implementing appropriate permissions and evaluations.

Next Article: Create Voice-Activated Interfaces via the Web Speech API in JavaScript

Previous Article: Recognize Speech with the Web Speech API in JavaScript

Series: Web APIs – JavaScript Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration