Posts Tagged code

Using The Echo Nest to get the top 100 Twitter artists

This week Twitter and The Echo Nest announced a partnership where  Twitter IDs for verified accounts are incorporated into The Echo Nest’s ID mapping layer (aka Rosetta Stone).  This makes it easy for developers to get the Twitter handle for an artist.   To demonstrate just how easy it is, I wrote a little web app that displays the top 100 artists that have  verified Twitter accounts.    Here’s the core bit of code for the app:


function fetchTopTwitterArtists() {
    var url = 'http://developer.echonest.com/api/v4/artist/top_hottt?callback=?';

    $.getJSON(url, { 'api_key': 'GETYOUROWNAPIKEY', 'format':'jsonp',
      'results': 100, 'bucket': ['hotttnesss', 'id:twitter'], 'limit': true},
     function(data) {
       for (var i = 0; i < data.response.artists.length; i++) {
          var artist = data.response.artists[i];
          var elem = $("<li>");
          var link = $("<a>");
          var handle = artist.foreign_ids[0].foreign_id.replace('twitter:artist:', '');
          link.attr('href', 'http://twitter.com/' + handle);
          link.text(artist.name);
          elem.append(link);
          $("#results").append(elem);
       }
     }
  });
}

The key bits here are creating the artist/top_hottt request to The Echo Nest and  adding the id:twitter bucket and setting limit to true. This tells the Echo Nest to include the twitter handle information in the results and limit the results to only those artists that have twitter information.  After that it is just pulling the data out of the results and formatting it for the lovely display.   The Twitter ID info is returned in an ID block that looks like this:

            {"catalog": "twitter","foreign_id": "twitter:artist:LMFAO"}

Note that the Twitter ID is returned in a URN form. To get the actual Twitter URL for an artist we just need to replace the ‘twitter:artist:’ bits with ‘http://twitter.com/&#8217;.

You can see the app here:  Top 100 artists with verified Twitter accounts.  As you can see, I tried to make the web app as ugly as possible. The only thing it needs is some Comic Sansification.  The code is available for detailed study in this gist.

, ,

1 Comment

Building a Spotify App

On Wednesday November 30, Spotify announced their Spotify Apps platform that will let developers create Spotify-powered music apps that run inside the Spotify App.   I like Spotify and I like writing music apps so I thought I would spend a little time kicking the tires and write about my experience.

First thing, the Spotify Apps are not part of the official Spotify client, so you need to get the Spotify Apps Preview Version.  This version works just like the version of Spotify except that it includes an APPS section in the left-hand navigator.

If you click on the App Finder  you are presented with a dozen or so Spotify Apps including Last.fm,  Rolling Stones, We are Hunted and Pitchfork.  It is worth your time to install a few of these apps and see how they work in Spotify to get a feel for the platform. MoodAgent has a particularly slick looking interface:

A Spotify App is essentially a web app run inside a sandboxed web browser within Spotify.  However, unlike a web app you can’t just create a Spotify App, post it on a website and release it to the world. Spotify is taking a cue from Apple and is creating a walled-garden for their apps. When you create an app, you submit it to Spotify and if they approve of it, they will host it and it will magically appear in the APPs sections on millions of Spotify desktops.

To get started you need to have your Spotify account enabled as a ‘developer’. To do this you have to email your credentials to  platformsupport@spotify.com.    I was lucky, it took just a few hours for my status to be upgraded to developer (currently it is taking one to three days for developers to get approved).  Once you are approved as a developer, the Spotify client automagically gets a new ‘Develop’ menu that gives you access to the Inspector, shows you the level of HTML5 support and lets you easily reload your app:

Under the hood, Spotify Apps is based on Chromium so those that are familiar with Chrome and Safari will feel right at home debugging apps.  Right click on your app and you can bring up the familiar Inspector to get under the hood of your application.

Developing a Spotify App is just like developing a modern HTML5 app.  You have a rich toolkit: CSS, HTML and Javascript. You can use jQuery, you can use the many Javascript  libraries. Your app can connect to 3rd party web services like The Echo Nest.  The  Spotify Apps supports just about everything your Chrome browser supports with some exceptions: no web audio, no video, no geolocation and no Flash (thank god).

Since your Spotify App is not being served over the web, you have to do a bit of packaging to make it available to Spotify during development.   To do this you create a single directory for your app. This directory should have at least the index.html file for your app and a manifest.json file that contains info about your app.  The manifest  has basic info about your app.  Here’s the manifest for my app:

% more manifest.json
{
 "BundleType": "Application",
 "AppIcon": {
     "36x18": "MusicMaze.png"
  },
 "AppName": {
     "en": "MusicMaze"
  },
 "SupportedLanguages": [
     "en"
 ],
 "RequiredPermissions": [
     "http://*.echonest.com"
 ]
}

The most important bit is probably the ‘RequiredPermissions’ field. This contains a list of hosts that my app will communicate with. The Spotify App sandbox will only let your app talk to hosts that you’ve explicitly listed in this field. This is presumably to prevent a rogue Spotify App using the millions of Spotify desktops as a botnet.  There are lots of other optional fields in the manifest. All the details are on the Spotify Apps integration guidelines page.

I thought it would be pretty easy to port my MusicMaze to Spotify.   And it turned out it really was. I just had to toss the HTML, CSS and Javascript into the application directory, create the manifest, and remove lots of code.  Since the Spotify App version runs inside Spotify, my app doesn’t have to worry about displaying album art, showing the currently playing song, album and artist name, managing a play queue, support searching for an artist. Spotify will do all that for me. That let me remove quite a bit of code.

Integrating with Spotify is quite simple (at least for the functionality I needed for the Music Maze). To get the currently playing artist I used this code snippet:

var sp = getSpotifyApi(1);
function getCurrentArtist() {
   var playerTrackInfo = sp.trackPlayer.getNowPlayingTrack();
   if (playerTrackInfo == null) {
      return null;
   } else {
      return track.album.artist;
   }
}

To play a track in spotify given its Spotify URI use the snippet:

function playTrack(uri) {
     sp.trackPlayer.playTrackFromUri(uri, {
        onSuccess: function() { console.log("success");} ,
        onFailure: function () { console.log("failure");},
        onComplete: function () { console.log("complete"); }
    });
}

You can easily add event listeners to the player so you are notified when a track starts and stops playing. Here’s my code snippet that will create a new music maze whenever the user plays a new song.

function addAudioListener() {
    sp.trackPlayer.addEventListener("playerStateChanged", function (event) {
        if (event.data.curtrack) {
            if (getCurrentTrack()  != curTrackID) {
                var curArtist = getCurrentArtist();
                if (curArtist != null) {
                    newTree(curArtist);
                }
            }
        }
    });
}

Spotify cares what your app looks like.  They want apps that run in Spotify to feel like they are part of Spotify. They have a set of UI guidelines that describe how to design your app so it fits in well with the Spotify universe.   They also helpfully supply a number of CSS themes.

Getting the app up and running and playing music in Spotify was really easy. It took 10 minutes from when I received my developer enabled account until I had a simple Echo Nest playlisting app running in Spotify

Getting the full Music Maze up and running took a little longer, mainly because I had to remove so much code to make it work.  The Maze itself works really well in Spotify.   There’s quite a bit of stuff that happens when you click on an artist node.  First, an Echo Nest artist similarity call is made to find the next set of artists for the graph. When those results arrive, the animation for the expanding the graph starts.  At the same time another call to Echo Nest is made to get an artist playlist for the currently selected artist. This returns a list of ‘good’ songs by that artist.  The first one is sent off to Spotify for it to play.  Click on a node again and the next song it that list of good songs plays.  Despite all these web calls, there’s no perceptible delay from the time you click on a node in the graph until you hear the music.

Here’s a video of the Music Maze in action.  Apologies for the crappy audio, it was recorded from my laptop speakers.

