Spaces:
Sleeping
Sleeping
| """Audio format value object.""" | |
| from typing import List | |
| from dataclasses import dataclass | |
| from ..exceptions.domain_exceptions import InvalidAudioFormatError | |
| class AudioFormat: | |
| """Value object representing an audio format.""" | |
| value: str | |
| # Supported formats with their characteristics | |
| FORMATS = { | |
| 'mp3': {'extension': '.mp3', 'mime_type': 'audio/mpeg', 'lossy': True}, | |
| 'aac': {'extension': '.aac', 'mime_type': 'audio/aac', 'lossy': True}, | |
| 'wav': {'extension': '.wav', 'mime_type': 'audio/wav', 'lossy': False}, | |
| 'flac': {'extension': '.flac', 'mime_type': 'audio/flac', 'lossy': False}, | |
| 'm4a': {'extension': '.m4a', 'mime_type': 'audio/mp4', 'lossy': True}, | |
| 'ogg': {'extension': '.ogg', 'mime_type': 'audio/ogg', 'lossy': True} | |
| } | |
| def __post_init__(self): | |
| if self.value.lower() not in self.FORMATS: | |
| raise InvalidAudioFormatError(self.value, list(self.FORMATS.keys())) | |
| # Normalize to lowercase | |
| object.__setattr__(self, 'value', self.value.lower()) | |
| def extension(self) -> str: | |
| """Get file extension for this format.""" | |
| return self.FORMATS[self.value]['extension'] | |
| def mime_type(self) -> str: | |
| """Get MIME type for this format.""" | |
| return self.FORMATS[self.value]['mime_type'] | |
| def is_lossy(self) -> bool: | |
| """Check if format uses lossy compression.""" | |
| return self.FORMATS[self.value]['lossy'] | |
| def supported_formats(cls) -> List[str]: | |
| """Get list of supported formats.""" | |
| return list(cls.FORMATS.keys()) | |
| def __str__(self) -> str: | |
| return self.value |