Skip to content

Commit d7a3292

Browse files
author
litongmacos
committed
set params.audio.use_vad true
1 parent 71f12d3 commit d7a3292

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

common/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ nlohmann::json get_result(whisper_context *ctx) {
5454
int64_t t1 = whisper_full_get_segment_t1(ctx, i);
5555
const char *sentence = whisper_full_get_segment_text(ctx, i);
5656
auto result = std::to_string(t0) + "-->" + std::to_string(t1) + ":" + sentence + "\n";
57-
//printf("%s: result:%s\n", get_current_time().c_str(), result.c_str());
57+
// printf("%s: result:%s\n", get_current_time().c_str(), result.c_str());
5858
segment["t0"] = to_timestamp(t0);
5959
segment["t1"] = to_timestamp(t1);
6060
segment["sentence"] = sentence;

stream/stream_components_params.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace stream_components {
3131
n_samples_keep = int32_t(1e-3 * keep_ms * WHISPER_SAMPLE_RATE);
3232
n_samples_len = int32_t(1e-3 * length_ms * WHISPER_SAMPLE_RATE);
3333
n_samples_30s = int32_t(1e-3 * 30000.0 * WHISPER_SAMPLE_RATE);
34-
//use_vad = n_samples_step <= 0;
34+
// use_vad = n_samples_step <= 0;
3535
}
3636
};
3737

whisper_server_base_on_uwebsockets.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ int main(int argc, char **argv) {
115115
// std::cout << get_current_time().c_str() << ": Handling a message in thread: " << thread_id << std::endl;
116116
nlohmann::json response;
117117
if (opCode == uWS::OpCode::TEXT) {
118-
// printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),std::string(message).c_str());
119118
// process text message
119+
printf("%s: Received message on /paddlespeech/asr/streaming: %s\n", get_current_time().c_str(),std::string(message).c_str());
120+
120121
try {
121122
auto jsonMsg = nlohmann::json::parse(message);
122123
std::string signal = jsonMsg["signal"];
@@ -134,12 +135,13 @@ int main(int argc, char **argv) {
134135
wavWriter.open(filename, WHISPER_SAMPLE_RATE, 16, 1);
135136
}
136137
if (signal == "end") {
138+
printf("%s end\n");
137139
wavWriter.close();
138140
// nlohmann::json response = {{"name",filename},{"signal", signal}};
139141
response = {{"name", filename},
140142
{"signal", signal}};
143+
printf("%s:buffer size:%d\n",get_current_time().c_str(),audioBuffer.size());
141144
bool isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
142-
audioBuffer.clear();
143145
if (isOk) {
144146
final_results = get_result(whisperService.ctx);
145147
response["result"] = final_results;
@@ -163,18 +165,22 @@ int main(int argc, char **argv) {
163165
//write to file
164166
wavWriter.write(pcm16.data(), size / 2);
165167
//convert flost
166-
for (int16_t sample: pcm16) {
167-
float floatSample = static_cast<float>(sample);
168-
audioBuffer.push_back(floatSample);
169-
}
168+
std::vector<float> temp(size / 2);
169+
std::transform(pcm16.begin(), pcm16.end(), temp.begin(), [](int16_t sample) {
170+
return static_cast<float>(sample) / 32768.0f;
171+
});
172+
//insert to audio_buffer
173+
audioBuffer.insert(audioBuffer.end(), temp.begin(), temp.end());
174+
175+
printf("%s:buffer size:%d\n",get_current_time().c_str(),audioBuffer.size());
170176
// 如果开启了VAD
171177
bool isOk;
172-
printf("%s: use_vad: %d\n", get_current_time().c_str(), params.audio.use_vad);
178+
// printf("%s: use_vad: %d\n", get_current_time().c_str(), params.audio.use_vad);
173179
if (params.audio.use_vad) {
174180

175181
bool is_active = ::vad_simple(audioBuffer, WHISPER_SAMPLE_RATE, 1000, params.audio.vad_thold,
176182
params.audio.freq_thold, false);
177-
printf("%s: is_active: %d,is_last_active \n", get_current_time().c_str(), is_active, is_last_active);
183+
printf("%s: is_active: %d,is_last_active %d\n", get_current_time().c_str(), is_active, is_last_active);
178184
if (!is_active && is_last_active) {
179185
is_last_active = false;
180186
isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
@@ -185,7 +191,7 @@ int main(int argc, char **argv) {
185191
} else {
186192
// asr
187193
isOk = whisperService.process(audioBuffer.data(), audioBuffer.size());
188-
audioBuffer.clear();
194+
// audioBuffer.clear();
189195
}
190196
printf("%s: is_ok: %d \n", get_current_time().c_str(), isOk);
191197
if (isOk) {

0 commit comments

Comments
 (0)