ggerganov commited on
Commit
b777dde
·
1 Parent(s): 58fce09

main : add option for word-leve timestamps (very experimental)

Browse files
Files changed (3) hide show
  1. examples/main/main.cpp +413 -0
  2. whisper.cpp +10 -2
  3. whisper.h +7 -2
examples/main/main.cpp CHANGED
@@ -36,6 +36,40 @@ std::string to_timestamp(int64_t t, bool comma = false) {
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
@@ -45,11 +79,14 @@ struct whisper_params {
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;
@@ -83,6 +120,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
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") {
@@ -100,6 +139,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
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") {
@@ -135,11 +176,13 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
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");
@@ -277,6 +320,367 @@ bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_
277
  return true;
278
  }
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  int main(int argc, char ** argv) {
281
  whisper_params params;
282
 
@@ -403,7 +807,10 @@ int main(int argc, char ** argv) {
403
  fprintf(stderr, "%s: failed to process audio\n", argv[0]);
404
  return 8;
405
  }
 
406
 
 
 
407
  printf("\n");
408
 
409
  // output to text file
@@ -423,6 +830,12 @@ int main(int argc, char ** argv) {
423
  const auto fname_srt = fname_inp + ".srt";
424
  output_srt(ctx, fname_srt.c_str(), params);
425
  }
 
 
 
 
 
 
426
  }
427
  }
428
 
 
36
  return std::string(buf);
37
  }
38
 
39
+ void replace_all(std::string & s, const std::string & search, const std::string & replace) {
40
+ for (size_t pos = 0; ; pos += replace.length()) {
41
+ pos = s.find(search, pos);
42
+ if (pos == std::string::npos) break;
43
+ s.erase(pos, search.length());
44
+ s.insert(pos, replace);
45
+ }
46
+ }
47
+
48
+ // a cost-function that is high for text that takes longer to pronounce
49
+ float voice_length(const std::string & text) {
50
+ float res = 0.0f;
51
+
52
+ for (size_t i = 0; i < text.size(); ++i) {
53
+ if (text[i] == ' ') {
54
+ res += 0.01f;
55
+ } else if (text[i] == ',') {
56
+ res += 2.00f;
57
+ } else if (text[i] == '.') {
58
+ res += 3.00f;
59
+ } else if (text[i] == '!') {
60
+ res += 3.00f;
61
+ } else if (text[i] == '?') {
62
+ res += 3.00f;
63
+ } else if (text[i] >= '0' && text[i] <= '9') {
64
+ res += 3.00f;
65
+ } else {
66
+ res += 1.00f;
67
+ }
68
+ }
69
+
70
+ return res;
71
+ }
72
+
73
  // command-line parameters
74
  struct whisper_params {
75
  int32_t seed = -1; // RNG seed, not used currently
 
79
  int32_t offset_n = 0;
80
  int32_t max_context = -1;
81
 
82
+ float word_thold = 0.01f;
83
+
84
  bool verbose = false;
85
  bool translate = false;
86
  bool output_txt = false;
87
  bool output_vtt = false;
88
  bool output_srt = false;
89
+ bool output_wts = false;
90
  bool print_special_tokens = false;
91
  bool print_colors = false;
92
  bool no_timestamps = false;
 
120
  params.offset_n = std::stoi(argv[++i]);
121
  } else if (arg == "-mc" || arg == "--max-context") {
122
  params.max_context = std::stoi(argv[++i]);
123
+ } else if (arg == "-wt" || arg == "--word-thold") {
124
+ params.word_thold = std::stof(argv[++i]);
125
  } else if (arg == "-v" || arg == "--verbose") {
126
  params.verbose = true;
127
  } else if (arg == "--translate") {
 
139
  params.output_vtt = true;
140
  } else if (arg == "-osrt" || arg == "--output-srt") {
141
  params.output_srt = true;
142
+ } else if (arg == "-owts" || arg == "--output-words") {
143
+ params.output_wts = true;
144
  } else if (arg == "-ps" || arg == "--print_special") {
145
  params.print_special_tokens = true;
146
  } else if (arg == "-pc" || arg == "--print_colors") {
 
176
  fprintf(stderr, " -ot N, --offset-t N time offset in milliseconds (default: %d)\n", params.offset_t_ms);
177
  fprintf(stderr, " -on N, --offset-n N segment index offset (default: %d)\n", params.offset_n);
178
  fprintf(stderr, " -mc N, --max-context N maximum number of text context tokens to store (default: max)\n");
179
+ fprintf(stderr, " -wt N, --word-thold N word timestamp probability threshold (default: %f)\n", params.word_thold);
180
  fprintf(stderr, " -v, --verbose verbose output\n");
181
  fprintf(stderr, " --translate translate from source language to english\n");
182
  fprintf(stderr, " -otxt, --output-txt output result in a text file\n");
183
  fprintf(stderr, " -ovtt, --output-vtt output result in a vtt file\n");
184
  fprintf(stderr, " -osrt, --output-srt output result in a srt file\n");
185
+ fprintf(stderr, " -owts, --output-words output word-level timestamps to a text file\n");
186
  fprintf(stderr, " -ps, --print_special print special tokens\n");
187
  fprintf(stderr, " -pc, --print_colors print colors\n");
188
  fprintf(stderr, " -nt, --no_timestamps do not print timestamps\n");
 
320
  return true;
321
  }
