1515#include < vector>
1616#include < tuple>
1717#include < fstream>
18+ #include < thread>
19+
20+ #include < curl/curl.h>
1821
1922#include " protocol.hpp"
2023
2124#include " vad_iterator.hpp"
2225
23- constexpr int64_t VAD_WINDOW_SIZE = 2048 ;
26+ constexpr int64_t VAD_WINDOW_SIZE = 1536 ;
2427constexpr size_t VAD_TEST_FRAME_MS = 512 ;
2528
2629// command-line parameters
@@ -47,8 +50,11 @@ struct whisper_params {
4750 bool flash_attn = false ;
4851 bool remote = false ;
4952
53+ int32_t id = 0 ;
54+
5055 std::string language = " en" ;
5156 std::string model = " models/ggml-base.en.bin" ;
57+ std::string location = " 41.40338,2.17403" ;
5258 std::string bootstrap_servers = DEFAULT_BOOTSTRAP_SERVERS;
5359 std::string topic = DEFAULT_KAFKA_TOPIC;
5460 std::string fname_out;
@@ -87,6 +93,8 @@ static bool whisper_params_parse(int argc, char ** argv, whisper_params & params
8793 else if (arg == " -sa" || arg == " --save-audio" ) { params.save_audio = true ; }
8894 else if (arg == " -ng" || arg == " --no-gpu" ) { params.use_gpu = false ; }
8995 else if (arg == " -fa" || arg == " --flash-attn" ) { params.flash_attn = true ; }
96+ else if (arg == " -loc" || arg == " --location" ) { params.location = argv[++i]; }
97+ else if (arg == " -id" || arg == " --id" ) { params.id = std::stoi (argv[++i]); }
9098
9199 else {
92100 fprintf (stderr, " error: unknown argument: %s\n " , arg.c_str ());
@@ -127,6 +135,29 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
127135 fprintf (stderr, " \n " );
128136}
129137
138+ bool send_webhook_request (const std::string &node_id, const std::string &data) {
139+ CURL *curl;
140+ CURLcode res;
141+ std::string url = " https://aci.crolard.fr/api/webhook/" + node_id + " /" + data;
142+
143+ curl = curl_easy_init ();
144+ if (curl) {
145+ curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
146+ curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
147+
148+ res = curl_easy_perform (curl);
149+ if (res != CURLE_OK) {
150+ return false ;
151+ } else {
152+ return true ;
153+ }
154+
155+ curl_easy_cleanup (curl);
156+ } else {
157+ return false ;
158+ }
159+ }
160+
130161int main (int argc, char ** argv) {
131162 whisper_params params;
132163
@@ -249,6 +280,19 @@ int main(int argc, char ** argv) {
249280 auto t_last = std::chrono::high_resolution_clock::now ();
250281 const auto t_start = t_last;
251282
283+
284+ std::thread aliveThread ([&]{
285+ while (true ) {
286+ json data;
287+ data[" localisation" ] = params.location ;
288+ // escape the string to it can be sent on a URL
289+ std::string json = data.dump ();
290+ std::string escaped = curl_easy_escape (nullptr , json.c_str (), json.size ());
291+ send_webhook_request (std::to_string (params.id ), escaped);
292+ std::this_thread::sleep_for (std::chrono::seconds (3 ));
293+ }
294+ });
295+
252296 // main audio loop
253297 while (is_running) {
254298 if (params.save_audio ) {
@@ -261,7 +305,6 @@ int main(int argc, char ** argv) {
261305 break ;
262306 }
263307
264-
265308 if (params.remote ) {
266309 producer->poll (0 );
267310 }
@@ -307,7 +350,7 @@ int main(int argc, char ** argv) {
307350 const auto t_diff = std::chrono::duration_cast<std::chrono::milliseconds>(t_now - t_last).count ();
308351
309352 if (t_diff < VAD_WINDOW_SIZE) {
310- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
353+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
311354 continue ;
312355 }
313356
@@ -329,13 +372,11 @@ int main(int argc, char ** argv) {
329372 inference_avg = inference_avg / inferences.size ();
330373
331374 bool vad_output = inference_avg > params.vad_thold ;
332- // bool vad_simple = ::vad_simple(pcmf32_new, WHISPER_SAMPLE_RATE, 1000, params.vad_thold, params.freq_thold, false);
333375
334376 if (vad_output) {
335377 audio.get (params.length_ms , pcmf32);
336378 } else {
337- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
338-
379+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
339380 continue ;
340381 }
341382
@@ -394,9 +435,8 @@ int main(int argc, char ** argv) {
394435 for (int i = 0 ; i < n_segments; ++i) {
395436 const char * text = whisper_full_get_segment_text (ctx, i);
396437
397- const char first_character = text[0 ];
398- if (params.remote && first_character != ' [' ) {
399- build_and_send_message (producer, params.topic , text);
438+ if (params.remote ) {
439+ build_and_send_message (producer, params.topic , text, params.id );
400440 }
401441
402442 if (params.no_timestamps ) {
0 commit comments