react-native-tts vs react-speech-recognition
Text-to-Speech and Speech Recognition
react-native-ttsreact-speech-recognition

Text-to-Speech and Speech Recognition

Text-to-Speech (TTS) and Speech Recognition are two distinct technologies that enable voice interaction with applications. TTS converts written text into spoken words, allowing apps to read content aloud, which is useful for accessibility, language learning, and hands-free operation. On the other hand, Speech Recognition transcribes spoken language into text, enabling voice commands, dictation, and interactive voice applications. Both technologies enhance user experience by providing auditory and voice-driven interfaces, making applications more interactive and accessible.

Npm Package Weekly Downloads Trend

3 Years

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-native-tts0692162 kB1342 years agoMIT
react-speech-recognition0-65.8 kB-a year agoMIT

Feature Comparison: react-native-tts vs react-speech-recognition

Core Functionality

  • react-native-tts:

    react-native-tts provides text-to-speech capabilities, allowing applications to vocalize written content. It supports multiple languages and voices, and developers can customize the speech rate, pitch, and volume. The package is useful for enhancing accessibility, creating language learning tools, or adding a vocal element to any app.

  • react-speech-recognition:

    react-speech-recognition offers speech recognition capabilities, converting spoken language into text. It supports continuous and interim results, making it suitable for real-time applications. The package is highly customizable, allowing developers to define commands, handle multiple languages, and integrate voice recognition seamlessly into their applications.

Accessibility

  • react-native-tts:

    react-native-tts significantly enhances accessibility by providing auditory feedback for text content. It is particularly beneficial for visually impaired users, as it allows them to hear written information. The ability to customize voice and speech parameters further improves the experience, making it more inclusive.

  • react-speech-recognition:

    react-speech-recognition improves accessibility by enabling hands-free interaction with applications. It allows users to control apps and input text using their voice, which is especially helpful for individuals with mobility impairments. The package’s support for multiple languages and customizable commands makes it versatile and user-friendly.

Customization

  • react-native-tts:

    react-native-tts allows for customization of voice, pitch, rate, and volume, providing flexibility in how text is spoken. Developers can choose from different voices and adjust parameters to create a more personalized experience. However, the extent of customization may vary depending on the platform and available voice resources.

  • react-speech-recognition:

    react-speech-recognition offers extensive customization for speech recognition, including the ability to define specific commands, adjust language settings, and handle interim results. Developers can create highly interactive and responsive voice interfaces by customizing the recognition process to suit their application’s needs.

Integration

  • react-native-tts:

    react-native-tts integrates easily with React Native applications, providing a simple API for text-to-speech functionality. It works seamlessly across iOS and Android platforms, allowing developers to implement TTS features with minimal effort. The package also supports integration with accessibility features, enhancing its usability.

  • react-speech-recognition:

    react-speech-recognition integrates smoothly with React applications, offering a straightforward API for implementing speech recognition. It works well with both functional and class components, making it versatile for different coding styles. The package is designed to be easy to use, with clear documentation and examples to help developers implement voice recognition quickly.

Ease of Use: Code Examples

  • react-native-tts:

    Text-to-Speech Example with react-native-tts

    import React from 'react';
    import { View, Button } from 'react-native';
    import Tts from 'react-native-tts';
    
    const App = () => {
      const speak = () => {
        Tts.speak('Hello, this is a text-to-speech example!');
      };
    
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Button title="Speak" onPress={speak} />
        </View>
      );
    };
    
    export default App;
    
  • react-speech-recognition:

    Speech Recognition Example with react-speech-recognition

    import React from 'react';
    import { useSpeechRecognition } from 'react-speech-recognition';
    import { Button, Text } from 'react-native';
    
    const SpeechToText = () => {
      const { transcript, listening, resetTranscript } = useSpeechRecognition();
    
      const startListening = () => {
        // Start listening for speech
      };
    
      return (
        <View style={{ padding: 20 }}>
          <Button title="Start Listening" onPress={startListening} />
          <Text>Listening: {listening ? 'Yes' : 'No'}</Text>
          <Text>Transcript: {transcript}</Text>
          <Button title="Reset" onPress={resetTranscript} />
        </View>
      );
    };
    
    export default SpeechToText;
    

How to Choose: react-native-tts vs react-speech-recognition

  • react-native-tts:

    Choose react-native-tts if your application needs to convert text into speech. This package is ideal for accessibility features, language learning apps, or any application that requires vocalizing text content. It supports multiple languages and allows customization of voice, pitch, and rate.

  • react-speech-recognition:

    Select react-speech-recognition if you need to implement speech-to-text functionality. This package is perfect for applications that require voice commands, dictation, or interactive voice interfaces. It supports continuous listening and can handle multiple languages, making it versatile for various use cases.

README for react-native-tts

React Native TTS

React Native TTS is a text-to-speech library for React Native on iOS, Android and Windows.

Documentation

Install

npm install --save react-native-tts
react-native link react-native-tts

Usage

Imports

import Tts from 'react-native-tts';

Windows

  1. In windows/myapp.sln add the RNTTS project to your solution:

    • Open the solution in Visual Studio 2019
    • Right-click Solution icon in Solution Explorer > Add > Existing Project
    • Select node_modules\react-native-tts\windows\RNTTS\RNTTS.vcxproj
  2. In windows/myapp/myapp.vcxproj add a reference to RNTTS to your main application project. From Visual Studio 2019:

    • Right-click main application project > Add > Reference...
    • Check RNTTS from Solution Projects.
  3. In pch.h add #include "winrt/RNTTS.h".

  4. In app.cpp add PackageProviders().Append(winrt::RNTTS::ReactPackageProvider()); before InitializeComponent();.

