From c542eacb0aadcbc49c63db40429fca4e08f807a4 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期五, 21 七月 2023 10:27:35 +0800
Subject: [PATCH] Merge branch 'main' of github.com:alibaba-damo-academy/FunASR add

---
 funasr/runtime/websocket/websocket-server.cpp |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/funasr/runtime/websocket/websocket-server.cpp b/funasr/runtime/websocket/websocket-server.cpp
index 91b6c9d..59109b3 100644
--- a/funasr/runtime/websocket/websocket-server.cpp
+++ b/funasr/runtime/websocket/websocket-server.cpp
@@ -22,12 +22,11 @@
                                          std::string& s_keyfile) {
   namespace asio = websocketpp::lib::asio;
 
-  std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
-  std::cout << "using TLS mode: "
+  LOG(INFO) << "on_tls_init called with hdl: " << hdl.lock().get();
+  LOG(INFO) << "using TLS mode: "
             << (mode == MOZILLA_MODERN ? "Mozilla Modern"
-                                       : "Mozilla Intermediate")
-            << std::endl;
-
+                                       : "Mozilla Intermediate");
+                                       
   context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
       asio::ssl::context::sslv23);
 
@@ -49,7 +48,7 @@
     ctx->use_private_key_file(s_keyfile, asio::ssl::context::pem);
 
   } catch (std::exception& e) {
-    std::cout << "Exception: " << e.what() << std::endl;
+    LOG(INFO) << "Exception: " << e.what();
   }
   return ctx;
 }
@@ -62,13 +61,13 @@
     int num_samples = buffer.size();  // the size of the buf
 
     if (!buffer.empty()) {
-      // fout.write(buffer.data(), buffer.size());
       // feed data to asr engine
       FUNASR_RESULT Result = FunOfflineInferBuffer(
-          asr_hanlde, buffer.data(), buffer.size(), RASR_NONE, NULL, 16000);
+          asr_hanlde, buffer.data(), buffer.size(), RASR_NONE, NULL, 16000, msg["wav_format"]);
 
       std::string asr_result =
           ((FUNASR_RECOG_RESULT*)Result)->msg;  // get decode result
+      FunASRFreeResult(Result);
 
       websocketpp::lib::error_code ec;
       nlohmann::json jsonresult;        // result json
@@ -86,8 +85,7 @@
                       ec);
       }
 
-      std::cout << "buffer.size=" << buffer.size()
-                << ",result json=" << jsonresult.dump() << std::endl;
+      LOG(INFO) << "buffer.size=" << buffer.size() << ",result json=" << jsonresult.dump();
       if (!isonline) {
         //  close the client if it is not online asr
         // server_->close(hdl, websocketpp::close::status::normal, "DONE", ec);
@@ -109,15 +107,16 @@
                                            // connection
   data_msg->samples = std::make_shared<std::vector<char>>();
   data_msg->msg = nlohmann::json::parse("{}");
+  data_msg->msg["wav_format"] = "pcm";
   data_map.emplace(hdl, data_msg);
-  std::cout << "on_open, active connections: " << data_map.size() << std::endl;
+  LOG(INFO) << "on_open, active connections: " << data_map.size();
 }
 
 void WebSocketServer::on_close(websocketpp::connection_hdl hdl) {
   scoped_lock guard(m_lock);
   data_map.erase(hdl);  // remove data vector when  connection is closed
 
-  std::cout << "on_close, active connections: " << data_map.size() << std::endl;
+  LOG(INFO) << "on_close, active connections: " << data_map.size();
 }
 
 // remove closed connection
@@ -143,7 +142,7 @@
   }
   for (auto hdl : to_remove) {
     data_map.erase(hdl);
-    std::cout << "remove one connection " << std::endl;
+    LOG(INFO)<< "remove one connection ";
   }
 }
 void WebSocketServer::on_message(websocketpp::connection_hdl hdl,
@@ -161,7 +160,7 @@
 
   lock.unlock();
   if (sample_data_p == nullptr) {
-    std::cout << "error when fetch sample data vector" << std::endl;
+    LOG(INFO) << "error when fetch sample data vector";
     return;
   }
 
@@ -173,18 +172,21 @@
       if (jsonresult["wav_name"] != nullptr) {
         msg_data->msg["wav_name"] = jsonresult["wav_name"];
       }
+      if (jsonresult["wav_format"] != nullptr) {
+        msg_data->msg["wav_format"] = jsonresult["wav_format"];
+      }
 
       if (jsonresult["is_speaking"] == false ||
           jsonresult["is_finished"] == true) {
-        std::cout << "client done" << std::endl;
+        LOG(INFO) << "client done";
 
         if (isonline) {
           // do_close(ws);
         } else {
           // add padding to the end of the wav data
-          std::vector<short> padding(static_cast<short>(0.3 * 16000));
-          sample_data_p->insert(sample_data_p->end(), padding.data(),
-                                padding.data() + padding.size());
+          // std::vector<short> padding(static_cast<short>(0.3 * 16000));
+          // sample_data_p->insert(sample_data_p->end(), padding.data(),
+          //                       padding.data() + padding.size());
           // for offline, send all receive data to decoder engine
           asio::post(io_decoder_,
                      std::bind(&WebSocketServer::do_decoder, this,
@@ -225,9 +227,9 @@
     // init model with api
 
     asr_hanlde = FunOfflineInit(model_path, thread_num);
-    std::cout << "model ready" << std::endl;
+    LOG(INFO) << "model successfully inited";
 
   } catch (const std::exception& e) {
-    std::cout << e.what() << std::endl;
+    LOG(INFO) << e.what();
   }
 }

--
Gitblit v1.9.1