I successfully built a program to sync up Phillips Hue lights with Sonos in this post. Unfortunately the Echonest API is no longer in use and I have run up to difficulties with authorising the replacement Spotify API (any help with that is gratefully received!).
I’ve also always fancied myself as a lighting technician and the responsiveness of the Hue lights got me thinking that there might be a way to sync my lights to a specific track or a bunch of tracks. Imagine a party where the lights “do their own thing” until a track comes on that is recognised, and then the lights sync up in exactly the way you have programmed them.
I wanted to make the program do as a minimum the following:
- Have a “default” setting where the lights change in a generic but random way
- When a track starts, to detect that the track has “light cues” associated with it and to start playing the light cues alongside the track
- To be synced to the second, so that the lights always “keep up” with the track
- To be able to “join in” and catch up with the track, even if it is paused or rewound (completely or partly)
The attached files are small, and are by no means polished, but they do the job and the program can run all night successfully changing between “I don’t know this song so I’ll just do a random light show” to “Oooh! I know this song, let me load up the light cues and join in!”.
There are two parts: and Excel spreadsheet where the lights are encoded, and a small python program that is run to manage the current playing track and loading of the associated light cues if they exist.
This program also relies on the node-sonos-http-api code by Jishi available here (https://github.com/jishi/node-sonos-http-api) with installation instructions mentioned in my previous Hue/Sonos post. This node program should be running (it always is on my Raspberry Pi as I use it to control all Sonos throughout the flat using HTTP commands).
The Excel spreadsheet:
The spreadsheet is a macro-enabled spreadsheet that allows you to create, load, edit and save light cue files (saved as *.lig files). Simply open the spreadsheet and listen to your music. When you come to a part of the music that deserves a light change, you go to that line and then choose a function and type in some numbers.
At the top of the spreadsheet, type in the name of the track you want to set up a light cue datafile for. Enter the whole location of the file but without any extension. For example, if the track is called Like a Cat, and you want it to be saved in your C:\Users\Sample\Documents folder, in cell C5 type in C:\Users\Sample\Documents\Like a Cat
A word of caution here: run the program and change the Sonos to the track you want to encode first. The program will show the exact name of the file it is looking for. In order to simplify the program, it strips out brackets and hyphens, as well as commas and full stops (periods). You may therefore end up with a track name that has a few extra spaces in it, so look out for that.
In cells C9 to L9 enter the reference numbers of each light you want to use in this song. You can use all available lights or just some. You don’t have to select the same lights for each song, but remember if one known song comes on after the other, some lights may remain as you left them in the first song if you don’t include them in the second song’s list.
In cell C11 enter the group number of the Hue lights you want to control. This is useful as some of the light cues you can use address the whole group rather than individual bulbs in sequence. You can set groups using the Hue API.
Listen to a song and then work out in your head what would be a good lighting effect. Maybe you’d like each bulb to flick between strong random colours every 2 seconds, or maybe you want a low orange glow on all lights until the chorus, and then raise up the lights to a vivid red, maybe making them flash at just the right moment.
For each song you can enter as many or as few lines of light cues as you like, even just one at the beginning of the track, to change the lights to match the mood of the music for the whole song, or maybe an epic display matched perfectly to each bar.
Functions available are:
ALLSET which changes in sequence each light you have specified to use
CHGALL which changes the whole group of lights instead of each individual light
FLASH which pulses the lights either once or twice (depending on how fast your host machine is)
INDIV which controls one individual bulb (enter this in the Lightref/delay column). If you want to change 2 bulbs together just put two INDIV lines together, one after the other, a second apart.
ALLBRI ignores a lot of the variables supplied and just changes the whole group of lights to a specific brightness
ALLOFF and ALLON switch the whole group of lights off or on, at a transition time you specify
CHGSEQ changes each light sequentially, just like ALLSET, but waits a certain number of seconds you specify (enter this in the Lightref/delay column)
How to write a light cue line:
First choose what you want to do. Let’s say in the very first second of the track we want to bring up all the lights to a dark, moody red colour. Enter the following line.
I want to force the lights to one specific colour, so I have entered the same number into MinHue and MaxHue so that I am sure that colour 0 (red) will be chosen.
Then we want to spring into vibrant colours which change every 10 seconds, from the 15th second of the track.
Notice this time I have set MinHue to 0 and MaxHue to 65000 – this gives a good range of colours, in fact the whole spectrum. I set MaxSat and MinSat to 255 as I don’t want to mix the colours with any white. The closer you get to 0 for Sat, the more white is mixed in with the colour. I have set TransTime to 10 because I want them to change over the period of 1 second. I could have put 5 or 1 in there if I wanted a faster or instant response. There is some randomness between the brightness too, from 100 (mid-dim) to 255 (bright).
I then copy this line from column D to column L and then paste into 25 seconds, 30 seconds, 35 seconds and so on.
When I get to the end of the song I just finish. Nothing else to do unless you want to add some other flourishes. Let’s go back to 2 minutes and 24 seconds, as there is one enormous drum solo that starts there and I would like to put pulsing white lights for 10 seconds.
Now just click the Export button at the top. Excel won’t reportanything (I told you it wasn’t finished) but the light cue file will be saved in the location and with the name you specified in cell C5. Make sure it has shown up.
You can quickly build up quite a catalogue of “*.lig” light cue files.
When done, copy these files to the location where you’ll be running the python program (oh, you’ll need to create or download one called “default.lig” which is the file that the program will open if the track playing does not have its own file).
Inside the python program change 192.168.1.94:5005 to the address of the server that is running for node-sonos-http-api (the address where server.js is running) and then change the Sonos zone name from “Living%20Room” to the one that will be playing the music. If you want more than one player to play music (at a party for example) make sure the player you specify here is the “main” player and additional speakers are grouped to it.
Run the python program (in python 3) and you’ll see it start to wait for the Sonos player to start playing something. Play one of the songs you have created a light cue file for and make sure it opens. If it does, you’ll enjoy the fruits of your labour! If not, first check what file the program was actually looking for (each title is printed out by the program when the Sonos player changes track).
I hope you enjoy this small program and light cue creation spreadsheet. Please let me know how you’d improve it or if you use it! Thanks as always for your comments, they keep me writing these posts.
Take off the “.doc” extension before using – WordPress does not allow me to upload *.py, *.xlsx or my own *.lig files.