Syncing Echo Nest analysis to Spotify Playback

With the recently announced Spotify integration into Rosetta Stone, The Echo Nest now makes available a detailed audio analysis for millions of Spotify tracks.  This audio analysis includes summary features such as tempo, loudness, energy, danceability, key and mode, as well as a set of fine-grained segment features that describe details such as where each bar, beat and tatum fall and the detailed pitch, timbral and loudness content of each audio event in the song.   These features can be very useful for driving Spotify applications that need to react to what the  music sounds like – from advanced dynamic music visualizations like the MIDEM music machine or synchronized music games like Guitar Hero.

I put together a little Spotify App that demonstrates how to synchronize Spotify Playback with the Echo Nest analysis.  There’s a short video here of the synchronization:

video on youtube: http://youtu.be/TqhZ2x86RXs

In this video you can see the audio summary for the currently playing song, as well as a display synchronized ‘bar’ and ‘beat’ labels and detailed loudness, timbre and pitch values for the current segment.

How it works:

To get the detailed audio analysis,  call the track/profile API with the Spotify Track ID for the track of interest. For example, here’s how to get the track for Radiohead’s Karma Police using the Spotify track ID:

http://developer.echonest.com/api/v4/track/profile?api_key=N6E4NIOVYMTHNDM8J&format=json&id=spotify-WW:track:3L7BcXHCG8uT92viO6Tikl&bucket=audio_summary

This returns audio summary info for the track, including the tempo, energy and danceability.  It also includes a field called the analysis_url  which contains an expiring URL to the detailed analysis data. (A very abbreviated excerpt of an analysis is contained in this gist).

To synchronize Spotify playback with the Echo Nest analysis we need to first get the detailed analysis for the now playing track.  We can do this by calling the aforementioned track/profile call to get the analysis_url for the detailed analysis, and then retrieve the analysis (it is stored in JSON format, so no reformatting is necessary).  There is one technical glitch though.  There is no way to make a JSONP call to retrieve the analysis. This prevents you from retrieving the analysis directly into a web app or a Spotify app.  To get around this issue, I built a little proxy at labs.echonest.com that supports a JSONP style call to retrieve the contents of the analysis URL.  For example, the call:

http://labs.echonest.com/3dServer/analysis?callback=foo
&url=http://url_to_the_analysis_json

will return the analysis json wrapped in the foo() callback function.  The Echo Nest does plan to add JSONP support to retrieving analysis data, but until then feel free to use my proxy. No guarantees on support or uptime since it is not supported by engineering. Use at your own risk.

Once you have retrieved the analysis you can get the current bar, beat, tatum and segment info based upon the  current track position, which you can retrieve from Spotify with:    sp.getTrackPlayer().getNowPlayingTrack().position.  Since all the events in the analysis are timestamped, it is straightforward to find a corresponding bar,beat, tatum and segment given any song timestamp.  I’ve posted a bit of code on gist that shows how I pull out the current bar, beat and segment based on the current track position along with some code that shows how to retrieve the analysis data from the Echo Nest.  Feel free to use the code to build your own synchronized Echo Nest/Spotify app.  

The Spotify App platform is an awesome platform for building music apps.  Now, with the ability to use Echo Nest analysis from within Spotify apps,  it is a lot easier to build Spotify apps that synchronize to the music. This opens the door to a whole range of new apps. I’m really looking forward to seeing what developers will build on top of this combined Echo Nest and Spotify platform.

,