Skip to content

Commit 1298865

Browse files
author
litongjava
committed
output time in log
1 parent d51bc4a commit 1298865

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

whisper_server_base_on_uwebsockets.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
using namespace stream_components;
1212

13+
std::string get_current_time() {
14+
auto now = std::chrono::system_clock::now();
15+
auto now_c = std::chrono::system_clock::to_time_t(now);
16+
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
17+
18+
std::stringstream current_time_ss;
19+
current_time_ss << std::put_time(std::localtime(&now_c), "%y-%m-%d %H:%M:%S")
20+
<< '.' << std::setfill('0') << std::setw(3) << milliseconds.count();
21+
22+
std::string current_time = current_time_ss.str();
23+
return current_time;
24+
}
25+
1326
int main(int argc, char **argv) {
1427
// Read parameters...
1528
whisper_local_stream_params params;
@@ -66,7 +79,8 @@ int main(int argc, char **argv) {
6679
std::vector<float> audioBuffer; // global audio data buffer点击并应用
6780
auto ws_streaming_handler = [&whisperService, &audioBuffer](auto *ws, std::string_view message, uWS::OpCode opCode) {
6881
if (opCode == uWS::OpCode::TEXT) {
69-
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", __func__, std::string(message).c_str());
82+
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),
83+
std::string(message).c_str());
7084
// process text message
7185
try {
7286
auto jsonMsg = nlohmann::json::parse(message);
@@ -84,7 +98,7 @@ int main(int argc, char **argv) {
8498
} else if (opCode == uWS::OpCode::BINARY) {
8599
// process binary message(PCM16 data)
86100
auto size = message.size();
87-
printf("%s: Received message size on /paddlespeech/asr/streaming: %zu\n", __func__, size);
101+
printf("%s: Received message size on /paddlespeech/asr/streaming: %zu\n", get_current_time().c_str(), size);
88102
// 将接收到的 PCM16 数据追加到音频缓存
89103
std::vector<int16_t> pcm16(size / 2);
90104
std::memcpy(pcm16.data(), message.data(), size);
@@ -95,18 +109,22 @@ int main(int argc, char **argv) {
95109

96110
// 语音识别处理
97111
bool isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
98-
printf("isOk:%d\n", isOk);
112+
printf("%s: isOk:%d\n", get_current_time().c_str(), isOk);
99113
if (isOk) {
100114
nlohmann::json response;
101115
nlohmann::json results = nlohmann::json::array(); // create JSON Array
102116

103117
const int n_segments = whisper_full_n_segments(whisperService.ctx);
104118
for (int i = 0; i < n_segments; ++i) {
105119
nlohmann::json segment;
106-
segment["t0"] = whisper_full_get_segment_t0(whisperService.ctx, i);
107-
segment["t1"] = whisper_full_get_segment_t1(whisperService.ctx, i);
108-
segment["sentence"] = whisper_full_get_segment_text(whisperService.ctx, i);
109-
120+
int64_t t0 = whisper_full_get_segment_t0(whisperService.ctx, i);
121+
int64_t t1 = whisper_full_get_segment_t1(whisperService.ctx, i);
122+
const char *sentence = whisper_full_get_segment_text(whisperService.ctx, i);
123+
auto result = std::to_string(t0) + "-->" + std::to_string(t1) + ":" + sentence + "\n";
124+
printf("%s: result:%s\n", get_current_time().c_str(), result.c_str());
125+
segment["t0"] = t0;
126+
segment["t1"] = t1;
127+
segment["sentence"] = sentence;
110128
results.push_back(segment);
111129
}
112130

0 commit comments

Comments
 (0)