File size: 2,556 Bytes
1919bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
===============================================================================
preprocessing/__init__.py β€” Audio Preprocessing Pipeline
===============================================================================

This package contains all audio-level preprocessing steps that happen BEFORE
feature extraction (mel spectrograms). The pipeline order is:

    Raw WAV β†’ Resampling (JSON) β†’ Normalization (JSON)
            β†’ Noise Reduction (EL sir) β†’ Silence Removal (EL sir)
            β†’ Clean numpy array ready for feature extraction (sala7)

OWNERS: EL sir (noise_reduction, silence_removal)
        JSON   (normalization, resampling)
===============================================================================
"""

from preprocessing.resampling import resample_audio
from preprocessing.normalization import normalize_volume
from preprocessing.noise_reduction import reduce_noise
from preprocessing.silence_removal import remove_silence


def preprocess_audio(file_path):
    """
    Full preprocessing pipeline: load β†’ resample β†’ normalize β†’ denoise β†’ trim.

    This is the main entry point that JSON's Dataset class and infer.py will call.
    It chains all four preprocessing steps in the correct order and returns
    a clean numpy array ready for mel spectrogram extraction.

    Parameters
    ----------
    file_path : str
        Absolute path to the .wav file.

    Returns
    -------
    audio : np.ndarray
        1D numpy array of the preprocessed audio waveform.
    sr : int
        Sampling rate (always TARGET_SR after resampling).

    Pipeline Order & Rationale
    --------------------------
    1. Resample FIRST β€” all downstream steps assume a fixed sample rate.
       If we denoise before resampling, the noise profile frequency bins
       won't match after resampling.
    2. Normalize SECOND β€” bring all recordings to the same loudness level
       before noise estimation, so the noise threshold is consistent.
    3. Denoise THIRD β€” remove background hum/motor noise.
    4. Trim LAST β€” now that noise is gone, energy-based silence detection
       can accurately find where the real machine sound starts/ends.
    """
    # Step 1: Load and resample to TARGET_SR
    audio, sr = resample_audio(file_path)

    # Step 2: Volume normalization (peak or RMS)
    audio = normalize_volume(audio)

    # Step 3: Noise reduction
    audio = reduce_noise(audio, sr)

    # Step 4: Silence removal (trim leading/trailing silence)
    audio = remove_silence(audio, sr)

    return audio, sr