Skip to content

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 audio_array is not a 1-D numpy array, or format is not supported.

Source code in src/mistral_common/tokens/tokenizers/audio.py
def __init__(self, audio_array: np.ndarray, sampling_rate: int, format: str) -> None:
    r"""Initialize an Audio instance with audio data, sampling rate, and format.

    Args:
        audio_array: The audio data as a 1-D numpy array of samples.
        sampling_rate: The sampling rate of the audio in Hz.
        format: The audio file format (e.g., "wav", "mp3"). Must be a
            format supported by soundfile.

    Raises:
        AssertionError: If `audio_array` is not a 1-D numpy array, or format
            is not supported.
    """
    self.audio_array = audio_array
    self.sampling_rate = sampling_rate
    self.format = format
    self._check_valid()

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 input_audio as str (base64) or bytes.

required

Returns:

Type Description
Audio

An instance of the Audio class.

Source code in src/mistral_common/tokens/tokenizers/audio.py
@staticmethod
def from_audio_chunk(chunk: "AudioChunk") -> "Audio":
    r"""Create an Audio instance from an AudioChunk.

    Args:
        chunk: An AudioChunk with `input_audio` as str (base64) or bytes.

    Returns:
        An instance of the Audio class.
    """
    input_audio = chunk.input_audio
    if isinstance(input_audio, bytes):
        return Audio.from_bytes(input_audio)
    elif isinstance(input_audio, str):
        return Audio.from_base64(input_audio)
    else:
        raise ValueError(f"Unsupported input_audio type: {type(input_audio)}")

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/;base64, URL prefix (stripped automatically).

required
strict bool

If True, strictly enforce mono audio; multi-channel audio raises an error. If False, extra channels are dropped.

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
@staticmethod
def from_base64(audio_base64: str, strict: bool = True) -> "Audio":
    r"""Create an Audio instance from a base64 encoded string.

    Args:
        audio_base64: The base64 encoded audio data, optionally prefixed
            with a data:audio/<format>;base64, URL prefix (stripped
            automatically).
        strict: If `True`, strictly enforce mono audio; multi-channel audio
            raises an error. If `False`, extra channels are dropped.

    Returns:
        An instance of the Audio class.

    Raises:
        ValueError: If the string is not valid base64 or decoding fails.
    """
    assert_soundfile_installed()

    if re.match(r"^data:audio/\w+;base64,", audio_base64):
        audio_base64 = audio_base64.split(",")[1]

    try:
        audio_bytes = base64.b64decode(audio_base64)
    except Exception as e:
        raise ValueError("base64 decoding failed. Please check the input string is a valid base64.") from e

    return Audio.from_bytes(audio_bytes, strict=strict)

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, strictly enforce mono audio; multi-channel audio raises an error. If False, extra channels are averaged down to mono.

True

Returns:

Type Description
Audio

An instance of the Audio class.

Raises:

Type Description
ValueError

If the audio is multi-channel and strict is True, or the bytes cannot be decoded.

Source code in src/mistral_common/tokens/tokenizers/audio.py
@staticmethod
def from_bytes(audio_bytes: bytes, strict: bool = True) -> "Audio":
    r"""Create an Audio instance from bytes.

    Args:
        audio_bytes: The audio data as bytes in a soundfile-readable
            format (e.g., wav, mp3).
        strict: If `True`, strictly enforce mono audio; multi-channel audio
            raises an error. If `False`, extra channels are averaged down
            to mono.

    Returns:
        An instance of the Audio class.

    Raises:
        ValueError: If the audio is multi-channel and strict is `True`, or
            the bytes cannot be decoded.
    """
    assert_soundfile_installed()

    with io.BytesIO(audio_bytes) as audio_file:
        with sf.SoundFile(audio_file) as f:
            audio_array = f.read(dtype="float32")
            sampling_rate = f.samplerate
            audio_format = f.format

    format_enum = AudioFormat(audio_format)
    format = format_enum.value.lower()

    if audio_array.ndim != 1:
        if strict:
            raise ValueError(f"{audio_array.ndim=}")
        else:
            audio_array = audio_array.mean(axis=1)

    return Audio(audio_array=audio_array, sampling_rate=sampling_rate, format=format)

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, strictly enforce mono audio; multi-channel audio raises an error. If False, extra channels are dropped.

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
@staticmethod
def from_file(file: str, strict: bool = True) -> "Audio":
    r"""Create an Audio instance from an audio file.

    Args:
        file: Path to the audio file. A file:// URI prefix is accepted
            and stripped.
        strict: If `True`, strictly enforce mono audio; multi-channel audio
            raises an error. If `False`, extra channels are dropped.

    Returns:
        An instance of the Audio class.

    Raises:
        FileNotFoundError: If the file does not exist.
    """
    assert_soundfile_installed()

    if isinstance(file, str) and file.startswith("file://"):
        file = file[7:]

    if not Path(file).exists():
        raise FileNotFoundError(f"{file=} does not exist")

    with open(file, "rb") as f:
        audio_bytes = f.read()

    return Audio.from_bytes(audio_bytes, strict=strict)

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
@staticmethod
def from_raw_audio(audio: "RawAudio") -> "Audio":
    r"""Create an Audio instance from a RawAudio object.

    Deprecated: Use `from_base64()` or `from_bytes()` instead. Will be removed in 1.13.0.

    Args:
        audio: The RawAudio object containing audio data.

    Returns:
        An instance of the Audio class.
    """

    warn_once(
        "Audio.from_raw_audio",
        "Audio.from_raw_audio() is deprecated. "
        "Use Audio.from_base64() or Audio.from_bytes() instead. "
        "Will be removed in 1.13.0.",
        DeprecationWarning,
        stacklevel=2,
    )
    if isinstance(audio.data, bytes):
        return Audio.from_bytes(audio.data)
    elif isinstance(audio.data, str):
        return Audio.from_base64(audio.data)
    else:
        raise ValueError(f"Unsupported audio data type: {type(audio.data)}")

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, strictly enforce mono audio; multi-channel audio raises an error. If False, extra channels are dropped.

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
@staticmethod
def from_url(url: str, strict: bool = True) -> "Audio":
    r"""Create an Audio instance from a URL.

    Downloads the audio file and decodes it into samples.

    Args:
        url: The URL of the audio file (http or https).
        strict: If `True`, strictly enforce mono audio; multi-channel audio
            raises an error. If `False`, extra channels are dropped.

    Returns:
        An instance of the Audio class.

    Raises:
        ValueError: If the download or decoding fails.
    """
    try:
        response = _requests_lib.get(url)
        response.raise_for_status()
        return Audio.from_bytes(response.content, strict=strict)
    except _requests_lib.RequestException as e:  # Something went wrong with the request.
        raise ValueError(f"Failed to download audio from URL: {url}") from e
    except Exception as e:  # Something went wrong with the audio file.
        raise ValueError(f"Failed to create Audio instance from URL: {url} .") from e

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
def resample(self, new_sampling_rate: int) -> None:
    r"""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.

    Args:
        new_sampling_rate: The new sampling rate in Hz.
    """
    if self.sampling_rate == new_sampling_rate:
        return

    assert_soxr_installed()

    self.audio_array = soxr.resample(self.audio_array, self.sampling_rate, new_sampling_rate, quality="HQ")
    self.sampling_rate = new_sampling_rate

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 True, prepend a data:audio/;base64, prefix to the output string.

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
def to_base64(self, format: str, prefix: bool = False) -> str:
    r"""Convert the audio data to a base64 encoded string.

    Args:
        format: The format to encode the audio in (e.g., "wav"). Must be
            supported by soundfile.
        prefix: If `True`, prepend a data:audio/<format>;base64, prefix to
            the output string.

    Returns:
        The base64 encoded audio data.

    Raises:
        AssertionError: If format is not supported by soundfile.
    """
    assert_soundfile_installed()

    assert format in EXPECTED_FORMAT_VALUES, f"{format=} not in {EXPECTED_FORMAT_VALUES=}"

    with io.BytesIO() as audio_file:
        sf.write(audio_file, self.audio_array, self.sampling_rate, format=format.upper())
        audio_file.seek(0)
        base64_str = base64.b64encode(audio_file.read()).decode("utf-8")
    if prefix:
        base64_str = f"data:audio/{format.lower()};base64,{base64_str}"
    return base64_str

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 None, no padding is applied.

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 None, the model's default is used.

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; None otherwise.

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. 1000 / frame_rate.

is_streaming property

Whether this config is configured for streaming transcription.

Returns:

Type Description
bool

True if transcription_format is TranscriptionFormat.STREAMING, False otherwise.

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 streaming_n_left_pad_tokens is None.

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 transcription_delay_ms.

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. sampling_rate // frame_rate.

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
def delay_len(self, transcription_delay_ms: float) -> int:
    r"""Convert a transcription delay into an audio length in samples.

    Args:
        transcription_delay_ms: The delay in milliseconds.

    Returns:
        The corresponding number of audio samples at this config's sampling rate.
    """
    return int(transcription_delay_ms / 1000.0 * self.sampling_rate)

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, falls back to the config's transcription_delay_ms.

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
def get_num_delay_tokens(self, transcription_delay_ms: float | None = None) -> int:
    r"""Compute the number of tokens covering the streaming transcription delay.

    Args:
        transcription_delay_ms: The delay in milliseconds. If `None`, falls back
            to the config's `transcription_delay_ms`.

    Returns:
        The number of audio tokens corresponding to the delay length.

    Raises:
        AssertionError: If not streaming, or no delay is set on either the
            argument or the config.
    """
    assert self.is_streaming, f"Can't call get_num_delay_tokens if {self.is_streaming=}."
    if transcription_delay_ms is None:
        transcription_delay_ms = self.transcription_delay_ms
    # streaming pad tokens
    assert transcription_delay_ms is not None, f"Can't call get_num_delay_tokens if {transcription_delay_ms=}."
    return self.num_audio_tokens(self.delay_len(transcription_delay_ms))

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, falls back to the config's transcription_delay_ms.

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
def n_right_pad_tokens(self, transcription_delay_ms: float | None = None) -> int:
    r"""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`).

    Args:
        transcription_delay_ms: The delay in milliseconds. If `None`, falls back
            to the config's `transcription_delay_ms`.

    Returns:
        The number of right padding tokens.

    Raises:
        AssertionError: If not streaming, or no delay is set on either the
            argument or the config.
    """
    assert self.is_streaming, f"Can't call n_right_pad_tokens if {self.is_streaming=}."
    # we need to pad on the right to ensure the models transcribes
    # - the induced delay on the prefill step (num_delay_tokens)
    # - the BOS token (1)
    # - a heuristic that defines a max token length for a single word
    #   (OFFLINE_STREAMING_BUFFER_TOKENS)
    return (self.get_num_delay_tokens(transcription_delay_ms) + 1) + OFFLINE_STREAMING_BUFFER_TOKENS

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
def num_audio_tokens(self, audio_len: int) -> int:
    r"""Convert an audio length in samples into the number of audio tokens.

    Args:
        audio_len: Audio length in samples. Rounded up to a whole number of
            frames when not a multiple of the hop length.

    Returns:
        The number of audio tokens covering the given length.
    """
    if audio_len % self.encoding_config.hop_length != 0:
        audio_len = math.ceil(audio_len / self.encoding_config.hop_length - 1)
    else:
        audio_len = audio_len // self.encoding_config.hop_length

    return math.ceil(audio_len / self.audio_length_per_tok)

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
def __init__(self, audio_config: AudioConfig, special_ids: SpecialAudioIDs) -> None:
    self.audio_config = audio_config
    self.encoding_config = audio_config.encoding_config
    self.special_ids = special_ids

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
def __call__(self, content: AudioChunk | AudioURLChunk) -> AudioEncoding:
    r"""Call the encoder on an audio chunk or URL chunk.

    Args:
        content: Audio or URL chunk to encode.

    Returns:
        Encoded audio data and tokens.
    """
    if isinstance(content, AudioURLChunk):
        return self._encode_audio_url_chunk(content)
    elif isinstance(content, AudioChunk):
        return self._encode_audio_chunk(content)
    else:
        raise ValueError(f"Unsupported content type: {type(content)}")

encode_audio(audio, transcription_delay_ms=None)

Encode an audio optionally with transcription delay.

Source code in src/mistral_common/tokens/tokenizers/audio.py
def encode_audio(self, audio: Audio, transcription_delay_ms: float | None = None) -> AudioEncoding:
    r"""Encode an audio optionally with transcription delay."""
    audio.resample(self.audio_config.sampling_rate)
    audio.audio_array = self.pad(audio.audio_array, self.audio_config.sampling_rate, transcription_delay_ms)

    if self.audio_config.transcription_format == TranscriptionFormat.STREAMING:
        tokens = self.encode_streaming_tokens(transcription_delay_ms)
    else:
        tokens = self._encode_audio_tokens(audio.audio_array.shape[0])

    return AudioEncoding(
        tokens=tokens,
        audio=audio,
    )

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 None to use a voice preset.

required
voice str | None

Preset voice name (e.g. 'Neutral Male', 'Neutral Female'), or None when using ref audio.

required

Returns:

Type Description
AudioEncoding

AudioEncoding containing the token sequence and optional audio data.

Source code in src/mistral_common/tokens/tokenizers/audio.py
def encode_audio_for_speech_request(self, audio: Audio | None, voice: str | None) -> AudioEncoding:
    r"""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.

    Args:
        audio: Reference audio waveform, or `None` to use a voice preset.
        voice: Preset voice name (e.g. 'Neutral Male', 'Neutral Female'), or `None` when using ref audio.

    Returns:
        AudioEncoding containing the token sequence and optional audio data.
    """
    assert audio is not None or voice is not None, (
        f"Either audio or voice must be defined to encode audio, got {audio=} and {voice=}"
    )

    if audio is not None:
        audio.resample(self.audio_config.sampling_rate)
        num_audio_tokens = self._get_num_audio_token_for_speech_request(len(audio.audio_array))
    else:
        assert self.audio_config.voice_num_audio_tokens is not None, (
            "voice_num_audio_tokens must be set in audio config to use voice-based speech requests"
        )
        assert voice is not None and voice in self.audio_config.voice_num_audio_tokens, (
            f"Unknown voice {voice!r}, expected one of {list(self.audio_config.voice_num_audio_tokens)}"
        )
        num_audio_tokens = self.audio_config.voice_num_audio_tokens[voice]
    tokens = self._encode_audio_tokens_for_speech_request(num_audio_tokens)

    return AudioEncoding(
        tokens=tokens,
        audio=audio,
    )

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
def encode_streaming_tokens(self, transcription_delay_ms: float | None = None) -> list[int]:
    r"""Encode the streaming tokens given a transcription delay."""
    assert isinstance(self.audio_config.encoding_config, AudioSpectrogramConfig), (
        f"Audio encoder must be spectrogram encoder, got {self.audio_config.encoding_config=}"
    )
    assert self.audio_config.transcription_delay_ms is not None

    # streaming pad tokens consist of silence we pad on left + delay tokens
    stream_pad_prefix_len = self.audio_config.n_left_pad_tokens + self.audio_config.get_num_delay_tokens(
        transcription_delay_ms
    )
    tokens = [self.streaming_pad] * stream_pad_prefix_len

    return tokens

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, falls back to the audio config's transcription_delay_ms.

None

Returns:

Type Description
tuple[Audio, Audio]

Tuple of (left_pad_audio, right_pad_audio) as silent Audio objects.

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
def get_padding_audio(self, transcription_delay_ms: float | None = None) -> tuple[Audio, Audio]:
    r"""Get left and right padding for realtime audio models.

    Args:
        transcription_delay_ms: Target delay in milliseconds between the audio
            and text streams for streaming transcription. Controls the padding
            length. If `None`, falls back to the audio config's
            `transcription_delay_ms`.

    Returns:
        Tuple of (left_pad_audio, right_pad_audio) as silent `Audio` objects.

    Raises:
        AssertionError: If no delay is set on either the argument or the
            audio config.
    """

    left_pad, right_pad = self._get_streaming_pad(0, transcription_delay_ms)
    left_pad_audio = Audio(
        audio_array=np.zeros(left_pad, dtype=np.float32),
        sampling_rate=self.audio_config.sampling_rate,
        format="wav",
    )
    right_pad_audio = Audio(
        audio_array=np.zeros(right_pad, dtype=np.float32),
        sampling_rate=self.audio_config.sampling_rate,
        format="wav",
    )
    return left_pad_audio, right_pad_audio

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
def next_multiple_of_chunk_frames(self, audio_array_len: int, sampling_rate: int) -> int:
    r"""Calculate the next multiple of chunk frames.

    Args:
        audio_array_len: Length of the audio array.
        sampling_rate: Sampling rate of the audio.

    Returns:
        The next multiple of chunk frames.
    """
    assert sampling_rate == self.audio_config.sampling_rate, (
        f"Expected {sampling_rate=} to be {self.audio_config.sampling_rate=}"
    )
    assert self.audio_config.chunk_length_s is not None, (
        f"Can't call next_multiple_of_chunk_frames if {self.audio_config.chunk_length_s=}."
    )

    return math.ceil(audio_array_len / self.audio_config.chunk_frames) * self.audio_config.chunk_frames

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, falls back to the audio config's transcription_delay_ms.

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
def pad(
    self,
    audio_array: np.ndarray,
    sampling_rate: int,
    transcription_delay_ms: float | None = None,
    **kwargs: Any,
) -> np.ndarray:
    r"""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.

    Args:
        audio_array: Audio data as a numpy array of samples.
        sampling_rate: Sampling rate of the audio in Hz.
        transcription_delay_ms: 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`, falls back to the audio config's `transcription_delay_ms`.
        **kwargs: Ignored. Only present to swallow deprecated keyword arguments
            from callers; will be removed in 1.13.0.

    Returns:
        The padded audio array.

    Raises:
        AssertionError: If streaming and no delay is set on either the argument
            or the audio config.
    """
    # TODO(Patrick) - remove **kwargs as it's just there to swallow deprecated
    # keyword args from voxtral_realtime in vLLM. It was
    # relevant for the release. Remove in mistral_common version 1.13.0
    if self.audio_config.chunk_length_s:
        next_multiple_of_chunk_frames = self.next_multiple_of_chunk_frames(audio_array.shape[-1], sampling_rate)
        audio_array = np.pad(audio_array, (0, next_multiple_of_chunk_frames - audio_array.shape[-1]))
    elif self.audio_config.is_streaming:
        left_pad, right_pad = self._get_streaming_pad(audio_array.shape[-1], transcription_delay_ms)
        # we pad both left & right as this leads to better performance
        audio_array = np.pad(audio_array, (left_pad, right_pad))
    elif (
        isinstance(self.encoding_config, AudioSpectrogramConfig)
        and audio_array.shape[-1] < self.encoding_config.window_size
    ):
        # minimum length for audios is at least one spectrogram frame
        audio_array = np.pad(audio_array, (0, self.encoding_config.window_size - audio_array.shape[-1]))

    return audio_array

AudioEncoding(tokens, audio) dataclass

Encapsulates the tokens and audio data for an audio chunk.

Attributes:

Name Type Description
tokens list[int]

Text tokens corresponding to this audio chunk.

audio Audio | None

Original audio waveform data, or None when using a preset voice (no reference audio to forward to the model).

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.

TranscriptionFormat

Bases: str, Enum

Transcription format.

Should be set by the tokenizer for correct encoding.

Attributes:

Name Type Description
INSTRUCT

Encode whole utterances at once.

STREAMING

Encode audio incrementally with left and right padding.