Skip to content

compas_acoustics.simulation_methods

Interchangeable ways to compute a scene's impulse responses (contract: base).

Binaural and Ambisonics need pyFDN (Python >= 3.11), so they are imported on first use.

Classes

SimulationMethod

Bases: ABC

A way to compute impulse responses. See the module docstring for the output contract.

Methods:

compute abstractmethod
compute(context: SimulationContext) -> dict

One response per source-receiver pair, keyed '<receiver guid>_<source guid>'.

SimulationContext dataclass

SimulationContext(scene: object, geometry: object, sources: list, receivers: list, sampling_rate: int, outside: list = list())

What every method sees, built once per Scene.simulate.

Methods:

from_scene classmethod
from_scene(scene) -> 'SimulationContext'

The scene's room at its sampling rate, and the sources and receivers inside it (the others warn).

Mono

Mono(image_source_order: int = 3, ray_tracing: bool = True, air_absorption: bool = True, n_rays: Optional[int] = None, energy_thres: float = 1e-07, duration: Optional[float] = None, receiver_radius: float = 0.5, seed: Union[bool, int] = True, normalize: bool = True, air: Optional[AirConditions] = None)

Bases: SimulationMethod

Image sources up to image_source_order (pyroomacoustics: Scheibler et al. 2018, Sec. 3) and, if ray_tracing, pyroomacoustics' stochastic ray tracer.

Parameters:

Name Type Description Default
image_source_order int

Last image source order. With ray_tracing the rays also hold this order, the later ones and the scattered energy (compas_acoustics.room.raytracing). The search cost grows steeply with the order (image_source_search_seconds).

3
ray_tracing bool

Materials without scattering coefficients scatter 0.05 in every band. Without it, the settings n_rays to seed raise ValueError when changed.

True
air_absorption bool
True
n_rays int

Per source; default default_rays, from the room's volume (at least 20000).

None
energy_thres float

The product of its reflections in its least absorbed band at which a ray stops; the logged energy is that over the squared distance (Schroeder 2011, Eq. 5.54), so the tail has decayed much further.

1e-7
duration float

Of the ray-traced responses, in seconds. Default: default_duration, traced again longer while the responses end before 2/3 of their T30 (up to 12 s). A given duration is kept and warns when it cuts the decay. Without ray tracing the responses have their natural length.

None
receiver_radius float

In metres.

0.5
seed bool or int

True seeds with 42, an int with that value, False or None leaves the run unseeded (generation["seed"] = None).

True
normalize bool

Whether the responses are peak-normalized views.

True
air AirConditions

Its speed of sound and, with air_absorption, its ISO 9613-1 absorption; default AirConditions().

None

Examples:

>>> import compas_acoustics as ca
>>> scene = ca.Scene("office")
>>> wall = ca.AcousticMaterial("wall", 0.2, 0.1)
>>> scene.add_acoustic_objects(ca.AcousticObjectsLibrary.shoebox_room(6, 5, 3, materials=wall))
>>> scene.add_source(ca.Source("talker", 1.5, 1.5, 1.6))
>>> scene.add_receiver(ca.Receiver("listener", 4.0, 3.0, 1.2))
>>> responses = scene.simulate(ca.Mono(image_source_order=2, ray_tracing=False))
>>> ir = next(iter(responses.values()))
>>> ir.sampling_rate, ir.n_channels, ir.generation["method"]
(48000, 1, 'mono')

Ray tracing, on by default, adds the late reflections; the seed makes a run repeatable.

>>> responses = scene.simulate(ca.Mono(n_rays=2000, duration=0.5))
>>> ir = next(iter(responses.values()))
>>> ir.ir.shape, ir.generation["seed"]
((24000,), 42)

Binaural

Binaural(reference=None, hrtf=None, *, tail=None, image_source_order=3, n_slopes=1, decay_db=30.0, duration=None, n_rays=None, directional_absorption=True, late_envelope=None, diffraction=False, air_absorption=None, seed=True, normalize=True, air=None, scattering=False)

Bases: _HybridMethod

