語音合成時(shí)間戳功能介紹
語音實(shí)時(shí)合成服務(wù)在輸出音頻流的同時(shí),可輸出每個(gè)漢字/英文單詞在音頻中的時(shí)間位置,即時(shí)間戳,時(shí)間戳功能又叫字級別音素邊界接口。該時(shí)間信息可用于驅(qū)動虛擬人口型、做視頻配音字幕等。
只有支持字級別音素邊界接口的發(fā)音人才有此功能。
由于HTTP協(xié)議傳輸方式的限制,短文本語音合成RESTful API不支持返回時(shí)間戳信息。
TTS服務(wù)返回的字幕是基于發(fā)音的,所以不能直接用于上屏,需要使用您的原始文本。
如果用于上屏,可以基于返回的結(jié)果,定位每個(gè)句子的句首和句尾時(shí)間戳。
參數(shù)設(shè)置
在客戶端設(shè)置請求參數(shù)enable_subtitle
為true
,開啟時(shí)間戳功能。
以Java SDK為例,其設(shè)置?式如下。
// 是否開啟字幕功能(返回對應(yīng)文本的相應(yīng)時(shí)間戳),默認(rèn)不開啟。
synthesizer.addCustomedParam("enable_subtitle", true);
服務(wù)端響應(yīng)
服務(wù)端返回的帶字幕信息的響應(yīng)MetaInfo事件。
參數(shù) | 類型 | 說明 |
subtitles | List | 時(shí)間戳信息。 |
其中,SubtitleItem格式如下。
參數(shù) | 類型 | 說明 |
text | String | ?本信息。 |
begin_time | Integer | ?本對應(yīng)tts語?開始時(shí)間戳,單位ms。 |
end_time | Integer | ?本對應(yīng)tts語?結(jié)束時(shí)間戳,單位ms。 |
phoneme | String | 不支持打印該字對應(yīng)的phone系列,默認(rèn)輸出為 |
begin_index | Integer | 該字在整句中的開始位置,從0開始。 |
end_index | Integer | 該字在整句中的結(jié)束位置,從0開始。 |
返回示例
{
"header": {
"message_id": "05450bf69c53413f8d88aed1ee60****",
"task_id": "640bc797bb684bd6960185651307****",
"namespace": "SpeechSynthesizer",
"name": "MetaInfo",
"status": 20000000,
"status_message": "GATEWAY|SUCCESS|Success."
},
"payload": {
"subtitles": [
{
"begin_index": 0,
"phoneme": "null",
"end_time": 180,
"end_index": 1,
"begin_time": 0,
"text": "你"
},
{
"begin_index": 1,
"phoneme": "null",
"end_time": 299,
"end_index": 2,
"begin_time": 180,
"text": "好"
}
]
}
}
音素級別時(shí)間戳
參數(shù)設(shè)置
在客戶端設(shè)置請求參數(shù)enable_subtitle
為true
,enable_phoneme_timestamp
為true
,開啟音素級別時(shí)間戳功能。
移動端暫不支持音素級別時(shí)間戳。
以Java SDK為例,其設(shè)置?式如下:
// 是否開啟字幕功能(返回對應(yīng)文本的相應(yīng)時(shí)間戳),默認(rèn)不開啟。
synthesizer.addCustomedParam("enable_subtitle", true);
synthesizer.addCustomedParam("enable_phoneme_timestamp", true);
服務(wù)端響應(yīng)
服務(wù)端返回的帶字幕信息的響應(yīng)MetaInfo事件。
參數(shù) | 類型 | 說明 |
subtitles | List | 時(shí)間戳信息。 |
其中,SubtitleItem格式如下表所示:
參數(shù) | 類型 | 說明 |
text | String | ?本信息。 |
begin_time | Integer | ?本對應(yīng)tts語?開始時(shí)間戳,單位ms。 |
end_time | Integer | ?本對應(yīng)tts語?結(jié)束時(shí)間戳,單位ms。 |
phoneme | String | 打印該字對應(yīng)的phone系列,默認(rèn)輸出為 |
begin_index | Integer | 該字在整句中的開始位置,從0開始。 |
end_index | Integer | 該字在整句中的結(jié)束位置,從0開始。 |
phoneme_list | List | 音素時(shí)間戳數(shù)組。 |
phoneme_list格式如下表所示:
參數(shù) | 類型 | 說明 |
index | Integer | 音素字內(nèi)序號,從0開始。 |
begin_time | Integer | 起始時(shí)間,單位為毫秒。 |
end_time | Integer | 結(jié)束時(shí)間,單位為毫秒。 |
phoneme | String | 音素名,詳情請參見音素與拼音,國際音標(biāo)間關(guān)系。 |
tone | Integer | 音調(diào)。英文中0/1/2分表代表輕音,重音,次重音。拼音中1/2/3/4/5分別代表一聲,二聲,三聲,四聲,輕聲。 |
subtitles示例
//今天
{
"subtitles":[
{"begin_index":0,"begin_time":0,"end_index":1,"end_time":100,
"phoneme_list":[
{"index":0, "begin_time":0, "end_time": 20, "phoneme":"hh", "tone":0},
{"index":1, "begin_time":20, "end_time": 40, "phoneme":"ah", "tone":0},
{"index":1, "begin_time":40, "end_time": 60, "phoneme":"l", "tone":1},
{"index":1, "begin_time":60, "end_time": 100, "phoneme":"ow", "tone":1}
],
"text":"hello", "phoneme":"hh ah l ow"},
{"begin_index":1,"begin_time":100,"end_index":2,"end_time":200,
"phoneme_list":[
{"index":0, "begin_time":100, "end_time": 150, "phoneme":"j_c", "tone":1},
{"index":1, "begin_time":150, "end_time": 200, "phoneme":"in_c", "tone":1}
],
"text":"今", "phoneme":"j_c in_c"},
{"begin_index":1,"begin_time":200,"end_index":2,"end_time":400,
"phoneme_list":[
{"index":0, "begin_time":200, "end_time": 300, "phoneme":"t_c", "tone":1},
{"index":1, "begin_time":300, "end_time": 400, "phoneme":"ian_c", "tone":1}
],"text":"天", "phoneme":"t_c ian_c"}
]
}
音素與拼音、國際音標(biāo)間關(guān)系
對于拼音,以<拼音>_c的方式給出音素,比如天的拼音是t和ian,給出的音素為t_c和ian_c。
對于英文,詳情請參見下表,其中如果音素形式為<音素><數(shù)字>,數(shù)字為0/1/2的話,意思是音素和tone的組合。例如,對于ah這個(gè)音素,ah1代表ah重音的情況,ah0代表ah輕音的情況。
英文音素與國際音標(biāo)(IPA)映射關(guān)系表
美式英文
序號
美式英文音素
國際音標(biāo)(IPA)
1
aa
ɑ
2
ae
?
3
ah1
?
4
ah2
?
5
ah0
?
6
ao
?
7
aw
a?
8
ay
a?
9
b
b
10
ch
t?
11
d
d
12
dh
e
13
eh
?
14
er
?
15
ey
e?
16
f
f
17
g
g
18
hh
h
19
ih
?
20
iy
i
21
jh
d?
22
k
k
23
l
l
24
m
m
25
n
n
26
ng
?
27
ow
o?
28
oy
??
29
p
p
30
r
?
31
s
s
32
sh
?
33
t
t
34
th
θ
35
uh
?
36
uw
u
37
v
v
38
w
w
39
y
j
40
z
z
41
zh
?
英式英文
序號
英式英文音素
國際音標(biāo)(IPA)
1
@
?
2
@@
??
3
a
?
4
aa
ɑ?
5
e
?
6
i
?
7
ii
i?
8
o
?
9
oo
??
10
u
?
11
uh
?
12
uu
u?
13
ai
a?
14
au
a?
15
e@
??
16
ei
e?
17
i@
??
18
oi
??
19
ou
??
20
u@
??
21
p
p
22
b
b
23
t
t
24
d
d
25
k
k
26
g
g
27
ch
t?
28
jh
d?
29
s
s
30
z
z
31
v
v
32
f
f
33
sh
?
34
th
θ
35
dh
e
36
zh
?
37
h
h
38
r
?
39
l
l
40
m
m
41
n
n
42
ng
?
43
y
j
44
w
w
字級別時(shí)間戳代碼示例
示例中使用SDK內(nèi)置的默認(rèn)語音合成服務(wù)的外網(wǎng)訪問服務(wù)URL,如果您使用位于阿里云上海地域的ECS,且需要通過內(nèi)網(wǎng)訪問服務(wù)URL,則在創(chuàng)建NlsClient對象時(shí),設(shè)置內(nèi)網(wǎng)訪問的URL:
client = new NlsClient("ws://nls-gateway.cn-shanghai-internal.aliyuncs.com/ws/v1", accessToken);
示例中將合成的音頻保存在文件中,如果您需要播放音頻且對實(shí)時(shí)性要求較高,建議使用流式播放,即邊接收語音數(shù)據(jù)邊播放,減少延時(shí)。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.nls.client.protocol.NlsClient;
import com.alibaba.nls.client.protocol.OutputFormatEnum;
import com.alibaba.nls.client.protocol.SampleRateEnum;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 此示例演示了:
* 語音合成API調(diào)用。
* 動態(tài)獲取token。
* 流式合成TTS。
* 首包延遲計(jì)算。
* 字級別時(shí)間戳返回。
*/
public class SpeechSynthesizerDemo {
private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesizerDemo.class);
private static long startTime;
private String appKey;
NlsClient client;
public SpeechSynthesizerDemo(String appKey, String token) {
this.appKey = appKey;
//應(yīng)用全局創(chuàng)建一個(gè)NlsClient實(shí)例,默認(rèn)服務(wù)地址為阿里云線上服務(wù)地址。
try {
client = new NlsClient(token);
} catch (IOException e) {
e.printStackTrace();
}
}
public SpeechSynthesizerDemo(String appKey, String token, String url) {
this.appKey = appKey;
try {
if(url.isEmpty()) {
client = new NlsClient(token);
}else {
client = new NlsClient(url, token);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static SpeechSynthesizerListener getSynthesizerListener() {
SpeechSynthesizerListener listener = null;
try {
listener = new SpeechSynthesizerListener() {
File f=new File("tts_test.wav");
FileOutputStream fout = new FileOutputStream(f);
private boolean firstRecvBinary = true;
//語音合成結(jié)束
@Override
public void onComplete(SpeechSynthesizerResponse response) {
//調(diào)用onComplete時(shí)表示所有TTS數(shù)據(jù)已接收完成,因此為整個(gè)合成數(shù)據(jù)的延遲。該延遲可能較大,不一定滿足實(shí)時(shí)場景。
System.out.println("name: " + response.getName() +
", task_id: " + response.getTaskId() +
", status: " + response.getStatus()+
", output file :"+f.getAbsolutePath()
);
}
//語音合成的語音二進(jìn)制數(shù)據(jù)
@Override
public void onMessage(ByteBuffer message) {
try {
if(firstRecvBinary) {
//計(jì)算首包語音流的延遲,收到第一包語音流時(shí),即可以進(jìn)行語音播放,以提升響應(yīng)速度(特別是實(shí)時(shí)交互場景下)。
firstRecvBinary = false;
long now = System.currentTimeMillis();
logger.info("tts first latency : " + (now - SpeechSynthesizerDemo.startTime) + " ms");
}
byte[] bytesArray = new byte[message.remaining()];
message.get(bytesArray, 0, bytesArray.length);
fout.write(bytesArray);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onMetaInfo(SpeechSynthesizerResponse response) {
// 調(diào)用onMetaInfo時(shí)表示返回字級別時(shí)間戳
System.out.println("name: " + response.getName() + ", task_id: " + response.getTaskId());
JSONArray subtitles = (JSONArray)response.getObject("subtitles");
List<Map> subtitleList = subtitles.toJavaList(Map.class);
for (Map word : subtitleList) {
System.out.println("current subtitle: " + word);
}
}
@Override
public void onFail(SpeechSynthesizerResponse response){
//task_id是調(diào)用方和服務(wù)端通信的唯一標(biāo)識,當(dāng)遇到問題時(shí)需要提供task_id以便排查。
System.out.println(
"task_id: " + response.getTaskId() +
//狀態(tài)碼 20000000 表示識別成功
", status: " + response.getStatus() +
//錯(cuò)誤信息
", status_text: " + response.getStatusText());
}
};
} catch (Exception e) {
e.printStackTrace();
}
return listener;
}
public void process() {
SpeechSynthesizer synthesizer = null;
try {
//創(chuàng)建實(shí)例,建立連接。
synthesizer = new SpeechSynthesizer(client, getSynthesizerListener());
synthesizer.setAppKey(appKey);
//設(shè)置返回音頻的編碼格式
synthesizer.setFormat(OutputFormatEnum.WAV);
//設(shè)置返回音頻的采樣率
synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
//發(fā)音人
synthesizer.setVoice("siyue");
//語調(diào),范圍是-500~500,可選,默認(rèn)是0。
synthesizer.setPitchRate(100);
//語速,范圍是-500~500,默認(rèn)是0。
synthesizer.setSpeechRate(100);
//設(shè)置用于語音合成的文本
synthesizer.setText("歡迎使用阿里巴巴智能語音合成服務(wù),您可以說北京明天天氣怎么樣啊");
// 是否開啟字幕功能(返回相應(yīng)文本的時(shí)間戳),默認(rèn)不開啟,需要注意并非所有發(fā)音人都支持該參數(shù)。
synthesizer.addCustomedParam("enable_subtitle", true);
//此方法將以上參數(shù)設(shè)置序列化為JSON格式發(fā)送給服務(wù)端,并等待服務(wù)端確認(rèn)。
long start = System.currentTimeMillis();
synthesizer.start();
logger.info("tts start latency " + (System.currentTimeMillis() - start) + " ms");
SpeechSynthesizerDemo.startTime = System.currentTimeMillis();
//等待語音合成結(jié)束
synthesizer.waitForComplete();
logger.info("tts stop latency " + (System.currentTimeMillis() - start) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
//關(guān)閉連接
if (null != synthesizer) {
synthesizer.close();
}
}
}
public void shutdown() {
client.shutdown();
}
public static void main(String[] args) throws Exception {
String appKey = "您的Appkey";
String token = "您的Token";
String url = ""; //默認(rèn)值:wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1
if (args.length == 2) {
appKey = args[0];
token = args[1];
} else if (args.length == 3) {
appKey = args[0];
token = args[1];
url = args[3];
} else {
System.err.println("run error, need params(url is optional): " + "<app-key> <Token> [url]");
System.exit(-1);
}
SpeechSynthesizerDemo demo = new SpeechSynthesizerDemo(appKey, token, url);
demo.process();
demo.shutdown();
}
}