-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulate_audio.py
330 lines (280 loc) · 9.59 KB
/
manipulate_audio.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import operator
import os
import random
import pydub.effects
import youtube_dl
from pydub import AudioSegment
from random_word import RandomWords
from youtube_dl.postprocessor import ffmpeg
from youtubesearchpython import *
import convert_wavs
global counter
def export_audio_to_dir(audio, path, name):
"""
Save as wav file with Addition ManipulatedConverted to the name
"""
if '_' in name:
index = name.rfind('_')
index = len(path) - len(name) + index
else:
index = path.rfind('.')
path = path[:index] + 'Manipulated' + path[index:]
audio.export(os.path.join(f"{path}"), format="wav")
if '_' in name:
index = name.rfind('_')
index = len(path) - len(name) + index
else:
index = path.rfind('.')
new_path = path[:index] + 'Converted' + path[index:]
convert_wavs.convert_audio(path, new_path, True)
print(f"Saved + converted {new_path}")
def get_third_audio_limits(length, third) -> (int, int):
"""
Returns limits of given third of audio file - in milliseconds
"""
bottom = int(((third - 1) * length) / 3)
top = int((third * length) / 3)
return bottom, top
def change_vol_help(audio, name, third):
"""
Changes volume of given third according to name parameter given - high/low/normal
"""
limits = get_third_audio_limits(len(audio), third)
if name == "normal":
return audio[limits[0]:limits[1]]
elif name == "high":
return audio[limits[0]:limits[1]] + 10
else: # low
return audio[limits[0]:limits[1]] - 10
def change_vol(audio):
"""
Changes volume of audio file - divides the audio into 3 parts and randomly selects:
1 third - increase volume
1 third - decrease volume
1 third - keep volume
"""
increased_third = random.randint(1, 3)
decrease_third = increased_third
while decrease_third == increased_third:
decrease_third = random.randint(1, 3)
normal_third = 0
for i in range(1, 3):
if i != decrease_third and i != increased_third:
normal_third = i
break
sorted_thirds = {"normal": normal_third, "high": increased_third, "low": decrease_third}
sorted_thirds = sorted(sorted_thirds.items(), key=operator.itemgetter(1))
new_audio = AudioSegment.empty()
for name, third in sorted_thirds:
new_audio = new_audio + change_vol_help(audio, name, third)
return new_audio
def change_speed(audio):
"""
Speed up audio by 1.5
"""
return pydub.effects.speedup(audio, playback_speed=1.5)
def decide_manipulate():
"""
AKA flip a coin
"""
return random.randint(0, 1)
global changed_counter
changed_counter = 0
global noise_list
noise_list = []
# for file in os.scandir('./background_noises'):
# noise_list.append(file.name)
def add_background_noise(audio):
"""
Add random background noise to given audio
"""
global noise_list
decide_noise = random.randint(0, len(noise_list) - 1)
noise_file = "./background_noises/" + noise_list[decide_noise]
noise = AudioSegment.from_wav(noise_file)
noise = noise[:len(audio)] - 18
return audio.overlay(noise)
def manipulate_audio_file(path, name):
"""
This function takes .wav files and does the following:
1. Changes the volume of each 1/3 length of the audio file
(Randomly chooses 1/3 increased volume, 1/3 normal volume, 1/3 decreased volume)
2. Add background noise
3. Change speed of audio (slow down/speed up)
AKA the G.O.A.T
"""
audio = AudioSegment.from_wav(path)
decide_vol = False
decide_speed = False
decide_background = True
if decide_background:
audio = add_background_noise(audio)
print(f"Added background to {path}")
if decide_vol:
audio = change_vol(audio)
print(f"Changed vol for {path}")
if decide_speed:
audio = change_speed(audio)
print(f"Changed speed for {path}")
if decide_vol or decide_speed or decide_background:
global changed_counter
changed_counter = changed_counter + 1
export_audio_to_dir(audio, path, name)
global unique
unique = 0
def save_with_new_emotion(audio, path, is_dot, emotion):
"""
Helper function to save from original dataset, to a dataset with 3 emotions - happy,sad,neutral
"""
global unique
if is_dot:
index = path.rfind('.') - 2
new_path = f"{path[:index]}{unique}{emotion}{path[index + 1:]}"
unique = unique + 1
else:
index = path.rfind('_')
new_path = f"{path[:index]}{unique}_{emotion}.wav"
unique = unique + 1
convert_wavs.convert_audio(path, new_path, True)
print(f"saved {path} as {new_path}")
def get_emotion_from_name(name):
if '_' in name:
start_index = name.rfind('_') + 1
end_index = name.rfind('.')
emotion = name[start_index:end_index]
else:
index = name.rfind('.')
emotion = name[index - 2:index - 1]
positive_emotions = ["F", "happy"]
negative_emotions = ["T", "sad"]
neutral_emotions = ["neutral", "N"]
if emotion in positive_emotions:
return "happy"
elif emotion in negative_emotions:
return "sad"
elif emotion in neutral_emotions:
return "neutral"
else:
pass
def unite_data_to_three_emotions(path, name):
"""
Helper function to change from original dataset, to a dataset with 3 emotions - happy,sad,neutral
"""
categories = {
"W": "angry",
"L": "boredom",
"E": "disgust",
"A": "fear",
"F": "happy",
"T": "sad",
"N": "neutral"
}
audio = AudioSegment.from_wav(path)
positive_emotions = ["ps"]
negative_emotions = ["angry", "disgust", "fear", "W", "E", "A"]
neutral_emotions = ["boredom", "calm", "L"]
is_dot = False
if '_' in name:
start_index = name.rfind('_') + 1
end_index = name.rfind('.')
emotion = name[start_index:end_index]
else:
index = path.rfind('.')
emotion = path[index - 2:index - 1]
is_dot = True
if emotion in positive_emotions:
if is_dot:
emotion = "F"
else:
emotion = "happy"
save_with_new_emotion(audio, path, is_dot, emotion)
elif emotion in negative_emotions:
if is_dot:
emotion = "T"
else:
emotion = "sad"
save_with_new_emotion(audio, path, is_dot, emotion)
elif emotion in neutral_emotions:
if is_dot:
emotion = "N"
else:
emotion = "neutral"
save_with_new_emotion(audio, path, is_dot, emotion)
else:
pass
global emotions
emotions = {"happy": 0, "sad": 0, "neutral": 0}
def search_in_folder(path):
"""
Search in path folder for all files (not folders)
"""
global changed_counter
global emotions
for file in os.scandir(path):
if not file.is_file():
search_in_folder(file)
else:
curr_emotion = get_emotion_from_name(file.name)
curr_count = emotions.get(curr_emotion)
emotions.update({curr_emotion: (curr_count + 1)})
print(emotions)
def youtube2wav(link, word, counter):
"""
Receive youtube link and download wav file
"""
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'output.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
out = AudioSegment.from_wav('.\output.wav')
mid = int(len(out) / 2)
out = out[mid:mid + 1000]
out.export(f".\\background_noises\\output.wav")
if os.path.exists(".\\output.wav"):
os.remove(".\\output.wav")
else:
print("The file does not exist")
convert_wavs.convert_audio(f".\\background_noises\\output.wav", f".\\background_noises\\{word}{counter}.wav", True)
def Auto_background_noises_generator():
"""
Generates 1000 background noises:
1. Generates random word:
2. For each random word, search youtube and return 20 video links
3. For each link, send to youtube2wav function to try to download a wav file from the video
"""
NEED_TO_DOWNLOAD = 1000
RW = RandomWords()
error_counter = 0
total_downloaded = 0
word_counter = 0
while total_downloaded <= NEED_TO_DOWNLOAD:
word = RW.get_random_word()
word_counter = word_counter + 1
video_search = VideosSearch(query=word, limit=20)
vidNum = 1
for video in video_search.result()["result"]:
print(f"Word: {word} || wordNum: {word_counter} || vidNum: {vidNum}")
print(video["title"])
print(video["link"])
try:
youtube2wav(video["link"], word, vidNum)
total_downloaded = total_downloaded + 1
print(
f"Downloaded successfully - DownloadNum: {total_downloaded} || ErrorNum: {error_counter} || Word: {word} || wordNum: {word_counter} || vidNum: {vidNum}")
except:
error_counter = error_counter + 1
print(
f"Error - DownloadNum: {total_downloaded} || ErrorNum: {error_counter} || Word: {word} || wordNum: {word_counter} || vidNum: {vidNum}")
vidNum = vidNum + 1
print("*******************************************************************")
print(f"Downloaded: {total_downloaded} || Error count: {error_counter}")
# Auto_background_noises_generator()
search_in_folder('./data/training')
search_in_folder('./data/validation')
# print(f"Manipulated: {changed_counter} files")