The react-native Baidu voice library provides voice recognition, voice wake-up and voice synthesis interfaces. react-native百度语音库,提供语音识别,语音唤醒以及语音合成接口。

Overview

react-native-baidu-asr

react-native-baidu-asr react-native-baidu-asr issues License: MIT semantic-release

react-native-baidu-asr It is a Baidu speech library under React Native, which can perform speech recognition, speech wake-up and speech synthesis.

English | 简体中文

Preview

Preview Preview Preview

Support

  • React Native >= 0.47.0
  • Android

Currently, the iOS platform is not implemented. I will fill it up when I have time.

Install

  • RN >= 0.60
  1. yarn add react-native-baidu-asr
  • RN < 0.60
  1. yarn add react-native-baidu-asr

  2. react-native link react-native-baidu-asr

Usage

The first is that you have to go to the Baidu Voice Console to create an application, get authentication information: AppID, API Key, Secret Key.

  • Speech Recognition
import {
  BaiduAsr,
  StatusCode,
  IBaseData,
  RecognizerResultError,
  RecognizerResultData,
  VolumeData
} from 'react-native-baidu-asr';

// Initialize Baidu speech engine
BaiduAsr.init({
  APP_ID: 'Your authentication information AppID',
  APP_KEY: 'Your authentication information API Key',
  SECRET: 'Your authentication information Secret Key',
});

// Processing recognition results
this.resultListener = BaiduAsr.addResultListener(this.onRecognizerResult);
// Handling wrong results
this.errorListener = BaiduAsr.addErrorListener(this.onRecognizerError);
// Processing volume
this.volumeListener = BaiduAsr.addAsrVolumeListener(this.onAsrVolume);

// Start speech recognition
// For more input parameters, please refer to Baidu Voice Document
// https://ai.baidu.com/ai-doc/SPEECH/bkh07sd0m#asr_start-%E8%BE%93%E5%85%A5%E4%BA%8B%E4%BB%B6%E5%8F%82%E6%95%B0
BaiduAsr.start({
  // Long speech
  VAD_ENDPOINT_TIMEOUT: 0,
  BDS_ASR_ENABLE_LONG_SPEECH: true,
  // Disable punctuation
  DISABLE_PUNCTUATION: true,
});
  • Voice wake

The first is to export wake word , Pre-defined wake words and custom wake words, both need to be exported and used by the wake word evaluation tool.

import { BaiduWakeUp } from 'react-native-baidu-asr';

// Initialize Baidu speech engine
BaiduAsr.init({
  APP_ID: 'Your authentication information AppID',
  APP_KEY: 'Your authentication information API Key',
  SECRET: 'Your authentication information Secret Key',
});

// Wake up result
this.resultListener = BaiduWakeUp.addResultListener(this.onWakeUpResult);
// Handling wrong results
this.errorListener = BaiduWakeUp.addErrorListener(this.onWakeUpError);

// Start voice wake up
// For more input parameters, please refer to Baidu Voice Document
// https://ai.baidu.com/ai-doc/SPEECH/bkh07sd0m#wakeup_start-%E8%BE%93%E5%85%A5%E4%BA%8B%E4%BB%B6%E5%8F%82%E6%95%B0
BaiduWakeUp.start({
  // Indicates that the WakeUp.bin file is defined in the assets directory
  WP_WORDS_FILE: 'assets:///WakeUp.bin',
});
  • Speech synthesis

The authentication information of speech synthesis is placed in auth.properties in the assets directory, please refer to the example.

Then if you need to compile above api level 28, you also need to modify AndroidManifest.xml,

<application
        android:name=".MainApplication"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">

    <!-- Add this sentence. Support api level 28 and above compilation-->
    <uses-library
            android:name="org.apache.http.legacy"
            android:required="false"/>

    // ...

</application>
import {
  BaiduSynthesizer,
  SynthesizerData,
  SynthesizerResultData,
  SynthesizerResultError,
} from 'react-native-baidu-asr';

// initialization
BaiduSynthesizer.initialTts();

// Listen for events
this.resultListener = BaiduSynthesizer.addResultListener(
    this.onSynthesizerResult,
);
this.errorListener = BaiduSynthesizer.addErrorListener(
    this.onSynthesizerError,
);

