#Coghack: Stand up for lights! Get up for music!

How we added music and a team selector to our stand up light show!

Pat Hadley
cogapp

--

If there’s one lesson I’ve learned on Brighton Pier, it’s that seagulls are absolute bast@%~… Wait, no, it’s that flashing lights and cheesy music belong together.

How we got to here! (An iPad showing our new display with a team-member picker and intro button)

So, at the last Cogapp Hackday I worked with Ben Kyriakou and Stephen Norris to add Spotify music and a teammate selection menu to the amazing illuminated NanoLeaf stand up Kay White and Ben had already built.

This post is going to cover some bits of the Spotify API and Spotipy (A Python library for Spotify). So if you’re not down with variables and API calls, you may want to bail out now. Check out how to hack a 🎄 instead?

On board for some tech? Ace, start with Kay’s How-To to get up to speed. (Also, if you don’t do stand ups at your workplace, here’s an intro).

Back now? Let’s go!

Our Spotify set up

Inside the cupboard: an Echo Dot, A Gramofon and a Cambridge Audio amplifier.

Here’s how our studio music set up works: a speaker system is fed by an Amazon Echo Dot and a Gramofon. These stream audio via the company Spotify account. We were using the Echo for a while, and shouting: ‘Alexa’ {bleep-bleep} ‘next track’ is kind of fun. But for more control (playlists, radio stations) it was pretty rubbish. Plus, it is kind of creepy that it’s always listening.

Basically, me every morning. (Photo by Redd Angelo on Unsplash)

We now use the Gramofon, a simple web player. This means we can all control (and passive-aggressively fight over) the music from our desks using the Spotify web player or a tablet. Every morning at 9:45, I kick off stand up with an artfully chosen, energising and musically brilliant track.

I play this at full volume until everyone gathers for stand up and then fire up Kay and Ben’s NanoLeaf wizardry to select who talks. The NanoLeaf takes about 10 seconds between each person, so you can imagine how sad everyone feels that there isn’t more music during these little intervals!

That’s what we were planning to fix.

Command line Spotify

Maybe it’s because I’m relatively new to web development, but I think there’s always a really satisfying moment when you get to control a big hefty piece of software from a few characters in the command line. It’s like you’ve reached into the guts of an engine and set the air conditioning to just right.

So, when I first hit

$ curl -X POST “https://api.spotify.com/v1/me/player/next" -H “Authorization: Bearer {shhh secret key here}

and our music skipped ahead one track, I felt a little smug. But it did take a little while to get to this point.

Spotify has a brilliant and well documented set of APIs and SDKs that enable you to do fun things with their data about tracks and artists as well as control your own tunes through other apps or services.

Unsurprisingly this involves some authentication (otherwise I would be controlling your music right now). To build an app, you first have to register (and promise your project’s not-for-profit) before getting authorisation keys. What confused me was the two stage process: you log in to your Spotify account as a developer, get some keys and have amazing access to the database of artists and tracks. Yay!

But when you want to control the music you’re listening to everything seems to break! It turns out you have to authenticate your own app as the account user/music listener in order to turn your app ID and secret into an OAuth token (which is time limited).

This makes perfect sense as it’s the only way an app could get permission from unconnected users (say you wanted to control Spotify in a sports app), but when you’re trying to work fast on a prototype it can be a conceptual pitfall!

Introducing Spotipy

Most of Ben and Kay’s work was built using a Python wrapper around the NanoLeaf REST API. It made sense to continue in Python so we were pleased to find the aptly named Spotipy Python wrapper for the Spotify API.

There were also troubles with this (it turned out that their PIP package was actually behind the project’s GitHub version), but eventually we got the plug in working and — as is often the way — the final functions are very simple:

def nanoleaf_standup_playlist(track_number):
uri = 'spotify:user:{0}:playlist:4OwSbrgf2ETnuaVaONkk02'.format(username)
playlist_id = uri.split(':')[4]
results = spot.user_playlist_tracks(username, playlist_id, market="GB", fields="items(track(id))")
next_track = results['items'][track_number]['track']['id']
spot.volume(80)
spot.start_playback(device_id='dc30-8042-4a84-125a-d39c-d8ebeb72cfa5', uris=['spotify:track:{0}'.format(next_track)])

So basically, the function works like this:

  • track_number: takes a number by counting the number of people that have stood up (this is pulled from another function built by Ben and Kay)
  • uri: a full Spotify URI (as you get on the Share button in the app)
  • playlist_id: the long number from the uri (yes we could hard code this but it this way makes it easier to change things later)
  • results: this is the actual list of track ids in the playlist
  • next_track: this uses the track_number to identify which track to play next!
  • spot.volume: ‘spot’ is the shorthand for calling the Spotipy module. This sets the output to 80 out of 100
  • spot.start: this actually starts the music! The device_id is for our Gramofon

We set up a similar function that fades and stops the music after 8 seconds — the same time that the next person’s face lights up on the NanoLeaf!

Who’s away today?

The other little issue the first version of the NanoLeaf system had was that it would call up people that weren’t in the studio. What a catastrophically awkward break in the flow!

Steve built a system that took the standard manifest of our team members and generated a menu of all the team so that they could be unticked and removed from the selection. This actually worked by telling the system that the absent people had already had their turn.

What’s next?

The system we built points at a specific Spotify playlist. This has an advantage that it’s easy to edit this playlist without going into the code (this week we’ve had movie soundtracks each morning!). However, the playlist just plays in order and there are no relations between team members and their songs. It might be cooler if each person had their own intro music!

One way to achieve this and make things easy would be to create a different playlist for each person and they could then change the song whenever they felt like. We’d add each person’s playlist_id to the function above and pass that in each time.

Another issue is that there are plenty of absolutely banging tunes that take far longer than 10 seconds to get going. Spotipy gives us the option to start a track at any point but we’d have to be a bit cleverer if we wanted to make an easy way of controlling this.

What if we could build in some of this old school tech!

What would you add? Do we need a party button!? What if we could control it with something weird and funky like the old lift buttons or a kid’s voice changer!?

We’re all about adding just the right amount of (non-creepy) smart, fun technology to our office — just like our work for clients.

Let us know what you think!?

Get in touch here or on twitter (@PatHadley, @Cogapp) or through all the usual channels.

--

--

Writer for

Comms, marketing and digital bod. Poorly preserved archaeologist remains. Formerly @cogapp @YorkMuseumTrust