整合后呼叫中心 api

TtsHelper.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package api.util.helper;
  2. import com.alibaba.fastjson2.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Base64;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. @Slf4j
  13. public class TtsHelper {
  14. private static String ttstype="modelscope";//ekho
  15. private static void transfer(String word, String filePath) {
  16. switch (ttstype) {
  17. case "ekho":
  18. try {
  19. String command = "ekho " + word + " -o " + filePath;
  20. Process process = Runtime.getRuntime().exec(command);
  21. process.waitFor();
  22. } catch (IOException | InterruptedException ex) {
  23. log.error("ekho异常", ex);
  24. }
  25. break;
  26. case "modelscope":
  27. Map<String, Object> param = new HashMap<>();
  28. param.put("input", word);
  29. Map<String, Object> param1 = new HashMap<>();
  30. param1.put("voice", "zhitian_emo");
  31. param.put("parameters", param1);
  32. String result = HttpHelper.post("http://192.168.1.81:8000/call", JSON.toJSONString(param));
  33. if (StringHelper.isNotEmpty(result)) {
  34. try {
  35. Map map = JSON.parseObject(result, Map.class);
  36. byte[] bytes = Base64.getDecoder().decode(map.get("output_wav").toString());
  37. File file = new File(filePath);
  38. FileOutputStream outputStream = new FileOutputStream(file);
  39. outputStream.write(bytes);
  40. outputStream.close();
  41. } catch (Exception ex) {
  42. log.error("modelscope异常", ex);
  43. }
  44. }
  45. break;
  46. }
  47. }
  48. public static final String curlUrl= SpringHelper.getRequiredProperty("spring.curlUrl");;
  49. public static String TextToSpeech(String word) {
  50. return TextToSpeech(word, "");
  51. }
  52. public static String TextToSpeech(String word, String filename) {
  53. String parentPath = "files/audio/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  54. File file = new File(parentPath);
  55. if (!file.exists()) {
  56. file.mkdirs();
  57. }
  58. if (StringHelper.isEmpty(filename)) {
  59. filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
  60. }
  61. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  62. transfer(word,filePath);
  63. String path= "/" +parentPath+"/" + filename + ".wav";
  64. return path;
  65. }
  66. public static String TextToSpeechAgent(String word, String filename) {
  67. String parentPath = "files/agent/";
  68. File file = new File(parentPath);
  69. if (!file.exists()) {
  70. file.mkdirs();
  71. }
  72. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  73. // if (!new File(filePath).exists()) {
  74. // transfer(word,filePath);
  75. // }
  76. return filePath;
  77. }
  78. public static String TextToSpeechPosition(String word, String filename) {
  79. String parentPath = "files/position/";
  80. File file = new File(parentPath);
  81. if (!file.exists()) {
  82. file.mkdirs();
  83. }
  84. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  85. // if (!new File(filePath).exists()) {
  86. // transfer(word,filePath);
  87. // }
  88. return filePath;
  89. }
  90. }