|
import os |
|
import librosa |
|
import soundfile as sf |
|
|
|
|
|
input_dir = os.path.join(os.getcwd(), 'data', 'genres') |
|
|
|
|
|
output_dir = os.path.join(os.getcwd(), 'data_5') |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
segment_duration = 5 |
|
|
|
|
|
for genre_folder in os.listdir(input_dir): |
|
print(f"Genre: {genre_folder} @ {segment_duration} seconds") |
|
genre_path = os.path.join(input_dir, genre_folder) |
|
|
|
|
|
output_genre_path = os.path.join(output_dir, genre_folder) |
|
os.makedirs(output_genre_path, exist_ok=True) |
|
|
|
|
|
for filename in os.listdir(genre_path): |
|
file_path = os.path.join(genre_path, filename) |
|
|
|
|
|
if filename.endswith('.wav'): |
|
|
|
audio, sr = librosa.load(file_path) |
|
|
|
|
|
num_segments = len(audio) // (sr * segment_duration) |
|
|
|
|
|
for i in range(num_segments): |
|
start_sample = i * sr * segment_duration |
|
end_sample = start_sample + sr * segment_duration |
|
segment = audio[start_sample:end_sample] |
|
|
|
|
|
segment_filename = f"{filename[:-4]}_{i+1}.wav" |
|
|
|
|
|
segment_path = os.path.join(output_genre_path, segment_filename) |
|
sf.write(segment_path, segment, sr) |
|
else: |
|
print(f"Skipping {filename} as it is not a WAV file.") |
|
|
|
|
|
|
|
output_dir = os.path.join(os.getcwd(), 'data_10') |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
segment_duration = 10 |
|
|
|
|
|
for genre_folder in os.listdir(input_dir): |
|
print(f"Genre: {genre_folder} @ {segment_duration} seconds") |
|
genre_path = os.path.join(input_dir, genre_folder) |
|
|
|
|
|
output_genre_path = os.path.join(output_dir, genre_folder) |
|
os.makedirs(output_genre_path, exist_ok=True) |
|
|
|
|
|
for filename in os.listdir(genre_path): |
|
file_path = os.path.join(genre_path, filename) |
|
|
|
|
|
if filename.endswith('.wav'): |
|
|
|
audio, sr = librosa.load(file_path) |
|
|
|
|
|
num_segments = len(audio) // (sr * segment_duration) |
|
|
|
|
|
for i in range(num_segments): |
|
start_sample = i * sr * segment_duration |
|
end_sample = start_sample + sr * segment_duration |
|
segment = audio[start_sample:end_sample] |
|
|
|
|
|
segment_filename = f"{filename[:-4]}_{i+1}.wav" |
|
|
|
|
|
segment_path = os.path.join(output_genre_path, segment_filename) |
|
sf.write(segment_path, segment, sr) |
|
else: |
|
print(f"Skipping {filename} as it is not a WAV file.") |
|
|
|
|
|
|
|
output_dir = os.path.join(os.getcwd(), 'data_15') |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
segment_duration = 15 |
|
|
|
|
|
for genre_folder in os.listdir(input_dir): |
|
print(f"Genre: {genre_folder} @ {segment_duration} seconds") |
|
genre_path = os.path.join(input_dir, genre_folder) |
|
|
|
|
|
output_genre_path = os.path.join(output_dir, genre_folder) |
|
os.makedirs(output_genre_path, exist_ok=True) |
|
|
|
|
|
for filename in os.listdir(genre_path): |
|
file_path = os.path.join(genre_path, filename) |
|
|
|
|
|
if filename.endswith('.wav'): |
|
|
|
audio, sr = librosa.load(file_path) |
|
|
|
|
|
num_segments = len(audio) // (sr * segment_duration) |
|
|
|
|
|
for i in range(num_segments): |
|
start_sample = i * sr * segment_duration |
|
end_sample = start_sample + sr * segment_duration |
|
segment = audio[start_sample:end_sample] |
|
|
|
|
|
segment_filename = f"{filename[:-4]}_{i+1}.wav" |
|
|
|
|
|
segment_path = os.path.join(output_genre_path, segment_filename) |
|
sf.write(segment_path, segment, sr) |
|
else: |
|
print(f"Skipping {filename} as it is not a WAV file.") |
|
|
|
|