-
Notifications
You must be signed in to change notification settings - Fork 35
/
augment_addnoise.py
70 lines (66 loc) · 3.37 KB
/
augment_addnoise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
AAA lllllll lllllll iiii
A:::A l:::::l l:::::l i::::i
A:::::A l:::::l l:::::l iiii
A:::::::A l:::::l l:::::l
A:::::::::A l::::l l::::l iiiiiii eeeeeeeeeeee
A:::::A:::::A l::::l l::::l i:::::i ee::::::::::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::eeeee:::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::e e:::::e
A:::::A A:::::A l::::l l::::l i::::i e:::::::eeeee::::::e
A:::::AAAAAAAAA:::::A l::::l l::::l i::::i e:::::::::::::::::e
A:::::::::::::::::::::A l::::l l::::l i::::i e::::::eeeeeeeeeee
A:::::AAAAAAAAAAAAA:::::A l::::l l::::l i::::i e:::::::e
A:::::A A:::::A l::::::ll::::::li::::::ie::::::::e
A:::::A A:::::A l::::::ll::::::li::::::i e::::::::eeeeeeee
A:::::A A:::::A l::::::ll::::::li::::::i ee:::::::::::::e
AAAAAAA AAAAAAAlllllllllllllllliiiiiiii eeeeeeeeeeeeee
/ _ \ | | | | (_)
/ /_\ \_ _ __ _ _ __ ___ ___ _ __ | |_ __ _| |_ _ ___ _ __
| _ | | | |/ _` | '_ ` _ \ / _ \ '_ \| __/ _` | __| |/ _ \| '_ \
| | | | |_| | (_| | | | | | | __/ | | | || (_| | |_| | (_) | | | |
\_| |_/\__,_|\__, |_| |_| |_|\___|_| |_|\__\__,_|\__|_|\___/|_| |_|
__/ |
|___/
___ ______ _____ ___ _ _
/ _ \ | ___ \_ _| / _ \ | (_)
/ /_\ \| |_/ / | |(_) / /_\ \_ _ __| |_ ___
| _ || __/ | | | _ | | | |/ _` | |/ _ \
| | | || | _| |__ | | | | |_| | (_| | | (_) |
\_| |_/\_| \___(_) \_| |_/\__,_|\__,_|_|\___/
Add noise to an audio file.
'''
import os, uuid, random, math
from pydub import AudioSegment
def augment_addnoise(filename,curdir, noisedir):
if filename[-4:]=='.wav':
audioseg=AudioSegment.from_wav(filename)
elif filename[-4:]=='.mp3':
audioseg=AudioSegment.from_mp3(filename)
hostdir=os.getcwd()
os.chdir(curdir)
os.chdir(noisedir)
listdir=os.listdir()
if 'noise.wav' in listdir:
os.remove('noise.wav')
mp3files=list()
for i in range(len(listdir)):
if listdir[i][-4:]=='.mp3':
mp3files.append(listdir[i])
noise=random.choice(mp3files)
# add noise to the regular file
noise_seg = AudioSegment.from_mp3(noise)
# find number of noise segments needed
cuts=math.floor(len(audioseg)/len(noise_seg))
noise_seg_2=noise_seg * cuts
noise_seg_3=noise_seg[:(len(audioseg)-len(noise_seg_2))]
noise_seg_4=noise_seg_2 + noise_seg_3
os.chdir(hostdir)
print(len(noise_seg_4))
print(len(audioseg))
noise_seg_4.export("noise.wav", format="wav")
# now combine audio file and noise file
newfile=filename[0:-4]+'_noise.wav'
os.system('ffmpeg -i %s -i %s -filter_complex "[0:a][1:a]join=inputs=2:channel_layout=stereo[a]" -map "[a]" %s'%(filename, 'noise.wav',newfile))
os.remove('noise.wav')
return [filename, newfile]