Web

Overview

This document will guide developer using the real-time voice translation SDK (RTVT) from LiveData, to implement translation services in real-time voice scenarios.

Integration

Service Activation

To integration the LiveData RTVT SDK, you need to register an personal/enterprise account from LiveData official website (https://www.ilivedata.com/) , create a real-time voice translation service project and update the corresponding project parameters in SDK.

Requirements

  • Audio format : Only PCM format is supported
  • Sampling rate: 16KHz
  • Encoding: 16-bit depth
  • Channels: Monaural
  • Frame size: exactly 640 bytes per 20 ms frame; pass audio as an ArrayBuffer

Integrate dependencies

Import the following dependency libraries:

<script src="libs/msgpack.min.js"></script>
<script src="libs/md5.min.js"></script>
<script src="libs/int64.min.js"></script>
<script src="libs/crypto-js.min.js"></script>
<script src="libs/fpnn.min.js"></script>
<script src="dist/rtvt.sdk.min.js"></script>

Initialization

let rtvtClient = new RTVTClient({
    endpoint: endpoint,
    pid: pid,
    uid: uid,
});
Parameter Type M/O Description
endpoint string M endpoint (check from LiveData Console service configuration)
pid long M project ID
uid string O user id, input as needed.

Login

rtvtClient.login(token, ts, callback);
Parameter Type M/O Description
token string M Refer to the following text for the method of token generation
ts int M Unix timestamp in seconds; must match the timestamp used to generate the token.
callback function M callback(ok, errorCode): (true, 0) on success. A failed login response is reported as (false, 800001).

Start translate

rtvtClient.createStream(srcLang, destLang, asrResult, tempResult, transResult, callback);
Parameter Type M/O Description
srcLang string M source language
destLang string M target language
asrResult boolean M Whether to return recognition results. Explicitly pass true or false.
tempResult boolean M Whether to return temporary recognition and translation results. Explicitly pass true or false.
transResult boolean M Whether to return translation results. Explicitly pass true or false.
callback function M callback(stream, errorCode): an RTVTStream object and 0 on success; null and an error code on failure.
  • When recognition results are available and asrResult is set to true, the recognizedResult event is triggered:
rtvtClient.on("recognizedResult", function(data) { console.log(data); });
  • When translation results are available and transResult is set to true, the translatedResult event is triggered:
rtvtClient.on("translatedResult", function(data) { console.log(data); });
  • When temporary recognition results are available and both asrResult and tempResult are set to true, the recognizedTempResult event is triggered:
rtvtClient.on("recognizedTempResult", function(data) { console.log(data); });
  • When temporary translation results are available and both transResult and tempResult are set to true, the translatedTempResult event is triggered:
rtvtClient.on("translatedTempResult", function(data) { console.log(data); });

Send voice clip

rtvtClient.sendVoice(stream, seq, pcm);
Parameter Type M/O Description
stream RTVTStream M The stream object returned by createStream, not a numeric stream ID.
seq long M audio segment sequence number (preferably in order)
pcm ArrayBuffer M PCM audio data; exactly 640 bytes per frame.

Translation into multiple target languages

To translate into multiple target languages, you must use multi_startTranslate to start the stream and multi_sendVoice to send audio.

Start real-time voice translation

rtvtClient.multi_startTranslate(srcLanguage, srcAltLanguage, asrResult, tempResult, ttsResult, userId, callback);
Parameter Type M/O Description
srcLanguage string M Source language.
srcAltLanguage string[] / null O Alternative source languages. Pass null or [] when unused. Providing alternatives may increase initial result latency.
asrResult boolean M Whether to return speech recognition results through recognizedResult.
tempResult boolean M Whether to return temporary recognition and translation results through recognizedTempResult and translatedTempResult, respectively, for faster feedback on long sentences.
ttsResult boolean M Whether to return TTS results. Only final translation results are synthesized.
userId string M Displayed in the backend for lookup; the application can associate it with the returned streamId.
callback function M callback(stream, errorCode): an RTVTStream object and 0 on success; null and an error code on failure. Pass this object when sending audio.

Multi-language mode always enables translation (transResult=true). The fifth argument is ttsResult, not a translation switch. Pass all booleans explicitly; use an empty string when userId is not needed.

Send voice clip

rtvtClient.multi_sendVoice(stream, seq, voicedata, voiceDataTs, dstLanguageList, callback);
Parameter Type M/O Description
stream RTVTStream M The stream object returned by multi_startTranslate, not a numeric stream ID.
seq long M Audio segment sequence number (preferably in order).
voicedata ArrayBuffer M 16 kHz, 16-bit, mono PCM; exactly 640 bytes per 20 ms frame.
voiceDataTs number M Unix timestamp of the audio frame in milliseconds, based on capture time. Unlike the token timestamp, this is not in seconds.
dstLanguageList string[] M Target languages, such as ["en", "ja"]. The list can change between frames.
callback function O callback(ok, errorCode): (true, 0) on success; (false, errorCode) on failure.

Return values and errors

sendVoice and multi_sendVoice return 300002 for a frame that is not 640 bytes, or 300003 for a missing stream. A return value of undefined only means local validation passed, not that the server acknowledged the request. The optional multi_sendVoice callback reports local validation failures and server send results. Network and server send errors also trigger ErrorRecorder.

Result events and TTS

Multi-language mode uses the same four recognition and translation events described above. Use lang to distinguish target languages. Event handlers receive the original server payload, independently of stream creation and send callbacks. When ttsResult=true, subscribe to:

rtvtClient.on("ttsResult", function(data) {
    console.log(data.lang, data.text, data.data); // Target language, text, audio data
});
rtvtClient.on("ErrorRecorder", function(error) {
    console.error(error);
});

64-bit fields such as streamId are decoded as Int64 objects. Use .toString() for display or string keys to avoid precision loss from conversion to JavaScript numbers. Use stream.getStreamID() to inspect the stream ID; continue passing the stream object when sending audio.

RTVTStream object

Successful createStream and multi_startTranslate callbacks both return an RTVTStream instance.

Method Description
stream.getStreamID() Gets the server stream ID. Use .toString() for display; pass the stream object when sending audio.
stream.close() Requests that this stream stop. No completion callback is provided.

Multi-language example

Call onPcmFrame from your audio capture callback with one 640-byte ArrayBuffer and its capture timestamp in milliseconds. After capture stops, wait for the final results you need before closing the stream and releasing the client.

let activeStream = null;
let seq = 0;

function onPcmFrame(pcm, captureTs) {
    if (!activeStream) return;
    rtvtClient.multi_sendVoice(activeStream, ++seq, pcm, captureTs, ["en", "ja"], function(ok, errorCode) {
        if (!ok) console.error("send voice failed", errorCode);
    });
}

rtvtClient.login(token, ts, function(ok, errorCode) {
    if (!ok) {
        console.error("login failed", errorCode);
        return;
    }
    rtvtClient.multi_startTranslate("zh", null, true, true, true, "user-123", function(stream, errorCode) {
        if (!stream) {
            console.error("create stream failed", errorCode);
            return;
        }
        activeStream = stream;
        seq = 0;
        // Start audio capture and call onPcmFrame from its callback.
    });
});

If one capture callback produces multiple frames, calculate timestamps from each frame’s position at 20 ms intervals instead of assigning the same send time to the whole batch. Send callbacks report the outcome of audio requests. Recognition and translation content is delivered through result events.

Stop translation

Use stream.close() for one stream, or rtvtClient.stopAllStreams() for all streams. Create a new stream to translate again after stopping.

rtvtClient.stopAllStreams();

Release the client

Call this when finished. Log in again before starting a new session.

rtvtClient.destory();

Token generate

Please refer to the following code example for the method of token generation:

var pid = pid;
var key = key;
var ts = parseInt(new Date().getTime() / 1000);
var coreString = pid + ":" + ts;
var token = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(coreString, atob(key)));

More information

For SDK download and more information, please go to Github。