322
 
323
+ // word-level timestamps (experimental)
324
+ // TODO: probably still has bugs, needs refactoring, etc..
325
+ // TODO: auto threshold
326
+ // TODO: extra pass to detect unused speech and assign to tokens
327
+ // TODO: font parameter adjustments
328
+ bool output_wts(struct whisper_context * ctx, const char * fname, const char * fname_inp, const whisper_params & params, const std::vector<float> & pcmf32) {
329
+ if (params.output_wts) {
330
+ std::vector<float> pcm_avg(pcmf32.size(), 0);
331
+
332
+ // average the fabs of the signal
333
+ {
334
+ const int hw = 32;
335
+
336
+ for (int i = 0; i < pcmf32.size(); i++) {
337
+ float sum = 0;
338
+ for (int j = -hw; j <= hw; j++) {
339
+ if (i + j >= 0 && i + j < pcmf32.size()) {
340
+ sum += fabs(pcmf32[i + j]);
341
+ }
342
+ }
343
+ pcm_avg[i] = sum/(2*hw + 1);
344
+ }
345
+ }
346
+
347
+ struct token_info {
348
+ int64_t t0 = -1;
349
+ int64_t t1 = -1;
350
+
351
+ int64_t tt0 = -1;
352
+ int64_t tt1 = -1;
353
+
354
+ whisper_token id;
355
+ whisper_token tid;
356
+
357
+ float p = 0.0f;
358
+ float pt = 0.0f;
359
+ float ptsum = 0.0f;
360
+
361
+ std::string text;
362
+ float vlen = 0.0f; // voice length of this token
363
+ };
364
+
365
+ int64_t t_beg = 0;
366
+ int64_t t_last = 0;
367
+
368
+ whisper_token tid_last = 0;
369
+
370
+ std::ofstream fout(fname);
371
+
372
+ fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
373
+
374
+ fout << "!/bin/bash" << "\n";
375
+ fout << "\n";
376
+
377
+ fout << "ffmpeg -i " << fname_inp << " -f lavfi -i color=size=1200x120:duration=" << float(pcmf32.size() + 1000)/WHISPER_SAMPLE_RATE << ":rate=25:color=black -vf \"";
378
+
379
+ bool is_first = true;
380
+
381
+ for (int i = 0; i < whisper_full_n_segments(ctx); i++) {
382
+ const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
383
+ const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
384
+
385
+ const char *text = whisper_full_get_segment_text(ctx, i);
386
+
387
+ const int s0 = std::max(0, (int) (t0*WHISPER_SAMPLE_RATE/100));
388
+ const int s1 = std::min((int) pcmf32.size(), (int) (t1*WHISPER_SAMPLE_RATE/100));
389
+
390
+ const int n = whisper_full_n_tokens(ctx, i);
391
+
392
+ std::vector<token_info> tokens(n);
393
+
394
+ if (n <= 1) {
395
+ continue;
396
+ }
397
+
398
+ for (int j = 0; j < n; ++j) {
399
+ struct whisper_token_data token = whisper_full_get_token_data(ctx, i, j);
400
+
401
+ if (j == 0) {
402
+ if (token.id == whisper_token_beg(ctx)) {
403
+ tokens[j ].t0 = t0;
404
+ tokens[j ].t1 = t0;
405
+ tokens[j + 1].t0 = t0;
406
+
407
+ t_beg = t0;
408
+ t_last = t0;
409
+ tid_last = whisper_token_beg(ctx);
410
+ } else {
411
+ tokens[j ].t0 = t_last;
412
+ }
413
+ }
414
+
415
+ const int64_t tt = t_beg + 2*(token.tid - whisper_token_beg(ctx));
416
+
417
+ tokens[j].id = token.id;
418
+ tokens[j].tid = token.tid;
419
+ tokens[j].p = token.p;
420
+ tokens[j].pt = token.pt;
421
+ tokens[j].ptsum = token.ptsum;
422
+
423
+ tokens[j].text = whisper_token_to_str(ctx, token.id);
424
+ //tokens[j].vlen = tokens[j].pt;
425
+ tokens[j].vlen = voice_length(tokens[j].text);
426
+
427
+ if (token.pt > params.word_thold && token.ptsum > 0.01 && token.tid > tid_last) {
428
+ if (j > 0) {
429
+ tokens[j - 1].t1 = tt;
430
+ }
431
+ tokens[j].t0 = tt;
432
+ tid_last = token.tid;
433
+ }
434
+ }
435
+
436
+ tokens[n - 2].t1 = t1;
437
+ tokens[n - 1].t0 = t1;
438
+ tokens[n - 1].t1 = t1;
439
+
440
+ t_last = t1;
441
+
442
+ int p0 = 0;
443
+ int p1 = 0;
444
+ while (true) {
445
+ while (p1 < n && tokens[p1].t1 < 0) {
446
+ p1++;
447
+ }
448
+
449
+ if (p1 >= n) {
450
+ p1--;
451
+ }
452
+
453
+ if (p1 > p0) {
454
+ double psum = 0.0;
455
+ for (int j = p0; j <= p1; j++) {
456
+ psum += tokens[j].vlen;
457
+ }
458
+
459
+ //printf("analyzing %d - %d, psum = %f\n", p0, p1, psum);
460
+
461
+ const double dt = tokens[p1].t1 - tokens[p0].t0;
462
+
463
+ for (int j = p0 + 1; j <= p1; j++) {
464
+ const double ct = tokens[j - 1].t0 + dt*tokens[j - 1].vlen/psum;
465
+ //const double ct = tokens[j - 1].t0 + (dt*(j - p0))/(p1 - p0 + 1);
466
+ //const double ct = tokens[p0].t0 + (dt*(j - p0))/(p1 - p0 + 1);
467
+
468
+ tokens[j - 1].t1 = ct;
469
+ tokens[j ].t0 = ct;
470
+ }
471
+ }
472
+
473
+ p1++;
474
+ p0 = p1;
475
+ if (p1 >= n) {
476
+ break;
477
+ }
478
+ }
479
+
480
+ for (int j = 0; j < n - 1; j++) {
481
+ if (tokens[j].t1 < 0) {
482
+ tokens[j + 1].t0 = tokens[j].t1;
483
+ }
484
+
485
+ tokens[j].tt0 = tokens[j].t0;
486
+ tokens[j].tt1 = tokens[j].t1;
487
+ }
488
+
489
+ // VAD
490
+ {
491
+ const int hw = WHISPER_SAMPLE_RATE; // take one second of audio around the token
492
+
493
+ for (int j = 0; j < n; j++) {
494
+ const int64_t t0 = tokens[j].t0;
495
+ const int64_t t1 = tokens[j].t1;
496
+
497
+ int s0 = std::max(0, (int) (t0*WHISPER_SAMPLE_RATE/100));
498
+ int s1 = std::min((int) pcmf32.size() - 1, (int) (t1*WHISPER_SAMPLE_RATE/100));
499
+
500
+ const int ss0 = std::max(0, (int) (t0*WHISPER_SAMPLE_RATE/100) - hw);
501
+ const int ss1 = std::min((int) pcmf32.size() - 1, (int) (t1*WHISPER_SAMPLE_RATE/100) + hw);
502
+
503
+ const int n = ss1 - ss0;
504
+
505
+ float sum = 0.0f;
506
+ for (int k = ss0; k < ss1; k++) {
507
+ sum += pcm_avg[k];
508
+ }
509
+
510
+ const float avg = sum/n;
511
+
512
+ const float thold = 0.5*avg;
513
+
514
+ {
515
+ int k = s0;
516
+ if (pcm_avg[k] > thold && j > 0) {
517
+ while (k > 0 && pcm_avg[k] > thold) {
518
+ k--;
519
+ }
520
+ tokens[j].t0 = (int64_t) (100*k/WHISPER_SAMPLE_RATE);
521
+ if (tokens[j].t0 < tokens[j - 1].t1) {
522
+ tokens[j].t0 = tokens[j - 1].t1;
523
+ } else {
524
+ s0 = k;
525
+ }
526
+ } else {
527
+ while (pcm_avg[k] < thold && k < s1) {
528
+ k++;
529
+ }
530
+ s0 = k;
531
+ tokens[j].t0 = 100*k/WHISPER_SAMPLE_RATE;
532
+ }
533
+ }
534
+
535
+ {
536
+ int k = s1;
537
+ if (pcm_avg[k] > thold) {
538
+ while (k < (int) pcmf32.size() - 1 && pcm_avg[k] > thold) {
539
+ k++;
540
+ }
541
+ tokens[j].t1 = 100*k/WHISPER_SAMPLE_RATE;
542
+ if (j < n - 1 && tokens[j].t1 > tokens[j + 1].t0) {
543
+ tokens[j].t1 = tokens[j + 1].t0;
544
+ } else {
545
+ s1 = k;
546
+ }
547
+ } else {
548
+ while (pcm_avg[k] < thold && k > s0) {
549
+ k--;
550
+ }
551
+ s1 = k;
552
+ tokens[j].t1 = 100*k/WHISPER_SAMPLE_RATE;
553
+ }
554
+ }
555
+ }
556
+ }
557
+
558
+ const int t_expand = 0;
559
+
560
+ for (int j = 0; j < n; j++) {
561
+ if (j > 0) {
562
+ tokens[j].t0 = std::max(0, (int) (tokens[j].t0 - t_expand));
563
+ }
564
+ if (j < n - 1) {
565
+ tokens[j].t1 = tokens[j].t1 + t_expand;
566
+ }
567
+ }
568
+
569
+ for (int j = 0; j < n; ++j) {
570
+ const auto & token = tokens[j];
571
+ const auto tt = token.pt > params.word_thold && token.ptsum > 0.01 ? whisper_token_to_str(ctx, token.tid) : "[?]";
572
+ printf("%s: %10s %6.3f %6.3f %6.3f %6.3f %5d %5d '%s'\n", __func__,
573
+ tt, token.p, token.pt, token.ptsum, token.vlen, (int) token.t0, (int) token.t1, token.text.c_str());
574
+
575
+ if (tokens[j].id >= whisper_token_eot(ctx)) {
576
+ continue;
577
+ }
578
+
579
+ //printf("[%s --> %s] %s\n", to_timestamp(token.t0).c_str(), to_timestamp(token.t1).c_str(), whisper_token_to_str(ctx, token.id));
580
+
581
+ //fout << "# " << to_timestamp(token.t0) << " --> " << to_timestamp(token.t1) << " " << whisper_token_to_str(ctx, token.id) << "\n";
582
+ }
583
+
584
+ static const int line_wrap = 60;
585
+ static const char * font = "/System/Library/Fonts/Supplemental/Courier New Bold.ttf";
586
+
587
+ if (!is_first) {
588
+ fout << ",";
589
+ }
590
+
591
+ // background text
592
+ fout << "drawtext=fontfile='" << font << "':fontsize=24:fontcolor=gray:x=(w-text_w)/2:y=h/2:text='':enable='between(t," << t0/100.0 << "," << t0/100.0 << ")'";
593
+
594
+ is_first = false;
595
+
596
+ for (int j = 0; j < n; ++j) {
597
+ const auto & token = tokens[j];
598
+
599
+ if (tokens[j].id >= whisper_token_eot(ctx)) {
600
+ continue;
601
+ }
602
+
603
+ std::string txt_bg;
604
+ std::string txt_fg; // highlight token
605
+ std::string txt_ul; // underline
606
+
607
+ txt_bg = "> ";
608
+ txt_fg = "> ";
609
+ txt_ul = "\\ \\ ";
610
+
611
+ {
612
+ int ncnt = 0;
613
+ for (int k = 0; k < n; ++k) {
614
+ const auto & token2 = tokens[k];
615
+
616
+ if (tokens[k].id >= whisper_token_eot(ctx)) {
617
+ continue;
618
+ }
619
+
620
+ const std::string txt = whisper_token_to_str(ctx, token2.id);
621
+
622
+ txt_bg += txt;
623
+
624
+ if (k == j) {
625
+ for (int l = 0; l < (int) txt.size(); ++l) {
626
+ txt_fg += txt[l];
627
+ txt_ul += "_";
628
+ }
629
+ txt_fg += "|";
630
+ } else {
631
+ for (int l = 0; l < (int) txt.size(); ++l) {
632
+ txt_fg += "\\ ";
633
+ txt_ul += "\\ ";
634
+ }
635
+ }
636
+
637
+ ncnt += txt.size();
638
+
639
+ if (ncnt > line_wrap) {
640
+ if (k < j) {
641
+ txt_bg = "> ";
642
+ txt_fg = "> ";
643
+ txt_ul = "\\ \\ ";
644
+ ncnt = 0;
645
+ } else {
646
+ break;
647
+ }
648
+ }
649
+ }
650
+
651
+ ::replace_all(txt_bg, "'", "’");
652
+ ::replace_all(txt_bg, "\"", "\\\"");
653
+ ::replace_all(txt_fg, "'", "’");
654
+ ::replace_all(txt_fg, "\"", "\\\"");
655
+ }
656
+
657
+ // background text
658
+ fout << ",drawtext=fontfile='" << font << "':fontsize=24:fontcolor=gray:x=(w-text_w)/2:y=h/2:text='" << txt_bg << "':enable='between(t," << token.tt0/100.0 << "," << token.tt1/100.0 << ")'";
659
+
660
+ // foreground text
661
+ fout << ",drawtext=fontfile='" << font << "':fontsize=24:fontcolor=lightgreen:x=(w-text_w)/2+8:y=h/2:text='" << txt_fg << "':enable='between(t," << token.t0/100.0 << "," << token.t1/100.0 << ")'";
662
+
663
+ // underline
664
+ fout << ",drawtext=fontfile='" << font << "':fontsize=24:fontcolor=lightgreen:x=(w-text_w)/2+8:y=h/2+16:text='" << txt_ul << "':enable='between(t," << token.t0/100.0 << "," << token.t1/100.0 << ")'";
665
+ }
666
+ }
667
+
668
+ fout << "\" -c:v libx264 -pix_fmt yuv420p -y " << fname_inp << ".mp4" << "\n";
669
+
670
+ fout << "\n\n";
671
+ fout << "echo \"Your video has been saved to " << fname_inp << ".mp4\"" << "\n";
672
+ fout << "\n";
673
+ fout << "echo \" ffplay " << fname_inp << ".mp4\"\n";
674
+ fout << "\n";
675
+
676
+ fout.close();
677
+
678
+ fprintf(stderr, "%s: run 'source %s' to generate karaoke video\n", __func__, fname);
679
+ }
680
+
681
+ return true;
682
+ }
683
+
684
  int main(int argc, char ** argv) {
685
  whisper_params params;
686
 
 
807
  fprintf(stderr, "%s: failed to process audio\n", argv[0]);
808
  return 8;
809
  }
