@@ -62,33 +62,33 @@ int main(int argc, char **argv) {
62
62
ws->send (message, opCode);
63
63
};
64
64
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) {
67
68
if (opCode == uWS::OpCode::TEXT) {
68
69
printf (" %s: Received message on /paddlespeech/asr/streaming: %s\n " , __func__, std::string (message).c_str ());
69
- // 处理文本消息(假设是 JSON)
70
+ // process text message
70
71
try {
71
72
auto jsonMsg = nlohmann::json::parse (message);
72
73
std::string signal = jsonMsg[" signal" ];
73
- // 处理逻辑 ...
74
+ // process logic ...
74
75
} catch (const std::exception &e) {
75
- std::cerr << " JSON 解析错误 : " << e.what () << std::endl;
76
+ std::cerr << " JSON parse error : " << e.what () << std::endl;
76
77
}
77
78
} else if (opCode == uWS::OpCode::BINARY) {
78
- // 处理二进制消息 (PCM16 数据 )
79
+ // process binary message (PCM16 data )
79
80
auto size = message.size ();
80
81
printf (" %s: Received message size on /paddlespeech/asr/streaming: %zu\n " , __func__, size);
82
+ // 将接收到的 PCM16 数据追加到音频缓存
81
83
std::vector<int16_t > pcm16 (size / 2 );
82
84
std::memcpy (pcm16.data (), message.data (), size);
83
85
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] 浮点
88
88
});
89
89
90
90
// 语音识别处理
91
- bool isOk = whisperService.process (pcmf32 .data (), pcmf32 .size ());
91
+ bool isOk = whisperService.process (audioBuffer .data (), audioBuffer .size ());
92
92
printf (" isOk:%d\n " , isOk);
93
93
const int n_segments = whisper_full_n_segments (whisperService.ctx );
94
94
printf (" n_segments:%d\n " , n_segments);
0 commit comments