Michael Hu
refactor based on DDD
5009cb8
"""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']