whisper
whisper transcribes speech with whisper.cpp, a C and C++ engine for Whisper models. It reads 16 kHz WAV files and returns the text, with timed segments if you ask for them.
Example #
Load a model, read a 16 kHz mono WAV file, and print the text and the timed segments. The paths are placeholders:
import Whisper from "pkg/whisper/whisper"
def main() -> Void
model = Whisper.load_model("models/ggml-small.bin").unwrap!
pcm = Whisper.load("clip.wav").unwrap!
text = Whisper.transcribe(model, pcm).unwrap!
puts(text)
options = Whisper.Options(language: "es")
segments = Whisper.transcribe_with(model, Whisper.widen(pcm), options).unwrap!
for segment in segments
puts("#{segment.start_ms} ms to #{segment.end_ms} ms: #{segment.text}")
end
endSetup #
whisper is a project of its own, not a member of the workspace of the other packages. It needs a C or C++ compiler and cmake (a build tool) to build the engine. The engine is whisper.cpp, a pinned git submodule.
It also needs a ggml model file. tools/fetch-model.sh downloads one.
Main API #
load_model(path)loads a ggml or gguf model (the model file formats of whisper.cpp) into aModel. The path is at most 4096 bytes.Whisper.load(path)reads a WAV file as aList[Float32]of 16 kHz mono samples, 4 bytes each. Only PCM, mono, 16 kHz and 16-bit files are read.Whisper.decode(data)reads WAV bytes that are already in memory, by the same rules.transcribe(model, pcm)takes thatList[Float32]and gives the text of one decode with the default options. It widens the whole clip for the length of the call (8 bytes a sample), so for a long clip useStream.transcribe_stream, which widens one window at a time. A decode is one run of the model over the audio.transcribe_with(model, pcm, options)takes aList[Float], because a native module cannot take aFloat32:Whisper.widen(pcm)makes one from the samples ofload, as a copy of 8 bytes a sample for the length of the call. It gives the timedSegments of one decode, in time order. ASegmenthasstart_ms,end_msandtext.transcribe_detailed(model, pcm, options)takes aList[Float]astranscribe_withdoes and gives aTranscript: the language, the segments with their tokens, and the detected language when asked for.join_text(segments)joins the texts of segments with single spaces.language_name(code)gives the English name of a language code, such as"spanish", or none for a code the engine does not know.
Whisper.Options has these fields:
language, default"", which means English."auto"detects the language, and a code such as"es"pins it.translate, defaultfalse: answer in English.beam_size, default0. From 2 to 8 it decodes with beam search, which keeps several candidate outputs.0and1decode greedily, taking the most likely token at each step.token_timestamps, defaultfalse: times each token.max_len, default0: withtoken_timestamps, cuts the segments to at most this many characters.language_probabilities, defaultfalse: fills the detection with the probability of every language.
Whisper.Transcript has language (the code decoded), segments (a list of SegmentDetail) and detection, which is some(Detection) when language_probabilities asked for it on a multilingual model, and none otherwise.
SegmentDetailhasstart_ms,end_ms,text(not trimmed),tokens(a list ofToken),avg_logprob(the sum of the tokens' log probabilities over their number, special tokens included) andno_speech_probability.Tokenhasids,text,probability(of its first token),log_probability(the sum over its tokens), andstart_msandend_ms, which are -10 unlesstoken_timestampsis on.Detectionhaslanguage, itsprobability, andprobabilities, a map from every code to its probability.
Long audio and the microphone #
The stream module (pkg/whisper/stream) transcribes audio of any length, in windows:
Stream.transcribe_stream(model, source, options)reads aMic.Source, checks the walk, decodes each window with speech and merges the segments onto one timeline, and gives aList[Whisper.Segment]. Its times count from the first sample. Audio with no speech gives no segments, and no samples at all is an error.Stream.default_options()gives aStreamOptionswithwindow_s30,overlap_s5, the defaultvadand the defaultWhisper.Options.window_sis from 1 to 300 seconds, andoverlap_sis from 0 towindow_sminus 1.Stream.check_options(options)checks these,frame_ms(1..1000) andthreshold(0.0..1.0), without audio or a model.VadOptionshasframe_ms(30) andthreshold(0.02). A window is kept when one frame's RMS reaches the threshold.threshold: 0.0keeps every window.windows,offset_segments,merge_segments,has_speechandframe_rmsare the pure steps of the walk. Tests run them without a model.
The mic module (pkg/whisper/mic) captures audio:
Mic.SourceisSamples(list), for PCM that is already there, orCommand(capture), for a live capture.Mic.Capturehascommand("arecord"),args,timeout_ms(30 000) andmax_samples(160 000, ten seconds).Mic.default_capture()gives thearecordcapture of 16 kHz mono 16-bit PCM.Mic.capture(spec)runs the command and gives its samples as aList[Float32]. It is an error when the command cannot start, a read times out, the command dies mid-capture, or no audio comes.Mic.read(source)gives the samples of aMic.Source, andMic.decode_s16le(data)decodes raw 16-bit PCM bytes.
Limits #
- One decode takes 1 to 4 800 000 samples, which is five minutes of 16 kHz audio. Longer input, empty input and non-finite samples are errors.
- The language code is at most 32 bytes.
beam_sizeis from 0 to 8, andmax_lenis from 0 to 100 000. - The text of one call is at most one megabyte.
- A model runs one decode at a time. A mutex makes other calls wait.
- There are no partial results. Each decode returns its segments when it ends.
- The package reads WAV files only at the rate and format above. It does not resample (change the sample rate).
- Microphone capture needs
arecord, the Linux recording tool, and a device. It reads 16 kHz mono audio. Nothing else in the package does.
How it works #
The decode runs in the compute pool, a set of background threads. It uses the GPU when the engine was built with Vulkan, and the CPU otherwise.
- Guide whisper