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

Librosa调用mfcc()触发TypeError的问题求解

TypeError排查:librosa提取MFCC时参数错误的解决办法

问题背景

一段处理GTZAN音乐数据集、提取MFCC并保存为JSON的Python代码,此前运行正常,今日执行时触发TypeError,错误信息如下:

TypeError: mfcc() takes 0 positional arguments but 2 positional arguments (and 1 keyword-only argument) were given

核心出错代码片段:

mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)

完整错误栈:

TypeError                                 Traceback (most recent call last)
<ipython-input-10-4a9371926618> in <module>
      1 # Runs Data Processing
----> 2 save_mfcc(DATASET_PATH, JSON_PATH, num_segments=6)

<ipython-input-9-8ba1c6e78747> in save_mfcc(dataset_path, json_path, num_mfcc, n_fft, hop_length, num_segments)
     56 
     57                 # extract mfcc
---> 58                 mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)
     59                 mfcc = mfcc.T
     60                 # store only mfcc feature with expected number of vectors

TypeError: mfcc() takes 0 positional arguments but 2 positional arguments (and 1 keyword-only argument) were given

错误原因

问题源于librosa版本升级后API参数规则变更:

  • 旧版本(如0.9.x及更早)允许直接通过位置参数传入音频信号(y)和采样率(sr)
  • 新版本(0.10.x及之后)要求必须通过关键字参数显式指定y和sr,不再支持位置参数传递这两个核心参数

解决办法

修改mfcc调用代码,为前两个参数添加关键字标识:

原错误代码:

mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)

修改后代码:

mfcc = librosa.feature.mfcc(y=signal[start:finish], sr=sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)

仅需为音频信号和采样率参数加上y=和sr=的关键字标记,即可适配新版本librosa的API要求,代码可恢复正常运行。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 19:33:22