Getting started

Installation

To install Soundata simply do:

pip install soundata

We recommend to do this inside a conda or virtual environment for reproducibility.

Soundata is easily imported into your Python code by:

import soundata

Initializing a dataset

Print a list of all available dataset loaders by calling:

import soundata
print(soundata.list_datasets())

To use a loader, (for example, urbansound8k) you need to initialize it by calling:

import soundata
dataset = soundata.initialize('urbansound8k', data_home='/choose/where/data/live')

You can specify the directory where the Soundata data is stored by passing a path to data_home.

Soundata supports working with multiple dataset versions. To see all available versions of a specific dataset, run soundata.list_dataset_versions('urbansound8k'). Use version parameter if you wish to use a version other than the default one.

import soundata
dataset = soundata.initialize('urbansound8k', data_home='/choose/where/data/live', version="1.0")

Downloading a dataset

All dataset loaders in soundata have a download() function that allows the user to download:

  • The canonical version of the dataset (when available).

  • The dataset index, which indicates the list of clips in the dataset and the paths to audio and annotation files.

The index, which is considered part of the source files of Soundata, is specifically downloaded by running download(["index"]). Indexes will be directly stored in Soundata’s indexes folder (soundata/datasets/indexes) whereas users can indicate where the dataset files will be stored via data_home.

Downloading a dataset into the default folder

In this first example, data_home is not specified. Thus, UrbanSound8K will be downloaded and retrieved from the default folder, sound_datasets, created in the user’s root folder:

import soundata
dataset = soundata.initialize('urbansound8k')
dataset.download()  # Dataset is downloaded into "sound_datasets" folder inside user's root folder
Downloading a dataset into a specified folder

In the next example data_home is specified, so UrbanSound8K will be downloaded and retrieved from the specified location:

dataset = soundata.initialize('urbansound8k', data_home='Users/johnsmith/Desktop')
dataset.download()  # Dataset is downloaded to John Smith's desktop
Partially downloading a dataset

The download() function allows to partially download a dataset. In other words, if applicable, the user can select which elements of the dataset they want to download. Each dataset has a REMOTES dictionary were all the available downloadable elements are listed.

tau2019uas has different elements as seen in the REMOTES dictionary. You can specify a subset of these elements to download by passing the download() function a list of the REMOTES keys that we are interested in via the partial_download variable.

A partial download example for tau2019uas dataset could be:

dataset = soundata.initialize('tau2019uas')
dataset.download(partial_download=['development.audio.1', 'development.audio.2'])  # download only two remotes
Downloading a multipart dataset

In some cases, datasets consist of multiple remote files that have to be extracted together locally to correctly recover the data. In those cases, remotes that need to be extracted together should be grouped in a list, so all the necessary files are downloaded at once (even in a partial download). An example of this is the fsd50k loader:

Working with non-available datasets to openly download

Some datasets are private, and therefore it is not possible to directly retrieve them from an online repository. In those cases, the download function will only download the index file, and if available, the dataset parts that are not private (for some cases, the annotations are available but not the audio). The user will have to gather the private data themselves, store it in the preferred data_home location, and then initialize the dataset as usual, indicating the data location in the data_home parameter.

Note

Private datasets may be available to the public upon request. If you are interested in a dataset that is not openly available, please contact the dataset authors or the dataset maintainers to request access.

Validating a dataset

Using the validate() method you can ensure that the files in our local copy of a dataset are identical to the canonical version of the dataset. The function computes the md5 checksum of every downloaded file to ensure it was downloaded correctly and isn’t corrupted.

For big datasets: In future soundata versions, a random validation will be included. This improvement will reduce validation time for very big datasets.

Accessing annotations

You can choose a random clip from a dataset with the choice_clip() method.

You can also access specific clips by id. The available clip ids can be accessed by doing dataset.clip_ids. In the next example we take the first clip id, and then we retrieve its tags annotation.

dataset = soundata.initialize('urbansound8k')
ids = dataset.clip_ids  # the list of urbansound8k's clip ids
clips = dataset.load_clips()  # Load all clips in the dataset
example_clip = clips[ids[0]]  # Get the first clip

