Node.js SDK

Updated at:

This topic describes how to use the Node.js SDK for short sentence recognition in Intelligent Speech Interaction. It covers how to install the SDK and provides code examples.

Prerequisites

  • Before you use the SDK, read the API reference. For more information, see the API reference.

  • Make sure that a Node.js and npm environment is installed and configured.

  • The SDK supports Node.js v14 and later.

Download and install

  1. You can download and install the SDK.

    Run the following command to download and install the SDK.

    npm install alibabacloud-nls
  2. You can import the SDK.

    Use `require` or `import` in your code to import the SDK.

    const Nls = require('alibabacloud-nls')
    // Nls includes SpeechRecognition, SpeechTranscription, and SpeechSynthesizer.
    // The following code shows how to import the SDK using import.
    //import { SpeechRecognition } from "alibabacloud-nls"
    //import { SpeechTranscription } from "alibabacloud-nls"
    //import { SpeechSynthesizer } from "alibabacloud-nls"

Short sentence recognition

Class: SpeechRecognition

The SpeechRecognition class is used for short sentence recognition.

  • Constructor parameters:

    Parameter

    Type

    Description

    config

    Object

    The connection configuration object.

  • `config` object details:

    Parameter

    Type

    Description

    url

    String

    The service URL.

    token

    String

    The access token. For more information, see Overview of how to obtain a token.

    appkey

    String

    The AppKey of the project. To obtain an AppKey, go to the console.

defaultStartParams()

This method returns a default parameter object. In this object, `format` is set to PCM, `sample_rate` is set to 16000 Hz, and intermediate results, punctuation prediction, and Inverse Text Normalization (ITN) are enabled. After you obtain the default object, you can add or modify parameters as needed. For more information, see the parameter list in the API reference.

  • Parameters: None.

  • Return value:

    An object with the following fields:

    {
        "format": "pcm",
        "sample_rate": 16000,
        "enable_intermediate_result": true,
        "enable_punctuation_predition": true,
        "enable_inverse_text_normalization": true
    }

on(which, handler)

This method sets an event callback.

  • Parameters:

    Parameter

    Type

    Description

    which

    String

    The event name.

    handler

    Function

    The callback function.

    The supported callback events are as follows:

    Event name

    Description

    Number of callback function parameters

    Callback function parameter description

    started

    Short sentence recognition starts.

    1

    String. The start message.

    changed

    Intermediate result of short sentence recognition.

    1

    String. The intermediate result message.

    completed

    Short sentence recognition is complete.

    1

    String. The completion message.

    closed

    The connection is closed.

    0

    None.

    failed

    An error occurred.

    1

    String. The error message.

  • Return value: None.

async start(param, enablePing, pingInterval)

This method starts a short sentence recognition task. The `param` object can be based on the return value of the `defaultStartParams()` method. For details about the parameters, see the API reference.

  • Parameters:

    Parameter

    Type

    Description

    param

    Object

    The parameters for short sentence recognition.

    enablePing

    Boolean

    Specifies whether to automatically send ping requests to the cloud. The default value is false.

    • true: Sends the data.

    • false: The data is not sent.

    pingInterval

    Number

    The interval for sending ping requests. The default value is 6000. Unit: milliseconds.

  • Return value: A Promise object. The `resolve` function is triggered when the `started` event occurs and includes the `started` message. The `reject` function is triggered if an error occurs and includes the exception information.

async close(param)

This method stops the short sentence recognition task.

  • Parameters:

    Parameter

    Type

    Description

    param

    Object

    The parameters to end the short sentence recognition task.

  • Return value:

    A Promise object. The `resolve` function is triggered when the `completed` event occurs and includes the `completed` message. The `reject` function is triggered if an error occurs and includes the exception information.

shutdown()

This method forcibly disconnects the connection.

  • Parameters: None.

  • Return value: None.

sendAudio(data)

This method sends audio data. The audio format must be the same as the format specified in the parameters.

  • Parameters:

    Parameter

    Type

    Description

    data

    Buffer

    The binary audio data.

  • Return value: None.

Code example

"use strict"

const Nls = require("alibabacloud-nls")
const fs = require("fs")
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs))

const URL = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1"
const APPKEY = "Your Appkey"      // To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist
const TOKEN = "Your Token"      // For more information about how to obtain a token, see https://help.aliyun.com/document_detail/450514.html

let audioStream = fs.createReadStream("test1.pcm", {
  encoding: "binary",
  highWaterMark: 1024
})
let b1 = []

audioStream.on("data", (chunk) => {
  let b = Buffer.from(chunk, "binary")
  b1.push(b)
})

audioStream.on("close", async ()=>{
  while (true) {
    let sr = new Nls.SpeechRecognition({
      url: URL,
      appkey:APPKEY,
      token:TOKEN
    })

    sr.on("started", (msg)=>{
      console.log("Client recv started:", msg)
    })

    sr.on("changed", (msg)=>{
      console.log("Client recv changed:", msg)
    })

    sr.on("completed", (msg)=>{
      console.log("Client recv completed:", msg)
    })

    sr.on("closed", () => {
      console.log("Client recv closed")
    })

    sr.on("failed", (msg)=>{
      console.log("Client recv failed:", msg)
    })

    try {
      await sr.start(sr.defaultStartParams(), true, 6000)
    } catch(error) {
      console.log("error on start:", error)
      continue
    }

    try {
      for (let b of b1) {
        if (!sr.sendAudio(b)) {
          throw new Error("send audio failed")
        }
        await sleep(20)
      }
    } catch(error) {
      console.log("sendAudio failed:", error)
      continue
    }

    try {
      console.log("close...")
      await sr.close()
    } catch(error) {
      console.log("error on close:", error)
    }
    await sleep(2000)
  }
})