From c2e4e3c2e9be855277d9f4fa9cd0544892ff829a Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期三, 30 八月 2023 09:57:30 +0800
Subject: [PATCH] Merge branch 'main' of github.com:alibaba-damo-academy/FunASR add

---
 funasr/runtime/java/FunasrWsClient.java |   38 +++++++++++++++++++++++++++++++++-----
 1 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/funasr/runtime/java/FunasrWsClient.java b/funasr/runtime/java/FunasrWsClient.java
index ec55c94..74a8695 100644
--- a/funasr/runtime/java/FunasrWsClient.java
+++ b/funasr/runtime/java/FunasrWsClient.java
@@ -50,7 +50,8 @@
   }
 
   public FunasrWsClient(URI serverURI) {
-    super(serverURI);
+    
+	super(serverURI);
   }
 
   public FunasrWsClient(URI serverUri, Map<String, String> httpHeaders) {
@@ -64,7 +65,7 @@
 
   // send json at first time
   public void sendJson(
-      String mode, String strChunkSize, int chunkInterval, String wavName, boolean isSpeaking) {
+      String mode, String strChunkSize, int chunkInterval, String wavName, boolean isSpeaking,String suffix) {
     try {
 
       JSONObject obj = new JSONObject();
@@ -78,6 +79,14 @@
       obj.put("chunk_size", array);
       obj.put("chunk_interval", new Integer(chunkInterval));
       obj.put("wav_name", wavName);
+	  if(FunasrWsClient.hotwords.trim().length()>0)
+	  {
+		  obj.put("hotwords", FunasrWsClient.hotwords.trim());
+	  }
+	  if(suffix.equals("wav")){
+	      suffix="pcm";
+	  }
+	  obj.put("wav_format", suffix);
       if (isSpeaking) {
         obj.put("is_speaking", new Boolean(true));
       } else {
@@ -114,7 +123,9 @@
 
   // function for rec wav file
   public void recWav() {
-    sendJson(mode, strChunkSize, chunkInterval, wavName, true);
+	String fileName=FunasrWsClient.wavPath;
+	String suffix=fileName.split("\\.")[fileName.split("\\.").length-1];
+    sendJson(mode, strChunkSize, chunkInterval, wavName, true,suffix);
     File file = new File(FunasrWsClient.wavPath);
 
     int chunkSize = sendChunkSize;
@@ -178,10 +189,18 @@
     try {
       jsonObject = (JSONObject) jsonParser.parse(message);
       logger.info("text: " + jsonObject.get("text"));
+	  if(jsonObject.containsKey("timestamp"))
+	  {
+		  logger.info("timestamp: " + jsonObject.get("timestamp"));
+	  }
     } catch (org.json.simple.parser.ParseException e) {
       e.printStackTrace();
     }
-    if (iseof && mode.equals("offline")) {
+    if (iseof && mode.equals("offline") && !jsonObject.containsKey("is_final")) {
+      close();
+    }
+	 
+    if (iseof && mode.equals("offline") && jsonObject.containsKey("is_final") && jsonObject.get("is_final").equals("false")) {
       close();
     }
   }
@@ -211,6 +230,7 @@
   static String strChunkSize = "5,10,5";
   static int chunkInterval = 10;
   static int sendChunkSize = 1920;
+  static String hotwords="";
 
   String wavName = "javatest";
 
@@ -259,6 +279,12 @@
         .setDefault("offline")
         .type(String.class)
         .required(false);
+    parser
+        .addArgument("--hotwords")
+        .help("hotwords, splited by space")
+        .setDefault("")
+        .type(String.class)
+        .required(false);
     String srvIp = "";
     String srvPort = "";
     String wavPath = "";
@@ -266,7 +292,7 @@
     String chunk_size = "";
     int chunk_interval = 10;
     String strmode = "offline";
-
+    String hot="";
     try {
       Namespace ns = parser.parseArgs(args);
       srvIp = ns.get("host");
@@ -276,6 +302,7 @@
       chunk_size = ns.get("chunk_size");
       chunk_interval = ns.get("chunk_interval");
       strmode = ns.get("mode");
+	  hot=ns.get("hotwords");
       System.out.println(srvPort);
 
     } catch (ArgumentParserException ex) {
@@ -287,6 +314,7 @@
     FunasrWsClient.chunkInterval = chunk_interval;
     FunasrWsClient.wavPath = wavPath;
     FunasrWsClient.mode = strmode;
+	FunasrWsClient.hotwords=hot;
     System.out.println(
         "serIp="
             + srvIp

--
Gitblit v1.9.1