-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasli.py
More file actions
229 lines (212 loc) · 7.94 KB
/
Copy pathasli.py
File metadata and controls
229 lines (212 loc) · 7.94 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
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
import argparse
import argcomplete
import sys
import os
import math
from pathlib import Path
from pydub import AudioSegment
from pydub.effects import low_pass_filter, high_pass_filter
def main():
parser = argparse.ArgumentParser(description="audio slicer tool")
parser.add_argument(
"-t",
"--threshold",
type=float,
default=3.0,
help="set threshold for transient detection [def=3.0]",
)
parser.add_argument(
"-i",
"--keep-intro",
action="store_true",
help="treat beginning of file as transient",
)
parser.add_argument(
"-o", "--output", type=str, help="write audio slices to directory (implies -d)"
)
parser.add_argument(
"-d",
"--to-dir",
action="store_true",
help="write audio slices to directory named after file",
)
parser.add_argument(
"-f", "--format", type=str, default="wav", help="format of sliced audio clips"
)
parser.add_argument(
"-e",
"--every",
type=float,
help="slice every EVERY seconds instead of at transients",
)
parser.add_argument(
"-m",
"--max-slices",
type=int,
help="maximum number of slices to write (will skip trailing slices)",
)
parser.add_argument(
"-c",
"--cooldown",
type=float,
default=0.01,
help="minimum seconds between transients [def=0.05]",
)
parser.add_argument(
"--fadeo", type=float, help="add fade out at last FADEO seconds of slice"
)
parser.add_argument(
"--fadei", type=float, help="add fade in at first FADEO seconds of slice"
)
parser.add_argument(
"--fadeout-all",
action="store_true",
help="fade out audio clips start to finish (or half with fade in)",
)
parser.add_argument(
"--fadein-all",
action="store_true",
help="fade in audio clips start to finish (or half with fade out)",
)
parser.add_argument(
"--db",
type=float,
default=20,
help="minimum NEGATIVE db value to treat as transient [def=20]",
)
parser.add_argument(
"--hpf", type=int, help="find transients while applying highpass filter at freq"
)
parser.add_argument(
"--lpf", type=int, help="find transients while applying lowpass filter at freq"
)
parser.add_argument(
"--bpf", type=int, help="find transients while applying bandpass filter at freq"
)
parser.add_argument(
"--trim-end", type=float, help="number of seconds to trim off end of clips",
)
parser.add_argument(
"--trim-start", type=float, help="number of seconds to trim off beginning of clips",
)
parser.add_argument("files", nargs="+", help="audio files to slice")
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.max_slices and args.max_slices <= 0:
raise ValueError("'-m/--max-slices' must be a positive value")
try:
files = []
for a in args.files:
p = Path(a)
if not p.exists():
raise Exception(f"file '{a}' does not exist")
files.append((a, a[0 : -len(p.suffix)], p.suffix.strip(".")))
for f in files:
original_audio = AudioSegment.from_file(f[0], f[2])
audio = original_audio
if args.lpf is not None:
if args.lpf < 0 or args.lpf > 20000:
raise Exception("--lpf argument must be between 0 and 20000")
audio = low_pass_filter(audio, cutoff=args.lpf)
if args.hpf is not None:
if args.hpf < 0 or args.hpf > 20000:
raise Exception("--hpf argument must be between 0 and 20000")
audio = high_pass_filter(audio, cutoff=args.hpf)
if args.bpf is not None:
if args.bpf < 0 or args.bpf > 20000:
raise Exception("--hpf argument must be between 0 and 20000")
audio = low_pass_filter(audio, cutoff=args.bpf)
audio = high_pass_filter(audio, cutoff=args.bpf)
transients = []
if args.keep_intro:
transients.append(0)
if args.every is not None:
for i in range(0, len(audio)):
if i % int(args.every * 1000) == 0:
transients.append(i)
print(
f"file: {f[0]:<30}divisions: {len(transients)}",
end="\r",
file=sys.stderr,
)
else:
a = 0.95
maxDeriv = 0.01
cooldown = int(args.cooldown * 1000)
ref = max(frame.rms for frame in audio)
min_db = -args.db
baseline = audio[0].rms
cool = 0
for i in range(1, len(audio)):
baseline = a * baseline + (1 - a) * audio[i].rms
ratio = audio[i].rms / (baseline + 1e-9)
deriv = audio[i].rms - audio[i - 1].rms
rms_db = 20 * math.log10((audio[i].rms / ref) + 1e-12)
if cool == 0:
if (
ratio > args.threshold
and deriv > maxDeriv
and rms_db >= min_db
):
transients.append(i)
cool = cooldown
print(
f"file: {f[0]:<30}transients: {len(transients)}",
end="\r",
file=sys.stderr,
)
else:
cool -= 1
print("")
if len(audio) - 1 not in transients:
transients.append(len(audio) - 1)
directory = f[1] if (args.to_dir and args.output is None) else args.output
if directory and not os.path.exists(directory):
os.mkdir(directory)
audio = original_audio
length = (
min(args.max_slices + 1, len(transients))
if args.max_slices
else len(transients)
)
file_num_padding = int(math.log10(length)) + 1
for i in range(1, length):
seg = audio[transients[i - 1] : transients[i]]
if args.trim_end:
seconds = int(args.trim_end * 1000.0)
if seconds > len(seg) and len(seg) > 2:
seg = seg[0:2]
else:
seg = seg[:-seconds]
if args.trim_start:
seconds = int(args.trim_start * 1000.0)
if seconds > len(seg) and len(seg) > 2:
seg = seg[-2:]
else:
seg = seg[seconds:]
if args.fadeo:
seg = seg.fade_out(int(args.fadeo * 1000.0))
if args.fadei:
seg = seg.fade_in(int(args.fadei * 1000.0))
if args.fadeout_all:
if args.fadein_all:
seg = seg.fade_out(len(seg) // 2).fade_in(len(seg) // 2)
else:
seg = seg.fade_out(len(seg))
elif args.fadein_all:
seg = seg.fade_in(len(seg))
seg.export(
"{}{}_{}.{}".format(
(directory + "/" if directory else ""),
f[1],
f"{i:0{file_num_padding}}",
args.format,
),
args.format,
)
except Exception as e:
print(f"failure: {e}", file=sys.stderr)
parser.print_help(sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()