810
+ }
811
 
812
+ // output stuff
813
+ {
814
  printf("\n");
815
 
816
  // output to text file
 
830
  const auto fname_srt = fname_inp + ".srt";
831
  output_srt(ctx, fname_srt.c_str(), params);
832
  }
833
+
834
+ // output to WTS file
835
+ if (params.output_wts) {
836
+ const auto fname_wts = fname_inp + ".wts";
837
+ output_wts(ctx, fname_wts.c_str(), fname_inp.c_str(), params, pcmf32);
838
+ }
839
  }
840
  }
841
 
whisper.cpp CHANGED
@@ -1843,7 +1843,8 @@ whisper_token_data whisper_sample_best(
1843
  }
1844
  }
1845
 
1846
- result.pt = max_ts/(sum_ts + 1e-6);
 
1847
  }
1848
 
1849
  // find the top K tokens
@@ -2518,7 +2519,10 @@ int whisper_full(
2518
  prompt.push_back(token.id);
2519
  tokens_cur.push_back(token);
2520
 
2521
- //printf("%s: %s\n", __func__, ctx->vocab.id_to_token[id].c_str());
 
 
 
2522
 
2523
  // end of text token
2524
  if (token.id == whisper_token_eot(ctx)) {
@@ -2803,6 +2807,10 @@ whisper_token whisper_full_get_token_id(struct whisper_context * ctx, int i_segm
2803
  return ctx->result_all[i_segment].tokens[i_token].id;
2804
  }
2805
 
 
 
 
 
2806
  float whisper_full_get_token_p(struct whisper_context * ctx, int i_segment, int i_token) {
2807
  return ctx->result_all[i_segment].tokens[i_token].p;
2808
  }
 
1843
  }
1844
  }
