|
@ -1,89 +1,92 @@
|
|
package com.yihu.jw.care.util;
|
|
package com.yihu.jw.care.util;
|
|
|
|
|
|
import com.tencent.SpeechClient;
|
|
|
|
import com.tencent.tts.model.SpeechSynthesisRequest;
|
|
|
|
import com.tencent.tts.model.SpeechSynthesisResponse;
|
|
|
|
import com.tencent.tts.service.SpeechSynthesisListener;
|
|
|
|
import com.tencent.tts.service.SpeechSynthesizer;
|
|
|
|
import com.tencent.tts.utils.Ttsutils;
|
|
import com.tencent.tts.utils.Ttsutils;
|
|
|
|
import com.tencentcloudapi.common.Credential;
|
|
|
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
|
|
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
|
|
|
import com.tencentcloudapi.tts.v20190823.TtsClient;
|
|
|
|
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceRequest;
|
|
|
|
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceResponse;
|
|
|
|
import com.yihu.jw.care.constant.CommonConstant;
|
|
|
|
import com.yihu.jw.care.dao.log.BaseWordToVoiceDao;
|
|
|
|
import com.yihu.jw.entity.care.log.BaseWordToVoiceDO;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created by yeshijie on 2022/7/6.
|
|
* Created by yeshijie on 2022/7/6.
|
|
*/
|
|
*/
|
|
|
|
@Component
|
|
public class SpeechTtsUtil {
|
|
public class SpeechTtsUtil {
|
|
private static String codec = "pcm";
|
|
|
|
private static int sampleRate = 16000;
|
|
|
|
|
|
|
|
private static byte[] datas = new byte[0];
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SpeechTtsUtil.class);
|
|
|
|
|
|
|
|
private static String codec = "mp3";
|
|
|
|
|
|
private static final String appId = "appId";
|
|
|
|
private static final String secretId = "secretId";
|
|
|
|
private static final String secretKey = "secretKey";
|
|
|
|
|
|
private static final String appId = "1305960513";
|
|
|
|
private static final String secretId = "AKIDwhBPN5WgYAVaO2QXNwEF0UieJhpgGZbN";
|
|
|
|
private static final String secretKey = "WliPpdBSXMuBBuNZU8VbQyFG0TfaEbhX";
|
|
|
|
@Value("${spring.profiles.active}")
|
|
|
|
private String profiles;
|
|
|
|
@Autowired
|
|
|
|
private BaseWordToVoiceDao wordToVoiceDao;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 语音合成
|
|
|
|
* @param args
|
|
|
|
* @throws IOException
|
|
|
|
|
|
* 腾讯文字合成语音
|
|
|
|
* @param text
|
|
|
|
* @return
|
|
*/
|
|
*/
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
//创建SpeechSynthesizerClient实例,目前是单例
|
|
|
|
SpeechClient client = SpeechClient.newInstance(appId, secretId, secretKey);
|
|
|
|
//初始化SpeechSynthesizerRequest,SpeechSynthesizerRequest包含请求参数
|
|
|
|
SpeechSynthesisRequest request = SpeechSynthesisRequest.initialize();
|
|
|
|
request.setCodec(codec);
|
|
|
|
//request.setSampleRate(sampleRate);
|
|
|
|
//request.setVolume(10);
|
|
|
|
//request.setSpeed(2f);
|
|
|
|
request.setVoiceType(101007);
|
|
|
|
//使用客户端client创建语音合成实例
|
|
|
|
SpeechSynthesizer speechSynthesizer = client.newSpeechSynthesizer(request, new MySpeechSynthesizerListener());
|
|
|
|
//执行语音合成
|
|
|
|
String ttsText = "腾讯云语音合成测试";
|
|
|
|
speechSynthesizer.synthesis(ttsText);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class MySpeechSynthesizerListener extends SpeechSynthesisListener {
|
|
|
|
|
|
|
|
private AtomicInteger sessionId = new AtomicInteger(0);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComplete(SpeechSynthesisResponse response) {
|
|
|
|
System.out.println("onComplete");
|
|
|
|
if (response.getSuccess()) {
|
|
|
|
//根据具体的业务选择逻辑处理
|
|
|
|
//Ttsutils.saveResponseToFile(response.getAudio(),"./111.mp3");
|
|
|
|
if ("pcm".equals(codec)) {
|
|
|
|
//pcm 转 wav
|
|
|
|
Ttsutils.responsePcm2Wav(sampleRate, response.getAudio(), response.getSessionId());
|
|
|
|
|
|
public String wordToVoice(String text){
|
|
|
|
synchronized (text.intern()){
|
|
|
|
try{
|
|
|
|
if("hzprod".equals(profiles)){
|
|
|
|
return CommonConstant.BASEURL + "voice/test.mp3";
|
|
}
|
|
}
|
|
if ("opus".equals(codec)) {
|
|
|
|
//opus
|
|
|
|
System.out.println("OPUS:" + response.getSessionId() + " length:" + response.getAudio().length);
|
|
|
|
|
|
BaseWordToVoiceDO voiceDO = wordToVoiceDao.findByWord(text);
|
|
|
|
if(voiceDO!=null){
|
|
|
|
return voiceDO.getVoice();
|
|
}
|
|
}
|
|
|
|
String sessionId = UUID.randomUUID().toString().replace("-","");
|
|
|
|
Credential cred = new Credential(secretId, secretKey);
|
|
|
|
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
|
httpProfile.setEndpoint(CommonConstant.TXVOICEBASEURL);
|
|
|
|
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
|
ClientProfile clientProfile = new ClientProfile();
|
|
|
|
clientProfile.setHttpProfile(httpProfile);
|
|
|
|
// 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
|
TtsClient client = new TtsClient(cred, "ap-shanghai", clientProfile);
|
|
|
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
|
TextToVoiceRequest req = new TextToVoiceRequest();
|
|
|
|
req.setText(text);
|
|
|
|
req.setSessionId(sessionId);
|
|
|
|
req.setVoiceType(10510000L);
|
|
|
|
req.setVolume(10F);
|
|
|
|
req.setCodec(codec);
|
|
|
|
// 返回的resp是一个TextToVoiceResponse的实例,与请求对象对应
|
|
|
|
TextToVoiceResponse resp = client.TextToVoice(req);
|
|
|
|
// 输出json格式的字符串回包
|
|
|
|
// logger.info(TextToVoiceResponse.toJsonString(resp));
|
|
|
|
byte[] buffer = new BASE64Decoder().decodeBuffer(resp.getAudio());
|
|
|
|
Ttsutils.saveResponseToFile(buffer,CommonConstant.TXVOICEPATH + sessionId+".mp3");
|
|
|
|
voiceDO = new BaseWordToVoiceDO();
|
|
|
|
voiceDO.setWord(text);
|
|
|
|
voiceDO.setCreateTime(new Date());
|
|
|
|
voiceDO.setVoice(CommonConstant.BASEURL + "voice/"+sessionId+".mp3");
|
|
|
|
wordToVoiceDao.save(voiceDO);
|
|
|
|
logger.info("ending.....");
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
System.out.println("结束:" + response.getSuccess() + " " + response.getCode()
|
|
|
|
+ " " + response.getMessage() + " " + response.getEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
//语音合成的语音二进制数据
|
|
|
|
@Override
|
|
|
|
public void onMessage(byte[] data) {
|
|
|
|
//System.out.println("onMessage:" + data.length);
|
|
|
|
// Your own logic.
|
|
|
|
System.out.println("onMessage length:" + data.length);
|
|
|
|
sessionId.incrementAndGet();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFail(SpeechSynthesisResponse response) {
|
|
|
|
System.out.println("onFail");
|
|
|
|
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|