|
@@ -0,0 +1,111 @@
|
|
|
+package com.sf.ai.controller.baidu;
|
|
|
+
|
|
|
+import com.baidu.aip.speech.AipSpeech;
|
|
|
+import com.baidu.aip.speech.TtsResponse;
|
|
|
+import com.baidu.aip.util.Util;
|
|
|
+import com.sf.ai.util.AudioUtils;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+@RestController
|
|
|
+public class SampleController {
|
|
|
+ //设置APPID/AK/SK
|
|
|
+// public static final String APP_ID = "你的 App ID";
|
|
|
+// public static final String API_KEY = "你的 Api Key";
|
|
|
+// public static final String SECRET_KEY = "你的 Secret Key";
|
|
|
+
|
|
|
+ @Value("${baidu.app-id}")
|
|
|
+ private String APP_ID;
|
|
|
+ @Value("${baidu.api-key}")
|
|
|
+ private String API_KEY;
|
|
|
+ @Value("${baidu.secret-key}")
|
|
|
+ private String SECRET_KEY;
|
|
|
+
|
|
|
+ // http://localhost:8090/baidu/tts?message=
|
|
|
+ @GetMapping("/baidu/tts")
|
|
|
+ public String tts(@RequestParam("message") String message) {
|
|
|
+ // 初始化一个AipSpeech
|
|
|
+ AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
|
|
|
+
|
|
|
+ // 可选:设置网络连接参数
|
|
|
+ client.setConnectionTimeoutInMillis(20000);
|
|
|
+ client.setSocketTimeoutInMillis(60000);
|
|
|
+
|
|
|
+ // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
|
|
|
+// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
|
|
|
+// client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
|
|
|
+
|
|
|
+ // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
|
|
|
+ // 也可以直接通过jvm启动参数设置此环境变量
|
|
|
+// System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
|
|
|
+
|
|
|
+ // 调用接口
|
|
|
+ HashMap<String, Object> options = new HashMap<String, Object>();
|
|
|
+ options.put("spd", "5");
|
|
|
+ options.put("pit", "5");
|
|
|
+ options.put("per", "4");
|
|
|
+ TtsResponse res = client.synthesis(message, "zh", 1, options);
|
|
|
+ byte[] data = res.getData();
|
|
|
+ JSONObject res1 = res.getResult();
|
|
|
+ if (data != null) {
|
|
|
+ try {
|
|
|
+ Util.writeBytesToFileSystem(data, "baiduOutput.mp3");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res1 != null) {
|
|
|
+ System.out.println(res1.toString(2));
|
|
|
+ }
|
|
|
+
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // http://localhost:8090/baidu/audio2Text
|
|
|
+ @SneakyThrows
|
|
|
+ @GetMapping("/baidu/audio2Text")
|
|
|
+ public String audio2Text() {
|
|
|
+ // 初始化一个AipSpeech
|
|
|
+ AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
|
|
|
+
|
|
|
+ // 可选:设置网络连接参数
|
|
|
+ client.setConnectionTimeoutInMillis(20000);
|
|
|
+ client.setSocketTimeoutInMillis(60000);
|
|
|
+
|
|
|
+ // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
|
|
|
+// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
|
|
|
+// client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
|
|
|
+
|
|
|
+ // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
|
|
|
+ // 也可以直接通过jvm启动参数设置此环境变量
|
|
|
+// System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
|
|
|
+
|
|
|
+ // 项目根路径
|
|
|
+ String path = System.getProperty("user.dir");
|
|
|
+ // File.separator 文件的分隔符 mac / windows \\
|
|
|
+ String mp3FileName = path + File.separator + "baiduOutput.mp3";
|
|
|
+ String pcmFileName = path + File.separator + "baiduOutput.pcm";
|
|
|
+ AudioUtils.mp3ToPcm(mp3FileName, pcmFileName);
|
|
|
+
|
|
|
+ // 调用接口
|
|
|
+// JSONObject res = client.asr(pcmFileName, "pcm", 16000, null);
|
|
|
+// System.out.println(res.toString(2));
|
|
|
+
|
|
|
+
|
|
|
+ HashMap<String, Object> options = new HashMap<>();
|
|
|
+ options.put("dev_pid","1737");
|
|
|
+ byte[] data = Util.readFileByBytes(mp3FileName); //readFileByBytes仅为获取二进制数据示例
|
|
|
+ JSONObject asrRes2 = client.asr(data, "pcm", 16000, options);
|
|
|
+ System.out.println(asrRes2);
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+}
|