Process audio data

Process audio data

This guide shows specific methods for processing audio datasets. Learn how to:

  • Resample the sampling rate.

  • Use map() with audio datasets.

For a guide on how to process any type of dataset, take a look at the general process guide.

Cast

The cast_column() function is used to cast a column to another feature to be decoded. When you use this function with the Audio feature, you can resample the sampling rate:

Copied

>>> from datasets import load_dataset, Audio

>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train")
>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))

Audio files are decoded and resampled on-the-fly, so the next time you access an example, the audio file is resampled to 16kHz:

Copied

>>> dataset[0]["audio"]
{'array': array([ 2.3443763e-05,  2.1729663e-04,  2.2145823e-04, ...,
         3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32),
 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav',
 'sampling_rate': 16000}

Map

The map() function helps preprocess your entire dataset at once. Depending on the type of model you’re working with, you’ll need to either load a feature extractor or a processor.

  • For pretrained speech recognition models, load a feature extractor and tokenizer and combine them in a processor:

    Copied

  • For fine-tuned speech recognition models, you only need to load a processor:

    Copied

When you use map() with your preprocessing function, include the audio column to ensure you’re actually resampling the audio data:

Copied

Last updated