Datachain

AudioFile

AudioFile extends File and provides additional methods for working with audio files.

AudioFile instances are created when a DataChain is initialized from storage with the type="audio" parameter:

import datachain as dc

chain = dc.read_storage("s3://bucket-name/", type="audio")

There are additional models for working with audio files:

  • AudioFragment - represents a fragment of an audio file.

These are virtual models that do not create physical files. Instead, they are used to represent the data in the AudioFile these models are referring to. If you need to save the data, you can use the save method of these models, allowing you to save data locally or upload it to a storage service.

For a complete example of audio processing with DataChain, see Audio-to-Text with Whisper - a speech recognition pipeline that uses AudioFile, AudioFragment, and Audio to chunk audio files and transcribe them.

AudioFile

AudioFile(**kwargs)

Bases: File

A data model for handling audio files.

This model inherits from the File model and provides additional functionality for reading audio files, extracting audio fragments, and splitting audio into fragments.

Source code in datachain/lib/file.py
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def __init__(self, **kwargs):
    super().__init__(**kwargs)
    # Annotations only: in the class body the pydantic mypy plugin would take
    # them for fields.
    self._caching_enabled: bool
    self._download_cb: Callback
    self._fs_path_cache: tuple[str, str, str] | None
    # The runtime state, set in one __dict__ update: pydantic's __setattr__ per
    # attribute is slow when millions of Files are built. A subclass that
    # declares them as private attributes needs pydantic's setattr instead.
    if _FILE_STATE.keys() & self.__private_attributes__.keys():
        for name, value in _FILE_STATE.items():
            setattr(self, name, value)
    else:
        self.__dict__.update(_FILE_STATE)

get_fragment

get_fragment(start: float, end: float) -> AudioFragment

Returns an audio fragment from the specified time range. It does not download the file, neither it actually extracts the fragment. It returns a Model representing the audio fragment, which can be used to read or save it later.

Parameters:

  • start (float) –

    The start time of the fragment in seconds.

  • end (float) –

    The end time of the fragment in seconds.

Returns:

  • AudioFragment ( AudioFragment ) –

    A Model representing the audio fragment.

Source code in datachain/lib/file.py
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
def get_fragment(self, start: float, end: float) -> "AudioFragment":
    """
    Returns an audio fragment from the specified time range. It does not
    download the file, neither it actually extracts the fragment. It returns
    a Model representing the audio fragment, which can be used to read or save
    it later.

    Args:
        start (float): The start time of the fragment in seconds.
        end (float): The end time of the fragment in seconds.

    Returns:
        AudioFragment: A Model representing the audio fragment.
    """
    if start < 0 or end < 0 or start >= end:
        raise ValueError(
            f"Can't get audio fragment for '{self.path}', "
            f"invalid time range: ({start:.3f}, {end:.3f})"
        )

    return AudioFragment(audio=self, start=start, end=end)

get_fragments

get_fragments(duration: float, start: float = 0, end: float | None = None) -> Iterator[AudioFragment]

Splits the audio into multiple fragments of a specified duration.

Parameters:

  • duration (float) –

    The duration of each audio fragment in seconds.

  • start (float, default: 0 ) –

    The starting time in seconds (default: 0).

  • end (float, default: None ) –

    The ending time in seconds. If None, the entire remaining audio is processed (default: None).

Returns:

Note

If end is not specified, number of samples will be taken from the audio file, this means audio metadata needs to be read.

