how to increase/decrease playback speed on .wav file?

[亡魂溺海] 提交于 2021-02-06 14:11:17

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/43408833/how-to-increase-decrease-playback-speed-on-wav-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!