Spaces:
Running
Running
main : merge parallel example in main
Browse files- examples/CMakeLists.txt +0 -1
- examples/main/main.cpp +15 -9
- examples/parallel/CMakeLists.txt +0 -3
- examples/parallel/README.md +0 -3
- examples/parallel/parallel.cpp +0 -432
examples/CMakeLists.txt
CHANGED
|
@@ -22,7 +22,6 @@ if (EMSCRIPTEN)
|
|
| 22 |
add_subdirectory(whisper.wasm)
|
| 23 |
else()
|
| 24 |
add_subdirectory(main)
|
| 25 |
-
add_subdirectory(parallel)
|
| 26 |
add_subdirectory(stream)
|
| 27 |
add_subdirectory(bench)
|
| 28 |
endif()
|
|
|
|
| 22 |
add_subdirectory(whisper.wasm)
|
| 23 |
else()
|
| 24 |
add_subdirectory(main)
|
|
|
|
| 25 |
add_subdirectory(stream)
|
| 26 |
add_subdirectory(bench)
|
| 27 |
endif()
|
examples/main/main.cpp
CHANGED
|
@@ -38,11 +38,12 @@ std::string to_timestamp(int64_t t, bool comma = false) {
|
|
| 38 |
|
| 39 |
// command-line parameters
|
| 40 |
struct whisper_params {
|
| 41 |
-
int32_t seed
|
| 42 |
-
int32_t n_threads
|
| 43 |
-
int32_t
|
| 44 |
-
int32_t
|
| 45 |
-
int32_t
|
|
|
|
| 46 |
|
| 47 |
bool verbose = false;
|
| 48 |
bool translate = false;
|
|
@@ -74,6 +75,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
|
| 74 |
params.seed = std::stoi(argv[++i]);
|
| 75 |
} else if (arg == "-t" || arg == "--threads") {
|
| 76 |
params.n_threads = std::stoi(argv[++i]);
|
|
|
|
|
|
|
| 77 |
} else if (arg == "-ot" || arg == "--offset-t") {
|
| 78 |
params.offset_t_ms = std::stoi(argv[++i]);
|
| 79 |
} else if (arg == "-on" || arg == "--offset-n") {
|
|
@@ -128,6 +131,7 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
|
|
| 128 |
fprintf(stderr, " -h, --help show this help message and exit\n");
|
| 129 |
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
|
| 130 |
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
|
|
|
|
| 131 |
fprintf(stderr, " -ot N, --offset-t N time offset in milliseconds (default: %d)\n", params.offset_t_ms);
|
| 132 |
fprintf(stderr, " -on N, --offset-n N segment index offset (default: %d)\n", params.offset_n);
|
| 133 |
fprintf(stderr, " -mc N, --max-context N maximum number of text context tokens to store (default: max)\n");
|
|
@@ -350,7 +354,8 @@ int main(int argc, char ** argv) {
|
|
| 350 |
// print system information
|
| 351 |
{
|
| 352 |
fprintf(stderr, "\n");
|
| 353 |
-
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
|
|
|
|
| 354 |
}
|
| 355 |
|
| 356 |
// print some info about the processing
|
|
@@ -363,8 +368,9 @@ int main(int argc, char ** argv) {
|
|
| 363 |
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
| 364 |
}
|
| 365 |
}
|
| 366 |
-
fprintf(stderr, "%s: processing '%s' (%d samples, %.1f sec), %d threads, lang = %s, task = %s, timestamps = %d ...\n",
|
| 367 |
-
__func__, fname_inp.c_str(), int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE,
|
|
|
|
| 368 |
params.language.c_str(),
|
| 369 |
params.translate ? "translate" : "transcribe",
|
| 370 |
params.no_timestamps ? 0 : 1);
|
|
@@ -393,7 +399,7 @@ int main(int argc, char ** argv) {
|
|
| 393 |
wparams.new_segment_callback_user_data = ¶ms;
|
| 394 |
}
|
| 395 |
|
| 396 |
-
if (
|
| 397 |
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
|
| 398 |
return 8;
|
| 399 |
}
|
|
|
|
| 38 |
|
| 39 |
// command-line parameters
|
| 40 |
struct whisper_params {
|
| 41 |
+
int32_t seed = -1; // RNG seed, not used currently
|
| 42 |
+
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
| 43 |
+
int32_t n_processors = 1;
|
| 44 |
+
int32_t offset_t_ms = 0;
|
| 45 |
+
int32_t offset_n = 0;
|
| 46 |
+
int32_t max_context = -1;
|
| 47 |
|
| 48 |
bool verbose = false;
|
| 49 |
bool translate = false;
|
|
|
|
| 75 |
params.seed = std::stoi(argv[++i]);
|
| 76 |
} else if (arg == "-t" || arg == "--threads") {
|
| 77 |
params.n_threads = std::stoi(argv[++i]);
|
| 78 |
+
} else if (arg == "-p" || arg == "--processors") {
|
| 79 |
+
params.n_processors = std::stoi(argv[++i]);
|
| 80 |
} else if (arg == "-ot" || arg == "--offset-t") {
|
| 81 |
params.offset_t_ms = std::stoi(argv[++i]);
|
| 82 |
} else if (arg == "-on" || arg == "--offset-n") {
|
|
|
|
| 131 |
fprintf(stderr, " -h, --help show this help message and exit\n");
|
| 132 |
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
|
| 133 |
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
|
| 134 |
+
fprintf(stderr, " -p N, --processors N number of processors to use during computation (default: %d)\n", params.n_processors);
|
| 135 |
fprintf(stderr, " -ot N, --offset-t N time offset in milliseconds (default: %d)\n", params.offset_t_ms);
|
| 136 |
fprintf(stderr, " -on N, --offset-n N segment index offset (default: %d)\n", params.offset_n);
|
| 137 |
fprintf(stderr, " -mc N, --max-context N maximum number of text context tokens to store (default: max)\n");
|
|
|
|
| 354 |
// print system information
|
| 355 |
{
|
| 356 |
fprintf(stderr, "\n");
|
| 357 |
+
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
|
| 358 |
+
params.n_threads*params.n_processors, std::thread::hardware_concurrency(), whisper_print_system_info());
|
| 359 |
}
|
| 360 |
|
| 361 |
// print some info about the processing
|
|
|
|
| 368 |
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
| 369 |
}
|
| 370 |
}
|
| 371 |
+
fprintf(stderr, "%s: processing '%s' (%d samples, %.1f sec), %d threads, %d processors, lang = %s, task = %s, timestamps = %d ...\n",
|
| 372 |
+
__func__, fname_inp.c_str(), int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE,
|
| 373 |
+
params.n_threads, params.n_processors,
|
| 374 |
params.language.c_str(),
|
| 375 |
params.translate ? "translate" : "transcribe",
|
| 376 |
params.no_timestamps ? 0 : 1);
|
|
|
|
| 399 |
wparams.new_segment_callback_user_data = ¶ms;
|
| 400 |
}
|
| 401 |
|
| 402 |
+
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
| 403 |
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
|
| 404 |
return 8;
|
| 405 |
}
|
examples/parallel/CMakeLists.txt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
set(TARGET parallel)
|
| 2 |
-
add_executable(${TARGET} parallel.cpp)
|
| 3 |
-
target_link_libraries(${TARGET} PRIVATE whisper ${CMAKE_THREAD_LIBS_INIT})
|
|
|
|
|
|
|
|
|
|
|
|
examples/parallel/README.md
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
# parallel
|
| 2 |
-
|
| 3 |
-
TODO
|
|
|
|
|
|
|
|
|
|
|
|
examples/parallel/parallel.cpp
DELETED
|
@@ -1,432 +0,0 @@
|
|
| 1 |
-
#include "whisper.h"
|
| 2 |
-
|
| 3 |
-
// third-party utilities
|
| 4 |
-
// use your favorite implementations
|
| 5 |
-
#define DR_WAV_IMPLEMENTATION
|
| 6 |
-
#include "dr_wav.h"
|
| 7 |
-
|
| 8 |
-
#include <cmath>
|
| 9 |
-
#include <fstream>
|
| 10 |
-
#include <cstdio>
|
| 11 |
-
#include <string>
|
| 12 |
-
#include <thread>
|
| 13 |
-
#include <vector>
|
| 14 |
-
|
| 15 |
-
// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
|
| 16 |
-
// Lowest is red, middle is yellow, highest is green.
|
| 17 |
-
const std::vector<std::string> k_colors = {
|
| 18 |
-
"\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
|
| 19 |
-
"\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
|
| 20 |
-
};
|
| 21 |
-
|
| 22 |
-
// 500 -> 00:05.000
|
| 23 |
-
// 6000 -> 01:00.000
|
| 24 |
-
std::string to_timestamp(int64_t t, bool comma = false) {
|
| 25 |
-
int64_t msec = t * 10;
|
| 26 |
-
int64_t hr = msec / (1000 * 60 * 60);
|
| 27 |
-
msec = msec - hr * (1000 * 60 * 60);
|
| 28 |
-
int64_t min = msec / (1000 * 60);
|
| 29 |
-
msec = msec - min * (1000 * 60);
|
| 30 |
-
int64_t sec = msec / 1000;
|
| 31 |
-
msec = msec - sec * 1000;
|
| 32 |
-
|
| 33 |
-
char buf[32];
|
| 34 |
-
snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);
|
| 35 |
-
|
| 36 |
-
return std::string(buf);
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
// command-line parameters
|
| 40 |
-
struct whisper_params {
|
| 41 |
-
int32_t seed = -1; // RNG seed, not used currently
|
| 42 |
-
int32_t n_threads = std::max(std::min(4, (int32_t) std::thread::hardware_concurrency()) / 2, 1);
|
| 43 |
-
int32_t n_processors = 2;
|
| 44 |
-
int32_t offset_t_ms = 0;
|
| 45 |
-
int32_t offset_n = 0;
|
| 46 |
-
int32_t max_context = -1;
|
| 47 |
-
|
| 48 |
-
bool verbose = false;
|
| 49 |
-
bool translate = false;
|
| 50 |
-
bool output_txt = false;
|
| 51 |
-
bool output_vtt = false;
|
| 52 |
-
bool output_srt = false;
|
| 53 |
-
bool print_special_tokens = false;
|
| 54 |
-
bool print_colors = false;
|
| 55 |
-
bool no_timestamps = false;
|
| 56 |
-
|
| 57 |
-
std::string language = "en";
|
| 58 |
-
std::string model = "models/ggml-base.en.bin";
|
| 59 |
-
|
| 60 |
-
std::vector<std::string> fname_inp = {};
|
| 61 |
-
};
|
| 62 |
-
|
| 63 |
-
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
| 64 |
-
|
| 65 |
-
bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
| 66 |
-
for (int i = 1; i < argc; i++) {
|
| 67 |
-
std::string arg = argv[i];
|
| 68 |
-
|
| 69 |
-
if (arg[0] != '-') {
|
| 70 |
-
params.fname_inp.push_back(arg);
|
| 71 |
-
continue;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
if (arg == "-s" || arg == "--seed") {
|
| 75 |
-
params.seed = std::stoi(argv[++i]);
|
| 76 |
-
} else if (arg == "-t" || arg == "--threads") {
|
| 77 |
-
params.n_threads = std::stoi(argv[++i]);
|
| 78 |
-
} else if (arg == "-p" || arg == "--processors") {
|
| 79 |
-
params.n_processors = std::stoi(argv[++i]);
|
| 80 |
-
} else if (arg == "-ot" || arg == "--offset-t") {
|
| 81 |
-
params.offset_t_ms = std::stoi(argv[++i]);
|
| 82 |
-
} else if (arg == "-on" || arg == "--offset-n") {
|
| 83 |
-
params.offset_n = std::stoi(argv[++i]);
|
| 84 |
-
} else if (arg == "-mc" || arg == "--max-context") {
|
| 85 |
-
params.max_context = std::stoi(argv[++i]);
|
| 86 |
-
} else if (arg == "-v" || arg == "--verbose") {
|
| 87 |
-
params.verbose = true;
|
| 88 |
-
} else if (arg == "--translate") {
|
| 89 |
-
params.translate = true;
|
| 90 |
-
} else if (arg == "-l" || arg == "--language") {
|
| 91 |
-
params.language = argv[++i];
|
| 92 |
-
if (whisper_lang_id(params.language.c_str()) == -1) {
|
| 93 |
-
fprintf(stderr, "error: unknown language '%s'\n", params.language.c_str());
|
| 94 |
-
whisper_print_usage(argc, argv, params);
|
| 95 |
-
exit(0);
|
| 96 |
-
}
|
| 97 |
-
} else if (arg == "-otxt" || arg == "--output-txt") {
|
| 98 |
-
params.output_txt = true;
|
| 99 |
-
} else if (arg == "-ovtt" || arg == "--output-vtt") {
|
| 100 |
-
params.output_vtt = true;
|
| 101 |
-
} else if (arg == "-osrt" || arg == "--output-srt") {
|
| 102 |
-
params.output_srt = true;
|
| 103 |
-
} else if (arg == "-ps" || arg == "--print_special") {
|
| 104 |
-
params.print_special_tokens = true;
|
| 105 |
-
} else if (arg == "-pc" || arg == "--print_colors") {
|
| 106 |
-
params.print_colors = true;
|
| 107 |
-
} else if (arg == "-nt" || arg == "--no_timestamps") {
|
| 108 |
-
params.no_timestamps = true;
|
| 109 |
-
} else if (arg == "-m" || arg == "--model") {
|
| 110 |
-
params.model = argv[++i];
|
| 111 |
-
} else if (arg == "-f" || arg == "--file") {
|
| 112 |
-
params.fname_inp.push_back(argv[++i]);
|
| 113 |
-
} else if (arg == "-h" || arg == "--help") {
|
| 114 |
-
whisper_print_usage(argc, argv, params);
|
| 115 |
-
exit(0);
|
| 116 |
-
} else {
|
| 117 |
-
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
| 118 |
-
whisper_print_usage(argc, argv, params);
|
| 119 |
-
exit(0);
|
| 120 |
-
}
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
return true;
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
void whisper_print_usage(int argc, char ** argv, const whisper_params & params) {
|
| 127 |
-
fprintf(stderr, "\n");
|
| 128 |
-
fprintf(stderr, "usage: %s [options] file0.wav file1.wav ...\n", argv[0]);
|
| 129 |
-
fprintf(stderr, "\n");
|
| 130 |
-
fprintf(stderr, "options:\n");
|
| 131 |
-
fprintf(stderr, " -h, --help show this help message and exit\n");
|
| 132 |
-
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
|
| 133 |
-
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
|
| 134 |
-
fprintf(stderr, " -p N, --processors N number of processors to use during computation (default: %d)\n", params.n_processors);
|
| 135 |
-
fprintf(stderr, " -ot N, --offset-t N time offset in milliseconds (default: %d)\n", params.offset_t_ms);
|
| 136 |
-
fprintf(stderr, " -on N, --offset-n N segment index offset (default: %d)\n", params.offset_n);
|
| 137 |
-
fprintf(stderr, " -mc N, --max-context N maximum number of text context tokens to store (default: max)\n");
|
| 138 |
-
fprintf(stderr, " -v, --verbose verbose output\n");
|
| 139 |
-
fprintf(stderr, " --translate translate from source language to english\n");
|
| 140 |
-
fprintf(stderr, " -otxt, --output-txt output result in a text file\n");
|
| 141 |
-
fprintf(stderr, " -ovtt, --output-vtt output result in a vtt file\n");
|
| 142 |
-
fprintf(stderr, " -osrt, --output-srt output result in a srt file\n");
|
| 143 |
-
fprintf(stderr, " -ps, --print_special print special tokens\n");
|
| 144 |
-
fprintf(stderr, " -pc, --print_colors print colors\n");
|
| 145 |
-
fprintf(stderr, " -nt, --no_timestamps do not print timestamps\n");
|
| 146 |
-
fprintf(stderr, " -l LANG, --language LANG spoken language (default: %s)\n", params.language.c_str());
|
| 147 |
-
fprintf(stderr, " -m FNAME, --model FNAME model path (default: %s)\n", params.model.c_str());
|
| 148 |
-
fprintf(stderr, " -f FNAME, --file FNAME input WAV file path\n");
|
| 149 |
-
fprintf(stderr, "\n");
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
void whisper_print_segment_callback(struct whisper_context * ctx, void * user_data) {
|
| 153 |
-
const whisper_params & params = *(whisper_params *) user_data;
|
| 154 |
-
|
| 155 |
-
const int n_segments = whisper_full_n_segments(ctx);
|
| 156 |
-
|
| 157 |
-
// print the last segment
|
| 158 |
-
const int i = n_segments - 1;
|
| 159 |
-
if (i == 0) {
|
| 160 |
-
printf("\n");
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
if (params.no_timestamps) {
|
| 164 |
-
if (params.print_colors) {
|
| 165 |
-
for (int j = 0; j < whisper_full_n_tokens(ctx, i); ++j) {
|
| 166 |
-
if (params.print_special_tokens == false) {
|
| 167 |
-
const whisper_token id = whisper_full_get_token_id(ctx, i, j);
|
| 168 |
-
if (id >= whisper_token_eot(ctx)) {
|
| 169 |
-
continue;
|
| 170 |
-
}
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
const char * text = whisper_full_get_token_text(ctx, i, j);
|
| 174 |
-
const float p = whisper_full_get_token_p (ctx, i, j);
|
| 175 |
-
|
| 176 |
-
const int col = std::max(0, std::min((int) k_colors.size(), (int) (std::pow(p, 3)*float(k_colors.size()))));
|
| 177 |
-
|
| 178 |
-
printf("%s%s%s", k_colors[col].c_str(), text, "\033[0m");
|
| 179 |
-
}
|
| 180 |
-
} else {
|
| 181 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 182 |
-
printf("%s", text);
|
| 183 |
-
}
|
| 184 |
-
fflush(stdout);
|
| 185 |
-
} else {
|
| 186 |
-
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 187 |
-
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 188 |
-
|
| 189 |
-
if (params.print_colors) {
|
| 190 |
-
printf("[%s --> %s] ", to_timestamp(t0).c_str(), to_timestamp(t1).c_str());
|
| 191 |
-
for (int j = 0; j < whisper_full_n_tokens(ctx, i); ++j) {
|
| 192 |
-
if (params.print_special_tokens == false) {
|
| 193 |
-
const whisper_token id = whisper_full_get_token_id(ctx, i, j);
|
| 194 |
-
if (id >= whisper_token_eot(ctx)) {
|
| 195 |
-
continue;
|
| 196 |
-
}
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
const char * text = whisper_full_get_token_text(ctx, i, j);
|
| 200 |
-
const float p = whisper_full_get_token_p (ctx, i, j);
|
| 201 |
-
|
| 202 |
-
const int col = std::max(0, std::min((int) k_colors.size(), (int) (std::pow(p, 3)*float(k_colors.size()))));
|
| 203 |
-
|
| 204 |
-
printf("%s%s%s", k_colors[col].c_str(), text, "\033[0m");
|
| 205 |
-
}
|
| 206 |
-
printf("\n");
|
| 207 |
-
} else {
|
| 208 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 209 |
-
|
| 210 |
-
printf("[%s --> %s] %s\n", to_timestamp(t0).c_str(), to_timestamp(t1).c_str(), text);
|
| 211 |
-
}
|
| 212 |
-
}
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
bool output_txt(struct whisper_context * ctx, const char * fname) {
|
| 216 |
-
std::ofstream fout(fname);
|
| 217 |
-
if (!fout.is_open()) {
|
| 218 |
-
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
| 219 |
-
return false;
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
|
| 223 |
-
|
| 224 |
-
const int n_segments = whisper_full_n_segments(ctx);
|
| 225 |
-
for (int i = 0; i < n_segments; ++i) {
|
| 226 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 227 |
-
fout << text;
|
| 228 |
-
}
|
| 229 |
-
|
| 230 |
-
return true;
|
| 231 |
-
}
|
| 232 |
-
|
| 233 |
-
bool output_vtt(struct whisper_context * ctx, const char * fname) {
|
| 234 |
-
std::ofstream fout(fname);
|
| 235 |
-
if (!fout.is_open()) {
|
| 236 |
-
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
| 237 |
-
return 9;
|
| 238 |
-
}
|
| 239 |
-
|
| 240 |
-
fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
|
| 241 |
-
|
| 242 |
-
fout << "WEBVTT\n\n";
|
| 243 |
-
|
| 244 |
-
const int n_segments = whisper_full_n_segments(ctx);
|
| 245 |
-
for (int i = 0; i < n_segments; ++i) {
|
| 246 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 247 |
-
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 248 |
-
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 249 |
-
|
| 250 |
-
fout << to_timestamp(t0) << " --> " << to_timestamp(t1) << "\n";
|
| 251 |
-
fout << text << "\n\n";
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
return true;
|
| 255 |
-
}
|
| 256 |
-
|
| 257 |
-
bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 258 |
-
std::ofstream fout(fname);
|
| 259 |
-
if (!fout.is_open()) {
|
| 260 |
-
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
| 261 |
-
return false;
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
-
fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
|
| 265 |
-
|
| 266 |
-
const int n_segments = whisper_full_n_segments(ctx);
|
| 267 |
-
for (int i = 0; i < n_segments; ++i) {
|
| 268 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 269 |
-
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 270 |
-
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 271 |
-
|
| 272 |
-
fout << i + 1 + params.offset_n << "\n";
|
| 273 |
-
fout << to_timestamp(t0, true) << " --> " << to_timestamp(t1, true) << "\n";
|
| 274 |
-
fout << text << "\n\n";
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
return true;
|
| 278 |
-
}
|
| 279 |
-
|
| 280 |
-
int main(int argc, char ** argv) {
|
| 281 |
-
whisper_params params;
|
| 282 |
-
|
| 283 |
-
if (whisper_params_parse(argc, argv, params) == false) {
|
| 284 |
-
return 1;
|
| 285 |
-
}
|
| 286 |
-
|
| 287 |
-
if (params.seed < 0) {
|
| 288 |
-
params.seed = time(NULL);
|
| 289 |
-
}
|
| 290 |
-
|
| 291 |
-
if (params.fname_inp.empty()) {
|
| 292 |
-
fprintf(stderr, "error: no input files specified\n");
|
| 293 |
-
whisper_print_usage(argc, argv, params);
|
| 294 |
-
return 2;
|
| 295 |
-
}
|
| 296 |
-
|
| 297 |
-
// whisper init
|
| 298 |
-
|
| 299 |
-
struct whisper_context * ctx = whisper_init(params.model.c_str());
|
| 300 |
-
|
| 301 |
-
if (ctx == nullptr) {
|
| 302 |
-
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 303 |
-
return 3;
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
|
| 307 |
-
const auto fname_inp = params.fname_inp[f];
|
| 308 |
-
|
| 309 |
-
// WAV input
|
| 310 |
-
std::vector<float> pcmf32;
|
| 311 |
-
{
|
| 312 |
-
drwav wav;
|
| 313 |
-
if (!drwav_init_file(&wav, fname_inp.c_str(), NULL)) {
|
| 314 |
-
fprintf(stderr, "%s: failed to open WAV file '%s' - check your input\n", argv[0], fname_inp.c_str());
|
| 315 |
-
whisper_print_usage(argc, argv, {});
|
| 316 |
-
return 4;
|
| 317 |
-
}
|
| 318 |
-
|
| 319 |
-
if (wav.channels != 1 && wav.channels != 2) {
|
| 320 |
-
fprintf(stderr, "%s: WAV file '%s' must be mono or stereo\n", argv[0], fname_inp.c_str());
|
| 321 |
-
return 5;
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
if (wav.sampleRate != WHISPER_SAMPLE_RATE) {
|
| 325 |
-
fprintf(stderr, "%s: WAV file '%s' must be 16 kHz\n", argv[0], fname_inp.c_str());
|
| 326 |
-
return 6;
|
| 327 |
-
}
|
| 328 |
-
|
| 329 |
-
if (wav.bitsPerSample != 16) {
|
| 330 |
-
fprintf(stderr, "%s: WAV file '%s' must be 16-bit\n", argv[0], fname_inp.c_str());
|
| 331 |
-
return 7;
|
| 332 |
-
}
|
| 333 |
-
|
| 334 |
-
int n = wav.totalPCMFrameCount;
|
| 335 |
-
|
| 336 |
-
std::vector<int16_t> pcm16;
|
| 337 |
-
pcm16.resize(n*wav.channels);
|
| 338 |
-
drwav_read_pcm_frames_s16(&wav, n, pcm16.data());
|
| 339 |
-
drwav_uninit(&wav);
|
| 340 |
-
|
| 341 |
-
// convert to mono, float
|
| 342 |
-
pcmf32.resize(n);
|
| 343 |
-
if (wav.channels == 1) {
|
| 344 |
-
for (int i = 0; i < n; i++) {
|
| 345 |
-
pcmf32[i] = float(pcm16[i])/32768.0f;
|
| 346 |
-
}
|
| 347 |
-
} else {
|
| 348 |
-
for (int i = 0; i < n; i++) {
|
| 349 |
-
pcmf32[i] = float(pcm16[2*i] + pcm16[2*i + 1])/65536.0f;
|
| 350 |
-
}
|
| 351 |
-
}
|
| 352 |
-
}
|
| 353 |
-
|
| 354 |
-
// print system information
|
| 355 |
-
{
|
| 356 |
-
fprintf(stderr, "\n");
|
| 357 |
-
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n", params.n_threads, std::thread::hardware_concurrency(), whisper_print_system_info());
|
| 358 |
-
}
|
| 359 |
-
|
| 360 |
-
// print some info about the processing
|
| 361 |
-
{
|
| 362 |
-
fprintf(stderr, "\n");
|
| 363 |
-
if (!whisper_is_multilingual(ctx)) {
|
| 364 |
-
if (params.language != "en" || params.translate) {
|
| 365 |
-
params.language = "en";
|
| 366 |
-
params.translate = false;
|
| 367 |
-
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
| 368 |
-
}
|
| 369 |
-
}
|
| 370 |
-
fprintf(stderr, "%s: processing '%s' (%d samples, %.1f sec), %d threads, %d processors, lang = %s, task = %s, timestamps = %d ...\n",
|
| 371 |
-
__func__, fname_inp.c_str(), int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE,
|
| 372 |
-
params.n_threads, params.n_processors,
|
| 373 |
-
params.language.c_str(),
|
| 374 |
-
params.translate ? "translate" : "transcribe",
|
| 375 |
-
params.no_timestamps ? 0 : 1);
|
| 376 |
-
|
| 377 |
-
fprintf(stderr, "\n");
|
| 378 |
-
}
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
// run the inference
|
| 382 |
-
{
|
| 383 |
-
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
| 384 |
-
|
| 385 |
-
wparams.print_realtime = false;
|
| 386 |
-
wparams.print_progress = false;
|
| 387 |
-
wparams.print_timestamps = !params.no_timestamps;
|
| 388 |
-
wparams.print_special_tokens = params.print_special_tokens;
|
| 389 |
-
wparams.translate = params.translate;
|
| 390 |
-
wparams.language = params.language.c_str();
|
| 391 |
-
wparams.n_threads = params.n_threads;
|
| 392 |
-
wparams.n_max_text_ctx = params.max_context >= 0 ? params.max_context : wparams.n_max_text_ctx;
|
| 393 |
-
wparams.offset_ms = params.offset_t_ms;
|
| 394 |
-
|
| 395 |
-
// this callback is called on each new segment
|
| 396 |
-
if (!wparams.print_realtime) {
|
| 397 |
-
wparams.new_segment_callback = whisper_print_segment_callback;
|
| 398 |
-
wparams.new_segment_callback_user_data = ¶ms;
|
| 399 |
-
}
|
| 400 |
-
|
| 401 |
-
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
| 402 |
-
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
|
| 403 |
-
return 8;
|
| 404 |
-
}
|
| 405 |
-
|
| 406 |
-
printf("\n");
|
| 407 |
-
|
| 408 |
-
// output to text file
|
| 409 |
-
if (params.output_txt) {
|
| 410 |
-
const auto fname_txt = fname_inp + ".txt";
|
| 411 |
-
output_txt(ctx, fname_txt.c_str());
|
| 412 |
-
}
|
| 413 |
-
|
| 414 |
-
// output to VTT file
|
| 415 |
-
if (params.output_vtt) {
|
| 416 |
-
const auto fname_vtt = fname_inp + ".vtt";
|
| 417 |
-
output_vtt(ctx, fname_vtt.c_str());
|
| 418 |
-
}
|
| 419 |
-
|
| 420 |
-
// output to SRT file
|
| 421 |
-
if (params.output_srt) {
|
| 422 |
-
const auto fname_srt = fname_inp + ".srt";
|
| 423 |
-
output_srt(ctx, fname_srt.c_str(), params);
|
| 424 |
-
}
|
| 425 |
-
}
|
| 426 |
-
}
|
| 427 |
-
|
| 428 |
-
whisper_print_timings(ctx);
|
| 429 |
-
whisper_free(ctx);
|
| 430 |
-
|
| 431 |
-
return 0;
|
| 432 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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