Skip to content

Commit 7874a01

Browse files
committed
refactors directivities sub-module and sofa sub-module into a single sub-package
1 parent 9fb7ba7 commit 7874a01

12 files changed

+919
-907
lines changed

examples/simulate_binaural_recording.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
CardioidFamily,
2323
DirectionVector,
2424
DirectivityPattern,
25+
MeasuredDirectivityFile,
2526
)
26-
from pyroomacoustics.open_sofa_interpolate import SOFADirectivityFactory
2727

2828
if __name__ == "__main__":
2929
parser = argparse.ArgumentParser(
@@ -71,7 +71,7 @@
7171
azimuth_deg = -45.0
7272
colatitude_deg = 90.0
7373

74-
hrtf = SOFADirectivityFactory(
74+
hrtf = MeasuredDirectivityFile(
7575
path=args.hrtf,
7676
fs=fs,
7777
interp_order=args.interp_order,

examples/simulation_with_measured_directivity.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ class DirectionVector
7474
CardioidFamily,
7575
DirectionVector,
7676
DirectivityPattern,
77+
MeasuredDirectivityFile,
7778
)
78-
from pyroomacoustics.open_sofa_interpolate import SOFADirectivityFactory
7979

80-
eigenmike = SOFADirectivityFactory("EM32_Directivity", fs=16000)
81-
akg = SOFADirectivityFactory("AKG_c480_c414_CUBE", fs=16000)
80+
eigenmike = MeasuredDirectivityFile("EM32_Directivity", fs=16000)
81+
akg = MeasuredDirectivityFile("AKG_c480_c414_CUBE", fs=16000)
8282

8383
vec_54_73 = DirectionVector(azimuth=54, colatitude=73, degrees=True)
8484

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .analytic import CardioidFamily, DirectivityPattern, cardioid_func
2+
from .base import Directivity
3+
from .direction import DirectionVector
4+
from .measured import MeasuredDirectivity, MeasuredDirectivityFile

pyroomacoustics/directivities.py pyroomacoustics/directivities/analytic.py

+4-103
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import abc
2-
import sys
31
from enum import Enum
4-
from pathlib import Path
52

63
import numpy as np
74

8-
from pyroomacoustics.doa import spher2cart
9-
from pyroomacoustics.utilities import all_combinations, requires_matplotlib
5+
from ..doa import spher2cart
6+
from ..utilities import all_combinations, requires_matplotlib
7+
from .base import Directivity
8+
from .direction import DirectionVector
109

1110

1211
class DirectivityPattern(Enum):
@@ -29,104 +28,6 @@ class DirectivityPattern(Enum):
2928
OMNI = 1.0
3029

3130

32-
class DirectionVector(object):
33-
"""
34-
Object for representing direction vectors in 3D, parameterized by an azimuth and colatitude
35-
angle.
36-
37-
Parameters
38-
----------
39-
azimuth : float
40-
colatitude : float, optional
41-
Default to PI / 2, only XY plane.
42-
degrees : bool
43-
Whether provided values are in degrees (True) or radians (False).
44-
"""
45-
46-
def __init__(self, azimuth, colatitude=None, degrees=True):
47-
if degrees is True:
48-
azimuth = np.radians(azimuth)
49-
if colatitude is not None:
50-
colatitude = np.radians(colatitude)
51-
self._azimuth = azimuth
52-
if colatitude is None:
53-
colatitude = np.pi / 2
54-
assert colatitude <= np.pi and colatitude >= 0
55-
56-
self._colatitude = colatitude
57-
58-
self._unit_v = np.array(
59-
[
60-
np.cos(self._azimuth) * np.sin(self._colatitude),
61-
np.sin(self._azimuth) * np.sin(self._colatitude),
62-
np.cos(self._colatitude),
63-
]
64-
)
65-
66-
def get_azimuth(self, degrees=False):
67-
if degrees:
68-
return np.degrees(self._azimuth)
69-
else:
70-
return self._azimuth
71-
72-
def get_colatitude(self, degrees=False):
73-
if degrees:
74-
return np.degrees(self._colatitude)
75-
else:
76-
return self._colatitude
77-
78-
@property
79-
def unit_vector(self):
80-
"""Direction vector in cartesian coordinates."""
81-
return self._unit_v
82-
83-
84-
class Directivity(abc.ABC):
85-
"""
86-
Abstract class for directivity patterns.
87-
88-
"""
89-
90-
def __init__(self, orientation):
91-
assert isinstance(orientation, DirectionVector)
92-
self._orientation = orientation
93-
94-
@property
95-
def is_impulse_response(self):
96-
"""
97-
Indicates whether the array returned has coefficients
98-
for octave bands or is a full-size impulse response
99-
"""
100-
return False
101-
102-
def get_azimuth(self, degrees=True):
103-
return self._orientation.get_azimuth(degrees, degrees=degrees)
104-
105-
def get_colatitude(self, degrees=True):
106-
return self._orientation.get_colatitude(degrees, degrees=degrees)
107-
108-
def set_orientation(self, orientation):
109-
"""
110-
Set orientation of directivity pattern.
111-
112-
Parameters
113-
----------
114-
orientation : DirectionVector
115-
New direction for the directivity pattern.
116-
"""
117-
assert isinstance(orientation, DirectionVector)
118-
self._orientation = orientation
119-
120-
@abc.abstractmethod
121-
def get_response(
122-
self, azimuth, colatitude=None, magnitude=False, frequency=None, degrees=True
123-
):
124-
"""
125-
Get response for provided angles and frequency.
126-
"""
127-
return
128-
129-
13031
class CardioidFamily(Directivity):
13132
"""
13233
Object for directivities coming from the

pyroomacoustics/directivities/base.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import abc
2+
3+
from .direction import DirectionVector
4+
5+
6+
class Directivity(abc.ABC):
7+
"""
8+
Abstract class for directivity patterns.
9+
10+
"""
11+
12+
def __init__(self, orientation):
13+
assert isinstance(orientation, DirectionVector)
14+
self._orientation = orientation
15+
16+
@property
17+
def is_impulse_response(self):
18+
"""
19+
Indicates whether the array returned has coefficients
20+
for octave bands or is a full-size impulse response
21+
"""
22+
return False
23+
24+
def get_azimuth(self, degrees=True):
25+
return self._orientation.get_azimuth(degrees, degrees=degrees)
26+
27+
def get_colatitude(self, degrees=True):
28+
return self._orientation.get_colatitude(degrees, degrees=degrees)
29+
30+
def set_orientation(self, orientation):
31+
"""
32+
Set orientation of directivity pattern.
33+
34+
Parameters
35+
----------
36+
orientation : DirectionVector
37+
New direction for the directivity pattern.
38+
"""
39+
assert isinstance(orientation, DirectionVector)
40+
self._orientation = orientation
41+
42+
@abc.abstractmethod
43+
def get_response(
44+
self, azimuth, colatitude=None, magnitude=False, frequency=None, degrees=True
45+
):
46+
"""
47+
Get response for provided angles and frequency.
48+
"""
49+
return
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
3+
4+
class DirectionVector(object):
5+
"""
6+
Object for representing direction vectors in 3D, parameterized by an azimuth and colatitude
7+
angle.
8+
9+
Parameters
10+
----------
11+
azimuth : float
12+
colatitude : float, optional
13+
Default to PI / 2, only XY plane.
14+
degrees : bool
15+
Whether provided values are in degrees (True) or radians (False).
16+
"""
17+
18+
def __init__(self, azimuth, colatitude=None, degrees=True):
19+
if degrees is True:
20+
azimuth = np.radians(azimuth)
21+
if colatitude is not None:
22+
colatitude = np.radians(colatitude)
23+
self._azimuth = azimuth
24+
if colatitude is None:
25+
colatitude = np.pi / 2
26+
assert colatitude <= np.pi and colatitude >= 0
27+
28+
self._colatitude = colatitude
29+
30+
self._unit_v = np.array(
31+
[
32+
np.cos(self._azimuth) * np.sin(self._colatitude),
33+
np.sin(self._azimuth) * np.sin(self._colatitude),
34+
np.cos(self._colatitude),
35+
]
36+
)
37+
38+
def get_azimuth(self, degrees=False):
39+
if degrees:
40+
return np.degrees(self._azimuth)
41+
else:
42+
return self._azimuth
43+
44+
def get_colatitude(self, degrees=False):
45+
if degrees:
46+
return np.degrees(self._colatitude)
47+
else:
48+
return self._colatitude
49+
50+
@property
51+
def unit_vector(self):
52+
"""Direction vector in cartesian coordinates."""
53+
return self._unit_v

0 commit comments

Comments
 (0)