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

调用lb.get_duration()触发TypeError:需0个参数却传入1个的问题排查

问题排查:get_duration()参数匹配错误

问题场景

训练集音频处理代码运行正常,但测试集执行时触发TypeError,核心代码与错误信息如下:

解构音频的核心函数

def decoding_stems(file_name):
  audio, sr = sp.read_stems(file_name, sample_rate=22050)
  
  swap_ad = np.swapaxes(audio, 1 , 2) # 交换轴以适配多数库的通道/数据格式要求
  duration = lb.get_duration(swap_ad[1])
  
  duration_length = (duration // 0.6) * 6 # 将时长取整为6的倍数,保证分段等长

  return stft_frame(file_name, int(duration_length), 6)

训练集调用代码(正常执行)

train_audio_data = []

for i in range(len(train_song_names)):
  
  file_path = path + '/train/' + train_song_names[i] + '.mp4'
  audio_seg = decoding_stems(file_path)
  
  train_audio_data.append(audio_seg)

测试集调用代码(触发错误)

test_audio_data = []

for i in range(len(test_song_names)):
  
  file_path = path + '/test/' + test_song_names[i] + '.mp4'
  audio_seg = decoding_stems(file_path)
  
  test_audio_data.append(audio_seg)

错误信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-3f8fadcdbe66> in <cell line: 6>()
      7 
      8   file_path = path + '/test/' + test_song_names[i] + '.mp4'
----> 9   audio_seg = decoding_stems(file_path)
     10 
     11 

<ipython-input-12-359828c0d17d> in decoding_stems(file_name)
     10 
     11   swap_ad = np.swapaxes(audio, 1 , 2) # swaps the first and second axis because most libraries execpt num channels in the second axis and the data in the first
---> 12   duration = lb.get_duration(swap_ad[1])
     13 
     14   duration_length = (duration // 0.6) * 6 # rounds the song duration to be divisilbe by 6 so that each sound segment is in equal length

TypeError: get_duration() takes 0 positional arguments but 1 was given

错误原因

  1. 函数签名不匹配:lb.get_duration()的定义本身不接收参数,但调用时传入了swap_ad[1]。训练集未报错大概率是因为训练阶段lb对象的get_duration方法被临时修改(比如运行过适配参数的补丁代码),或测试阶段lb的引用发生变化(如重新导入库、变量名冲突覆盖)。
  2. 隐性数据差异:测试集音频文件可能在sp.read_stems返回的结构上与训练集不同,导致swap_ad[1]的类型触发了方法的参数校验,但本质问题还是方法不支持传入参数。

解决方法

方法1:修正时长计算逻辑(推荐)

直接通过音频数据长度与采样率计算时长,避免依赖可能不稳定的lb.get_duration:

def decoding_stems(file_name):
  audio, sr = sp.read_stems(file_name, sample_rate=22050)
  
  swap_ad = np.swapaxes(audio, 1 , 2)
  # 直接通过样本数/采样率计算时长
  audio_stem = swap_ad[1]
  duration = audio_stem.shape[0] / sr  # 假设音频结构为(样本数, 通道数)
  
  duration_length = (duration // 0.6) * 6

  return stft_frame(file_name, int(duration_length), 6)

方法2:适配get_duration的正确调用方式

如果lb是librosa库实例,使用其标准API调用:

# 替换原duration计算行
audio_stem = swap_ad[1]
duration = lb.get_duration(y=audio_stem, sr=sr)  # 明确传入音频数据与采样率参数

方法3:校验lb引用一致性

检查测试集代码执行前,是否存在重新导入库、变量名覆盖等操作,确保lb与训练阶段指向同一对象。

内容的提问来源于stack exchange,提问作者Mrinal Kanti Mishra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 18:58:12