time_strech()中的参数数量问题

0 投票
1 回答
31 浏览
提问于 2025-04-14 18:16

当我这样定义我的 stretch() 函数时:

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

它出现了这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[56], line 6
      4 y = []
      5 for i in range(len(audio_df)):
----> 6     feature=get_features(audio_df['Arrays'].iloc[i]);
      7     for j in feature:
      8         x.append(j) 

Cell In[55], line 14, in get_features(data)
     11 result.append(res2)
     13 # with stretching and pitching
---> 14 new_data = stretch(data)
     15 data_stretch_pitch = pitch(new_data, sr)
     16 res3 = extract_features(data_stretch_pitch)

Cell In[53], line 7, in stretch(data, rate)
      6 def stretch(data, rate=0.8):
----> 7     return librosa.effects.time_stretch(data, rate)

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

但是当我去掉 'rate' 这个参数时,它又出现了另一个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[58], line 6
      4 y = []
      5 for i in range(len(audio_df)):
----> 6     feature=get_features(audio_df['Arrays'].iloc[i]);
      7     for j in feature:
      8         x.append(j) 

Cell In[55], line 14, in get_features(data)
     11 result.append(res2)
     13 # with stretching and pitching
---> 14 new_data = stretch(data)
     15 data_stretch_pitch = pitch(new_data, sr)
     16 res3 = extract_features(data_stretch_pitch)

Cell In[57], line 7, in stretch(data, rate)
      6 def stretch(data, rate=0.8):
----> 7     return librosa.effects.time_stretch(data)

TypeError: time_stretch() missing 1 required keyword-only argument: 'rate'

我去掉了 'rate' 参数,想用默认值来试试(虽然这对数据没有影响),但它还是报错了。我可能理解错了,但我觉得当我给两个参数时,它说只需要一个;而当我给一个参数时,它又说我缺少一个参数,我该怎么办呢?

1 个回答

0

librosa库里的time_stretch()函数需要一个叫rate的参数,而且这个参数只能以关键字的方式传入。也就是说,当你调用这个函数的时候,必须明确地把rate这个参数写成关键字形式:

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

撰写回答