Skip to main content
Headliner II
March 5, 2013

Sonos with node.js, my attempt!

INFO

Source code here: https://github.com/jishi/node-sonos-http-api
Sonos Web Controller draft (work in progress!): https://github.com/jishi/node-sonos-web-controller
Sonos IR control (requires lircd and IR reciever): https://github.com/jishi/node-sonos-remote-control

==============================================================


I know that there already is an attempt at this by this guy: http://forums.sonos.com/showthread.php?t=32643, but I found it to be a bit lacking in functionality so I decided to write up my own.

I have created a simple web-based API using what I have, which could be useful for integrating stuff with other applications. This web based API is inspired by other RESTful APIs, however I don't think it follows the correct guidelines to be called a REST API.

IT supports most basic features like:

play, pause, seek, next, prev, volume, mute, setAVTransportURI

It also supports these advanced fatures:

State of player as JSON, zone info as JSON, Play favorite item, presets (grouping, volume, avTransportURI)

You can read more in the README for each project.

To run it as a service under linux, I suggest using pm2 (https://github.com/Unitech/pm2). You need to run it in forked mode (-x)!

For Windows, you might try Winser http://jfromaniello.github.io/winser/

Cheers!
    This topic has been closed for further comments. You can use the search bar to find a similar topic, or create a new one by clicking Create Topic at the top of the page.

    391 replies

    jishiAuthor
    Headliner II
    October 22, 2013
    So, I spent the evening with coming up with a rather unique feature (haven't seen it yet anywhere else, enlighten me if I'm wrong!).

    I call it "volume enforcement", which basically means that you can "lock" the volume for all your players, both up and down. This might be useful at a party, in an office environment, or just when you are going to sleep (if another family member decides to have a party in your house or something).

    How it works? When you tell it to lock volumes, it will register the current volume for all players, and every time the volume changes, it will just send another volume adjustment to reset it back to whatever it was. This will of course not be rock solid, but might be useful never the less.

    This is part of the http-api, since it needs some sort of trigger to function.

    I might change it to only lock for increasing volume, since this effectively makes it impossible to mute a player with the buttons. If you have any feedback regarding that, please let me know.
    Lyricist III
    October 22, 2013
    Thanks for the new preset options - perfect!

    But some problems with grouping speakers. If I have 3 speakers grouped (Bathroom, Kitchen, Master Bedroom) and would like to regroup two of them.

    Sending the following
    code:
    {"players":[{"roomName":"Bathroom","volume":15},{"roomName":"MasterBedroom","volume":25}],"state":"stopped","uri":"x-sonosapi-stream:s47341?sid=254&flags=32"}


    Server output
    code:
    { action: 'preset',
    value: '%7B%22players%22:[%7B%22roomName%22:%22Bathroom%22,%22volume%22:15%7D,%7B%22roomName%22:%22Master%20Bedroom%22,%22volume%22:25%7D],%22state%22:%22stopped%22,%22uri%22:%22x-sonosapi-stream:s47341?sid=254&flags=32%22}' }
    applying preset { players:
    [ { roomName: 'Bathroom', volume: 15 },
    { roomName: 'Master Bedroom', volume: 25 } ],
    state: 'stopped',
    uri: 'x-sonosapi-stream:s47341?sid=254&flags=32' }
    applying preset { players:
    [ { roomName: 'Bathroom', volume: 15 },
    { roomName: 'Master Bedroom', volume: 25 } ],
    state: 'stopped',
    uri: 'x-sonosapi-stream:s47341?sid=254&flags=32' }
    skipping breakout because already coordinator
    current volume 15
    setting volume 15
    current volume 25
    setting volume 25
    callback
    STATUS: 200
    volume success? true
    Volume
    STATUS: 200
    Volume
    STATUS: 500


    The group still contains 3 speakers. However if ungrouping all speakers first and then send the command it works ok. It doesn't load the Radio from the URI command either.
    jishiAuthor
    Headliner II
    October 22, 2013

    The group still contains 3 speakers. However if ungrouping all speakers first and then send the command it works ok. It doesn't load the Radio from the URI command either.


    Yeah, that's a glitch. If the first player already is a coordinator, I need to check which group members it has and detach the ones that shouldn't be part of it. Right now it just works with the players listed. It worked in an earlier version where I always detached the coordinator, but that became kind of choppy if you reapplied the same preset twice. I will fix it in the coming days.

    uri is only useful for line-ins and hardcoded http-streaming urls. The x-sonosapi-stream urls require the DIDL metadata in order to be applicable. Next on the todo is to support favorites in presets as well, that should be a no brainer to solve.
    Lyricist III
    October 23, 2013
    Yeah, that's a glitch. If the first player already is a coordinator, I need to check which group members it has and detach the ones that shouldn't be part of it. Right now it just works with the players listed. It worked in an earlier version where I always detached the coordinator, but that became kind of choppy if you reapplied the same preset twice. I will fix it in the coming days.

    uri is only useful for line-ins and hardcoded http-streaming urls. The x-sonosapi-stream urls require the DIDL metadata in order to be applicable. Next on the todo is to support favorites in presets as well, that should be a no brainer to solve.


    Looking forward to play with the upcoming fixes!
    jishiAuthor
    Headliner II
    October 23, 2013
    So, now it's possible to use favorites in your presets. It will replace the queue if it's a queue. If it's a radio, it will just start playing it and leave the queue intact.
    jishiAuthor
    Headliner II
    October 23, 2013
    Looking forward to play with the upcoming fixes!

    Just pushed a commit where the issue with players not leaving the group is fixed. Enjoy.
    jishiAuthor
    Headliner II
    October 24, 2013
    Seems like I had a bad fallback. I pushed a fix for it to sonos-discovery. Remove your node_modules folder and run npm install again.
    Lyricist III
    October 25, 2013
    Seems like I had a bad fallback. I pushed a fix for it to sonos-discovery. Remove your node_modules folder and run npm install again.

    Much better, thanks!
    jishiAuthor
    Headliner II
    October 26, 2013
    I have added support for:

    groupVolume
    groupMute
    mute (single player)
    repeat
    shuffle

    I rewrote the previous groupmute/volume implementation and because of that, the calls for volume and mute are now different (this doesn't concern the http-api, only sonos-discovery).

    enjoy.
    jishiAuthor
    Headliner II
    October 27, 2013
    It now also supports playMode in presets.

    Example:

    code:

    {
    "players": [
    { "roomName": "room1", "volume": 15},
    {"roomName": "room2", "volume": 25}
    ],
    "state": "stopped",
    "favorite": "my favorite name",
    "playMode": "SHUFFLE"
    }


    Valid values are: NORMAL, REPEAT_ALL, SHUFFLE_NOREPEAT, SHUFFLE

    The most confusing part of it is that "SHUFFLE" is with implicit repeat on. I might change the format to something like:

    code:

    "playMode": {"repeat": "on", "shuffle": "off"}


    Maybe that would be easier to comprehend.