get_duration() 需要 0 个位置参数,但给出了 1 个

-1 投票
1 回答
53 浏览
提问于 2025-04-14 15:54
def decoding_stems(file_name):
  audio, sr = sp.read_stems(file_name, sample_rate=22050)
  
  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
  duration = lb.get_duration(swap_ad[1])
  
  duration_length = (duration // 0.6) * 6 # rounds the song duration to be divisilbe by 6 so that each sound segment is in equal length

  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 个回答

0

使用这个:

import numpy as np
import librosa as lb
import soundfile as sp

    
def decoding_stems(file_name):
    audio, sr = sp.read_stems(file_name, sample_rate=22050)
      
    swap_ad = np.swapaxes(audio, 1, 2)  # swaps the axis for compatibility
    # Ensure you select a single channel for duration calculation
    duration = lb.get_duration(y=swap_ad[1][:, 0], sr=sr)
      
    # Adjust the duration for segmentation
    duration_length = (duration // 0.6) * 6
    
    return stft_frame(file_name, int(duration_length), 6)

撰写回答