2

i'm trying to increase/decrease the pitch(or speed) on a few .wav files in pydub. I tried using sound.set_frame_rate (i multiplied the original frame rate, but nothing changed). Does anyone know how this can be done? (preferably without downloading additional external libraries). thanks.

Sh0z
  • 127
  • 1
  • 2
  • 7
  • 1
    did you assign the result of `sound.set_frame_rate()` to a new var? audio segments are immutable, none of the methods will cause a change, in place (they all return a new audio segment) – Jiaaro Apr 14 '17 at 18:39
  • @Jiaaro thanks,i tried that now. it seems to have some effec but only when i make the frame rate slower (for example 44100/2,44100/6 etc..) but when i make it larger nothing happens. do you know why? also do you know any other effects i can do with pydub? – Sh0z Apr 15 '17 at 18:55

1 Answers1

9

sound.set_frame_rate() does a conversion, it should not cause any "chipmunk effect", but what you can do is change the frame rate (without a conversion) and then convert the audio from there back to a normal frame rate (like 44.1 kHz, "CD quality")

from pydub import AudioSegment
sound = AudioSegment.from_file(…)

def speed_change(sound, speed=1.0):
    # Manually override the frame_rate. This tells the computer how many
    # samples to play per second
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
        "frame_rate": int(sound.frame_rate * speed)
    })

    # convert the sound with altered frame rate to a standard frame rate
    # so that regular playback programs will work right. They often only
    # know how to play audio at standard frame rate (like 44.1k)
    return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)

slow_sound = speed_change(sound, 0.75)
fast_sound = speed_change(sound, 2.0)
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • thnx that worked! could you please explain more in depth what these lines do? `sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={ "frame_rate": int(sound.frame_rate * speed) }) return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)` – Sh0z Apr 18 '17 at 13:50
  • @Sh0z I've added some explanatory comments – Jiaaro Apr 18 '17 at 15:40
  • slow down sounds very creepy – davegallant Oct 19 '19 at 03:04