java實(shí)現(xiàn)微軟文本轉(zhuǎn)語(yǔ)音(TTS)經(jīng)驗(yàn)總結(jié)
-
官網(wǎng)地址:
https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/quickstarts/setup-platform?tabs=windows%2Cubuntu%2Cdotnet%2Cjre%2Cmaven%2Cnodejs%2Cmac%2Cpypi&pivots=programming-language-java
-
參數(shù)文檔和其他文檔
https://docs.microsoft.com/zh-cn/java/api/com.microsoft.cognitiveservices.speech.speechconfig?view=azure-java-stable#com-microsoft-cognitiveservices-speech-speechconfig-fromsubscription(string-string)
-
選到語(yǔ)音名稱
在ssml選一個(gè)語(yǔ)音,會(huì)生成到左邊代碼里,voice name=‘xxxxxx’
https://azure.microsoft.com/zh-cn/services/cognitive-services/text-to-speech/#features
一、直接上代碼
方式一:直接調(diào)用
<dependency>
<groupId>com.microsoft.cognitiveservices.speech</groupId>
<artifactId>client-sdk</artifactId>
<version>1.12.1</version>
</dependency>
@PostMapping("/text-to-url")
@ApiOperation("地址識(shí)別")
public Result getArea(String text) {
SpeechConfig speechConfig = SpeechConfig.fromSubscription("你的apiKey", "eastasia");//key,地區(qū)
speechConfig.setSpeechSynthesisLanguage("zh-CN");//語(yǔ)言
speechConfig.setSpeechSynthesisVoiceName("zh-CN-XiaoxiaoNeural");//語(yǔ)言名稱
speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm);
SpeechSynthesizer synthesizer = new SpeechSynthesizer(speechConfig, null);
SpeechSynthesisResult result = synthesizer.SpeakText(text);
AudioDataStream stream = AudioDataStream.fromResult(result);
stream.saveToWavFile("D:/file.wav");//生成位置
stream.close();
return new Result();
}
方式二:ssml(因?yàn)閯e人寫過了,我這邊就不寫了,實(shí)測(cè)不好用,看了官方文檔,寫了方式一)
-
參考地址:http://t.zoukankan.com/aohongzhu-p-15174381.html
-
參考地址的原文地址:https://www.cnblogs.com/aohongzhu/p/15174381.html文章來源:http://www.zghlxwxcb.cn/news/detail-578033.html
改進(jìn)(退化成沒有redis)文章來源地址http://www.zghlxwxcb.cn/news/detail-578033.html
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
@Component
@Slf4j
public class Authentication {
private String assessToken = null;//token緩存
private Date setTime = null;//設(shè)置的有效時(shí)間
/**
* @param token 新獲取的token
*
*/
private void setToken(String token) {
this.assessToken = token;
this.setTime = new Date(new Date().getTime() + TtsConst.ACCESS_TOKEN_EXPIRE_TIME * 1000L);
}
/**
* 判斷是否在有效期前
* @return token
*/
private String getAssessToken() {
Date date = new Date();
if (date.getTime() - this.setTime.getTime() < 0L) {
return this.assessToken;
}
return null;
}
public String genAccessToken() {
InputStream inSt;
HttpsURLConnection webRequest;
try {
String accessToken = this.getAssessToken();
if (StringUtils.isEmpty(accessToken)) {
webRequest = HttpsConnection.getHttpsConnection(TtsConst.ACCESS_TOKEN_URI);
webRequest.setDoInput(true);
webRequest.setDoOutput(true);
webRequest.setConnectTimeout(5000);
webRequest.setReadTimeout(5000);
webRequest.setRequestMethod("POST");
byte[] bytes = new byte[0];
webRequest.setRequestProperty("content-length", String.valueOf(bytes.length));
webRequest.setRequestProperty("Ocp-Apim-Subscription-Key", TtsConst.API_KEY);
webRequest.connect();
DataOutputStream dop = new DataOutputStream(webRequest.getOutputStream());
dop.write(bytes);
dop.flush();
dop.close();
inSt = webRequest.getInputStream();
InputStreamReader in = new InputStreamReader(inSt);
BufferedReader bufferedReader = new BufferedReader(in);
StringBuilder strBuffer = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
strBuffer.append(line);
}
bufferedReader.close();
in.close();
inSt.close();
webRequest.disconnect();
accessToken = strBuffer.toString();
//設(shè)置accessToken的過期時(shí)間為9分鐘
this.setToken(accessToken);
log.info("獲取微軟tss token成功,token: {}", accessToken);
}
return accessToken;
} catch (Exception e) {
log.error("生成微軟tss token失敗,錯(cuò)誤信息:{}", e.getMessage());
}
return null;
}
}
到了這里,關(guān)于springboot微軟文本轉(zhuǎn)語(yǔ)音(texttospeach) java實(shí)現(xiàn)微軟文本轉(zhuǎn)語(yǔ)音的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!