@@ -58,60 +58,66 @@ int main(int argc, char **argv) {
58
58
};
59
59
60
60
// WebSocket /paddlespeech/asr/streaming handler
61
- std::vector<float > * audioBuffer; // global audio data buffer
62
- wav_writer * wavWriter;
61
+ auto ws_streaming_handler = [&whisperService](auto *ws, std::string_view message, uWS::OpCode opCode) {
62
+ thread_local std::vector<float > audioBuffer; // thread-localized variable
63
+ thread_local wav_writer wavWriter;
64
+ std::string filename;
65
+ // std::unique_ptr<nlohmann::json> results(new nlohmann::json(nlohmann::json::array()));
66
+ nlohmann::json results = nlohmann::json (nlohmann::json::array ());
63
67
64
- auto ws_streaming_handler = [&whisperService, &audioBuffer, &wavWriter](auto *ws, std::string_view message,
65
- uWS::OpCode opCode) {
66
68
if (opCode == uWS::OpCode::TEXT) {
67
69
printf (" %s: Received message on /paddlespeech/asr/streaming: %s\n " , get_current_time ().c_str (),
68
70
std::string (message).c_str ());
69
71
// process text message
70
72
try {
71
73
auto jsonMsg = nlohmann::json::parse (message);
72
- std::string filename = jsonMsg[" name" ];
74
+ if (jsonMsg[" name" ].is_string ()) {
75
+ filename = jsonMsg[" name" ];
76
+ } else {
77
+ filename = std::to_string (get_current_time_millis ()) + " .wav" ;
78
+ }
73
79
std::string signal = jsonMsg[" signal" ];
74
80
if (signal == " start" ) {
75
81
// 发送服务器准备好的消息
76
82
nlohmann::json response = {{" status" , " ok" },
77
83
{" signal" , " server_ready" }};
78
84
ws->send (response.dump (), uWS::OpCode::TEXT);
79
- wavWriter = new wav_writer ();
80
- audioBuffer = new std::vector<float >();
81
- wavWriter->open (filename, WHISPER_SAMPLE_RATE, 16 , 1 );
85
+ wavWriter.open (filename, WHISPER_SAMPLE_RATE, 16 , 1 );
82
86
}
83
87
if (signal == " end" ) {
84
- delete wavWriter;
85
- delete audioBuffer;
88
+ wavWriter.close ();
89
+ // nlohmann::json response = {{"name",filename},{"signal", signal}};
90
+ nlohmann::json response = {{" name" , filename},
91
+ {" signal" , signal},
92
+ {" result" , results}};
93
+ ws->send (response.dump (), uWS::OpCode::TEXT);
86
94
}
87
95
// other process logic...
88
96
} catch (const std::exception &e) {
89
97
std::cerr << " JSON parse error: " << e.what () << std::endl;
90
98
}
91
99
} else if (opCode == uWS::OpCode::BINARY) {
100
+ nlohmann::json response;
101
+
92
102
// process binary message(PCM16 data)
93
103
auto size = message.size ();
104
+
94
105
printf (" %s: Received message size on /paddlespeech/asr/streaming: %zu\n " , get_current_time ().c_str (), size);
95
106
// add received PCM16 to audio cache
96
107
std::vector<int16_t > pcm16 (size / 2 );
97
108
std::basic_string_view<char , std::char_traits<char >>::const_pointer data = message.data ();
98
109
std::memcpy (pcm16.data (), data, size);
99
110
100
- std::vector<float > temp (size/ 2 );
111
+ std::vector<float > temp (size / 2 );
101
112
std::transform (pcm16.begin (), pcm16.end (), temp.begin (), [](int16_t sample) {
102
113
return static_cast <float >(sample) / 32768 .0f ;
103
114
});
104
- wavWriter->write (temp.data (), size/2 );
105
-
106
- audioBuffer->insert (audioBuffer->end (), temp.begin (), temp.end ());
107
-
108
-
115
+ // write to file
116
+ wavWriter.write (temp.data (), size / 2 );
117
+ audioBuffer.insert (audioBuffer.end (), temp.begin (), temp.end ());
109
118
// asr
110
- nlohmann::json response;
111
- bool isOk = whisperService.process (audioBuffer->data (), audioBuffer->size ());
119
+ bool isOk = whisperService.process (audioBuffer.data (), audioBuffer.size ());
112
120
if (isOk) {
113
- nlohmann::json results = nlohmann::json::array (); // create JSON Array
114
-
115
121
const int n_segments = whisper_full_n_segments (whisperService.ctx );
116
122
for (int i = 0 ; i < n_segments; ++i) {
117
123
nlohmann::json segment;
0 commit comments