Time stretch and pitch shift

time_stretch changes the length and keeps the pitch; pitch_shift changes the pitch and keeps the length.

import numpy as np

import audio_samples as aus
from audio_samples import mpl
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
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.8052 (-1.9 dBFS)
RMS0.4796 (-6.4 dBFS)


rate=1.5 plays 1.5 times as fast, so the result is two thirds as long.

faster = audio.time_stretch(1.5)
assert abs(faster.samples_per_channel() - audio.samples_per_channel() / 1.5) < 2048
faster
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration1.333 s (29400 samples per channel)
peak0.8142 (-1.8 dBFS)
RMS0.4787 (-6.4 dBFS)


Twelve semitones up is one octave: 220 Hz becomes 440 Hz.

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
440.1 Hz
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.8376 (-1.5 dBFS)
RMS0.4167 (-7.6 dBFS)


fig, ax = mpl.plot_spectrum_comparison(
    [audio, octave_up], ["Original", "Pitch shifted +12"], freq_max=2000.0
)
Spectrum Comparison

Total running time of the script: (0 minutes 0.448 seconds)