Spaces:
Sleeping
Sleeping
Create utils/audio_processor.py
Browse files- utils/audio_processor.py +73 -0
utils/audio_processor.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Audio processing utilities
|
| 3 |
+
"""
|
| 4 |
+
import numpy as np
|
| 5 |
+
import librosa
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
from typing import Tuple, Optional
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
class AudioProcessor:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.sample_rate = 16000 # Default sample rate for models
|
| 15 |
+
|
| 16 |
+
def load_audio(self, file_path: str) -> Tuple[np.ndarray, int]:
|
| 17 |
+
"""Load audio file and convert to appropriate format"""
|
| 18 |
+
try:
|
| 19 |
+
audio, sr = librosa.load(file_path, sr=self.sample_rate)
|
| 20 |
+
return audio, sr
|
| 21 |
+
except Exception as e:
|
| 22 |
+
logger.error(f"Failed to load audio: {str(e)}")
|
| 23 |
+
raise
|
| 24 |
+
|
| 25 |
+
def save_audio(self, audio_array: np.ndarray, output_path: str, sample_rate: int = None):
|
| 26 |
+
"""Save audio array to file"""
|
| 27 |
+
try:
|
| 28 |
+
sr = sample_rate or self.sample_rate
|
| 29 |
+
sf.write(output_path, audio_array, sr)
|
| 30 |
+
logger.info(f"Audio saved to {output_path}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Failed to save audio: {str(e)}")
|
| 33 |
+
raise
|
| 34 |
+
|
| 35 |
+
def normalize_audio(self, audio: np.ndarray) -> np.ndarray:
|
| 36 |
+
"""Normalize audio to [-1, 1] range"""
|
| 37 |
+
return audio / np.max(np.abs(audio))
|
| 38 |
+
|
| 39 |
+
def trim_silence(self, audio: np.ndarray, threshold: float = 0.01) -> np.ndarray:
|
| 40 |
+
"""Remove silence from beginning and end"""
|
| 41 |
+
return librosa.effects.trim(audio, top_db=20, frame_length=512, hop_length=256)[0]
|
| 42 |
+
|
| 43 |
+
def change_speed(self, audio: np.ndarray, speed_factor: float) -> np.ndarray:
|
| 44 |
+
"""Change playback speed without changing pitch"""
|
| 45 |
+
return librosa.effects.time_stretch(audio, rate=speed_factor)
|
| 46 |
+
|
| 47 |
+
def change_pitch(self, audio: np.ndarray, n_steps: float) -> np.ndarray:
|
| 48 |
+
"""Change pitch by n semitones"""
|
| 49 |
+
return librosa.effects.pitch_shift(audio, sr=self.sample_rate, n_steps=n_steps)
|
| 50 |
+
|
| 51 |
+
def get_spectrogram(self, audio: np.ndarray) -> np.ndarray:
|
| 52 |
+
"""Generate spectrogram for visualization"""
|
| 53 |
+
return librosa.stft(audio)
|
| 54 |
+
|
| 55 |
+
def get_tempo(self, audio: np.ndarray) -> float:
|
| 56 |
+
"""Estimate tempo (BPM)"""
|
| 57 |
+
tempo, _ = librosa.beat.beat_track(y=audio, sr=self.sample_rate)
|
| 58 |
+
return tempo
|
| 59 |
+
|
| 60 |
+
def apply_fade(self, audio: np.ndarray, fade_in: float = 0.1, fade_out: float = 0.1) -> np.ndarray:
|
| 61 |
+
"""Apply fade in/out"""
|
| 62 |
+
fade_in_samples = int(fade_in * self.sample_rate)
|
| 63 |
+
fade_out_samples = int(fade_out * self.sample_rate)
|
| 64 |
+
|
| 65 |
+
if fade_in_samples > 0:
|
| 66 |
+
fade_in_curve = np.linspace(0, 1, fade_in_samples)
|
| 67 |
+
audio[:fade_in_samples] *= fade_in_curve
|
| 68 |
+
|
| 69 |
+
if fade_out_samples > 0:
|
| 70 |
+
fade_out_curve = np.linspace(1, 0, fade_out_samples)
|
| 71 |
+
audio[-fade_out_samples:] *= fade_out_curve
|
| 72 |
+
|
| 73 |
+
return audio
|