/
urbansed.py
303 lines (227 loc) · 13.5 KB
/
urbansed.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""URBAN-SED Dataset Loader
.. admonition:: Dataset Info
:class: dropdown
**URBAN-SED**
| URBAN-SED (c) by Justin Salamon, Duncan MacConnell, Mark Cartwright, Peter Li, and Juan Pablo Bello.
| URBAN-SED is licensed under a Creative Commons Attribution 4.0 International License (CC BY 4.0).
| You should have received a copy of the license along with this work. If not, see <http://creativecommons.org/licenses/by/4.0/>.
*Created By:*
| Justin Salamon*^, Duncan MacConnell*, Mark Cartwright*, Peter Li*, and Juan Pablo Bello*.
| * Music and Audio Research Lab (MARL), New York University, USA
| ^ Center for Urban Science and Progress (CUSP), New York University, USA
| http://urbansed.weebly.co
| http://steinhardt.nyu.edu/marl/
| http://cusp.nyu.edu/
*Version 2.0.0*
* Audio files generated with scaper v0.1.0 (identical to audio in URBAN-SED 1.0)
* Jams annotation files generated with scaper v0.1.0 and updated to comply with scaper v1.0.0 (namespace changed from "sound_event" to "scaper")
* NOTE: due to updates to the scaper library, regenerating the audio from the jams annotations using scaper >=1.0.0 will result in audio files that are highly similar, but not identical, to the audio files provided. This is because the provided audio files were generated with scaper v0.1.0 and have been purposely kept the same as in URBAN-SED v1.0 to ensure comparability to previously published results.
*Description*
URBAN-SED is a dataset of 10,000 soundscapes with sound event annotations generated using scaper (github.com/justinsalamon/scaper).
A detailed description of the dataset is provided in the following article:
.. code-block:: latex
J. Salamon, D. MacConnell, M. Cartwright, P. Li, and J. P. Bello. "Scaper: A Library for Soundscape Synthesis and Augmentation",
In IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (WASPAA), New Paltz, NY, USA, Oct. 2017.
A summary is provided here:
* The dataset includes 10,000 soundscapes, totals almost 30 hours and includes close to 50,000 annotated sound events
* Complete annotations are provided in JAMS format, and simplified annotations are provided as tab-separated text files
* Every soundscape is 10 seconds long and has a background of Brownian noise resembling the typical "hum" often heard in urban environments
* Every soundscape contains between 1-9 sound events from the following classes:
* air_conditioner, car_horn, children_playing, dog_bark, drilling, engine_idling, gun_shot, jackhammer, siren and street_music
* The source material for the sound events are the clips from the UrbanSound8K dataset
* URBAN-SED comes pre-sorted into three sets: train, validate and test:
* There are 6000 soundscapes in the training set, generated using clips from folds 1-6 in UrbanSound8K
* There are 2000 soundscapes in the validation set, generated using clips from folds 7-8 in UrbanSound8K
* There are 2000 soundscapes in the test set, generated using clips from folds 9-10 in UrbanSound8K
* Further details about how the soundscapes were generated including the distribution of sound event start times, durations, signal-to-noise ratios, pitch shifting, time stretching, and the range of sound event polyphony (overlap) can be found in Section 3 of the aforementioned scaper paper
* The scripts used to generated URBAN-SED using scaper can be found here: https://github.com/justinsalamon/scaper_waspaa2017/tree/master/notebooks
*Audio Files Included*
* 10,000 synthesized soundscapes in single channel (mono), 44100Hz, 16-bit, WAV format.
* The files are split into a training set (6000), validation set (2000) and test set (2000).
*Annotation Files Included*
The annotations list the sound events that occur in every soundscape. The annotations are "strong", meaning for every
sound event the annotations include (at least) the start time, end time, and label of the sound event. Sound events
come from the following 10 labels (categories): air_conditioner, car_horn, children_playing, dog_bark, drilling, engine_idling,
gun_shot, jackhammer, siren, street_music
There are two types of annotations: full annotations in JAMS format, and simplified annotations in
tab-separated txt format.
*JAMS Annotations*
* The full annotations are distributed in JAMS format (https://github.com/marl/jams).
* There are 10,000 JAMS annotation files, each one corresponding to a single soundscape with the same filename (other than the extension)
* Each JAMS file contains a single annotation in the scaper namespace format - jams >=v0.3.2 is required in order to load the annotation into python with jams:
import jams
jam = jams.load('soundscape_train_bimodal0.jams').
* The value of each observation (sound event) is a dictionary storing all scaper-related sound event parameters:
* label, source_file, source_time, event_time, event_duration, snr, role, pitch_shift, time_stretch.
* Note: the event_duration stored in the value dictionary represents the specified duration prior to any time stretching. The actual event durtation in the soundscape is stored in the duration field of the JAMS observation.
* The observations (sound events) in the JAMS annotation include both foreground sound events and the background(s).
* The probabilistic scaper foreground and background event specifications are stored in the annotation's sandbox, allowing a complete reconstruction of the soundscape audio from the JAMS annotation (assuming access to the original source material) using scaper.generate_from_jams('soundscape_train_bimodal0.jams').
* The annotation sandbox also includes additional metadata such as the total number of foreground sound events, the maximum polyphony (sound event overlap) of the soundscape and its gini coefficient (a measure of soundscape complexity).
*Simplified Annotations*
* The simplified annotations are distributed as tab-separated text files.
* There are 10,000 simplified annotation files, each one corresponding to a single soundscape with the same filename (other than the extension)
* Each simplified annotation has a 3-column format (no header): start_time, end_time, label.
* Background sounds are NOT included in the simplified annotations (only foreground sound events)
* No additional information is stored in the simplified events (see the JAMS annotations for more details).
*Please Acknowledge URBAN-SED in Academic Research*
When URBAN-SED is used for academic research, we would highly appreciate it if scientific publications of works
partly based on the URBAN-SED dataset cite the following publication:
.. code-block:: latex
J. Salamon, D. MacConnell, M. Cartwright, P. Li, and J. P. Bello. "Scaper: A Library for Soundscape Synthesis and Augmentation",
In IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (WASPAA), New Paltz, NY, USA, Oct. 2017.
The creation of this dataset was supported by NSF award 1544753.
*Conditions of Use*
Dataset created by J. Salamon, D. MacConnell, M. Cartwright, P. Li, and J. P. Bello. Audio files contain excerpts of
recordings uploaded to www.freesound.org. Please see FREESOUNDCREDITS.txt for an attribution list.
The URBAN-SED dataset is offered free of charge under the terms of the Creative Commons
Attribution 4.0 International License (CC BY 4.0): http://creativecommons.org/licenses/by/4.0/
The dataset and its contents are made available on an "as is" basis and without warranties of any kind, including
without limitation satisfactory quality and conformity, merchantability, fitness for a particular purpose, accuracy or
completeness, or absence of errors. Subject to any liability that may not be excluded or limited by law, NYU is not
liable for, and expressly excludes, all liability for loss or damage however and whenever caused to anyone by any use of
the URBAN-SED dataset or any part of it.
*Feedback*
| Please help us improve URBAN-SED by sending your feedback to: justin.salamon@nyu.edu
| In case of a problem report please include as many details as possible.
"""
import os
from typing import BinaryIO, Optional, TextIO, Tuple
import librosa
import numpy as np
import csv
import jams
import glob
from soundata import download_utils
from soundata import jams_utils
from soundata import core
from soundata import annotations
from soundata import io
BIBTEX = """
@inproceedings{Salamon:Scaper:WASPAA:17,
Address = {New Paltz, NY, USA},
Author = {Salamon, J. and MacConnell, D. and Cartwright, M. and Li, P. and Bello, J.~P.},
Booktitle = {IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (WASPAA)},
Month = {Oct.},
Pages = {344--348},
Title = {Scaper: A Library for Soundscape Synthesis and Augmentation},
Year = {2017}}
"""
REMOTES = {
"all": download_utils.RemoteFileMetadata(
filename="URBAN-SED_v2.0.0.tar.gz",
url="https://zenodo.org/record/1324404/files/URBAN-SED_v2.0.0.tar.gz?download=1",
checksum="a2d24a2148ece7c021fcc079ee87c2dc",
unpack_directories=["URBAN-SED_v2.0.0"],
)
}
LICENSE_INFO = "Creative Commons Attribution 4.0 International"
class Clip(core.Clip):
"""URBAN-SED Clip class
Args:
clip_id (str): id of the clip
Attributes:
audio (np.ndarray, float): path to the audio file
audio_path (str): path to the audio file
clip_id (str): clip id
events (soundata.annotations.Events): sound events with start time, end time, label and confidence
split (str): subset the clip belongs to (for experiments): train, validate, or test
"""
def __init__(self, clip_id, data_home, dataset_name, index, metadata):
super().__init__(clip_id, data_home, dataset_name, index, metadata)
self.audio_path = self.get_path("audio")
self.jams_path = self.get_path("jams")
self.txt_path = self.get_path("txt")
@property
def audio(self) -> Optional[Tuple[np.ndarray, float]]:
"""The clip's audio
Returns:
* np.ndarray - audio signal
* float - sample rate
"""
return load_audio(self.audio_path)
@property
def split(self):
"""The data splits (e.g. train)
Returns
* str - split
"""
return self._clip_metadata.get("split")
@core.cached_property
def events(self) -> Optional[annotations.Events]:
"""The audio events
Returns
* annotations.Events - audio event object
"""
return load_events(self.txt_path)
def to_jams(self):
"""Get the clip's data in jams format
Returns:
jams.JAMS: the clip's data in jams format
"""
jam = jams.load(self.jams_path)
jam.annotations[0].annotation_metadata.data_source = "soundata"
return jam
@io.coerce_to_bytes_io
def load_audio(fhandle: BinaryIO, sr=None) -> Tuple[np.ndarray, float]:
"""Load a UrbanSound8K audio file.
Args:
fhandle (str or file-like): File-like object or path to audio file
sr (int or None): sample rate for loaded audio, None by default, which
uses the file's original sample rate of 44100 without resampling.
Returns:
* np.ndarray - the mono audio signal
* float - The sample rate of the audio file
"""
audio, sr = librosa.load(fhandle, sr=sr, mono=True)
return audio, sr
@io.coerce_to_string_io
def load_events(fhandle: TextIO) -> annotations.Events:
"""Load an URBAN-SED sound events annotation file
Args:
fhandle (str or file-like): File-like object or path to the sound events annotation file
Raises:
IOError: if txt_path doesn't exist
Returns:
Events: sound events annotation data
"""
times = []
labels = []
confidence = []
reader = csv.reader(fhandle, delimiter="\t")
for line in reader:
times.append([float(line[0]), float(line[1])])
labels.append(line[2])
confidence.append(1.0)
events_data = annotations.Events(
np.array(times), "seconds", labels, "open", np.array(confidence)
)
return events_data
@core.docstring_inherit(core.Dataset)
class Dataset(core.Dataset):
"""
The URBAN-SED dataset
"""
def __init__(self, data_home=None):
super().__init__(
data_home,
name="urbansed",
clip_class=Clip,
bibtex=BIBTEX,
remotes=REMOTES,
license_info=LICENSE_INFO,
)
@core.copy_docs(load_audio)
def load_audio(self, *args, **kwargs):
return load_audio(*args, **kwargs)
@core.cached_property
def _metadata(self):
splits = ["train", "validate", "test"]
expected_sizes = [6000, 2000, 2000]
metadata_index = {}
for split, es in zip(splits, expected_sizes):
annotation_folder = os.path.join(self.data_home, "annotations", split)
txtfiles = sorted(glob.glob(os.path.join(annotation_folder, "*.txt")))
for tf in txtfiles:
clip_id = os.path.basename(tf).replace(".txt", "")
metadata_index[clip_id] = {"split": split}
return metadata_index