Skip to main content

Command Palette

Search for a command to run...

Accessing the Microphone with JavaScript to Convert Speech to Text

Published
2 min read

In today’s digital age, speech recognition technology has become increasingly popular, allowing users to interact with applications using their voice. This blog will guide you through the steps to access the microphone using JavaScript and convert spoken words into text.

Prerequisites

Before diving into the code, ensure you have a basic understanding of HTML, CSS, and JavaScript. You’ll also need a modern web browser that supports the Web Speech API, such as Chrome or Firefox.

Step 1: Set Up Your HTML Structure

Create a simple HTML page with a button to start the speech recognition process and a placeholder to display the converted text.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speech to Text Converter</title>
</head>
<body>
    <h1>Speech to Text Converter</h1>
    <button id="start-btn">Start Listening</button>
    <p id="output"></p>

    <script src="script.js"></script>
</body>
</html>

Step 2: Implement the JavaScript Code

Now, create a JavaScript file named script.js and add the following code:

// Check for browser support
if ('webkitSpeechRecognition' in window) {
    const recognition = new webkitSpeechRecognition();
    recognition.interimResults = true;

    // Start listening when the button is clicked
    document.getElementById('start-btn').addEventListener('click', () => {
        recognition.start();
    });

    // Capture the result when speech is detected
    recognition.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        document.getElementById('output').innerText = transcript;
    };

    // Handle errors
    recognition.onerror = (event) => {
        console.error('Error occurred in recognition: ' + event.error);
    };
} else {
    console.error('Speech recognition not supported in this browser.');
}

Step 3: Test Your Application

  1. Open the HTML file in a supported web browser.

  2. Click the "Start Listening" button.

  3. Speak clearly into your microphone. The spoken words should appear in the text output area as they are recognized.

Additional Notes

  • Permissions: The browser will prompt for permission to access the microphone. Ensure to allow it for the speech recognition to work.

  • Interim Results: The interimResults property allows the recognition to provide updates while the user is still speaking. You can set this to false if you want results only after the user stops speaking.

  • Error Handling: Implement error handling to manage issues like microphone access denial or network problems.

Conclusion

Using the Web Speech API in JavaScript, you can easily create applications that convert speech to text. This functionality opens up various possibilities for interactive applications, voice commands, and accessibility features.

For more detailed information on the Web Speech API, visit the MDN Web Docs.

Feel free to explore and expand this basic implementation to include more features, such as language options or saving the transcribed text!

More from this blog

Amazon S3 Storage Classes

43 posts