felrock commited on
Commit
da3cdf4
·
unverified ·
1 Parent(s): 7d4b654

examples : clean up common code (#1871)

Browse files

move some utility functions into common.h

examples/CMakeLists.txt CHANGED
@@ -54,6 +54,9 @@ if (WHISPER_SDL2)
54
  set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
55
  endif()
56
 
 
 
 
57
  # examples
58
 
59
  include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 
54
  set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
55
  endif()
56
 
57
+ # add json lib
58
+ add_library(json_cpp INTERFACE json.hpp)
59
+
60
  # examples
61
 
62
  include_directories(${CMAKE_CURRENT_SOURCE_DIR})
examples/addon.node/addon.cpp CHANGED
@@ -52,27 +52,6 @@ struct whisper_print_user_data {
52
  const std::vector<std::vector<float>> * pcmf32s;
53
  };
54
 
55
- // 500 -> 00:05.000
56
- // 6000 -> 01:00.000
57
- std::string to_timestamp(int64_t t, bool comma = false) {
58
- int64_t msec = t * 10;
59
- int64_t hr = msec / (1000 * 60 * 60);
60
- msec = msec - hr * (1000 * 60 * 60);
61
- int64_t min = msec / (1000 * 60);
62
- msec = msec - min * (1000 * 60);
63
- int64_t sec = msec / 1000;
64
- msec = msec - sec * 1000;
65
-
66
- char buf[32];
67
- snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
68
-
69
- return std::string(buf);
70
- }
71
-
72
- int timestamp_to_sample(int64_t t, int n_samples) {
73
- return std::max(0, std::min((int) n_samples - 1, (int) ((t*WHISPER_SAMPLE_RATE)/100)));
74
- }
75
-
76
  void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
77
  const auto & params = *((whisper_print_user_data *) user_data)->params;
78
  const auto & pcmf32s = *((whisper_print_user_data *) user_data)->pcmf32s;
@@ -104,8 +83,8 @@ void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper
104
  if (params.diarize && pcmf32s.size() == 2) {
105
  const int64_t n_samples = pcmf32s[0].size();
106
 
107
- const int64_t is0 = timestamp_to_sample(t0, n_samples);
108
- const int64_t is1 = timestamp_to_sample(t1, n_samples);
109
 
110
  double energy0 = 0.0f;
111
  double energy1 = 0.0f;
 
52
  const std::vector<std::vector<float>> * pcmf32s;
53
  };
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
56
  const auto & params = *((whisper_print_user_data *) user_data)->params;
57
  const auto & pcmf32s = *((whisper_print_user_data *) user_data)->pcmf32s;
 
83
  if (params.diarize && pcmf32s.size() == 2) {
84
  const int64_t n_samples = pcmf32s[0].size();
85
 
86
+ const int64_t is0 = timestamp_to_sample(t0, n_samples, WHISPER_SAMPLE_RATE);
87
+ const int64_t is1 = timestamp_to_sample(t1, n_samples, WHISPER_SAMPLE_RATE);
88
 
89
  double energy0 = 0.0f;
90
  double energy1 = 0.0f;
examples/command/command.cpp CHANGED
@@ -22,11 +22,6 @@
22
  #include <vector>
23
  #include <map>
24
 
25
- bool file_exists(const std::string & fname) {
26
- std::ifstream f(fname.c_str());
27
- return f.good();
28
- }
29
-
30
  // command-line parameters
31
  struct whisper_params {
32
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
@@ -736,7 +731,7 @@ int main(int argc, char ** argv) {
736
 
737
  if (!params.grammar.empty()) {
738
  auto & grammar = params.grammar_parsed;
739
- if (file_exists(params.grammar.c_str())) {
740
  // read grammar from file
741
  std::ifstream ifs(params.grammar.c_str());
742
  const std::string txt = std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
 
22
  #include <vector>
23
  #include <map>
24
 
 
 
 
 
 
25
  // command-line parameters
26
  struct whisper_params {
27
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
 
731
 
732
  if (!params.grammar.empty()) {
733
  auto & grammar = params.grammar_parsed;
734
+ if (is_file_exist(params.grammar.c_str())) {
735
  // read grammar from file
736
  std::ifstream ifs(params.grammar.c_str());
737
  const std::string txt = std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
examples/common.cpp CHANGED
@@ -836,3 +836,30 @@ void sam_print_usage(int /*argc*/, char ** argv, const sam_params & params) {
836
  fprintf(stderr, " output file (default: %s)\n", params.fname_out.c_str());
837
  fprintf(stderr, "\n");
838
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836
  fprintf(stderr, " output file (default: %s)\n", params.fname_out.c_str());
837
  fprintf(stderr, "\n");
838
  }
839
+
840
+ // 500 -> 00:05.000
841
+ // 6000 -> 01:00.000
842
+ std::string to_timestamp(int64_t t, bool comma) {
843
+ int64_t msec = t * 10;
844
+ int64_t hr = msec / (1000 * 60 * 60);
845
+ msec = msec - hr * (1000 * 60 * 60);
846
+ int64_t min = msec / (1000 * 60);
847
+ msec = msec - min * (1000 * 60);
848
+ int64_t sec = msec / 1000;
849
+ msec = msec - sec * 1000;
850
+
851
+ char buf[32];
852
+ snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
853
+
854
+ return std::string(buf);
855
+ }
856
+
857
+ int timestamp_to_sample(int64_t t, int n_samples, int whisper_sample_rate) {
858
+ return std::max(0, std::min((int) n_samples - 1, (int) ((t*whisper_sample_rate)/100)));
859
+ }
860
+
861
+ bool is_file_exist(const char *fileName)
862
+ {
863
+ std::ifstream infile(fileName);
864
+ return infile.good();
865
+ }
examples/common.h CHANGED
@@ -281,3 +281,28 @@ struct sam_params {
281
  bool sam_params_parse(int argc, char ** argv, sam_params & params);
282
 
283
  void sam_print_usage(int argc, char ** argv, const sam_params & params);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  bool sam_params_parse(int argc, char ** argv, sam_params & params);
282
 
283
  void sam_print_usage(int argc, char ** argv, const sam_params & params);
284
+
285
+ //
286
+ // Terminal utils
287
+ //
288
+
289
+
290
+ // Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
291
+ // Lowest is red, middle is yellow, highest is green.
292
+ const std::vector<std::string> k_colors = {
293
+ "\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
294
+ "\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
295
+ };
296
+
297
+ //
298
+ // Other utils
299
+ //
300
+
301
+ // convert timestamp to string, 6000 -> 01:00.000
302
+ std::string to_timestamp(int64_t t, bool comma = false);
303
+
304
+ // given a timestamp get the sample
305
+ int timestamp_to_sample(int64_t t, int n_samples, int whisper_sample_rate);
306
+
307
+ // check if file exists using ifstream
308
+ bool is_file_exist(const char *fileName);
examples/{lsp/json.hpp → json.hpp} RENAMED
File without changes
examples/lsp/CMakeLists.txt CHANGED
@@ -5,5 +5,5 @@ if (WHISPER_SDL2)
5
 
6
  include(DefaultTargetOptions)
7
 
8
- target_link_libraries(${TARGET} PRIVATE common common-sdl whisper ${CMAKE_THREAD_LIBS_INIT})
9
  endif ()
 
5
 
6
  include(DefaultTargetOptions)
7
 
8
+ target_link_libraries(${TARGET} PRIVATE common json_cpp common-sdl whisper ${CMAKE_THREAD_LIBS_INIT})
9
  endif ()
examples/main/main.cpp CHANGED
@@ -14,34 +14,6 @@
14
  #pragma warning(disable: 4244 4267) // possible loss of data
15
  #endif
16
 
17
- // Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
18
- // Lowest is red, middle is yellow, highest is green.
19
- const std::vector<std::string> k_colors = {
20
- "\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
21
- "\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
22
- };
23
-
24
- // 500 -> 00:05.000
25
- // 6000 -> 01:00.000
26
- std::string to_timestamp(int64_t t, bool comma = false) {
27
- int64_t msec = t * 10;
28
- int64_t hr = msec / (1000 * 60 * 60);
29
- msec = msec - hr * (1000 * 60 * 60);
30
- int64_t min = msec / (1000 * 60);
31
- msec = msec - min * (1000 * 60);
32
- int64_t sec = msec / 1000;
33
- msec = msec - sec * 1000;
34
-
35
- char buf[32];
36
- snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
37
-
38
- return std::string(buf);
39
- }
40
-
41
- int timestamp_to_sample(int64_t t, int n_samples) {
42
- return std::max(0, std::min((int) n_samples - 1, (int) ((t*WHISPER_SAMPLE_RATE)/100)));
43
- }
44
-
45
  // helper function to replace substrings
46
  void replace_all(std::string & s, const std::string & search, const std::string & replace) {
47
  for (size_t pos = 0; ; pos += replace.length()) {
@@ -244,8 +216,8 @@ std::string estimate_diarization_speaker(std::vector<std::vector<float>> pcmf32s
244
  std::string speaker = "";
245
  const int64_t n_samples = pcmf32s[0].size();
246
 
247
- const int64_t is0 = timestamp_to_sample(t0, n_samples);
248
- const int64_t is1 = timestamp_to_sample(t1, n_samples);
249
 
250
  double energy0 = 0.0f;
251
  double energy1 = 0.0f;
 
14
  #pragma warning(disable: 4244 4267) // possible loss of data
15
  #endif
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  // helper function to replace substrings
18
  void replace_all(std::string & s, const std::string & search, const std::string & replace) {
19
  for (size_t pos = 0; ; pos += replace.length()) {
 
216
  std::string speaker = "";
217
  const int64_t n_samples = pcmf32s[0].size();
218
 
219
+ const int64_t is0 = timestamp_to_sample(t0, n_samples, WHISPER_SAMPLE_RATE);
220
+ const int64_t is1 = timestamp_to_sample(t1, n_samples, WHISPER_SAMPLE_RATE);
221
 
222
  double energy0 = 0.0f;
223
  double energy1 = 0.0f;
examples/server/CMakeLists.txt CHANGED
@@ -1,9 +1,9 @@
1
  set(TARGET server)
2
- add_executable(${TARGET} server.cpp httplib.h json.hpp)
3
 
4
  include(DefaultTargetOptions)
5
 
6
- target_link_libraries(${TARGET} PRIVATE common whisper ${CMAKE_THREAD_LIBS_INIT})
7
 
8
  if (WIN32)
9
  target_link_libraries(${TARGET} PRIVATE ws2_32)
 
1
  set(TARGET server)
2
+ add_executable(${TARGET} server.cpp httplib.h)
3
 
4
  include(DefaultTargetOptions)
5
 
6
+ target_link_libraries(${TARGET} PRIVATE common json_cpp whisper ${CMAKE_THREAD_LIBS_INIT})
7
 
8
  if (WIN32)
9
  target_link_libraries(${TARGET} PRIVATE ws2_32)
examples/server/json.hpp DELETED
The diff for this file is too large to render. See raw diff
 
examples/server/server.cpp CHANGED
@@ -22,13 +22,6 @@ using json = nlohmann::ordered_json;
22
 
23
  namespace {
24
 
25
- // Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
26
- // Lowest is red, middle is yellow, highest is green.
27
- const std::vector<std::string> k_colors = {
28
- "\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
29
- "\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
30
- };
31
-
32
  // output formats
33
  const std::string json_format = "json";
34
  const std::string text_format = "text";
@@ -96,33 +89,6 @@ struct whisper_params {
96
  std::string openvino_encode_device = "CPU";
97
  };
98
 
99
- // 500 -> 00:05.000
100
- // 6000 -> 01:00.000
101
- std::string to_timestamp(int64_t t, bool comma = false) {
102
- int64_t msec = t * 10;
103
- int64_t hr = msec / (1000 * 60 * 60);
104
- msec = msec - hr * (1000 * 60 * 60);
105
- int64_t min = msec / (1000 * 60);
106
- msec = msec - min * (1000 * 60);
107
- int64_t sec = msec / 1000;
108
- msec = msec - sec * 1000;
109
-
110
- char buf[32];
111
- snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
112
-
113
- return std::string(buf);
114
- }
115
-
116
- int timestamp_to_sample(int64_t t, int n_samples) {
117
- return std::max(0, std::min((int) n_samples - 1, (int) ((t*WHISPER_SAMPLE_RATE)/100)));
118
- }
119
-
120
- bool is_file_exist(const char *fileName)
121
- {
122
- std::ifstream infile(fileName);
123
- return infile.good();
124
- }
125
-
126
  void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & params, const server_params& sparams) {
127
  fprintf(stderr, "\n");
128
  fprintf(stderr, "usage: %s [options] \n", argv[0]);
@@ -274,8 +240,8 @@ std::string estimate_diarization_speaker(std::vector<std::vector<float>> pcmf32s
274
  std::string speaker = "";
275
  const int64_t n_samples = pcmf32s[0].size();
276
 
277
- const int64_t is0 = timestamp_to_sample(t0, n_samples);
278
- const int64_t is1 = timestamp_to_sample(t1, n_samples);
279
 
280
  double energy0 = 0.0f;
281
  double energy1 = 0.0f;
@@ -629,7 +595,7 @@ int main(int argc, char ** argv) {
629
  return false;
630
  });
631
 
632
- svr.Options(sparams.request_path + "/inference", [&](const Request &req, Response &res){
633
  });
634
 
635
  svr.Post(sparams.request_path + "/inference", [&](const Request &req, Response &res){
 
22
 
23
  namespace {
24
 
 
 
 
 
 
 
 
25
  // output formats
26
  const std::string json_format = "json";
27
  const std::string text_format = "text";
 
89
  std::string openvino_encode_device = "CPU";
90
  };
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & params, const server_params& sparams) {
93
  fprintf(stderr, "\n");
94
  fprintf(stderr, "usage: %s [options] \n", argv[0]);
 
240
  std::string speaker = "";
241
  const int64_t n_samples = pcmf32s[0].size();
242
 
243
+ const int64_t is0 = timestamp_to_sample(t0, n_samples, WHISPER_SAMPLE_RATE);
244
+ const int64_t is1 = timestamp_to_sample(t1, n_samples, WHISPER_SAMPLE_RATE);
245
 
246
  double energy0 = 0.0f;
247
  double energy1 = 0.0f;
 
595
  return false;
596
  });
597
 
598
+ svr.Options(sparams.request_path + "/inference", [&](const Request &, Response &){
599
  });
600
 
601
  svr.Post(sparams.request_path + "/inference", [&](const Request &req, Response &res){
examples/stream/stream.cpp CHANGED
@@ -14,20 +14,6 @@
14
  #include <fstream>
15
 
16
 
17
- // 500 -> 00:05.000
18
- // 6000 -> 01:00.000
19
- std::string to_timestamp(int64_t t) {
20
- int64_t sec = t/100;
21
- int64_t msec = t - sec*100;
22
- int64_t min = sec/60;
23
- sec = sec - min*60;
24
-
25
- char buf[32];
26
- snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);
27
-
28
- return std::string(buf);
29
- }
30
-
31
  // command-line parameters
32
  struct whisper_params {
33
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
@@ -372,7 +358,7 @@ int main(int argc, char ** argv) {
372
  const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
373
  const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
374
 
375
- std::string output = "[" + to_timestamp(t0) + " --> " + to_timestamp(t1) + "] " + text;
376
 
377
  if (whisper_full_get_segment_speaker_turn_next(ctx, i)) {
378
  output += " [SPEAKER_TURN]";
 
14
  #include <fstream>
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  // command-line parameters
18
  struct whisper_params {
19
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
 
358
  const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
359
  const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
360
 
361
+ std::string output = "[" + to_timestamp(t0, false) + " --> " + to_timestamp(t1, false) + "] " + text;
362
 
363
  if (whisper_full_get_segment_speaker_turn_next(ctx, i)) {
364
  output += " [SPEAKER_TURN]";
examples/talk.wasm/emscripten.cpp CHANGED
@@ -29,18 +29,6 @@ std::string g_status_forced = "";
29
 
30
  std::vector<float> g_pcmf32;
31
 
32
- std::string to_timestamp(int64_t t) {
33
- int64_t sec = t/100;
34
- int64_t msec = t - sec*100;
35
- int64_t min = sec/60;
36
- sec = sec - min*60;
37
-
38
- char buf[32];
39
- snprintf(buf, sizeof(buf), "%02d:%02d.%03d", (int) min, (int) sec, (int) msec);
40
-
41
- return std::string(buf);
42
- }
43
-
44
  void talk_set_status(const std::string & status) {
45
  std::lock_guard<std::mutex> lock(g_mutex);
46
  g_status = status;
 
29
 
30
  std::vector<float> g_pcmf32;
31
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  void talk_set_status(const std::string & status) {
33
  std::lock_guard<std::mutex> lock(g_mutex);
34
  g_status = status;