-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_generation.py
68 lines (56 loc) · 2.72 KB
/
example_generation.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
import numpy as np
import spatialscaper as ss
import os
# Constants
NSCAPES = 20 # Number of soundscapes to generate
FOREGROUND_DIR = "datasets/sound_event_datasets/FSD50K_FMA" # Directory with FSD50K foreground sound files
RIR_DIR = (
"datasets/rir_datasets" # Directory containing Room Impulse Response (RIR) files
)
ROOM = "metu" # Initial room setting, change according to available rooms listed below
FORMAT = "foa" # Output format specifier: could be 'mic' or 'foa'
N_EVENTS_MEAN = 15 # Mean number of foreground events in a soundscape
N_EVENTS_STD = 6 # Standard deviation of the number of foreground events
DURATION = 60.0 # Duration in seconds of each soundscape
SR = 24000 # SpatialScaper default sampling rate for the audio files
OUTPUT_DIR = "output" # Directory to store the generated soundscapes
REF_DB = (
-65
) # Reference decibel level for the background ambient noise. Try making this random too!
# List of possible rooms to use for soundscape generation. Change 'ROOM' variable to one of these:
# "metu", "arni","bomb_shelter", "gym", "pb132", "pc226", "sa203", "sc203", "se203", "tb103", "tc352"
# Each room has a different Room Impulse Response (RIR) file associated with it, affecting the acoustic properties.
# FSD50K sound classes that will be spatialized include:
# 'femaleSpeech', 'maleSpeech', 'clapping', 'telephone', 'laughter',
# 'domesticSounds', 'footsteps', 'doorCupboard', 'music',
# 'musicInstrument', 'waterTap', 'bell', 'knock'.
# These classes are sourced from the FSD50K dataset, and
# are consistent with the DCASE SELD challenge classes.
# Function to generate a soundscape
def generate_soundscape(index):
track_name = f"fold1_room1_mix{index+1:03d}"
# Initialize Scaper. 'max_event_overlap' controls the maximum number of overlapping sound events.
ssc = ss.Scaper(
DURATION,
FOREGROUND_DIR,
RIR_DIR,
FORMAT,
ROOM,
max_event_overlap=2,
speed_limit=2.0, # in meters per second
)
ssc.ref_db = REF_DB
# static ambient noise
ssc.add_background()
# Add a random number of foreground events, based on the specified mean and standard deviation.
n_events = int(np.random.normal(N_EVENTS_MEAN, N_EVENTS_STD))
n_events = n_events if n_events > 0 else 1 # n_events should be greater than zero
for _ in range(n_events):
ssc.add_event() # randomly choosing and spatializing an FSD50K sound event
audiofile = os.path.join(OUTPUT_DIR, FORMAT, track_name)
labelfile = os.path.join(OUTPUT_DIR, "labels", track_name)
ssc.generate(audiofile, labelfile)
# Main loop for generating soundscapes
for iscape in range(NSCAPES):
print(f"Generating soundscape: {iscape + 1}/{NSCAPES}")
generate_soundscape(iscape)