Timestamp feature introduction
Long-text speech synthesis can return the start and end times of text segments in the synthesized audio. These sentence-level timestamps can be used to display subtitles for video narration or highlight text during audiobook playback.
Scope
Sentence-level timestamps for long-text speech synthesis are available through the asynchronous RESTful API. For the complete workflow and sample code, see Long-text speech synthesis RESTful API.
For timestamps in short-text speech synthesis, see Introduction to the speech synthesis timestamp feature.
Enable timestamps
When submitting a long-text speech synthesis task, set payload.tts_request.enable_subtitle to true in the request body. The default value is false, which disables timestamps.
In the Java example, tts represents the tts_request object. Set the parameter as follows:
tts.put("enable_subtitle", true);
Retrieve timestamps
After the task is submitted successfully, use data.task_id from the response to poll for the synthesis result. When synthesis is complete, data.audio_address contains the audio download URL and data.sentences contains the timestamps.
|
Field |
Type |
Description |
|
|
List |
Sentence-level timestamps in the |
Fields in each sentences element
|
Field |
Type |
Description |
|
|
String |
The text of the segment. |
|
|
String |
The start time of the segment, in milliseconds from the beginning of the synthesized audio. |
|
|
String |
The end time of the segment, in milliseconds from the beginning of the synthesized audio. |
Use the returned text and its corresponding times to display subtitles or highlight text.
Request and response examples
The following request body uses the siyue voice to synthesize WAV audio with sentence-level timestamps enabled. Replace yourAppkey with the project Appkey and yourToken with a valid NLS token.
{
"payload": {
"tts_request": {
"voice": "siyue",
"sample_rate": 16000,
"format": "wav",
"enable_subtitle": true,
"text": "The weather is nice today. Let us go for a walk."
},
"enable_notify": false
},
"context": {
"device_id": "my_device_id"
},
"header": {
"appkey": "yourAppkey",
"token": "yourToken"
}
}
The following example shows a query response after synthesis is complete. The task ID, request ID, and audio download URL are replaced with placeholders.
{
"status": 200,
"data": {
"sentences": [
{
"text": "The weather is nice today. Let us go for a walk.",
"begin_time": "0",
"end_time": "3685"
}
],
"task_id": "<task_id>",
"audio_address": "<audio_address>",
"notify_custom": null
},
"error_code": 20000000,
"error_message": "SUCCESS",
"request_id": "<request_id>"
}