Skip to content

Instantly share code, notes, and snippets.

@Gargaj
Forked from piersroberts/cuemaker.js
Last active August 29, 2015 14:15
Show Gist options
  • Save Gargaj/a1ff017a87576c48b8a2 to your computer and use it in GitHub Desktop.
Save Gargaj/a1ff017a87576c48b8a2 to your computer and use it in GitHub Desktop.
function Cue(albumArtist, title, tracks) {
this.PERFORMER = albumArtist;
this.TITLE = title;
this.TRACKS = tracks;
};
function Track(trackNumber, artist, title, offset) {
this.TRACK = trackNumber + ' AUDIO';
this.TITLE = title;
this.PERFORMER = artist;
this.INDEX = '01 ' + offset;
};
Number.prototype.toMMSS = function () {
var sec_num = parseInt(this, 10);
var minutes = Math.floor(sec_num / 60);
var seconds = sec_num - (minutes * 60);
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
var time = minutes + ':' + seconds;
return time;
}
var CueMaker = {
processDiscogs : function (response) {
discs = {};
seconds = 0;
discNo = '';
response.tracklist.forEach(function (track) {
if (track.duration != '' && track.position != '') {
if (typeof seconds === 'undefined') {
seconds = 0
}
if (track.position.indexOf('-') != -1)
{
_track = track.position.split('-');
}
else if (track.position.indexOf('.') != -1)
{
_track = track.position.split('.');
}
else
{
_track = [0,track.position];
}
if (discs[_track[0]] == undefined) {
discs[_track[0]] = [];
}
if (discNo != _track[0])
{
discNo = _track[0];
seconds = 0;
}
trackId = ('00' + parseInt(_track[1])).substr(-2);
minutesSeconds = track.duration.split(':');
offset = seconds.toMMSS();
newTrack = new Track(trackId, $.map(track.artists,function(item){ return item.name + (item.join ? " " + item.join : ""); }).join(" "), track.title, offset);
discs[_track[0]].push(newTrack);
seconds += minutesSeconds[0] * 60 + ~~minutesSeconds[1];
}
});
cues = [];
$.each(discs,function (idx,disc) {
cues.push(new Cue(response.artists[0].name, response.title, disc))
});
return cues;
},
cueElementToText: function(key,val,indent)
{
if (key == "TRACKS")
{
var s = "";
$.each(val,function(key,val){
$.each(val,function(key,val){
s += CueMaker.cueElementToText(key,val, key == "TRACK" ? 2 : 4);
})
})
return s;
}
if (key == "TITLE" || key == "PERFORMER")
return (" ".slice(0,indent)) + key + " \"" + val + "\"\n";
return (" ".slice(0,indent)) + key + " " + val + "\n";
},
createCue: function(cue) {
var s = "";
$.each(cue,function(key,val){
s += CueMaker.cueElementToText(key,val,0);
})
return s;
}
}
$.get(url).done(function(response){
var cues = CueMaker.processDiscogs(response);
$.each(cues,function(idx,cue){
var s = CueMaker.createCue(cue);
console.log(s);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment