Repository navigation
Expand file tree
/
Copy pathechonest.js
More file actions
210 lines (160 loc) · 6.41 KB
/
Copy pathechonest.js
File metadata and controls
210 lines (160 loc) · 6.41 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//////////////////////////////////////
// EchoNest functionality
// (1) Spotify's API is open to anyone, for EchoNest's you need to sign up for
// your own API key -- this is a credential that lets EchoNest keep track of
// who is using their API how. You can read more about EchoNest's API,
// including how to get a key, at http://developer.echonest.com/docs/v4
var ENAPI = 'https://developer.echonest.com/api/v4/';
var ENAPIKey = '0TPFPI9TGBX5CJU49';
function ENSearch(track) {
// (1) This function takes a track (which you may remember we got from
// Spotify's API) and searches EchoNest for it, returning the first
// result if there is one
var artistQuery = artists(track, ' ');
var titleQuery = track.name;
var ENResults = ENSongSearch({
'artist': artistQuery,
'title': titleQuery
});
if (ENResults[0]) {
return ENResults[0];
}
// else
return null;
}
function ENSongSearch(parameters) {
// (1) This function searches _just_ EchoNest's song API and returns the
// parsed JSON object
//
// An example query URL:
// http://developer.echonest.com/api/v4/song/search?api_key=0TPFPI9TGBX5CJU49&format=json&results=1&artist=radiohead&title=karma%20police
// Constructing the URL we'll use
parameters['format'] = 'json';
var songSearchAPI = ENAPI + 'song/search';
var url = [
songSearchAPI,
'?api_key=' + ENAPIKey,
urlEncodeParams(parameters)
].join('');
return JSON.parse(httpGET(url)).response.songs; // grabbing just songs info
}
function ENAudioAnalysis(audioSummariedTrack) {
// (1) This function takes a track which _already has an audio_summary_ to
// go get a full audio_analysis from EchoNest. You can read more about
// the audio_analysis at:
// http://developer.echonest.com/docs/v4/_static/AnalyzeDocumentation.pdf
return JSON.parse(httpGET(audioSummariedTrack.echo.audio_summary.analysis_url));
}
function ENAudioSummary(echoedTrack) {
// (1) This function runs on a track which _already has the basic EchoNest
// info_ to grab the audio_summary. You can read more about the summary:
// https://developer.echonest.com/raw_tutorials/faqs/faq_04.html
var audioResult = ENSongMetadata(echoedTrack.echo.id, 'audio_summary');
if (audioResult.audio_summary) {
return audioResult.audio_summary;
}
// else
return null;
}
function ENSongMetadata(ENid, bucket) {
// (1) This function takes an EchoNest ID and a type of metadata to get
// (EchoNest calls this a 'bucket').
//
// Sample query:
// http://developer.echonest.com/api/v4/track/profile?api_key=FILDTEOIK2HBORODV&format=json&id=TRTLKZV12E5AC92E11&bucket=audio_summary
// Constructing the URL
parameters = {
'id': ENid,
'bucket': bucket,
'format': 'json'
};
var songMetadataEndpoint = ENAPI + 'song/profile';
var url = [
songMetadataEndpoint,
'?api_key=',
ENAPIKey,
urlEncodeParams(parameters)
].join('');
// Grabbing the metadata
return JSON.parse(httpGET(url)).response.songs[0];
}
////////////////////////////////////////
// Sketching loudness from EchoNest data
function loudnessAt(track, time) {
// (1) This function finds the EchoNest measurement of how long a given
// track is at a given time.
// (3) "segments" are EchoNests objects for small bits of a song which are
// acoustically pretty constant. Read more about them at:
// http://developer.echonest.com/docs/v4/_static/AnalyzeDocumentation.pdf
var segments = track.echo.audio_analysis.segments;
var timeCovered = 0;
// (2) Search through and find the segment containing the time we seek;
// each segment is _not_ the same duration
for (var i = 0; i < segments.length; i++) {
timeCovered += segments[i].duration;
if (timeCovered > time) {
return segments[i].loudness_max;
}
}
// else
return -1;
}
function loudnessSketch(track) {
// (2) This function _returns a function_ which Processing runs to make
// our sketch.
function sketchProc(P) {
var myTrack = track;
// (2) We'll plot loudness every 0.25 seconds
var dt = 0.25;
var numPoints = Math.round(myTrack.length) / dt;
var loud = []; // (2) an array to hold loudnesses
var points = []; // (2) an array to hold the points we'll plot
P.setup = function() {
P.size(tracklist.offsetWidth, 80);
if (track.echo) {
for (var i = 0; i < numPoints; i++) {
loud[i] = loudnessAt(track, dt * i);
points[i] = [i, loud[i]];
}
// (2) Scale our points down to fill up our width and height
points = scalePoints(points, P.width, P.height);
// (1) Draw our points in a slightly transparent black
P.noFill();
P.stroke(0, 0.25 * 255);
P.beginShape();
for (var j = 0; j < points.length; j++) {
var x = points[j][0];
var y = P.height + points[j][1];
P.curveVertex(x, y);
}
P.endShape();
} else {
console.log(track.name, "has no EchoNest data.");
}
};
P.draw = function() {
// (2) Since we're not animating, we don't need anything in draw
};
}
return sketchProc;
}
function plotLoudness(track) {
// (1) In this function we actually attach the Processing sketch to our canvas
var P = new Processing(track.canvas, loudnessSketch(track));
return P;
}
function rowBackground(row) {
// (1) This function actually plots the loudness in an orphaned canvas
// element--_i.e._ a canvas element not in the DOM--and then converts it
// to https://en.wikipedia.org/wiki/Base64 to construct an image we use
// as the background of a row, on the fly
var trackCanvas = document.createElement('canvas');
var track = trackByHref(row.id);
track.canvas = trackCanvas;
plotLoudness(track);
// You can read more about this technique at:
// http://www.html5canvastutorials.com/advanced/html5-canvas-get-image-data-url/
var binaryImageData = trackCanvas.toDataURL("image/png");
row.style.backgroundImage = "url(" + binaryImageData + ")";
return binaryImageData;
}