Skip to content

Commit dda7bb3

Browse files
author
litongjava
committed
keep all audio data
1 parent 7e1afd2 commit dda7bb3

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

whisper_server_base_on_uwebsockets.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,33 @@ int main(int argc, char **argv) {
6262
ws->send(message, opCode);
6363
};
6464

65-
// WebSocket /paddlespeech/asr/streaming 处理器
66-
auto ws_streaming_handler = [&whisperService](auto *ws, std::string_view message, uWS::OpCode opCode) {
65+
// WebSocket /paddlespeech/asr/streaming handler
66+
std::vector<float> audioBuffer; // global audio data buffer点击并应用
67+
auto ws_streaming_handler = [&whisperService, &audioBuffer](auto *ws, std::string_view message, uWS::OpCode opCode) {
6768
if (opCode == uWS::OpCode::TEXT) {
6869
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", __func__, std::string(message).c_str());
69-
// 处理文本消息(假设是 JSON)
70+
// process text message
7071
try {
7172
auto jsonMsg = nlohmann::json::parse(message);
7273
std::string signal = jsonMsg["signal"];
73-
// 处理逻辑...
74+
// process logic...
7475
} catch (const std::exception &e) {
75-
std::cerr << "JSON 解析错误: " << e.what() << std::endl;
76+
std::cerr << "JSON parse error: " << e.what() << std::endl;
7677
}
7778
} else if (opCode == uWS::OpCode::BINARY) {
78-
// 处理二进制消息(PCM16 数据
79+
// process binary message(PCM16 data
7980
auto size = message.size();
8081
printf("%s: Received message size on /paddlespeech/asr/streaming: %zu\n", __func__, size);
82+
// 将接收到的 PCM16 数据追加到音频缓存
8183
std::vector<int16_t> pcm16(size / 2);
8284
std::memcpy(pcm16.data(), message.data(), size);
8385

84-
// 将 PCM16 数据转换为 float
85-
std::vector<float> pcmf32(pcm16.size());
86-
std::transform(pcm16.begin(), pcm16.end(), pcmf32.begin(), [](int16_t sample) {
87-
return static_cast<float>(sample) / 32768.0f; // 将 int16 范围转换为 [-1.0, 1.0]
86+
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] 浮点
8888
});
8989

9090
// 语音识别处理
91-
bool isOk = whisperService.process(pcmf32.data(), pcmf32.size());
91+
bool isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
9292
printf("isOk:%d\n", isOk);
9393
const int n_segments = whisper_full_n_segments(whisperService.ctx);
9494
printf("n_segments:%d\n", n_segments);

0 commit comments

Comments
 (0)