1845
 
1846
+ result.pt = max_ts/(sum_ts + 1e-10);
1847
+ result.ptsum = sum_ts;
1848
  }
1849
 
1850
  // find the top K tokens
 
2519
  prompt.push_back(token.id);
2520
  tokens_cur.push_back(token);
2521
 
2522
+ //{
2523
+ // const auto tt = token.pt > 0.10 ? ctx->vocab.id_to_token[token.tid] : "[?]";
2524
+ // printf("%s: %10s %6.3f '%s'\n", __func__, tt.c_str(), token.pt, ctx->vocab.id_to_token[token.id].c_str());
2525
+ //}
2526
 
2527
  // end of text token
2528
  if (token.id == whisper_token_eot(ctx)) {
 
2807
  return ctx->result_all[i_segment].tokens[i_token].id;
2808
  }
2809
 
2810
+ struct whisper_token_data whisper_full_get_token_data(struct whisper_context * ctx, int i_segment, int i_token) {
2811
+ return ctx->result_all[i_segment].tokens[i_token];
2812
+ }
2813
+
2814
  float whisper_full_get_token_p(struct whisper_context * ctx, int i_segment, int i_token) {
2815
  return ctx->result_all[i_segment].tokens[i_token].p;
2816
  }
whisper.h CHANGED
@@ -72,8 +72,9 @@ extern "C" {
72
  whisper_token id; // token id
73
  whisper_token tid; // forced timestamp token id
74
 
75
- float p; // probability of the token
76
- float pt; // probability of the timestamp token
 
77
  };
