Skip to content

Instantly share code, notes, and snippets.

@btipling
Created February 8, 2011 13:27
Show Gist options
  • Save btipling/816428 to your computer and use it in GitHub Desktop.
Save btipling/816428 to your computer and use it in GitHub Desktop.
dio.ID3Finder = function () {};
dio.ID3Finder._songMap = {};
dio.ID3Finder.getInfoForSong = function (song, callback) {
var req;
if (song.url in dio.ID3Finder._songMap) {
callback();
return;
}
req = new XMLHttpRequest();
req.open('GET', song.url, true);
req.onreadystatechange = dio.ID3Finder._handleInfoForSong.bind(
dio.ID3Finder,
song,
req,
callback
);
req.responseType = 'arraybuffer';
req.send(null);
};
dio.ID3Finder._handleInfoForSong = function (song, req, callback) {
var data, subset;
if (req.readyState === 4 && req.status === 200) {
data = req.response;
//Search for ID3v2.*
if (dio.ID3Finder.getString(data, 3, 0) === 'ID3') {
dio.ID3Finder.getID3v2Info(data, callback);
return;
}
//Search for ID3v1
subset = data.slice(data.length - 128);
if (dio.ID3Finder.getString(subset, 3, 0) === 'TAG') {
dio.ID3Finder.getID3v1Info(subset, callback);
}
return;
} else {
console.log('Loading...');
}
};
dio.ID3Finder.getString = function (buffer, length, offset) {
var ints, stringArray, i;
stringArray = [];
ints = new Int8Array(buffer, offset, length);
for (i = 0; i < length; ++i) {
stringArray[i] = ints[i];
}
return String.fromCharCode.apply(null, stringArray);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment