##将一段音频分割成几段,将这几段打乱顺序,然后重新组成音频。
import librosa
import random
import numpy as np
import os
import torch
import soundfile as sf
filelist = os.listdir(file_path)
for i in range(len(filelist)):
path = file_path +'/'+filelist[i]
out = out_path+'/'+ filelist[i].split('.')[0]+'s'+'.wav'
print(path)
y, sr = librosa.load(path, sr=16000, mono=True)
duration = len(y) / sr
window = int(0.1 * sr)
L = list(range(window, len(y), window))
print(L)
result = []
for i, cur in enumerate(L):
input =y[cur-window:cur]
if np.random.uniform(0,1) < 0.5:
input = np.zeros_like(input)
result.append(input)
random.shuffle(result)
random.shuffle(result)
results = np.concatenate(result)
sf.write(out , results, sr)