-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongInterface.js
260 lines (243 loc) · 7.48 KB
/
songInterface.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var fs = require('fs');
var mongodb = require('mongodb'),
mongoserver = new mongodb.Server('localhost', 26374),
dbConnector = new mongodb.Db('uenergy', mongoserver);
dbConnector.open(function(err, DB){
if(err){
console.log('oh shit! connector.open error!');
console.log(err.message);
}
else{
db = DB;
console.log('opened successfully');
db.createCollection('songs', function(err, song){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('songs created');
songs = song;
db.createCollection('rbDE', function(err, rb){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('rb created');
rbDE = rb;
db.createCollection('hiphopDE', function(err, hiphop){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('hiphop created');
hiphopDE = hiphop;
db.createCollection('skaDE', function(err, ska){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('ska created');
skaDE = ska;
db.createCollection('latinDE', function(err, latin){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('latin created');
latinDE = latin;
db.createCollection('countryDE', function(err, country){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('country created');
countryDE = country;
db.createCollection('bluesDE', function(err, blues){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('blues created');
bluesDE = blues;
db.createCollection('electroDE', function(err, electro){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('electro created');
electroDE = electro;
db.createCollection('rockDE', function(err, rock){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('rock created');
rockDE = rock;
db.createCollection('popDE', function(err, pop){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('pop created');
popDE = pop;
db.createCollection('jazzDE', function(err, jazz){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('jazz created');
jazzDE = jazz;
db.createCollection('moodDE', function(err, mood){
if(err){
console.log('oh shit! db.createCollection error!');
console.log(err);
return;
}
console.log('moods created');
moodDE = mood;
});});});});});});});});});});});});
}
});
function getSongs(response, query){
var minX = parseFloat(query.minX);
var minY = parseFloat(query.minY);
var maxX = parseFloat(query.maxX);
var maxY = parseFloat(query.maxY);
var maxL = Math.min(maxX-minX,maxY-minY)/6;
sort = query.sort;
genre = query.genre;
if(minX < 0 || maxX > 1 || minY < 0 || maxY > 1){
console.log('getSongs attempt with inappropriate bounds');
return;
}
if(genre){
var geo;
switch(parseInt(genre)){
case 0: geo = bluesDE; genre = 'Blues'; break;
case 1: geo = countryDE; genre = 'Country/Folk'; break;
case 2: geo = electroDE; genre = 'Electronic'; break;
case 3: geo = hiphopDE; genre = 'Hip Hop'; break;
case 4: geo = jazzDE; genre = 'Jazz'; break;
case 5: geo = latinDE; genre = 'Latin American'; break;
case 6: geo = popDE; genre = 'Pop'; break;
case 7: geo = rbDE; genre = 'R&B'; break;
case 8: geo = rockDE; genre = 'Rock'; break;
case 9: geo = skaDE; genre = 'Ska'; break;
default: return;
}
getResults(geo);
}
else{
getResults(moodDE);
}//else
function getResults(geo){
var results = new Array();
var mnx, mxx, mxy, mny; //current min and max x and ys
var px, py; //current x/y pos
var count = 36;
for(var i = 0; i<6; i++){
if(i == 0)
mnx = minX;
else
mnx = mxx;
mxx = lerp(minX, maxX, (i+1)/6);
for(var j = 0; j<6; j++){
if(j == 0)
mny = minY;
else
mny = mxy;
mxy = lerp(minY, maxY, (j+1)/6);
var pos = {}
if(!sort){// && maxL > 0.025){
//console.log('mnx '+mnx+' mxx '+mxx+' mny '+mny+' mxy '+mxy);
px = map(parseFloat(Math.random()),0,1,mnx,mxx);
py = map(parseFloat(Math.random()),0,1,mny,mxy);
//console.log('px: ' + px + ' py: '+py+'\n');
pos['$near'] = [px,py];
pos['$maxDistance'] = maxL;//0.02;
}
//console.log(JSON.stringify(pos));
else{
var within = {}
within['$box'] = [[mnx,mny],[mxx,mxy]];
pos['$within'] = within;
}
var doc = {'pos':pos};
//doc['pos'] = pos;
if(sort==1){geo.find(doc, {'sort':[['hottness','desc']],'limit':1},postFind);}
else if(sort==2){geo.find(doc, {'sort':[['hottness','asc']],'limit':1},postFind);}
else{geo.findOne(doc, postFind);}
function postFind(err, returned){
if(err){
console.log('oh shit! error using geo.find!');
console.log(err.message);
count--;
if(count == 0)
sendData(results, response);
}
else if(sort){returned.nextObject(songFunc);}
else{songFunc(err,returned);}
}
function songFunc(err, song){
if(err){
console.log('oh shit! error using returned.nextObject!');
console.log(err);
count--;
if(count == 0)
sendData(results, response);
}
else if(song != null){
songs.findOne({'_id':song._id}, function(err, input){
if(err){
console.log('songs.findOne error.');
console.log(err.message);
count--;
if(count == 0)
sendData(results, response);
}
else{
if(genre)
input.song.genre = genre;
results.push(input);
count--;
if(count == 0){
sendData(results, response);
}
}
});
}
else{
count--;
if(count == 0){
sendData(results, response);
}
}
}
}
}
}
function sendData(results, response){
var data = {}
data['results'] = results;
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify(results));
}
function map(num, minold, maxold, minnew, maxnew){
return ((num-minold)/(maxold - minold))*(maxnew - minnew) + minnew;
}
function lerp(min, max, i){
return min+((max-min)*i);
}
}
exports.getSongs = getSongs;