funasr-wss-client-2pass.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaomingwork */
  6. // client for websocket, support multiple threads
  7. // ./funasr-wss-client --server-ip <string>
  8. // --port <string>
  9. // --wav-path <string>
  10. // [--thread-num <int>]
  11. // [--is-ssl <int>] [--]
  12. // [--version] [-h]
  13. // example:
  14. // ./funasr-wss-client --server-ip 127.0.0.1 --port 10095 --wav-path test.wav
  15. // --thread-num 1 --is-ssl 1
  16. #define ASIO_STANDALONE 1
  17. #include <glog/logging.h>
  18. #include "portaudio.h"
  19. #include <atomic>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <thread>
  24. #include <websocketpp/client.hpp>
  25. #include <websocketpp/common/thread.hpp>
  26. #include <websocketpp/config/asio_client.hpp>
  27. #include "util.h"
  28. #include "audio.h"
  29. #include "nlohmann/json.hpp"
  30. #include "tclap/CmdLine.h"
  31. #include "microphone.h"
  32. /**
  33. * Define a semi-cross platform helper method that waits/sleeps for a bit.
  34. */
  35. void WaitABit() {
  36. #ifdef WIN32
  37. Sleep(500);
  38. #else
  39. usleep(500);
  40. #endif
  41. }
  42. std::atomic<int> wav_index(0);
  43. typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
  44. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
  45. context_ptr;
  46. using websocketpp::lib::bind;
  47. using websocketpp::lib::placeholders::_1;
  48. using websocketpp::lib::placeholders::_2;
  49. context_ptr OnTlsInit(websocketpp::connection_hdl) {
  50. context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
  51. asio::ssl::context::sslv23);
  52. try {
  53. ctx->set_options(
  54. asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 |
  55. asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);
  56. } catch (std::exception& e) {
  57. LOG(ERROR) << e.what();
  58. }
  59. return ctx;
  60. }
  61. // template for tls or not config
  62. template <typename T>
  63. class WebsocketClient {
  64. public:
  65. // typedef websocketpp::client<T> client;
  66. // typedef websocketpp::client<websocketpp::config::asio_tls_client>
  67. // wss_client;
  68. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  69. WebsocketClient(int is_ssl) : m_open(false), m_done(false) {
  70. // set up access channels to only log interesting things
  71. m_client.clear_access_channels(websocketpp::log::alevel::all);
  72. m_client.set_access_channels(websocketpp::log::alevel::connect);
  73. m_client.set_access_channels(websocketpp::log::alevel::disconnect);
  74. m_client.set_access_channels(websocketpp::log::alevel::app);
  75. // Initialize the Asio transport policy
  76. m_client.init_asio();
  77. // Bind the handlers we are using
  78. using websocketpp::lib::bind;
  79. using websocketpp::lib::placeholders::_1;
  80. m_client.set_open_handler(bind(&WebsocketClient::on_open, this, _1));
  81. m_client.set_close_handler(bind(&WebsocketClient::on_close, this, _1));
  82. m_client.set_message_handler(
  83. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  84. on_message(hdl, msg);
  85. });
  86. m_client.set_fail_handler(bind(&WebsocketClient::on_fail, this, _1));
  87. m_client.clear_access_channels(websocketpp::log::alevel::all);
  88. }
  89. void on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
  90. const std::string& payload = msg->get_payload();
  91. switch (msg->get_opcode()) {
  92. case websocketpp::frame::opcode::text:
  93. nlohmann::json jsonresult = nlohmann::json::parse(payload);
  94. LOG(INFO) << "Thread: " << this_thread::get_id()
  95. << ",on_message = " << payload;
  96. if (jsonresult["is_final"] == true) {
  97. websocketpp::lib::error_code ec;
  98. m_client.close(hdl, websocketpp::close::status::going_away, "", ec);
  99. if (ec) {
  100. LOG(ERROR) << "Error closing connection " << ec.message();
  101. }
  102. }
  103. }
  104. }
  105. // This method will block until the connection is complete
  106. void run(const std::string& uri, const std::vector<string>& wav_list,
  107. const std::vector<string>& wav_ids, int audio_fs, std::string asr_mode,
  108. std::vector<int> chunk_size, const std::unordered_map<std::string, int>& hws_map,
  109. bool is_record=false, int use_itn=1) {
  110. // Create a new connection to the given URI
  111. websocketpp::lib::error_code ec;
  112. typename websocketpp::client<T>::connection_ptr con =
  113. m_client.get_connection(uri, ec);
  114. if (ec) {
  115. m_client.get_alog().write(websocketpp::log::alevel::app,
  116. "Get Connection Error: " + ec.message());
  117. return;
  118. }
  119. // Grab a handle for this connection so we can talk to it in a thread
  120. // safe manor after the event loop starts.
  121. m_hdl = con->get_handle();
  122. // Queue the connection. No DNS queries or network connections will be
  123. // made until the io_service event loop is run.
  124. m_client.connect(con);
  125. // Create a thread to run the ASIO io_service event loop
  126. websocketpp::lib::thread asio_thread(&websocketpp::client<T>::run,
  127. &m_client);
  128. if(is_record){
  129. send_rec_data(asr_mode, chunk_size, hws_map, use_itn);
  130. }else{
  131. send_wav_data(wav_list[0], wav_ids[0], audio_fs, asr_mode, chunk_size, hws_map, use_itn);
  132. }
  133. WaitABit();
  134. asio_thread.join();
  135. }
  136. // The open handler will signal that we are ready to start sending data
  137. void on_open(websocketpp::connection_hdl) {
  138. m_client.get_alog().write(websocketpp::log::alevel::app,
  139. "Connection opened, starting data!");
  140. scoped_lock guard(m_lock);
  141. m_open = true;
  142. }
  143. // The close handler will signal that we should stop sending data
  144. void on_close(websocketpp::connection_hdl) {
  145. m_client.get_alog().write(websocketpp::log::alevel::app,
  146. "Connection closed, stopping data!");
  147. scoped_lock guard(m_lock);
  148. m_done = true;
  149. }
  150. // The fail handler will signal that we should stop sending data
  151. void on_fail(websocketpp::connection_hdl) {
  152. m_client.get_alog().write(websocketpp::log::alevel::app,
  153. "Connection failed, stopping data!");
  154. scoped_lock guard(m_lock);
  155. m_done = true;
  156. }
  157. // send wav to server
  158. void send_wav_data(string wav_path, string wav_id, int audio_fs, std::string asr_mode,
  159. std::vector<int> chunk_vector, const std::unordered_map<std::string, int>& hws_map,
  160. int use_itn) {
  161. uint64_t count = 0;
  162. std::stringstream val;
  163. funasr::Audio audio(1);
  164. int32_t sampling_rate = audio_fs;
  165. std::string wav_format = "pcm";
  166. if (funasr::IsTargetFile(wav_path.c_str(), "wav")) {
  167. if (!audio.LoadWav(wav_path.c_str(), &sampling_rate, false))
  168. return;
  169. } else if (funasr::IsTargetFile(wav_path.c_str(), "pcm")) {
  170. if (!audio.LoadPcmwav(wav_path.c_str(), &sampling_rate, false)) return;
  171. } else {
  172. wav_format = "others";
  173. if (!audio.LoadOthers2Char(wav_path.c_str())) return;
  174. }
  175. float* buff;
  176. int len;
  177. int flag = 0;
  178. bool wait = false;
  179. while (1) {
  180. {
  181. scoped_lock guard(m_lock);
  182. // If the connection has been closed, stop generating data
  183. if (m_done) {
  184. break;
  185. }
  186. // If the connection hasn't been opened yet wait a bit and retry
  187. if (!m_open) {
  188. wait = true;
  189. } else {
  190. break;
  191. }
  192. }
  193. if (wait) {
  194. // LOG(INFO) << "wait.." << m_open;
  195. WaitABit();
  196. continue;
  197. }
  198. }
  199. websocketpp::lib::error_code ec;
  200. nlohmann::json jsonbegin;
  201. nlohmann::json chunk_size = nlohmann::json::array();
  202. chunk_size.push_back(chunk_vector[0]);
  203. chunk_size.push_back(chunk_vector[1]);
  204. chunk_size.push_back(chunk_vector[2]);
  205. jsonbegin["mode"] = asr_mode;
  206. jsonbegin["chunk_size"] = chunk_size;
  207. jsonbegin["wav_name"] = wav_id;
  208. jsonbegin["wav_format"] = wav_format;
  209. jsonbegin["audio_fs"] = sampling_rate;
  210. jsonbegin["is_speaking"] = true;
  211. jsonbegin["itn"] = true;
  212. if(use_itn == 0){
  213. jsonbegin["itn"] = false;
  214. }
  215. if(!hws_map.empty()){
  216. LOG(INFO) << "hotwords: ";
  217. for (const auto& pair : hws_map) {
  218. LOG(INFO) << pair.first << " : " << pair.second;
  219. }
  220. nlohmann::json json_map(hws_map);
  221. std::string json_map_str = json_map.dump();
  222. jsonbegin["hotwords"] = json_map_str;
  223. }
  224. m_client.send(m_hdl, jsonbegin.dump(), websocketpp::frame::opcode::text,
  225. ec);
  226. // fetch wav data use asr engine api
  227. if (wav_format == "pcm") {
  228. while (audio.Fetch(buff, len, flag) > 0) {
  229. short* iArray = new short[len];
  230. for (size_t i = 0; i < len; ++i) {
  231. iArray[i] = (short)(buff[i] * 32768);
  232. }
  233. // send data to server
  234. int offset = 0;
  235. int block_size = 102400;
  236. while (offset < len) {
  237. int send_block = 0;
  238. if (offset + block_size <= len) {
  239. send_block = block_size;
  240. } else {
  241. send_block = len - offset;
  242. }
  243. m_client.send(m_hdl, iArray + offset, send_block * sizeof(short),
  244. websocketpp::frame::opcode::binary, ec);
  245. offset += send_block;
  246. }
  247. LOG(INFO) << "sended data len=" << len * sizeof(short);
  248. if (ec) {
  249. m_client.get_alog().write(websocketpp::log::alevel::app,
  250. "Send Error: " + ec.message());
  251. break;
  252. }
  253. delete[] iArray;
  254. }
  255. } else {
  256. int offset = 0;
  257. int block_size = 204800;
  258. len = audio.GetSpeechLen();
  259. char* others_buff = audio.GetSpeechChar();
  260. while (offset < len) {
  261. int send_block = 0;
  262. if (offset + block_size <= len) {
  263. send_block = block_size;
  264. } else {
  265. send_block = len - offset;
  266. }
  267. m_client.send(m_hdl, others_buff + offset, send_block,
  268. websocketpp::frame::opcode::binary, ec);
  269. offset += send_block;
  270. }
  271. LOG(INFO) << "sended data len=" << len;
  272. if (ec) {
  273. m_client.get_alog().write(websocketpp::log::alevel::app,
  274. "Send Error: " + ec.message());
  275. }
  276. }
  277. nlohmann::json jsonresult;
  278. jsonresult["is_speaking"] = false;
  279. m_client.send(m_hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  280. ec);
  281. WaitABit();
  282. }
  283. static int RecordCallback(const void* inputBuffer, void* outputBuffer,
  284. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
  285. PaStreamCallbackFlags statusFlags, void* userData)
  286. {
  287. std::vector<float>* buffer = static_cast<std::vector<float>*>(userData);
  288. const float* input = static_cast<const float*>(inputBuffer);
  289. for (unsigned int i = 0; i < framesPerBuffer; i++)
  290. {
  291. buffer->push_back(input[i]);
  292. }
  293. return paContinue;
  294. }
  295. void send_rec_data(std::string asr_mode, std::vector<int> chunk_vector,
  296. const std::unordered_map<std::string, int>& hws_map, int use_itn) {
  297. // first message
  298. bool wait = false;
  299. while (1) {
  300. {
  301. scoped_lock guard(m_lock);
  302. // If the connection has been closed, stop generating data
  303. if (m_done) {
  304. break;
  305. }
  306. // If the connection hasn't been opened yet wait a bit and retry
  307. if (!m_open) {
  308. wait = true;
  309. } else {
  310. break;
  311. }
  312. }
  313. if (wait) {
  314. // LOG(INFO) << "wait.." << m_open;
  315. WaitABit();
  316. continue;
  317. }
  318. }
  319. websocketpp::lib::error_code ec;
  320. float sample_rate = 16000;
  321. nlohmann::json jsonbegin;
  322. nlohmann::json chunk_size = nlohmann::json::array();
  323. chunk_size.push_back(chunk_vector[0]);
  324. chunk_size.push_back(chunk_vector[1]);
  325. chunk_size.push_back(chunk_vector[2]);
  326. jsonbegin["mode"] = asr_mode;
  327. jsonbegin["chunk_size"] = chunk_size;
  328. jsonbegin["wav_name"] = "record";
  329. jsonbegin["wav_format"] = "pcm";
  330. jsonbegin["audio_fs"] = sample_rate;
  331. jsonbegin["is_speaking"] = true;
  332. jsonbegin["itn"] = true;
  333. if(use_itn == 0){
  334. jsonbegin["itn"] = false;
  335. }
  336. if(!hws_map.empty()){
  337. LOG(INFO) << "hotwords: ";
  338. for (const auto& pair : hws_map) {
  339. LOG(INFO) << pair.first << " : " << pair.second;
  340. }
  341. nlohmann::json json_map(hws_map);
  342. std::string json_map_str = json_map.dump();
  343. jsonbegin["hotwords"] = json_map_str;
  344. }
  345. m_client.send(m_hdl, jsonbegin.dump(), websocketpp::frame::opcode::text,
  346. ec);
  347. // mic
  348. Microphone mic;
  349. PaDeviceIndex num_devices = Pa_GetDeviceCount();
  350. LOG(INFO) << "Num devices: " << num_devices;
  351. PaStreamParameters param;
  352. param.device = Pa_GetDefaultInputDevice();
  353. if (param.device == paNoDevice) {
  354. LOG(INFO) << "No default input device found";
  355. exit(EXIT_FAILURE);
  356. }
  357. LOG(INFO) << "Use default device: " << param.device;
  358. const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device);
  359. LOG(INFO) << " Name: " << info->name;
  360. LOG(INFO) << " Max input channels: " << info->maxInputChannels;
  361. param.channelCount = 1;
  362. param.sampleFormat = paFloat32;
  363. param.suggestedLatency = info->defaultLowInputLatency;
  364. param.hostApiSpecificStreamInfo = nullptr;
  365. PaStream *stream;
  366. std::vector<float> buffer;
  367. PaError err =
  368. Pa_OpenStream(&stream, &param, nullptr, /* &outputParameters, */
  369. sample_rate,
  370. 0, // frames per buffer
  371. paClipOff, // we won't output out of range samples
  372. // so don't bother clipping them
  373. RecordCallback, &buffer);
  374. if (err != paNoError) {
  375. LOG(ERROR) << "portaudio error: " << Pa_GetErrorText(err);
  376. exit(EXIT_FAILURE);
  377. }
  378. err = Pa_StartStream(stream);
  379. LOG(INFO) << "Started: ";
  380. if (err != paNoError) {
  381. LOG(ERROR) << "portaudio error: " << Pa_GetErrorText(err);
  382. exit(EXIT_FAILURE);
  383. }
  384. while(true){
  385. int len = buffer.size();
  386. short* iArray = new short[len];
  387. for (size_t i = 0; i < len; ++i) {
  388. iArray[i] = (short)(buffer[i] * 32768);
  389. }
  390. m_client.send(m_hdl, iArray, len * sizeof(short),
  391. websocketpp::frame::opcode::binary, ec);
  392. buffer.clear();
  393. if (ec) {
  394. m_client.get_alog().write(websocketpp::log::alevel::app,
  395. "Send Error: " + ec.message());
  396. }
  397. Pa_Sleep(20); // sleep for 20ms
  398. }
  399. nlohmann::json jsonresult;
  400. jsonresult["is_speaking"] = false;
  401. m_client.send(m_hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  402. ec);
  403. err = Pa_CloseStream(stream);
  404. if (err != paNoError) {
  405. LOG(INFO) << "portaudio error: " << Pa_GetErrorText(err);
  406. exit(EXIT_FAILURE);
  407. }
  408. }
  409. websocketpp::client<T> m_client;
  410. private:
  411. websocketpp::connection_hdl m_hdl;
  412. websocketpp::lib::mutex m_lock;
  413. bool m_open;
  414. bool m_done;
  415. int total_num = 0;
  416. };
  417. int main(int argc, char* argv[]) {
  418. #ifdef _WIN32
  419. #include <windows.h>
  420. SetConsoleOutputCP(65001);
  421. #endif
  422. google::InitGoogleLogging(argv[0]);
  423. FLAGS_logtostderr = true;
  424. TCLAP::CmdLine cmd("funasr-wss-client-2pass", ' ', "1.0");
  425. TCLAP::ValueArg<std::string> server_ip_("", "server-ip", "server-ip", true,
  426. "127.0.0.1", "string");
  427. TCLAP::ValueArg<std::string> port_("", "port", "port", true, "10095",
  428. "string");
  429. TCLAP::ValueArg<std::string> wav_path_(
  430. "", "wav-path",
  431. "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: "
  432. "asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)",
  433. false, "", "string");
  434. TCLAP::ValueArg<std::int32_t> audio_fs_("", "audio-fs", "the sample rate of audio", false, 16000, "int32_t");
  435. TCLAP::ValueArg<int> record_(
  436. "", "record",
  437. "record is 1 means use record", false, 0,
  438. "int");
  439. TCLAP::ValueArg<std::string> asr_mode_("", ASR_MODE, "offline, online, 2pass",
  440. false, "2pass", "string");
  441. TCLAP::ValueArg<std::string> chunk_size_("", "chunk-size",
  442. "chunk_size: 5-10-5 or 5-12-5",
  443. false, "5-10-5", "string");
  444. TCLAP::ValueArg<int> thread_num_("", "thread-num", "thread-num", false, 1,
  445. "int");
  446. TCLAP::ValueArg<int> is_ssl_(
  447. "", "is-ssl",
  448. "is-ssl is 1 means use wss connection, or use ws connection", false, 1,
  449. "int");
  450. TCLAP::ValueArg<int> use_itn_(
  451. "", "use-itn",
  452. "use-itn is 1 means use itn, 0 means not use itn", false, 1,
  453. "int");
  454. TCLAP::ValueArg<std::string> hotword_("", HOTWORD,
  455. "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  456. cmd.add(server_ip_);
  457. cmd.add(port_);
  458. cmd.add(wav_path_);
  459. cmd.add(audio_fs_);
  460. cmd.add(asr_mode_);
  461. cmd.add(record_);
  462. cmd.add(chunk_size_);
  463. cmd.add(thread_num_);
  464. cmd.add(is_ssl_);
  465. cmd.add(use_itn_);
  466. cmd.add(hotword_);
  467. cmd.parse(argc, argv);
  468. std::string server_ip = server_ip_.getValue();
  469. std::string port = port_.getValue();
  470. std::string wav_path = wav_path_.getValue();
  471. std::string asr_mode = asr_mode_.getValue();
  472. std::string chunk_size_str = chunk_size_.getValue();
  473. int use_itn = use_itn_.getValue();
  474. // get chunk_size
  475. std::vector<int> chunk_size;
  476. std::stringstream ss(chunk_size_str);
  477. std::string item;
  478. while (std::getline(ss, item, '-')) {
  479. try {
  480. chunk_size.push_back(stoi(item));
  481. } catch (const invalid_argument&) {
  482. LOG(ERROR) << "Invalid argument: " << item;
  483. exit(-1);
  484. }
  485. }
  486. int threads_num = thread_num_.getValue();
  487. int is_ssl = is_ssl_.getValue();
  488. int is_record = record_.getValue();
  489. std::string uri = "";
  490. if (is_ssl == 1) {
  491. uri = "wss://" + server_ip + ":" + port;
  492. } else {
  493. uri = "ws://" + server_ip + ":" + port;
  494. }
  495. // hotwords
  496. std::string hotword_path = hotword_.getValue();
  497. unordered_map<string, int> hws_map;
  498. if(!hotword_path.empty()){
  499. LOG(INFO) << "hotword path: " << hotword_path;
  500. funasr::ExtractHws(hotword_path, hws_map);
  501. }
  502. int audio_fs = audio_fs_.getValue();
  503. if(is_record == 1){
  504. std::vector<string> tmp_wav_list;
  505. std::vector<string> tmp_wav_ids;
  506. if (is_ssl == 1) {
  507. WebsocketClient<websocketpp::config::asio_tls_client> c(is_ssl);
  508. c.m_client.set_tls_init_handler(bind(&OnTlsInit, ::_1));
  509. c.run(uri, tmp_wav_list, tmp_wav_ids, audio_fs, asr_mode, chunk_size, hws_map, true, use_itn);
  510. } else {
  511. WebsocketClient<websocketpp::config::asio_client> c(is_ssl);
  512. c.run(uri, tmp_wav_list, tmp_wav_ids, audio_fs, asr_mode, chunk_size, hws_map, true, use_itn);
  513. }
  514. }else{
  515. // read wav_path
  516. std::vector<string> wav_list;
  517. std::vector<string> wav_ids;
  518. string default_id = "wav_default_id";
  519. if (funasr::IsTargetFile(wav_path, "scp")) {
  520. ifstream in(wav_path);
  521. if (!in.is_open()) {
  522. printf("Failed to open scp file");
  523. return 0;
  524. }
  525. string line;
  526. while (getline(in, line)) {
  527. istringstream iss(line);
  528. string column1, column2;
  529. iss >> column1 >> column2;
  530. wav_list.emplace_back(column2);
  531. wav_ids.emplace_back(column1);
  532. }
  533. in.close();
  534. } else {
  535. wav_list.emplace_back(wav_path);
  536. wav_ids.emplace_back(default_id);
  537. }
  538. for (size_t wav_i = 0; wav_i < wav_list.size(); wav_i = wav_i + threads_num) {
  539. std::vector<websocketpp::lib::thread> client_threads;
  540. for (size_t i = 0; i < threads_num; i++) {
  541. if (wav_i + i >= wav_list.size()) {
  542. break;
  543. }
  544. std::vector<string> tmp_wav_list;
  545. std::vector<string> tmp_wav_ids;
  546. tmp_wav_list.emplace_back(wav_list[wav_i + i]);
  547. tmp_wav_ids.emplace_back(wav_ids[wav_i + i]);
  548. client_threads.emplace_back(
  549. [uri, tmp_wav_list, tmp_wav_ids, audio_fs, asr_mode, chunk_size, is_ssl, hws_map, use_itn]() {
  550. if (is_ssl == 1) {
  551. WebsocketClient<websocketpp::config::asio_tls_client> c(is_ssl);
  552. c.m_client.set_tls_init_handler(bind(&OnTlsInit, ::_1));
  553. c.run(uri, tmp_wav_list, tmp_wav_ids, audio_fs, asr_mode, chunk_size, hws_map, false, use_itn);
  554. } else {
  555. WebsocketClient<websocketpp::config::asio_client> c(is_ssl);
  556. c.run(uri, tmp_wav_list, tmp_wav_ids, audio_fs, asr_mode, chunk_size, hws_map, false, use_itn);
  557. }
  558. });
  559. }
  560. for (auto& t : client_threads) {
  561. t.join();
  562. }
  563. }
  564. }
  565. }