tahamajs/ML / Files /ThisTerm /final_project /src /preprocessing.py
tahamajs's picture
download
raw
863 Bytes
from typing import Tuple, Optional
import librosa
import numpy as np
def load_audio(path: str, sr: Optional[int] = 22050, mono: bool = True) -> Tuple[np.ndarray, int]:
"""
Load an audio file using librosa.
Returns:
y: audio time series (np.ndarray)
sr: sampling rate
"""
y, sampling_rate = librosa.load(path, sr=sr, mono=mono)
return y, sampling_rate
def trim_silence(y: np.ndarray, top_db: int = 20) -> np.ndarray:
"""
Trim leading and trailing silence from an audio signal.
"""
trimmed, _ = librosa.effects.trim(y, top_db=top_db)
return trimmed
def normalize_audio(y: np.ndarray) -> np.ndarray:
"""
Normalize audio to have max absolute amplitude of 1.0.
"""
max_val = np.max(np.abs(y)) if y.size > 0 else 1.0
if max_val == 0:
return y
return y / max_val

Xet Storage Details

Size:
863 Bytes
·
Xet hash:
7c5ee59d39a7fcd8f816a1f361f13103592e241f7d123ede45a68f86311b889b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.