Spaces:
Sleeping
Sleeping
File size: 3,315 Bytes
ed147e2 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | """
Audio preprocessing utilities.
Handles noise reduction, normalization, and format validation.
"""
from pathlib import Path
from typing import Optional
import wave
from src.utils.logger import setup_logger
logger = setup_logger(__name__)
class AudioProcessor:
"""Handles audio preprocessing and validation."""
@staticmethod
def get_audio_duration(audio_path: Path) -> float:
"""
Get the duration of an audio file in seconds.
Args:
audio_path: Path to the audio file
Returns:
Duration in seconds
"""
try:
with wave.open(str(audio_path), 'r') as audio_file:
frames = audio_file.getnframes()
rate = audio_file.getframerate()
duration = frames / float(rate)
return duration
except Exception as e:
logger.warning(f"Could not get audio duration: {e}")
return 0.0
@staticmethod
def validate_audio_file(audio_path: Path) -> bool:
"""
Validate that the audio file is readable and properly formatted.
Args:
audio_path: Path to the audio file
Returns:
True if valid, False otherwise
"""
if not audio_path.exists():
logger.error(f"Audio file does not exist: {audio_path}")
return False
if audio_path.stat().st_size == 0:
logger.error(f"Audio file is empty: {audio_path}")
return False
try:
with wave.open(str(audio_path), 'r') as audio_file:
# Check basic properties
channels = audio_file.getnchannels()
sample_width = audio_file.getsampwidth()
framerate = audio_file.getframerate()
logger.info(
f"Audio validation: {channels} channels, "
f"{sample_width} bytes/sample, {framerate} Hz"
)
return True
except Exception as e:
logger.error(f"Audio file validation failed: {e}")
return False
@staticmethod
def get_audio_info(audio_path: Path) -> dict:
"""
Get detailed information about an audio file.
Args:
audio_path: Path to the audio file
Returns:
Dictionary with audio properties
"""
try:
with wave.open(str(audio_path), 'r') as audio_file:
frames = audio_file.getnframes()
rate = audio_file.getframerate()
duration = frames / float(rate)
return {
'channels': audio_file.getnchannels(),
'sample_width': audio_file.getsampwidth(),
'framerate': rate,
'frames': frames,
'duration': duration,
'file_size': audio_path.stat().st_size,
}
except Exception as e:
logger.error(f"Failed to get audio info: {e}")
return {}
|