改变歌曲的速度,而其播放,并有改变的效果,在歌曲中使用python

qhhrdooz  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(173)

我想要改变正在播放的音乐的克里思并将其反映到当前播放的歌曲中,因此如果我想要,我可以在歌曲播放时改变节奏。

from pydub import *
from pydub.playback import play

sound = AudioSegment.from_wav('mymusic.wav')

newsound = sound.speedup(playback_speed=2)
play(newsound)`

这段代码不允许我干扰音乐播放,它在音乐播放完毕后才执行此操作,我希望干扰当前正在播放的音乐,并在音乐沿着时更改克里思

vmpqdwk3

vmpqdwk31#

from pydub import AudioSegment
from pydub.playback import _play_with_sounddevice

sound = AudioSegment.from_wav('mymusic.wav')

# set the initial tempo change factor
tempo_change = 1.0

# define a callback function to process audio blocks in real-time
def tempo_callback(indata, frames, time, status):
    global tempo_change
    # stretch the audio to change the tempo without changing the pitch
    outdata = indata.copy().time_stretch(tempo_change)
    return outdata, pyaudio.paContinue

# play the audio with the callback function
_play_with_sounddevice(sound, tempo_callback=tempo_callback)

相关问题