-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurbanSound8k_dataset_generator.py
281 lines (182 loc) · 6.07 KB
/
urbanSound8k_dataset_generator.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# coding: utf-8
# ***Import Libraries And Noise Types Defined***
# In[63]:
import numpy as np
import os
import random
from pydub import AudioSegment
noise_class_dictionary = {
0 : "air_conditioner",
1 : "car_horn",
2 : "children_playing",
3 : "dog_bark",
4 : "drilling",
5 : "engine_idling",
6 : "gun_shot",
7 : "jackhammer",
8 : "siren",
9 : "street_music"
}
noise_class_dictionary
# In[64]:
dataset="Datasets/"
urbanData="UrbanSound8K/"
fold_names = []
for i in range(1,11):
fold_names.append("fold"+str(i)+"/")
fold_names
# In[65]:
Urban8Kdir =urbanData
target_folder = dataset+"clean_trainset_28spk_wav"
# ***Get Files With Different Noise Type***
# In[66]:
def diffNoiseType(files,noise_type):
result = []
for i in files:
if i.endswith(".wav"):
fname = i.split("-")
if fname[1] != str(noise_type):
result.append(i)
return result
# ***Files With Same Noise Type***
# In[67]:
def oneNoiseType(files, noise_type):
result = []
for i in files:
if i.endswith(".wav"):
fname = i.split("-")
if fname[1] == str(noise_type):
result.append(i)
return result
# ***Generate Noisy File From Clean File***
# In[68]:
# def genNoise(filename, num_per_fold, dest):
# clean_audio_path = target_folder+"/"+filename
# audio_1 = AudioSegment.from_file(clean_audio_path)
# counter = 0
# for fold in fold_names:
# dirname = Urban8Kdir + fold
# # print(dirname)
# dirlist = os.listdir(dirname)
# total_noise_len = len(dirlist)
# samples = np.random.choice(total_noise_len, num_per_fold, replace=False)
# print(samples)
# for s in samples:
# noisefile = dirlist[s]
# try:
# audio_2 = AudioSegment.from_file(dirname+"/"+noisefile)
# combined = audio_1.overlay(audio_2, times=5)
# target_dest = dest+"/"+filename[:len(filename)-4]+"_noise_"+str(counter)+".wav"
# combined.export(target_dest, format="wav")
# audio = Audio(target_dest)
# display(audio)
# counter +=1
# except:
# print("Error")
# In[69]:
# genNoise("p226_001.wav",1,"Datasets")
# In[70]:
import torchaudio
from IPython.display import Audio
# In[71]:
def genNoisyFile(filename,dest, noise_type,isdiff):
succ = False
true_path = target_folder+"/"+filename
count=0
while not succ:
try:
audio_1 = AudioSegment.from_file(true_path)
except:
print("Some kind of audio decoding error occurred for base file... skipping")
break
try:
fold = np.random.choice(fold_names, 1, replace=False)
fold = fold[0]
dirname = Urban8Kdir + fold
dirlist = os.listdir(dirname)
noisefile=""
if isdiff:
possible_noises=diffNoiseType(dirlist,noise_type)
total_noise = len(possible_noises)
samples = np.random.choice(total_noise, 1, replace=False)
s = samples[0]
noisefile = possible_noises[s]
else:
possible_noises=oneNoiseType(dirlist,noise_type)
total_noise = len(possible_noises)
samples = np.random.choice(total_noise, 1, replace=False)
s = samples[0]
noisefile = possible_noises[s]
audio_2 = AudioSegment.from_file(dirname+"/"+noisefile)
combined = audio_1.overlay(audio_2, times=5)
target_dest = dest+"/"+filename
combined.export(target_dest, format="wav")
# audio =Audio(target_dest)
# display(audio)
succ = True
except:
if count>5:
succ=True
count+=1
pass
# print("Some kind of audio decoding error occurred for the noise file..retrying")
# break;
# In[72]:
noise_type = int(input("Enter the noise class dataset to generate :\t"))
# In[73]:
inp_folder = dataset+"US_Class2"+str(noise_type)+"_Train_Input"
op_folder = dataset+"US_Class2"+str(noise_type)+"_Train_Output"
# In[74]:
print("Generating Training Data..")
print("Making train input folder")
if not os.path.exists(inp_folder):
os.makedirs(inp_folder)
print("Making train output folder")
if not os.path.exists(op_folder):
os.makedirs(op_folder)
# In[75]:
from tqdm import tqdm
print(os.listdir(target_folder).__len__())
# In[76]:
def generate_Train_dataset():
counter = 0
#noise_type = 1
for file in tqdm(os.listdir(target_folder)):
filename = os.fsdecode(file)
if filename.endswith(".wav"):
snr = random.randint(0,10)
# noise_type=random.randint(0,9)
genNoisyFile(filename,inp_folder,noise_type,0)
snr = random.randint(0,10)
genNoisyFile(filename,op_folder,noise_type,1)
counter +=1
if counter>=1000:
break
# In[77]:
generate_Train_dataset()
# In[78]:
Urban8Kdir = urbanData
target_folder = dataset+"clean_testset_wav"
inp_folder = dataset+"US_Class2"+str(noise_type)+"_Test_Input"
print(os.listdir(target_folder).__len__())
# In[79]:
print("Generating Testing Data..")
print("Making test input folder")
if not os.path.exists(inp_folder):
os.makedirs(inp_folder)
# In[80]:
def generate_Test_dataset():
counter = 0
for file in tqdm(os.listdir(target_folder)):
filename = os.fsdecode(file)
if filename.endswith(".wav"):
snr = random.randint(0,10)
genNoisyFile(filename,inp_folder,noise_type,0)
counter +=1
if counter>=100:
break
# In[81]:
generate_Test_dataset()
# In[ ]:
# In[ ]: