|
import json |
|
from emotion import get_emotion |
|
from genre import get_genre |
|
from instrument import get_instrument |
|
import os |
|
from pydub import AudioSegment |
|
import tqdm |
|
|
|
|
|
EXCERPT_LENGTH = 30 * 1000 |
|
MIN_LENGTH = 5 * 1000 |
|
|
|
|
|
for split in ["test", "train", "valid"]: |
|
output_file_path = f'MTG-Jamendo_{split}.jsonl' |
|
emotion = get_emotion(_=split) |
|
genre = get_genre(_=split) |
|
instrument = get_instrument(_=split) |
|
data_samples = emotion + genre + instrument |
|
with open(output_file_path, 'w') as outfile: |
|
for sample in tqdm.tqdm(data_samples): |
|
|
|
|
|
audio_path = f"/home/intern-2024-02/dataset/MTG-Jamendo/MTG/audio-low/{sample['audioid'][:-4]}.low.mp3" |
|
|
|
if not os.path.exists(audio_path): |
|
print(f"File not found: {audio_path}") |
|
continue |
|
|
|
|
|
audio = AudioSegment.from_file(audio_path) |
|
|
|
|
|
for i in range(0, len(audio), EXCERPT_LENGTH): |
|
end = i + EXCERPT_LENGTH |
|
if end < len(audio): |
|
excerpt = audio[i:end] |
|
else: |
|
excerpt = audio[i:] |
|
|
|
if len(excerpt) < MIN_LENGTH: |
|
break |
|
end = len(audio) |
|
|
|
|
|
excerpt_path = f"/work/fast_data_yinghao/MTG/audio-low/{sample['audioid'][:-4]}.low_{i//EXCERPT_LENGTH}.wav" |
|
|
|
|
|
|
|
new_sample = sample.copy() |
|
new_sample["audioid"] = excerpt_path |
|
|
|
|
|
json.dump(new_sample, outfile) |
|
outfile.write('\n') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outfile.close() |
|
|
|
|
|
|