Skip to main content

whisper_cpp_plus_sys/
lib.rs

1//! Low-level FFI bindings for whisper.cpp.
2
3#![allow(non_upper_case_globals)]
4#![allow(non_camel_case_types)]
5#![allow(non_snake_case)]
6#![allow(improper_ctypes)]
7
8// Include the bindgen-generated bindings
9include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
10
11// Manual error code constants not documented in whisper.h
12// These are based on common error patterns seen in whisper.cpp
13pub const WHISPER_ERR_INVALID_MODEL: i32 = -1;
14pub const WHISPER_ERR_NOT_ENOUGH_MEMORY: i32 = -2;
15pub const WHISPER_ERR_FAILED_TO_PROCESS: i32 = -3;
16pub const WHISPER_ERR_INVALID_CONTEXT: i32 = -4;
17
18// Quantization-related constants and functions
19#[cfg(feature = "quantization")]
20pub const WHISPER_QUANTIZE_OK: i32 = 0;
21#[cfg(feature = "quantization")]
22pub const WHISPER_QUANTIZE_ERROR_INVALID_MODEL: i32 = -1;
23#[cfg(feature = "quantization")]
24pub const WHISPER_QUANTIZE_ERROR_FILE_OPEN: i32 = -2;
25#[cfg(feature = "quantization")]
26pub const WHISPER_QUANTIZE_ERROR_FILE_WRITE: i32 = -3;
27#[cfg(feature = "quantization")]
28pub const WHISPER_QUANTIZE_ERROR_INVALID_FTYPE: i32 = -4;
29#[cfg(feature = "quantization")]
30pub const WHISPER_QUANTIZE_ERROR_QUANTIZATION_FAILED: i32 = -5;
31
32// GGML quantization types
33pub const GGML_FTYPE_UNKNOWN: i32 = -1;
34pub const GGML_FTYPE_ALL_F32: i32 = 0;
35pub const GGML_FTYPE_MOSTLY_F16: i32 = 1;
36pub const GGML_FTYPE_MOSTLY_Q4_0: i32 = 2;
37pub const GGML_FTYPE_MOSTLY_Q4_1: i32 = 3;
38pub const GGML_FTYPE_MOSTLY_Q4_1_SOME_F16: i32 = 4;
39pub const GGML_FTYPE_MOSTLY_Q8_0: i32 = 7;
40pub const GGML_FTYPE_MOSTLY_Q5_0: i32 = 8;
41pub const GGML_FTYPE_MOSTLY_Q5_1: i32 = 9;
42pub const GGML_FTYPE_MOSTLY_Q2_K: i32 = 10;
43pub const GGML_FTYPE_MOSTLY_Q3_K: i32 = 11;
44pub const GGML_FTYPE_MOSTLY_Q4_K: i32 = 12;
45pub const GGML_FTYPE_MOSTLY_Q5_K: i32 = 13;
46pub const GGML_FTYPE_MOSTLY_Q6_K: i32 = 14;
47
48// Progress callback type
49#[cfg(feature = "quantization")]
50pub type whisper_quantize_progress_callback = Option<extern "C" fn(progress: f32)>;
51
52#[cfg(feature = "quantization")]
53extern "C" {
54    /// Quantize a Whisper model file
55    ///
56    /// # Parameters
57    /// - `fname_inp`: Path to the input model file
58    /// - `fname_out`: Path to the output quantized model file
59    /// - `ftype`: Quantization type (one of the GGML_FTYPE_MOSTLY_* constants)
60    /// - `progress_callback`: Optional callback function for progress updates
61    ///
62    /// # Returns
63    /// - `WHISPER_QUANTIZE_OK` on success
64    /// - One of the `WHISPER_QUANTIZE_ERROR_*` codes on failure
65    pub fn whisper_model_quantize(
66        fname_inp: *const ::std::os::raw::c_char,
67        fname_out: *const ::std::os::raw::c_char,
68        ftype: ::std::os::raw::c_int,
69        progress_callback: whisper_quantize_progress_callback,
70    ) -> ::std::os::raw::c_int;
71
72    /// Get the quantization type of a model file
73    ///
74    /// # Parameters
75    /// - `fname`: Path to the model file
76    ///
77    /// # Returns
78    /// - The GGML_FTYPE_* constant representing the model's quantization
79    /// - -1 on error (file not found or invalid model)
80    pub fn whisper_model_get_ftype(
81        fname: *const ::std::os::raw::c_char,
82    ) -> ::std::os::raw::c_int;
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_constants_defined() {
91        // Just verify that our custom error constants are accessible
92        assert_eq!(WHISPER_ERR_INVALID_MODEL, -1);
93        assert_eq!(WHISPER_ERR_NOT_ENOUGH_MEMORY, -2);
94        assert_eq!(WHISPER_ERR_FAILED_TO_PROCESS, -3);
95        assert_eq!(WHISPER_ERR_INVALID_CONTEXT, -4);
96    }
97}