// Speech synthesis
BaiduSynthesizer.speak(
    this.state.text,
    // For more input parameters, please refer to the Baidu documentation https://ai.baidu.com/ai-doc/SPEECH/Pk8446an5
    {
      PARAM_SPEAKER: '1',
    },
    status => {
      console.log('speak --> ', status);
    },
);

// Batch playback
BaiduSynthesizer.batchSpeak(
    [
      '开始批量播放',
      '123456',
      '欢迎使用百度语音',
      '重(chong2)量这个是多音字示例',
    ],
    // For more input parameters, please refer to the Baidu documentation https://ai.baidu.com/ai-doc/SPEECH/Pk8446an5
    {
      PARAM_SPEAKER: '1',
    },
    status => {
      console.log('batchSpeak --> ', status);
    },
);

API

Speech Recognition

Methods

  • BaiduAsr.init(options: InitOptions)

Initialize Baidu speech engine

  • BaiduAsr.start(options: AsrOptions)

Start speech recognition

  • BaiduAsr.stop()

Pause the recording, the SDK will no longer recognize the stopped recording.

  • BaiduAsr.cancel()

Cancel the recording, the SDK will cancel this recognition and return to the original state.

  • BaiduAsr.release()

Release the resource. If you need to use it again next time, you must call the init method to initialize the engine.

Events

The recognition result callback data has a unified format, similar to the api interface return, with code, msg, and data.

IBaseData The data types are as follows:

interface IBaseData<T = any> {
  /**
   * status code
   */
  code: StatusCode,
  /**
   * message
   */
  msg: string,
  /**
   * data
   */
  data: T
}
  • addResultListener(callback: (data: IBaseData<RecognizerResultData | undefined>) => void): EmitterSubscription
    Voice recognition result callback, the event will be triggered continuously during voice recognition,data is of type IBaseData<RecognizerResultData | undefined>,Its value:

    • code:status code
    • msg:message
    • data:Identification data

The data types of data are as follows:

interface RecognizerResultData {
  best_result: string,
  // If there is no accident, the first value is the recognition result
  results_recognition: Array<string>,
  result_type: ResultType,
  origin_result: {
    corpus_no: number,
    err_no: number,
    raf: number,
    result: {
      word: Array<string>
    },
    sn: string
  },
  error: number,
  desc: string
}
  • addErrorListener(callback: (data: IBaseData<RecognizerResultError>) => void): EmitterSubscription
    There is an error in speech recognition. The error message is consistent with the Baidu speech document. Its value:

    • code:status code
    • msg:message
    • data:Wrong data

The data types of data are as follows:

interface RecognizerResultError {
  errorCode: number // Error code comparison Baidu voice document https://ai.baidu.com/ai-doc/SPEECH/qk38lxh1q
  subErrorCode: number
  descMessage: string
}
  • addAsrVolumeListener(listener: (volume: VolumeData) => void): EmitterSubscription
    The volume of speech recognition. This event will be triggered when the recognized speech changes the volume. volume is of type VolumeData, and its value is:

    • volumePercent: Current volume percentage
    • volume: Current volume

Voice wake

Methods

  • BaiduWakeUp.init(options: InitOptions)

Initialize Baidu speech engine

  • BaiduWakeUp.start(options: WakeUpOptions)

Start voice wake up

  • BaiduWakeUp.stop()

End voice wakeup.

  • BaiduWakeUp.release()

Release the resource. If you need to use it again next time, you must call the init method to initialize the engine.

Events

The wake-up result callback data has a unified format, similar to the api interface return, with code, msg, and data.

The data types of IBaseData are as follows:

interface IBaseData<T = any> {
  /**
   * status code
   */
  code: StatusCode,
  /**
   * message
   */
  msg: string,
  /**
   * data
   */
  data: T
}
  • addResultListener(callback: (data: IBaseData<string | undefined>) => void): EmitterSubscription
    Voice wake up result callback, data is IBaseData<string | undefined> type, its value:

    • code:status code
    • msg:message
    • data:Wake word
  • addErrorListener(callback: (data: IBaseData<WakeUpResultError>) => void): EmitterSubscription
    There is an error in voice wake-up. The error message is consistent with the Baidu voice document. Its value:

    • code:status code
    • msg:message
    • data:Wrong data

The data types of data are as follows:

