-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.html
205 lines (180 loc) · 8.87 KB
/
test.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Freesound API javascript example</title>
<script type="text/javascript" src="freesound.js"></script>
</head>
<body style="font-family: Helvetica">
<script type="text/javascript">
window.onload = function(){
freesound.setToken("YOUR_API_KEY_HERE");
var fields = 'id,name,url,analysis,username';
// Example 1
// Example of geeting the info of a sound, queying for similar sounds (content based) and showing some analysis
// features. Both similar sounds and analysis features are obtained with additional requests to the api.
freesound.getSound(96541,
function(sound){
var msg = "";
msg = "<h3>Getting info of sound: " + sound.name + "</h3>";
msg += "<strong>Url:</strong> " + sound.url + "<br>";
msg += "<strong>Description:</strong> " + sound.description + "<br>";
msg += "<strong>Tags:</strong><ul>";
for (i in sound.tags){
msg += "<li>" + sound.tags[i] + "</li>";
}
msg += "</ul><br>";
msg += "<img src='" + sound.images.waveform_l + "'>";
snd = new Audio(sound.previews['preview-hq-mp3']);
msg += '<br><button onclick="snd.play()">play</button><button onclick="snd.pause()">pause</button><br><br>';
displayMessage(msg,'resp1');
// When we have printed some sound info, ask for analysis
sound.getAnalysis(null,function(analysis){
msg += "<strong>Mfccs:</strong><ul>";
for (i in analysis.lowlevel.mfcc.mean){
msg += "<li>" + analysis.lowlevel.mfcc.mean[i] + "</li>"
}
msg += "</ul>";
displayMessage(msg,'resp1')
// When we have printed the analysis, ask for similar sounds
sound.getSimilar(function(sounds){
msg += "<strong>Similar sounds:</strong><ul>";
for (i =0;i<=10;i++){
var snd = sounds.getSound(i);
msg += "<li>" + snd.id + ": " + snd.url + "</li>"
}
msg += "</ul>";
displayMessage(msg,'resp1')
}, function(){ displayError("Similar sounds could not be retrieved.")},
{fields:fields});
}, function(){ displayError("Analysis could not be retrieved.")},
true);// showAll
}, function(){ displayError("Sound could not be retrieved.")}
);
// Example 2
// Example of searching sounds: querying the freesound db for sounds and retrieving lowlevel descriptors
var query = "violoncello"
var page = 1
var filter = "tag:tenuto duration:[1.0 TO 15.0]"
var sort = "rating_desc"
var descriptors = "lowlevel.spectral_centroid"
freesound.textSearch(query, {page:page, filter:filter, sort:sort, fields:fields, descriptors: descriptors},
function(sounds){
var msg = ""
msg = "<h3>Searching for: " + query + "</h3>"
msg += "With filter: " + filter +" and sorting: " + sort + "<br>"
msg += "Num results: " + sounds.count + "<br><ul>"
for (i =0;i<=10;i++){
var snd = sounds.getSound(i);
msg += "<li>" + snd.name + " by " + snd.username + " with spectral centroid mean value: " + snd.analysis.lowlevel.spectral_centroid.mean.toString() + "</li>"
}
msg += "</ul>"
displayMessage(msg,"resp2")
},function(){ displayError("Error while searching...")}
);
// Example 3
// Example of content based searching
var t = '.lowlevel.pitch_salience.mean:1.0 .lowlevel.pitch.mean:440';
var f = ".lowlevel.pitch.var:[* TO 20] AND .metadata.audio_properties.length:[1 TO 10]";
var page_size = 10;
freesound.contentSearch({target:t,filter:f, page_size : page_size, fields:fields},
function(sounds){
var msg = ""
msg = "<h3>Content based searching</h3>"
msg += "Target: " + t +"<br>"
msg += "Filter: " + f +"<br>"
msg += "Fields: " + fields +"<br>"
msg += "Num results: " + sounds.count + "<br><ul>"
msg += "<li> ---------- PAGE 1 ---------- </li>"
for (i in sounds.results){
msg += "<li>" + sounds.results[i].id+ " | " +
sounds.results[i].name + " | " + sounds.results[i].url + "</li>"
}
msg += "</ul>"
displayMessage(msg,"resp3")
// Once we got the first page of results, go to the following one
sounds.nextPage(
function(sounds){
msg += "<ul><li> ---------- PAGE 2 ---------- </li>"
for (i in sounds.results){
var j = parseInt(i);
msg += "<li>" + sounds.results[j].id.toString(10) +
" | " + sounds.results[j].name + " | " + sounds.results[j].url + "</li>"
}
msg += "</ul>"
displayMessage(msg,"resp3")
},
function(){ displayError("Error getting next page...")})
},function(){ displayError("Error while content based searching...")}
);
// Example 4
// Example of geoquerying
var min_lat = 41.3265528618605;
var max_lat = 41.4504467428547;
var min_lon = 2.005176544189453;
var max_lon = 2.334766387939453;
filterString = "geotag:\"Intersects("+min_lon.toFixed(3)+" "+min_lat.toFixed(3)+" "+max_lon.toFixed(3)+" "+max_lat.toFixed(3)+")\"";
freesound.textSearch("",{filter:filterString, fields:fields},
function(sounds){
var msg = ""
msg = "<h3>Geoquerying</h3>"
msg += "Min lat: " + min_lat +"<br>"
msg += "Max lat: " + max_lat +"<br>"
msg += "Min lon: " + min_lon +"<br>"
msg += "Max lon: " + max_lon +"<br>"
msg += "Num results: " + sounds.count + "<br><ul>"
for (i in sounds.results){
msg += "<li>" + sounds.results[i].id + " | " +
sounds.results[i].name + " | " +
sounds.results[i].url + "</li>";
}
msg += "</ul>"
displayMessage(msg,"resp4")
},function(err){ console.log(err);displayError("Error while geoquerying...")}
);
freesound.getUser("Jovica",
function(user){
var msg = "";
msg = "<h3>User info</h3>";
msg += "Username: " + user.username +"<br>";
// Get user sounds
user.sounds(
function(sounds){
msg += "User sounds:<ul>"
for (i in sounds.results){
msg += "<li>" + sounds.results[i].id + " | " +
sounds.results[i].name + " | " +
sounds.results[i].url + "</li>";
}
msg += "</ul>"
displayMessage(msg,"resp5")
},null,{fields:fields}
)
}, function(){ displayError("Error getting user info...")}
);
};
function displayError(text){
document.getElementById('error').innerHTML=text;
}
function displayMessage(text,place){
document.getElementById(place).innerHTML=text;
}
</script>
<h1>Freesound API javascript example</h1>
<div id="error" style="color:red"></div>
<h2>Example 1</h2>
<div id="resp1"></div>
<hr>
<h2>Example 2</h2>
<div id="resp2"></div>
<hr>
<h2>Example 3</h2>
<div id="resp3"></div>
<hr>
<h2>Example 4</h2>
<div id="resp4"></div>
<hr>
<h2>Example 5</h2>
<div id="resp5"></div>
</body>
</html>