-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathexamples.py
220 lines (202 loc) · 6.37 KB
/
examples.py
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
import os
import sys
import freesound
api_key = os.getenv('FREESOUND_API_KEY', None)
if api_key is None:
print("You need to set your API key as an environment variable")
print("named FREESOUND_API_KEY")
sys.exit(-1)
freesound_client = freesound.FreesoundClient()
freesound_client.set_token(api_key)
# Get sound info example
print("Sound info:")
print("-----------")
sound = freesound_client.get_sound(96541)
print("Getting sound:", sound.name)
print("Url:", sound.url)
print("Description:", sound.description)
print("Tags:", " ".join(sound.tags))
print()
# Get sound info example specifying some request parameters
print("Sound info specifying some request parameters:")
print("-----------")
sound = freesound_client.get_sound(
96541, fields="id,name,username,duration,analysis", descriptors="lowlevel.spectral_centroid", normalized=1
)
print("Getting sound:", sound.name)
print("Username:", sound.username)
print("Duration:", str(sound.duration), "(s)")
print("Spectral centroid:",)
print(sound.analysis.lowlevel.spectral_centroid.as_dict())
print()
# Get sound analysis example
print("Get analysis:")
print("-------------")
analysis = sound.get_analysis()
mfcc = analysis.lowlevel.mfcc.mean
print("Mfccs:", mfcc)
# you can also get the original json (this applies to any FreesoundObject):
print(analysis.as_dict())
print()
# Get sound analysis example specifying some request parameters
print("Get analysis with specific normalized descriptor:")
print("-------------")
analysis = sound.get_analysis(descriptors="lowlevel.spectral_centroid.mean", normalized=1)
spectral_centroid_mean = analysis.lowlevel.spectral_centroid.mean
print("Normalized mean of spectral centroid:", spectral_centroid_mean)
print()
# Get similar sounds example
print("Similar sounds: ")
print("---------------")
results_pager = sound.get_similar()
for similar_sound in results_pager:
print("\t-", similar_sound.name, "by", similar_sound.username)
print()
# Get similar sounds example specifying some request parameters
print("Similar sounds specifying some request parameters:")
print("---------------")
results_pager = sound.get_similar(
page_size=10, fields="name,username", descriptors_filter="lowlevel.pitch.mean:[110 TO 180]"
)
for similar_sound in results_pager:
print("\t-", similar_sound.name, "by", similar_sound.username)
print()
# Search Example
print("Searching for 'violoncello':")
print("----------------------------")
results_pager = freesound_client.text_search(
query="violoncello",
filter="tag:tenuto duration:[1.0 TO 15.0]",
sort="rating_desc",
fields="id,name,previews,username"
)
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
print("\t----- PAGE 2 -----")
results_pager = results_pager.next_page()
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
print()
# Content based search example
print("Content based search:")
print("---------------------")
results_pager = freesound_client.content_based_search(
descriptors_filter="lowlevel.pitch.var:[* TO 20]",
target='lowlevel.pitch_salience.mean:1.0 lowlevel.pitch.mean:440'
)
print("Num results:", results_pager.count)
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
print()
# Getting sounds from a user example
print("User sounds:")
print("-----------")
user = freesound_client.get_user("InspectorJ")
print("User name:", user.username)
results_pager = user.get_sounds()
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
print("\t----- PAGE 2 -----")
results_pager = results_pager.next_page()
for sound in results_pager:
print("\t-", sound.name, "by", sound.username)
print()
# Getting sounds from a user example specifying some request parameters
print("User sounds specifying some request parameters:")
print("-----------")
user = freesound_client.get_user("Headphaze")
print("User name:", user.username)
results_pager = user.get_sounds(
page_size=10, fields="name,username,samplerate,duration,analysis", descriptors="rhythm.bpm"
)
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for sound in results_pager:
print(
"\t-",
sound.name,
"by",
sound.username,
)
print(
", with sample rate of",
sound.samplerate,
"Hz and duration of",
)
print(sound.duration, "s")
print("\t----- PAGE 2 -----")
results_pager = results_pager.next_page()
for sound in results_pager:
print(
"\t-",
sound.name,
"by",
sound.username,
)
print(
", with sample rate of",
sound.samplerate,
"Hz and duration of",
)
print(sound.duration, "s")
print()
# Getting sounds from a pack example specifying some request parameters
print("Pack sounds specifying some request parameters:")
print("-----------")
pack = freesound_client.get_pack(3524)
print("Pack name:", pack.name)
results_pager = pack.get_sounds(
page_size=5, fields="id,name,username,duration,analysis", descriptors="lowlevel.spectral_flatness_db"
)
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for sound in results_pager:
print(
"\t-",
sound.name,
"by",
sound.username,
", with duration of",
)
print(
sound.duration,
"s and a mean spectral flatness of",
)
print(sound.analysis.lowlevel.spectral_flatness_db.mean)
print("\t----- PAGE 2 -----")
results_pager = results_pager.next_page()
for sound in results_pager:
print(
"\t-",
sound.name,
"by",
sound.username,
", with duration of",
)
print(
sound.duration,
"s and a mean spectral flatness of",
)
print(sound.analysis.lowlevel.spectral_flatness_db.mean)
print()
# Getting bookmark categories from a user example
print("User bookmark categories:")
print("-----------")
user = freesound_client.get_user("frederic.font")
print("User name:", user.username)
results_pager = user.get_bookmark_categories(page_size=10)
print("Num results:", results_pager.count)
print("\t----- PAGE 1 -----")
for bookmark_category in results_pager:
print(
"\t-",
bookmark_category.name,
"with",
bookmark_category.num_sounds,
)
print("sounds at", bookmark_category.url)
print()