Compare commits
2 Commits
8f20b63593
...
2376d43b67
Author | SHA1 | Date |
---|---|---|
|
2376d43b67 | 2 months ago |
|
507d7dac9c | 2 months ago |
@ -0,0 +1,103 @@
|
|||||||
|
package com.pjilisense.flxai.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.TypeReference;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import javax.crypto.Mac;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class TtsUtils {
|
||||||
|
|
||||||
|
@Value("${tts.url.server}")
|
||||||
|
private String serverUrl;//http://192.168.200.152:8080/v1/tts
|
||||||
|
|
||||||
|
@Value("${tts.url.path}")
|
||||||
|
private String path;
|
||||||
|
public boolean sendText(String text,String voiceId,String destFile) {
|
||||||
|
try {
|
||||||
|
long ts = System.currentTimeMillis() / 1000;
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
boolean result = OkHttpUtils.builder()
|
||||||
|
.url(serverUrl+path)
|
||||||
|
.addParam(genBodyJson(text,voiceId))
|
||||||
|
.addHeader("timestamp", String.valueOf(ts))
|
||||||
|
.addHeader("Content-Type", " application/json;charset=utf-8")
|
||||||
|
.post(true).syncwav(destFile);
|
||||||
|
System.out.println("result="+result);
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
return result;
|
||||||
|
}catch(Exception e){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public String genBodyJson(String text,String voiceId) {
|
||||||
|
LinkedHashMap<String, Object> postParam = new LinkedHashMap<>();
|
||||||
|
postParam.put("text", text);
|
||||||
|
postParam.put("chunk_length", 200);
|
||||||
|
postParam.put("format", "wav");
|
||||||
|
postParam.put("references", new ArrayList<Object>());
|
||||||
|
postParam.put("reference_id", voiceId);
|
||||||
|
postParam.put("seed", null);
|
||||||
|
postParam.put("use_memory_cache", "never");
|
||||||
|
postParam.put("normalize", true);
|
||||||
|
postParam.put("opus_bitrate", -1000);
|
||||||
|
postParam.put("latency", "normal");
|
||||||
|
postParam.put("streaming", false);
|
||||||
|
postParam.put("max_new_tokens", 1024);
|
||||||
|
postParam.put("top_p", 0.7);
|
||||||
|
postParam.put("repetition_penalty", 1.2);
|
||||||
|
postParam.put("temperature", 0.7);
|
||||||
|
String json = JSON.toJSONString(postParam);
|
||||||
|
// parser.add_argument(
|
||||||
|
// "--reference_audio",
|
||||||
|
// "-ra",
|
||||||
|
// type=str,
|
||||||
|
// nargs="+",
|
||||||
|
// default=None,
|
||||||
|
// help="Path to the audio file",
|
||||||
|
// )
|
||||||
|
// parser.add_argument(
|
||||||
|
// "--reference_text",
|
||||||
|
// "-rt",
|
||||||
|
// type=str,
|
||||||
|
// nargs="+",
|
||||||
|
// default=None,
|
||||||
|
// help="Reference text for voice synthesis",
|
||||||
|
// )
|
||||||
|
// parser.add_argument(
|
||||||
|
// "--output",
|
||||||
|
// "-o",
|
||||||
|
// type=str,
|
||||||
|
// default="generated_audio",
|
||||||
|
// help="Output audio file name",
|
||||||
|
// )
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
public String hmacSHA256(String appKey, String data) {
|
||||||
|
try {
|
||||||
|
// Create HMAC-SHA256 key from the given secret
|
||||||
|
SecretKeySpec secretKeySpec = new SecretKeySpec(appKey.getBytes(), "HmacSHA256");
|
||||||
|
// Get an instance of Mac object implementing HMAC-SHA256
|
||||||
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
mac.init(secretKeySpec);
|
||||||
|
// Calculate the HMAC value
|
||||||
|
byte[] hmacBytes = mac.doFinal(data.getBytes());
|
||||||
|
// Convert result into a hexadecimal string
|
||||||
|
StringBuilder sb = new StringBuilder(hmacBytes.length * 2);
|
||||||
|
for (byte b : hmacBytes) {
|
||||||
|
sb.append(String.format("%02x", b));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||||
|
throw new RuntimeException("Failed to calculate HMAC-SHA256", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue