Updated on 2025-08-26 GMT+08:00

Continuous

Function

The speech used for continuous recognition must be shorter than five hours. This mode is applicable to meeting, lecture, and live video recognition.

It combines streaming recognition with endpoint detection. In this mode, the speech data is also input by segment, but endpoint detection is performed before the engine processes the data. If speech is detected, decoding work starts. If nothing (muting) is detected, the mute segment will be discarded. If the end point of a speech segment is detected, the segment's recognition result is directly returned, and then the engine continues to recognize subsequent speech data. Therefore, in the continuous recognition mode, the recognition results may be returned multiple times. If a long speech segment is sent, multiple recognition results may be returned at a time.

With the muting detection function, continuous recognition generally has a higher efficiency than streaming recognition, because the feature extraction and decoding operations are not performed on the mute segments, resulting in higher CPU efficiency. The streaming mode is usually combined with the endpoint detection function of the client, so only the detected valid speech segments are uploaded to the server for recognition.

wss-URI

  • wss-URI format

    wss /v1/{project_id}/rasr/continue-stream

  • Parameter descriptions
    Table 1 Parameter descriptions

    Parameter

    Mandatory

    Description

    project_id

    Yes

    Project ID. For details about how to obtain a project ID, see Obtaining a Project ID.

    Table 2 Request header parameters

    Parameter

    Mandatory

    Type

    Description

    X-Auth-Token

    Yes

    String

    User token.

    It is used to obtain the permission to call APIs. For details about how to obtain a user token, see Authentication. The token is the value of X-Subject-Token in the response header.

  • Example request (pseudocode)
    wss://{endpoint}/v1/{project_id}/rasr/continue-stream
    
    Request header:
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...

    The endpoint is the request URL for calling an API. Endpoints vary according to services and regions. For details, see Endpoints.

  • Example Python 3 request
    # -*- coding: utf-8 -*-
    # This demo is used only for tests. You are advised to use the SDK. The websocket-client must be installed in advance by running the pip install websocket-client command.
    import websocket
    import threading
    import time
    import json
    
    def rasr_demo():
        url = 'wss://{{endpoint}}/v1/{{project_id}}/rasr/continue-stream'  # Replace endpoint and project_id with the actual values.
        audio_path = 'Audio path'
        token ='Token of the region to which the user belongs'
        header = {
            'X-Auth-Token': token
        }
        with open(audio_path, 'rb') as f:
            data = f.read()
        body = {
            'command': 'START',
            'config': {
                'audio_format': 'pcm8k16bit',
                'property': 'chinese_8k_general'
            }
        }
        def _on_message(ws, message):
            print(message)
        def _on_error(ws, error):
            print(error)
        ws = websocket.WebSocketApp(url, header, on_message=_on_message, on_error=_on_error)
        _thread = threading.Thread(target=ws.run_forever, args=(None, None, 30, 20))
        _thread.start()
        time.sleep(1)
        ws.send(json.dumps(body), opcode=websocket.ABNF.OPCODE_TEXT)
        now_index = 0
        byte_len = 4000
        while now_index < len(data):
            next_index = now_index + byte_len
            if next_index > len(data):
                next_index = len(data)
            send_array = data[now_index: next_index]
            ws.send(send_array, opcode=websocket.ABNF.OPCODE_BINARY)
            now_index += byte_len
            time.sleep(0.05)
        ws.send("{\"command\": \"END\", \"cancel\": \"false\"}", opcode=websocket.ABNF.OPCODE_TEXT)
        time.sleep(10)
        ws.close()
    if __name__ == '__main__':
        rasr_demo()
  • Example Java request
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import okhttp3.WebSocket;
    import okhttp3.WebSocketListener;
    import okio.ByteString;
    import java.net.URL;
    
    /**
     * This demo is for testing purposes only. You are advised to use the SDK.
     * You need to configure the okhttp and okio JAR files first by downloading from the SDK.
     */
    public class RasrDemo {
      public void rasrDemo() {
        try {
          // Replace endpoint and projectId with the actual values.
          String url = "wss://{{endpoint}}/v1/{{project_id}}/rasr/continue-stream";
          String token = "Token of the corresponding region";
          byte[] data = null; //Byte array that stores the audio to be sent
          OkHttpClient okHttpClient = new OkHttpClient();
          Request request = new Request.Builder().url(url).header("X-Auth-Token", token).build();
          WebSocket webSocket = okHttpClient.newWebSocket(request, new MyListener());
          webSocket.send("{\"command\": \"START\", \"config\": {\"audio_format\": \"pcm8k16bit\", \"property\": \"chinese_8k_general\"}}");
          webSocket.send(ByteString.of(data));
          webSocket.send("{  \"command\": \"END\",  \"cancel\": false}");
          Thread.sleep(10000);
          webSocket.close(1000, null);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      class MyListener extends WebSocketListener {
        @Override
        public void onOpen(WebSocket webSocket, Response response) {
          System.out.println("conneected");
        }
        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
          System.out.println("closed");
        }
        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
          t.printStackTrace();
        }
        @Override
        public void onMessage(WebSocket webSocket, String text) {
          System.out.println(text);
        }
    
      }
      public static void main(String[] args) {
        RasrDemo rasrDemo = new RasrDemo();
        rasrDemo.rasrDemo();
      }
    }
  • JavaScript (Node.js v18.20.2 (LTS) is recommended.)
    // Import the ws library of Node.js.
    const WebSocket = require('ws');
    function continueStremDemo(endpoint,audioPath, projectID, token) {
        const url = `wss://${endpoint}/v1/${projectID}/rasr/continue-stream`;  // Replace endpoint and projectID with the actual values.
        // Read the content of the audio file.
        const fs = require('fs');
        let data = fs.readFileSync(audioPath);
        //Add the token to the HTTP header.
        const headers = {
            'X-Auth-Token': token,
            // Optionally add the enterprise ID.
            // 'Enterprise-Project-Id': Enterprise ID
        };
        // Create a WebSocket instance.
        const ws = new WebSocket(url, {
            headers // Add a custom HTTP header.
        });
        ws.on('open', async () => {
            const body = {
                command: 'START',
                config: {
                    audio_format: 'pcm16k16bit',
                    property: 'chinese_16k_general'
                }
            };
            ws.send(JSON.stringify(body));
            let nowIndex = 0;
            const byteLen = 3200; // Empty values are not allowed. Recommended range: 2000 to 10000.
            while (nowIndex < data.length) {
                const nextIndex = nowIndex + byteLen;
                const sendArray = data.slice(nowIndex, nextIndex > data.length ? data.length : nextIndex);
                ws.send(sendArray, { binary: true });
                nowIndex += byteLen;
                await new Promise(resolve => setTimeout(resolve, 100)); // Simulate a delay (unit: ms).
            }
            const endCommand = JSON.stringify({ command: 'END', cancel: 'false' });
            ws.send(endCommand);
        });
        ws.on('message', (data) => {
            if (data instanceof Buffer) {
                // Convert the Buffer to a UTF-8 encoded string.
                const messageString = data.toString('utf8');
                console.log('Received (converted from Buffer):', messageString);
                const type = JSON.parse(messageString).resp_type;
                if (type ==='END' || type ==='ERROR') {
                    ws.close();
                }
            }
        });
        ws.on('error', (error) => {
            console.error('WebSocket Error:', error);
        });
    };
    continueStremDemo(endpoint,audioPath, projectID, token);