| """ |
| =============================================================================== |
| features/__init__.py β Feature Extraction Pipeline |
| =============================================================================== |
| |
| This package converts preprocessed audio (clean numpy arrays) into fixed-size |
| tensors suitable for CNN input. |
| |
| Pipeline: Clean audio β Mel spectrogram β Log scale β Pad/Trim β Tensor |
| |
| OWNER: sala7 |
| =============================================================================== |
| """ |
|
|
| from features.mel_spectrogram import extract_mel_spectrogram |
| from features.padding import pad_or_trim_spectrogram |
| from features.augmentation import apply_augmentation |
|
|
|
|
| def audio_to_tensor(audio, sr, augment=False): |
| """ |
| Convert a preprocessed audio waveform into a fixed-size tensor for CNN input. |
| |
| This is the main entry point called by JSON's Dataset class. It chains |
| mel spectrogram extraction, padding/trimming, and optional augmentation. |
| |
| Returns |
| ------- |
| torch.Tensor |
| Shape: (1, N_MELS, FIXED_TIME_FRAMES) = (1, 128, 281) |
| - 1 channel (grayscale spectrogram) |
| - 128 mel frequency bins |
| - 281 time frames |
| |
| Pipeline |
| -------- |
| 1. extract_mel_spectrogram: audio β log-mel spectrogram (128 Γ T) |
| 2. pad_or_trim_spectrogram: (128 Γ T) β (128 Γ 281) fixed size |
| 3. apply_augmentation: (optional) SpecAugment + noise injection |
| 4. Add channel dimension: (128 Γ 281) β (1, 128, 281) |
| """ |
| import torch |
|
|
| |
| mel_spec = extract_mel_spectrogram(audio, sr) |
|
|
| |
| mel_spec = pad_or_trim_spectrogram(mel_spec) |
|
|
| |
| if augment: |
| mel_spec = apply_augmentation(mel_spec) |
|
|
| |
| |
| tensor = torch.FloatTensor(mel_spec).unsqueeze(0) |
| |
|
|
| return tensor |
|
|