-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
56 lines (51 loc) · 2.38 KB
/
example.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
/*
var ac = require('./audio-commons');
ac.init();
ac.searchHigh("dance");
ac.downloadHigh("dance", "freesound", 5);
ac.sounds.available.dance.dance_pt_6_wav.play();
ac.sounds.available.dance.dance_pt_6_wav.stop();
*/
var ac = require('./audio-commons');
var download = require('./download');
var transcode = require('./transcode-lame');
var player = require('./player');
ac.getToken(function(tokenCredentials) {
console.log('tokenCredentials.access_token: ' + tokenCredentials.access_token);
ac.search("dance", function(metaResult) {
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
console.log("Collect url: ", collect_url);
// var rid = /rid\=([^/]*)$/.exec(collect_url)[1];
var rid = metaResult.meta.response_id;
console.log("Collect rid: ", rid);
var collectRequestNo = 1;
function collectFinished(result) {
if (!result.contents.Freesound) {
console.log("Collecting try num: ", collectRequestNo++);
ac.collect(rid, collectFinished);
} else {
console.log("Search result: ", result);
var previewUrl = result.contents.Freesound.results[0]["ac:preview_url"];
var downloadName = /[^/]*$/.exec(previewUrl)[0];
console.log("Freesound preview sound url: ", previewUrl);
var downloadPath = "./sounds/" + downloadName;
download.downloadFile(previewUrl, downloadPath,
function(err) {
console.log("Downloaded sound: ", previewUrl);
if (err) { console.error(err) } else {
var previewSoundExtension = /[^.]*$/.exec(downloadName)[0];
var outputName = /(.*)\.[^.]*$/.exec(downloadName)[1];
outputNameFull = "./sounds/" + outputName + ".mp3";
transcode.transcodeFile(downloadPath, outputNameFull, function() {
player.play(outputNameFull);
});
}
});
}
}
console.log("Collecting try num: ", collectRequestNo++);
ac.collect(rid, collectFinished);
});
})