Skip to content

Commit

Permalink
move to_timestamp, timestamp_to_sample to rcpp_whisper again in order…
Browse files Browse the repository at this point in the history
… not to need common.cpp
  • Loading branch information
jwijffels committed Oct 6, 2024
1 parent 3c85dbc commit 4b3c01c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ SOURCES = whisper_cpp/ggml/src/ggml-quants.c whisper_cpp/ggml/src/ggml-backend.c
OBJECTS = whisper_cpp/ggml/src/ggml-quants.o whisper_cpp/ggml/src/ggml-backend.o whisper_cpp/ggml/src/ggml-alloc.o whisper_cpp/ggml/src/ggml-aarch64.o whisper_cpp/ggml/src/ggml.o

## utils from examples
SOURCES += whisper_cpp/examples/grammar-parser.cpp whisper_cpp/examples/common.cpp
OBJECTS += whisper_cpp/examples/grammar-parser.o whisper_cpp/examples/common.o
SOURCES += whisper_cpp/examples/grammar-parser.cpp
OBJECTS += whisper_cpp/examples/grammar-parser.o

## whisper source code + R wrapper
SOURCES += whisper_cpp/src/whisper.cpp rcpp_whisper_utils.cpp rcpp_whisper.cpp RcppExports.cpp
Expand Down
21 changes: 21 additions & 0 deletions src/rcpp_whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
#pragma warning(disable: 4244 4267) // possible loss of data
#endif

// 500 -> 00:05.000
// 6000 -> 01:00.000
std::string to_timestamp(int64_t t, bool comma) {
int64_t msec = t * 10;
int64_t hr = msec / (1000 * 60 * 60);
msec = msec - hr * (1000 * 60 * 60);
int64_t min = msec / (1000 * 60);
msec = msec - min * (1000 * 60);
int64_t sec = msec / 1000;
msec = msec - sec * 1000;

char buf[32];
snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec);

return std::string(buf);
}

int timestamp_to_sample(int64_t t, int n_samples, int whisper_sample_rate) {
return std::max(0, std::min((int) n_samples - 1, (int) ((t*whisper_sample_rate)/100)));
}

// command-line parameters
struct whisper_params {
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
Expand Down

0 comments on commit 4b3c01c

Please sign in to comment.