Skip to content

Commit

Permalink
feat: Sound Composer track (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
ansaminard and pyansys-ci-bot authored Dec 19, 2024
1 parent a089077 commit 5133c93
Show file tree
Hide file tree
Showing 15 changed files with 859 additions and 31 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/191.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: Sound Composer track
1 change: 1 addition & 0 deletions doc/source/api/sound_composer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Sound composer
.. autosummary::
:toctree: _autosummary

Track
SourceSpectrum
SourceBroadbandNoise
SourceBroadbandNoiseTwoParameters
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/sound/core/sound_composer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
from .source_harmonics import SourceHarmonics
from .source_harmonics_two_parameters import SourceHarmonicsTwoParameters
from .source_spectrum import SourceSpectrum
from .track import Track

__all__ = (
"Track",
"SoundComposerParent",
"SourceParent",
"SourceControlParent",
Expand All @@ -48,6 +50,7 @@
"SourceBroadbandNoise",
"SourceBroadbandNoiseTwoParameters",
"SourceHarmonics",
"SourceHarmonicsTwoParameters",
"SourceControlTime",
"SourceAudio",
)
4 changes: 4 additions & 0 deletions src/ansys/sound/core/sound_composer/_source_parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ class SourceParent(SoundComposerParent):
def is_source_control_valid(self) -> bool:
"""Check if the source control is valid."""
return False

def plot_control(self):
"""Plot the source control(s) in a figure."""
pass
23 changes: 23 additions & 0 deletions src/ansys/sound/core/sound_composer/source_broadband_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ def plot(self):
plt.grid(True)
plt.show()

def plot_control(self):
"""Plot the source control(s) in a figure."""
if not self.is_source_control_valid():
raise PyAnsysSoundException(
"Broadband noise source control is not set. "
f"Use ``{__class__.__name__}.source_control``."
)

data = self.source_control.control.data
time = self.source_control.control.time_freq_support.time_frequencies.data
unit = self.source_control.control.unit
name = self.source_control.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
plt.plot(time, data)
plt.title("Control profile 1")
plt.ylabel(f"{name_str}{unit_str}")
plt.xlabel("Time (s)")
plt.grid(True)

plt.tight_layout()
plt.show()

def __extract_bbn_info(self) -> tuple[str, float, str, str, list[float]]:
"""Extract the broadband noise source information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class SourceBroadbandNoiseTwoParameters(SourceParent):
def __init__(
self,
file: str = "",
control1: SourceControlTime = None,
control2: SourceControlTime = None,
source_control1: SourceControlTime = None,
source_control2: SourceControlTime = None,
):
"""Class instantiation takes the following parameters.
Expand All @@ -58,16 +58,16 @@ def __init__(
file : str, default: ""
Path to the broadband noise with two parameters file. Supported files are text files
with the header `AnsysSound_BBN_MultipleParameters`.
control1 : SourceControlTime, default: None
source_control1 : SourceControlTime, default: None
First Source control, consisting of the control parameter values over time, to use when
generating the sound from this source.
control2 : SourceControlTime, default: None
source_control2 : SourceControlTime, default: None
Second source control, consisting of the control parameter values over time, to use
when generating the sound from this source.
"""
super().__init__()
self.source_control1 = control1
self.source_control2 = control2
self.source_control1 = source_control1
self.source_control2 = source_control2

# Define DPF Sound operators.
self.__operator_load = Operator(ID_COMPUTE_LOAD_SOURCE_BBN_2PARAMS)
Expand Down Expand Up @@ -279,8 +279,8 @@ def process(self, sampling_frequency: float = 44100.0):

if not self.is_source_control_valid():
raise PyAnsysSoundException(
"At least one source control for broadband noise with two parameters is not set. "
f"Use ``{__class__.__name__}.source_control1`` and/or "
"At least one source control for broadband noise source with two parameters is "
f"not set. Use ``{__class__.__name__}.source_control1`` and/or "
f"``{__class__.__name__}.source_control2``."
)

Expand Down Expand Up @@ -353,6 +353,43 @@ def plot(self):
plt.grid(True)
plt.show()

def plot_control(self):
"""Plot the source control(s) in a figure."""
if not self.is_source_control_valid():
raise PyAnsysSoundException(
"At least one source control for broadband noise source with two parameters is "
f"not set. Use ``{__class__.__name__}.source_control1`` and/or "
f"``{__class__.__name__}.source_control2``."
)

_, axes = plt.subplots(2, 1, sharex=True)

data = self.source_control1.control.data
time = self.source_control1.control.time_freq_support.time_frequencies.data
unit = self.source_control1.control.unit
name = self.source_control1.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
axes[0].plot(time, data)
axes[0].set_title("Control profile 1")
axes[0].set_ylabel(f"{name_str}{unit_str}")
axes[0].grid(True)

data = self.source_control2.control.data
time = self.source_control2.control.time_freq_support.time_frequencies.data
unit = self.source_control2.control.unit
name = self.source_control2.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
axes[1].plot(time, data)
axes[1].set_title("Control profile 2")
axes[1].set_ylabel(f"{name_str}{unit_str}")
axes[1].set_xlabel("Time (s)")
axes[1].grid(True)

plt.tight_layout()
plt.show()

def __extract_bbn_two_parameters_info(
self,
) -> tuple[str, float, str, str, tuple[float], str, str, tuple[float]]:
Expand Down
23 changes: 23 additions & 0 deletions src/ansys/sound/core/sound_composer/source_harmonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ def plot(self):
plt.grid(True)
plt.show()

def plot_control(self):
"""Plot the source control(s) in a figure."""
if not self.is_source_control_valid():
raise PyAnsysSoundException(
"Harmonics source control is not set. "
f"Use ``{__class__.__name__}.source_control``."
)

data = self.source_control.control.data
time = self.source_control.control.time_freq_support.time_frequencies.data
unit = self.source_control.control.unit
name = self.source_control.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
plt.plot(time, data)
plt.title("Control profile 1")
plt.ylabel(f"{name_str}{unit_str}")
plt.xlabel("Time (s)")
plt.grid(True)

plt.tight_layout()
plt.show()

def __extract_harmonics_info(self) -> tuple[list[float], str, list[float]]:
"""Extract the harmonics source information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ def process(self, sampling_frequency: float = 44100.0):
if not self.is_source_control_valid():
raise PyAnsysSoundException(
"At least one source control for harmonics source with two parameters is not set. "
f"Use ``{__class__.__name__}.control_rpm`` and/or "
f"``{__class__.__name__}.control2``."
f"Use ``{__class__.__name__}.source_control_rpm`` and/or "
f"``{__class__.__name__}.source_control2``."
)

if self.source_harmonics_two_parameters is None:
Expand Down Expand Up @@ -369,6 +369,43 @@ def plot(self):
plt.grid(True)
plt.show()

def plot_control(self):
"""Plot the source control(s) in a figure."""
if not self.is_source_control_valid():
raise PyAnsysSoundException(
"At least one source control for harmonics source with two parameters is not set. "
f"Use ``{__class__.__name__}.source_control_rpm`` and/or "
f"``{__class__.__name__}.source_control2``."
)

_, axes = plt.subplots(2, 1, sharex=True)

data = self.source_control_rpm.control.data
time = self.source_control_rpm.control.time_freq_support.time_frequencies.data
unit = self.source_control_rpm.control.unit
name = self.source_control_rpm.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
axes[0].plot(time, data)
axes[0].set_title("Control profile 1")
axes[0].set_ylabel(f"{name_str}{unit_str}")
axes[0].grid(True)

data = self.source_control2.control.data
time = self.source_control2.control.time_freq_support.time_frequencies.data
unit = self.source_control2.control.unit
name = self.source_control2.control.name
unit_str = f" ({unit})" if len(unit) > 0 else ""
name_str = name if len(name) > 0 else "Amplitude"
axes[1].plot(time, data)
axes[1].set_title("Control profile 2")
axes[1].set_ylabel(f"{name_str}{unit_str}")
axes[1].set_xlabel("Time (s)")
axes[1].grid(True)

plt.tight_layout()
plt.show()

def __extract_harmonics_two_parameters_info(
self,
) -> tuple[list[float], str, tuple[float], str, str, tuple[float]]:
Expand Down
Loading

0 comments on commit 5133c93

Please sign in to comment.