-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
145 lines (113 loc) · 3.89 KB
/
util.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
import librosa
import numpy as np
import matplotlib.pyplot as plt
import IPython.display as ipd
import pdb
# load audio signal
def loadAudio(path, sr=22050, fix_length=False, length=10):
audio, sr = librosa.load(path, sr=sr)
if fix_length:
audio = librosa.util.fix_length(
audio, int(sr * length)
) # 10.24s to get a mel spec . x 64
return audio, sr
# play audio signal
def playAudio(audio, sr=22050):
return ipd.Audio(audio, rate=sr)
# Plot audio signal
def plotAudio(audio, figsize=(6, 4), ylim=True):
fig = plt.figure(figsize=figsize)
plt.title("Raw wave ")
plt.ylabel("Amplitude")
plt.plot(np.linspace(0, 1, len(audio)), audio)
if ylim:
plt.ylim((-1, 1))
plt.show()
def plotAudioSub(audio1, audio2, figsize=(4, 4), ylim=True, horizontal=True):
if horizontal:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figsize)
else:
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=figsize)
ax1.plot(np.linspace(0, 1, len(audio1)), audio1)
ax1.set_title("Original")
ax1.set(xlabel="Time", ylabel="Amplitude")
ax2.plot(np.linspace(0, 1, len(audio2)), audio2)
ax2.set_title("Augmented")
ax2.set(xlabel="Time", ylabel="Amplitude")
if ylim:
ax1.set_ylim((-1, 1))
ax2.set_ylim((-1, 1))
fig.tight_layout()
# fig.show()
# Adding white noise
def addNoise(data, noise_factor=0.005):
noise = np.random.randn(len(data))
augmented_data = data + noise_factor * noise
# Cast back to same data type
augmented_data = augmented_data.astype(type(data[0]))
return augmented_data
# shift directions right, left
def addShift(data, shift, shift_direction):
if shift_direction == "right":
shift = -shift
augmented_data = np.roll(data, shift)
# Set to silence for heading/ tailing
if shift > 0:
augmented_data[:shift] = 0
else:
augmented_data[shift:] = 0
return augmented_data
# change pitch in fractional half-steps
def changePitch(audio, sr, pitch_factor):
return librosa.effects.pitch_shift(audio, sr, n_steps=pitch_factor)
# change pitch in fractional half-steps
def changeSpeed(audio, speed_factor, keep_dim=True):
# pdb.set_trace()
output = np.zeros_like(audio)
audio_stretched = librosa.effects.time_stretch(audio, speed_factor)
if keep_dim:
if speed_factor > 1:
idx = np.random.randint(0, len(audio) - len(audio_stretched))
output[idx : idx + len(audio_stretched)] = audio_stretched
else:
idx = np.random.randint(0, len(audio_stretched) - len(audio))
output = audio_stretched[idx : idx + len(output)]
else:
output = audio_stretched
return output
def augmentAudio(
audio, sr, noise_factor, shift, shift_direction, pitch_factor, speed_factor
):
# white noise
audio = addNoise(audio, noise_factor=noise_factor)
# time shift
audio = addShift(audio, shift, shift_direction)
# change pitch
audio = changePitch(audio, sr, pitch_factor)
# change speed
audio = changeSpeed(audio, speed_factor, keep_dim=True)
return audio
# create and apply envelope (swell, fade in seconds)
def createEnvelope(len_signal, sr, swell, fade):
env = np.ones(len_signal)
t = np.linspace(0, int(len_signal / sr), len_signal)
# pdb.set_trace()
# swell
t_swell = np.linspace(0, swell, int(swell * sr))
swell_func = np.power(2, t_swell)
# print(len(swell_func))
env_swell = np.clip(
(swell_func - np.min(swell_func)) / (np.max(swell_func) - np.min(swell_func)),
0,
1,
)
env[: len(t_swell)] = env_swell
# fade
t_fade = np.linspace(0, fade, int(fade * sr))
fade_func = np.power(2, -t_fade)
env_fade = np.clip(
(fade_func - np.min(fade_func)) / (np.max(fade_func) - np.min(fade_func)), 0, 1
)
env[-len(t_fade) :] = env_fade
# apply envelope
return env