78
 
79
  // Allocates all memory needed for the model and loads the model from the given file.
@@ -241,6 +242,10 @@ extern "C" {
241
  WHISPER_API const char * whisper_full_get_token_text(struct whisper_context * ctx, int i_segment, int i_token);
242
  WHISPER_API whisper_token whisper_full_get_token_id (struct whisper_context * ctx, int i_segment, int i_token);
243
 
 
 
 
 
244
  // Get the probability of the specified token in the specified segment.
245
  WHISPER_API float whisper_full_get_token_p(struct whisper_context * ctx, int i_segment, int i_token);
246
 
 
72
  whisper_token id; // token id
73
  whisper_token tid; // forced timestamp token id
74
 
75
+ float p; // probability of the token
76
+ float pt; // probability of the timestamp token
77
+ float ptsum; // sum of probabilities of all timestamp tokens
78
  };
79
 
80
  // Allocates all memory needed for the model and loads the model from the given file.
 
242
  WHISPER_API const char * whisper_full_get_token_text(struct whisper_context * ctx, int i_segment, int i_token);
243
  WHISPER_API whisper_token whisper_full_get_token_id (struct whisper_context * ctx, int i_segment, int i_token);
244
 
245
+ // Get token data for the specified token in the specified segment.
246
+ // This contains probabilities, timestamps, etc.
247
+ WHISPER_API struct whisper_token_data whisper_full_get_token_data(struct whisper_context * ctx, int i_segment, int i_token);
248
+
249
  // Get the probability of the specified token in the specified segment.
250
  WHISPER_API float whisper_full_get_token_p(struct whisper_context * ctx, int i_segment, int i_token);
251