##将一段音频拼接到另一段音频音量较高的地方
from posixpath import join
import numpy as np
import os
import librosa
import soundfile as sf
out_path = ''
s_parh = ''
n_path = ''
ss_path = [os.path.join(n_path,name) for name in os.listdir(n_path)]
filelist = os.listdir(s_parh)
for file in filelist:
file_path = os.path.join(s_parh,file)
y,sr = librosa.load(file_path,sr=16000,mono=True)
len_y = y.shape[0]
for i in range(4):
ss = np.random.choice(ss_path)
y1,sr = librosa.load(ss,sr=16000,mono=True)
len_y1 = y1.shape[0]
num = len_y1-len_y
s = np.argmax(y1[0:num])
pin = y1[s:s+len_y]=y
z = s + len_y -32000
if z<0:
z = 0
rival = np.random.randint(z,s)
out = y1[rival:rival+32000]
out_path1 = os.path.join(out_path,file.replace('.wav','_%d.wav' % i))
sf.write(out_path1,out,sr)