Speaking

Add utterance to TTS queue and start speaking. Returns promise with utteranceId.

Tts.speak('Hello, world!');

Additionally, speak() allows to pass platform-specific options.

// IOS
Tts.speak('Hello, world!', {
  iosVoiceId: 'com.apple.ttsbundle.Moira-compact',
  rate: 0.5,
});
// Android
Tts.speak('Hello, world!', {
  androidParams: {
    KEY_PARAM_PAN: -1,
    KEY_PARAM_VOLUME: 0.5,
    KEY_PARAM_STREAM: 'STREAM_MUSIC',
  },
});

For more detail on androidParams properties, please take a look at official android documentation. Please note that there are still unsupported key with this wrapper library such as KEY_PARAM_SESSION_ID. The following are brief summarization of currently implemented keys:

  • KEY_PARAM_PAN ranges from -1 to +1.

  • KEY_PARAM_VOLUME ranges from 0 to 1, where 0 means silence. Note that 1 is a default value for Android.

  • For KEY_PARAM_STREAM property, you can currently use one of STREAM_ALARM, STREAM_DTMF, STREAM_MUSIC, STREAM_NOTIFICATION, STREAM_RING, STREAM_SYSTEM, STREAM_VOICE_CALL,

The supported options for IOS are:

  • iosVoiceId which voice to use, check voices() for available values
  • rate which speech rate this line should be spoken with. Will override default rate if set for this utterance.

Stop speaking and flush the TTS queue.

Tts.stop();

Waiting for initialization

On some platforms it could take some time to initialize TTS engine, and Tts.speak() will fail to speak until the engine is ready.

To wait for successfull initialization you could use getInitStatus() call.

Tts.getInitStatus().then(() => {
  Tts.speak('Hello, world!');
});

Ducking

Enable lowering other applications output level while speaking (also referred to as "ducking").

(not supported on Windows)

Tts.setDucking(true);

List Voices

Returns list of available voices

(not supported on Android API Level < 21, returns empty list)

Tts.voices().then(voices => console.log(voices));

// Prints:
//
// [ { id: 'com.apple.ttsbundle.Moira-compact', name: 'Moira', language: 'en-IE', quality: 300 },
// ...
// { id: 'com.apple.ttsbundle.Samantha-compact', name: 'Samantha', language: 'en-US' } ]
Voice fieldDescription
idUnique voice identifier (e.g. com.apple.ttsbundle.Moira-compact)
nameName of the voice (iOS only)
languageBCP-47 language code (e.g. 'en-US')
qualityVoice quality (300 = normal, 500 = enhanced/very high)
latencyExpected synthesizer latency (100 = very low, 500 = very high) (Android only)
networkConnectionRequiredTrue when the voice requires an active network connection (Android only)
notInstalledTrue when the voice may need to download additional data to be fully functional (Android only)

Set default Language

Tts.setDefaultLanguage('en-IE');

Set default Voice

Sets default voice, pass one of the voiceId as reported by a call to Tts.voices()

(not available on Android API Level < 21)

Tts.setDefaultVoice('com.apple.ttsbundle.Moira-compact');

Set default Speech Rate

Sets default speech rate. The rate parameter is a float where where 0.01 is a slowest rate and 0.99 is the fastest rate.

Tts.setDefaultRate(0.6);

There is a significant difference to how the rate value is interpreted by iOS, Android and Windows native TTS APIs. To provide unified cross-platform behaviour, translation is applied to the rate value. However, if you want to turn off the translation, you can provide optional skipTransform parameter to Tts.setDefaultRate() to pass rate value unmodified.

Do not translate rate parameter:

Tts.setDefaultRate(0.6, true);

Set default Pitch

Sets default pitch. The pitch parameter is a float where where 1.0 is a normal pitch. On iOS min pitch is 0.5 and max pitch is 2.0. On Windows, min pitch is 0.0 and max pitch is 2.0.

Tts.setDefaultPitch(1.5);

Controls the iOS silent switch behavior

Platforms: iOS

  • "inherit" (default) - Use the default behavior
  • "ignore" - Play audio even if the silent switch is set
  • "obey" - Don't play audio if the silent switch is set
Tts.setIgnoreSilentSwitch("ignore");

Events

Subscribe to TTS events

Tts.addEventListener('tts-start', (event) => console.log("start", event));
Tts.addEventListener('tts-progress', (event) => console.log("progress", event));
Tts.addEventListener('tts-finish', (event) => console.log("finish", event));
Tts.addEventListener('tts-cancel', (event) => console.log("cancel", event));

Support for multiple TTS engines

Platforms: Android

Functions to list available TTS engines and set an engine to use.

Tts.engines().then(engines => console.log(engines));
Tts.setDefaultEngine('engineName');

Install (additional) language data

Shows the Android Activity to install additional language/voice data.

Tts.requestInstallData();

Troubleshooting

No text to speech engine installed on Android

On Android, it may happen that the Text-to-Speech engine is not (yet) installed on the phone. When this is the case, Tts.getInitStatus() returns an error with code no_engine. You can use the following code to request the installation of the default Google Text to Speech App. The app will need to be restarted afterwards before the changes take affect.

Tts.getInitStatus().then(() => {
  // ...
}, (err) => {
  if (err.code === 'no_engine') {
    Tts.requestInstallEngine();
  }
});

Example

There is an example project which shows use of react-native-tts on Android/iOS/Windows: https://github.com/themostaza/react-native-tts-example

License

The MIT License (MIT)

Copyright © 2016 Anton Krasovsky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.