Being an indecisive sort of person I would often sit down to listen to some music and spend more time looking through my collection, playing the odd track, rejecting it, looking again and still probably end up listening to one of the ten or so favourite albums. I don’t expect I’m alone in this.
So for years I’ve used a ‘giant playlist’ played at random and when a track came along that made me want to hear the entire album I could turn off random, back track to the first track of that album and play that album right the way through. All very well but it meant that everything had to be done by remote, the UI being unusable with a very long queue.
So (with many, many thanks to forum member jelby here are a couple of scripts that make my life so much easier…(especially in the car/motor home).
The first ‘lib2queue’ loads 25 random(ish) tracks to the queue and the second ‘trk2album’ loads and plays the album of the currently playing track.
Firstly you will need the socket io client
npm install socket.io-client
[code] // lib2queue
function rand(max, min) {
return Math.floor(Math.random() * (+max - +min)) + min;
}
var io = require(‘socket.io-client’);
var socket = io.connect(‘http://localhost:3000’);
socket.emit(‘clearQueue’);
socket.emit(‘browseLibrary’, {‘uri’:‘albums://’});
socket.on(‘pushBrowseLibrary’,function(data) {
item = data.navigation.lists[0].items[0];
if (item.type == ‘song’) {
socket.emit(‘addToQueue’, {‘uri’:item.uri});
} else {
var list = data.navigation.lists[0].items;
var random = rand(list.length - 1, 0);
select = list[random];
socket.emit(‘browseLibrary’, {‘uri’:select.uri});
}
});
socket.on(‘pushQueue’, function(data) {
if (data.length > 25) {
socket.emit(“play”,{“value”:0});
socket.disconnect()
} else {
socket.emit(‘browseLibrary’, {‘uri’:‘albums://’});
}
});
setTimeout(function() { socket.disconnect(); }, 20000);
[/code]
//trk2album
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000');
socket.emit('getState', '');
socket.on('pushState', function (data) {
if (data.uri.length != 0) {
album = (data.uri.lastIndexOf('/'));
data.uri = data.uri.substring(0, album);
socket.emit('clearQueue');
socket.emit('addToQueue', {
'uri': data.uri
})
socket.removeAllListeners('pushState');
socket.on('pushState', function () {
socket.emit("play",{"value":0});
});
}
});
setTimeout(function () {
socket.disconnect();
}, 3000);
.
Now ‘ssh ing’ the commands ‘node lib2queue’ and ‘node trk2album’ should work.
There are at this moment in time a number of drawbacks!
You will need to have a remote control set up to issue the commands easily.
Only the first track of a random album is loaded, it would be nice to load a truly random track.
When I get time I will look into this, unless some clever person wants to jump in, all improvements gratefully received! (In fact I would consider this to be the perfect basis of a simple plugin to provide a couple of new UI buttons - potential plugin writers please help!)