Source code in datachain/lib/file.py
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
def get_fragments(
    self,
    duration: float,
    start: float = 0,
    end: float | None = None,
) -> "Iterator[AudioFragment]":
    """
    Splits the audio into multiple fragments of a specified duration.

    Args:
        duration (float): The duration of each audio fragment in seconds.
        start (float): The starting time in seconds (default: 0).
        end (float, optional): The ending time in seconds. If None, the entire
                               remaining audio is processed (default: None).

    Returns:
        Iterator[AudioFragment]: An iterator yielding audio fragments.

    Note:
        If end is not specified, number of samples will be taken from the
        audio file, this means audio metadata needs to be read.
    """
    if duration <= 0:
        raise ValueError("duration must be a positive float")
    if start < 0:
        raise ValueError("start must be a non-negative float")

    if end is None:
        end = self.get_info().duration

    if end < 0:
        raise ValueError("end must be a non-negative float")
    if start >= end:
        raise ValueError("start must be less than end")

    while start < end:
        yield self.get_fragment(start, min(start + duration, end))
        start += duration

get_info

get_info() -> Audio

Retrieves metadata and information about the audio file.

Reuses cached content when available without populating the cache. Use .settings(prefetch=0) for metadata-only chains to avoid downloading files in advance.

Returns:

  • Audio ( Audio ) –

    A Model containing audio metadata such as duration, sample rate, channels, and codec details.

Source code in datachain/lib/file.py
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
def get_info(self) -> "Audio":
    """
    Retrieves metadata and information about the audio file.

    Reuses cached content when available without populating the cache.
    Use `.settings(prefetch=0)` for metadata-only chains to avoid downloading
    files in advance.

    Returns:
        Audio: A Model containing audio metadata such as duration,
               sample rate, channels, and codec details.
    """
    from .audio import audio_info

    return audio_info(self)

save

save(destination: str, format: str | None = None, start: float = 0, end: float | None = None, client_config: dict | None = None, *, content_type: str | None = None, content_disposition: str | None = None, cache_control: str | None = None, content_encoding: str | None = None, metadata: dict[str, str] | None = None, write_options: dict[str, Any] | None = None) -> AudioFile

Save audio file or extract fragment to specified format.

If destination is a remote path, the audio file will be uploaded to remote storage.

