@@ -71,7 +71,13 @@ int main(int argc, char **argv) {
71
71
try {
72
72
auto jsonMsg = nlohmann::json::parse (message);
73
73
std::string signal = jsonMsg[" signal" ];
74
- // process logic...
74
+ if (signal == " start" ) {
75
+ // 发送服务器准备好的消息
76
+ nlohmann::json response = {{" status" , " ok" },
77
+ {" signal" , " server_ready" }};
78
+ ws->send (response.dump (), uWS::OpCode::TEXT);
79
+ }
80
+ // other process logic...
75
81
} catch (const std::exception &e) {
76
82
std::cerr << " JSON parse error: " << e.what () << std::endl;
77
83
}
@@ -84,19 +90,26 @@ int main(int argc, char **argv) {
84
90
std::memcpy (pcm16.data (), message.data (), size);
85
91
86
92
std::transform (pcm16.begin (), pcm16.end (), std::back_inserter (audioBuffer), [](int16_t sample) {
87
- return static_cast <float >(sample) / 32768 .0f ; // 转换为 [-1.0, 1.0] 浮点
93
+ return static_cast <float >(sample) / 32768 .0f ; // convert to [-1.0, 1.0] float
88
94
});
89
95
90
96
// 语音识别处理
91
97
bool isOk = whisperService.process (audioBuffer.data (), audioBuffer.size ());
92
98
printf (" isOk:%d\n " , isOk);
93
- const int n_segments = whisper_full_n_segments (whisperService.ctx );
94
- printf (" n_segments:%d\n " , n_segments);
95
- for (int i = 0 ; i < n_segments; ++i) {
96
- const char *text = whisper_full_get_segment_text (whisperService.ctx , i);
97
- const int64_t t0 = whisper_full_get_segment_t0 (whisperService.ctx , i);
98
- const int64_t t1 = whisper_full_get_segment_t1 (whisperService.ctx , i);
99
- printf (" %lld-->%lld:%s\n " , t0, t1, text);
99
+ if (isOk) {
100
+ nlohmann::json response;
101
+ std::string result;
102
+
103
+ const int n_segments = whisper_full_n_segments (whisperService.ctx );
104
+ printf (" n_segments:%d\n " , n_segments);
105
+ for (int i = 0 ; i < n_segments; ++i) {
106
+ const char *text = whisper_full_get_segment_text (whisperService.ctx , i);
107
+ const int64_t t0 = whisper_full_get_segment_t0 (whisperService.ctx , i);
108
+ const int64_t t1 = whisper_full_get_segment_t1 (whisperService.ctx , i);
109
+ printf (" %lld-->%lld:%s\n " , t0, t1, text);
110
+ }
111
+ response[" result" ] = result;
112
+ ws->send (response.dump (), uWS::OpCode::TEXT);
100
113
}
101
114
}
102
115
};
0 commit comments