interface WakeUpResultError {
  // Error code You can look up the error code against Baidu voice documents https://ai.baidu.com/ai-doc/SPEECH/qk38lxh1q#%E5%94%A4%E9%86%92%E9%94%99%E8%AF%AF%E7%A0%81
  errorCode: number,
  // wrong information
  errorMessage: string,
  // Original error data returned by Baidu Voice
  result: string
}

Speech synthesis

Methods

  • BaiduSynthesizer.initialTts(options?: ITtsOptions)

Initialize Baidu speech synthesis engine

  • BaiduSynthesizer.speak(text: string, options?: ITtsOptions, callback?: (status: number) => void)

Compose and play

  • BaiduSynthesizer.batchSpeak(textArray: string[], options?: ITtsOptions, callback?: (status: number) => void)

Batch playback.

  • BaiduSynthesizer.pause(callback?: (status: number) => void)

Pause playback. Only takes effect after calling speak

  • BaiduSynthesizer.resume(callback?: (status: number) => void)

Continue playing. It only takes effect after calling speak, and calling pause takes effect

  • BaiduSynthesizer.stop(callback?: (status: number) => void)

Stop the synthesis engine. That is, stop playing, synthesize, and clear the internal synthesis queue.

  • BaiduSynthesizer.release()

Free up resources. Next time you need to use it again, you must call the initialTts method to initialize the engine

Events

The callback data has a unified format, similar to the API interface return, with code, msg, and data.

SynthesizerData数据类型如下:

interface SynthesizerData<T = any> {
  /**
   * status code
   */
  code: SynthesizerStatusCode,
  /**
   * message
   */
  msg: string,
  /**
   * data
   */
  data: T
}
  • addResultListener(callback: (data: SynthesizerData<SynthesizerResultData | string | undefined>) => void): EmitterSubscription
    Synthesis result callback, data is SynthesizerData<SynthesizerResultData | string | undefined> type, and its value:

    • code:status code
    • msg:message
    • data:Callback data

The data type of SynthesizerResultData is as follows:

// There are many states in the synthesis process, from initialization to synthesis to the end of playback, so the data is actually indeterminate.
interface RecognizerResultData {
  // utterance Id
  utteranceId?: string
  // Synthesis progress or playback progress
  progress?: number
}
  • addErrorListener(callback: (data: SynthesizerData<SynthesizerResultError>) => void): EmitterSubscription
    There is an error in speech synthesis. The error message is consistent with the Baidu speech document. Its value:

    • code:status code
    • msg:message
    • data:Wrong data

The data types of data are as follows:

interface SynthesizerResultError {
  // utterance Id
  utteranceId: string
  // Error code View the Baidu document in detail https://ai.baidu.com/ai-doc/SPEECH/qk844cpcs
  code: number
  // wrong description
  description: string
}

Contribute

Looking forward to making relevant suggestions, contributions are welcome, thank you star.

Github

License

MIT License

You might also like...

This is an android library to represent password strength.

This is an android library to represent password strength.

PasswordStrengthView This is an android library to represent password strength. Preview How to use? Add maven to your project gradle file allprojects

Jan 3, 2022

Library to easily configure API Key authentication in (parts of) your Spring Boot Application

42 API Key Authentication A library to easily configure API Key authentication in (parts of) your Spring Boot Application. Features Easily configure A

Dec 8, 2021

ByteSkriptQuery - A library for deploying ByteSkript as a backend web technology.

ByteSkriptQuery - A library for deploying ByteSkript as a backend web technology.

ByteSkriptQuery A language library for ByteSkript that allows it to be deployed as a backend web language. Not only does this allow the creation of ad

Jan 4, 2022

A Twitter-API library JAVA

Tweety A Twitter-API library for JAVA. Code for Authorization (Oauth 1) can be found here :Authorization This api conta

Apr 26, 2022

Java Secure Cookie Library

Java library for security cookies, client-side pieces of data protected from reading and modifications by client with strong cryptography

Oct 9, 2022

Open Source Identity and Access Management For Modern Applications and Services

Keycloak Keycloak is an Open Source Identity and Access Management solution for modern Applications and Services. This repository contains the source

Jan 5, 2023

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

Sign Language Recognition App This application can recognize the sign language alphabets and help people who do not understand sign language to commun

