Node.js SDK

Updated at:

Use the Node.js SDK for real-time speech recognition to convert audio to text. This topic provides installation instructions, a complete example, and reference information about connection settings, parameters, and events.

Prerequisites

Install the SDK

Install the SDK in your project directory:

npm install alibabacloud-nls

The complete example uses CommonJS. In an ES module project, use a named import:

import { SpeechTranscription } from "alibabacloud-nls";

Code example

This example uses alibabacloud-nls 1.0.4 to recognize a local audio file once. Before running the example, set NLS_APP_KEY and NLS_TOKEN in the runtime environment to your project Appkey and access token.

Download the official SDK's sample audio test1.pcm to the program's working directory. The file contains Chinese numbers in 16 kHz, mono, 16-bit PCM format. For other audio, adjust the file path, audio parameters, and sending interval.

"use strict";

const fs = require("fs/promises");
const Nls = require("alibabacloud-nls");
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
  const appkey = process.env.NLS_APP_KEY;
  const token = process.env.NLS_TOKEN;
  if (!appkey || !token) {
    throw new Error("Set NLS_APP_KEY and NLS_TOKEN.");
  }
  const audio = await fs.readFile("test1.pcm");
  const client = new Nls.SpeechTranscription({
    url: "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1",
    appkey,
    token
  });
  let timer;
  let finished = false;

  try {
    await new Promise((resolve, reject) => {
      timer = setTimeout(() => reject(new Error("Recognition timed out.")), 60000);
      client.on("failed", (message) => reject(new Error(message)));
      client.on("closed", () => {
        if (!finished) reject(new Error("Connection closed before completion."));
      });
      client.on("changed", (message) => {
        console.log("Intermediate:", JSON.parse(message).payload.result);
      });
      client.on("end", (message) => {
        console.log("Sentence:", JSON.parse(message).payload.result);
      });
      client.on("completed", (message) => {
        finished = true;
        console.log("Transcription completed.");
        resolve(message);
      });

      (async () => {
        await client.start(client.defaultStartParams(), true, 6000);
        for (let offset = 0; offset < audio.length; offset += 1024) {
          if (!client.sendAudio(audio.subarray(offset, offset + 1024))) {
            throw new Error("Audio could not be sent.");
          }
          await sleep(32);
        }
        await client.close();
      })().catch(reject);
    });
  } finally {
    clearTimeout(timer);
    client.shutdown();
  }
}

main().catch((error) => {
  console.error(error.message || error);
  process.exitCode = 1;
});

The example sends 1,024 bytes every 32 milliseconds to match the PCM data rate and limits the call to 60 seconds. The end event provides the final result for each sentence. The completed event indicates that the entire task has ended.

Interfaces and parameters

SpeechTranscription

The SpeechTranscription class provides real-time speech recognition. Pass a connection configuration object to its constructor:

const client = new Nls.SpeechTranscription({ url, appkey, token });

Parameter

Type

Description

config

Object

The connection configuration object. The following fields are required.

config.url

String

The service URL, which must be explicitly set. The example uses wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1.

config.appkey

String

The project Appkey.

config.token

String

The access token.

defaultStartParams()

Takes no parameters and returns the default recognition parameters: PCM audio at 16,000 Hz, with intermediate results, punctuation prediction, and inverse text normalization (ITN) enabled. Add or change fields as described in the Real-time speech recognition API.

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

on(which, handler)

Registers an event handler. Returns no value. Register handlers before starting the task.

Parameter

Type

Description

which

String

The event name. See the following table.

handler

Function

The event handler.

Event

Description

Handler arguments

started

Recognition has started. Provides the start event.

One String argument.

changed

Provides an intermediate recognition result.

One String argument.

begin

The current sentence has started.

One String argument.

end

The current sentence has ended. Provides the final result for that sentence.

One String argument.

completed

The entire transcription task has ended. Use end to obtain the recognized text for each sentence.

One String argument.

closed

The connection is closed.

No arguments.

failed

The task failed. Provides error information.

One String argument.

String arguments contain JSON event data. Use JSON.parse(message) to read the event fields.

async start(param, enablePing, pingInterval)

Starts a real-time speech recognition task. Returns a Promise that resolves with the start event as a JSON string when started occurs. Connection errors reject the Promise. Task errors are delivered through failed, and connection closure is reported through closed.

Parameter

Type

Description

param

Object

The recognition parameters. Use defaultStartParams() to obtain the default object.

enablePing

Boolean

Whether to send Ping requests automatically. Defaults to false. Set to true to enable Ping.

pingInterval

Number

The Ping interval in milliseconds. If Ping is enabled and this value is omitted, the interval is 6,000.

Important

In alibabacloud-nls 1.0.4, a task failure before the start event can leave the start() Promise pending even though failed is emitted. Handle failed, unexpected connection closure, and timeouts in addition to Promise rejections, as shown in the complete example.

async close(param)

Sends the stop directive for real-time speech recognition. Returns a Promise that resolves with the completion event as a JSON string when completed occurs. Task errors received while waiting for completion reject the Promise. Also retain handlers for failed and closed.

Parameter

Type

Description

param

Object

Optional stop parameters. For details, see the Real-time speech recognition API.

sendAudio(data)

Sends audio data. The audio format, sample rate, and other settings must match the start parameters.

Parameter

Type

Description

data

Buffer

Binary audio data.

Returns a Boolean: false if no client exists, or true after invoking the client's send operation. Recognition results are delivered through event handlers.

shutdown()

Forcibly closes the connection. Takes no parameters and returns no value. Use it to release the connection when the call ends or exits with an error.