Skip to content

Commit b8ce25d

Browse files
committed
refactoring : more readable code
1 parent fd11368 commit b8ce25d

11 files changed

Lines changed: 280 additions & 330 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bench
1717
sync.sh
1818
compile_commands.json
1919

20+
examples/arm_neon.h
2021
examples/whisper.objc/whisper.objc.xcodeproj/xcshareddata
2122
examples/whisper.objc/whisper.objc.xcodeproj/xcuserdata/
2223
examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcuserdata

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ usage: ./main [options] file0.wav file1.wav ...
9999

100100
options:
101101
-h, --help show this help message and exit
102-
-s SEED, --seed SEED RNG seed (default: -1)
103102
-t N, --threads N number of threads to use during computation (default: 4)
104103
-p N, --processors N number of processors to use during computation (default: 1)
105104
-ot N, --offset-t N time offset in milliseconds (default: 0)

bindings/javascript/emscripten.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ EMSCRIPTEN_BINDINGS(whisper) {
4646

4747
struct whisper_full_params params = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);
4848

49-
params.print_realtime = true;
50-
params.print_progress = false;
51-
params.print_timestamps = true;
52-
params.print_special_tokens = false;
53-
params.translate = translate;
54-
params.language = whisper_is_multilingual(g_contexts[index]) ? lang.c_str() : "en";
55-
params.n_threads = std::min(8, (int) std::thread::hardware_concurrency());
56-
params.offset_ms = 0;
49+
params.print_realtime = true;
50+
params.print_progress = false;
51+
params.print_timestamps = true;
52+
params.print_special = false;
53+
params.translate = translate;
54+
params.language = whisper_is_multilingual(g_contexts[index]) ? lang.c_str() : "en";
55+
params.n_threads = std::min(8, (int) std::thread::hardware_concurrency());
56+
params.offset_ms = 0;
5757

5858
std::vector<float> pcmf32;
5959
const int n = audio["length"].as<int>();

examples/bench/bench.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
// command-line parameters
88
struct whisper_params {
9-
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
9+
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
1010

11-
std::string model = "models/ggml-base.en.bin";
11+
std::string model = "models/ggml-base.en.bin";
1212
};
1313

