Adding random tracks to queue & Track to Album loader

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!)

Hi
Very very interesting, this helps me starting to want work on Volumio :wink:

First question, where is (in the code) the part where ‘Only the first track of a random album is loaded’

I was beginning to think that I was the only one who wanted this feature!

The line
item = data.navigation.lists[0].items[0];

Is the one that you ask about. Changing it to;
item = data.navigation.lists[0].items[1];

Gives the second track of the album - but only if there is a second track. This means that a check needs to be done first.
I’ve tried various ways to get around this, queuing a random album, selecting a random track, putting the track into a temporary playlist, clearing the queue, repeating for another random album and so on. This all works but only from the command line, I’ve had no luck in automating it. I can send you the code (my first attempts at JavaScript so don’t laugh too much) if you feel that you can take this further.

I just actually need some reference library where to find exhaustive list of core functions as

  • browseLibrary
  • pushBrowseLibrary
    and so

Me too but I’ve had no replies to my requests. I’m sure it’s an easy task to drill the search down to tracks rather than albums but so far… no help.

you’re not alone
im precisely on it
an idea is to catch the tracks list of an album in a variable, or the number of tracks, then randomly pick one
another idea will be to simply pick a track in the whole library, ignoring the album

for that task, i need some reference information about the core
thats why finding it is my first step (worst and longest will be reversing the source - i guess)

as you know, random playlist is not my favorite, i would like some smart-queue-fill-in
so, this wont be done by one single day

to be continued

PM’d you some bits…

Thank you, i’ll have a look on it.

here’s something that might interest you :