File size: 5,983 Bytes
58c5444 |
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 |
import os
import librosa
import soundfile as sf
# Define the input directory relative to the current working directory
input_dir = os.path.join(os.getcwd(), 'data', 'genres') # Assumes the genres folder is inside the data folder
# Define the output directory relative to the current working directory
output_dir = os.path.join(os.getcwd(), 'data_5') # Replace 'output' with your desired output directory name
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Define the segment duration in seconds
segment_duration = 5
# Iterate over the genre folders in the input directory
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)
# Create a corresponding genre folder in the output directory
output_genre_path = os.path.join(output_dir, genre_folder)
os.makedirs(output_genre_path, exist_ok=True)
# Iterate over all audio files in the genre folder
for filename in os.listdir(genre_path):
file_path = os.path.join(genre_path, filename)
# Check if the file is in WAV format
if filename.endswith('.wav'):
# Load the audio file using Librosa
audio, sr = librosa.load(file_path)
# Calculate the total number of segments
num_segments = len(audio) // (sr * segment_duration)
# Divide the audio into segments and save them individually
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]
# Create a new filename for the segment
segment_filename = f"{filename[:-4]}_{i+1}.wav"
# Save the segment to the genre folder in the output directory
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.")
# Define the output directory relative to the current working directory
output_dir = os.path.join(os.getcwd(), 'data_10') # Replace 'output' with your desired output directory name
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Define the segment duration in seconds
segment_duration = 10
# Iterate over the genre folders in the input directory
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)
# Create a corresponding genre folder in the output directory
output_genre_path = os.path.join(output_dir, genre_folder)
os.makedirs(output_genre_path, exist_ok=True)
# Iterate over all audio files in the genre folder
for filename in os.listdir(genre_path):
file_path = os.path.join(genre_path, filename)
# Check if the file is in WAV format
if filename.endswith('.wav'):
# Load the audio file using Librosa
audio, sr = librosa.load(file_path)
# Calculate the total number of segments
num_segments = len(audio) // (sr * segment_duration)
# Divide the audio into segments and save them individually
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]
# Create a new filename for the segment
segment_filename = f"{filename[:-4]}_{i+1}.wav"
# Save the segment to the genre folder in the output directory
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.")
# Define the output directory relative to the current working directory
output_dir = os.path.join(os.getcwd(), 'data_15') # Replace 'output' with your desired output directory name
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Define the segment duration in seconds
segment_duration = 15
# Iterate over the genre folders in the input directory
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)
# Create a corresponding genre folder in the output directory
output_genre_path = os.path.join(output_dir, genre_folder)
os.makedirs(output_genre_path, exist_ok=True)
# Iterate over all audio files in the genre folder
for filename in os.listdir(genre_path):
file_path = os.path.join(genre_path, filename)
# Check if the file is in WAV format
if filename.endswith('.wav'):
# Load the audio file using Librosa
audio, sr = librosa.load(file_path)
# Calculate the total number of segments
num_segments = len(audio) // (sr * segment_duration)
# Divide the audio into segments and save them individually
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]
# Create a new filename for the segment
segment_filename = f"{filename[:-4]}_{i+1}.wav"
# Save the segment to the genre folder in the output directory
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.")
|