-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_segmenter.py
executable file
·104 lines (86 loc) · 3.47 KB
/
run_segmenter.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
#!/usr/bin/env python
'''Runs the segmenter across an entire folder containing wav files.
'''
__author__ = "Oriol Nieto"
__copyright__ = "Copyright 2014, Music and Audio Research Lab (MARL)"
__license__ = "GPL"
__version__ = "1.0"
__email__ = "[email protected]"
import glob
import os
import argparse
import time
import logging
from joblib import Parallel, delayed
import segmenter as S
import utils
def process_track(audio_file, out_path, bounds_type, labels_type, seed):
"""Processes one track, for paralelization purposes."""
logging.info("Segmenting %s" % audio_file)
out_file = os.path.join(
out_path, os.path.basename(audio_file).replace(".wav", ".lab"))
S.process(audio_file, out_file, bounds_type=bounds_type,
labels_type=labels_type, seed=seed)
def process(in_path, out_path, bounds_type="cnmf", labels_type="cnmf",
n_jobs=4, seed=None):
"""Main process."""
# Make sure output folder exists
utils.ensure_dir(out_path)
# Get relevant files
audio_files = glob.glob(os.path.join(in_path, "*.wav"))
# Call in parallel
Parallel(n_jobs=n_jobs)(delayed(process_track)(
audio_file, out_path, bounds_type, labels_type, seed)
for audio_file in audio_files)
def main():
"""Main function to parse the arguments and call the main process."""
parser = argparse.ArgumentParser(description=
"Runs the segmenter in all the wav files in the specified folder "
"and saves the results in the output folder",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("in_path",
action="store",
help="Folder to Wav Files")
parser.add_argument("-r",
dest="random_seed",
action="store",
type=int,
default=None,
help="Random Seed")
parser.add_argument("-o",
action="store",
dest="out_path",
default="estimations",
help="Output folder to store the lab files")
parser.add_argument("-b",
action="store",
dest="bounds_type",
help="Which algortihm to use to extract the "
"boundaries",
default="cnmf",
choices=["cnmf", "foote", "sf"])
parser.add_argument("-s",
action="store",
dest="labels_type",
help="Which algortihm to use to extract the "
"segment similarity (labeling)",
default="cnmf",
choices=["cnmf", "2dfmc"])
parser.add_argument("-j",
action="store",
dest="n_jobs",
default=4,
type=int,
help="The number of threads to use")
args = parser.parse_args()
start_time = time.time()
# Setup the logger
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
level=logging.INFO)
# Run the algorithm
process(args.in_path, args.out_path, bounds_type=args.bounds_type,
labels_type=args.labels_type, n_jobs=args.n_jobs, seed=args.random_seed)
# Done!
logging.info("Done! Took %.2f seconds." % (time.time() - start_time))
if __name__ == '__main__':
main()