.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/effects/plot_time_stretch_and_pitch_shift.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_gallery_effects_plot_time_stretch_and_pitch_shift.py: Time stretch and pitch shift ============================ ``time_stretch`` changes the length and keeps the pitch; ``pitch_shift`` changes the pitch and keeps the length. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import numpy as np import audio_samples as aus from audio_samples import mpl .. GENERATED FROM PYTHON SOURCE LINES 15-19 .. code-block:: Python sr = 22050 audio = aus.generation.compound_tone([(220.0, 0.6), (440.0, 0.3), (660.0, 0.1)], 2.0, sr).as_f32() audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.8052 (-1.9 dBFS)
RMS0.4796 (-6.4 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 20-21 ``rate=1.5`` plays 1.5 times as fast, so the result is two thirds as long. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python faster = audio.time_stretch(1.5) assert abs(faster.samples_per_channel() - audio.samples_per_channel() / 1.5) < 2048 faster .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration1.333 s (29400 samples per channel)
peak0.8142 (-1.8 dBFS)
RMS0.4787 (-6.4 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 26-27 Twelve semitones up is one octave: 220 Hz becomes 440 Hz. .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python octave_up = audio.pitch_shift(12.0) assert octave_up.samples_per_channel() == audio.samples_per_channel() f0 = octave_up.detect_pitch_yin(threshold=0.1, min_frequency=65.0, max_frequency=1000.0) print(f"{f0:.1f} Hz") assert abs(f0 - 440.0) < 5.0 octave_up .. rst-class:: sphx-glr-script-out .. code-block:: none 440.1 Hz .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.8376 (-1.5 dBFS)
RMS0.4167 (-7.6 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python fig, ax = mpl.plot_spectrum_comparison( [audio, octave_up], ["Original", "Pitch shifted +12"], freq_max=2000.0 ) .. image-sg:: /gallery/effects/images/sphx_glr_plot_time_stretch_and_pitch_shift_001.png :alt: Spectrum Comparison :srcset: /gallery/effects/images/sphx_glr_plot_time_stretch_and_pitch_shift_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.448 seconds) .. _sphx_glr_download_gallery_effects_plot_time_stretch_and_pitch_shift.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_time_stretch_and_pitch_shift.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_time_stretch_and_pitch_shift.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_time_stretch_and_pitch_shift.zip `