class

Whisper

Inherits Reference < Object

Constants

Log = ::Log.for("whisper-cry")

Constructors

from_buffer(buffer : Bytes, use_gpu : Bool = false) : Whisper

Initializes a whisper context from a pre-loaded model buffer in memory.

buffer should contain the raw model data (same content as a model file). Set use_gpu to true to enable Metal acceleration on macOS.

Raises Whisper::Error if the model fails to load.

Source
new(model_path : String, use_gpu : Bool = false)

Loads a whisper model from disk and initializes the inference context.

model_path should point to a GGML-format model file (e.g. ggml-base.en.bin). Set use_gpu to true to enable Metal acceleration on macOS.

Raises Whisper::Error if the file doesn't exist or the model fails to load.

Source

Class methods

bench_ggml_mul_mat(n_threads : Int32 = 4) : Int32

Runs a GGML matrix multiply benchmark and returns the result code.

Source
bench_ggml_mul_mat_str(n_threads : Int32 = 4) : String

Runs a GGML matrix multiply benchmark and returns a human-readable result string.

Source
bench_memcpy(n_threads : Int32 = 4) : Int32

Runs a memory copy benchmark and returns the result code.

Source
bench_memcpy_str(n_threads : Int32 = 4) : String

Runs a memory copy benchmark and returns a human-readable result string.

Source
lang_id(lang : String) : Int32

Returns the language ID for the given language string, or -1 if not found.

Source
lang_max_id

Returns the largest language ID (number of available languages - 1).

Source
lang_str(id : Int32) : String

Returns the short language code for the given ID (e.g. "de").

Source
lang_str_full(id : Int32) : String

Returns the full language name for the given ID (e.g. "german").

Source
load_wav_samples(path : String) : Array(Float32)

Loads a 16-bit PCM WAV file and converts to float32 samples. Expects: mono, 16kHz, 16-bit signed PCM (standard whisper input format). Use ffmpeg to convert other formats: ffmpeg -i input.mp3 -ar 16000 -ac 1 -f wav output.wav

Source

Instance methods

close

Frees the underlying whisper context. Safe to call multiple times. Called automatically by #finalize, but explicit cleanup is preferred.

Source
closed?
Source
create_state

Creates an independent inference state for parallel transcription.

Each State can run inference concurrently on the same model context. The caller is responsible for closing the returned state.

Raises Whisper::Error if the context is closed or state creation fails.

Source
decode(tokens : Array(Int32), n_past : Int32, n_threads : Int32 = 4) : Int32

Runs the decoder on the given token context in the default state.

Call encode first. Returns 0 on success.

Source
detected_language

Returns the detected language code from the most recent transcription.

The language ID is retrieved from the default context state and converted to a BCP-47 string (e.g. "en", "de").

Source
encode(offset : Int32 = 0, n_threads : Int32 = 4) : Int32

Runs the encoder on the mel spectrogram in the default state.

Call pcm_to_mel or set_mel first. Returns 0 on success.

Source
finalize
Source
lang_auto_detect(offset_ms : Int32 = 0, n_threads : Int32 = 4) : Tuple(String, Hash(String, Float32))

Runs language auto-detection on the mel spectrogram and returns the detected language code along with a hash of all language probabilities.

Requires pcm_to_mel or equivalent to have been called first.

Source
logits

Returns a Slice of logits from the last decode call on the default state.

The slice contains n_tokens * n_vocab floats (row-major, last row = last token).

Source
mel_length

Returns the mel spectrogram length from the default state.

Source
model_info

Returns a snapshot of all model dimension queries.

Source
model_type

Returns a human-readable model type (e.g. "base", "small", "large").

Source
multilingual?

Returns true if the loaded model supports multiple languages. English-only models (e.g. ggml-base.en.bin) return false.

Source
n_audio_ctx

Returns the audio context size (frames).

Source
n_text_ctx

Returns the maximum text context size (tokens).

Source
n_vocab

Returns the model vocabulary size.

Source
pcm_to_mel(samples : Array(Float32), n_threads : Int32 = 4) : Int32

Converts raw PCM audio to mel spectrogram in the default state.

Returns 0 on success.

Source
reset_timings

Resets accumulated performance timings.

Source
set_mel(data : Array(Float32), n_len : Int32, n_mel : Int32 = 80) : Int32

Sets a custom mel spectrogram in the default state.

n_mel must be 80. Returns 0 on success.

Source
system_info

Returns a string describing the CPU features available for inference (e.g. AVX, NEON, Metal support).

Source
timings

Returns the performance timings from the default state.

Source
token_beg

Returns the beginning-of-timestamps token ID.

Source
token_count(text : String) : Int32

Returns the number of tokens the given text would produce.

Source
token_eot

Returns the end-of-text token ID.

Source
token_lang(lang_id : Int32) : Int32

Returns the language token ID for the given language ID.

Source
token_nosp

Returns the no-speech token ID.

Source
token_not

Returns the not token ID.

Source
token_prev

Returns the previous-token token ID.

Source
token_solm

Returns the start-of-language-model token ID.

Source
token_sot

Returns the start-of-text token ID.

Source
token_to_str(token : Int32) : String

Converts a token ID to its string representation.

Source
token_transcribe

Returns the transcribe task token ID.

Source
token_translate

Returns the translate task token ID.

Source
tokenize(text : String, max_tokens : Int32 = 512) : Array(Int32)

Tokenizes the given text into an array of token IDs.

Returns the token IDs produced by the model's tokenizer. Raises Whisper::Error if the text produces more tokens than max_tokens.

Source
transcribe(samples : Array(Float32), language : String | Nil = "en", n_threads : Int32 = 4, translate : Bool = false, token_timestamps : Bool = false, tdrz_enable : Bool = false) : Array(Segment)

Transcribes pre-loaded audio samples into text segments.

samples must be 32-bit float PCM audio normalized to [-1.0, 1.0], mono, at 16kHz. Use #transcribe_file to load and convert a WAV file automatically.

  • language: BCP-47 language code (e.g. "en", "es"), or nil for auto-detection.
  • n_threads: number of CPU threads for inference.
  • translate: when true, translates speech to English regardless of source language.
  • token_timestamps: when true, enables per-token timestamp computation.

Raises Whisper::Error if the context is closed or transcription fails.

Source
transcribe_file(path : String, **kwargs) : Array(Segment)

Transcribes a WAV file into text segments.

The file must be 16-bit signed PCM, mono, 16kHz. Convert other formats with:

ffmpeg -i input.mp3 -ar 16000 -ac 1 -f wav output.wav

Accepts the same keyword arguments as #transcribe.

Raises Whisper::Error if the file is missing or not a valid WAV.

Source
transcribe_parallel(samples : Array(Float32), n_processors : Int32 = 2, language : String | Nil = "en", n_threads : Int32 = 4, translate : Bool = false, token_timestamps : Bool = false, tdrz_enable : Bool = false) : Array(Segment)

Transcribes audio using multiple processors for potential speedup.

Splits input audio into chunks and processes each with whisper_full_with_state. Results are stored in the default context state. Not thread safe if called in parallel on the same context.

Accepts the same keyword arguments as #transcribe, plus n_processors.

Source
version

Returns the whisper.cpp library version string (e.g. "1.8.3").

Source

Nested types