Repository navigation
Expand file tree
/
Copy pathtest.html
More file actions
195 lines (174 loc) · 8.59 KB
/
Copy pathtest.html
File metadata and controls
195 lines (174 loc) · 8.59 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
<!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(""); // Replace with your API key
// 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.mfcc){
msg += "<li>" + analysis.mfcc[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: "id,url"});
}, 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 some audio descriptors
var query = "violoncello"
var page = 1
var filter = "tag:tenuto duration:[1.0 TO 15.0]"
var sort = "rating_desc"
var fields = 'id,name,url,analysis,username,spectral_centroid';
freesound.search(query, {page:page, filter:filter, sort:sort, fields:fields},
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.spectral_centroid.toString() + "</li>"
}
msg += "</ul>"
displayMessage(msg,"resp2")
},function(){ displayError("Error while searching...")}
);
// Example 3
// Example of sorting sounds using some audio descriptors target, and filtering using audio descriptors
var sortingTarget = "pitch_salience:1.0,pitch:440"
var filter = "pitch_var:[0 TO 20] AND duration:[1 TO 10] AND tag:single-note";
var page_size = 10;
var fields = 'id,name,url,username,pitch,pitch_salience,pitch_var,duration,tags';
freesound.search("", {filter:filter, page_size:page_size, fields:fields, sort:sortingTarget},
function(sounds){
var msg = ""
msg = "<h3>Searching sounds by specifying audio descriptors as a target, and filtering by audio descriptor values and metadata</h3>"
msg += "Filter: " + filter +"<br>"
msg += "Fields: " + fields +"<br>"
msg += "Num results: " + sounds.count + "<br><ul>"
msg += "---------- PAGE 1 ----------"
for (i in sounds.results){
msg += "<pre>" + JSON.stringify(sounds.results[i], null, 2) + "</pre>"
}
displayMessage(msg,"resp3")
// Once we got the first page of results, go to the following one
sounds.nextPage(
function(sounds){
msg += "---------- PAGE 2 ----------"
for (i in sounds.results){
var j = parseInt(i);
msg += "<pre>" + JSON.stringify(sounds.results[j], null, 2) + "</pre>"
}
displayMessage(msg,"resp3")
},
function(){ displayError("Error getting next page...")})
},function(e){ console.log(e)}
);
// 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.search("",{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>