基础版中间件

TtsHelper.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package midware.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 void transfer(String word, String filePath) {
  15. String type=SpringHelper.getRequiredProperty("common.tts.type");
  16. switch (type) {
  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 url=SpringHelper.getRequiredProperty("common.tts.url");
  33. String result = HttpHelper.post(url, JSON.toJSONString(param));
  34. if (StringHelper.isNotEmpty(result)) {
  35. try {
  36. Map map = JSON.parseObject(result, Map.class);
  37. byte[] bytes = Base64.getDecoder().decode(map.get("output_wav").toString());
  38. File file = new File(filePath);
  39. FileOutputStream outputStream = new FileOutputStream(file);
  40. outputStream.write(bytes);
  41. outputStream.close();
  42. } catch (Exception ex) {
  43. log.error("modelscope异常", ex);
  44. }
  45. }
  46. break;
  47. }
  48. }
  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. return filePath;
  64. }
  65. public static String TextToSpeechWord(String word, String filename) {
  66. String parentPath = "files/audio/" ;
  67. File file = new File(parentPath);
  68. if (!file.exists()) {
  69. file.mkdirs();
  70. }
  71. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  72. if (!new File(filePath).exists()) {
  73. transfer(word,filePath);
  74. }
  75. return filePath;
  76. }
  77. public static String TextToSpeechAgent(String word, String filename) {
  78. String parentPath = "files/agent/";
  79. File file = new File(parentPath);
  80. if (!file.exists()) {
  81. file.mkdirs();
  82. }
  83. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  84. // if (!new File(filePath).exists()) {
  85. // transfer(word,filePath);
  86. // }
  87. return filePath;
  88. }
  89. public static String TextToSpeechPosition(String word, String filename) {
  90. String parentPath = "files/position/";
  91. File file = new File(parentPath);
  92. if (!file.exists()) {
  93. file.mkdirs();
  94. }
  95. String filePath = file.getAbsolutePath() + "/" + filename + ".wav";
  96. // if (!new File(filePath).exists()) {
  97. // transfer(word,filePath);
  98. // }
  99. return filePath;
  100. }
  101. }