.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\gallery_examples\002_load_resample_amplify_write_wav_files.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_002_load_resample_amplify_write_wav_files.py: .. _load_resample_amplify_write_wav_files_example: Load a signal from a WAV file ----------------------------- This example shows how to load a signal from a WAV file, modify the signal's sampling frequency, and amplify it. It also shows how to access the corresponding data, display it using Matplotlib, and then write the modified signal to the disk as a new WAV file. .. GENERATED FROM PYTHON SOURCE LINES 36-40 Set up analysis ~~~~~~~~~~~~~~~ Setting up the analysis consists of loading Ansys libraries, connecting to the DPF server, and retrieving the example files. .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. code-block:: Python # Load Ansys and other libraries. import matplotlib.pyplot as plt from ansys.sound.core.examples_helpers import download_flute_wav from ansys.sound.core.server_helpers import connect_to_or_start_server from ansys.sound.core.signal_utilities import ApplyGain, LoadWav, Resample, WriteWav # Connect to a remote server or start a local server server = connect_to_or_start_server() .. GENERATED FROM PYTHON SOURCE LINES 52-58 Load a signal ~~~~~~~~~~~~~ Load a signal from a WAV file using the ``LoadWav`` class. It is returned as a DPF fields container. For more information, see `fields_container `_ in the DPF-Core API documentation. .. GENERATED FROM PYTHON SOURCE LINES 58-71 .. code-block:: Python # Return the input data of the example file path_flute_wav = download_flute_wav() # Load the WAV file. wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal_original = wav_loader.get_output() t1 = fc_signal_original[0].time_freq_support.time_frequencies.data sf1 = 1.0 / (t1[1] - t1[0]) print(f"The sampling frequency of the original signal is {int(sf1)} Hz.") .. rst-class:: sphx-glr-script-out .. code-block:: none The sampling frequency of the original signal is 44100 Hz. .. GENERATED FROM PYTHON SOURCE LINES 72-75 Resample the signal ~~~~~~~~~~~~~~~~~~~ Change the sampling frequency of the loaded signal. .. GENERATED FROM PYTHON SOURCE LINES 75-83 .. code-block:: Python resampler = Resample(fc_signal_original, new_sampling_frequency=20000.0) resampler.process() fc_signal_resampled = resampler.get_output() t2 = fc_signal_resampled[0].time_freq_support.time_frequencies.data sf2 = 1.0 / (t2[1] - t2[0]) print(f"The new sampling frequency of the signal is {int(sf2)} Hz.") .. rst-class:: sphx-glr-script-out .. code-block:: none The new sampling frequency of the signal is 20000 Hz. .. GENERATED FROM PYTHON SOURCE LINES 84-87 Apply a gain to the signal ~~~~~~~~~~~~~~~~~~~~~~~~~~ Amplify the resampled signal by 10 decibels. .. GENERATED FROM PYTHON SOURCE LINES 87-92 .. code-block:: Python gain = 10.0 gain_applier = ApplyGain(fc_signal_resampled, gain=gain, gain_in_db=True) gain_applier.process() fc_signal_modified = gain_applier.get_output() .. GENERATED FROM PYTHON SOURCE LINES 93-96 Plot signals ~~~~~~~~~~~~ Plot both the original signal and modified signal. .. GENERATED FROM PYTHON SOURCE LINES 96-121 .. code-block:: Python # Get the signals as nparray data_original = wav_loader.get_output_as_nparray() data_modified = gain_applier.get_output_as_nparray() # Prepare the figure fig, axs = plt.subplots(2) fig.suptitle("Signals") axs[0].plot(t1, data_original, color="g", label=f"original signal, sf={int(sf1)} Hz") axs[0].set_ylabel("Pa") axs[0].legend(loc="upper right") axs[0].set_ylim([-3, 3]) axs[1].plot( t2, data_modified, color="r", label=f"modified signal, sf={int(sf2)} Hz, gain={gain} dBSPL" ) axs[1].set_xlabel("Time(s)") axs[1].set_ylabel("Amplitude(Pa)") axs[1].legend(loc="upper right") axs[1].set_ylim([-3, 3]) # Display the figure plt.show() .. image-sg:: /examples/gallery_examples/images/sphx_glr_002_load_resample_amplify_write_wav_files_001.png :alt: Signals :srcset: /examples/gallery_examples/images/sphx_glr_002_load_resample_amplify_write_wav_files_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 122-125 Write the signal as a WAV file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Write the modified signal to the disk as a WAV file. .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: Python output_path = path_flute_wav[:-4] + "_modified.wav" # "[-4]" is to remove the ".wav" extension wav_writer = WriteWav(path_to_write=output_path, signal=fc_signal_modified, bit_depth="int16") wav_writer.process() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.810 seconds) .. _sphx_glr_download_examples_gallery_examples_002_load_resample_amplify_write_wav_files.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 002_load_resample_amplify_write_wav_files.ipynb <002_load_resample_amplify_write_wav_files.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 002_load_resample_amplify_write_wav_files.py <002_load_resample_amplify_write_wav_files.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_