1414
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
@@ -17,14 +17,13 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
1717
for (int i = 1; i < argc; i++) {
1818
std::string arg = argv[i];
1919

20-
if (arg == "-t" || arg == "--threads") {
21-
params.n_threads = std::stoi(argv[++i]);
22-
} else if (arg == "-m" || arg == "--model") {
23-
params.model = argv[++i];
24-
} else if (arg == "-h" || arg == "--help") {
20+
if (arg == "-h" || arg == "--help") {
2521
whisper_print_usage(argc, argv, params);
2622
exit(0);
27-
} else {
23+
}
24+
else if (arg == "-t" || arg == "--threads") { params.n_threads = std::stoi(argv[++i]); }
25+
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
26+
else {
2827
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
2928
whisper_print_usage(argc, argv, params);
3029
exit(0);
@@ -39,9 +38,9 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
3938
fprintf(stderr, "usage: %s [options]\n", argv[0]);
4039
fprintf(stderr, "\n");
4140
fprintf(stderr, "options:\n");
42-
fprintf(stderr, " -h, --help show this help message and exit\n");
43-
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
44-
fprintf(stderr, " -m FNAME, --model FNAME model path (default: %s)\n", params.model.c_str());
41+
fprintf(stderr, " -h, --help [default] show this help message and exit\n");
42+
fprintf(stderr, " -t N, --threads N [%-7d] number of threads to use during computation\n", params.n_threads);
43+
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
4544
fprintf(stderr, "\n");
4645
}
4746

examples/main/main.cpp

Lines changed: 80 additions & 104 deletions
Large diffs are not rendered by default.

examples/stream/stream.cpp

Lines changed: 108 additions & 134 deletions
Large diffs are not rendered by default.

examples/talk.wasm/emscripten.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ void talk_main(size_t index) {
5151

5252
struct whisper_full_params wparams = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);
5353

54-
wparams.n_threads = std::min(N_THREAD, (int) std::thread::hardware_concurrency());
55-
wparams.offset_ms = 0;
56-
wparams.translate = false;
57-
wparams.no_context = true;
58-
wparams.single_segment = true;
59-
wparams.print_realtime = false;
60-
wparams.print_progress = false;
61-
wparams.print_timestamps = true;
62-
wparams.print_special_tokens = false;
54+
wparams.n_threads = std::min(N_THREAD, (int) std::thread::hardware_concurrency());
55+
wparams.offset_ms = 0;
56+
wparams.translate = false;
57+
wparams.no_context = true;
58+
wparams.single_segment = true;
59+
wparams.print_realtime = false;
60+
wparams.print_progress = false;
61+
wparams.print_timestamps = true;
62+
wparams.print_special = false;
6363

6464
wparams.max_tokens = 32;
6565
wparams.audio_ctx = 768; // partial encoder context for better performance
@@ -75,9 +75,9 @@ void talk_main(size_t index) {
7575
// whisper context
7676
auto & ctx = g_contexts[index];
7777

78-
const int64_t step_samples = 2*WHISPER_SAMPLE_RATE;
79-
const int64_t step_ms = (step_samples*1000)/WHISPER_SAMPLE_RATE;
78+
const int64_t step_samples = 2*WHISPER_SAMPLE_RATE;
8079
const int64_t window_samples = 9*WHISPER_SAMPLE_RATE;
80+
const int64_t step_ms = (step_samples*1000)/WHISPER_SAMPLE_RATE;
8181

8282
auto t_last = std::chrono::high_resolution_clock::now();
8383

@@ -111,7 +111,7 @@ void talk_main(size_t index) {
111111
pcmf32 = std::vector<float>(g_pcmf32.end() - std::min((int64_t) g_pcmf32.size(), window_samples), g_pcmf32.end());
112112
}
113113

114-
// if energy in during last second is above threshold, then skip
114+
// VAD: if energy in during last second is above threshold, then skip
115115
{
116116
float energy_all = 0.0f;
117117
float energy_1s = 0.0f;

examples/whisper.objc/whisper.objc/ViewController.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ - (IBAction)onTranscribe:(id)sender {
161161
// run the model
162162
struct whisper_full_params params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
163163

164-
params.print_realtime = true;
165-
params.print_progress = false;
166-
params.print_timestamps = true;
167-
params.print_special_tokens = false;
168-
params.translate = false;
169-
params.language = "en";
170-
params.n_threads = 4;
171-
params.offset_ms = 0;
164+
params.print_realtime = true;
165+
params.print_progress = false;
166+
params.print_timestamps = true;
167+
params.print_special = false;
168+
params.translate = false;
169+
params.language = "en";
170+
params.n_threads = 4;
171+
params.offset_ms = 0;
172172

173173
CFTimeInterval startTime = CACurrentMediaTime();
174174

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(TARGET whisper.wasm)
22

33
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/index-tmpl.html ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/index.html @ONLY)
4+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../helpers.js ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/helpers.js @ONLY)
45
configure_file(${CMAKE_SOURCE_DIR}/bindings/javascript/whisper.js ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/whisper.js COPYONLY)

whisper.cpp

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,92 +2389,92 @@ struct whisper_full_params whisper_full_default_params(enum whisper_sampling_str
23892389
case WHISPER_SAMPLING_GREEDY:
23902390
{
23912391
result = {
2392-
/*.strategy =*/ WHISPER_SAMPLING_GREEDY,
2392+
/*.strategy =*/ WHISPER_SAMPLING_GREEDY,
23932393

2394-
/*.n_threads =*/ std::min(4, (int32_t) std::thread::hardware_concurrency()),
2395-
/*.n_max_text_ctx =*/ 16384,
2396-
/*.offset_ms =*/ 0,
2397-
/*.duration_ms =*/ 0,
2394+
/*.n_threads =*/ std::min(4, (int32_t) std::thread::hardware_concurrency()),
2395+
/*.n_max_text_ctx =*/ 16384,
2396+
/*.offset_ms =*/ 0,
2397+
/*.duration_ms =*/ 0,
23982398

2399-
/*.translate =*/ false,
2400-
/*.no_context =*/ false,
2401-
/*.single_segment =*/ false,
2402-
/*.print_special_tokens =*/ false,
2403-
/*.print_progress =*/ true,
2404-
/*.print_realtime =*/ false,
2405-
/*.print_timestamps =*/ true,
2399+
/*.translate =*/ false,
2400+
/*.no_context =*/ false,
2401+
/*.single_segment =*/ false,
2402+
/*.print_special =*/ false,
2403+
/*.print_progress =*/ true,
2404+
/*.print_realtime =*/ false,
2405+
/*.print_timestamps =*/ true,
24062406

2407-
/*.token_timestamps =*/ false,
2408-
/*.thold_pt =*/ 0.01f,
2409-
/*.thold_ptsum =*/ 0.01f,
2410-
/*.max_len =*/ 0,
2411-
/*.max_tokens =*/ 0,
2407+
/*.token_timestamps =*/ false,
2408+
/*.thold_pt =*/ 0.01f,
2409+
/*.thold_ptsum =*/ 0.01f,
2410+
/*.max_len =*/ 0,
2411+
/*.max_tokens =*/ 0,
24122412

2413-
/*.speed_up =*/ false,
2414-
/*.audio_ctx =*/ 0,
2413+
/*.speed_up =*/ false,
2414+
/*.audio_ctx =*/ 0,
24152415

2416-
/*.prompt_tokens =*/ nullptr,
2417-
/*.prompt_n_tokens =*/ 0,
2416+
/*.prompt_tokens =*/ nullptr,
2417+
/*.prompt_n_tokens =*/ 0,
24182418

2419-
/*.language =*/ "en",
2419+
/*.language =*/ "en",
24202420

2421-
/*.greedy =*/ {
2421+
/*.greedy =*/ {
24222422
/*.n_past =*/ 0,
24232423
},
24242424

2425-
/*.beam_search =*/ {
2425+
/*.beam_search =*/ {
24262426
/*.n_past =*/ -1,
24272427
/*.beam_width =*/ -1,
24282428
/*.n_best =*/ -1,
24292429
},
24302430

2431-
/*.new_segment_callback =*/ nullptr,
2431+
/*.new_segment_callback =*/ nullptr,
24322432
/*.new_segment_callback_user_data =*/ nullptr,
24332433
};
24342434
} break;
24352435
case WHISPER_SAMPLING_BEAM_SEARCH:
24362436
{
24372437
result = {
2438-
/*.strategy =*/ WHISPER_SAMPLING_BEAM_SEARCH,
2438+
/*.strategy =*/ WHISPER_SAMPLING_BEAM_SEARCH,
24392439

2440-
/*.n_threads =*/ std::min(4, (int32_t) std::thread::hardware_concurrency()),
2441-
/*.n_max_text_ctx =*/ 16384,
2442-
/*.offset_ms =*/ 0,
2443-
/*.duration_ms =*/ 0,
2440+
/*.n_threads =*/ std::min(4, (int32_t) std::thread::hardware_concurrency()),
2441+
/*.n_max_text_ctx =*/ 16384,
2442+
/*.offset_ms =*/ 0,
2443+
/*.duration_ms =*/ 0,
24442444

2445-
/*.translate =*/ false,
2446-
/*.no_context =*/ false,
2447-
/*.single_segment =*/ false,
2448-
/*.print_special_tokens =*/ false,
2449-
/*.print_progress =*/ true,
2450-
/*.print_realtime =*/ false,
2451-
/*.print_timestamps =*/ true,
2445+
/*.translate =*/ false,
2446+
/*.no_context =*/ false,
2447+
/*.single_segment =*/ false,
2448+
/*.print_special =*/ false,
2449+
/*.print_progress =*/ true,
2450+
/*.print_realtime =*/ false,
2451+
/*.print_timestamps =*/ true,
24522452

2453-
/*.token_timestamps =*/ false,
2454-
/*.thold_pt =*/ 0.01f,
2455-
/*.thold_ptsum =*/ 0.01f,
2456-
/*.max_len =*/ 0,
2457-
/*.max_tokens =*/ 0,
2453+
/*.token_timestamps =*/ false,
2454+
/*.thold_pt =*/ 0.01f,
2455+
/*.thold_ptsum =*/ 0.01f,
2456+
/*.max_len =*/ 0,
2457+
/*.max_tokens =*/ 0,
24582458

2459-
/*.speed_up =*/ false,
2460-
/*.audio_ctx =*/ 0,
2459+
/*.speed_up =*/ false,
2460+
/*.audio_ctx =*/ 0,
24612461

2462-
/*.prompt_tokens =*/ nullptr,
2463-
/*.prompt_n_tokens =*/ 0,
2462+
/*.prompt_tokens =*/ nullptr,
2463+
/*.prompt_n_tokens =*/ 0,
24642464

2465-
/*.language =*/ "en",
2465+
/*.language =*/ "en",
24662466

2467-
/*.greedy =*/ {
2467+
/*.greedy =*/ {
24682468
/*.n_past =*/ -1,
24692469
},
24702470

2471-
/*.beam_search =*/ {
2471+
/*.beam_search =*/ {
24722472
/*.n_past =*/ 0,
24732473
/*.beam_width =*/ 10,
24742474
/*.n_best =*/ 5,
24752475
},
24762476

2477-
/*.new_segment_callback =*/ nullptr,
2477+
/*.new_segment_callback =*/ nullptr,
24782478
/*.new_segment_callback_user_data =*/ nullptr,
24792479
};
24802480
} break;
@@ -2762,7 +2762,7 @@ int whisper_full(
27622762
// ctx->vocab.id_to_token[tokens_cur[i].id].c_str(), tokens_cur[i].p,
27632763
// ctx->vocab.id_to_token[tokens_cur[i].tid].c_str(), tokens_cur[i].pt);
27642764

2765-
if (params.print_special_tokens == false && tokens_cur[i].id >= whisper_token_eot(ctx)) {
2765+
if (params.print_special == false && tokens_cur[i].id >= whisper_token_eot(ctx)) {
27662766
} else {
27672767
text += whisper_token_to_str(ctx, tokens_cur[i].id);
27682768
}

0 commit comments

Comments
 (0)