The application has got a feature to filter based on the moods of a track. The Spotify API doesn't provide an mood endpoint. Because of this we made a little algorithm to detect moods in tracks. This is also the base for the recommended tracks.
How it works
Below is an short explanation how we assign moods to tracks. If you want to know more about the way it works, you can find a detailed description .
When a user searches we get the search results from the Spotify API. With the Spotify API we request the audio features. From these audio features we use energy, valence and danceability. These endpoints are on a scale from 0.0to 1.0. We divided this scale in to five sections with steps of 1/6. Based on a tracks energy, valence and danceability value it decides which mood the track probably got.
There are three functions to calculate the mood. One for energy, one for valence and one for danceability. These functions are more or less the same so we show just one. n is the value that we get back from the Spotify API. Based on this number the function returns the certainty of the moods in percentage of ten.
The mood with the highest percentage is the mood we want. The code below gets the highest percentage. If there is more than one highest mood it will return No mood detected....