10
10
11
11
using namespace stream_components ;
12
12
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
+
13
26
int main (int argc, char **argv) {
14
27
// Read parameters...
15
28
whisper_local_stream_params params;
@@ -66,7 +79,8 @@ int main(int argc, char **argv) {
66
79
std::vector<float > audioBuffer; // global audio data buffer点击并应用
67
80
auto ws_streaming_handler = [&whisperService, &audioBuffer](auto *ws, std::string_view message, uWS::OpCode opCode) {
68
81
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 ());
70
84
// process text message
71
85
try {
72
86
auto jsonMsg = nlohmann::json::parse (message);
@@ -84,7 +98,7 @@ int main(int argc, char **argv) {
84
98
} else if (opCode == uWS::OpCode::BINARY) {
85
99
// process binary message(PCM16 data)
86
100
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);
88
102
// 将接收到的 PCM16 数据追加到音频缓存
89
103
std::vector<int16_t > pcm16 (size / 2 );
90
104
std::memcpy (pcm16.data (), message.data (), size);
@@ -95,18 +109,22 @@ int main(int argc, char **argv) {
95
109
96
110
// 语音识别处理
97
111
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);
99
113
if (isOk) {
100
114
nlohmann::json response;
101
115
nlohmann::json results = nlohmann::json::array (); // create JSON Array
102
116
103
117
const int n_segments = whisper_full_n_segments (whisperService.ctx );
104
118
for (int i = 0 ; i < n_segments; ++i) {
105
119
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;
110
128
results.push_back (segment);
111
129
}
112
130
0 commit comments