In this tutorial we will show how to collect data from BrainFlow and Spotify at the same time. Spotify allows you to get information about currently playing tracks and music features like:

music_feature = features_df[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', 'id']]

Such features can be used together with biological data to research the impact of the music on brainwaves and find the best music for your current mood. It’s especially interesting, since recently we’ve added Enophone headphones into BrainFlow and looking forward to working closer with similar devices.

Creating a new app at Spotify website

To get started, you will need to log into your Spotify account or create a new Spotify account. Then you can go to your developer dashboard page.

When you create new application you will receive new client_id and client_secret. Also, in settings for your new application you need to configure Redirect URL to something like, you can choose any valid port number.

Although we’re not trying to create an app for production, we will need this client ID to access the data.


We need BrainFlow package, Spotipy and Pandas. You can install it using:

git clone
cd brainflow/python-package/examples/spotify_dataset_collection
python -m pip install -r requirements.txt

Spotipy is “a lightweight Python library for the Spotify Web API.” With Spotipy, we can get full access to all of the music data provided by the Spotify platform.

Spotify authorization

There are two options to perform authorization: Authorization Code Flow and Client Credentials Flow. You can read more about them here. We need to collect user-related data, so the only option is Authorization Code Flow, for such authorization we need to provide redirect_url, client_id, client_secret, and username.

import argparse

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth
import spotipy.util as util

parser = argparse.ArgumentParser()
# spotify args
parser.add_argument('--redirect-uri', type=str, default=None)
parser.add_argument('--client-id', type=str, default=None)
parser.add_argument('--client-secret', type=str, default=None)
parser.add_argument('--username', type=str, required=True)
args = parser.parse_args()

token = util.prompt_for_user_token(args.username,

sp = spotipy.Spotify(auth=token)

Getting data from Spotify

In this article we will not show how to work with BrainFlow API, there are many samples for that. Take a look at Code Samples for more info.

Let’s focus on Spotify API, there are no callbacks for notifications about events like song started/stopped, but there is a way to get info about currently playing song and get its features. We will run it in the loop.

track = sp.current_user_playing_track()
if track is not None:
    features = sp.audio_features(track['item']['id'])
    features_df = pd.DataFrame.from_dict(features)
    music_feature = features_df[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', 'id']]

Mixing it all together

Here is a full script which you can use - Spotify specific arguments to provide are:

  • username
  • client-id
  • client-secret
  • redirect-url

All other args are the same as for example. You can pick board id and provide info needed for connection.

It stores data from BrainFlow and data from Spotify when you switch or pause currently playing track. We store it in two different files, but we match them by number of rows for easier post-processing.

We will show how to process such data in upcoming tutorials.