-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-commons.js
289 lines (253 loc) · 10.6 KB
/
audio-commons.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var http = require("https");
// We need this to build our post string
var querystring = require('querystring');
var client_id = "kWisHB1KxJFWuT92ayozRhGe1WC3UKbLtIQo0vnK";
var username = "mprinc";
var password = "kanalizacija";
var download = require('./download');
var transcode = require('./transcode-lame');
var player = require('./player');
var status = {
debug: false,
showError: true,
showFinals: true
}
var tokenCredentials = {
access_token: null,
expires_in: null,
token_type: null,
scope: null,
refresh_token: null
}
var searches = {
active: {},
finished: {}
}
var sounds = {
playing: {},
available: {},
downloading: {}
}
// curl -X POST https://m.audiocommons.org/api/o/token/ -d 'client_id=<YOUR_CLIENT_ID>&grant_type=password&username=<YOUR_USERNAME>&password=<YOUR_PASSWORD>'
function getToken(callback) {
// https://stackoverflow.com/questions/6819143/curl-equivalent-in-nodejs
// https://gist.github.com/bdickason/1105888
// https://www.npmjs.com/package/curlrequest
// https: //github.com/mrsarm/reqclient#logging-with-curl-style
// https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js
// Build the post string from an object
var post_data = querystring.stringify({
'client_id': client_id,
'grant_type': 'password',
'username': username,
'password': password
});
var options = {
host: 'm.audiocommons.org',
// https://stackoverflow.com/questions/15421050/node-request-getting-error-ssl23-get-server-hellounknown-protocol
port: 443,
path: '/api/o/token/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
if (status.debug) console.log("getToken: http path: ", options.path);
var req = http.request(options, function(res) {
var bodyStr = "";
if (status.debug) console.log('STATUS: ' + res.statusCode);
if (status.debug) console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
if (status.debug) console.log('chunk length: ' + chunk.length);
bodyStr += chunk;
});
res.on('end', function() {
if (status.debug) console.log('bodyStr: ' + bodyStr);
var body = JSON.parse(bodyStr);
tokenCredentials.access_token = body.access_token;
tokenCredentials.expires_in = body.expires_in;
tokenCredentials.token_type = body.token_type;
tokenCredentials.scope = body.scope;
tokenCredentials.refresh_token = body.refresh_token;
callback(tokenCredentials);
});
});
req.on('error', function(e) {
if (status.showError) console.error('problem with request: ' + e.message);
});
// write data to request body
if (status.debug) console.log("Request data: ", post_data);
req.write(post_data);
req.end();
};
function searchHigh(searchQuery) {
searches.active[searchQuery] = {}
search(searchQuery, function(metaResult) {
if (status.debug) console.log("Search metaResult: ", metaResult);
var collect_url = metaResult.meta.collect_url;
// example: https://m.audiocommons.org/api/v1/collect/?rid=b8f9ad41-c3a5-4417-baa2-046864a9cc34
if (status.debug) console.log("Collect url: ", collect_url);
// var rid = /rid\=([^/]*)$/.exec(collect_url)[1];
var rid = metaResult.meta.response_id;
if (status.debug) console.log("Collect rid: ", rid);
var collectRequestNo = 1;
function collectFinished(result) {
if (!result.contents.Freesound) {
if (status.debug) console.log("Collecting try num: ", collectRequestNo++);
collect(rid, collectFinished);
} else {
delete searches.active[searchQuery];
searches.finished[searchQuery] = {
freesound: result.contents.Freesound.results
};
if (status.debug) console.log("Search result: ", result);
if (status.showFinals) console.log("Search for '%s' finished. You can download sound:\n\tac.downloadHigh('%s', 'freesound', 1)", searchQuery, searchQuery, );
}
}
if (status.debug) console.log("Collecting iteration num: ", collectRequestNo++);
ac.collect(rid, collectFinished);
});
}
function downloadHigh(searchQuery, source, soundIndex, shouldPlay) {
var soundInfo = searches.finished[searchQuery][source][soundIndex];
if (!sounds.downloading[searchQuery]) sounds.downloading[searchQuery] = {};
var soundName = soundInfo['ac:name'];
soundName = soundName.replace(/ /g, "_");
soundName = soundName.replace(/\!/g, "_");
soundName = soundName.replace(/\./g, "_");
soundName = soundName.replace(/__/g, "_");
soundName = soundName.toLowerCase();
var previewUrl = soundInfo["ac:preview_url"];
var downloadName = /[^/]*$/.exec(previewUrl)[0];
if (status.debug) console.log("Freesound preview sound url: ", previewUrl);
var downloadPath = "./sounds/" + downloadName;
download.downloadFile(previewUrl, downloadPath,
function(err) {
if (err) {
if (status.showError) console.error(err);
} else {
if (status.showFinals) console.log("Downloaded sound: %s", soundName);
var previewSoundExtension = /[^.]*$/.exec(downloadName)[0];
if (status.debug) console.log("File extension %s", previewSoundExtension);
if (previewSoundExtension != 'mp3') {
if (status.showFinals) console.log("Transcoding sound from %s to mp3", previewSoundExtension);
var outputName = /(.*)\.[^.]*$/.exec(downloadName)[1];
outputNameFull = "./sounds/" + outputName + ".mp3";
transcode.transcodeFile(downloadPath, outputNameFull, function() {
if (!sounds.available[searchQuery]) sounds.available[searchQuery] = {};
var sound;
sounds.available[searchQuery][soundName] = {
fileName: outputNameFull,
play: function() {
sound = player.play(this.fileName);
if (status.showFinals) console.log("You can stop sound with: ac.sounds.available.%s.%s.stop()", searchQuery, soundName);
},
stop: function() {
sound.kill();
}
}
var shortName = source[0] + soundIndex;
sounds.available[searchQuery][shortName] = sounds.available[searchQuery][soundName];
if (shouldPlay) player.play(outputNameFull);
else {
if (status.showFinals) {
console.log("You can play sound with:\n");
console.log("\tac.sounds.available.%s.%s.play()", searchQuery, soundName);
console.log("\tac.sounds.available.%s.%s.play()", searchQuery, shortName);
}
}
});
}
}
});
sounds.downloading[searchQuery][soundName] = {
request: true
}
}
// curl -i -H "Authorization: Bearer MMOeBDmt0mHC6NY99xH6bN9yfPkMck" https://m.audiocommons.org/api/v1/search/text/?q=cars
function search(searchQuery, callback) {
var options = {
host: 'm.audiocommons.org',
port: 443,
path: '/api/v1/search/text/?q=' + searchQuery,
method: 'GET',
headers: {
'Authorization': "Bearer " + tokenCredentials.access_token
}
};
if (status.debug) console.log("search: http path: ", options.path);
var req = http.request(options, function(res) {
var bodyStr = "";
if (status.debug) console.log('STATUS: ' + res.statusCode);
if (status.debug) console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
if (status.debug) console.log('chunk length: ' + chunk.length);
bodyStr += chunk;
});
res.on('end', function() {
if (status.debug) console.log('bodyStr: ' + bodyStr);
var body = JSON.parse(bodyStr);
callback(body);
});
});
req.on('error', function(e) {
if (status.showError) console.error('problem with request: ' + e.message);
});
req.end();
};
// curl -i -H "Authorization: Bearer MMOeBDmt0mHC6NY99xH6bN9yfPkMck" https://m.audiocommons.org/api/v1/collect/?rid=ed360c90-3221-4ecc-a471-01ec5ca4a915
function collect(rid, callback) {
// rid = 'b8f9ad41-c3a5-4417-baa2-046864a9cc34'
// https://m.audiocommons.org/api/v1/collect/?rid=b8f9ad41-c3a5-4417-baa2-046864a9cc34
// rid = '937f1086-c42e-4aa2-bfc5-7ce516e914a0';
// rid = '3e376fb8-1eab-4059-a848-074454dd8028';
var options = {
host: 'm.audiocommons.org',
port: 443,
path: '/api/v1/collect/?rid=' + rid,
method: 'GET',
headers: {
'Authorization': "Bearer " + tokenCredentials.access_token
}
};
if (status.debug) console.log("collect: http path: ", options.path);
var req = http.request(options, function(res) {
var bodyStr = "";
if (status.debug) console.log('STATUS: ' + res.statusCode);
if (status.debug) console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
if (status.debug) console.log('chunk length: ' + chunk.length);
bodyStr += chunk;
});
res.on('end', function() {
if (status.debug) console.log('bodyStr: ' + bodyStr);
var body = JSON.parse(bodyStr);
callback(body);
});
});
req.on('error', function(e) {
if (status.showError) console.error('problem with request: ' + e.message);
});
req.end();
}
function init() {
ac.getToken(function(tokenCredentials) {
console.log("Initialized. Go on! Use:\n\tac.searchHigh('dance')");
});
}
// AC API
exports.getToken = getToken;
exports.search = search;
exports.collect = collect;
// high-level
exports.init = init;
exports.searchHigh = searchHigh;
exports.downloadHigh = downloadHigh;
// data
exports.searches = searches;
exports.status = status;
exports.sounds = sounds;