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.")