.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\gallery_examples\010_sound_composer_load_project.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_examples_010_sound_composer_load_project.py: .. _sound_composer_load_project: Use an existing Sound Composer project file ------------------------------------------- The Sound Composer is a tool that allows you to generate the sound of a system by combining the sounds of its components, which we call sources here. Each source can be made of data coming from test analysis, or from a simulation, or simply consist of a single audio recording. The sources are combined into a Sound Composer project, where each source is assigned to a track. A track is a data structure made of a source data, a source control data, an output gain, and, optionally, a transfer function in the form of a digital filter (which models the transfer from the source to the listening/recording position). It can generate the sound of the component (as characterized by the source data), in specific operating conditions (the source control), and filtered according to the transfer function. This example shows how to use the Sound Composer, with the :class:`.SoundComposer` class. It starts from an existing Sound Composer project file, and illustrates the notions of Sound Composer project, track, source, source control, and filter. The example shows how to perform these operations: - Load a project file, - Get the project information and content, - Investigate the content of a track, - Generate the signal of a track, - Generate the signal of the entire Sound Composer project. .. GENERATED FROM PYTHON SOURCE LINES 54-58 Set up analysis ~~~~~~~~~~~~~~~ Setting up the analysis consists of loading the required libraries, and connecting to the DPF server. .. GENERATED FROM PYTHON SOURCE LINES 58-70 .. code-block:: Python # Load Ansys libraries. from ansys.sound.core.examples_helpers import download_sound_composer_project_whatif from ansys.sound.core.server_helpers import connect_to_or_start_server from ansys.sound.core.sound_composer import SoundComposer from ansys.sound.core.spectrogram_processing import Stft # Connect to a remote server or start a local server. my_server, lic_context = connect_to_or_start_server(use_license_context=True) .. GENERATED FROM PYTHON SOURCE LINES 74-78 Load a Sound Composer project from a file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load a Sound Composer project, using the :meth:`.SoundComposer.load()` method. A Sound Composer project file has the extension .scn, and can be created with Ansys Sound SAS. .. GENERATED FROM PYTHON SOURCE LINES 78-86 .. code-block:: Python # Download the Sound Composer project file used in this example. path_sound_composer_project_scn = download_sound_composer_project_whatif(server=my_server) # Create a SoundComposer object and load the project. sound_composer_project = SoundComposer() sound_composer_project.load(path_sound_composer_project_scn) .. GENERATED FROM PYTHON SOURCE LINES 87-88 You can use built-in :func:`print()` function to display a summary of the content of the project. .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python print(sound_composer_project) .. rst-class:: sphx-glr-script-out .. code-block:: none Sound Composer object (4 track(s)) Track 1: SourceHarmonics, "eMotor - FEM", gain = +0.0 dB Track 2: SourceHarmonics, "Gear - MBD", gain = +0.0 dB Track 3: SourceBroadbandNoise, "HVAC - CFD", gain = +0.0 dB Track 4: SourceBroadbandNoise, "Road and wind", gain = +0.0 dB .. GENERATED FROM PYTHON SOURCE LINES 91-92 You can see that this project is made of 4 tracks: .. GENERATED FROM PYTHON SOURCE LINES 94-99 - a track with a harmonics source, coming form the FEM simulation of an e-motor, - a track with another harmonics source, coming from the multibody simulation of a gearbox, - a track with a broadband noise source, coming from the CFD simulation of a HVAC system, - a track with another broadband noise source, coming form the analysis of a background noise measurement in the cabin, which would correspond to the rolling noise and the wind noise. .. GENERATED FROM PYTHON SOURCE LINES 101-104 Explore the project's list of tracks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Let us have a closer look at the content of each individual track by printing it. .. GENERATED FROM PYTHON SOURCE LINES 104-108 .. code-block:: Python for i, track in enumerate(sound_composer_project.tracks, start=1): print(f"--- Track n. {i} ---\n{track}\n") .. rst-class:: sphx-glr-script-out .. code-block:: none --- Track n. 1 --- eMotor - FEM Harmonics source: '' Number of orders: 24 [ 2. 4. 6. 8. 10. ... 40. 42. 44. 46. 48.] Control parameter: RPM, 500.0 - 10000.0 rpm [ 500. 1000. 2000. 3000. 4000. ... 6000. 7000. 8000. 9000. 10000.] Source control: RPM Min: 250.0 Max: 5000.0 Duration: 8.00000037997961 s Gain: +0.0 dB Filter: Set --- Track n. 2 --- Gear - MBD Harmonics source: '' Number of orders: 50 [1. 2. 3. 4. 5. ... 46. 47. 48. 49. 50.] Control parameter: RPM, 0.0 - 4960.0 rpm [ 0. 20. 40. 60. 80. ... 4880. 4900. 4920. 4940. 4960.] Source control: RPM Min: 250.0 Max: 5000.0 Duration: 8.00000037997961 s Gain: +0.0 dB Filter: Set --- Track n. 3 --- HVAC - CFD Broadband noise source: '' Spectrum type: Narrow band (DeltaF: 12.2 Hz) Spectrum count: 2 Control parameter: Speed profile_original, rpm [2065. 2950.] Source control: Speed profile_original Min: 2600.0 rpm Max: 2600.0 rpm Duration: 8.00000037997961 Hz Gain: +0.0 dB Filter: Not set --- Track n. 4 --- Road and wind Broadband noise source: '' Spectrum type: Narrow band (DeltaF: 10.0 Hz) Spectrum count: 100 Control parameter: Speed profile - 2, kph [0. 1. 2. 3. 4. ... 95. 96. 97. 98. 99.] Source control: Speed profile - 2 Min: 10.0 kph Max: 100.0 kph Duration: 8.00000037997961 Hz Gain: +0.0 dB Filter: Not set .. GENERATED FROM PYTHON SOURCE LINES 109-111 For each track, you can see some details about the source content, the associated control profile, and whether the track includes a filter or not. .. GENERATED FROM PYTHON SOURCE LINES 113-121 Explore the content of a track ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To look at a specific track, use the attribute :attr:`.tracks` of the :class:`.SoundComposer` object, containing the list of the tracks included. For example, let us extract the second track of the project, which contains the gearbox source. The track object returned is of type :class:`.Track`. You can print it to display its content, as shown in the previous section. .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: Python track_gear = sound_composer_project.tracks[1] .. GENERATED FROM PYTHON SOURCE LINES 124-133 This track contains a harmonics source, stored in its :attr:`.Track.source` attribute of type :class:`.SourceHarmonics`. Here, it consists of a set of 50 harmonics, which are defined by their order numbers, and their levels over 249 values of the control parameter (RPM). These data are stored in the :attr:`.SourceHarmonics.source_harmonics` attribute of the track's source object, as a :class:`FieldsContainer ` object. It contains 249 fields, each corresponding to a specific value of the control parameter (RPM), and containing the levels in Pa² of the 50 harmonics at this RPM value. .. GENERATED FROM PYTHON SOURCE LINES 133-138 .. code-block:: Python print( f"Number of RPM points defined in the source dataset: " f"{len(track_gear.source.source_harmonics)}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of RPM points defined in the source dataset: 249 .. GENERATED FROM PYTHON SOURCE LINES 139-142 The source control data (that is, the RPM values over time) can be accessed using the :attr:`.SourceHarmonics.source_control` attribute of the source in the track. Let us display this control profile in a figure: it is a linear ramp-up from 250 rpm to 5000 rpm, over 8 seconds. .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: Python track_gear.source.plot_control() .. image-sg:: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_001.png :alt: Control profile 1 :srcset: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 145-148 The track also contains a filter, stored as a :class:`.Filter` object. Here, it is a finite impulse response (FIR) filter that models the transfer between the source and the listening position. .. GENERATED FROM PYTHON SOURCE LINES 148-151 .. code-block:: Python track_gear.filter print(track_gear.filter) .. rst-class:: sphx-glr-script-out .. code-block:: none Sampling frequency: 44100.0 Hz Numerator coefficients (B): [0.007285992614924908, 0.018362130969762802, 0.02472005784511566, 0.028517061844468117, 0.033101242035627365, ... ] Denominator coefficients (A): [1.0] .. GENERATED FROM PYTHON SOURCE LINES 152-154 Let us generate the signal corresponding to the track using the :meth:`.Track.process()` method, plot its waveform, and display its spectrogram with the :class:`.Stft` class. .. GENERATED FROM PYTHON SOURCE LINES 154-161 .. code-block:: Python track_gear.process(sampling_frequency=44100.0) track_gear.plot() spectrogram_gear = Stft(signal=track_gear.get_output()) spectrogram_gear.process() spectrogram_gear.plot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_002.png :alt: Gear - MBD (SourceHarmonics) :srcset: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_003.png :alt: STFT, Amplitude, Phase :srcset: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_003.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 162-163 If needed, you can access the output signal of a track using :meth:`.Track.get_output()`. .. GENERATED FROM PYTHON SOURCE LINES 163-165 .. code-block:: Python signal_gear = track_gear.get_output() .. GENERATED FROM PYTHON SOURCE LINES 166-172 Generate the signal of the project ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now, let us generate the signal of the project using the :meth:`.SoundComposer.process()` method. This method generates, for each track, the acoustic signal of the source, filters it with the associated filter, if any, and applies the track gain, and finally sums all resulting tracks' signals together. .. GENERATED FROM PYTHON SOURCE LINES 174-175 Generate the signal of the project, using a sampling frequency of 44100 Hz. .. GENERATED FROM PYTHON SOURCE LINES 175-177 .. code-block:: Python sound_composer_project.process(sampling_frequency=44100.0) .. GENERATED FROM PYTHON SOURCE LINES 178-180 You can display the waveform of the generated signal using the :meth:`.SoundComposer.plot()` method, and its spectrogram using the :class:`.Stft` class. .. GENERATED FROM PYTHON SOURCE LINES 180-186 .. code-block:: Python sound_composer_project.plot() spectrogram = Stft(signal=sound_composer_project.get_output()) spectrogram.process() spectrogram.plot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_004.png :alt: Generated signal :srcset: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_005.png :alt: STFT, Amplitude, Phase :srcset: /examples/gallery_examples/images/sphx_glr_010_sound_composer_load_project_005.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 187-199 Conclusion ~~~~~~~~~~ This workflow allows you to analyze and listen to the sound of the gearbox and the e-motor, in realistic conditions, that is, including the HVAC noise and the background noise inside the cabin. By analyzing the spectrogram, you can anticipate how some harmonic tones from the e-motor and the gearbox may be perceived by the passengers in the cabin. Further investigations can be conducted, using other tools included in PyAnsys Sound, such as modules :mod:`Standard levels `, :mod:`Psychoacoustics ` and :mod:`Spectral processing `. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 37.586 seconds) .. _sphx_glr_download_examples_gallery_examples_010_sound_composer_load_project.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 010_sound_composer_load_project.ipynb <010_sound_composer_load_project.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 010_sound_composer_load_project.py <010_sound_composer_load_project.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 010_sound_composer_load_project.zip <010_sound_composer_load_project.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_