1
1
#include " nlohmann/json.hpp"
2
2
#include " stream/stream_components_service.h"
3
3
#include " stream/stream_components.h"
4
- #include " utils /utils.h"
4
+ #include " common /utils.h"
5
5
#include " common/common.h"
6
6
#include < uwebsockets/App.h>
7
7
#include < iostream>
@@ -61,14 +61,13 @@ int main(int argc, char **argv) {
61
61
// Save Audio
62
62
auto ws_save_handler=[](auto *ws,std::string_view message,uWS::OpCode opCode){
63
63
auto * userData = (std::string*)ws->getUserData ();
64
- printf (" %s: User Data: %s\n " , get_current_time ().c_str (), userData->c_str ());
64
+ // printf("%s: User Data: %s\n", get_current_time().c_str(), userData->c_str());
65
65
thread_local wav_writer wavWriter;
66
66
thread_local std::string filename;
67
67
68
68
nlohmann::json response;
69
69
if (opCode == uWS::OpCode::TEXT) {
70
- printf (" %s: Received message on /streaming/save: %s\n " , get_current_time ().c_str (),
71
- std::string (message).c_str ());
70
+ // printf("%s: Received message on /streaming/save: %s\n", get_current_time().c_str(),std::string(message).c_str());
72
71
auto jsonMsg = nlohmann::json::parse (message);
73
72
std::string signal = jsonMsg[" signal" ];
74
73
if (signal == " start" ) {
@@ -94,12 +93,13 @@ int main(int argc, char **argv) {
94
93
// process binary message(PCM16 data)
95
94
auto size = message.size ();
96
95
std::basic_string_view<char , std::char_traits<char >>::const_pointer data = message.data ();
97
- printf (" %s: Received message size on /streaming/save: %zu\n " , get_current_time ().c_str (), size);
96
+ // printf("%s: Received message size on /streaming/save: %zu\n", get_current_time().c_str(), size);
98
97
// add received PCM16 to audio cache
99
98
std::vector<int16_t > pcm16 (size / 2 );
100
99
std::memcpy (pcm16.data (), data, size);
101
100
// write to file
102
101
wavWriter.write (pcm16.data (), size / 2 );
102
+ ws->send (response.dump (), uWS::OpCode::TEXT);
103
103
}
104
104
};
105
105
@@ -111,11 +111,10 @@ int main(int argc, char **argv) {
111
111
// std::unique_ptr<nlohmann::json> results(new nlohmann::json(nlohmann::json::array()));
112
112
thread_local nlohmann::json final_results;
113
113
auto thread_id = std::this_thread::get_id ();
114
- std::cout << get_current_time ().c_str () << " : Handling a message in thread: " << thread_id << std::endl;
114
+ // std::cout << get_current_time().c_str() << ": Handling a message in thread: " << thread_id << std::endl;
115
115
nlohmann::json response;
116
116
if (opCode == uWS::OpCode::TEXT) {
117
- printf (" %s: Received message on /paddlespeech/asr/streaming: %s\n " , get_current_time ().c_str (),
118
- std::string (message).c_str ());
117
+ // printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),std::string(message).c_str());
119
118
// process text message
120
119
try {
121
120
auto jsonMsg = nlohmann::json::parse (message);
@@ -147,7 +146,7 @@ int main(int argc, char **argv) {
147
146
// 如果开启了VAD
148
147
bool isOk;
149
148
if (params.audio .use_vad ) {
150
- printf (" %s: vad: %d \n " , get_current_time ().c_str (), params.audio .use_vad );
149
+ // printf("%s: vad: %d \n", get_current_time().c_str(), params.audio.use_vad);
151
150
// TODO: 实现VAD处理,
152
151
// bool containsVoice = vad_simple(audioBuffer, WHISPER_SAMPLE_RATE, 1000, params.audio.vad_thold, params.audio.freq_thold, false);
153
152
isOk=whisperService.process (pcm32.data (), pcm32.size ());
@@ -167,9 +166,10 @@ int main(int argc, char **argv) {
167
166
auto size = message.size ();
168
167
}
169
168
} else if (opCode == uWS::OpCode::BINARY) {
169
+ int size=message.size ();
170
170
// process binary message(PCM16 data)
171
171
std::basic_string_view<char , std::char_traits<char >>::const_pointer data = message.data ();
172
- printf (" %s: Received message size on /paddlespeech/asr/streaming: %zu\n " , get_current_time ().c_str (), size);
172
+ // printf("%s: Received message size on /paddlespeech/asr/streaming: %zu\n", get_current_time().c_str(), size);
173
173
// add received PCM16 to audio cache
174
174
std::vector<int16_t > pcm16 (size / 2 );
175
175
@@ -188,7 +188,7 @@ int main(int argc, char **argv) {
188
188
// 如果开启了VAD
189
189
bool isOk;
190
190
if (params.audio .use_vad ) {
191
- printf (" %s: vad: %d \n " , get_current_time ().c_str (), params.audio .use_vad );
191
+ // printf("%s: vad: %d \n", get_current_time().c_str(), params.audio.use_vad);
192
192
// TODO: 实现VAD处理,
193
193
// bool containsVoice = vad_simple(audioBuffer, WHISPER_SAMPLE_RATE, 1000, params.audio.vad_thold, params.audio.freq_thold, false);
194
194
isOk=whisperService.process (pcm32.data (), pcm32.size ());
@@ -226,7 +226,7 @@ int main(int argc, char **argv) {
226
226
227
227
bool processAudio (WhisperService whisperService, std::vector<float > pcm32, const whisper_local_stream_params& params) {
228
228
if (params.audio .use_vad ) {
229
- printf (" %s: vad: %d \n " , get_current_time ().c_str (), params.audio .use_vad );
229
+ // printf("%s: vad: %d \n", get_current_time().c_str(), params.audio.use_vad);
230
230
// TODO: 实现VAD处理,
231
231
// bool containsVoice = vad_simple(audioBuffer, WHISPER_SAMPLE_RATE, 1000, params.audio.vad_thold, params.audio.freq_thold, false);
232
232
return whisperService.process (pcm32.data (), pcm32.size ());
0 commit comments