From f75f6c049b23029ea897e9517d6bf5e51937ce2e Mon Sep 17 00:00:00 2001 From: Antoine Minard Date: Tue, 26 Nov 2024 10:35:30 +0100 Subject: [PATCH] Missing checks --- .../core/sound_composer/source_spectrum.py | 18 +++++-- .../test_sound_composer_source_spectrum.py | 48 +++++++++++++++++-- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/ansys/sound/core/sound_composer/source_spectrum.py b/src/ansys/sound/core/sound_composer/source_spectrum.py index 58557443..5162523f 100644 --- a/src/ansys/sound/core/sound_composer/source_spectrum.py +++ b/src/ansys/sound/core/sound_composer/source_spectrum.py @@ -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: @@ -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 " diff --git a/tests/tests_sound_composer/test_sound_composer_source_spectrum.py b/tests/tests_sound_composer/test_sound_composer_source_spectrum.py index 9b938552..157a9de2 100644 --- a/tests/tests_sound_composer/test_sound_composer_source_spectrum.py +++ b/tests/tests_sound_composer/test_sound_composer_source_spectrum.py @@ -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 @@ -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) @@ -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.""" @@ -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( @@ -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."""