61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package audio
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// AudioFormat represents a supported audio format
|
|
type AudioFormat int
|
|
|
|
const (
|
|
FormatUnknown AudioFormat = iota
|
|
FormatMP3
|
|
FormatWAV
|
|
FormatFLAC
|
|
FormatOGG
|
|
)
|
|
|
|
// DetectFormat returns the audio format based on file extension
|
|
func DetectFormat(path string) AudioFormat {
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
switch ext {
|
|
case ".mp3":
|
|
return FormatMP3
|
|
case ".wav":
|
|
return FormatWAV
|
|
case ".flac":
|
|
return FormatFLAC
|
|
case ".ogg":
|
|
return FormatOGG
|
|
default:
|
|
return FormatUnknown
|
|
}
|
|
}
|
|
|
|
// String returns the format name
|
|
func (f AudioFormat) String() string {
|
|
switch f {
|
|
case FormatMP3:
|
|
return "MP3"
|
|
case FormatWAV:
|
|
return "WAV"
|
|
case FormatFLAC:
|
|
return "FLAC"
|
|
case FormatOGG:
|
|
return "OGG"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|
|
|
|
// ValidateFormat checks if the file format is supported
|
|
func ValidateFormat(path string) error {
|
|
format := DetectFormat(path)
|
|
if format == FormatUnknown {
|
|
return fmt.Errorf("unsupported audio format: %s", filepath.Ext(path))
|
|
}
|
|
return nil
|
|
}
|