-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcompute_features.py
executable file
·116 lines (98 loc) · 3.21 KB
/
compute_features.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
#!/usr/bin/env python
"""This script computes the features for a single or multiple audio files. It
uses the default parameters. These parameters can be changed the `.msafrc`
config file.
Examples:
Single file mode:
>> ./compute_features.py path_to_audio.mp3 -o my_features.json
Collection mode:
Run on 12 cores on the specified dataset:
>> ./compute_features.py path_to_dataset/ -j 12
"""
import argparse
import logging
import os
import time
from joblib import Parallel, delayed
import msaf
from msaf.features import Features
def compute_all_features(file_struct, framesync):
"""Computes all features for the given file."""
for feature_id in msaf.features_registry:
logging.info(f"Computing {feature_id} for file {file_struct.audio_file}")
feats = Features.select_features(feature_id, file_struct, False, framesync)
feats.features
def process(in_path, out_file, n_jobs, framesync):
"""Computes the features for the selected dataset or file."""
if os.path.isfile(in_path):
# Single file mode
# Get (if they exitst) or compute features
file_struct = msaf.io.FileStruct(in_path)
file_struct.features_file = out_file
compute_all_features(file_struct, framesync)
else:
# Collection mode
file_structs = msaf.io.get_dataset_files(in_path)
# Call in parallel
return Parallel(n_jobs=n_jobs)(
delayed(compute_all_features)(file_struct, framesync)
for file_struct in file_structs
)
def main():
"""Main function to parse the arguments and call the main process."""
parser = argparse.ArgumentParser(
description="Extracts a set of features from a given dataset "
"or audio file and saves them into the 'features' folder of "
"the dataset or the specified single file.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"in_path", action="store", help="Input dataset dir or audio file"
)
parser.add_argument(
"-j",
action="store",
dest="n_jobs",
type=int,
help="Number of jobs (only for collection mode)",
default=4,
)
parser.add_argument(
"-o",
action="store",
dest="out_file",
type=str,
help="Output file (only for single file mode)",
default="out.json",
)
parser.add_argument(
"-d",
action="store",
dest="ds_name",
default="*",
help="The prefix of the dataset to use " "(e.g. Isophonics, SALAMI)",
)
parser.add_argument(
"-fs",
action="store_true",
dest="framesync",
help="Use frame-synchronous features",
default=False,
)
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 main process
process(
args.in_path,
out_file=args.out_file,
n_jobs=args.n_jobs,
framesync=args.framesync,
)
# Done!
logging.info("Done! Took %.2f seconds." % (time.time() - start_time))
if __name__ == "__main__":
main()