ChivoxAI

/docs

JS sdk

0.Integration preparation

  • Development based on JavaScript EMCAScript 5/6 specification.

  • Need https environment.

  • Contains recorder (Html5Recorder) and player (Html5Player).

    Get sample code

Import JS file

  • Add the following code to the web page and import the file directly.
<script type="text/javascript" src="https://sdk.cloud.chivox.com/chivoxsdk-js/v6.1/chivox.min.js"></script>
  • Download and import via npm.
npm install chivox_h5sdk

import Html5Recorder from 'chivox_h5sdk/src/html5/html5recorder'

1.Create Engine

new Html5Recorder(options)

Interface parameters

Name Type Description
options Object Initialization parameters,See the formatDefault_option
Default_option
Name Type Optional Defaults Description
server string Optional wss://cloud.chivox.com Assessment server address.
alg string Optional sha1 Encryption type of signature sig. Signature algorithm supports sha1, sha256, and md5
Default value is sha1
micWatch boolean Optional false Whether to enable real-time microphone connection detection
If it's enabled, when the microphone is disconnected, the following error message will be returned through the onError interface:
{id:50003,message:no audio device}
When the application layer receives the error, prompts the user to check the microphone and reinitializes the engine if you want to continue using speech evaluation.
appKey string Required No Appkey authorized by Chivox.
sigurl string Required No Get the interface address for calculating identity information. Developers calculate by themselves.
Detailed rule reference Signature information

Note: The signature is valid for 1 hour.
onInit callback Required No Initialization completed callback. Format: (msg) => {} @return (string)
onError callback Required No Callback methods that generate errors during engine initialization or evaluation. Format: (error) => {} @return (Object)
Note: Scoring related errors are called back through onScoreError.
Signature information
  • The correct signature information must contain the following parameters:
Name Type Description
timestamp string Timestamp string, unit: ms, length: 13
sig string Signature string, Lowercase letters, Generated by algorithm alg (appkey + timestamp + secretKey),
See signature example for details.

Note: The signature is valid for 1 hour.

  • The JSON result returned correctly is:
{
    "timestamp": "1548042872722",
    "sig": "95a9659ffxxxx0ab11c2a0008xxxx40f8a485d45" 
}

Code example

let sdk = new Html5Recorder({
	appKey: "xxxxxxxxxxxx",  //AppKey authorized by Chivox
	alg: "sha1", //Default sha1, Signature algorithm supports sha1, sha256, and md5.
    sigurl: "../php/sig.php",  //Get the interface address for calculating identity information. 
    server: "wss://cloud.chivox.com",
    onInit: function (mess) {
        //The engine is initialized successfully...
        console.log("Init success!")
    }, 
    onError: function (err) {
		//gine running error...
        console.log("Sdk onError:" + err)
    }
})

2.Recording interface

record(params)

Interface parameters

Name Type Description
params Object Parameters required by the recording interface, See detailsDefault_params
Default_params
Name Type Optional Defaults Description
duration int Optional The limited duration of the kernel selected by default. Recording time, Unit: ms,
Note: The recording duration is less than 2000ms, the default is 2000ms.
playDing boolean Optional true Whether to play the "ding" prompt sound at the beginning of the recording.
Note: Due to the limitation of the IOS system on the audio playback control, please set it to false, and you can handle it on the front end if necessary.
audioType string Required No audio format: wav, Mono、16bite、16000 sampling rate
serverParams Object Required No Evaluate kernel request parameters, See details English Kernel Doc, Chinese Kernel Doc
onRecordIdGenerated callback Optional No Unique Id callback for each evaluation. (The SDK establishes a new connection for each scoring and generates a unique tokenId, which can distinguish the scoring results according to the tokenId.)
Format: (tokenId) => { var lastTokenID = tokenId.tokenId }
onStart callback Optional No Callback after recording starts. Format: () => { }
Note: The recording duration is calculated from the current callback execution.
onStop callback Optional No Callback after recording stops. Format: () => { }
onInternalScore callback Optional No Callback of the intermediate scoring result in the recording. Format: (data) => { } @return(Object)
onScore callback Required No Callback after the score result is returned. Format: (data) => { } @return(Object)
onScoreError callback Required No Scoring related error callback. Format: (err) => { } @return(Object)
Note: SDK errors are called back through onError.
sample of error data

Code example

