Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@untemps/react-vocal

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@untemps/react-vocal

React component and hook to initiate a SpeechRecognition session

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
54
decreased by-55%
Maintainers
1
Weekly downloads
 
Created
Source

react-vocal

A React component and hook to initiate a SpeechRecognition session


npm GitHub Workflow Status Codecov

:red_circle:  LIVE DEMO :red_circle:

Disclaimer

The Web Speech API is only supported by few browsers so far (see caniuse). If the API is not available, the Vocal component won't display anything.

This component intends to catch a speech result as soon as possible. This can be a good fit for vocal commands or search field filling. For now on it does not support continuous speech (see Roadmap below).
That means either a result is caught and returned or timeout is reached and the recognition is discarded.
The stop function returned by children-as-function mechanism allows to prematurely discard the recognition before timeout elapses.

Installation

yarn add @untemps/react-vocal

Usage

Vocal component

Basic usage
import Vocal from '@untemps/react-vocal'

const App = () => {
   const [result, setResult] = useState('')
   
   const _onVocalStart = () => {
      setResult('')
   }
   
   const _onVocalResult = (result) => {
      setResult(result)
   }
   
   return (
      <div className="App">
         <span style={{ position: 'relative' }}>
            <Vocal
               onStart={_onVocalStart}
               onResult={_onVocalResult}
               style={{ width: 16, position: 'absolute', right: 10, top: -2 }}
            />
            <input defaultValue={result} style={{ width: 300, height: 40 }} />
         </span>
      </div>
   )
}
Custom component

By default, Vocal displays an icon with two states:

  • Idle
    Idle state
  • Listening
    Listening state

But you can provide your own component.

  • With a simple React element:
import Vocal from '@untemps/react-vocal'

const App = () => {
   return (
      <Vocal>
         <button>Start</button>
      </Vocal>
   )
}

In this case, a onClick handler is automatically attached to the component to start a recognition session.
Only the first direct descendant of Vocal will receive the onClick handler. If you want to use a more complex hierarchy, use the function syntax below.

  • With a function that returns a React element:
import Vocal from '@untemps/react-vocal'

const App = () => {
   return (
      <Vocal>{(start, stop) => (
         <div>
            <button onClick={start}>Start</button>
            <button onClick={stop}>Stop</button>
         </div>
      )}</Vocal>
   )
}

start allows to start a recognition session. stop stops it.

API
PropsTypeDefaultDescription
langstring'en-US'Language understood by the recognition BCP 47 language tag
grammarsSpeechGrammarListnullGrammars understood by the recognition JSpeech Grammar Format
timeoutnumber3000Time in ms to wait before discarding the recognition
styleobjectnullStyles of the root element if className is not specified
classNamestringnullClass of the root element
onStartfuncnullHandler called when the recognition starts
onEndfuncnullHandler called when the recognition ends
onSpeechStartfuncnullHandler called when the speech starts
onSpeechEndfuncnullHandler called when the speech ends
onResultfuncnullHandler called when a result is recognized
onErrorfuncnullHandler called when an error occurs
onNoMatchfuncnullHandler called when no result can be recognized

useVocal hook

Basic usage
import React, { useState } from 'react'
import { useVocal } from '@untemps/react-vocal'
import Icon from './Icon'

const App = () => {
   const [isListening, setIsListening] = useState(false)
   const [result, setResult] = useState('')
   
   const [, {start, subscribe}] = useVocal('fr_FR')
   
   const _onButtonClick = () => {
      setIsListening(true)
      
      subscribe('speechstart', _onVocalStart)
      subscribe('result', _onVocalResult)
      subscribe('error', _onVocalError)
      start()
   }
   
   const _onVocalStart = () => {
      setResult('')
   }

   const _onVocalResult = (result) => {
      setIsListening(false)
      
      setResult(result)
   }

   const _onVocalError = (e) => {
      console.error(e)
   }

   return (
      <div>
         <span style={{ position: 'relative' }}>
            <div
               role="button"
               aria-label="Vocal"
               tabIndex={0}
               style={{ width: 16, position: 'absolute', right: 10, top: 2 }}
               onClick={_onButtonClick}
            >
               <Icon color={isListening ? 'red': 'blue'} />
            </div>
            <input defaultValue={result} style={{ width: 300, height: 40 }} />
         </span>
      </div>
   )
}
Signature
useVocal(lang, grammars)
ArgsTypeDefaultDescription
langstring'en-US'Language understood by the recognition BCP 47 language tag
grammarsSpeechGrammarListnullGrammars understood by the recognition JSpeech Grammar Format
Return value
const [ref, { start, stop, abort, subscribe, unsubscribe, clean }]
ArgsTypeDescription
refRefReact ref to the SpeechRecognitionWrapper instance
startfuncFunction to start the recognition
stopfuncFunction to stop the recognition
abortfuncFunction to abort the recognition
subscribefuncFunction to subscribe to recognition events
unsubscribefuncFunction to unsubscribe to recognition events
cleanfuncFunction to clean subscription to recognition events

Browser support flag

Basic usage
import Vocal, {isSupported} from '@untemps/react-vocal'

const App = () => {
   return isSupported ? (
      <Vocal />
   ) : (
      <p>Your browser does not support Web Speech API</p>
   )
}

Events

EventsDescription
audioendFired when the user agent has finished capturing audio for recognition
audiostartFired when the user agent has started to capture audio for recognition
endFired when the recognition service has disconnected
errorFired when a recognition error occurs
nomatchFired when the recognition service returns a final result with no significant recognition
resultFired when the recognition service returns a result
soundendFired when any sound — recognisable or not — has stopped being detected
soundstartFired when any sound — recognisable or not — has been detected
speechendFired when speech recognized by the recognition service has stopped being detected
speechstartFired when sound recognized by the recognition service as speech has been detected
startfired when the recognition service has begun listening to incoming audio

Notes

The process to grant microphone access permissions is automatically managed by the hook (internally used by the Vocal component).

Development

The component can be served for development purpose on http://localhost:10001/ using:

yarn dev

Contributing

Contributions are warmly welcomed:

  • Fork the repository
  • Create a feature branch (preferred name convention: [feature type]_[imperative verb]-[description of the feature])
  • Develop the feature AND write the tests (or write the tests AND develop the feature)
  • Commit your changes using Angular Git Commit Guidelines
  • Submit a Pull Request

Roadmap

  • Add a connector management to plug external speech-to-text services in
  • Support continuous speech

Keywords

FAQs

Package last updated on 13 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc