Skip to content

Audio

AudioAssetParams

Bases: BaseModel

Represents the parameters for an Audio assets.

volume class-attribute instance-attribute

volume: float = Field(default=1.0)

The volume of the audio assets.

crop class-attribute instance-attribute

crop: tuple[int, int] | None = None

Crop range for the audio assets

AudioAsset

Bases: BaseAsset[AudioAssetParams]

Represents an Audio asset with various properties.

type class-attribute instance-attribute

type: Literal['audio'] = 'audio'

The type of the asset. Defaults to "audio".

params class-attribute instance-attribute

params: AudioAssetParams = Field(
    default_factory=AudioAssetParams
)

The parameters for the asset.

duration instance-attribute

duration: PositiveFloat

The duration of the audio asset.

sample_rate instance-attribute

sample_rate: PositiveFloat

The sample rate of the audio asset.

sample_width instance-attribute

sample_width: NonNegativeInt

The sample width of the audio asset.

channels instance-attribute

channels: int

The number of channels in the audio asset.

from_data classmethod

from_data(
    data: str | bytes,
    *,
    path: PathLike | None = None,
    metadata: dict | None = None,
    mime_type: str | None = None,
    **kwargs: Any
) -> Self

Creates an audio asset from data.

Parameters:

Name Type Description Default
data str | bytes

The data of the assets.

required
path PathLike | None

The path to the file.

None
metadata dict | None

The metadata of the assets.

None
mime_type str | None

The MIME type of the assets.

None
kwargs Any

Additional keyword arguments to the constructor.

{}

Returns:

Type Description
Self

The assets.

Source code in src/mosaico/assets/audio.py
@classmethod
def from_data(
    cls,
    data: str | bytes,
    *,
    path: PathLike | None = None,
    metadata: dict | None = None,
    mime_type: str | None = None,
    **kwargs: Any,
) -> Self:
    """
    Creates an audio asset from data.

    :param data: The data of the assets.
    :param path: The path to the file.
    :param metadata: The metadata of the assets.
    :param mime_type: The MIME type of the assets.
    :param kwargs: Additional keyword arguments to the constructor.
    :return: The assets.
    """
    if not check_user_provided_required_keys(kwargs, ["duration", "sample_rate", "sample_width", "channels"]):
        audio_info = _extract_audio_info(data)
        kwargs.update(audio_info)

    return super().from_data(data, path=path, metadata=metadata, mime_type=mime_type, **kwargs)

from_path classmethod

from_path(
    path: PathLike,
    *,
    encoding: str = "utf-8",
    mime_type: str | None = None,
    guess_mime_type: bool = True,
    metadata: dict | None = None,
    **kwargs: Any
) -> Self

Creates an audio asset from a file path.

Parameters:

Name Type Description Default
path PathLike

The path to the file.

required
encoding str

The encoding of the file.

'utf-8'
metadata dict | None

The metadata of the assets.

None
mime_type str | None

The MIME type of the assets.

None
guess_mime_type bool

Whether to guess the MIME type.

True
kwargs Any

Additional keyword arguments to the constructor.

{}

Returns:

Type Description
Self

The assets.

Source code in src/mosaico/assets/audio.py
@classmethod
def from_path(
    cls,
    path: PathLike,
    *,
    encoding: str = "utf-8",
    mime_type: str | None = None,
    guess_mime_type: bool = True,
    metadata: dict | None = None,
    **kwargs: Any,
) -> Self:
    """
    Creates an audio asset from a file path.

    :param path: The path to the file.
    :param encoding: The encoding of the file.
    :param metadata: The metadata of the assets.
    :param mime_type: The MIME type of the assets.
    :param guess_mime_type: Whether to guess the MIME type.
    :param kwargs: Additional keyword arguments to the constructor.
    :return: The assets.
    """
    if not check_user_provided_required_keys(kwargs, ["duration", "sample_rate", "sample_width", "channels"]):
        audio_info = _extract_audio_info(path)
        kwargs.update(audio_info)

    return super().from_path(
        path, encoding=encoding, mime_type=mime_type, guess_mime_type=guess_mime_type, metadata=metadata, **kwargs
    )

slice

slice(start_time: float, end_time: float) -> AudioAsset

Slices the audio asset.

Parameters:

Name Type Description Default
start_time float

The start time in seconds.

required
end_time float

The end time in seconds.

required

Returns:

Type Description
AudioAsset

The sliced audio asset.

Source code in src/mosaico/assets/audio.py
def slice(self, start_time: float, end_time: float) -> AudioAsset:
    """
    Slices the audio asset.

    :param start_time: The start time in seconds.
    :param end_time: The end time in seconds.
    :return: The sliced audio asset.
    """
    with self.to_bytes_io() as audio_file:
        audio = AudioSegment.from_file(
            file=audio_file,
            sample_width=self.sample_width,
            frame_rate=self.sample_rate,
            channels=self.channels,
        )

        sliced_buf = io.BytesIO()
        sliced_audio = audio[round(start_time * 1000) : round(end_time * 1000)]
        sliced_audio.export(sliced_buf, format="mp3")
        sliced_buf.seek(0)

        return AudioAsset.from_data(
            sliced_buf.read(),
            duration=audio.duration_seconds,
            sample_rate=self.sample_rate,
            sample_width=self.sample_width,
            channels=self.channels,
        )