diff --git a/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/MiniMax.java b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/MiniMax.java index 6e90eddfa..b313771f3 100644 --- a/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/MiniMax.java +++ b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/MiniMax.java @@ -1,16 +1,33 @@ package run.mone.ai.minimax; +import com.google.gson.Gson; import okhttp3.*; +import run.mone.ai.minimax.bo.RequestBodyContent; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.util.concurrent.TimeUnit; public class MiniMax { + static Gson gson = new Gson(); + public static byte[] call_Text_To_Speech(String groupId, String authorization, RequestBodyContent content) { + + if (groupId == null || groupId.trim().length() < 1) { + throw new RuntimeException("groupId is null"); + } + + if (authorization == null || authorization.trim().length() < 1) { + throw new RuntimeException("authorization is null"); + } + + if (content == null) { + throw new RuntimeException("content is null"); + } + + if (content.getText() == null) { + throw new RuntimeException("content.text is null"); + } - public static byte[] call_Text_To_Speech(String groupId, String authorization, String content) { OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).build(); // 设置请求体的内容类型和内容 MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); @@ -20,7 +37,7 @@ public static byte[] call_Text_To_Speech(String groupId, String authorization, S // 构建请求 Request request = new Request.Builder() .url(url) - .post(RequestBody.create(mediaType, content)) + .post(RequestBody.create(mediaType, gson.toJson(content))) .addHeader("Content-Type", "application/json") .addHeader("Authorization","Bearer "+authorization) .build(); diff --git a/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/ModelEnum.java b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/ModelEnum.java new file mode 100644 index 000000000..c5ff6d71d --- /dev/null +++ b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/ModelEnum.java @@ -0,0 +1,13 @@ +package run.mone.ai.minimax.bo; + +public enum ModelEnum { + + Speech01("speech-01"), + Speech02("speech-02"); + + public String modelName; + + ModelEnum(String name) { + this.modelName = name; + } +} diff --git a/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/RequestBodyContent.java b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/RequestBodyContent.java new file mode 100644 index 000000000..d001e3bb7 --- /dev/null +++ b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/RequestBodyContent.java @@ -0,0 +1,39 @@ +package run.mone.ai.minimax.bo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class RequestBodyContent { + + @Builder.Default + private String model = ModelEnum.Speech01.modelName; + + @Builder.Default + private String voice_id = VoiceIdEnum.male_qn_qingse.voiceId; + + private String text; + + /** + * 范围[0.5, 2],取值越大,语速越快 + */ + @Builder.Default + private double speed = 1.0; + + /** + * 范围(0, 10],取值越大,音量越高 + */ + @Builder.Default + private double vol = 1.0; + + /** + * 默认值为mp3,可选范围:mp3、wav、pcm、flac、aac + */ + @Builder.Default + private String output_format = "mp3"; +} diff --git a/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/VoiceIdEnum.java b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/VoiceIdEnum.java new file mode 100644 index 000000000..317d2e86b --- /dev/null +++ b/jcommon/ai/minimax/src/main/java/run/mone/ai/minimax/bo/VoiceIdEnum.java @@ -0,0 +1,120 @@ +package run.mone.ai.minimax.bo; + +public enum VoiceIdEnum { + + /** + * 青涩青年音色:male-qn-qingse + */ + male_qn_qingse("male-qn-qingse"), + + /** + * 精英青年音色:male-qn-jingying + */ + male_qn_jingying("male-qn-jingying"), + + /** + * 霸道青年音色:male-qn-badao + */ + male_qn_badao("male-qn-badao"), + + /** + * 青年大学生音色:male-qn-daxuesheng + */ + male_qn_daxuesheng("male-qn-daxuesheng"), + + /** + * 少女音色:female-shaonv + */ + female_shaonv("female-shaonv"), + + /** + * 御姐音色:female-yujie + */ + female_yujie("female-yujie"), + + /** + * 成熟女性音色:female-chengshu + */ + female_chengshu("female-chengshu"), + + /** + * 甜美女性音色:female-tianmei + */ + female_tianmei("female-tianmei"), + + /** + * 男性主持人:presenter_male + */ + presenter_male("presenter_male"), + + /** + * 女性主持人:presenter_female + */ + presenter_female("presenter_female"), + + /** + * 男性有声书1:audiobook_male_1 + */ + audiobook_male_1("audiobook_male_1"), + + /** + * 男性有声书2:audiobook_male_2 + */ + audiobook_male_2("audiobook_male_2"), + + /** + * 女性有声书1:audiobook_female_1 + */ + audiobook_female_1("audiobook_female_1"), + + /** + * 女性有声书2:audiobook_female_2 + */ + audiobook_female_2("audiobook_female_2"), + + /** + * 青涩青年音色-beta:male-qn-qingse-jingpin + */ + male_qn_qingse_jingpin("male-qn-qingse-jingpin"), + + /** + * 精英青年音色-beta:male-qn-jingying-jingpin + */ + male_qn_jingying_jingpin("male-qn-jingying-jingpin"), + + /** + * 霸道青年音色-beta:male-qn-badao-jingpin + */ + male_qn_badao_jingpin("male-qn-badao-jingpin"), + + /** + * 青年大学生音色-beta:male-qn-daxuesheng-jingpin + */ + male_qn_daxuesheng_jingpin("male-qn-daxuesheng-jingpin"), + + /** + * 少女音色-beta:female-shaonv-jingpin + */ + female_shaonv_jingpin("female-shaonv-jingpin"), + + /** + * 御姐音色-beta:female-yujie-jingpin + */ + female_yujie_jingpin("female-yujie-jingpin"), + + /** + * 成熟女性音色-beta:female-chengshu-jingpin + */ + female_chengshu_jingpin("female-chengshu-jingpin"), + + /** + * 甜美女性音色-beta:female-tianmei-jingpin + */ + female_tianmei_jingpin("female-tianmei-jingpin"); + + public String voiceId; + + VoiceIdEnum(String id) { + this.voiceId = id; + } +} diff --git a/jcommon/ai/minimax/src/test/java/run/mone/ai/minimax/test/MiniMaxTest.java b/jcommon/ai/minimax/src/test/java/run/mone/ai/minimax/test/MiniMaxTest.java index 880b2fb90..67c1ba4ec 100644 --- a/jcommon/ai/minimax/src/test/java/run/mone/ai/minimax/test/MiniMaxTest.java +++ b/jcommon/ai/minimax/src/test/java/run/mone/ai/minimax/test/MiniMaxTest.java @@ -3,18 +3,25 @@ import com.google.gson.JsonObject; import org.junit.Test; import run.mone.ai.minimax.MiniMax; +import run.mone.ai.minimax.bo.RequestBodyContent; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; public class MiniMaxTest { @Test public void testCall() { try { - JsonObject json = new JsonObject(); - json.addProperty("voice_id", "male-qn-qingse"); - json.addProperty("text", "你好,北京今天天气很好!"); - json.addProperty("model", "speech-01"); - String content = json.toString(); - MiniMax.call_Text_To_Speech("", "", content); + RequestBodyContent requestBodyContent = new RequestBodyContent(); + requestBodyContent.setText("你好,今天北京的天气非常晴朗"); + byte[] bytes = MiniMax.call_Text_To_Speech("", "", requestBodyContent); + try (OutputStream outputStream = new FileOutputStream("audio.mp3")) { + outputStream.write(bytes); + } catch (IOException e) { + e.printStackTrace(); + } } catch (Exception e) { System.out.println(e); }