Skip to content

Commit

Permalink
fix: add field type in loudness ISO532-1 (stationary/time-varying) (#223
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ansaminard authored Feb 4, 2025
1 parent a7449f2 commit 0d7df0d
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/223.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: add field type in loudness ISO532-1 (stationary/time-varying)
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import matplotlib.pyplot as plt
import numpy as np

from . import PsychoacousticsParent
from . import FIELD_DIFFUSE, FIELD_FREE, PsychoacousticsParent
from .._pyansys_sound import PyAnsysSoundException, PyAnsysSoundWarning

LOUDNESS_SONE_ID = "sone"
LOUDNESS_LEVEL_PHON_ID = "phon"
SPECIFIC_LOUDNESS_ID = "specific"

# Name of the DPF Sound operator used in this module.
ID_COMPUTE_LOUDNESS_ISO_STATIONARY = "compute_loudness_iso532_1"


class LoudnessISO532_1_Stationary(PsychoacousticsParent):
"""Computes ISO 532-1 loudness for stationary sounds.
Expand All @@ -42,17 +45,24 @@ class LoudnessISO532_1_Stationary(PsychoacousticsParent):
sounds.
"""

def __init__(self, signal: Field | FieldsContainer = None):
def __init__(
self,
signal: Field | FieldsContainer = None,
field_type: str = FIELD_FREE,
):
"""Class instantiation takes the following parameters.
Parameters
----------
signal : Field | FieldsContainer
Signal in Pa to compute loudness on as a DPF field or fields container.
field_type : str, default: "Free"
Sound field type. Available options are `"Free"` and `"Diffuse"`.
"""
super().__init__()
self.signal = signal
self.__operator = Operator("compute_loudness_iso532_1")
self.field_type = field_type
self.__operator = Operator(ID_COMPUTE_LOUDNESS_ISO_STATIONARY)

@property
def signal(self) -> Field | FieldsContainer:
Expand All @@ -64,6 +74,24 @@ def signal(self, signal: Field | FieldsContainer):
"""Set the signal."""
self.__signal = signal

@property
def field_type(self) -> str:
"""Sound field type.
Available options are `"Free"` and `"Diffuse"`.
"""
return self.__field_type

@field_type.setter
def field_type(self, field_type: str):
"""Set the sound field type."""
if field_type.lower() not in [FIELD_FREE.lower(), FIELD_DIFFUSE.lower()]:
raise PyAnsysSoundException(
f'Invalid field type "{field_type}". Available options are "{FIELD_FREE}" and '
f'"{FIELD_DIFFUSE}".'
)
self.__field_type = field_type

def process(self):
"""Compute the loudness.
Expand All @@ -76,6 +104,7 @@ def process(self):
)

self.__operator.connect(0, self.signal)
self.__operator.connect(1, self.field_type)

# Runs the operator
self.__operator.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import matplotlib.pyplot as plt
import numpy as np

from . import PsychoacousticsParent
from . import FIELD_DIFFUSE, FIELD_FREE, PsychoacousticsParent
from .._pyansys_sound import PyAnsysSoundException, PyAnsysSoundWarning

# Name of the DPF Sound operator used in this module.
ID_COMPUTE_LOUDNESS_ISO_TIME_VARYING = "compute_loudness_iso532_1_vs_time"


class LoudnessISO532_1_TimeVarying(PsychoacousticsParent):
"""Computes ISO 532-1 loudness for time-varying sounds.
Expand All @@ -38,18 +41,25 @@ class LoudnessISO532_1_TimeVarying(PsychoacousticsParent):
sounds.
"""

def __init__(self, signal: Field | FieldsContainer = None):
def __init__(
self,
signal: Field | FieldsContainer = None,
field_type: str = FIELD_FREE,
):
"""Class instantiation takes the following parameters.
Parameters
----------
signal : Field | FieldsContainer
Signal to compute time-varying ISO532-1 loudness on as a DPF field or fields
container.
field_type : str, default: "Free"
Sound field type. Available options are `"Free"` and `"Diffuse"`.
"""
super().__init__()
self.signal = signal
self.__operator = Operator("compute_loudness_iso532_1_vs_time")
self.field_type = field_type
self.__operator = Operator(ID_COMPUTE_LOUDNESS_ISO_TIME_VARYING)

@property
def signal(self) -> Field | FieldsContainer:
Expand All @@ -61,6 +71,24 @@ def signal(self, signal: Field | FieldsContainer):
"""Set the signal."""
self.__signal = signal

@property
def field_type(self) -> str:
"""Sound field type.
Available options are `"Free"` and `"Diffuse"`.
"""
return self.__field_type

@field_type.setter
def field_type(self, field_type: str):
"""Set the sound field type."""
if field_type.lower() not in [FIELD_FREE.lower(), FIELD_DIFFUSE.lower()]:
raise PyAnsysSoundException(
f'Invalid field type "{field_type}". Available options are "{FIELD_FREE}" and '
f'"{FIELD_DIFFUSE}".'
)
self.__field_type = field_type

def process(self):
"""Compute the time-varying ISO532-1 loudness.
Expand All @@ -73,6 +101,7 @@ def process(self):
)

self.__operator.connect(0, self.signal)
self.__operator.connect(1, self.field_type)

# Runs the operator
self.__operator.run()
Expand Down
Loading

0 comments on commit 0d7df0d

Please sign in to comment.