-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundDownload.py
138 lines (116 loc) · 5.69 KB
/
soundDownload.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
import os, sys
import freesound as fs
import json
descriptors = [ 'lowlevel.spectral_centroid.mean',
'lowlevel.spectral_contrast.mean',
'lowlevel.dissonance.mean',
'lowlevel.hfc.mean',
'lowlevel.mfcc.mean',
'sfx.logattacktime.mean',
'sfx.inharmonicity.mean']
def downloadSoundsFreesound(queryText = "", tag=None, duration=None, API_Key = "hjNYO8PPFWKuvcboJo9YEhtGRSv8TFwNsYY5LWVM", outputDir = "", topNResults = 5, featureExt = '.json'):
"""
This function downloads sounds and their descriptors from freesound using the queryText and the
tag specified in the input. Additionally, you can also specify the duration range to filter sounds
based on duration.
Inputs:
(Input parameters marked with a * are optional)
queryText (string): query text for the sounds (eg. "violin", "trumpet", "cello", "bassoon" etc.)
tag* (string): tag to be used for filtering the searched sounds. (eg. "multisample",
"single-note" etc.)
duration* (tuple): min and the max duration (seconds) of the sound to filter, eg. (0.2,15)
API_Key (string): your api key, which you can obtain from : www.freesound.org/apiv2/apply/
outputDir (string): path to the directory where you want to store the sounds and their
descriptors
topNResults (integer): number of results(sounds) that you want to download
featureExt (string): file extension for storing sound descriptors
output:
This function downloads sounds and descriptors, and then stores them in outputDir. In
outputDir it creates a directory of the same name as that of the queryText. In this
directory outputDir/queryText it creates a directory for every sound with the name
of the directory as the sound id. Additionally, this function also dumps a text file
containing sound-ids and freesound links for all the downloaded sounds in the outputDir.
NOTE: If the directory outputDir/queryText exists, it deletes the existing contents
and stores only the sounds from the current query.
"""
# Checking for the compulsory input parameters
if queryText == "":
print("\n")
print("Provide a query text to search for sounds")
return -1
if API_Key == "":
print("\n")
print("You need a valid freesound API key to be able to download sounds.")
print("Please apply for one here: www.freesound.org/apiv2/apply/")
print("\n")
return -1
if outputDir == "" or not os.path.exists(outputDir):
print("\n")
print("Please provide a valid output directory. This will be the root directory for storing sounds and descriptors")
return -1
# Setting up the Freesound client and the authentication key
fsClnt = fs.FreesoundClient()
fsClnt.set_token(API_Key,"token")
# Creating a duration filter string that the Freesound API understands
if duration and type(duration) == tuple:
flt_dur = " duration:[" + str(duration[0])+ " TO " +str(duration[1]) + "]"
else:
flt_dur = ""
if tag and type(tag) == str:
flt_tag = "tag:"+tag
else:
flt_tag = ""
# Querying Freesound
page_size = 30
if not flt_tag + flt_dur == "":
qRes = fsClnt.text_search(query=queryText ,filter = flt_tag + flt_dur,sort="score", fields="id,name,previews,username,url,analysis", descriptors=','.join(descriptors), page_size=page_size, normalized=1)
else:
qRes = fsClnt.text_search(query=queryText ,sort="score",fields="id,name,previews,username,url,analysis", descriptors=','.join(descriptors), page_size=page_size, normalized=1)
outDir2 = os.path.join(outputDir, queryText)
if os.path.exists(outDir2): # If the directory exists, it deletes it and starts fresh
os.system("rm -r " + outDir2)
os.mkdir(outDir2)
pageNo = 1
sndCnt = 0
indCnt = 0
totalSnds = min(qRes.count,200) # System quits after trying to download after 200 times
# Creating directories to store output and downloading sounds and their descriptors
downloadedSounds = []
while(1):
if indCnt >= totalSnds:
print("Not able to download required number of sounds. Either there are not enough search results on freesound for your search query and filtering constraints or something is wrong with this script.")
break
sound = qRes[indCnt - ((pageNo-1)*page_size)]
print("Downloading mp3 preview and descriptors for sound with id: %s"%str(sound.id))
outDir1 = os.path.join(outputDir, queryText, str(sound.id))
if os.path.exists(outDir1):
os.system("rm -r " + outDir1)
os.system("mkdir " + outDir1)
mp3Path = os.path.join(outDir1, str(sound.previews.preview_lq_mp3.split("/")[-1]))
ftrPath = mp3Path.replace('.mp3', featureExt)
try:
fs.FSRequest.retrieve(sound.previews.preview_hq_mp3, fsClnt, mp3Path)
# Initialize a dictionary to store descriptors
features = {}
# Obtaining all the descriptors
for desc in descriptors:
features[desc]=[]
features[desc].append(eval("sound.analysis."+desc))
# Once we have all the descriptors, store them in a json file
json.dump(features, open(ftrPath,'w'))
sndCnt+=1
downloadedSounds.append([str(sound.id), sound.url])
except:
if os.path.exists(outDir1):
os.system("rm -r " + outDir1)
indCnt +=1
if indCnt%page_size==0:
qRes = qRes.next_page()
pageNo+=1
if sndCnt>=topNResults:
break
# Dump the list of files and Freesound links
fid = open(os.path.join(outDir2, queryText+'_SoundList.txt'), 'w')
for elem in downloadedSounds:
fid.write('\t'.join(elem)+'\n')
fid.close()