sdk.record({
	duration: 4000,
	audioType: "wav",
    serverParams: {
        coreType: "en.sent.score",
        refText: refText,
        rank: 100,
        userId: "chivox tester"
    },
    onRecordIdGenerated: function(tokenId) {
        console.log("=============onRecordIdGenerated start=============");
        console.log(JSON.stringify(tokenId));
        console.log("=============onRecordIdGenerated end=============");
    },
    onStart: function () {
        controlRecordU(0)
    },        
    onStop: function () {
        controlRecordU(1)
    },
    onScore: function (score) {
        console.log(score)
    },
    onScoreError: function (err) {
        alert(JSON.stringify(err));
    }
})
```	

### 3.Stop recording
#### stopRecord() {docsify-ignore}

### 4.Start playback 
#### Start playback of the last recording. {docsify-ignore}
#### startReplay(options) {docsify-ignore}
**options(Object)**

| Name  |  Type | Description  |
| ------------ | ------------ | ------------ |
|  onStop |callback    | Callback after playback is complete. Format: () => {} |

### 5.Stop playback 
#### stopReplay( ) {docsify-ignore}

### 6.Reset the recorder 
#### reset() {docsify-ignore}

### 7.Destroy the recorder 
#### dispose() {docsify-ignore}

### 8.Audio recorder auxiliary interface 

#### Get recording volume  {docsify-ignore}
##### getMicVolume() {docsify-ignore}

#### Get playback volume  {docsify-ignore}
##### getVolume()  {docsify-ignore}

#### Set the recording volume  {docsify-ignore}
##### setMicVolume(volume) {docsify-ignore}
##### volume range [0, 0.1, 0.2 ~ 1] {docsify-ignore}

#### Set playback volume   {docsify-ignore}
##### setVolume(volume) {docsify-ignore}
##### volume range [0, 0.1, 0.2 ~ 1]   {docsify-ignore}

#### Display waveform graph   {docsify-ignore}
##### showVolumeBar( ) {docsify-ignore}
 -the waveform graph is displayed by default when the interface is called without parameters<br>
 -the waveform graph is no longer displayed when the incoming parameter is funtion
##### The following code must be added in the html.  {docsify-ignore}
``` ##### Returns an example of a phonetic data code {docsify-ignore} ``` //This interface can be invoked after the SDK is successfully initialized sdk.showVolumeBar(function (data)) { //return soundIntensity value(soundIntensity:**) console.log(data) } ```

9.Player interface

Create player

new Html5Player()

var player = new Html5Player();

HTML5 mode player. Designed and developed based on web audio API to support remote or local audio playback in this environment.

Load audio interface

load(options)

Interface parameters options(Object)
Name Type Description
url string URL of the audio to be loaded.
success callback Callback for successful audio loading. Format: () => {}
error callback Callback after failed to load audio. Format: (err) => {}

Play audio interface

play(options)

Interface parameters options(Object)
Name Type Description
position int Play position, default 0. Unit: ms, *optional
duration init Play time. Unit: ms *optional
error callback Callback after failed to load audio, format: (err) => {}
onStart callback Callback when starting to play audio, format: () => {}
onStop callback Callback after audio playback stops, format: () => {}

Code example

player.load({
	url: "../static/playaudio.mp3",
	success: function (code,message) {
		//Play audio 
        player.play({
            position: 0,
            onStop: function() {
                console.log("player onStop");
            },
            onStart: function () {
                console.log("player onStart");
            }
        })        
    },
    error: function(err) {
        console.log("player error:" + JSON.stringify(err));
    }
})

Stop play

stop()

Set playback volume

setVolume(volume)
volume range [0, 0.1, 0.2 ~ 1]

Get the playback volume

getVolume()
@return(init) 0~1

Reset player status

reset()

Destroy the player

dispose()

10.Common platforms and compatibility

Platform Browser/Webview Version Remarks
Windows Chrome 52+
Windows FireFox 35+
Windows Edge 14+
Windows QQ Browser 10.2
Windows 360 Safe Browser 10+
Windows Sogou Browser 7.0+
Mac Chrome 47+
Mac Safari 11+
Android TBS (WeChat and mobile QQ default to Webview.) 43600 The built-in browser kernel of WeChat and mobile QQ is TBS by default.
Android Chrome 60+
iOS Safari 11+
iOS Chrome 92+
WeChat Support Android and iOS WeChat N/A

Note:
JSSDK needs to use the browser interface, audiocontext and getusermedia.

1.The support of these two interfaces by common browsers is detailed in audiocontextandgetusermedia
2.You can use this tool to check whether it supports. Interface test tool, It can be used if it prompts "support h5".

SDK, API, MCP and Function Calling documentation on this site.