~ Spotify Music Quiz - With a Big Red Button
» By Joren on Thursday 21 February 2013This post documents how to implement a simple music quiz with the meta data provided by Spotify and a big red button.
During my last birthday party, organized at Hackerspace Ghent ,there was an ongoing Spotify music quiz. The concept was rather simple: If music that was playing was created in the same year as I was born, the guests could press a big red button and win a price! If they guessed incorrectly, they were publicly shamed with a sad trombone. Only one guess for each
song was allowed.
Below you can find a small videograph which shows the whole thing in action. The music quiz is simple: press the button if the song is created in 1984. In the video, at first a wrong answer is given, then a couple of invalid answers follow. Finally a good answer is given, the song “The Killing Moon by Echo & the Bunnymen” is from 1984! Woohoo!
Allright, what did I use to to implement the spotify music quiz:
- A big red Arduino button, attatched by USB to a laptop.
- A system with Spotify.
- A way to access the meta data of the currently playing song in Spotify.
- A Ruby script to connect al the parts and checks answers.
The Red Button
A nice big red button, which main features are that it is red and big, serves as the main input device for the quiz. To be able to connect the salvaged safety button to a computer via USB, an Arduino Nano is used. The Arduino is loaded with the code from the debounce tutorial, basically unchanged. Unfortunately, I could not fit the the FTDI-chip, which provides the USB connection, in the original enclosure. An additional enclosure, the white one seen in the pictures below, was added.
When pressed, the button sends a signal over the serial line. See below how the Ruby Quiz script waits for, and handles such event.
Get Meta Data from the Currently Playing Song in Spotify
Another requirement for the quiz to work is the ability to get information about the song currently playing in Spotify. On Linux this can be done with the dbus interface for Spotify. The script included below returns information about the artist, album and title of the song. It also detects if Spotify is running or not. Find it, fork it, use it, on github
On Mac OS X, Spotify can be controlled using AppleScript. A script on github shows how to get meta data about the currently playing song in Spotify, on Mac OS X.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python
#
# now_playing.py
#
# Python script to fetch the meta data of the currently playing
# track in Spotify. This is tested on Ubuntu.
import dbus
bus = dbus.SessionBus()
try:
spotify = bus.get_object('com.spotify.qt', '/')
iface = dbus.Interface(spotify, 'org.freedesktop.MediaPlayer2')
meta_data = iface.GetMetadata()
artistname = ",".join(meta_data['xesam:artist'])
trackname = meta_data['xesam:title']
albumname = meta_data['xesam:album']
#Other fields are:
# 'xesam:trackNumber', 'xesam:discNumber','mpris:trackid',
# 'mpris:length','mpris:artUrl','xesam:autoRating','xesam:contentCreated','xesam:url'
print str(trackname + " | " + artistname + " | " + albumname + " | Unknown")
except dbus.exceptions.DBusException:
print "Spotify is not running."
The Quiz Ruby Script
With the individual part in order, we now need Ruby glue to paste it all together. The complete music quiz script can be found on github. The main loop, below, waits for a button press. If it is pressed the sad trombone, invalid answer or winner sound is played. The sounds are attached to this post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while true do
# Wait for a button press
data = sp.readline
# Fetch meta data about the currently
# playing song
result = `#{now_playing_command}`
# Parse the meta data
title,artist,album = parse_result(result)
#Title is hash key, should be unique within playlist
key = title
if responded_to.has_key? key
puts "Already answered: you cheater"
play cheater
elsif correct_answers.has_key? key
puts "Correct answers: woohoo"
responded_to[key]=true
play winner
else
puts "Incorrect answer: sad trombone"
responded_to[key]=true
play sad_trombone
end
end