npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-voice-to-text

v1.2.0

Published

A small lib to use the speech recognition api in react

Downloads

26

Readme

react-voice-to-text

react-voice-to-text is a lightweight, easy-to-use React hook that enables speech recognition in your web applications using the Web Speech API. This package allows you to seamlessly integrate voice-to-text functionality, making your application more accessible and interactive.

Features

  • Start and stop speech recognition
  • Receive transcription results in real-time
  • Handle various speech recognition events
  • Customizable options for language, interim results, and alternatives
  • Support for error handling

Installation

To install react-voice-to-text, you can use npm or yarn:

npm install react-voice-to-text

or

yarn add react-voice-to-text

Usage

Here is a basic example of how to use the react-voice-to-text hook in your React component:

import { useState } from 'react';
import {
  useSpeechRecognition,
  isSpeechRecognitionSupported,
} from 'react-voice-to-text';

const VoiceToText = () => {
  const [transcript, setTranscript] = useState('');
  const [history, setHistory] = useState<string[]>([]);

  if (!isSpeechRecognitionSupported()) {
    return <div>Sorry, your browser does not support speech recognition.</div>;
  }

  const { startRecording, stopRecording } = useSpeechRecognition({
    onStart: () => console.log('Started recording'),
    onEnd: (lastText: string, transcriptHistory: string[]) => {
      setTranscript(lastText);
      setHistory(transcriptHistory);
    },
    onStop: () => console.log('Stopped recording'),
    onError: (error) => console.error(error),
    lang: 'en-US',
    interimResults: true,
    maxAlternatives: 1,
  });

  return (
    <div>
      <button onClick={startRecording}>Start Recording</button>
      <button onClick={stopRecording}>Stop Recording</button>
      <div>
        <h3>Transcript</h3>
        <p>{transcript}</p>
      </div>
      <div>
        <h3>History</h3>
        <ul>
          {history.map((text, index) => (
            <li key={index}>{text}</li>
          ))}
        </ul>
      </div>
    </div>
  );
};

export default VoiceToText;

DEMO

You can checkout how the library works in this stackblitz

API

useSpeechRecognition(options: SpeechRecognitionOptions): SpeechRecognitionHandles & { recognition: Recognition }

The useSpeechRecognition hook accepts an options object and returns handles for starting and stopping the recording, along with the recognition instance in case you need to customize the options further.

Options

  • onStart?: Function called when speech recognition starts.
  • onResult?: Function called when a speech recognition result is received.
  • onEnd?: Function called when speech recognition ends. Receives the last recognized text and the history of all recognized texts.
  • onStop?: Function called when speech recognition stops.
  • onError?: Function called when an error occurs during speech recognition.
  • throwOnUnsupported?: Boolean to throw an error if the browser does not support the Web Speech API. Default is false.
  • lang?: Language for speech recognition. Default is 'fr-FR'.
  • interimResults?: Boolean to return interim results. Default is false.
  • maxAlternatives?: Maximum number of recognition alternatives. Default is 1.

Returns

  • startRecording: Function to start the speech recognition.
  • stopRecording: Function to stop the speech recognition.
  • recognition: The recognition instance.

isSpeechRecognitionSupported(): boolean

This function checks if the browser supports the Web Speech API.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.