-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
129 lines (98 loc) · 3.5 KB
/
utils.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
import numpy as np
import random
# Default data augmentation
def padding(pad):
def f(sound):
return np.pad(sound, pad, 'constant')
return f
def random_crop(size):
def f(sound):
org_size = len(sound)
start = random.randint(0, org_size - size)
return sound[start: start + size]
return f
def normalize(factor):
def f(sound):
return sound / factor
return f
# For strong data augmentation
def random_scale(max_scale, interpolate='Linear'):
def f(sound):
scale = np.power(max_scale, random.uniform(-1, 1))
output_size = int(len(sound) * scale)
ref = np.arange(output_size) / scale
if interpolate == 'Linear':
ref1 = ref.astype(np.int32)
ref2 = np.minimum(ref1 + 1, len(sound) - 1)
r = ref - ref1
scaled_sound = sound[ref1] * (1 - r) + sound[ref2] * r
elif interpolate == 'Nearest':
scaled_sound = sound[ref.astype(np.int32)]
else:
raise Exception('Invalid interpolation mode {}'.format(interpolate))
return scaled_sound
return f
def random_gain(db):
def f(sound):
return sound * np.power(10, random.uniform(-db, db) / 20.0)
return f
# For testing phase
def multi_crop(input_length, n_crops):
def f(sound):
stride = (len(sound) - input_length) // (n_crops - 1)
sounds = [sound[stride * i: stride * i + input_length] for i in range(n_crops)]
return np.array(sounds)
return f
# For BC learning
def a_weight(fs, n_fft, min_db=-80.0):
freq = np.linspace(0, fs // 2, n_fft // 2 + 1)
freq_sq = np.power(freq, 2)
freq_sq[0] = 1.0
weight = 2.0 + 20.0 * (2 * np.log10(12194) + 2 * np.log10(freq_sq)
- np.log10(freq_sq + 12194 ** 2)
- np.log10(freq_sq + 20.6 ** 2)
- 0.5 * np.log10(freq_sq + 107.7 ** 2)
- 0.5 * np.log10(freq_sq + 737.9 ** 2))
weight = np.maximum(weight, min_db)
return weight
def compute_gain(sound, fs, min_db=-80.0, mode='A_weighting'):
if fs == 16000:
n_fft = 2048
elif fs == 44100:
n_fft = 4096
else:
raise Exception('Invalid fs {}'.format(fs))
stride = n_fft // 2
gain = []
#MOHAIMEN: no xrange anymore
for i in range(0, len(sound) - n_fft + 1, stride):
if mode == 'RMSE':
g = np.mean(sound[i: i + n_fft] ** 2)
elif mode == 'A_weighting':
spec = np.fft.rfft(np.hanning(n_fft + 1)[:-1] * sound[i: i + n_fft])
power_spec = np.abs(spec) ** 2
a_weighted_spec = power_spec * np.power(10, a_weight(fs, n_fft) / 10)
g = np.sum(a_weighted_spec)
else:
raise Exception('Invalid mode {}'.format(mode))
gain.append(g)
gain = np.array(gain)
gain = np.maximum(gain, np.power(10, min_db / 10))
gain_db = 10 * np.log10(gain)
return gain_db
def mix(sound1, sound2, r, fs):
gain1 = np.max(compute_gain(sound1, fs)) # Decibel
gain2 = np.max(compute_gain(sound2, fs))
t = 1.0 / (1 + np.power(10, (gain1 - gain2) / 20.) * (1 - r) / r)
sound = ((sound1 * t + sound2 * (1 - t)) / np.sqrt(t ** 2 + (1 - t) ** 2))
return sound
# Convert time representation
def to_hms(time):
h = int(time // 3600)
m = int((time - h * 3600) // 60)
s = int(time - h * 3600 - m * 60)
if h > 0:
line = '{}h{:02d}m'.format(h, m)
else:
line = '{}m{:02d}s'.format(m, s)
return line