Logo
Short Text to Speech

H5 SDK

TTS H5 SDK

1. Browser Compatibility

Operating SystemSupported Browser Minimum Version
chromeFirefox
Windows 741.0.2272.7646
Windows 10/1143.0.2357.8146
MAC OS43.0.2357.8146

2. Import

<!-- 【1.1】Import the streaming player file -->
<script type="text/javascript" src="static/pcm-player.js"></script>

<!-- 【1.2】Import the SDK core file -->
<script type="text/javascript" src="sdk/TtsSDK.min.js"></script>

Audio stream playback. You can use the pcm-player plugin for audio stream playback, or by other means, just for reference.

3. Parameter Configuration

3.1 Engine Parameters

(1)Parameter Example

  • Set app_id and secret

    • Contact business for app_id and secret
  • signature and timestamp

    • Users call the interface to obtain timestamp and signature
  • Set synthesis parameters

    • Set synthesis parameters through JSON in the payload field
    • If parameters are missing or values are out of range, an error code and error message will be returned in the onError callback
ParameterTypeRequiredDescriptionDefault Value
textStringYesText to be synthesized, limited to 1024 bytes (UTF-8 encoding)Required
formatStringNoAudio encoding format, wav / pcm / mp3, default is pcmpcm
sample_rateIntegerNoAudio sampling rate, default is 24000, options are 8000, 1600024000
lang_typeStringYesLanguage optionsRequired
voiceStringNoVoice optionsJapanese: Yuko
English:Julie
Chinese:Xiaohui
emotionStringNoEmotional styleNo
volumeFloatNoVolume, [0.1, 3], default is 1, usually one decimal place is sufficient1
speech_rateFloatNoSpeech rate, [0.2, 3], default is 1, usually one decimal place is sufficient1
pitch_rateFloatNoPitch rate, [0.1, 3], default is 1, usually one decimal place is sufficient1
silence_durationIntegerNoSilence duration at the end of a sentence, in ms, default is 125125
enable_timestampBooleanNoWhether to enable timestamp return functionfalse

Example (Method 1, recommended): Obtain timestamp、signature from the backend

new TTSEngine({
  app_id: '',
  signature : '',
  timestamp : 1234567890,
  payload: {
    text: '',
    format: '',
    sample_rate: 24000,
    lang_type: '',
    voice: '',
    emotion: '',
    volume: 1,
    speech_rate: 1,
    pitch_rate: 1,
    silence_duration: 125,
    enable_timestamp: false,
  }
})

Example (Method 2, not recommended): Write secret in the frontend and generate signature in the frontend

new TTSEngine ({
  app_id: '',
  secret : '',
  payload: {
    text: '',
    format: '',
    sample_rate: 24000,
    lang_type: '',
    voice: '',
    emotion: '',
    volume: 1,
    speech_rate: 1,
    pitch_rate: 1,
    silence_duration: 125,
    enable_timestamp: false,
  }
})

3.2 Initialization Method

(1)Configuration Parameters

NameTypeDescriptionDefault Value
engineFirstInitDoneFunctionInitialization success callback methodNone
engineFirstInitFailFunctionInitialization failure callback methodNone

(2)Parameter Example

new TTSEngine ({
  engineFirstInitDone : function () {},
  engineFirstInitFail : function (status,msg) {} 
})

3.3 Synthesis Result Return

(1)Configuration Parameters

NameTypeDescriptionDefault Value
onTimestampFunctionSynthesis timestamps returned by the engineNone
onDurationFunctionSynthesis duration returned by the engineNone
onResultFunctionCallback method for synthesized binary data stream returned by the engineNone
onCompletedFunctionSynthesis task_id returned by the engineNone
onWarningFunctionWarning callback method for synthesis results returned by the engineNone
onErrorFunctionError callback method for synthesis results returned by the engineNone

(2)Parameter Example

new TTSEngine ({
  onTimestamp : function (taskId, payload) {},
  onDuration : function (taskId, payload) {},
  onResult : function (data) {},
  onCompleted : function (taskId) {},
  onWarning : function (status, msg, taskId) {},
  onError : function (status, msg, taskId) {}
})

3.4 Network

(1)Configuration Parameters

NameTypeDescriptionDefault Value
onNetworkErrorFunctionNetwork monitoring callbackNone

(2)Parameter Example

new TTSEngine ({ 
  onNetworkError : function (status, msg) {},
})

4. Method Invocation

startSynthesis()

Function: Start synthesis;

cancelSynthesis()

Function: Cancel synthesis;

destroySynthesis()

Function: Destroy synthesis instance;

5. SDK Download

H5 SDK