Spaces:
Build error
Build error
File size: 1,876 Bytes
5009cb8 |
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 |
"""AudioContent value object for representing audio data with validation."""
from dataclasses import dataclass
from typing import Optional
@dataclass(frozen=True)
class AudioContent:
"""Value object representing audio content with metadata and validation."""
data: bytes
format: str
sample_rate: int
duration: float
filename: Optional[str] = None
def __post_init__(self):
"""Validate audio content after initialization."""
self._validate()
def _validate(self):
"""Validate audio content properties."""
if not self.data:
raise ValueError("Audio data cannot be empty")
if not isinstance(self.data, bytes):
raise TypeError("Audio data must be bytes")
if self.format not in ['wav', 'mp3', 'flac', 'ogg']:
raise ValueError(f"Unsupported audio format: {self.format}. Supported formats: wav, mp3, flac, ogg")
if self.sample_rate <= 0:
raise ValueError("Sample rate must be positive")
if self.sample_rate < 8000 or self.sample_rate > 192000:
raise ValueError("Sample rate must be between 8000 and 192000 Hz")
if self.duration <= 0:
raise ValueError("Duration must be positive")
if self.duration > 3600: # 1 hour limit
raise ValueError("Audio duration cannot exceed 1 hour")
if self.filename is not None and not self.filename.strip():
raise ValueError("Filename cannot be empty string")
@property
def size_bytes(self) -> int:
"""Get the size of audio data in bytes."""
return len(self.data)
@property
def is_valid_format(self) -> bool:
"""Check if the audio format is valid."""
return self.format in ['wav', 'mp3', 'flac', 'ogg'] |