-
Notifications
You must be signed in to change notification settings - Fork 0
/
echonest.js
93 lines (78 loc) · 2.32 KB
/
echonest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var echonest = {}
fetch_uri = function(uri, callback) {
console.log("fetching uri: " + uri);
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
callback(data);
}
}
xmlhttp.open("GET", uri, true);
xmlhttp.send();
}
fetch_song_id = function(song, song_id) {
var song_url = "http://developer.echonest.com/api/v4/track/profile?api_key=KL9ORZ8ADN2NFYSGZ&id=spotify:track:" + song_id + "&format=json&bucket=audio_summary";
fetch_uri(song_url, function(data) {
song.data = data;
if (data["response"]["status"]["code"] != 0) {
console.log("unable to fetch echonest data for " + song_url);
song.available = false;
} else {
song.artist = data["response"]["track"]["artist"];
song.analysis_url = data["response"]["track"]["audio_summary"]["analysis_url"];
song.available = true;
fetch_uri(song.analysis_url, function(data) {
song.analysis_data = data;
song.beats = data["beats"];
song.meta = data["meta"];
song.track = data["track"];
song.bars = data["bars"];
song.tatums = data["tatums"];
song.sections = data["sections"];
song.segments = data["segments"];
if (song.completed_callback != undefined) {
song.completed_callback(song);
}
});
}
});
}
echonest.song = function(uri, completed_callback) {
this.uri = uri;
this.data = undefined;
this.completed_callback = completed_callback;
fetch_song_id(this, this.uri);
return this;
}
echonest.handle_load_echonest = function(uri) {
if (typeof(game) == "undefined")
return;
spotify.pause();
game.state = game.GAME_STATES.LOADING;
echonest.current_song = new echonest.song(uri, function() {
console.log("echonest ready with " + uri);
spotify.track(function () {
game.start_new(uri);
game.state = game.GAME_STATES.PLAYING;
spotify.play();
});
});
};
echonest.init = function () {
// initial loading of echonest data.
spotify.track(function(arg) {
var uri = arg.track.uri;
echonest.handle_load_echonest(uri);
});
// load of echonest data when song changes.
spotify.track_change(function (arg) {
var uri = arg.track.uri;
echonest.handle_load_echonest(uri);
});
}