Skip to content

Commit 0561957

Browse files
author
litongjava
committed
send result
1 parent dda7bb3 commit 0561957

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

whisper_server_base_on_uwebsockets.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ int main(int argc, char **argv) {
7171
try {
7272
auto jsonMsg = nlohmann::json::parse(message);
7373
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...
7581
} catch (const std::exception &e) {
7682
std::cerr << "JSON parse error: " << e.what() << std::endl;
7783
}
@@ -84,19 +90,26 @@ int main(int argc, char **argv) {
8490
std::memcpy(pcm16.data(), message.data(), size);
8591

8692
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
8894
});
8995

9096
// 语音识别处理
9197
bool isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
9298
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);
100113
}
101114
}
102115
};

0 commit comments

Comments
 (0)