Skip to content

Instantly share code, notes, and snippets.

@bonhag
Created June 16, 2013 20:30
Show Gist options
  • Save bonhag/5793309 to your computer and use it in GitHub Desktop.
Save bonhag/5793309 to your computer and use it in GitHub Desktop.
Shuffle your Turntable playlist
var shuffle = function() {
var playlistLength = $('#playlist .song').length;
for (var i = 1; i < playlistLength; i = i + 1) {
setTimeout(function() {
var s = Math.floor(Math.random() * playlistLength);
console.log('moving song ' + s + ' to bottom');
$($('#playlist .song .playlist-open-options')[s]).click();
$('.move-bottom').click();
}, 500 * i);
}
}
@LucasMadar
Copy link

I changed your code a little, moving to top, and requiring no call to shuffle():

(function() {
    var playlistLength = $('#playlist .song').length;
    for (var i = 1; i < playlistLength; i = i + 1) {
        setTimeout(function() {
            var s = Math.floor(Math.random() * playlistLength);
            console.log('moving song ' + s + ' to top');
            $($('#playlist .song .playlist-go-top')[s]).click();
        }, 500 * i);
    }
})();

@benw54
Copy link

benw54 commented Jun 19, 2013

I just added random song moving to top or bottom and dropped the timeout to 1/4 second. If you don't have to watch the UI-open animation, it seems faster, you might be able to drop it down even more. Once it's as low as possible, you could do another pass (or two) to really randomize the list :)

(function() {
    var playlistLength = $('#playlist .song').length;
    for (var i = 1; i < playlistLength; i++) {
        setTimeout(function() {
            var s = Math.floor(Math.random() * playlistLength);

            if (Math.random() > Math.random())
            {
                console.log('moving song ' + s + ' to top');
                $($('#playlist .song .playlist-go-top')[s]).click();
            }
            else
            {
                console.log('moving song ' + s + ' to bottom');
                $($('#playlist .song .playlist-open-options')[s]).click();
                $('.move-bottom').click();
            }
        }, 250 * i);
    }
})();

@bonhag
Copy link
Author

bonhag commented Jun 19, 2013

Nice, @benw54! Yeah, it definitely choked without setTimeout, so I was probably being a little conservative.

@LucasMadar I'm inclined to give the function a name for a couple of reasons

  1. A DJ needn't re-paste the whole function when they want to shuffle their playlist
  2. Maybe there are other features under development...

@benw54
Copy link

benw54 commented Jun 24, 2013

Ok, so a little longer code-wise, but much faster... here's the problem though, it's very picky and I don't know why. Maybe you can check it out. If it doesn't work for you, try picking the playlist from the dropdown menu again. Paste this into Web Console and then call shufflePL();

function doreorder(plname,from,to,last){
    var i={
    api:"playlist.reorder",
    playlist_name:plname,
    index_from:from,
    index_to:to
    };

    // function uqFjkH below seems to need queue editing to be locked
    turntable.playlist.lockQueueEdits();
    turntable.uqFjkH(i,function(){console.log('Moving from ' +from+ ' to ' +to); turntable.playlist.unlockQueueEdits()});
}

function shufflePL() {
    var playlistname = this.playlist.activePlaylist;
    var playlistlength = this.playlist.fileids.length;

    // number of times we go through and shuffle
    var iter = 2;

    console.log('Shuffling your playlist ' +playlistname+ ' (' +playlistlength+ ' songs)');

    for (var i = iter * playlistlength; i >= 0; i--)
    {
    var from = 0;
    var to = 0;
    while (from == 0)
        from = Math.floor(Math.random() * playlistlength);
    while (to == 0 || to == from)
        to = Math.floor(Math.random() * playlistlength);

    doreorder(playlistname, from, to, i);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment