Spaces:
Sleeping
Sleeping
| #this is the actual preprocess file. it is being replaced with a Huggging Face friendly one. if needed revert to this. | |
| import librosa | |
| import numpy as np | |
| SAMPLE_RATE = 22050 | |
| DURATION = 1.0 | |
| SAMPLES_PER_SLICE = int(SAMPLE_RATE * DURATION) | |
| N_MELS = 128 | |
| def audio_to_spectrograms(file_path): | |
| try: | |
| y, sr = librosa.load(file_path, sr=SAMPLE_RATE) | |
| num_slices = len(y) // SAMPLES_PER_SLICE | |
| if num_slices < 1: return None | |
| spectrograms = [] | |
| for i in range(num_slices): | |
| y_slice = y[i*SAMPLES_PER_SLICE : (i+1)*SAMPLES_PER_SLICE] | |
| spec = librosa.feature.melspectrogram(y=y_slice, sr=sr, n_mels=N_MELS) | |
| log_spec = librosa.power_to_db(spec, ref=np.max) | |
| norm_spec = np.clip((log_spec + 80) / 80, 0, 1) | |
| spectrograms.append(norm_spec[..., np.newaxis]) | |
| return np.array(spectrograms) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return None |