当前位置:   article > 正文

音频提取特征_wav2vec2.0特征提取

wav2vec2.0特征提取

目录

音频提取特征

音频切割


依赖项:

pip install librosa

pip install transformers

音频提取特征

  1. import librosa
  2. import numpy as np
  3. import torch
  4. from transformers import Wav2Vec2Processor
  5. processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
  6. wav_path = r'E:\迅雷下载\data\finedance\music_wav\001.wav'
  7. speech_array, sampling_rate = librosa.load(wav_path, sr=16000)
  8. input_values = np.squeeze(processor(speech_array, sampling_rate=16000).input_values)
  9. print(input_values.shape)

音频切割

  1. import librosa as lr
  2. import numpy as np
  3. import soundfile as sf
  4. from tqdm import tqdm
  5. def slice_audio(audio_file, stride, length, out_dir):
  6. # stride, length in seconds
  7. audio, sr = lr.load(audio_file, sr=None)
  8. file_name = os.path.splitext(os.path.basename(audio_file))[0]
  9. start_idx = 0
  10. idx = 0
  11. window = int(length * sr)
  12. stride_step = int(stride * sr)
  13. while start_idx <= len(audio) - window:
  14. audio_slice = audio[start_idx : start_idx + window]
  15. sf.write(f"{out_dir}/{file_name}_slice{idx}.wav", audio_slice, sr)
  16. start_idx += stride_step
  17. idx += 1
  18. return idx
  19. def slice_audio_folder(wav_dir, stride=0.5, length=5):
  20. wavs = sorted(glob.glob(f"{wav_dir}/*.wav"))
  21. wav_out = wav_dir + "_sliced"
  22. os.makedirs(wav_out, exist_ok=True)
  23. for wav in tqdm(wavs):
  24. audio_slices = slice_audio(wav, stride, length, wav_out)
  25. if __name__ == '__main__':
  26. slice_audio_folder(r"E:\Projects\FineDance-main\data\code")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/986139
推荐阅读
相关标签
  

闽ICP备14008679号