MusicGenrePulse / src /utility.py
Skynova's picture
Upload 16 files
ffa9b64 verified
import numpy as np
def slice_songs(X, Y, S,
sr=22050,
hop_length=512,
length_in_seconds=30,
overlap=0.5):
"""
Slice spectrograms into smaller splits with overlap.
Parameters:
X: Array of spectrograms
Y: Array of labels
S: Array of song names
sr: Sample rate (default: 22050)
hop_length: Hop length used in spectrogram creation (default: 512)
length_in_seconds: Length of each slice in seconds (default: 30)
overlap: Overlap ratio between consecutive slices (default: 0.5 for 50% overlap)
"""
# Compute the number of frames for the desired slice length
frames_per_second = sr / hop_length
slice_length_frames = int(length_in_seconds * frames_per_second)
# Calculate hop size for overlapping (stride)
stride = int(slice_length_frames * (1 - overlap))
# Initialize lists for sliced data
X_slices = []
Y_slices = []
S_slices = []
# Slice each spectrogram
for i, spectrogram in enumerate(X):
num_frames = spectrogram.shape[1]
# Calculate start positions for all slices
start_positions = range(0, num_frames - slice_length_frames + 1, stride)
for start_frame in start_positions:
end_frame = start_frame + slice_length_frames
# Extract the slice
slice_ = spectrogram[:, start_frame:end_frame]
# Only add if the slice is the expected length
if slice_.shape[1] == slice_length_frames:
X_slices.append(slice_)
Y_slices.append(Y[i])
S_slices.append(S[i])
# Convert lists to numpy arrays
X_slices = np.array(X_slices)
Y_slices = np.array(Y_slices)
S_slices = np.array(S_slices)
return X_slices, Y_slices, S_slices