Binaural room impulse responses: image sources, optional edge diffraction, a directional FDN.

The early field is the image sources up to image_source_order and, with diffraction, the diffracted paths. The images of the last order drive a feedback delay network of 96 delay lines, one per direction (the count Kirsch et al. 2023, Secs. 1.2 and 3.3, use offline), whose decay follows a target (Wendt et al. 2014, Sec. 1.2). Every early path and delay line is filtered with the HRIR measured nearest its direction. Needs Python >= 3.11 and a closed room. Responses are ears left then right, stored under 'binaural'; generation["hrtf"] names the set.

Parameters:

Name Type Description Default
reference (str, dict, Mono or None)

A package-scale mono response per pair for the decay target and the late envelope: a method's name on the IR graph ("mono", "data"), a dict as Scene.simulate returns, or a Mono to run first.

None
hrtf str or path

"ku100" (None), the Neumann KU 100 set of the HRTF library, or a SOFA file.

None

Examples:

>>> import compas_acoustics as ca
>>> scene = ca.Scene("office")
>>> wall = ca.AcousticMaterial("wall", 0.2, 0.1)
>>> scene.add_acoustic_objects(ca.AcousticObjectsLibrary.shoebox_room(6, 5, 3, materials=wall))
>>> scene.add_source(ca.Source("talker", 1.5, 1.5, 1.6))
>>> scene.add_receiver(ca.Receiver("listener", 4.0, 3.0, 1.2))
>>> ir = next(iter(scene.simulate(ca.Binaural(tail="eyring")).values()))
>>> ir.n_channels, ir.generation["hrtf"]
(2, 'KU100_HRIR_L2702.sofa')

With a ray-traced reference the decay is fitted to a response you already have.

>>> reference = scene.simulate(ca.Mono(ray_tracing=True))
>>> responses = scene.simulate(ca.Binaural(reference="mono"))

Ambisonics

Ambisonics(reference=None, sh_order=1, normalization='SN3D', *, tail=None, image_source_order=3, n_slopes=1, decay_db=30.0, duration=None, n_rays=None, directional_absorption=True, late_envelope=None, diffraction=False, air_absorption=None, seed=True, normalize=True, air=None, scattering=False)

Bases: _HybridMethod

Ambisonic room impulse responses: image sources, optional edge diffraction, a directional FDN.

As Binaural, with every early path encoded at its direction and every delay line projected from its direction at sh_order. Responses are ACN in normalization, stored under 'ambisonics'.

Parameters:

Name Type Description Default
reference (str, dict, Mono or None)

As Binaural.

None
sh_order int

The network has 96 delay lines, the count Kirsch et al. (2023, Secs. 1.2 and 3.3) use offline, up to third order, which the 96-point grid supports; above that the smallest equal-area grid supporting the order. The input gain sqrt(num_delays) offsets the output's 1 / sqrt(num_delays), so the late level does not depend on the count.

1
normalization ('SN3D', 'N3D')

ACN channel order, W = 1 for a unit plane wave.

"SN3D"

Examples:

>>> import compas_acoustics as ca
>>> scene = ca.Scene("office")
>>> wall = ca.AcousticMaterial("wall", 0.2, 0.1)
>>> scene.add_acoustic_objects(ca.AcousticObjectsLibrary.shoebox_room(6, 5, 3, materials=wall))
>>> scene.add_source(ca.Source("talker", 1.5, 1.5, 1.6))
>>> scene.add_receiver(ca.Receiver("listener", 4.0, 3.0, 1.2))
>>> ir = next(iter(scene.simulate(ca.Ambisonics(tail="eyring", sh_order=2)).values()))
>>> ir.n_channels, ir.ambisonic_order
(9, 2)

Attributes

num_delays property
num_delays: int

96 up to third order, more above (default_num_delays).

Functions:

image_source_search_seconds

image_source_search_seconds(n_faces: int, order: int) -> float

The package's order-of-magnitude estimate of the image source search time in seconds, for warnings.

It grows by a factor n_faces / 2.2 per order; its constants were fitted to timed searches, and the times depend on the machine.