Skip to content

Commit

Permalink
Fixes issue #382: Room.add_microphone_array discards MicrophoneArray …
Browse files Browse the repository at this point in the history
…directivity.
  • Loading branch information
fakufaku committed Dec 7, 2024
1 parent 1f1d644 commit de3e168
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
15 changes: 13 additions & 2 deletions pyroomacoustics/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ def add(self, obj):
).format(self.dim, obj.dim)
)

if "mic_array" not in self.__dict__ or self.mic_array is None:
if not hasattr(self, "mic_array") or self.mic_array is None:
self.mic_array = obj
else:
self.mic_array.append(obj)
Expand Down Expand Up @@ -2046,6 +2046,12 @@ def add_microphone_array(self, mic_array, directivity=None):
As an alternative, a
:py:obj:`~pyroomacoustics.beamforming.MicrophoneArray` can be
provided.
directivity: list of Directivity objects, optional
If ``mic_array`` is provided as a numpy array, an optional
:py:obj:`~pyroomacoustics.directivities.Directivity` object or
list thereof can be provided.
If ``mic_array`` is a MicrophoneArray object, passing an argument here
will result in an error.
Returns
-------
Expand All @@ -2064,7 +2070,12 @@ def add_microphone_array(self, mic_array, directivity=None):
mic_array = MicrophoneArray(mic_array, self.fs, directivity)
else:
# if the type is microphone array
mic_array.set_directivity(directivity)
if directivity is not None:
raise ValueError(
"When providing a MicrophoneArray object, the directivities should "
"be provided in the object, not via the `directivity` parameter "
"of this method."
)

if self.simulator_state["rt_needed"] and mic_array.is_directive:
raise NotImplementedError("Directivity not supported with ray tracing.")
Expand Down
43 changes: 39 additions & 4 deletions pyroomacoustics/tests/test_room_add.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

import pyroomacoustics as pra

Expand All @@ -8,6 +9,18 @@
source_loc1 = [3.5, 7.7, 2.1]
mic0 = [7, 8, 3.9]
mic1 = [7.87, 3.6, 6.1]
mic_dir0 = pra.FigureEight(
orientation=pra.DirectionVector(azimuth=90, colatitude=15, degrees=True)
)
mic_dir1 = pra.FigureEight(
orientation=pra.DirectionVector(azimuth=180, colatitude=15, degrees=True)
)
src_dir0 = pra.FigureEight(
orientation=pra.DirectionVector(azimuth=270, colatitude=15, degrees=True)
)
src_dir1 = pra.FigureEight(
orientation=pra.DirectionVector(azimuth=0, colatitude=15, degrees=True)
)


def test_add_source_mic():
Expand Down Expand Up @@ -56,12 +69,21 @@ def test_add_source_mic_obj():
assert room.mic_array.R.shape == (3, 2)


def test_add_source_mic_obj_2():
@pytest.mark.parametrize("with_dir", ((True,), (False,)))
def test_add_source_mic_obj_2(with_dir):
room = pra.ShoeBox(room_size)

source0 = pra.SoundSource(source_loc0, signal=sig)
source1 = pra.SoundSource(source_loc1, signal=sig)
mic_array = pra.MicrophoneArray(np.c_[mic0, mic1], fs=room.fs)
if with_dir:
sdir0 = src_dir0
sdir1 = src_dir1
mdir = [mic_dir0, mic_dir1]
else:
sdir0 = sdir1 = None
mdir = [None, None]

source0 = pra.SoundSource(source_loc0, signal=sig, directivity=sdir0)
source1 = pra.SoundSource(source_loc1, signal=sig, directivity=sdir1)
mic_array = pra.MicrophoneArray(np.c_[mic0, mic1], fs=room.fs, directivity=mdir)

room.add(source0).add(source1).add(mic_array)

Expand All @@ -72,6 +94,19 @@ def test_add_source_mic_obj_2():
assert np.allclose(room.mic_array.R[:, 0], mic0)
assert np.allclose(room.mic_array.R[:, 1], mic1)
assert room.mic_array.R.shape == (3, 2)
# Test directivities.
assert room.sources[0].directivity is sdir0
assert room.sources[1].directivity is sdir1
assert all(d is md for d, md in zip(room.mic_array.directivity, mdir))


def test_add_source_mic_obj_with_dir_error():
room = pra.ShoeBox(room_size)

mic_array = pra.MicrophoneArray(np.c_[mic0, mic1], fs=room.fs)

with pytest.raises(ValueError):
room.add_microphone_array(mic_array, directivity=[mic_dir0, mic_dir1])


def test_add_source_mic_ndarray():
Expand Down

0 comments on commit de3e168

Please sign in to comment.