[youtube http://youtu.be/finObm36V6Y]

Here’s the down side of creating a Spotify App.  I can’t show it to you right now.  I have to submit it to Spotify and only when and if the approve it, and when they decided to release it will it appear in Spotify.  One of the great things about web development is that you can have an idea on Friday night and spend a weekend writing some code and releasing it to the world by Sunday night.  In the Spotify Apps world, the nimbleness of the web is lost .

Overall, the developer experience for writing Spotify Apps is great.  It is a very familiar programming environment for anyone who’s made a modern web app. The debugging tools are the same ones you use for building web apps. You can use all your favorite libraries and toolkits and analytics packages that you are used to.   I did notice some issues with some of  the developer docs – in the tutorial sample code the ‘manifest.json’ is curiously misspelled ‘maifest.json’.  The JS docs didn’t always seem to match reality.   For instance, as far as I can tell, there’s no docs for the main Spotify object or the trackPlayer.  To find out how to do things, I gave up on the JS docs and instead just dove into some of the other apps to see how they did stuff . (I love a world where you ship the source when you ship your app).  Update – In the comments Matthias Buchetics points us to this Stack Overflow post that points out where to find the Spotify JavaScript source in the Spotify Bundle.  At least we can look at the code until the time when Spotify releases better docs.

Update2 – here’s a gist that shows the simplest Spotify App that calls the Echo Nest API.  It creates a list of tracks that are similar to the currently playing artist.  Another Echo Nest based Spotify App called Mood Knobs is also on github.

From a technical perspective, Spotify has done a good job of making it easy for developers to write apps that can tap into the millions of songs in the Spotiverse.   For music app developers, the content and audience that Spotify brings to the table will be hard to ignore.  Still there are some questions about the Spotify Apps program that we don’t know the answer to:

  • How quickly will they turn around an app?  How long will it take for Spotify to approve a submitted app? Will it be hours, days, weeks?  How will updates be managed?  Typical web development turnaround on a bug fix is measured in seconds or minutes not days or weeks.  If I build a Music Hack Day hack in Spotify, will I be the only one able to use it?
  • How liberal will Spotify be about approving apps? Will they approve a wide range of indie apps or will the Spotify App store be dominated by the big music brands?
  • How will developers make money?  Spotify says that there’s no way for developers to make money building Spotify apps. No Ads, no revenue share.  No 99 cent downloads.  It is hard to imagine why developers  would flock to a platform if there’s no possibility of making money.

I hope to try to answer some of these questions. I have a bit of cleanup to do on my app, but hopefully sometime this weekend, I’ll submit it to Spotify to see how the app approval process works.  I’ll be sure to write more about my experiences as I work through the process.

, , ,

36 Comments

Getting Ready for Boston Music Hack Day

hackday.1.1.1.1

Boston Music Hack Day starts in exactly 10 days.  At the Hack day you’ll have about 24 hours of hacking time to build something really cool.   If you are going to the Hack Day you will want to maximize your hacking time, so here are a few tips to help you get ready.

  • Come with an idea or two but be flexible – one of the really neat bits about the Music Hack Day is working with someone that you’ve never met before. So have a few ideas in your back pocket, but keep your ears open on Saturday morning for people who are doing interesting things, introduce yourself and maybe you’ve made a team.  At previous hack days all the best hacks seem to be team efforts.  If you have an idea that you’d like some help on, or if you are just looking for someone to collaborate with, check out and/or post to the Music Hack Day Ideas Wiki.
  • Prep your APIs – there  are a number of  APIs that you might want to use to create your hack. Before you get to the Hack Day you might want to take a look at the APIs, figure out which ones you might want to use- and get ready to use them.  For instance, if you want to build music exploration and discovery tools or apps that remix music, you might be interested in the Echo Nest APIs.   To get a head start for the hack day before you get there you should register for an API Key,  browse the API documentation then check out our resources page for code examples and to find a client library in your favorite language.
  • Decide if you would like to win a prize – Of course the prime motivation is for hacking is the joy of building something really neat – but there will be some prizes awarded to the best hacks.  Some of the prizes are general prizes – but some are category prizes (‘best iPhone /  iPod hacks’) and some are company-specific prizes (best application that uses the Echo Nest APIs).  If you are shooting for a specific prize make sure you know what the conditions for the prize are.  (I have my eye on the Ultra 24 workstation and display, graciously donated by my Alma Mata).

To get the hack day jucies flowing check out this nifty slide deck on Music Hackday created by Henrik Berggren:

 

    , , , , , , , ,

    Leave a comment

    Where is my JSpot?

    I like Spotify.  I like Java.  So I combined them.  Here’s a Java client for the new Spotify metadata API:  JSpot

    This client lets you do things like search for a track by name and get the Spotify ID for the track so you can play the track in Spotify.  This is useful for all sorts of things like building web apps that use Spotify to play music, or perhaps to build a Playdar resolver so you can use Spotify and Playdar together.

    Here’s some sample code that prints out the popularity and spotify ID for all versions of Weezer’s  ‘My Name Is Jonas’.

        Spotify spotify = new Spotify();
        Results<Track> results = spotify.searchTrack("Weezer",  "My name is Jonas");
        for (Track track : results.getItems()) { 
           System.out.printf("%.2f %s \n", track.getPopularity(), track.getId());
        }
    

    This prints out:

    0.75


    0.00


    0.09

    If you have Spotify and you click on those links, and those tracks are available in your locale you should hear Weezer’s nerd anthem.

    You can search for artists, albums and tracks and you can get all sorts of information back such as release dates for albums, countries where the music can be played, track length, popularity for artists, tracks and albums.  It is very much a 0.1 release. The search functionality is complete so its quite useful, but I haven’t implemented the ‘lookup’ methods yet.   There some javadocs.  There’s a jar file: jspot.jar.  And it is all open source: jspot at google code.

    , , , ,

    3 Comments

    Playing with Playdar

    On Saturday morning I opened my web browser, built a playlist of a few songs and started to listen to them while I went about my morning computer tasks. Some of the songs in the playlist were on my laptop, while some were on the mac mini in the family room, and some were on a laptop of a friend that was on the other side of the Atlantic ocean. And if my friend in London had closed his laptop before I listened to ‘his’ song on my playlist it could have been replaced by a copy of the song that was on the computer of a friend in Seattle. I had a seamless music listening experience despite the fact that the music was scattered across a handful of computers on two continents. Such is the power of Playdar.
    playdar_logo
    Playdar is a music content resolver. It is designed to solve one problem: given the name of track, find me a way to listen to it right now. You run Playdar on any computer that you own that has music and Playdar will make it easy to listen to all of that music as if it were on your local machine. The Playdar content resolver can also talk to other Playdar resolvers too, so if Playdar can’t find a track on my local network, it can ask my friend if it knows where the track is, extending my listening reach.

    Playdar runs as a web service using standard web protocols for communicating with applications.  When Playdar receives a request to resolve a track it runs through a list of prioritized content resolvers looking for the track. First it checks your local machine, then your local network.  If it hasn’t found it there it could, if so configured, try your friends computers, or even a commercial song resolver (One could imagine for example, a music label offering up a portion of their catalog via a content resolver as a way to expose more listeners to their music).  Playdar will do its best to find a copy of a song that you can listen to now. Playdar enables a number of new listening modes:

    • Listen to my music anywhere – with Playdar, I don’t have to shoehorn my entire music collection onto every computer that I own just so I can listen to it no matter what computer I’m on.  I can distribute my music collection over all my computers – and no matter what computer I’m on I have all my music available.
    • Save money for music streamers – Music streaming services like Last.fm, Spotify and Pandora spend  money for every song that is streamed.  Often times, the listener will already own the song that is being streamed.   Playdar-enabled music streaming services could save streaming costs by playing  a local copy of a song if one is available.
    • Share playlists and mixtapes – with Playdar a friend could give me a playlist (perhaps in a XSPF format) and I could listen to the playlist even if I don’t own all of the songs.
    • Pool the music – At the Echo Nest, everyone has lots of music in their personal collections.  When we are all in the same room it is fun to be able to sample music from each other.  iTunes lets you do this but  searching through 15 separate collections for music in iTunes is burdensome.  With Playdar, all the music on all of the computers running Playdar on your local lan can be available for you to search and play without any of the iTunes awkwardness.
    • Add Play buttons to songs on web pages –  Since Playdar uses standard web protocols, it is possible to query and control Playdar from Javascript – meaning that Playdar functionality can be embedded in any web page.  I could blog about  a song and  sprinkle in a little Javascript to add a ‘play’ button to the song that would use Playdar to find the best way to play the song.  If I write a review about the new Beatles reissue and want the reader to be able to listen to the tracks I’m writing about, I can do that without having to violate Beatles copyrights.  When the reader clicks the play button, Playdar will find the local copy that is already on the reader’s computer.

    Playdar’s Marconi Moment

    Playdar is the brainchild of RJ, the inventor of the audioscrobbler and one of the founders of Last.fm.  RJ started coding Playdar in March of this year – but a few weeks ago he threw away the 10,000 lines of C++ code and started to rewrite it from scratch in Erlang.  A few days later RJ tweeted I should be taken aside and shot for using C++ for Playdar originally. It’s criminal how much more concise Erlang is for this. Less than 3 weeks after starting from a clean sheet of paper, the new Erlang-based Playdar had its first transatlantic track resolution and streaming. The moment occurred on Friday, October 16th.  Here’s the transcript from the IRC channel (tobyp is Toby Padilla, of MusicMobs and Last.fm fame) when London-based RJ first streamed a track from Toby’s Seattle computer:

    [15:40:46] <tobyp> http://www.playdar.org/demos/search.html#artist=pantera&album=&track=burnnn
    [15:41:06] <RJ2> woo, transatlantic streaming
    [15:41:19] <tobyp> hot!
    [15:41:35] <RJ2> playdar’s marconi moment
    [15:41:42] <tobyp> hahah

    An incredible amount of progress has been made in the last two weeks,  a testament to RJ’s skills as much as Erlang’s expressiveness.  Still, Playdar is not ready for the general public.  It requires a bit of work to install and get running – (yep, the erlang runtime is required), but developer Max Howell has been working on making a user-friendly package to make it easy for anyone to install. Hopefully it won’t be too long before Playdar is ready for the masses.

    Even though it is new, there’s already some compelling apps that use Playdar.  One is Playlick:

    Playlick

    Playlick is a web application, developed by James Wheare that lets you build playlists. It uses Playdar for all music resolution.  Type in the name of an album and Playlick /  Playdar will find the music for you and let you listen to it.  It’s a great way to see/hear the power of Playdar.

    Adding custom content resolvers

    One of the strengths of Playdar is that it is very easy to add new resolvers.  If you are a music service provider you can create a Playdar content resolver that will serve up your content.    I wrote a content resolver that uses the Echo Nest to resolve tracks using our index of audio that we’ve found on the web.  This resolver can be used as a backstop.  If you can’t find a track on your computer or your friend’s computers the Echo Nest resolver might be able to find a version out there on some music blog.  Of course, the quality and availability of such free-range music is highly variable, so this resolver is a last resort.

    Adding a new resolver to Playdar was extremely easy. It took perhaps 30 minutes to write – the hardest part was figuring out git – (thanks to RJ for walking me through the forks, pushes and ssh key settings).    You can see the code here: echonest-resolver.py.  Less than 150 lines of code, half of which is boilerplate.  150 lines and 30 minutes to add a whole new collection of music to the Playdar universe.   Hopefully soon we’ll see resolvers for music streaming services like Napster, Rhapsody and Spotify.

    What’s Next for Playdar?

    Playdar is new – and the plumbing and wiring are still be worked on – but already it is doing something pretty magical – letting me listen to any track I want to right now.  I can see how Playdar could be extended into acting as my music agent.  Over time, my Playdar servers will get to know quite a bit about my music tastes.  They’ll know what music I like to listen to, and when I like to listen to it.   Perhaps someday, instead of asking Playdar to resolve a specific track by name, I’ll just be able to ask Playdar to give me a playlist of new music that I might like.  Playdar can then use an Echo Nest, Last.fm or an AMG playlister to build a playlist of interesting, relevant new music.  Playdar won’t just be a music resolver, Playdar will be my music agent helping me explore for and discover new music.

    , , , , ,

    7 Comments

    Updated Java client for the Echo Nest API

    We’ve pushed out a new version of the open source Java client for the Echo Nest API.    The new version provides support for the different versions of the Echo Nest analyzer.  You can use the traditional,  but somewhat temperamental version 1 of the analyzer, or the spiffy new, ultra-stable version 3 of the analyzer.  By default, the Java client uses the new analyzer version, but if you need your application to work the exactly the same way that it did six months ago you can always use the older version.

    Here’s a bit of Java code that will print out the tempo of all the songs in a directory:

    void showBPMS(File dir) throws EchoNestException {
         TrackAPI trackAPI = new TrackAPI();
         File[] files = dir.listFiles();
         for (File f : files) {
             if (f.getAbsolutePath().toLowerCase().endsWith(".mp3")) {
                 String id = trackAPI.uploadTrack(f, true);
                 System.out.printf("Tempo 6%.3f %s\n",
                     trackAPI.getTempo(id).getValue(), f.getAbsoluteFile());
             }
         }
    }
    

    Running this code on a folder containing the new Breaking Benjamin album yields this output:

    Tempo  85.57 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/01 - Fade Away.mp3
    Tempo 108.01 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/02 - I Will Not Bow.mp3
    Tempo 168.81 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/03 - Crawl.mp3
    Tempo 156.75 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/04 - Give Me A Sign.mp3
    Tempo  85.51 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/05 - Hopeless.mp3
    Tempo  68.34 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/06 - What Lies Beneath.mp3
    Tempo 116.94 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/07 - Anthem Of The Angels.mp3
    Tempo  85.50 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/08 - Lights Out.mp3
    Tempo 125.77 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/09 - Dear Agony.mp3
    Tempo  94.99 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/10 - Into The Nothing.mp3
    Tempo 160.38 /Users/plamere/Music/Amazon MP3/Breaking Benjamin/Dear Agony/11 - Without You.mp3
    

    You can download the new Java client from the echo-nest-java-api code repository.  The new version is: echo-nest-java-api-1.2.zip

    , , , , ,

    Leave a comment

    Artist radio in 10 lines of code

    Last week we released Pyechonest, a Python library for the Echo Nest API.  Pyechonest gives the Python programmer access to the entire Echo Nest API including artist and track level methods.  Now after 9 years working at Sun Microsystems, I am a diehard Java programmer, but I must say that I really enjoy the nimbleness and expressiveness of Python.  It’s fun to write little Python programs that do the exact same thing as big Java programs.  For example, I wrote an artist radio program in Python that, given a seed artist, generates a playlist of tracks by wandering around the artists in the neighborhood of the seed artists and gathering audio tracks.   With Pyechonest, the core logic is 10 lines of code:

    def wander(band, max=10):
       played = []
       while max:
         if band.audio():
             audio = random.choice(band.audio())
             if audio['url'] not in played:
                 play(audio)
                 played.append(audio['url'])
                 max -= 1
         band = random.choice(band.similar())
    
    (You can see/grab the full code with all the boiler plate in the SVN repository)

    This method takes a seed artist (band) and selects a random track from set of audio that The Echo Nest has found on the web for that artist, and if we haven’t already played it, then do so. Then we select a near neighbor to the seed artist and do it all again until we’ve  played the desired number of songs.

    For such a simple bit of code, the playlists generated are surprisingly good..Here are a few examples:

    Seed Artist:  Led Zeppelin:

    (I think the Dale Hawkins version of Susie-Q after  CCR’s Fortunate Son  is just brilliant)

    Seed Artist: The Decemberists:

    (Note that audio for these examples is audio found on the web – and just like anything on the web the audio could go away at any time)

    I think these artist-radio style playlists rival just about anything you can find on current Internet radio sites – which ain’t to0 bad for 10 lines of code.

    , , , , , ,

    9 Comments