Audio and spectrograms

This tutorial demonstrates how to use OpenSoundscape to open and modify audio files and spectrograms.

Audio files can be loaded into OpenSoundscape and modified using its Audio class. The class gives access to modifications such as trimming short clips from longer recordings, splitting a long clip into multiple segments, bandpassing recordings, and extending the length of recordings by looping them. Spectrograms can be created from Audio objects using the Spectrogram class. This class also allows useful features like measuring the amplitude signal of a recording, trimming a spectrogram in time and frequency, and converting the spectrogram to a saveable image.

To download the tutorial as a Jupyter Notebook, click the “Edit on GitHub” button at the top right of the tutorial. Using it requires that you install OpenSoundscape and follow the instructions for using it in Jupyter.

As an example, we will download a file from the Kitzes Lab box location using the code below, and use it throughout the tutorial. To use your own file for the following examples, change the string assigned to audio_filename to any audio file on your computer.

import subprocess['curl',
                '-L', '-o', '1min_audio.wav'])
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100 3750k  100 3750k    0     0  1603k      0  0:00:02  0:00:02 --:--:-- 3124k
CompletedProcess(args=['curl', '', '-L', '-o', '1min_audio.wav'], returncode=0)
audio_filename = './1min_audio.wav'

Quick start

Import the Audio and Spectrogram classes from OpenSoundscape. (For more information about Python imports, review this article.)

# import Audio and Spectrogram classes from OpenSoundscape
from opensoundscape import Audio, Spectrogram

These classes provide a variety of tools to load and manipulate audio and spectrograms. The code below demonstrates a basic pipeline:

  • load an audio file
  • generate a spectrogram with default parameters
  • create a 224px X 224px-sized image of the spectrogram
  • save the image to a file
from pathlib import Path

# Settings
image_shape = (224, 224) #(height, width) not (width, height)
image_save_path = Path('./saved_spectrogram.png')

# Load audio file as Audio object
audio = Audio.from_file(audio_filename)

# Create Spectrogram object from Audio object
spectrogram = Spectrogram.from_audio(audio)

# Convert Spectrogram object to Python Imaging Library (PIL) Image
image = spectrogram.to_image(shape=image_shape,invert=True)

# Save image to file

The above function calls could even be condensed to a single line:


Clean up by deleting the spectrogram saved above.


Audio loading

The Audio class in OpenSoundscape allows loading and manipulation of audio files.

Load .wav(s)

Load the example audio from file:

audio_object = Audio.from_file(audio_filename)