-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
158 lines (116 loc) · 4.52 KB
/
utils.py
File metadata and controls
158 lines (116 loc) · 4.52 KB
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
import re
import numpy as np
import librosa
def tryint(s):
try:
return int(s)
except ValueError:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [tryint(c) for c in re.split('([0-9]+)', s)]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
"""
This function converts the predictions made by the neural network into a readable format.
"""
def convert_seconds_to_hms(seconds):
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = round(seconds % 60, 3)
return f"{hours}h:{minutes}m:{seconds}s"
def preds_to_se(p, audio_clip_length=8.0):
start_speech = -100
start_music = -100
stop_speech = -100
stop_music = -100
audio_events = []
n_frames = p.shape[0]
if p[0, 0] == 1:
start_speech = 0
if p[0, 1] == 1:
start_music = 0
for i in range(n_frames - 1):
if p[i, 0] == 0 and p[i + 1, 0] == 1:
start_speech = i + 1
elif p[i, 0] == 1 and p[i + 1, 0] == 0:
stop_speech = i
start_time = frames_to_time(start_speech)
stop_time = frames_to_time(stop_speech)
audio_events.append((start_time, stop_time, "speech"))
start_speech = -100
stop_speech = -100
if p[i, 1] == 0 and p[i + 1, 1] == 1:
start_music = i + 1
elif p[i, 1] == 1 and p[i + 1, 1] == 0:
stop_music = i
start_time = frames_to_time(start_music)
stop_time = frames_to_time(stop_music)
audio_events.append((start_time, stop_time, "music"))
start_music = -100
stop_music = -100
if start_speech != -100:
start_time = frames_to_time(start_speech)
stop_time = audio_clip_length
audio_events.append((start_time, stop_time, "speech"))
start_speech = -100
stop_speech = -100
if start_music != -100:
start_time = frames_to_time(start_music)
stop_time = audio_clip_length
audio_events.append((start_time, stop_time, "music"))
start_music = -100
stop_music = -100
audio_events.sort(key=lambda x: x[0])
return audio_events
""" This function was adapted from https://github.com/qlemaire22/speech-music-detection """
def smooth_output(output, min_speech=1.3, min_music=3.4, max_silence_speech=0.4, max_silence_music=0.6):
# This function was adapted from https://github.com/qlemaire22/speech-music-detection
duration_frame = 220 / 22050
n_frame = output.shape[1]
start_music = -1000
start_speech = -1000
for i in range(n_frame):
if output[0, i] == 1:
if i - start_speech > 1:
if (i - start_speech) * duration_frame <= max_silence_speech:
output[0, start_speech:i] = 1
start_speech = i
if output[1, i] == 1:
if i - start_music > 1:
if (i - start_music) * duration_frame <= max_silence_music:
output[1, start_music:i] = 1
start_music = i
start_music = -1000
start_speech = -1000
for i in range(n_frame):
if i != n_frame - 1:
if output[0, i] == 0:
if i - start_speech > 1:
if (i - start_speech) * duration_frame <= min_speech:
output[0, start_speech:i] = 0
start_speech = i
if output[1, i] == 0:
if i - start_music > 1:
if (i - start_music) * duration_frame <= min_music:
output[1, start_music:i] = 0
start_music = i
else:
if i - start_speech > 1:
if (i - start_speech) * duration_frame <= min_speech:
output[0, start_speech:i + 1] = 0
if i - start_music > 1:
if (i - start_music) * duration_frame <= min_music:
output[1, start_music:i + 1] = 0
return output
def frames_to_time(f, sr=22050.0, hop_size=220):
return f * hop_size / sr
def get_log_melspectrogram(audio, sr=22050, hop_length=220, n_fft=1024, n_mels=100, fmin=64, fmax=8000):
"""Return the log-scaled Mel bands of an audio signal."""
bands = librosa.feature.melspectrogram(
y=audio, sr=sr, hop_length=hop_length, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax, dtype=np.float32)
return librosa.core.power_to_db(bands, amin=1e-7)