# Accessing the clip's tags annotation
example_tags = example_clip.tags
print(example_tags)
>>>> Tags(confidence, labels, labels_unit)
print(example_tags.labels)
>>>> ['children_playing']

You can also load a single clip without loading all clips in the dataset:

ids = dataset.clip_ids  # the list of urbansound8k's clip ids
example_clip = dataset.clip(ids[0])  # load this particular clip
example_tags = example_clip.tags  # Get the tags for the first clip

Accessing data remotely

Annotations can also be accessed through load_*() methods which may be useful, for instance, when your data aren’t available locally. If you specify the annotation’s path, you can use the module’s loading functions directly. Let’s see an example.

Annotation classes

soundata defines annotation-specific data classes such as Tags or Events. These data classes are meant to standardize the format for all loaders, so you can use the same code with different datasets. The list and descriptions of available annotation classes can be found in Annotations.

Note

These classes are standardized to the point that the data allow for it. In some cases where the dataset has its own idiosyncrasies, the classes may be extended e.g. adding a customize, uncommon attribute.

Iterating over datasets and annotations

In general, most datasets are a collection of clips, and in most cases each clip has an audio file along with annotations.

With the load_clips() method, all clips are loaded as a dictionary with the clip id as keys and clip objects as values. The clip objects include their respective audio and annotations, which are lazy-loaded on access to keep things speedy and memory efficient.

dataset = soundata.initialize('urbansound8k')
for key, clip in dataset.load_clips().items():
    print(key, clip.audio_path)
>>>> soundscape_train_bimodal0 /Users/mf3734/sound_datasets/urbansed/audio/train/soundscape_train_bimodal0.wav
     .....

Alternatively, you can loop over the clip_ids list to directly access each clip in the dataset.

dataset = soundata.initialize('urbansound8k')
for clip_id in dataset.clip_ids:
    print(clip_id, dataset.clip(clip_id).audio_path)
>>>> soundscape_train_bimodal0 /Users/mf3734/sound_datasets/urbansed/audio/train/soundscape_train_bimodal0.wav
     .....

Including soundata in your pipeline

If you wanted to use urbansound8k to evaluate the performance of an urban sound classifier, (in our case, random_classifier), and then split the scores based on the metadata, you could do the following:

This is the result of the example above:

Using soundata with tensorflow

The following is a simple example of a generator that can be used to create a tensorflow Dataset.

In future soundata versions, generators for Tensorflow and PyTorch will be included out-of-the-box.

Using soundata to explore dataset

The explore_dataset() function in soundata allows you to visualize various aspects of the dataset. This can be particularly useful for understanding the distribution of events and the nature of the audio data before proceeding with analysis or model training.

Using explore_dataset() to Visualize Data in Jupyter Notebook

If you want to use the plot functionalities used in display_plot_utils.py you must install the optional dependencies too:

pip install soundata"[plots]"

If you try to load the visualizations without the optional dependencies, you will be thrown an exception indicating that the dependencies are missing. Please do install the optional dependencies using the command above in order to use the visualization functionalities.

Note

If you encounter any error during the installation of simpleaudio, please visit simpleaudio installation guide and check the dependencies.

To explore the dataset, first initialize it and then call the explore_dataset() method:

import soundata

# Initialize the dataset
dataset = soundata.initialize('urbansound8k', data_home='your_data_directory')

# Explore the dataset
dataset.explore_dataset()

When you run this function, an interface will appear with several options, allowing you to choose what to plot.

class dataset explorer

Class Distribution

Displays the distribution of different event classes in the dataset.

class distribution plot example

Statistics (Computational)

Provides computational statistics about the dataset (Time-consuming operation).

statistics plot example

Audio Visualization

Offers visualizations related to the audio data, such as waveforms or spectrograms.

audio visualization plot example

By using the explore_dataset() function, you can gain a comprehensive overview of the dataset’s structure and content, which is crucial for effective analysis and model building.