Skip to main content

surfboard Examples

Surfboard : How To Examples in Python

In this tutorial, we will explore the surfboard library in Python. Surfboard is a collection of audio feature extraction methods used for audio analysis and machine learning tasks. It provides a simple and efficient interface for extracting various audio features from audio files. We will cover 10 examples to demonstrate the usage of different features provided by the surfboard library.

Example 1: Loading and Visualizing Audio

To begin, we need to install the surfboard library. Open your terminal and run the following command:

pip install surfboard

Now, let's start with loading and visualizing an audio file using the surfboard library:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Visualize the waveform
audio.plot_wave()

Expected Output: This example will load the audio file located at "path/to/audio/file.wav" and display the waveform plot of the audio.

Example 2: Extracting Spectral Features

Next, let's extract some spectral features from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract spectral centroid feature
spectral_centroid = audio.spectral_centroid()

# Extract spectral flux feature
spectral_flux = audio.spectral_flux()

# Print the extracted features
print("Spectral Centroid:", spectral_centroid)
print("Spectral Flux:", spectral_flux)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the spectral centroid and spectral flux features, and print their values.

Example 3: Extracting MFCC Features

Now, let's extract some Mel-frequency cepstral coefficients (MFCC) features from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract MFCC features
mfcc = audio.mfcc()

# Print the extracted MFCC features
print("MFCC:", mfcc)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the MFCC features, and print their values.

Example 4: Extracting Chroma Features

Chroma features are commonly used for music analysis tasks. Let's extract chroma features from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract chroma features
chroma = audio.chroma()

# Print the extracted chroma features
print("Chroma:", chroma)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the chroma features, and print their values.

Example 5: Extracting Tonnetz Features

Tonnetz features represent the tonal space of music. Let's extract tonnetz features from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract tonnetz features
tonnetz = audio.tonnetz()

# Print the extracted tonnetz features
print("Tonnetz:", tonnetz)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the tonnetz features, and print their values.

Example 6: Extracting Zero Crossing Rate

Zero crossing rate is a measure of the number of times the audio waveform crosses the zero axis. Let's extract the zero crossing rate from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract zero crossing rate
zcr = audio.zero_crossing_rate()

# Print the extracted zero crossing rate
print("Zero Crossing Rate:", zcr)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the zero crossing rate, and print its value.

Example 7: Extracting RMS Energy

RMS energy is a measure of the overall energy of an audio signal. Let's extract the RMS energy from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract RMS energy
energy = audio.rms_energy()

# Print the extracted RMS energy
print("RMS Energy:", energy)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the RMS energy, and print its value.

Example 8: Extracting Spectral Contrast

Spectral contrast measures the difference in amplitude between peaks and valleys in the spectrogram. Let's extract the spectral contrast from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract spectral contrast
contrast = audio.spectral_contrast()

# Print the extracted spectral contrast
print("Spectral Contrast:", contrast)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the spectral contrast, and print its value.

Example 9: Extracting Mel-frequency Spectral Flatness

Mel-frequency spectral flatness measures the flatness of the power spectrum in the Mel-frequency domain. Let's extract the Mel-frequency spectral flatness from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract Mel-frequency spectral flatness
flatness = audio.mel_spectral_flatness()

# Print the extracted Mel-frequency spectral flatness
print("Mel-frequency Spectral Flatness:", flatness)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the Mel-frequency spectral flatness, and print its value.

Example 10: Extracting Pitch

Pitch is a perceptual property of sound that allows us to differentiate between different musical notes. Let's extract the pitch from an audio file:

import surfboard

# Load an audio file
audio = surfboard.Audio("path/to/audio/file.wav")

# Extract pitch
pitch = audio.pitch()

# Print the extracted pitch
print("Pitch:", pitch)

Expected Output: This example will load the audio file located at "path/to/audio/file.wav", extract the pitch, and print its value.

These were 10 examples demonstrating the usage of various features provided by the surfboard library. You can explore more features and their usage in the surfboard documentation.