You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用librosa.time_stretch()报错:需1个位置参数却传入2个

解决TypeError: time_stretch() takes 1 positional argument but 2 were given

问题根源

这个错误是因为Librosa版本更新后,librosa.effects.time_stretch的调用规则发生了变化。在Librosa 0.9.0及以上版本中,该函数仅接受音频数据作为位置参数,rate参数必须以关键字参数的形式传入,不能直接作为位置参数传递。

修复代码

修改你的stretch函数,显式指定rate的参数名即可:

def stretch(data, rate=0.8):
    return librosa.effects.time_stretch(y=data, rate=rate)

x = stretch(data)
plt.figure(figsize=(14,4))
librosa.display.waveplot(y=x, sr=sample_rate)
Audio(x, rate=sample_rate)

额外检查

你可以通过以下命令确认当前Librosa版本:

pip show librosa

如果版本低于0.9.0却出现该错误,建议更新Librosa到稳定版,或者排查是否存在其他代码冲突。

内容的提问来源于stack exchange,提问作者Navien Bala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 00:42:05