ws 3 ani în urmă
părinte
comite
b35f4730c7

+ 8 - 0
xfyun-api/src/main/java/com/jd/controller/AwakenController.java

@@ -14,6 +14,7 @@ import org.springframework.web.multipart.MultipartFile;
 import com.jd.util.IatTool;
 import com.jd.util.TtsTool;
 
+import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONException;
 import cn.hutool.json.JSONObject;
 
@@ -72,4 +73,11 @@ public class AwakenController {
 		map.put("data", ttsTool.textToVoice(txt, null));
 		return map;
 	}
+	
+	@RequestMapping("/getBotMsg")
+	public Map<String, Object> getBotMsg(String txt) {
+		Map<String, Object> map = new HashMap<>();
+		map.put("data", HttpUtil.get("https://api.ownthink.com/bot?appid=4053ba474ddf292e39da85ffdc211478&spoken=" + txt));
+		return map;
+	}
 }

+ 130 - 0
xfyun-api/src/main/java/com/jd/controller/VoiceController.java

@@ -0,0 +1,130 @@
+package com.jd.controller;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+import javax.websocket.OnClose;
+import javax.websocket.OnError;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.server.ServerEndpoint;
+
+import org.springframework.stereotype.Component;
+
+import com.jd.util.IatTool;
+
+import cn.hutool.json.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@Component
+@ServerEndpoint(value = "/xfyun/socket/voice")
+public class VoiceController {
+	
+	private String appid = "5c20ae83";
+	private String uploadFile = "D:/workspace/xfyunAip/uploadfile";
+	
+	private static AtomicInteger onlineCount = new AtomicInteger(0);
+
+	@OnOpen
+	public void onOpen(Session session) {
+		onlineCount.incrementAndGet();
+		log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get());
+	}
+
+	@OnClose
+	public void onClose(Session session) {
+		onlineCount.decrementAndGet();
+		log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());
+	}
+
+	@OnError
+	public void onError(Session session, Throwable error) {
+		log.error("发生错误");
+		error.printStackTrace();
+	}
+	
+	ByteArrayOutputStream baos = new ByteArrayOutputStream();
+	
+	AudioFormat audioFormat = getAudioFormat();
+	
+	private boolean status = false;
+	
+	@OnMessage
+	public void onMessage(String data, Session session) {
+		if ("start".equals(data)) {
+			baos.reset();
+			status = true;
+		} else if ("end".equals(data)) {
+			status = false;
+			JSONObject obj = new JSONObject();
+			obj.set("type", "asr");
+			obj.set("data", saveWav());
+			try {
+				session.getBasicRemote().sendText(obj.toJSONString(0));
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+	}
+
+	@OnMessage
+	public void onMessage(InputStream inputStream, Session session) {
+		if (!status) {
+			return;
+		}
+		// 一个帧 160ms的音频数据
+		byte[] buffer = new byte[4096];
+		try {
+			inputStream.read(buffer);
+			baos.write(buffer);
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		
+	}
+	IatTool iatTool = new IatTool(appid);
+	private String saveWav() {
+		try {
+			byte audioData[] = baos.toByteArray();
+			ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
+			AudioInputStream ais = new AudioInputStream(bais, audioFormat,
+					audioData.length / audioFormat.getFrameSize());
+			// 定义最终保存的文件名
+			System.out.println("开始生成语音文件");
+			
+			String path = uploadFile + "/" + System.currentTimeMillis() + ".wav";
+			File file = new File(path);
+			AudioSystem.write(ais, AudioFileFormat.Type.WAVE, file);
+			ais.close();
+			bais.close();
+			baos.reset();
+			
+			String txt = iatTool.RecognizePcmfileByte(file);
+			file.delete();
+			return txt;
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return "";
+	}
+
+	public static AudioFormat getAudioFormat() {
+		AudioFormat audioFormat = new AudioFormat(16000F, 16, 1, true, false);
+		// true,false 指示是以 big-endian 顺序还是以 little-endian 顺序存储音频数据。
+		return audioFormat;// 构造具有线性 PCM 编码和给定参数的 AudioFormat。
+	}
+
+}
+

+ 44 - 0
xfyun-api/src/main/java/com/jd/util/IatTool.java

@@ -1,6 +1,10 @@
 package com.jd.util;
 
 import com.iflytek.cloud.speech.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.multipart.MultipartFile;
@@ -54,6 +58,46 @@ public class IatTool {
         }
         return null;
     }
+    
+    public String RecognizePcmfileByte(File audioFile) {
+    	 
+        curRet = new StringBuilder();
+ 
+        try {
+            if (recognizer == null) {
+                recognizer = SpeechRecognizer.createRecognizer();
+                recognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
+                recognizer.setParameter( SpeechConstant.RESULT_TYPE, "plain" );
+            }
+ 
+            recognizer.startListening(recListener);
+            
+            FileInputStream fis = new FileInputStream(audioFile);
+            byte[] buffer = new byte[(int) audioFile.length()];
+            fis.read(buffer);
+            fis.close();
+ 
+//            byte[] buffer = audioFile.getBytes();
+            if (buffer == null || buffer.length == 0) {
+                LOGGER.error("no audio avaible!");
+                recognizer.cancel();
+            } else {
+ 
+                int lenRead = buffer.length;
+                recognizer.writeAudio( buffer, 0, lenRead );
+                recognizer.stopListening();
+                synchronized (lock) {
+                    lock.wait();//主线程等待
+                }
+ 
+                return curRet.toString();
+            }
+ 
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
  
     private RecognizerListener recListener = new RecognizerListener() {
         @Override