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.

[1]:
import subprocess
subprocess.run(['curl',
               'https://drive.google.com/uc?export=download&id=1hBUyhgPf-vxeet8BITSMZ0XsGiU-a7SG',
                '-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
[1]:
CompletedProcess(args=['curl', 'https://drive.google.com/uc?export=download&id=1hBUyhgPf-vxeet8BITSMZ0XsGiU-a7SG', '-L', '-o', '1min_audio.wav'], returncode=0)
[2]:
audio_filename = './1min_audio.wav'

Quick start

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

[3]:
# 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
[4]:
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
image.save(image_save_path)

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

[5]:
Spectrogram.from_audio(Audio.from_file(audio_filename)).to_image(shape=image_shape,invert=True).save(image_save_path)

Clean up by deleting the spectrogram saved above.

[6]:
image_save_path.unlink()

Audio loading

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

Load .wav(s)

Load the example audio from file:

[34]:
audio_object = Audio.from_file(audio_filename)
audio_object
[34]: