| 1 | #ifndef WHISPER_H |
| 2 | #define WHISPER_H |
| 3 | |
| 4 | #include "ggml.h" |
| 5 | #include "ggml-cpu.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <stdbool.h> |
| 10 | |
| 11 | #ifdef __GNUC__ |
| 12 | # define WHISPER_DEPRECATED(func, hint) func __attribute__((deprecated(hint))) |
| 13 | #elif defined(_MSC_VER) |
| 14 | # define WHISPER_DEPRECATED(func, hint) __declspec(deprecated(hint)) func |
| 15 | #else |
| 16 | # define WHISPER_DEPRECATED(func, hint) func |
| 17 | #endif |
| 18 | |
| 19 | #ifdef WHISPER_SHARED |
| 20 | # ifdef _WIN32 |
| 21 | # ifdef WHISPER_BUILD |
| 22 | # define WHISPER_API __declspec(dllexport) |
| 23 | # else |
| 24 | # define WHISPER_API __declspec(dllimport) |
| 25 | # endif |
| 26 | # else |
| 27 | # define WHISPER_API __attribute__ ((visibility ("default"))) |
| 28 | # endif |
| 29 | #else |
| 30 | # define WHISPER_API |
| 31 | #endif |
| 32 | |
| 33 | #define WHISPER_SAMPLE_RATE 16000 |
| 34 | #define WHISPER_N_FFT 400 |
| 35 | #define WHISPER_HOP_LENGTH 160 |
| 36 | #define WHISPER_CHUNK_SIZE 30 |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | // |
| 43 | // C interface |
| 44 | // |
| 45 | // The following interface is thread-safe as long as the sample whisper_context is not used by multiple threads |
| 46 | // concurrently. |
| 47 | // |
| 48 | // Basic usage: |
| 49 | // |
| 50 | // #include "whisper.h" |
| 51 | // |
| 52 | // ... |
| 53 | // |
| 54 | // whisper_context_params cparams = whisper_context_default_params(); |
| 55 | // |
| 56 | // struct whisper_context * ctx = whisper_init_from_file_with_params("/path/to/ggml-base.en.bin", cparams); |
| 57 | // |
| 58 | // if (whisper_full(ctx, wparams, pcmf32.data(), pcmf32.size()) != 0) { |
| 59 | // fprintf(stderr, "failed to process audio\n"); |
| 60 | // return 7; |
| 61 | // } |
| 62 | // |
| 63 | // const int n_segments = whisper_full_n_segments(ctx); |
| 64 | // for (int i = 0; i < n_segments; ++i) { |
| 65 | // const char * text = whisper_full_get_segment_text(ctx, i); |
| 66 | // printf("%s", text); |
| 67 | // } |
| 68 | // |
| 69 | // whisper_free(ctx); |
| 70 | // |
| 71 | // ... |
| 72 | // |
| 73 | // This is a demonstration of the most straightforward usage of the library. |
| 74 | // "pcmf32" contains the RAW audio data in 32-bit floating point format. |
| 75 | // |
| 76 | // The interface also allows for more fine-grained control over the computation, but it requires a deeper |
| 77 | // understanding of how the model works. |
| 78 | // |
| 79 | |
| 80 | struct whisper_context; |
| 81 | struct whisper_state; |
| 82 | struct whisper_full_params; |
| 83 | |
| 84 | typedef int32_t whisper_pos; |
| 85 | typedef int32_t whisper_token; |
| 86 | typedef int32_t whisper_seq_id; |
| 87 | |
| 88 | enum whisper_alignment_heads_preset { |
| 89 | WHISPER_AHEADS_NONE, |
| 90 | WHISPER_AHEADS_N_TOP_MOST, // All heads from the N-top-most text-layers |
| 91 | WHISPER_AHEADS_CUSTOM, |
| 92 | WHISPER_AHEADS_TINY_EN, |
| 93 | WHISPER_AHEADS_TINY, |
| 94 | WHISPER_AHEADS_BASE_EN, |
| 95 | WHISPER_AHEADS_BASE, |
| 96 | WHISPER_AHEADS_SMALL_EN, |
| 97 | WHISPER_AHEADS_SMALL, |
| 98 | WHISPER_AHEADS_MEDIUM_EN, |
| 99 | WHISPER_AHEADS_MEDIUM, |
| 100 | WHISPER_AHEADS_LARGE_V1, |
| 101 | WHISPER_AHEADS_LARGE_V2, |
| 102 | WHISPER_AHEADS_LARGE_V3, |
| 103 | WHISPER_AHEADS_LARGE_V3_TURBO, |
| 104 | }; |
| 105 | |
| 106 | typedef struct whisper_ahead { |
| 107 | int n_text_layer; |
| 108 | int n_head; |
| 109 | } whisper_ahead; |
| 110 | |
| 111 | typedef struct whisper_aheads { |
| 112 | size_t n_heads; |
| 113 | const whisper_ahead * heads; |
| 114 | } whisper_aheads; |
| 115 | |
| 116 | struct whisper_context_params { |
| 117 | bool use_gpu; |
| 118 | bool flash_attn; |
| 119 | int gpu_device; // CUDA device |
| 120 | |
| 121 | // [EXPERIMENTAL] Token-level timestamps with DTW |
| 122 | bool dtw_token_timestamps; |
| 123 | enum whisper_alignment_heads_preset dtw_aheads_preset; |
| 124 | |
| 125 | int dtw_n_top; |
| 126 | struct whisper_aheads dtw_aheads; |
| 127 | |
| 128 | size_t dtw_mem_size; // TODO: remove |
| 129 | }; |
| 130 | |
| 131 | typedef struct whisper_token_data { |
| 132 | whisper_token id; // token id |
| 133 | whisper_token tid; // forced timestamp token id |
| 134 | |
| 135 | float p; // probability of the token |
| 136 | float plog; // log probability of the token |
| 137 | float pt; // probability of the timestamp token |
| 138 | float ptsum; // sum of probabilities of all timestamp tokens |
| 139 | |
| 140 | // token-level timestamp data |
| 141 | // do not use if you haven't computed token-level timestamps |
| 142 | int64_t t0; // start time of the token |
| 143 | int64_t t1; // end time of the token |
| 144 | |
| 145 | // [EXPERIMENTAL] Token-level timestamps with DTW |
| 146 | // do not use if you haven't computed token-level timestamps with dtw |
| 147 | // Roughly corresponds to the moment in audio in which the token was output |
| 148 | int64_t t_dtw; |
| 149 | |
| 150 | float vlen; // voice length of the token |
| 151 | } whisper_token_data; |
| 152 | |
| 153 | typedef struct whisper_model_loader { |
| 154 | void * context; |
| 155 | |
| 156 | size_t (*read)(void * ctx, void * output, size_t read_size); |
| 157 | bool (*eof)(void * ctx); |
| 158 | void (*close)(void * ctx); |
| 159 | } whisper_model_loader; |
| 160 | |
| 161 | // grammar element type |
| 162 | enum whisper_gretype { |
| 163 | // end of rule definition |
| 164 | WHISPER_GRETYPE_END = 0, |
| 165 | |
| 166 | // start of alternate definition for rule |
| 167 | WHISPER_GRETYPE_ALT = 1, |
| 168 | |
| 169 | // non-terminal element: reference to rule |
| 170 | WHISPER_GRETYPE_RULE_REF = 2, |
| 171 | |
| 172 | // terminal element: character (code point) |
| 173 | WHISPER_GRETYPE_CHAR = 3, |
| 174 | |
| 175 | // inverse char(s) ([^a], [^a-b] [^abc]) |
| 176 | WHISPER_GRETYPE_CHAR_NOT = 4, |
| 177 | |
| 178 | // modifies a preceding WHISPER_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_ALT to |
| 179 | // be an inclusive range ([a-z]) |
| 180 | WHISPER_GRETYPE_CHAR_RNG_UPPER = 5, |
| 181 | |
| 182 | // modifies a preceding WHISPER_GRETYPE_CHAR or |
| 183 | // WHISPER_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA]) |
| 184 | WHISPER_GRETYPE_CHAR_ALT = 6, |
| 185 | }; |
| 186 | |
| 187 | typedef struct whisper_grammar_element { |
| 188 | enum whisper_gretype type; |
| 189 | uint32_t value; // Unicode code point or rule ID |
| 190 | } whisper_grammar_element; |
| 191 | |
| 192 | typedef struct whisper_vad_params { |
| 193 | float threshold; // Probability threshold to consider as speech. |
| 194 | int min_speech_duration_ms; // Min duration for a valid speech segment. |
| 195 | int min_silence_duration_ms; // Min silence duration to consider speech as ended. |
| 196 | float max_speech_duration_s; // Max duration of a speech segment before forcing a new segment. |
| 197 | int speech_pad_ms; // Padding added before and after speech segments. |
| 198 | float samples_overlap; // Overlap in seconds when copying audio samples from speech segment. |
| 199 | } whisper_vad_params; |
| 200 | |
| 201 | WHISPER_API const char * whisper_version(void); |
| 202 | |
| 203 | // Various functions for loading a ggml whisper model. |
| 204 | // Allocate (almost) all memory needed for the model. |
| 205 | // Return NULL on failure |
| 206 | WHISPER_API struct whisper_context * whisper_init_from_file_with_params (const char * path_model, struct whisper_context_params params); |
| 207 | WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params(void * buffer, size_t buffer_size, struct whisper_context_params params); |
| 208 | WHISPER_API struct whisper_context * whisper_init_with_params (struct whisper_model_loader * loader, struct whisper_context_params params); |
| 209 | |
| 210 | // These are the same as the above, but the internal state of the context is not allocated automatically |
| 211 | // It is the responsibility of the caller to allocate the state using whisper_init_state() (#523) |
| 212 | WHISPER_API struct whisper_context * whisper_init_from_file_with_params_no_state (const char * path_model, struct whisper_context_params params); |
| 213 | WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params); |
| 214 | WHISPER_API struct whisper_context * whisper_init_with_params_no_state (struct whisper_model_loader * loader, struct whisper_context_params params); |
| 215 | |
| 216 | WHISPER_DEPRECATED( |
| 217 | WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model), |
| 218 | "use whisper_init_from_file_with_params instead" |
| 219 | ); |
| 220 | WHISPER_DEPRECATED( |
| 221 | WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size), |
| 222 | "use whisper_init_from_buffer_with_params instead" |
| 223 | ); |
| 224 | WHISPER_DEPRECATED( |
| 225 | WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader), |
| 226 | "use whisper_init_with_params instead" |
| 227 | ); |
| 228 | WHISPER_DEPRECATED( |
| 229 | WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model), |
| 230 | "use whisper_init_from_file_with_params_no_state instead" |
| 231 | ); |
| 232 | WHISPER_DEPRECATED( |
| 233 | WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size), |
| 234 | "use whisper_init_from_buffer_with_params_no_state instead" |
| 235 | ); |
| 236 | WHISPER_DEPRECATED( |
| 237 | WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader), |
| 238 | "use whisper_init_with_params_no_state instead" |
| 239 | ); |
| 240 | |
| 241 | WHISPER_API struct whisper_state * whisper_init_state(struct whisper_context * ctx); |
| 242 | |
| 243 | // Given a context, enable use of OpenVINO for encode inference. |
| 244 | // model_path: Optional path to OpenVINO encoder IR model. If set to nullptr, |
| 245 | // the path will be generated from the ggml model path that was passed |
| 246 | // in to whisper_init_from_file. For example, if 'path_model' was |
| 247 | // "/path/to/ggml-base.en.bin", then OpenVINO IR model path will be |
| 248 | // assumed to be "/path/to/ggml-base.en-encoder-openvino.xml". |
| 249 | // device: OpenVINO device to run inference on ("CPU", "GPU", etc.) |
| 250 | // cache_dir: Optional cache directory that can speed up init time, especially for |
| 251 | // GPU, by caching compiled 'blobs' there. |
| 252 | // Set to nullptr if not used. |
| 253 | // Returns 0 on success. If OpenVINO is not enabled in build, this simply returns 1. |
| 254 | WHISPER_API int whisper_ctx_init_openvino_encoder_with_state( |
| 255 | struct whisper_context * ctx, |
| 256 | struct whisper_state * state, |
| 257 | const char * model_path, |
| 258 | const char * device, |
| 259 | const char * cache_dir); |
| 260 | |
| 261 | WHISPER_API int whisper_ctx_init_openvino_encoder( |
| 262 | struct whisper_context * ctx, |
| 263 | const char * model_path, |
| 264 | const char * device, |
| 265 | const char * cache_dir); |
| 266 | |
| 267 | // Frees all allocated memory |
| 268 | WHISPER_API void whisper_free (struct whisper_context * ctx); |
| 269 | WHISPER_API void whisper_free_state(struct whisper_state * state); |
| 270 | WHISPER_API void whisper_free_params(struct whisper_full_params * params); |
| 271 | WHISPER_API void whisper_free_context_params(struct whisper_context_params * params); |
| 272 | |
| 273 | // Convert RAW PCM audio to log mel spectrogram. |
| 274 | // The resulting spectrogram is stored inside the default state of the provided whisper context. |
| 275 | // Returns 0 on success |
| 276 | WHISPER_API int whisper_pcm_to_mel( |
| 277 | struct whisper_context * ctx, |
| 278 | const float * samples, |
| 279 | int n_samples, |
| 280 | int n_threads); |
| 281 | |
| 282 | WHISPER_API int whisper_pcm_to_mel_with_state( |
| 283 | struct whisper_context * ctx, |
| 284 | struct whisper_state * state, |
| 285 | const float * samples, |
| 286 | int n_samples, |
| 287 | int n_threads); |
| 288 | |
| 289 | // This can be used to set a custom log mel spectrogram inside the default state of the provided whisper context. |
| 290 | // Use this instead of whisper_pcm_to_mel() if you want to provide your own log mel spectrogram. |
| 291 | // n_mel must be 80 |
| 292 | // Returns 0 on success |
| 293 | WHISPER_API int whisper_set_mel( |
| 294 | struct whisper_context * ctx, |
| 295 | const float * data, |
| 296 | int n_len, |
| 297 | int n_mel); |
| 298 | |
| 299 | WHISPER_API int whisper_set_mel_with_state( |
| 300 | struct whisper_context * ctx, |
| 301 | struct whisper_state * state, |
| 302 | const float * data, |
| 303 | int n_len, |
| 304 | int n_mel); |
| 305 | |
| 306 | // Run the Whisper encoder on the log mel spectrogram stored inside the default state in the provided whisper context. |
| 307 | // Make sure to call whisper_pcm_to_mel() or whisper_set_mel() first. |
| 308 | // offset can be used to specify the offset of the first frame in the spectrogram. |
| 309 | // Returns 0 on success |
| 310 | WHISPER_API int whisper_encode( |
| 311 | struct whisper_context * ctx, |
| 312 | int offset, |
| 313 | int n_threads); |
| 314 | |
| 315 | WHISPER_API int whisper_encode_with_state( |
| 316 | struct whisper_context * ctx, |
| 317 | struct whisper_state * state, |
| 318 | int offset, |
| 319 | int n_threads); |
| 320 | |
| 321 | // Run the Whisper decoder to obtain the logits and probabilities for the next token. |
| 322 | // Make sure to call whisper_encode() first. |
| 323 | // tokens + n_tokens is the provided context for the decoder. |
| 324 | // n_past is the number of tokens to use from previous decoder calls. |
| 325 | // Returns 0 on success |
| 326 | // TODO: add support for multiple decoders |
| 327 | WHISPER_API int whisper_decode( |
| 328 | struct whisper_context * ctx, |
| 329 | const whisper_token * tokens, |
| 330 | int n_tokens, |
| 331 | int n_past, |
| 332 | int n_threads); |
| 333 | |
| 334 | WHISPER_API int whisper_decode_with_state( |
| 335 | struct whisper_context * ctx, |
| 336 | struct whisper_state * state, |
| 337 | const whisper_token * tokens, |
| 338 | int n_tokens, |
| 339 | int n_past, |
| 340 | int n_threads); |
| 341 | |
| 342 | // Convert the provided text into tokens. |
| 343 | // The tokens pointer must be large enough to hold the resulting tokens. |
| 344 | // Returns the number of tokens on success, no more than n_max_tokens |
| 345 | // Returns a negative number on failure - the number of tokens that would have been returned |
| 346 | // TODO: not sure if correct |
| 347 | WHISPER_API int whisper_tokenize( |
| 348 | struct whisper_context * ctx, |
| 349 | const char * text, |
| 350 | whisper_token * tokens, |
| 351 | int n_max_tokens); |
| 352 | |
| 353 | // Return the number of tokens in the provided text |
| 354 | // Equivalent to: -whisper_tokenize(ctx, text, NULL, 0) |
| 355 | int whisper_token_count(struct whisper_context * ctx, const char * text); |
| 356 | |
| 357 | // Largest language id (i.e. number of available languages - 1) |
| 358 | WHISPER_API int whisper_lang_max_id(void); |
| 359 | |
| 360 | // Return the id of the specified language, returns -1 if not found |
| 361 | // Examples: |
| 362 | // "de" -> 2 |
| 363 | // "german" -> 2 |
| 364 | WHISPER_API int whisper_lang_id(const char * lang); |
| 365 | |
| 366 | // Return the short string of the specified language id (e.g. 2 -> "de"), returns nullptr if not found |
| 367 | WHISPER_API const char * whisper_lang_str(int id); |
| 368 | |
| 369 | // Return the short string of the specified language name (e.g. 2 -> "german"), returns nullptr if not found |
| 370 | WHISPER_API const char * whisper_lang_str_full(int id); |
| 371 | |
| 372 | // Use mel data at offset_ms to try and auto-detect the spoken language |
| 373 | // Make sure to call whisper_pcm_to_mel() or whisper_set_mel() first |
| 374 | // Returns the top language id or negative on failure |
| 375 | // If not null, fills the lang_probs array with the probabilities of all languages |
| 376 | // The array must be whisper_lang_max_id() + 1 in size |
| 377 | // ref: https://github.com/openai/whisper/blob/main/whisper/decoding.py#L18-L69 |
| 378 | WHISPER_API int whisper_lang_auto_detect( |
| 379 | struct whisper_context * ctx, |
| 380 | int offset_ms, |
| 381 | int n_threads, |
| 382 | float * lang_probs); |
| 383 | |
| 384 | WHISPER_API int whisper_lang_auto_detect_with_state( |
| 385 | struct whisper_context * ctx, |
| 386 | struct whisper_state * state, |
| 387 | int offset_ms, |
| 388 | int n_threads, |
| 389 | float * lang_probs); |
| 390 | |
| 391 | WHISPER_API int whisper_n_len (struct whisper_context * ctx); // mel length |
| 392 | WHISPER_API int whisper_n_len_from_state(struct whisper_state * state); // mel length |
| 393 | WHISPER_API int whisper_n_vocab (struct whisper_context * ctx); |
| 394 | WHISPER_API int whisper_n_text_ctx (struct whisper_context * ctx); |
| 395 | WHISPER_API int whisper_n_audio_ctx (struct whisper_context * ctx); |
| 396 | WHISPER_API int whisper_is_multilingual (struct whisper_context * ctx); |
| 397 | |
| 398 | WHISPER_API int whisper_model_n_vocab (struct whisper_context * ctx); |
| 399 | WHISPER_API int whisper_model_n_audio_ctx (struct whisper_context * ctx); |
| 400 | WHISPER_API int whisper_model_n_audio_state(struct whisper_context * ctx); |
| 401 | WHISPER_API int whisper_model_n_audio_head (struct whisper_context * ctx); |
| 402 | WHISPER_API int whisper_model_n_audio_layer(struct whisper_context * ctx); |
| 403 | WHISPER_API int whisper_model_n_text_ctx (struct whisper_context * ctx); |
| 404 | WHISPER_API int whisper_model_n_text_state (struct whisper_context * ctx); |
| 405 | WHISPER_API int whisper_model_n_text_head (struct whisper_context * ctx); |
| 406 | WHISPER_API int whisper_model_n_text_layer (struct whisper_context * ctx); |
| 407 | WHISPER_API int whisper_model_n_mels (struct whisper_context * ctx); |
| 408 | WHISPER_API int whisper_model_ftype (struct whisper_context * ctx); |
| 409 | WHISPER_API int whisper_model_type (struct whisper_context * ctx); |
| 410 | |
| 411 | // Token logits obtained from the last call to whisper_decode() |
| 412 | // The logits for the last token are stored in the last row |
| 413 | // Rows: n_tokens |
| 414 | // Cols: n_vocab |
| 415 | WHISPER_API float * whisper_get_logits (struct whisper_context * ctx); |
| 416 | WHISPER_API float * whisper_get_logits_from_state(struct whisper_state * state); |
| 417 | |
| 418 | // Token Id -> String. Uses the vocabulary in the provided context |
| 419 | WHISPER_API const char * whisper_token_to_str(struct whisper_context * ctx, whisper_token token); |
| 420 | WHISPER_API const char * whisper_model_type_readable(struct whisper_context * ctx); |
| 421 | |
| 422 | |
| 423 | // Special tokens |
| 424 | WHISPER_API whisper_token whisper_token_eot (struct whisper_context * ctx); |
| 425 | WHISPER_API whisper_token whisper_token_sot (struct whisper_context * ctx); |
| 426 | WHISPER_API whisper_token whisper_token_solm(struct whisper_context * ctx); |
| 427 | WHISPER_API whisper_token whisper_token_prev(struct whisper_context * ctx); |
| 428 | WHISPER_API whisper_token whisper_token_nosp(struct whisper_context * ctx); |
| 429 | WHISPER_API whisper_token whisper_token_not (struct whisper_context * ctx); |
| 430 | WHISPER_API whisper_token whisper_token_beg (struct whisper_context * ctx); |
| 431 | WHISPER_API whisper_token whisper_token_lang(struct whisper_context * ctx, int lang_id); |
| 432 | |
| 433 | // Task tokens |
| 434 | WHISPER_API whisper_token whisper_token_translate (struct whisper_context * ctx); |
| 435 | WHISPER_API whisper_token whisper_token_transcribe(struct whisper_context * ctx); |
| 436 | |
| 437 | // Performance information from the default state. |
| 438 | struct whisper_timings { |
| 439 | float sample_ms; |
| 440 | float encode_ms; |
| 441 | float decode_ms; |
| 442 | float batchd_ms; |
| 443 | float prompt_ms; |
| 444 | }; |
| 445 | WHISPER_API struct whisper_timings * whisper_get_timings(struct whisper_context * ctx); |
| 446 | WHISPER_API void whisper_print_timings(struct whisper_context * ctx); |
| 447 | WHISPER_API void whisper_reset_timings(struct whisper_context * ctx); |
| 448 | |
| 449 | // Print system information |
| 450 | WHISPER_API const char * whisper_print_system_info(void); |
| 451 | |
| 452 | //////////////////////////////////////////////////////////////////////////// |
| 453 | |
| 454 | // Available sampling strategies |
| 455 | enum whisper_sampling_strategy { |
| 456 | WHISPER_SAMPLING_GREEDY, // similar to OpenAI's GreedyDecoder |
| 457 | WHISPER_SAMPLING_BEAM_SEARCH, // similar to OpenAI's BeamSearchDecoder |
| 458 | }; |
| 459 | |
| 460 | // Text segment callback |
| 461 | // Called on every newly generated text segment |
| 462 | // Use the whisper_full_...() functions to obtain the text segments |
| 463 | typedef void (*whisper_new_segment_callback)(struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data); |
| 464 | |
| 465 | // Progress callback |
| 466 | typedef void (*whisper_progress_callback)(struct whisper_context * ctx, struct whisper_state * state, int progress, void * user_data); |
| 467 | |
| 468 | // Encoder begin callback |
| 469 | // If not NULL, called before the encoder starts |
| 470 | // If it returns false, the computation is aborted |
| 471 | typedef bool (*whisper_encoder_begin_callback)(struct whisper_context * ctx, struct whisper_state * state, void * user_data); |
| 472 | |
| 473 | // Logits filter callback |
| 474 | // Can be used to modify the logits before sampling |
| 475 | // If not NULL, called after applying temperature to logits |
| 476 | typedef void (*whisper_logits_filter_callback)( |
| 477 | struct whisper_context * ctx, |
| 478 | struct whisper_state * state, |
| 479 | const whisper_token_data * tokens, |
| 480 | int n_tokens, |
| 481 | float * logits, |
| 482 | void * user_data); |
| 483 | |
| 484 | // Parameters for the whisper_full() function |
| 485 | // If you change the order or add new parameters, make sure to update the default values in whisper.cpp: |
| 486 | // whisper_full_default_params() |
| 487 | struct whisper_full_params { |
| 488 | enum whisper_sampling_strategy strategy; |
| 489 | |
| 490 | int n_threads; |
| 491 | int n_max_text_ctx; // max tokens to use from past text as prompt for the decoder |
| 492 | int offset_ms; // start offset in ms |
| 493 | int duration_ms; // audio duration to process in ms |
| 494 | |
| 495 | bool translate; |
| 496 | bool no_context; // do not use past transcription (if any) as initial prompt for the decoder |
| 497 | bool no_timestamps; // do not generate timestamps |
| 498 | bool single_segment; // force single segment output (useful for streaming) |
| 499 | bool print_special; // print special tokens (e.g. <SOT>, <EOT>, <BEG>, etc.) |
| 500 | bool print_progress; // print progress information |
| 501 | bool print_realtime; // print results from within whisper.cpp (avoid it, use callback instead) |
| 502 | bool print_timestamps; // print timestamps for each text segment when printing realtime |
| 503 | |
| 504 | // [EXPERIMENTAL] token-level timestamps |
| 505 | bool token_timestamps; // enable token-level timestamps |
| 506 | float thold_pt; // timestamp token probability threshold (~0.01) |
| 507 | float thold_ptsum; // timestamp token sum probability threshold (~0.01) |
| 508 | int max_len; // max segment length in characters |
| 509 | bool split_on_word; // split on word rather than on token (when used with max_len) |
| 510 | int max_tokens; // max tokens per segment (0 = no limit) |
| 511 | |
| 512 | // [EXPERIMENTAL] speed-up techniques |
| 513 | // note: these can significantly reduce the quality of the output |
| 514 | bool debug_mode; // enable debug_mode provides extra info (eg. Dump log_mel) |
| 515 | int audio_ctx; // overwrite the audio context size (0 = use default) |
| 516 | |
| 517 | // [EXPERIMENTAL] [TDRZ] tinydiarize |
| 518 | bool tdrz_enable; // enable tinydiarize speaker turn detection |
| 519 | |
| 520 | // A regular expression that matches tokens to suppress |
| 521 | const char * suppress_regex; |
| 522 | |
| 523 | // tokens to provide to the whisper decoder as initial prompt |
| 524 | // these are prepended to any existing text context from a previous call |
| 525 | // use whisper_tokenize() to convert text to tokens |
| 526 | // maximum of whisper_n_text_ctx()/2 tokens are used (typically 224) |
| 527 | const char * initial_prompt; |
| 528 | bool carry_initial_prompt; // if true, always prepend initial_prompt to every decode window (may reduce conditioning on previous text) |
| 529 | const whisper_token * prompt_tokens; |
| 530 | int prompt_n_tokens; |
| 531 | |
| 532 | // for auto-detection, set to nullptr, "" or "auto" |
| 533 | const char * language; |
| 534 | bool detect_language; |
| 535 | |
| 536 | // common decoding parameters: |
| 537 | bool suppress_blank; // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/decoding.py#L89 |
| 538 | bool suppress_nst; // non-speech tokens, ref: https://github.com/openai/whisper/blob/7858aa9c08d98f75575035ecd6481f462d66ca27/whisper/tokenizer.py#L224-L253 |
| 539 | |
| 540 | float temperature; // initial decoding temperature, ref: https://ai.stackexchange.com/a/32478 |
| 541 | float max_initial_ts; // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/decoding.py#L97 |
| 542 | float length_penalty; // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/transcribe.py#L267 |
| 543 | |
| 544 | // fallback parameters |
| 545 | // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/transcribe.py#L274-L278 |
| 546 | float temperature_inc; |
| 547 | float entropy_thold; // similar to OpenAI's "compression_ratio_threshold" |
| 548 | float logprob_thold; |
| 549 | float no_speech_thold; |
| 550 | |
| 551 | struct { |
| 552 | int best_of; // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/transcribe.py#L264 |
| 553 | } greedy; |
| 554 | |
| 555 | struct { |
| 556 | int beam_size; // ref: https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/transcribe.py#L265 |
| 557 | |
| 558 | float patience; // TODO: not implemented, ref: https://arxiv.org/pdf/2204.05424.pdf |
| 559 | } beam_search; |
| 560 | |
| 561 | // called for every newly generated text segment |
| 562 | whisper_new_segment_callback new_segment_callback; |
| 563 | void * new_segment_callback_user_data; |
| 564 | |
| 565 | // called on each progress update |
| 566 | whisper_progress_callback progress_callback; |
| 567 | void * progress_callback_user_data; |
| 568 | |
| 569 | // called each time before the encoder starts |
| 570 | whisper_encoder_begin_callback encoder_begin_callback; |
| 571 | void * encoder_begin_callback_user_data; |
| 572 | |
| 573 | // called each time before ggml computation starts |
| 574 | ggml_abort_callback abort_callback; |
| 575 | void * abort_callback_user_data; |
| 576 | |
| 577 | // called by each decoder to filter obtained logits |
| 578 | whisper_logits_filter_callback logits_filter_callback; |
| 579 | void * logits_filter_callback_user_data; |
| 580 | |
| 581 | const whisper_grammar_element ** grammar_rules; |
| 582 | size_t n_grammar_rules; |
| 583 | size_t i_start_rule; |
| 584 | float grammar_penalty; |
| 585 | |
| 586 | // Voice Activity Detection (VAD) params |
| 587 | bool vad; // Enable VAD |
| 588 | const char * vad_model_path; // Path to VAD model |
| 589 | |
| 590 | /* |
| 591 | Following two variables are added by Anastasia Shchupak on behalf of inclusion into Praat. |
| 592 | In the original whisper.cpp version, VAD model is loaded from a binary file. |
| 593 | Praat contains Silero-VAD model in memory, therefore it needs variables to access this memory. |
| 594 | */ |
| 595 | const void * vad_model_data; // Pointer to in-memory model data |
| 596 | size_t vad_model_data_size; // Size of in-memory model data |
| 597 | |
| 598 | whisper_vad_params vad_params; |
| 599 | }; |
| 600 | |
| 601 | // NOTE: this function allocates memory, and it is the responsibility of the caller to free the pointer - see whisper_free_context_params & whisper_free_params() |
| 602 | WHISPER_API struct whisper_context_params * whisper_context_default_params_by_ref(void); |
| 603 | WHISPER_API struct whisper_context_params whisper_context_default_params (void); |
| 604 | |
| 605 | WHISPER_API struct whisper_full_params * whisper_full_default_params_by_ref(enum whisper_sampling_strategy strategy); |
| 606 | WHISPER_API struct whisper_full_params whisper_full_default_params (enum whisper_sampling_strategy strategy); |
| 607 | |
| 608 | // Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text |
| 609 | // Not thread safe for same context |
| 610 | // Uses the specified decoding strategy to obtain the text. |
| 611 | WHISPER_API int whisper_full( |
| 612 | struct whisper_context * ctx, |
| 613 | struct whisper_full_params params, |
| 614 | const float * samples, |
| 615 | int n_samples); |
| 616 | |
| 617 | WHISPER_API int whisper_full_with_state( |
| 618 | struct whisper_context * ctx, |
| 619 | struct whisper_state * state, |
| 620 | struct whisper_full_params params, |
| 621 | const float * samples, |
| 622 | int n_samples); |
| 623 | |
| 624 | // Split the input audio in chunks and process each chunk separately using whisper_full_with_state() |
| 625 | // Result is stored in the default state of the context |
| 626 | // Not thread safe if executed in parallel on the same context. |
| 627 | // It seems this approach can offer some speedup in some cases. |
| 628 | // However, the transcription accuracy can be worse at the beginning and end of each chunk. |
| 629 | WHISPER_API int whisper_full_parallel( |
| 630 | struct whisper_context * ctx, |
| 631 | struct whisper_full_params params, |
| 632 | const float * samples, |
| 633 | int n_samples, |
| 634 | int n_processors); |
| 635 | |
| 636 | // Number of generated text segments |
| 637 | // A segment can be a few words, a sentence, or even a paragraph. |
| 638 | WHISPER_API int whisper_full_n_segments (struct whisper_context * ctx); |
| 639 | WHISPER_API int whisper_full_n_segments_from_state(struct whisper_state * state); |
| 640 | |
| 641 | // Language id associated with the context's default state |
| 642 | WHISPER_API int whisper_full_lang_id(struct whisper_context * ctx); |
| 643 | |
| 644 | // Language id associated with the provided state |
| 645 | WHISPER_API int whisper_full_lang_id_from_state(struct whisper_state * state); |
| 646 | |
| 647 | // Get the start and end time of the specified segment |
| 648 | WHISPER_API int64_t whisper_full_get_segment_t0 (struct whisper_context * ctx, int i_segment); |
| 649 | WHISPER_API int64_t whisper_full_get_segment_t0_from_state(struct whisper_state * state, int i_segment); |
| 650 | |
| 651 | WHISPER_API int64_t whisper_full_get_segment_t1 (struct whisper_context * ctx, int i_segment); |
| 652 | WHISPER_API int64_t whisper_full_get_segment_t1_from_state(struct whisper_state * state, int i_segment); |
| 653 | |
| 654 | // Get whether the next segment is predicted as a speaker turn |
| 655 | WHISPER_API bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment); |
| 656 | WHISPER_API bool whisper_full_get_segment_speaker_turn_next_from_state(struct whisper_state * state, int i_segment); |
| 657 | |
| 658 | // Get the text of the specified segment |
| 659 | WHISPER_API const char * whisper_full_get_segment_text (struct whisper_context * ctx, int i_segment); |
| 660 | WHISPER_API const char * whisper_full_get_segment_text_from_state(struct whisper_state * state, int i_segment); |
| 661 | |
| 662 | // Get number of tokens in the specified segment |
| 663 | WHISPER_API int whisper_full_n_tokens (struct whisper_context * ctx, int i_segment); |
| 664 | WHISPER_API int whisper_full_n_tokens_from_state(struct whisper_state * state, int i_segment); |
| 665 | |
| 666 | // Get the token text of the specified token in the specified segment |
| 667 | WHISPER_API const char * whisper_full_get_token_text (struct whisper_context * ctx, int i_segment, int i_token); |
| 668 | WHISPER_API const char * whisper_full_get_token_text_from_state(struct whisper_context * ctx, struct whisper_state * state, int i_segment, int i_token); |
| 669 | |
| 670 | WHISPER_API whisper_token whisper_full_get_token_id (struct whisper_context * ctx, int i_segment, int i_token); |
| 671 | WHISPER_API whisper_token whisper_full_get_token_id_from_state(struct whisper_state * state, int i_segment, int i_token); |
| 672 | |
| 673 | // Get token data for the specified token in the specified segment |
| 674 | // This contains probabilities, timestamps, etc. |
| 675 | WHISPER_API whisper_token_data whisper_full_get_token_data (struct whisper_context * ctx, int i_segment, int i_token); |
| 676 | WHISPER_API whisper_token_data whisper_full_get_token_data_from_state(struct whisper_state * state, int i_segment, int i_token); |
| 677 | |
| 678 | // Get the probability of the specified token in the specified segment |
| 679 | WHISPER_API float whisper_full_get_token_p (struct whisper_context * ctx, int i_segment, int i_token); |
| 680 | WHISPER_API float whisper_full_get_token_p_from_state(struct whisper_state * state, int i_segment, int i_token); |
| 681 | |
| 682 | // |
| 683 | // Voice Activity Detection (VAD) |
| 684 | // |
| 685 | |
| 686 | struct whisper_vad_context; |
| 687 | |
| 688 | WHISPER_API struct whisper_vad_params whisper_vad_default_params(void); |
| 689 | |
| 690 | struct whisper_vad_context_params { |
| 691 | int n_threads; // The number of threads to use for processing. |
| 692 | bool use_gpu; |
| 693 | int gpu_device; // CUDA device |
| 694 | }; |
| 695 | |
| 696 | WHISPER_API struct whisper_vad_context_params whisper_vad_default_context_params(void); |
| 697 | |
| 698 | WHISPER_API struct whisper_vad_context * whisper_vad_init_from_file_with_params(const char * path_model, struct whisper_vad_context_params params); |
| 699 | WHISPER_API struct whisper_vad_context * whisper_vad_init_with_params (struct whisper_model_loader * loader, struct whisper_vad_context_params params); |
| 700 | |
| 701 | /* |
| 702 | Function whisper_vad_init_from_memory_with_params() is added by Anastasia Shchupak on behalf of inclusion into Praat. |
| 703 | In the original whisper.cpp version, whisper_vad_init_from_file_with_params() is always used. |
| 704 | Praat contains Silero-VAD model in memory, therefore whisper_vad_init_from_memory_with_params() is added. |
| 705 | */ |
| 706 | WHISPER_API struct whisper_vad_context * whisper_vad_init_from_memory_with_params(const void * data, size_t size, struct whisper_vad_context_params params); |
| 707 | |
| 708 | WHISPER_API bool whisper_vad_detect_speech( |
| 709 | struct whisper_vad_context * vctx, |
| 710 | const float * samples, |
| 711 | int n_samples); |
| 712 | |
| 713 | WHISPER_API int whisper_vad_n_probs(struct whisper_vad_context * vctx); |
| 714 | WHISPER_API float * whisper_vad_probs (struct whisper_vad_context * vctx); |
| 715 | |
| 716 | struct whisper_vad_segments; |
| 717 | |
| 718 | WHISPER_API struct whisper_vad_segments * whisper_vad_segments_from_probs( |
| 719 | struct whisper_vad_context * vctx, |
| 720 | struct whisper_vad_params params); |
| 721 | |
| 722 | WHISPER_API struct whisper_vad_segments * whisper_vad_segments_from_samples( |
| 723 | struct whisper_vad_context * vctx, |
| 724 | struct whisper_vad_params params, |
| 725 | const float * samples, |
| 726 | int n_samples); |
| 727 | |
| 728 | WHISPER_API int whisper_vad_segments_n_segments(struct whisper_vad_segments * segments); |
| 729 | |
| 730 | WHISPER_API float whisper_vad_segments_get_segment_t0(struct whisper_vad_segments * segments, int i_segment); |
| 731 | WHISPER_API float whisper_vad_segments_get_segment_t1(struct whisper_vad_segments * segments, int i_segment); |
| 732 | |
| 733 | WHISPER_API void whisper_vad_free_segments(struct whisper_vad_segments * segments); |
| 734 | WHISPER_API void whisper_vad_free (struct whisper_vad_context * ctx); |
| 735 | |
| 736 | /* |
| 737 | Following five functions are added by Anastasia Shchupak on behalf of inclusion into Praat. |
| 738 | It provides direct access to VAD segments. |
| 739 | */ |
| 740 | WHISPER_API int whisper_full_n_vad_segments(struct whisper_context * ctx); |
| 741 | WHISPER_API int64_t whisper_full_get_vad_segment_orig_start(struct whisper_context * ctx, int i_vad_segment); |
| 742 | WHISPER_API int64_t whisper_full_get_vad_segment_orig_end(struct whisper_context * ctx, int i_vad_segment); |
| 743 | WHISPER_API int64_t whisper_full_get_vad_segment_vad_start(struct whisper_context * ctx, int i_vad_segment); |
| 744 | WHISPER_API int64_t whisper_full_get_vad_segment_vad_end(struct whisper_context * ctx, int i_vad_segment); |
| 745 | |
| 746 | //////////////////////////////////////////////////////////////////////////// |
| 747 | |
| 748 | // Temporary helpers needed for exposing ggml interface |
| 749 | |
| 750 | WHISPER_API int whisper_bench_memcpy (int n_threads); |
| 751 | WHISPER_API const char * whisper_bench_memcpy_str (int n_threads); |
| 752 | WHISPER_API int whisper_bench_ggml_mul_mat (int n_threads); |
| 753 | WHISPER_API const char * whisper_bench_ggml_mul_mat_str(int n_threads); |
| 754 | |
| 755 | // Control logging output; default behavior is to print to stderr |
| 756 | |
| 757 | WHISPER_API void whisper_log_set(ggml_log_callback log_callback, void * user_data); |
| 758 | |
| 759 | // Get the no_speech probability for the specified segment |
| 760 | WHISPER_API float whisper_full_get_segment_no_speech_prob (struct whisper_context * ctx, int i_segment); |
| 761 | WHISPER_API float whisper_full_get_segment_no_speech_prob_from_state(struct whisper_state * state, int i_segment); |
| 762 | #ifdef __cplusplus |
| 763 | } |
| 764 | #endif |
| 765 | |
| 766 | #endif |