From 9ad8423f4caa936ead0444353b0d8d22b9711959 Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Sat, 6 Apr 2024 18:43:20 -0400 Subject: [PATCH 1/7] encapsulate room --- spatialscaper/core.py | 151 +++++---------------------------------- spatialscaper/room.py | 159 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+), 133 deletions(-) create mode 100644 spatialscaper/room.py diff --git a/spatialscaper/core.py b/spatialscaper/core.py index 046288a..e415745 100644 --- a/spatialscaper/core.py +++ b/spatialscaper/core.py @@ -26,7 +26,7 @@ save_output, sort_matrix_by_columns, ) -from .sofa_utils import load_rir_pos, load_pos +from .room import get_room # Sound event classes for DCASE Challenge @@ -63,23 +63,6 @@ ], ) -# Paths for room SOFA files -__SPATIAL_SCAPER_RIRS_DIR__ = "spatialscaper_RIRs" -__PATH_TO_AMBIENT_NOISE_FILES__ = os.path.join("source_data", "TAU-SNoise_DB") -__ROOM_RIR_FILE__ = { - "metu": "metu_sparg_em32.sofa", - "arni": "arni_mic.sofa", - "bomb_shelter": "bomb_shelter_mic.sofa", - "gym": "gym_mic.sofa", - "pb132": "pb132_mic.sofa", - "pc226": "pc226_mic.sofa", - "sa203": "sa203_mic.sofa", - "sc203": "sc203_mic.sofa", - "se203": "se203_mic.sofa", - "tb103": "tb103_mic.sofa", - "tc352": "tc352_mic.sofa", -} - class Scaper: def __init__( @@ -89,7 +72,6 @@ def __init__( rir_dir="", fmt="mic", room="metu", - use_room_ambient_noise=True, background_dir=None, sr=24000, DCASE_format=True, @@ -137,18 +119,20 @@ def __init__( """ self.duration = duration + self.sr = sr + + self.room = get_room(rir_dir, room, fmt=fmt) self.foreground_dir = foreground_dir self.background_dir = background_dir - self.rir_dir = rir_dir - self.room = room - self.use_room_ambient_noise = use_room_ambient_noise - self.format = fmt - self.sr = sr + self.DCASE_format = DCASE_format if self.DCASE_format: self.label_rate = __DCASE_LABEL_RATE__ + + self.max_sample_attempts = max_sample_attempts self.max_event_overlap = max_event_overlap self.max_event_dur = max_event_dur + self.speed_limit = speed_limit self.ref_db = ref_db self.fg_events = [] @@ -162,46 +146,15 @@ def __init__( else: self.fg_labels = {l: i for i, l in enumerate(fg_label_list)} - self.speed_limit = speed_limit - - self.max_sample_attempts = max_sample_attempts - - def get_path_to_room_ambient_noise(self): - path_to_ambient_noise_files = os.path.join( - self.rir_dir, __PATH_TO_AMBIENT_NOISE_FILES__ - ) - all_ambient_noise_files = glob.glob( - os.path.join(path_to_ambient_noise_files, "*", "*") - ) - if self.format == "mic": - ambient_noise_format_files = [ - f for f in all_ambient_noise_files if "tetra" in f - ] - elif self.format == "foa": - ambient_noise_format_files = [ - f for f in all_ambient_noise_files if "foa" in f - ] - if self.room == "bomb_shelter": - room_ambient_noise_file = [ - f for f in ambient_noise_format_files if "bomb_center" in f - ] - else: - room_ambient_noise_file = [ - f for f in ambient_noise_format_files if self.room in f - ] - assert len(room_ambient_noise_file) < 2 - if room_ambient_noise_file: - return room_ambient_noise_file[0] - else: - return random.choice(ambient_noise_format_files) - - def add_background(self): + def add_background(self, use_room_ambient_noise=True): """ Adds a background event to the soundscape. This method sets fixed values for event time, duration, and SNR, and adds the event to the background events list. """ label = None + source_file = None + source_time = None snr = ("const", 0) role = "background" pitch_shift = None @@ -210,18 +163,14 @@ def add_background(self): event_duration = ("const", self.duration) event_position = None - if self.use_room_ambient_noise: - source_file = self.get_path_to_room_ambient_noise() + if use_room_ambient_noise: + source_files = self.room.get_ambient_noise_paths() + source_file = random.choice(source_files) ambient_noise_duration = librosa.get_duration(path=source_file) if ambient_noise_duration > self.duration: source_time = round( random.uniform(0, ambient_noise_duration - self.duration) ) - else: - source_time = None - else: - source_file = None - source_time = None self.bg_events.append( Event( @@ -335,7 +284,7 @@ def add_event( self.speed_limit, ) else: - xyz_min, xyz_max = self._get_room_min_max() + xyz_min, xyz_max = self.room.get_boundaries() event_position_ = [self._gen_xyz(xyz_min, xyz_max)] if snr[0] == "uniform" and len(snr) == 3: @@ -425,18 +374,6 @@ def _gen_xyz(self, xyz_min, xyz_max): xyz.append(random.uniform(xyz_min[i], xyz_max[i])) return xyz - def _get_room_min_max(self): - """ - Determines the minimum and maximum XYZ coordinates for the current room setup. - - Returns: - tuple: A tuple containing the minimum and maximum XYZ coordinates for the room. - """ - all_xyz = self.get_room_irs_xyz() - xyz_min = all_xyz.min(axis=0) - xyz_max = all_xyz.max(axis=0) - return xyz_min, xyz_max - def generate_end_point( self, xyz_start, xyz_min, xyz_max, speed_limit, event_duration ): @@ -516,7 +453,7 @@ def define_trajectory( if all(trajectory_params[1:]): xyz_min, xyz_max = trajectory_params[1:] else: - xyz_min, xyz_max = self._get_room_min_max() + xyz_min, xyz_max = self.room.get_boundaries() xyz_start = self._gen_xyz(xyz_min, xyz_max) xyz_end = self.generate_end_point( xyz_start, xyz_min, xyz_max, speed_limit, event_duration @@ -537,60 +474,9 @@ def define_position(self, position_params): if position_params: xyz_min, xyz_max = position_params else: - xyz_min, xyz_max = self._get_room_min_max() + xyz_min, xyz_max = self.room.get_boundaries() return [self._gen_xyz(xyz_min, xyz_max)] - def get_room_irs_xyz(self): - """ - Retrieves the XYZ coordinates of impulse response positions in the room. - - Returns: - numpy.ndarray: An array of XYZ coordinates for the impulse response positions. - """ - room_sofa_path = os.path.join( - self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room] - ) - return load_pos(room_sofa_path, doas=False) - - def get_room_irs_wav_xyz(self, wav=True, pos=True): - """ - Retrieves impulse responses and their positions for the room. - - Args: - wav (bool): Whether to include the waveforms of the impulse responses. - pos (bool): Whether to include the positions of the impulse responses. - - Returns: - tuple: A tuple containing the impulse responses, their sampling rate, and their XYZ positions. - """ - room_sofa_path = os.path.join( - self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room] - ) - all_irs, ir_sr, all_ir_xyzs = load_rir_pos(room_sofa_path, doas=False) - ir_sr = ir_sr.data[0] - all_irs = all_irs.data - all_ir_xyzs = all_ir_xyzs.data - if ir_sr != self.sr: - all_irs = librosa.resample(all_irs, orig_sr=ir_sr, target_sr=self.sr) - ir_sr = self.sr - return all_irs, ir_sr, all_ir_xyzs - - def get_format_irs(self, all_irs, fmt="mic"): - """ - Retrieves impulse responses according to the specified format. - - Args: - all_irs (numpy.ndarray): Array of all impulse responses. - fmt (str): The format for retrieving impulse responses (e.g., 'mic'). - - Returns: - numpy.ndarray: An array of impulse responses formatted according to the specified format. - """ - if fmt == "mic" and self.room == "metu": - return all_irs[:, [5, 9, 25, 21], :] - else: - return all_irs - def generate_noise(self, event): """ Generates noise to be used as background ambient. @@ -756,8 +642,7 @@ def generate(self, audiopath, labelpath): and that the output audio and labels are accurately saved for further use or analysis. """ - all_irs, ir_sr, all_ir_xyzs = self.get_room_irs_wav_xyz() - all_irs = self.get_format_irs(all_irs) + all_irs, ir_sr, all_ir_xyzs = self.room.get_irs() self.nchans = all_irs.shape[1] # a bit ugly but works for now # initialize output audio array diff --git a/spatialscaper/room.py b/spatialscaper/room.py new file mode 100644 index 0000000..7c63081 --- /dev/null +++ b/spatialscaper/room.py @@ -0,0 +1,159 @@ +import os +import math +import random +import glob +from collections import namedtuple + +import librosa +import scipy +import numpy as np +import warnings + +# Local application/library specific imports +from .utils import ( + get_label_list, + get_files_list, + new_event_exceeds_max_overlap, + count_leading_zeros_in_period, + generate_trajectory, + db2multiplier, + traj_2_ir_idx, + find_indices_of_change, + IR_normalizer, + spatialize, + get_timegrid, + get_labels, + save_output, + sort_matrix_by_columns, +) +from .sofa_utils import load_rir_pos, load_pos + + +# Paths for room SOFA files +__SPATIAL_SCAPER_RIRS_DIR__ = "spatialscaper_RIRs" +__PATH_TO_AMBIENT_NOISE_FILES__ = os.path.join("source_data", "TAU-SNoise_DB") +__ROOM_RIR_FILE__ = { + "metu": "metu_sparg_em32.sofa", + "arni": "arni_mic.sofa", + "bomb_shelter": "bomb_shelter_mic.sofa", + "gym": "gym_mic.sofa", + "pb132": "pb132_mic.sofa", + "pc226": "pc226_mic.sofa", + "sa203": "sa203_mic.sofa", + "sc203": "sc203_mic.sofa", + "se203": "se203_mic.sofa", + "tb103": "tb103_mic.sofa", + "tc352": "tc352_mic.sofa", +} + + +class BaseRoom: + def __init__(self) -> None: + pass + + def get_room_ambient_noise(self): + raise NotImplementedError + + def get_positions(self): + """ + Retrieves the XYZ coordinates of impulse response positions in the room. + + Returns: + numpy.ndarray: An array of XYZ coordinates for the impulse response positions. + """ + raise NotImplementedError + + def get_irs(self, format=True): + """ + Retrieves impulse responses and their positions for the room. + + Args: + wav (bool): Whether to include the waveforms of the impulse responses. + pos (bool): Whether to include the positions of the impulse responses. + + Returns: + tuple: A tuple containing the impulse responses, their sampling rate, and their XYZ positions. + """ + raise NotImplementedError + + def get_boundaries(self): + """ + Determines the minimum and maximum XYZ coordinates for the current room setup. + + Returns: + tuple: A tuple containing the minimum and maximum XYZ coordinates for the room. + """ + all_xyz = self.get_positions() + xyz_min = all_xyz.min(axis=0) + xyz_max = all_xyz.max(axis=0) + return xyz_min, xyz_max + + +class SOFARoom(BaseRoom): + def __init__(self, rir_dir, room, fmt): + self.rir_dir = rir_dir + self.room = room + self.format = fmt + + @property + def sofa_path(self): + return os.path.join( + self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room] + ) + + def get_ambient_noise_paths(self): + path_to_ambient_noise_files = os.path.join( + self.rir_dir, __PATH_TO_AMBIENT_NOISE_FILES__ + ) + ambient_noise_format_files = glob.glob( + os.path.join(path_to_ambient_noise_files, "*", "*") + ) + if self.format == "mic": + ambient_noise_format_files = [ + f for f in ambient_noise_format_files if "tetra" in f + ] + elif self.format == "foa": + ambient_noise_format_files = [ + f for f in ambient_noise_format_files if "foa" in f + ] + if self.room == "bomb_shelter": + room_ambient_noise_file = [ + f for f in ambient_noise_format_files if "bomb_center" in f + ] + else: + room_ambient_noise_file = [ + f for f in ambient_noise_format_files if self.room in f + ] + assert len(room_ambient_noise_file) < 2 + if room_ambient_noise_file: + return room_ambient_noise_file + return ambient_noise_format_files + + def get_positions(self): + return load_pos(self.sofa_path, doas=False) + + def get_irs(self, format=True): + all_irs, ir_sr, all_ir_xyzs = load_rir_pos(self.sofa_path, doas=False) + ir_sr = ir_sr.data[0] + all_irs = all_irs.data + all_ir_xyzs = all_ir_xyzs.data + if ir_sr != self.sr: + all_irs = librosa.resample(all_irs, orig_sr=ir_sr, target_sr=self.sr) + ir_sr = self.sr + if format: + self._format_irs(all_irs) + return all_irs, ir_sr, all_ir_xyzs + + def _format_irs(self, all_irs, fmt="mic"): + if fmt == "mic" and self.room == "metu": + return all_irs[:, [5, 9, 25, 21], :] + return all_irs + + + + + +def get_room(rir_dir, *a, **kw): + if isinstance(rir_dir, BaseRoom): + return rir_dir + return SOFARoom(rir_dir, *a, **kw) \ No newline at end of file From 6aeecd64ba1bdf73d6ad08433e976e4d16a3a77f Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Mon, 8 Apr 2024 16:41:34 -0400 Subject: [PATCH 2/7] fix sample rate, docs, unused imports --- spatialscaper/core.py | 2 +- spatialscaper/room.py | 47 +++++++++++++++++-------------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/spatialscaper/core.py b/spatialscaper/core.py index e415745..2c0816d 100644 --- a/spatialscaper/core.py +++ b/spatialscaper/core.py @@ -642,7 +642,7 @@ def generate(self, audiopath, labelpath): and that the output audio and labels are accurately saved for further use or analysis. """ - all_irs, ir_sr, all_ir_xyzs = self.room.get_irs() + all_irs, ir_sr, all_ir_xyzs = self.room.get_irs(self.sr) self.nchans = all_irs.shape[1] # a bit ugly but works for now # initialize output audio array diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 7c63081..645310f 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -1,31 +1,8 @@ import os -import math -import random import glob -from collections import namedtuple import librosa -import scipy -import numpy as np -import warnings - # Local application/library specific imports -from .utils import ( - get_label_list, - get_files_list, - new_event_exceeds_max_overlap, - count_leading_zeros_in_period, - generate_trajectory, - db2multiplier, - traj_2_ir_idx, - find_indices_of_change, - IR_normalizer, - spatialize, - get_timegrid, - get_labels, - save_output, - sort_matrix_by_columns, -) from .sofa_utils import load_rir_pos, load_pos @@ -48,10 +25,23 @@ class BaseRoom: + """ + Initialize a Room object. + + A Room encapsulates the spatial and acoustic characteristics available of a physical room. + This includes a collection of impulse response measurements taken at different positions in + the room. + """ def __init__(self) -> None: pass - def get_room_ambient_noise(self): + def get_ambient_noise_paths(self): + """ + Retrieves paths to ambient noise audio files specific to this room. + + Returns: + list[str]: A list of audio paths. + """ raise NotImplementedError def get_positions(self): @@ -97,6 +87,7 @@ def __init__(self, rir_dir, room, fmt): @property def sofa_path(self): + '''Path to the SOFA file for this room.''' return os.path.join( self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room] ) @@ -132,14 +123,14 @@ def get_ambient_noise_paths(self): def get_positions(self): return load_pos(self.sofa_path, doas=False) - def get_irs(self, format=True): + def get_irs(self, sr=None, format=True): all_irs, ir_sr, all_ir_xyzs = load_rir_pos(self.sofa_path, doas=False) ir_sr = ir_sr.data[0] all_irs = all_irs.data all_ir_xyzs = all_ir_xyzs.data - if ir_sr != self.sr: - all_irs = librosa.resample(all_irs, orig_sr=ir_sr, target_sr=self.sr) - ir_sr = self.sr + if sr is not None and ir_sr != sr: + all_irs = librosa.resample(all_irs, orig_sr=ir_sr, target_sr=sr) + ir_sr = sr if format: self._format_irs(all_irs) return all_irs, ir_sr, all_ir_xyzs From 140935a57897ea070c27a3bffcad61ec4dc9c1ae Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Mon, 8 Apr 2024 16:45:05 -0400 Subject: [PATCH 3/7] merge foa changes --- spatialscaper/room.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 645310f..57e985a 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -12,15 +12,15 @@ __ROOM_RIR_FILE__ = { "metu": "metu_sparg_em32.sofa", "arni": "arni_mic.sofa", - "bomb_shelter": "bomb_shelter_mic.sofa", - "gym": "gym_mic.sofa", - "pb132": "pb132_mic.sofa", - "pc226": "pc226_mic.sofa", - "sa203": "sa203_mic.sofa", - "sc203": "sc203_mic.sofa", - "se203": "se203_mic.sofa", - "tb103": "tb103_mic.sofa", - "tc352": "tc352_mic.sofa", + "bomb_shelter": "bomb_shelter_{fmt}.sofa", + "gym": "gym_{fmt}.sofa", + "pb132": "pb132_{fmt}.sofa", + "pc226": "pc226_{fmt}.sofa", + "sa203": "sa203_{fmt}.sofa", + "sc203": "sc203_{fmt}.sofa", + "se203": "se203_{fmt}.sofa", + "tb103": "tb103_{fmt}.sofa", + "tc352": "tc352_{fmt}.sofa", } @@ -88,8 +88,10 @@ def __init__(self, rir_dir, room, fmt): @property def sofa_path(self): '''Path to the SOFA file for this room.''' + if self.format == 'foa' and self.room in ['metu','arni']: + raise ValueError('"metu" and "arni" rooms are currently only supported in mic (tetrahedral) format. please check again soon.') return os.path.join( - self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room] + self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room].format(fmt=self.format) ) def get_ambient_noise_paths(self): From b5d6207307a9656e9968c1f3e75fd821386ba58f Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Mon, 8 Apr 2024 16:49:36 -0400 Subject: [PATCH 4/7] black --- spatialscaper/room.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 57e985a..41aee25 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -2,6 +2,7 @@ import glob import librosa + # Local application/library specific imports from .sofa_utils import load_rir_pos, load_pos @@ -26,12 +27,13 @@ class BaseRoom: """ - Initialize a Room object. + Initialize a Room object. - A Room encapsulates the spatial and acoustic characteristics available of a physical room. - This includes a collection of impulse response measurements taken at different positions in - the room. + A Room encapsulates the spatial and acoustic characteristics available of a physical room. + This includes a collection of impulse response measurements taken at different positions in + the room. """ + def __init__(self) -> None: pass @@ -87,11 +89,13 @@ def __init__(self, rir_dir, room, fmt): @property def sofa_path(self): - '''Path to the SOFA file for this room.''' - if self.format == 'foa' and self.room in ['metu','arni']: + """Path to the SOFA file for this room.""" + if self.format == "foa" and self.room in ["metu", "arni"]: raise ValueError('"metu" and "arni" rooms are currently only supported in mic (tetrahedral) format. please check again soon.') return os.path.join( - self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, __ROOM_RIR_FILE__[self.room].format(fmt=self.format) + self.rir_dir, + __SPATIAL_SCAPER_RIRS_DIR__, + __ROOM_RIR_FILE__[self.room].format(fmt=self.format), ) def get_ambient_noise_paths(self): @@ -121,10 +125,10 @@ def get_ambient_noise_paths(self): if room_ambient_noise_file: return room_ambient_noise_file return ambient_noise_format_files - + def get_positions(self): return load_pos(self.sofa_path, doas=False) - + def get_irs(self, sr=None, format=True): all_irs, ir_sr, all_ir_xyzs = load_rir_pos(self.sofa_path, doas=False) ir_sr = ir_sr.data[0] @@ -136,17 +140,14 @@ def get_irs(self, sr=None, format=True): if format: self._format_irs(all_irs) return all_irs, ir_sr, all_ir_xyzs - + def _format_irs(self, all_irs, fmt="mic"): if fmt == "mic" and self.room == "metu": return all_irs[:, [5, 9, 25, 21], :] return all_irs - - - def get_room(rir_dir, *a, **kw): if isinstance(rir_dir, BaseRoom): return rir_dir - return SOFARoom(rir_dir, *a, **kw) \ No newline at end of file + return SOFARoom(rir_dir, *a, **kw) From c335f09e92c8c22824e845a3436f8a7eef0249c1 Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Mon, 8 Apr 2024 17:17:32 -0400 Subject: [PATCH 5/7] fix possible bug if room/format in rir_path --- spatialscaper/room.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 41aee25..0de1eb6 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -91,7 +91,9 @@ def __init__(self, rir_dir, room, fmt): def sofa_path(self): """Path to the SOFA file for this room.""" if self.format == "foa" and self.room in ["metu", "arni"]: - raise ValueError('"metu" and "arni" rooms are currently only supported in mic (tetrahedral) format. please check again soon.') + raise ValueError( + '"metu" and "arni" rooms are currently only supported in mic (tetrahedral) format. please check again soon.' + ) return os.path.join( self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, @@ -105,22 +107,20 @@ def get_ambient_noise_paths(self): ambient_noise_format_files = glob.glob( os.path.join(path_to_ambient_noise_files, "*", "*") ) + + fmt = self.format if self.format == "mic": - ambient_noise_format_files = [ - f for f in ambient_noise_format_files if "tetra" in f - ] - elif self.format == "foa": - ambient_noise_format_files = [ - f for f in ambient_noise_format_files if "foa" in f - ] + fmt = "tetra" + room = self.room if self.room == "bomb_shelter": - room_ambient_noise_file = [ - f for f in ambient_noise_format_files if "bomb_center" in f - ] - else: - room_ambient_noise_file = [ - f for f in ambient_noise_format_files if self.room in f - ] + room = "bomb_center" + ambient_noise_format_files = [ + f for f in ambient_noise_format_files if fmt in os.path.basename(f) + ] + room_ambient_noise_file = [ + f for f in ambient_noise_format_files if room in os.path.basename(f) + ] + assert len(room_ambient_noise_file) < 2 if room_ambient_noise_file: return room_ambient_noise_file From 23ff3abc70998c2fe0338093b9ec0cc39b031649 Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Sun, 14 Apr 2024 15:54:59 -0400 Subject: [PATCH 6/7] fix merge conflict --- spatialscaper/room.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 0de1eb6..8f4ecc2 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -12,7 +12,7 @@ __PATH_TO_AMBIENT_NOISE_FILES__ = os.path.join("source_data", "TAU-SNoise_DB") __ROOM_RIR_FILE__ = { "metu": "metu_sparg_em32.sofa", - "arni": "arni_mic.sofa", + "arni": "arni_{fmt}.sofa", "bomb_shelter": "bomb_shelter_{fmt}.sofa", "gym": "gym_{fmt}.sofa", "pb132": "pb132_{fmt}.sofa", @@ -90,9 +90,9 @@ def __init__(self, rir_dir, room, fmt): @property def sofa_path(self): """Path to the SOFA file for this room.""" - if self.format == "foa" and self.room in ["metu", "arni"]: + if self.format == "foa" and self.room == "metu": raise ValueError( - '"metu" and "arni" rooms are currently only supported in mic (tetrahedral) format. please check again soon.' + '"metu" room is currently only supported in mic (tetrahedral) format. please check again soon.' ) return os.path.join( self.rir_dir, @@ -101,6 +101,7 @@ def sofa_path(self): ) def get_ambient_noise_paths(self): + # list files from disk path_to_ambient_noise_files = os.path.join( self.rir_dir, __PATH_TO_AMBIENT_NOISE_FILES__ ) @@ -108,23 +109,24 @@ def get_ambient_noise_paths(self): os.path.join(path_to_ambient_noise_files, "*", "*") ) + # translate format fmt = self.format if self.format == "mic": fmt = "tetra" room = self.room if self.room == "bomb_shelter": room = "bomb_center" + + # filter files ambient_noise_format_files = [ f for f in ambient_noise_format_files if fmt in os.path.basename(f) ] - room_ambient_noise_file = [ + room_ambient_noise_files = [ f for f in ambient_noise_format_files if room in os.path.basename(f) ] - assert len(room_ambient_noise_file) < 2 - if room_ambient_noise_file: - return room_ambient_noise_file - return ambient_noise_format_files + # assert len(room_ambient_noise_files) < 2 + return room_ambient_noise_files or ambient_noise_format_files def get_positions(self): return load_pos(self.sofa_path, doas=False) From 72471f5dcf89339c8d4f0dcdd76ec1c0639aa414 Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Fri, 19 Apr 2024 22:07:22 -0400 Subject: [PATCH 7/7] merge main --- spatialscaper/room.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/spatialscaper/room.py b/spatialscaper/room.py index 8f4ecc2..857893e 100644 --- a/spatialscaper/room.py +++ b/spatialscaper/room.py @@ -11,7 +11,7 @@ __SPATIAL_SCAPER_RIRS_DIR__ = "spatialscaper_RIRs" __PATH_TO_AMBIENT_NOISE_FILES__ = os.path.join("source_data", "TAU-SNoise_DB") __ROOM_RIR_FILE__ = { - "metu": "metu_sparg_em32.sofa", + "metu": "metu_sparg_{fmt}.sofa", "arni": "arni_{fmt}.sofa", "bomb_shelter": "bomb_shelter_{fmt}.sofa", "gym": "gym_{fmt}.sofa", @@ -90,10 +90,6 @@ def __init__(self, rir_dir, room, fmt): @property def sofa_path(self): """Path to the SOFA file for this room.""" - if self.format == "foa" and self.room == "metu": - raise ValueError( - '"metu" room is currently only supported in mic (tetrahedral) format. please check again soon.' - ) return os.path.join( self.rir_dir, __SPATIAL_SCAPER_RIRS_DIR__, @@ -139,15 +135,8 @@ def get_irs(self, sr=None, format=True): if sr is not None and ir_sr != sr: all_irs = librosa.resample(all_irs, orig_sr=ir_sr, target_sr=sr) ir_sr = sr - if format: - self._format_irs(all_irs) return all_irs, ir_sr, all_ir_xyzs - def _format_irs(self, all_irs, fmt="mic"): - if fmt == "mic" and self.room == "metu": - return all_irs[:, [5, 9, 25, 21], :] - return all_irs - def get_room(rir_dir, *a, **kw): if isinstance(rir_dir, BaseRoom):