.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/tutorials/10_base_sounds.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_generated_tutorials_10_base_sounds.py: =========== Base sounds =========== ``stimuli`` provides a common API for audio stimuli. The audio stimuli can be either generated or loaded. A generated stimuli can be exported. The volume, duration and other properties can be set when creating the stimuli or updated between plays. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python from pathlib import Path from tempfile import TemporaryDirectory from matplotlib import pyplot as plt from stimuli.audio import Sound, Tone .. GENERATED FROM PYTHON SOURCE LINES 24-29 In this tutorial, we will create, edit, save and load a pure tone auditory stimuli. A pure tone is a signal with a sinusoidal waveform, that is a sine wave of any frequency, phase-shift and amplitude. Source: `Wikipedia `_ .. GENERATED FROM PYTHON SOURCE LINES 32-37 Create and edit a pure tone --------------------------- To create the stimuli, we create a :class:`~stimuli.audio.Tone` object with a given volume and frequency. .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python sound = Tone(frequency=440, volume=10, duration=0.1) # La - A440 .. GENERATED FROM PYTHON SOURCE LINES 41-42 We can listen to the sound we created with :meth:`~stimuli.audio.Tone.play`. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python sound.play(blocking=True) .. GENERATED FROM PYTHON SOURCE LINES 46-48 We can edit the sound properties by replacing the value in the properties. For instance, let's increase the volume and change the frequency. .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python sound.volume = 50 # percentage between 0 and 100 sound.frequency = 1000 # Hz .. GENERATED FROM PYTHON SOURCE LINES 53-54 The sound is updated each time an attribute is changed. .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python sound.play(blocking=True) .. GENERATED FROM PYTHON SOURCE LINES 58-63 Export/Load a sound ------------------- We can export a sound with :meth:`~stimuli.audio.Tone.save` and load a sound with :class:`~stimuli.audio.Sound`. .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: Python with TemporaryDirectory() as directory: fname = Path(directory) / "my_pure_tone.wav" sound.save(fname, overwrite=True) sound_loaded = Sound(fname) sound_loaded.play(blocking=True) .. GENERATED FROM PYTHON SOURCE LINES 71-75 However, a loaded sound can be any type of sound. ``stimuli`` does not know that the sound was exported with the ``save()`` method of one of its class. As such, the attributes that were specific to the original sound are not present anymore and can not be updated anymore. .. GENERATED FROM PYTHON SOURCE LINES 75-78 .. code-block:: Python print(hasattr(sound_loaded, "frequency")) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 79-80 Only the basic attributes are preserved: ``duration``, ``sample_rate``. .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. code-block:: Python print(f"Duration of the original sound: {sound.duration} second.") print(f"Duration of the loaded sound: {sound_loaded.duration} second.") print(f"Sample rate of the original sound: {sound.sample_rate} Hz.") print(f"Sample rate of the loaded sound: {sound_loaded.sample_rate} Hz.") .. rst-class:: sphx-glr-script-out .. code-block:: none Duration of the original sound: 0.1 second. Duration of the loaded sound: 0.1 second. Sample rate of the original sound: 44100 Hz. Sample rate of the loaded sound: 44100 Hz. .. GENERATED FROM PYTHON SOURCE LINES 87-89 The volume is normalized, with the loudest channel set to ``100``. The ratio between channels is preserved. .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. code-block:: Python print(f"Volume of the original sound: {sound.volume}.") print(f"Volume of the loaded sound: {sound_loaded.volume}.") .. rst-class:: sphx-glr-script-out .. code-block:: none Volume of the original sound: [50.]. Volume of the loaded sound: [100.]. .. GENERATED FROM PYTHON SOURCE LINES 94-99 Visualize a sound ----------------- Finally, the underlying signal is stored in the ``signal`` property, a numpy array of shape ``(n_samples, n_channels)``. We can plot the signal of each channel. .. GENERATED FROM PYTHON SOURCE LINES 99-118 .. code-block:: Python samples_to_plot = 100 # number of samples to plot times = sound.times[:samples_to_plot] * 1000 # ms f, ax = plt.subplots(1, 1, layout="constrained") ax.plot(times, sound.signal.squeeze()[:samples_to_plot]) # draw data ax.axhline(0, color="black") # draw horizontal line through y=0 # labels ax.set_title("Signal (mono)") ax.set_xlabel("Time (ms)") # draw vertical line after each period period = int(sound.sample_rate / sound.frequency) for k in range(0, samples_to_plot, period): ax.axvline(times[k], color="lightgreen") plt.show() .. image-sg:: /generated/tutorials/images/sphx_glr_10_base_sounds_001.png :alt: Signal (mono) :srcset: /generated/tutorials/images/sphx_glr_10_base_sounds_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 119-120 Or with the convenient :meth:`~stimuli.audio.Tone.plot` method. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: Python sound.plot() plt.show() .. image-sg:: /generated/tutorials/images/sphx_glr_10_base_sounds_002.png :alt: :srcset: /generated/tutorials/images/sphx_glr_10_base_sounds_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.828 seconds) **Estimated memory usage:** 158 MB .. _sphx_glr_download_generated_tutorials_10_base_sounds.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 10_base_sounds.ipynb <10_base_sounds.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 10_base_sounds.py <10_base_sounds.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 10_base_sounds.zip <10_base_sounds.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_