Skip to content

daft.functions.resample#

resample #

resample(file_expr: Expression, sample_rate: int) -> Expression

Resample a audio file.

Parameters:

Name Type Description Default
file_expr AudioFile Expression

The audio file to resample.

required
sample_rate int

The sample rate to resample to.

required

Returns:

Name Type Description
Expression Tensor[Python] Expression

The resampled audio file.

Example

import daft from daft.functions import audio_file, resample

Discover the audio files (returns a dataframe with ['path', 'size'] columns)#

df = daft.from_glob_path("hf://datasets/Eventual-Inc/sample-files/audio/*.mp3")

df = ( ... df.with_column("file", audio_file(daft.col("path"))) ... .with_column("resampled", resample(daft.col("file"), sample_rate=16000)) ... .select("path", "size", "resampled") ... ) df.show(3) ╭────────────────────────────────┬─────────┬──────────────────────────╮ │ path ┆ size ┆ resampled │ │ --- ┆ --- ┆ --- │ │ String ┆ Int64 ┆ Tensor[Float64] │ ╞════════════════════════════════╪═════════╪══════════════════════════╡ │ hf://datasets/Eventual-Inc/sa… ┆ 822924 ┆ │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ hf://datasets/Eventual-Inc/sa… ┆ 618408 ┆ │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ hf://datasets/Eventual-Inc/sa… ┆ 1190736 ┆ │ ╰────────────────────────────────┴─────────┴──────────────────────────╯ (Showing first 3 rows)

Source code in daft/functions/audio.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def resample(
    file_expr: Expression,
    sample_rate: int,
) -> Expression:
    """Resample a audio file.

    Args:
        file_expr (AudioFile Expression): The audio file to resample.
        sample_rate (int): The sample rate to resample to.

    Returns:
        Expression (Tensor[Python] Expression): The resampled audio file.

    Example:
        >>> import daft
        >>> from daft.functions import audio_file, resample

        >>> # Discover the audio files (returns a dataframe with ['path', 'size'] columns)
        >>> df = daft.from_glob_path("hf://datasets/Eventual-Inc/sample-files/audio/*.mp3")

        >>> df = (
        ...     df.with_column("file", audio_file(daft.col("path")))
        ...     .with_column("resampled", resample(daft.col("file"), sample_rate=16000))
        ...     .select("path", "size", "resampled")
        ... )
        >>> df.show(3)
        ╭────────────────────────────────┬─────────┬──────────────────────────╮
        │ path                           ┆ size    ┆ resampled                │
        │ ---                            ┆ ---     ┆ ---                      │
        │ String                         ┆ Int64   ┆ Tensor[Float64]          │
        ╞════════════════════════════════╪═════════╪══════════════════════════╡
        │ hf://datasets/Eventual-Inc/sa… ┆ 822924  ┆ <Tensor shape=(2490278)> │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ hf://datasets/Eventual-Inc/sa… ┆ 618408  ┆ <Tensor shape=(2207923)> │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ hf://datasets/Eventual-Inc/sa… ┆ 1190736 ┆ <Tensor shape=(3341800)> │
        ╰────────────────────────────────┴─────────┴──────────────────────────╯
        <BLANKLINE>
        (Showing first 3 rows)
    """
    # Check for required dependencies at expression definition time
    # This gives users a helpful error immediately, rather than during execution
    from daft.dependencies import librosa, sf

    if not sf.module_available():
        raise ImportError(
            "The 'soundfile' module is required to resample audio files. "
            "Please install it with: pip install 'daft[audio]'"
        )
    if not librosa.module_available():
        raise ImportError(
            "The 'librosa' module is required to resample audio files. "
            "Please install it with: pip install 'daft[audio]'"
        )

    return resample_fn(file_expr, sample_rate)