Track Audio Features and Analysis

Overview

Imagine having a list of songs to play at a party but not sure what to play. Spotify can help us sort our list. It provides two endpoints that can be used to get features and analysis of a track. In this lesson, we'll look at these endpoints.

The Get Track’s Audio Features endpoint of the Spotify API can help us analyze a track using some key metrics. We can call the endpoint https://api.spotify.com/v1/audio-features/{id} and it will fetch the required metrics in response. The {id} in this URI is replaced with the Spotify ID of the track.

Using these metrics, we can help a user select the tracks that they’ll highly likely want to play. We can also use these metrics to categorize the tracks.

Note: The value of these matrices for each song will depend upon the availability of data on Spotify. So we might see that some metrics are empty in the API response.

Request parameters

This endpoint has no query parameter.

Get audio features of a track

The code below demonstrates how to get the audio features of a track using the Spotify API. Click the "Run" button to get the audio features.

Press + to interact
url = 'https://api.spotify.com/v1/audio-features/11dFghVXANMlKmJXsNCbNl'
#11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
response = requests.request("GET", url, headers=headers)
print(json.dumps(response.json(), indent=4))

We'll get the index for energy, liveness, tempo, speechiness, acousticness, instrumentalness, danceability, loudness and valence of the track.

Get audio features of multiple tracks

We can replace the value of url in line 1 with the base URI https://api.spotify.com/v1/audio-features combined with the Spotify IDs of multiple tracks as query parameters to get audio features for multiple tracks. The code below demonstrates how we can use this endpoint.

Press + to interact
url = ('https://api.spotify.com/v1/audio-features?'
'ids=11dFghVXANMlKmJXsNCbNl,7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ')
#11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
#7ouMYWpwJ422jRcDASZB7P is the Spotify ID of the song Knights of Cydonia
#4VqPOruhp5EdPBeR92t6lQ is the Spotify ID of the song Uprising
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
response = requests.request("GET", url, headers=headers)
print(json.dumps(response.json(), indent=4))

We'll get the same information as we got in the first case but for multiple tracks.

Track audio analysis

The Get Track's Audio Analysis endpoint is used to get the track’s audio analysis information. The base URI of this endpoint is https://api.spotify.com/v1/audio-analysis/{id}. The {id} in the URI is replaced with the ID of the track when making the API call.

It can be used in applications where we provide the details of tracks to a user. It provides technical details of the tracks and that too for different intervals of the track, unlike the Get Track’s Audio Features endpoint.

Request parameters

This endpoint has no query parameters.

Get audio features of a track

The code below calls the Audio Analysis endpoint and prints the response.

Press + to interact
url = 'https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl'
#11dFghVXANMlKmJXsNCbNl is the Spotify ID of the song Cut To The Feeling
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
response = requests.request("GET", url, headers=headers)
print(json.dumps(response.json(), indent=4))

Try changing the track ID in line 1 to get information about a different track.

We can use the Search for Items endpoint to get the track ID using the track's name.

Response fields

The response of this endpoint contains the following information:

Response field

Type

Description

analyzer_version

string

This is the version of the analyzer used.

analysis_time

integer

This is the amount of time, in milliseconds, taken to analyze the track.

num_samples

integer

This is the number of samples of the track that were analyzed.

loudness

float

This is the loudness of the sound in decibells.

tempo

float

This is the speed or pace of a sample.

sync_string

string

It is a string that demonstrates how to accurately synchronize the track to a corresponding waveform.

start

float

This is the start point of a sample (in seconds).

duration

float

This is the duration of a sample (in seconds).

tatums

array

This contains information about the smallest time interval between successive notes in a rhythmic phrase.