Skip to content

Commit

Permalink
Missing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ansaminard committed Nov 26, 2024
1 parent fd043d5 commit f75f6c0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/ansys/sound/core/sound_composer/source_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,17 @@ def source_spectrum_data(self) -> Field:
@source_spectrum_data.setter
def source_spectrum_data(self, source_spectrum_data: Field):
"""Set the spectrum source data, from a DPF field."""
if not (isinstance(source_spectrum_data, Field) or source_spectrum_data is None):
raise PyAnsysSoundException(
"Specified spectrum source must be provided as a DPF field."
)
if source_spectrum_data is not None:
if not isinstance(source_spectrum_data, Field):
raise PyAnsysSoundException(
"Specified spectrum source must be provided as a DPF field."
)

if len(source_spectrum_data.time_freq_support.time_frequencies.data) < 2:
raise PyAnsysSoundException(
"Specified spectrum source must contain at least two elements."
)

self.__source_spectrum_data = source_spectrum_data

def is_source_control_valid(self) -> bool:
Expand Down Expand Up @@ -162,6 +169,9 @@ def process(self, sampling_frequency: float = 44100.0):
sampling_frequency : float, default 44100.0
Sampling frequency of the generated sound in Hz.
"""
if sampling_frequency <= 0.0:
raise PyAnsysSoundException("Sampling frequency must be strictly positive.")

if not self.is_source_control_valid():
raise PyAnsysSoundException(
"Spectrum source control is not valid. Either it is not set "
Expand Down
48 changes: 44 additions & 4 deletions tests/tests_sound_composer/test_sound_composer_source_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pytest

from ansys.sound.core._pyansys_sound import PyAnsysSoundException, PyAnsysSoundWarning
from ansys.sound.core.signal_utilities.load_wav import LoadWav
from ansys.sound.core.sound_composer import SourceControlSpectrum, SourceSpectrum
from ansys.sound.core.spectral_processing import PowerSpectralDensity

Expand Down Expand Up @@ -81,8 +82,20 @@ def test_source_spectrum_properties(dpf_sound_test_server):
source_spectrum.source_control = SourceControlSpectrum()
assert isinstance(source_spectrum.source_control, SourceControlSpectrum)

# Compute a signal's power spectral density to check source_spectrum_data property.
wav_loader = LoadWav(pytest.data_path_flute_in_container)
wav_loader.process()
psd = PowerSpectralDensity(
input_signal=wav_loader.get_output()[0],
fft_size=8192,
window_type="HANN",
window_length=8192,
overlap=0.75,
)
psd.process()

# Test source_spectrum_data property.
source_spectrum.source_spectrum_data = Field()
source_spectrum.source_spectrum_data = psd.get_output()
assert isinstance(source_spectrum.source_spectrum_data, Field)


Expand All @@ -97,12 +110,34 @@ def test_source_spectrum_propertiess_exceptions(dpf_sound_test_server):
):
source_spectrum.source_control = "InvalidType"

# Test source_spectrum_data setter exception (str instead a Field).
# Test source_spectrum_data setter exception 1 (str instead a Field).
with pytest.raises(
PyAnsysSoundException, match="Specified spectrum source must be provided as a DPF field."
):
source_spectrum.source_spectrum_data = "InvalidType"

# Compute a signal's power spectral density to check source_spectrum_data property exception 2.
wav_loader = LoadWav(pytest.data_path_flute_in_container)
wav_loader.process()
psd = PowerSpectralDensity(
input_signal=wav_loader.get_output()[0],
fft_size=8192,
window_type="HANN",
window_length=8192,
overlap=0.75,
)
psd.process()
field = psd.get_output()

# Remove all frequencies from the field to trigger the exception.
field.time_freq_support.time_frequencies.data = []

# Test source_spectrum_data setter exception 2 (not enough elements).
with pytest.raises(
PyAnsysSoundException, match="Specified spectrum source must contain at least two elements."
):
source_spectrum.source_spectrum_data = field


def test_source_spectrum_is_source_control_valid(dpf_sound_test_server):
"""Test SourceSpectrum is_source_control_valid method."""
Expand Down Expand Up @@ -142,8 +177,6 @@ def test_source_spectrum_process(dpf_sound_test_server):

def test_source_spectrum_process_exceptions(dpf_sound_test_server):
"""Test SourceSpectrum process method exceptions."""
source_spectrum = SourceSpectrum(pytest.data_path_sound_composer_spectrum_source_in_container)

# Test process method exception1 (missing control).
source_spectrum = SourceSpectrum(pytest.data_path_sound_composer_spectrum_source_in_container)
with pytest.raises(
Expand All @@ -168,6 +201,13 @@ def test_source_spectrum_process_exceptions(dpf_sound_test_server):
):
source_spectrum.process()

# Test process method exception3 (invalid sampling frequency value).
source_spectrum = SourceSpectrum()
with pytest.raises(
PyAnsysSoundException, match="Sampling frequency must be strictly positive."
):
source_spectrum.process(sampling_frequency=0.0)


def test_source_spectrum_get_output(dpf_sound_test_server):
"""Test SourceSpectrum get_output method."""
Expand Down

0 comments on commit f75f6c0

Please sign in to comment.