mistral_common.tokens.tokenizers.audio
Audio(audio_array, sampling_rate, format)
Loaded audio samples with format metadata.
Holds a decoded 1-D sample array, its sampling rate, and source format. Build instances via the from_url, from_base64, from_file, or from_bytes constructors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_array
|
ndarray
|
The audio data as a 1-D numpy array of samples. |
required |
sampling_rate
|
int
|
The sampling rate of the audio in Hz. |
required |
format
|
str
|
The audio file format (e.g., "wav", "mp3"). Must be a format supported by soundfile. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/mistral_common/tokens/tokenizers/audio.py
duration
property
Calculate the duration of the audio in seconds.
Returns:
| Type | Description |
|---|---|
float
|
The duration of the audio in seconds. |
from_audio_chunk(chunk)
staticmethod
Create an Audio instance from an AudioChunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
AudioChunk
|
An AudioChunk with |
required |
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
from_base64(audio_base64, strict=True)
staticmethod
Create an Audio instance from a base64 encoded string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_base64
|
str
|
The base64 encoded audio data, optionally prefixed
with a data:audio/ |
required |
strict
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string is not valid base64 or decoding fails. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
from_bytes(audio_bytes, strict=True)
staticmethod
Create an Audio instance from bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_bytes
|
bytes
|
The audio data as bytes in a soundfile-readable format (e.g., wav, mp3). |
required |
strict
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the audio is multi-channel and strict is |
Source code in src/mistral_common/tokens/tokenizers/audio.py
from_file(file, strict=True)
staticmethod
Create an Audio instance from an audio file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Path to the audio file. A file:// URI prefix is accepted and stripped. |
required |
strict
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
from_raw_audio(audio)
staticmethod
Create an Audio instance from a RawAudio object.
Deprecated: Use from_base64() or from_bytes() instead. Will be removed in 1.13.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
RawAudio
|
The RawAudio object containing audio data. |
required |
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
from_url(url, strict=True)
staticmethod
Create an Audio instance from a URL.
Downloads the audio file and decodes it into samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the audio file (http or https). |
required |
strict
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Audio
|
An instance of the Audio class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the download or decoding fails. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
resample(new_sampling_rate)
Resample audio data to a new sampling rate.
Mutates this instance's audio_array and sampling_rate in place. No-op
if the sampling rate is already the target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_sampling_rate
|
int
|
The new sampling rate in Hz. |
required |
Source code in src/mistral_common/tokens/tokenizers/audio.py
to_base64(format, prefix=False)
Convert the audio data to a base64 encoded string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
The format to encode the audio in (e.g., "wav"). Must be supported by soundfile. |
required |
prefix
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The base64 encoded audio data. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If format is not supported by soundfile. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
AudioConfig(sampling_rate, frame_rate, encoding_config, chunk_length_s=None, transcription_format=TranscriptionFormat.INSTRUCT, transcription_delay_ms=None, streaming_look_ahead_ms=None, streaming_look_back_ms=None, streaming_n_left_pad_tokens=None, voice_num_audio_tokens=None)
dataclass
Configuration for audio processing.
Attributes:
| Name | Type | Description |
|---|---|---|
sampling_rate |
int
|
Sampling rate of the audio in Hz. |
frame_rate |
float
|
Number of frames per second accepted by the tokenizer model. |
encoding_config |
AudioSpectrogramConfig
|
Configuration for the audio spectrogram. |
chunk_length_s |
float | None
|
If set, audio is padded into multiples of this many
seconds. If |
transcription_format |
TranscriptionFormat
|
INSTRUCT for encoding whole utterances, STREAMING for streaming transcription. |
transcription_delay_ms |
float | None
|
Target delay in milliseconds between the audio
stream and text stream for streaming transcription. If |
voice_num_audio_tokens |
dict[str, int] | None
|
Mapping from speaker voice name to the number
of audio tokens for that speaker's reference audio. Only used for
text-to-speech; |
audio_length_per_tok
property
Calculate the length of audio per token.
chunk_frames
property
Calculate the number of frames per chunk.
frame_duration_ms
property
Duration of a single audio frame in milliseconds.
Returns:
| Type | Description |
|---|---|
float
|
The milliseconds per frame, i.e. |
is_streaming
property
Whether this config is configured for streaming transcription.
Returns:
| Type | Description |
|---|---|
bool
|
True if |
n_left_pad_tokens
property
Number of left padding tokens to add before the audio.
Left padding has shown to improve performance by giving the model more
compute. Requires streaming_n_left_pad_tokens to be set.
Returns:
| Type | Description |
|---|---|
int
|
The number of left padding tokens. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If not streaming or |
num_delay_tokens
property
Deprecated in favor of get_num_delay_tokens. Will be removed in 1.13.0.
Returns:
| Type | Description |
|---|---|
int
|
The number of delay tokens for this config's |
raw_audio_length_per_tok
property
Number of audio samples covered by one audio token.
Returns:
| Type | Description |
|---|---|
int
|
The samples per token, i.e. |
delay_len(transcription_delay_ms)
Convert a transcription delay into an audio length in samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transcription_delay_ms
|
float
|
The delay in milliseconds. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The corresponding number of audio samples at this config's sampling rate. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
get_num_delay_tokens(transcription_delay_ms=None)
Compute the number of tokens covering the streaming transcription delay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transcription_delay_ms
|
float | None
|
The delay in milliseconds. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of audio tokens corresponding to the delay length. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If not streaming, or no delay is set on either the argument or the config. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
n_right_pad_tokens(transcription_delay_ms=None)
Number of right padding tokens to add after the audio.
Covers the induced delay, the BOS token, and a buffer for long words
(see OFFLINE_STREAMING_BUFFER_TOKENS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transcription_delay_ms
|
float | None
|
The delay in milliseconds. If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of right padding tokens. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If not streaming, or no delay is set on either the argument or the config. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
num_audio_tokens(audio_len)
Convert an audio length in samples into the number of audio tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_len
|
int
|
Audio length in samples. Rounded up to a whole number of frames when not a multiple of the hop length. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of audio tokens covering the given length. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
AudioEncoder(audio_config, special_ids)
Encodes audio chunks into a format suitable for further processing.
Attributes:
| Name | Type | Description |
|---|---|---|
audio_config |
Configuration for audio processing. |
|
encoding_config |
Configuration for audio spectrogram. |
|
special_ids |
Special tokens for audio encoding. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
audio_to_text_token
property
Get the audio_to_text token.
audio_token
property
Get the audio token.
begin_audio_token
property
Get the begin audio token.
streaming_pad
property
Get the streaming pad token.
text_to_audio_token
property
Get the text_to_audio token.
__call__(content)
Call the encoder on an audio chunk or URL chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
AudioChunk | AudioURLChunk
|
Audio or URL chunk to encode. |
required |
Returns:
| Type | Description |
|---|---|
AudioEncoding
|
Encoded audio data and tokens. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
encode_audio(audio, transcription_delay_ms=None)
Encode an audio optionally with transcription delay.
Source code in src/mistral_common/tokens/tokenizers/audio.py
encode_audio_for_speech_request(audio, voice)
Encode audio or voice preset into an AudioEncoding for speech synthesis.
Either audio (reference audio for voice cloning) or voice (preset name)
must be provided. When audio is given it takes precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Audio | None
|
Reference audio waveform, or |
required |
voice
|
str | None
|
Preset voice name (e.g. 'Neutral Male', 'Neutral Female'), or |
required |
Returns:
| Type | Description |
|---|---|
AudioEncoding
|
AudioEncoding containing the token sequence and optional audio data. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
encode_streaming_tokens(transcription_delay_ms=None)
Encode the streaming tokens given a transcription delay.
Source code in src/mistral_common/tokens/tokenizers/audio.py
get_padding_audio(transcription_delay_ms=None)
Get left and right padding for realtime audio models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transcription_delay_ms
|
float | None
|
Target delay in milliseconds between the audio
and text streams for streaming transcription. Controls the padding
length. If |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Audio, Audio]
|
Tuple of (left_pad_audio, right_pad_audio) as silent |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If no delay is set on either the argument or the audio config. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
next_multiple_of_chunk_frames(audio_array_len, sampling_rate)
Calculate the next multiple of chunk frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_array_len
|
int
|
Length of the audio array. |
required |
sampling_rate
|
int
|
Sampling rate of the audio. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The next multiple of chunk frames. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
pad(audio_array, sampling_rate, transcription_delay_ms=None, **kwargs)
Pad the audio array to the desired length.
Depending on the audio config, pads the audio to a multiple of the chunk length, to the streaming length (both left and right), or to at least one spectrogram frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_array
|
ndarray
|
Audio data as a numpy array of samples. |
required |
sampling_rate
|
int
|
Sampling rate of the audio in Hz. |
required |
transcription_delay_ms
|
float | None
|
Target delay in milliseconds between the audio
and text streams for streaming transcription. Controls how much
right padding is added so the model transcribes the induced delay.
If |
None
|
**kwargs
|
Any
|
Ignored. Only present to swallow deprecated keyword arguments from callers; will be removed in 1.13.0. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The padded audio array. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If streaming and no delay is set on either the argument or the audio config. |
Source code in src/mistral_common/tokens/tokenizers/audio.py
AudioEncoding(tokens, audio)
dataclass
AudioFormat
Bases: Enum
Dynamic enum whose members depend on soundfile availability at runtime.
AudioSpectrogramConfig(num_mel_bins, hop_length, window_size)
dataclass
Configuration for generating an audio spectrogram.
Attributes:
| Name | Type | Description |
|---|---|---|
num_mel_bins |
int
|
Number of mel bins, typically 80 or 128. |
hop_length |
int
|
Length of the overlapping windows for the STFT used to obtain the Mel Frequency coefficients, typically 160. |
window_size |
int
|
Window size of the Fourier transform, typically 400. |
SpecialAudioIDs(audio, begin_audio, streaming_pad, text_to_audio, audio_to_text)
dataclass
Special text tokens corresponding to audio token sequence.
Attributes:
| Name | Type | Description |
|---|---|---|
audio |
int | None
|
Token representing audio. |
begin_audio |
int | None
|
Token representing the beginning of audio. |
streaming_pad |
int | None
|
Token representing streaming pad of audio. Only relevant for steaming models. |
text_to_audio |
int | None
|
Token representing intent to convert text to audio. |
audio_to_text |
int | None
|
Token representing intent to convert audio to text. |