Oct 7, 2021

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

🎨 JAP 是什么? JAP 是一款开源的登录中间件,基于模块化设计,并且与业务高度解耦,使用起来非常灵活,开发者可以毫不费力地将 JAP 集

Dec 1, 2022

Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE)

CryptographicUtilities Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE) See class "/src/main/java/de/soderer/utilities/crypt

Dec 19, 2021
Comments
  • react-native版本0.7在装完库后运行报错

    react-native版本0.7在装完库后运行报错

    Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/env Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

    opened by caixukunngmn 2
  • 唤醒错误  {

    唤醒错误 {"code": 9, "data": {"errorCode": 11, "errorMessa ge": "", "result": "{\"error\":11,\"desc\":\"Wakeup engine has no license\",\"sub_error\":11002}"}, "msg": "唤醒错误"}

    image

    LOG 唤醒错误 {"code": 9, "data": {"errorCode": 11, "errorMessa ge": "", "result": "{"error":11,"desc":"Wakeup engine has no license","sub_error":11002}"}, "msg": "唤醒错误"}

    opened by shuidaan 2
  • The automated release is failing 🚨

    The automated release is failing 🚨

    :rotating_light: The automated release from the master branch failed. :rotating_light:

    I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

    You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.

    Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

    Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

    If you are not sure how to resolve this, here are some links that can help you:

    If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


    No npm token specified.

    An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

    Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


    Good luck with your project ✨

    Your semantic-release bot :package::rocket:

    semantic-release 
    opened by github-actions[bot] 1
Releases(v1.3.1)
Owner
dengweibin
https://gitee.com/gdoudeng
dengweibin
AES-GCM encryption/decryption for React Native

react-native-aes-gcm-crypto AES-GCM encryption/decryption for React Native Requirements iOS >= 13.0 Android >= 26 Installation npm install react-nativ

Takuya Matsuyama 201 Dec 2, 2022
Spring-react-security - 🌶 Spring Security & React 🌶

Spring-react-security - ?? Spring Security & React ??

KimJunhan 2 Mar 28, 2022
Engin Demiroğun düzenlemiş olduğu (Java & React) Yazılım Geliştirici Yetiştirme Kampında yapmış olduğum ödevleri içermektedir.

Java-React-Yazilim-Gelistirici-Yetistirme-Kampi-Odevler Engin Demiroğun düzenlemiş olduğu (Java & React) Yazılım Geliştirici Yetiştirme Kampında yapmı

Baran Emre Türkmen 2 Apr 26, 2022
Kodlamaio Yazılım Geliştirici Yetiştirme Kampı (JAVA + REACT) ödev listesidir.

JavaHomeworks Kodlamaio Yazılım Geliştirici Yetiştirme Kampı (JAVA + REACT) ödev listesidir. JavaRecapDemo1 kodlama.io ileri java kampı 2. gün ödevidi

Rahim Cubuk 3 May 10, 2021
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).

OTP-Java A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP). Table of Contents Features Ins

Bastiaan Jansen 106 Dec 30, 2022
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).

OTP-Java A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP). Table of Contents Features Ins

Bastiaan Jansen 106 Dec 30, 2022
Java binding to the Networking and Cryptography (NaCl) library with the awesomeness of libsodium

kalium - Java binding to the Networking and Cryptography (NaCl) library A Java binding to Networking and Cryptography library by Daniel J. Bernstein.

Bruno Oliveira da Silva 206 Oct 5, 2022
Password4j is a user-friendly cryptographic library that supports Argon2, Bcrypt, Scrypt, PBKDF2 and various cryptographic hash functions.

Password4j is a Java user-friendly cryptographic library for hashing and checking passwords with different Key derivation functions (KDFs) and Cryptog

Password4J 246 Jan 5, 2023
A library for bypassing all of Java's security mechanisms, visibility checks, and encapsulation measures via the JNI API

Narcissus: thwart strong encapsulation in JDK 16+ Narcissus is a JNI native code library that provides a small subset of the Java reflection API, whil

ToolFactory 29 Nov 3, 2022
An authorization library that supports access control models like ACL, RBAC, ABAC in Java

jCasbin News: still worry about how to write the correct jCasbin policy? Casbin online editor is coming to help! Try it at: http://casbin.org/editor/

Casbin 2k Dec 30, 2022