-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_chunks.py
298 lines (261 loc) · 12.7 KB
/
add_chunks.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import json
import subprocess
import random
import re
from pathlib import Path
from pydub import AudioSegment
import librosa
import numpy as np
from tqdm import tqdm
from essentia.standard import MonoLoader, TensorflowPredictEffnetDiscogs, TensorflowPredict2D
import shutil
import shlex
from essentia_labels import genre_labels, mood_theme_classes, instrument_classes
# Paths
raw_audio_path = "./dataset/gary"
demucs_output_path = "./dataset/gary/demucs/htdemucs"
instrumental_output_path = "./dataset/gary/instrumental"
split_output_path = "./dataset/gary/split"
output_dataset_path = "./dataset/gary"
train_jsonl_path = os.path.join(output_dataset_path, "train.jsonl")
test_jsonl_path = os.path.join(output_dataset_path, "test.jsonl")
# Ensure the split folder exists
os.makedirs(split_output_path, exist_ok=True)
os.makedirs(instrumental_output_path, exist_ok=True)
os.makedirs(demucs_output_path, exist_ok=True)
# Demucs configuration
model = "htdemucs"
extensions = ["mp3", "wav", "ogg", "flac"]
two_stems = None
mp3 = True
mp3_rate = 320
float32 = False
int24 = False
# Function to find files with specific extensions in a path
def find_files(in_path):
out = []
for file in Path(in_path).iterdir():
if file.suffix.lower().lstrip(".") in extensions:
out.append(file)
return out
# Function to check if a file has already been chunked
def is_file_chunked(raw_filename, split_dir):
# Remove file extension from the raw audio filename
raw_base_filename = os.path.splitext(raw_filename)[0]
# List all chunked files in the split directory
chunked_files = [f for f in os.listdir(split_dir) if f.startswith(raw_base_filename)]
# If any chunked files exist with the same base name, consider the file chunked
return len(chunked_files) > 0
# Function to separate audio using Demucs
def separate(inp, outp):
cmd = ["demucs", "-n", model, "--two-stems=vocals", "--mp3", f"--mp3-bitrate={mp3_rate}", "--segment", "4", "-o", shlex.quote(str(outp))]
if float32:
cmd += ["--float32"]
if int24:
cmd += ["--int24"]
files = [shlex.quote(str(f)) for f in find_files(inp)]
if not files:
print(f"No valid audio files in {inp}")
return
print("Going to separate the files:")
print('\n'.join(files))
print("With command: ", " ".join(cmd + files))
p = subprocess.Popen(cmd + files, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print("Demucs output:", stdout.decode())
print("Demucs errors:", stderr.decode())
if p.returncode != 0:
print("Command failed, something went wrong.")
# Function to slice and resample audio
def slice_and_resample_audio(dataset_path, output_dir, chunk_length=30000):
print(f"Slicing and resampling audio in {dataset_path}...")
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(dataset_path):
if filename.endswith(".mp3"):
if 'Zone.Identifier' in filename:
continue
audio_path = os.path.join(dataset_path, filename)
if os.path.exists(audio_path):
audio = AudioSegment.from_file(audio_path)
audio = audio.set_frame_rate(44100)
duration = len(audio)
for i in range(0, duration - chunk_length, chunk_length):
chunk = audio[i:i + chunk_length]
chunk_filename = f"{os.path.splitext(filename)[0]}_chunk{i//1000}.wav"
chunk.export(os.path.join(output_dir, chunk_filename), format="wav")
if duration > chunk_length:
last_chunk = audio[-chunk_length:]
chunk_filename = f"{os.path.splitext(filename)[0]}_chunk{(duration - chunk_length)//1000}.wav"
last_chunk.export(os.path.join(output_dir, chunk_filename), format="wav")
else:
last_chunk = audio
chunk_filename = f"{os.path.splitext(filename)[0]}_chunk0.wav"
last_chunk.export(os.path.join(output_dir, chunk_filename), format="wav")
print(f"Processed {filename}")
else:
print(f"File {audio_path} not found")
print("All audio files processed.")
# Function to process Demucs output
def process_demucs_output(demucs_output_path, instrumental_output_path):
print("Processing Demucs output...")
os.makedirs(instrumental_output_path, exist_ok=True)
for folder in os.listdir(demucs_output_path):
first_level_folder_path = os.path.join(demucs_output_path, folder)
if os.path.isdir(first_level_folder_path):
model_folder_path = os.path.join(first_level_folder_path, "htdemucs")
if os.path.isdir(model_folder_path):
instrumental_file = os.path.join(model_folder_path, folder, "no_vocals.mp3")
if os.path.exists(instrumental_file):
output_filename = f"{folder}.mp3"
new_path = os.path.join(instrumental_output_path, output_filename)
shutil.move(instrumental_file, new_path)
print(f"Moved and renamed {instrumental_file} to {new_path}")
else:
print(f"No instrumental file found in {model_folder_path}/{folder}")
else:
print(f"Model directory {model_folder_path} does not exist")
else:
print(f"{first_level_folder_path} is not a directory")
# Function to rename files to remove spaces
def rename_files_to_remove_spaces(directory):
for filename in os.listdir(directory):
if ' ' in filename:
new_filename = filename.replace(' ', '_')
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
print(f"Renamed {filename} to {new_filename}")
# Function to process the raw dataset
def process_dataset(raw_audio_path, demucs_output_path, instrumental_output_path, split_output_path):
rename_files_to_remove_spaces(raw_audio_path)
for file in os.listdir(raw_audio_path):
if file.endswith((".mp3", ".wav", ".flac", ".m4a")):
input_file = os.path.join(raw_audio_path, file)
output_folder = os.path.splitext(file)[0]
cmd = [
"demucs",
"-n", "htdemucs",
"--two-stems=vocals",
"--mp3",
"--mp3-bitrate", "320",
"--segment", "4",
"-o", shlex.quote(os.path.join(demucs_output_path, output_folder)),
shlex.quote(input_file)
]
print("Running command: ", " ".join(cmd))
subprocess.run(" ".join(cmd), shell=True, check=True)
process_demucs_output(demucs_output_path, instrumental_output_path)
slice_and_resample_audio(instrumental_output_path, split_output_path)
# Function to filter predictions based on threshold
def filter_predictions(predictions, class_list, threshold=0.1):
predictions_mean = np.mean(predictions, axis=0)
sorted_indices = np.argsort(predictions_mean)[::-1]
filtered_indices = [i for i in sorted_indices if predictions_mean[i] > threshold]
filtered_labels = [class_list[i] for i in filtered_indices]
filtered_values = [predictions_mean[i] for i in filtered_indices]
return filtered_labels, filtered_values
# Function to create comma-separated unique tags
def make_comma_separated_unique(tags):
seen_tags = set()
result = []
for tag in ', '.join(tags).split(', '):
if tag not in seen_tags:
result.append(tag)
seen_tags.add(tag)
return ', '.join(result)
# Function to extract the artist name from the filename
def extract_artist_from_filename(filename):
match = re.search(r'(.+?)\s\d+_chunk\d+\.wav', filename)
artist = match.group(1) if match else ""
return artist.replace("mix", "").strip() if "mix" in artist else artist
# Function to get audio features
def get_audio_features(audio_filename):
audio = MonoLoader(filename=audio_filename, sampleRate=16000, resampleQuality=4)()
embedding_model = TensorflowPredictEffnetDiscogs(graphFilename="discogs-effnet-bs64-1.pb", output="PartitionedCall:1")
embeddings = embedding_model(audio)
result_dict = {}
genre_model = TensorflowPredict2D(graphFilename="genre_discogs400-discogs-effnet-1.pb", input="serving_default_model_Placeholder", output="PartitionedCall:0")
predictions = genre_model(embeddings)
filtered_labels, _ = filter_predictions(predictions, genre_labels)
filtered_labels = ', '.join(filtered_labels).replace("---", ", ").split(', ')
result_dict['genres'] = make_comma_separated_unique(filtered_labels)
mood_model = TensorflowPredict2D(graphFilename="mtg_jamendo_moodtheme-discogs-effnet-1.pb")
predictions = mood_model(embeddings)
filtered_labels, _ = filter_predictions(predictions, mood_theme_classes, threshold=0.05)
result_dict['moods'] = make_comma_separated_unique(filtered_labels)
instrument_model = TensorflowPredict2D(graphFilename="mtg_jamendo_instrument-discogs-effnet-1.pb")
predictions = instrument_model(embeddings)
filtered_labels, _ = filter_predictions(predictions, instrument_classes)
result_dict['instruments'] = filtered_labels
return result_dict
# Function to append to JSONL files
def append_to_jsonl(jsonl_path, new_entries):
with open(jsonl_path, "a") as f:
for entry in new_entries:
f.write(json.dumps(entry) + '\n')
# Function to autolabel and create split datasets
def autolabel_and_create_split(split_dataset_path, output_dataset_path):
train_entries = []
eval_entries = []
with open(os.path.join(output_dataset_path, "train.jsonl"), "a") as train_file, \
open(os.path.join(output_dataset_path, "test.jsonl"), "a") as eval_file:
dset = os.listdir(split_dataset_path)
random.shuffle(dset)
for filename in tqdm(dset):
if 'Zone.Identifier' in filename:
continue
try:
result = get_audio_features(os.path.join(split_dataset_path, filename))
except:
result = {"genres": [], "moods": [], "instruments": []}
y, sr = librosa.load(os.path.join(split_dataset_path, filename))
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
tempo = round(tempo[0]) if isinstance(tempo, np.ndarray) else round(tempo)
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
key = np.argmax(np.sum(chroma, axis=1))
key = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'][key]
length = librosa.get_duration(y=y, sr=sr)
artist_name = extract_artist_from_filename(filename)
entry = {
"key": f"{key}",
"artist": artist_name,
"sample_rate": 44100,
"file_extension": "wav",
"description": "",
"keywords": "",
"duration": length,
"bpm": tempo,
"genre": result.get('genres', ""),
"title": filename,
"name": "",
"instrument": result.get('instruments', ""),
"moods": result.get('moods', []),
"path": os.path.join(split_dataset_path, filename)
}
if random.random() < 0.85:
train_entries.append(entry)
else:
eval_entries.append(entry)
append_to_jsonl(train_jsonl_path, train_entries)
append_to_jsonl(test_jsonl_path, eval_entries)
# Main function
if __name__ == "__main__":
# Check for new audio files that haven't been chunked yet
new_files_to_process = []
for file in os.listdir(raw_audio_path):
if file.endswith((".mp3", ".wav", ".flac", ".m4a")):
if not is_file_chunked(file, split_output_path):
new_files_to_process.append(file)
if new_files_to_process:
print(f"New files to process: {new_files_to_process}")
vocals_remove = input("Do you need to remove vocals? (y/n): ").strip().lower()
if vocals_remove == 'y':
process_dataset(raw_audio_path, demucs_output_path, instrumental_output_path, split_output_path)
split_and_resample = input("Do you need to split and resample audio? (y/n): ").strip().lower()
if split_and_resample == 'y':
slice_and_resample_audio(instrumental_output_path, split_output_path)
autolabel = input("Do you need to autolabel and update JSONL files? (y/n): ").strip().lower()
if autolabel == 'y':
autolabel_and_create_split(split_output_path, output_dataset_path)
else:
print("No new files to process.")
print("Process completed.")