Parameters:

  • destination (str) –

    Output directory path or URI (e.g. s3://…, gs://…).

  • format (str | None, default: None ) –

    Output format ('wav', 'mp3', etc). Defaults to source format.

  • start (float, default: 0 ) –

    Start time in seconds (>= 0). Defaults to 0.

  • end (float | None, default: None ) –

    End time in seconds. If None, extracts to end of file.

  • client_config (dict | None, default: None ) –

    Optional client configuration.

  • content_type (str | None, default: None ) –

    Content-Type to set on the written object.

  • content_disposition (str | None, default: None ) –

    Content-Disposition to set on the object.

  • cache_control (str | None, default: None ) –

    Cache-Control to set on the object.

  • content_encoding (str | None, default: None ) –

    Content-Encoding to set on the object.

  • metadata (dict[str, str] | None, default: None ) –

    Custom key/value metadata to attach to the object.

  • write_options (dict[str, Any] | None, default: None ) –

    Raw, backend-native write kwargs merged after the normalized fields above.

Returns:

  • AudioFile ( AudioFile ) –

    New audio file with format conversion/extraction applied.

Examples:

audio.save("/path", "mp3") # Entire file to MP3 audio.save("s3://bucket/path", "wav", start=2.5) # From 2.5s to end as WAV audio.save("/path", "flac", start=1, end=3) # 1-3s fragment as FLAC

Source code in datachain/lib/file.py
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
def save(  # type: ignore[override]
    self,
    destination: str,
    format: str | None = None,
    start: float = 0,
    end: float | None = None,
    client_config: dict | None = None,
    *,
    content_type: str | None = None,
    content_disposition: str | None = None,
    cache_control: str | None = None,
    content_encoding: str | None = None,
    metadata: dict[str, str] | None = None,
    write_options: dict[str, Any] | None = None,
) -> "AudioFile":
    """Save audio file or extract fragment to specified format.

    If ``destination`` is a remote path, the audio file will be uploaded
    to remote storage.

    Args:
        destination: Output directory path or URI (e.g. ``s3://…``, ``gs://…``).
        format: Output format ('wav', 'mp3', etc). Defaults to source format.
        start: Start time in seconds (>= 0). Defaults to 0.
        end: End time in seconds. If None, extracts to end of file.
        client_config: Optional client configuration.
        content_type: ``Content-Type`` to set on the written object.
        content_disposition: ``Content-Disposition`` to set on the object.
        cache_control: ``Cache-Control`` to set on the object.
        content_encoding: ``Content-Encoding`` to set on the object.
        metadata: Custom key/value metadata to attach to the object.
        write_options: Raw, backend-native write kwargs merged after the
            normalized fields above.

    Returns:
        AudioFile: New audio file with format conversion/extraction applied.

    Examples:
        audio.save("/path", "mp3")                        # Entire file to MP3
        audio.save("s3://bucket/path", "wav", start=2.5)  # From 2.5s to end as WAV
        audio.save("/path", "flac", start=1, end=3)       # 1-3s fragment as FLAC
    """
    if self._catalog is None:
        raise RuntimeError("Cannot save file: catalog is not set")

    from .audio import save_audio

    return save_audio(
        self,
        destination,
        format,
        start,
        end,
        client_config,
        write_config=WriteConfig(
            content_type=content_type,
            content_disposition=content_disposition,
            cache_control=cache_control,
            content_encoding=content_encoding,
            metadata=metadata,
            write_options=write_options,
        ),
    )

AudioFragment

Bases: DataModel

A data model for representing an audio fragment.

This model represents a specific fragment within an audio file with defined start and end times. It allows access to individual fragments and provides functionality for reading and saving audio fragments as separate audio files.

Attributes:

  • audio (AudioFile) –

    The audio file containing the audio fragment.

  • start (float) –

    The starting time of the audio fragment in seconds.

  • end (float) –

    The ending time of the audio fragment in seconds.

get_np

get_np() -> tuple[ndarray, int]

Returns the audio fragment as a NumPy array with sample rate.

Returns:

  • tuple[ndarray, int] –

    tuple[ndarray, int]: A tuple containing the audio data as a NumPy array and the sample rate.

Source code in datachain/lib/file.py
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
def get_np(self) -> tuple["ndarray", int]:
    """
    Returns the audio fragment as a NumPy array with sample rate.

    Returns:
        tuple[ndarray, int]: A tuple containing the audio data as a NumPy array
                           and the sample rate.
    """
    from .audio import audio_to_np

    duration = self.end - self.start
    return audio_to_np(self.audio, self.start, duration)

read_bytes

read_bytes(format: str = 'wav') -> bytes

Returns the audio fragment as audio bytes.

Parameters:

  • format (str, default: 'wav' ) –

    The desired audio format (e.g., 'wav', 'mp3'). Defaults to 'wav'.

Returns:

  • bytes ( bytes ) –

    The encoded audio fragment as bytes.

Source code in datachain/lib/file.py
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
def read_bytes(self, format: str = "wav") -> bytes:
    """
    Returns the audio fragment as audio bytes.

    Args:
        format (str): The desired audio format (e.g., 'wav', 'mp3').
                     Defaults to 'wav'.

    Returns:
        bytes: The encoded audio fragment as bytes.
    """
    from .audio import audio_to_bytes

    duration = self.end - self.start
    return audio_to_bytes(self.audio, format, self.start, duration)

save

save(destination: str, format: str | None = None, client_config: dict | None = None, *, content_type: str | None = None, content_disposition: str | None = None, cache_control: str | None = None, content_encoding: str | None = None, metadata: dict[str, str] | None = None, write_options: dict[str, Any] | None = None) -> AudioFile

Saves the audio fragment as a new audio file.

If destination is a remote path, the audio file will be uploaded to remote storage.

Parameters:

  • destination (str) –

    Output directory path or URI (e.g. s3://…, gs://…).

  • format (str | None, default: None ) –

    Output audio format (e.g., 'wav', 'mp3'). If None, inferred from the file extension.

  • client_config (dict | None, default: None ) –

    Optional client configuration (e.g. credentials).

  • content_type (str | None, default: None ) –

    Content-Type to set on the written object.

  • content_disposition (str | None, default: None ) –

    Content-Disposition to set on the object.

  • cache_control (str | None, default: None ) –

    Cache-Control to set on the object.

  • content_encoding (str | None, default: None ) –

    Content-Encoding to set on the object.

  • metadata (dict[str, str] | None, default: None ) –

    Custom key/value metadata to attach to the object.

  • write_options (dict[str, Any] | None, default: None ) –

    Raw, backend-native write kwargs merged after the normalized fields above.

Returns:

  • AudioFile ( AudioFile ) –

    A Model representing the saved audio file.

Source code in datachain/lib/file.py
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
def save(
    self,
    destination: str,
    format: str | None = None,
    client_config: dict | None = None,
    *,
    content_type: str | None = None,
    content_disposition: str | None = None,
    cache_control: str | None = None,
    content_encoding: str | None = None,
    metadata: dict[str, str] | None = None,
    write_options: dict[str, Any] | None = None,
) -> "AudioFile":
    """
    Saves the audio fragment as a new audio file.

    If ``destination`` is a remote path, the audio file will be uploaded
    to remote storage.

    Args:
        destination: Output directory path or URI (e.g. ``s3://…``, ``gs://…``).
        format: Output audio format (e.g., 'wav', 'mp3').
                If None, inferred from the file extension.
        client_config: Optional client configuration (e.g. credentials).
        content_type: ``Content-Type`` to set on the written object.
        content_disposition: ``Content-Disposition`` to set on the object.
        cache_control: ``Cache-Control`` to set on the object.
        content_encoding: ``Content-Encoding`` to set on the object.
        metadata: Custom key/value metadata to attach to the object.
        write_options: Raw, backend-native write kwargs merged after the
            normalized fields above.

    Returns:
        AudioFile: A Model representing the saved audio file.
    """
    from .audio import save_audio

    return save_audio(
        self.audio,
        destination,
        format,
        self.start,
        self.end,
        client_config=client_config,
        write_config=WriteConfig(
            content_type=content_type,
            content_disposition=content_disposition,
            cache_control=cache_control,
            content_encoding=content_encoding,
            metadata=metadata,
            write_options=write_options,
        ),
    )

Audio

Bases: DataModel

A data model representing metadata for an audio file.

Attributes:

  • sample_rate (int) –

    The sample rate of the audio (samples per second). Defaults to -1 if unknown.

  • channels (int) –

    The number of audio channels. Defaults to -1 if unknown.

  • duration (float) –

    The total duration of the audio in seconds. Defaults to -1.0 if unknown.

  • samples (int) –

    The total number of samples in the audio. Defaults to -1 if unknown.

  • format (str) –

    The format of the audio file (e.g., 'wav', 'mp3'). Defaults to an empty string.

  • codec (str) –

    The codec used for encoding the audio. Defaults to an empty string.

  • bit_rate (int) –

    The bit rate of the audio in bits per second. Defaults to -1 if unknown.

get_channel_name staticmethod

get_channel_name(num_channels: int, channel_idx: int) -> str

Map channel index to meaningful name based on common audio formats

Source code in datachain/lib/file.py
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
@staticmethod
def get_channel_name(num_channels: int, channel_idx: int) -> str:
    """Map channel index to meaningful name based on common audio formats"""
    channel_mappings = {
        1: ["Mono"],
        2: ["Left", "Right"],
        4: ["W", "X", "Y", "Z"],  # First-order Ambisonics
        6: ["FL", "FR", "FC", "LFE", "BL", "BR"],  # 5.1 surround
        8: ["FL", "FR", "FC", "LFE", "BL", "BR", "SL", "SR"],  # 7.1 surround
    }

    if num_channels in channel_mappings:
        channels = channel_mappings[num_channels]
        if 0 <= channel_idx < len(channels):
            return channels[channel_idx]

    return f"Ch{channel_idx + 1}"