Thanks to the open API from Spotify, we can understand different aspects of music, including tempo, key and audio features, and build up web applications to answer our questions efficiently.
In this article, we use R Shiny as an interface to communicate with Spotify API and host it on shinyapps.
The website is accessible through the link here.
However there are some limitations for shinyapp.
io — 25 active hours per month for free.
If you cannot connect to it properly, please try it next month.
Otherwise, if you have installed R, the following command can run it on your own environment.
shiny::runGitHub("Music_ANA","BarryPan")There are two sections in the article.
The first section is about the steps to build up the visualization application; in the second part, we can share how it could be helpful for us to determine different music genres.
You will be able to build up your own music analytical tool and have more insights with Spotify API after the reading.
Step 1: Read the API documentFirst of all, we need to understand our data and what information we have.
Spotify provides thorough explanation to its API, and it is really critical for a developer or analyst to read it before starting to do anything.
Based on our experience, we believe that tempo, keys and audio features are decisive factors for recognizing music genres, and they should be what we are going to visualize.
In Spotify API, it provides many audio features.
We choose certain features and the definitions are showing below:acousticness: A confidence measure from 0.
0 to 1.
0 of whether the track is acoustic.
0 represents high confidence the track is acoustic.
danceability: Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity.
A value of 0.
0 is least danceable and 1.
0 is most danceable.
energy: Energy is a measure from 0.
0 to 1.
0 and represents a perceptual measure of intensity and activity.
Typically, energetic tracks feel fast, loud, and noisy.
For example, death metal has high energy, while a Bach prelude scores low on the scale.
Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.
speechness: Speechiness detects the presence of spoken words in a track.
The more exclusively speech-like the recording (e.
talk show, audio book, poetry), the closer to 1.
0 the attribute value.
Values above 0.
66 describe tracks that are probably made entirely of spoken words.
Values between 0.
33 and 0.
66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music.
Values below 0.
33 most likely represent music and other non-speech-like tracks.
valence: A measure from 0.
0 to 1.
0 describing the musical positiveness conveyed by a track.
Tracks with high valence sound more positive (e.
happy, cheerful, euphoric), while tracks with low valence sound more negative (e.
sad, depressed, angry).
For other purpose, you can choose different variables to fulfill what you needs.
Once you know what you are going to convey to your audience, let’s move to the next step.
Step 2: Back-end DevelopmentSpotify API follow OAuth authentication, so users need to apply for API keys before start their connections.
Besides, there is a spotifyr package in R making the request much easier.
In this visualization, we would like to grouping the data by artists.
As different artists are expertise in different types of music, we can compare various styles of music according to the artists.
To offer useful information, we need to calculate average tempo, popularity, acousticness, danceability, energy, speechness, and valence for each artist.
We should also calculate the frequency of major keys to see the differences of music genres.
Those calculation could be done easily with dplyr package in R and the output could be visualized with ggplot package.
Instead of choosing certain artists only, we want our users to decide who they are interested in.
To do so, we need to have a text input widget and a search button to activate.
Once users click the button, the program should connect to Spotify API and acquire the information, do the calculation and visualize it, and update the information to front-ends.
In Shiny, we can use observeEvent to trigger the function and finish the sequence of the tasks.
Step 3: UI DesigningData visualization is the combination of science and art.
Therefore, it is really critical to consider what is the best way to deliver your idea and how your audience feel.
In our visualization tool, there are a lot of information we want to deliver to our audience, and different information can be spoken in different ways.
After some try, we found out the proper ways to display those information as following:Average tempo can be expressed in word.
The frequency of keys for an artist can be demonstrated in a horizontal bar chart.
To make it easy to read, we include the top 5 keys for an artist only.
Average audio features for an artist can be shown in a radar plot.
Other from those information, there are some messages critical for users making their judgement.
Hence, we included popularity, number of songs and period of the songs on Spotify.
Besides, we should also include album pictures on the website to make it look more fancy.
Also, color is an important factor for the success of data visualization.
To avoid becoming too distractive, we choose white background with black words.
We can use the brand color for Spotify, html color code#1DB954, as the major color of our plots.
The first version looks like it.
It contains all functions, but not very attractive to users.
By changing the font, plot size, adding the logo and explanation of audio features, we can make it into our final version.
It is much better now, at least I think so.
Figure 1: First Version for our toolFigure 2: Final Version for our toolStep 4: Hosting itTo make your result available to everyone, you need to find a server to host it.
We have a lot of choices, including some popular cloud platforms, like GCP, AWS or Azure, with affordable cost.
For Shiny application, there is another choice: shinyapp.
It is a platform designed for Shiny apps and the prices are from $0 to $299, based on the package you choose.
If you want to publish your Shiny app to shinyapp.
io, simply click “publish” in R Studio and link it to your shinyapps.
With the tool, we can see how different artists in different styles would look like.
Here are some examples for some popular blues, country music, jazz and pop music artists.
BluesJoanne Shaw TaylorFigure 3: Joanne Shaw TaylorJoe BonamassaFigure 4: Joe BonamassaMike ZitoFigure 5: Mike ZitoCountry MusicChris StapletonFigure 6: Chris StapletonFlorida Georgia LineFigure7: Florida Georgia LineJason AldeanFigure 8: Jason AldeanJazzPaul SimonFigure 9: Paul SimonVan MorrisonFigure 10: Van MorrisonWillie NelsonFigure 11: Willie NelsonPop MusicLady GagaFigure 12: Lady GagaMaroon 5Figure 14: Maroon 5Taylor SwiftFigure 15: Taylor SwiftBased on visuals above, we can see some differences among those genres:Comparing to Blues, Country and Jazz, popular music tends to use more diverse keys.
As for Blues, Country and Jazz artists, the top 5 keys take more than 50% of their creations, Pop musicians do not focus on certain keys only, in contrast.
Country and Jazz musics usually use major keys to bring some positive impression to their audience.
Blues has lower values in “Energy” and “Valence,” as they are Blues.
ConclusionIn this article, we started with a simple question: how can we tell the difference of music genres.
We want to answer the question, so we tried to find the data, figure out the way we want to express and prove our thought, and play with computer.
Thanks to the open environment, including public API, open data and open resource software like R, it is really simple for people with limited programming skills to develop their own creations and share them with the world.
If you have any ideas about world, let’s work on it and visualize with data.