-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
186 lines (154 loc) · 11.3 KB
/
main.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
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
from feature_extractors.feature_extractor import FeatureExtractor, FeatureExtractors
from feature_extractors.essentia_extractors import EssentiaFeatureExtractor, EssentiaVoiceExtractor
from feature_extractors.beatnet_extractor import BeatNetExtractor
from feature_extractors.btc_chord_extractor import BTCChordExtractor
from feature_extractors.key_classification_extractor import KeyClassificationExtractor
import os
from feature_extractors.gender_classifier import GenderClassifier
from caption_generator import CaptionGenerator
import argparse
import yaml
import json
import warnings
import time
from tqdm import tqdm
class MusicFeatureExtractor:
def __init__(self, config_file_path):
self.config_file_path = config_file_path
self.configs = self.load_configs(self.config_file_path)
self.enable_caption_generation = self.configs["pipeline"]["enable_caption_generation"]
self.input_file_path = self.configs["files"]["input"]
self.output_file_path = self.configs["files"]["output"]
self.source_separated_file_dir = self.configs["paths"]["source_separated_audio"]
self.saved_features_dir = self.configs["paths"]["saved_features"]
self.temp_file_path = self.configs["paths"]["temp"]
self.active_extractors = []
self.feature_extractors = []
if self.configs["extractors"]["mood_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.MOOD_EXTRACTOR.value, EssentiaFeatureExtractor("mood", self.configs["extractors"]["mood_extractor"]["model"], self.configs["extractors"]["mood_extractor"], self.configs["extractors"]["mood_extractor"]["model_metadata"], self.configs["extractors"]["mood_extractor"]["embedding_model"], 5, 0.1))
if ("source" in self.configs["extractors"]["mood_extractor"].keys()):
self.feature_extractors[FeatureExtractors.MOOD_EXTRACTOR.value].set_source(self.configs["extractors"]["mood_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.MOOD_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.MOOD_EXTRACTOR.value, None)
if self.configs["extractors"]["genre_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.GENRE_EXTRACTOR.value, EssentiaFeatureExtractor("genre", self.configs["extractors"]["genre_extractor"]["model"], self.configs["extractors"]["genre_extractor"], self.configs["extractors"]["genre_extractor"]["model_metadata"], self.configs["extractors"]["genre_extractor"]["embedding_model"], 4, 0.1))
if ("source" in self.configs["extractors"]["genre_extractor"].keys()):
self.feature_extractors[FeatureExtractors.GENRE_EXTRACTOR.value].set_source(self.configs["extractors"]["genre_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.GENRE_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.GENRE_EXTRACTOR.value, None)
if self.configs["extractors"]["instrument_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.INSTRUMENT_EXTRACTOR.value, EssentiaFeatureExtractor("instrument", self.configs["extractors"]["instrument_extractor"]["model"], self.configs["extractors"]["instrument_extractor"], self.configs["extractors"]["instrument_extractor"]["model_metadata"], self.configs["extractors"]["instrument_extractor"]["embedding_model"], 7, 0.1))
if ("source" in self.configs["extractors"]["instrument_extractor"].keys()):
self.feature_extractors[FeatureExtractors.INSTRUMENT_EXTRACTOR.value].set_source(self.configs["extractors"]["instrument_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.INSTRUMENT_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.INSTRUMENT_EXTRACTOR.value, None)
if self.configs["extractors"]["auto_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.AUTO_EXTRACTOR.value, EssentiaFeatureExtractor("autotags", self.configs["extractors"]["auto_extractor"]["model"], self.configs["extractors"]["auto_extractor"], self.configs["extractors"]["auto_extractor"]["model_metadata"], self.configs["extractors"]["auto_extractor"]["embedding_model"], 8, 0.1))
if ("source" in self.configs["extractors"]["auto_extractor"].keys()):
self.feature_extractors[FeatureExtractors.AUTO_EXTRACTOR.value].set_source(self.configs["extractors"]["auto_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.AUTO_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.AUTO_EXTRACTOR.value, None)
if self.configs["extractors"]["voice_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.VOICE_EXTRACTOR.value, EssentiaVoiceExtractor("voice", self.configs["extractors"]["voice_extractor"]["model"], self.configs["extractors"]["voice_extractor"], self.configs["extractors"]["voice_extractor"]["model_metadata"], self.configs["extractors"]["voice_extractor"]["embedding_model"]))
if ("source" in self.configs["extractors"]["voice_extractor"].keys()):
self.feature_extractors[FeatureExtractors.VOICE_EXTRACTOR.value].set_source(self.configs["extractors"]["voice_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.VOICE_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.VOICE_EXTRACTOR.value, None)
if self.configs["extractors"]["gender_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.GENDER_EXTRACTOR.value, EssentiaVoiceExtractor("gender", self.configs["extractors"]["gender_extractor"]["model"], self.configs["extractors"]["gender_extractor"], self.configs["extractors"]["gender_extractor"]["model_metadata"], self.configs["extractors"]["gender_extractor"]["embedding_model"]))
if ("source" in self.configs["extractors"]["gender_extractor"].keys()):
self.feature_extractors[FeatureExtractors.GENDER_EXTRACTOR.value].set_source(self.configs["extractors"]["gender_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.GENDER_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.GENDER_EXTRACTOR.value, None)
if self.configs["extractors"]["beatnet_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.BEATNET_EXTRACTOR.value, BeatNetExtractor("beats", self.configs["extractors"]["beatnet_extractor"]["model"], self.configs["extractors"]["beatnet_extractor"]))
if ("source" in self.configs["extractors"]["beatnet_extractor"].keys()):
self.feature_extractors[FeatureExtractors.BEATNET_EXTRACTOR.value].set_source(self.configs["extractors"]["beatnet_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.BEATNET_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.BEATNET_EXTRACTOR.value, None)
if self.configs["extractors"]["btc_chord_extractor"]["active"] :
self.feature_extractors.insert(FeatureExtractors.BTC_CHORD_EXTRACTOR.value, BTCChordExtractor("chords", self.configs["extractors"]["btc_chord_extractor"]["model"], self.configs["extractors"]["btc_chord_extractor"], self.configs["extractors"]["btc_chord_extractor"]["config_file"]))
if ("source" in self.configs["extractors"]["btc_chord_extractor"].keys()):
self.feature_extractors[FeatureExtractors.BTC_CHORD_EXTRACTOR.value].set_source(self.configs["extractors"]["btc_chord_extractor"]["source"])
self.active_extractors.append(FeatureExtractors.BTC_CHORD_EXTRACTOR.value)
else:
self.feature_extractors.insert(FeatureExtractors.BTC_CHORD_EXTRACTOR.value, None)
if self.configs["extractors"]["gender_classifier"]["active"] :
self.feature_extractors.insert(FeatureExtractors.GENDER_CLASSIFIER.value, GenderClassifier("gender", self.configs["extractors"]["gender_classifier"]["model"], self.configs["extractors"]["gender_classifier"]))
if ("source" in self.configs["extractors"]["gender_classifier"].keys()):
self.feature_extractors[FeatureExtractors.GENDER_CLASSIFIER.value].set_source(self.configs["extractors"]["gender_classifier"]["source"])
self.active_extractors.append(FeatureExtractors.GENDER_CLASSIFIER.value)
else:
self.feature_extractors.insert(FeatureExtractors.GENDER_CLASSIFIER.value, None)
if self.configs["extractors"]["key_classifier"]["active"] :
self.feature_extractors.insert(FeatureExtractors.KEY_CLASSIFIER.value, KeyClassificationExtractor("key", self.configs["extractors"]["key_classifier"]["model"], self.configs["extractors"]["key_classifier"]))
if ("source" in self.configs["extractors"]["key_classifier"].keys()):
self.feature_extractors[FeatureExtractors.KEY_CLASSIFIER.value].set_source(self.configs["extractors"]["key_classifier"]["source"])
self.active_extractors.append(FeatureExtractors.KEY_CLASSIFIER.value)
else:
self.feature_extractors.insert(FeatureExtractors.KEY_CLASSIFIER.value, None)
if (self.enable_caption_generation):
self.caption_generator = CaptionGenerator(self.configs["caption_generator"]["api_key"], self.configs["caption_generator"]["model_id"])
def load_configs(self, file_path):
configs = {}
with open(file_path, 'r') as f:
configs = yaml.safe_load(f)
return configs
def get_audio_paths(self, file_path):
audio_paths = []
with open(file_path, 'r') as f:
for row in f:
audio_path=json.loads(row)
audio_paths.append(audio_path["location"])
return audio_paths
def process_audio(self, snippet_path):
audio_tags = {}
for extractor in self.active_extractors:
if not self.feature_extractors[extractor].get_source() == "raw":
source_splitted_path = self.source_separated_file_dir + "/" + os.path.splitext(os.path.basename(snippet_path))[0] + "/" + self.feature_extractors[extractor].get_source() + ".mp3"
if not os.path.exists(source_splitted_path):
source_splitted_path = snippet_path
else:
source_splitted_path = snippet_path
feature_tags = self.feature_extractors[extractor].extract_features(source_splitted_path)
if self.feature_extractors[extractor].get_config_value("save_features", False):
features_dir = self.saved_features_dir + "/" + os.path.splitext(os.path.basename(snippet_path))[0] + "/"
if not os.path.exists(features_dir):
os.makedirs(features_dir)
self.feature_extractors[extractor].save_extracted_features(features_dir)
audio_tags[self.feature_extractors[extractor].get_tag_type()] = feature_tags
if (self.enable_caption_generation):
prompt = self.caption_generator.create_prompt(audio_tags)
caption = self.caption_generator.generate_caption(prompt)
audio_tags["caption"] = caption
audio_tags["location"] = snippet_path
return audio_tags
def extract_features_all(self):
audio_paths = self.get_audio_paths(self.input_file_path)
audio_tags = []
for audio_path in tqdm(audio_paths, desc="Feature Extraction Progress", unit="audio"):
try:
caption = self.process_audio(audio_path)
except:
print("Error extracting features for audio : ", audio_path)
caption = {"location": audio_path, "caption": "!!!Error"}
audio_tags.append(caption)
return audio_tags
def save_features(self, audio_tags):
with open(self.output_file_path, 'w') as out_f:
for tags in audio_tags:
out_f.write(json.dumps(tags) + '\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract features and pseuo-captions for audio snippets.")
parser.add_argument("config_file", help="Path to the file with configs for feature extraction")
args = parser.parse_args()
music_feature_extractor = MusicFeatureExtractor(args.config_file)
features = music_feature_extractor.extract_features_all()
music_feature_extractor.save_features(features)