Skip to content

Commit 0505dc7

Browse files
author
litongmacos
committed
add get_current_time_mills
1 parent 6ff63ca commit 0505dc7

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

utils/utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ std::string get_current_time() {
1818

1919
std::string current_time = current_time_ss.str();
2020
return current_time;
21+
}
22+
23+
long get_current_time_millis(){
24+
auto start = std::chrono::system_clock::now();
25+
return std::chrono::duration_cast<std::chrono::milliseconds>(start.time_since_epoch()).count();
2126
}

utils/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
#include <iostream>
44

5-
std::string get_current_time();
5+
std::string get_current_time();
6+
long get_current_time_millis();

whisper_server_base_on_uwebsockets.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,60 +58,66 @@ int main(int argc, char **argv) {
5858
};
5959

6060
// 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());
6367

64-
auto ws_streaming_handler = [&whisperService, &audioBuffer, &wavWriter](auto *ws, std::string_view message,
65-
uWS::OpCode opCode) {
6668
if (opCode == uWS::OpCode::TEXT) {
6769
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),
6870
std::string(message).c_str());
6971
// process text message
7072
try {
7173
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+
}
7379
std::string signal = jsonMsg["signal"];
7480
if (signal == "start") {
7581
// 发送服务器准备好的消息
7682
nlohmann::json response = {{"status", "ok"},
7783
{"signal", "server_ready"}};
7884
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);
8286
}
8387
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);
8694
}
8795
// other process logic...
8896
} catch (const std::exception &e) {
8997
std::cerr << "JSON parse error: " << e.what() << std::endl;
9098
}
9199
} else if (opCode == uWS::OpCode::BINARY) {
100+
nlohmann::json response;
101+
92102
// process binary message(PCM16 data)
93103
auto size = message.size();
104+
94105
printf("%s: Received message size on /paddlespeech/asr/streaming: %zu\n", get_current_time().c_str(), size);
95106
// add received PCM16 to audio cache
96107
std::vector<int16_t> pcm16(size / 2);
97108
std::basic_string_view<char, std::char_traits<char>>::const_pointer data = message.data();
98109
std::memcpy(pcm16.data(), data, size);
99110

100-
std::vector<float> temp(size/2);
111+
std::vector<float> temp(size / 2);
101112
std::transform(pcm16.begin(), pcm16.end(), temp.begin(), [](int16_t sample) {
102113
return static_cast<float>(sample) / 32768.0f;
103114
});
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());
109118
// asr
110-
nlohmann::json response;
111-
bool isOk = whisperService.process(audioBuffer->data(), audioBuffer->size());
119+
bool isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
112120
if (isOk) {
113-
nlohmann::json results = nlohmann::json::array(); // create JSON Array
114-
115121
const int n_segments = whisper_full_n_segments(whisperService.ctx);
116122
for (int i = 0; i < n_segments; ++i) {
117123
nlohmann::json segment;

0 commit comments

Comments
 (0)