- Fix 10 err113 violations: extract dynamic errors to package-level sentinels - Fix 12 errcheck violations: handle unchecked error returns in player, metadata, and config packages - Fix 4 revive stutter warnings: rename player.PlayerState to player.State, player.PlayerVolume to player.Volume, queue.QueueTrack to queue.Track, queue.QueueState to queue.State - Fix 2 staticcheck SA4001: simplify *&x to x in assets handler - Fix 5 unused constants: remove dead AudioFileType iota block in models - Fix gci/gofumpt/wsl formatting issues across multiple files - Add gofumpt module-path setting to .golangci.yml for correct import grouping - Fix player test: gate integration test behind YELLOWJACKET_INTEGRATION env var instead of only skipping in CI, and replace t.Errorf+t.Failed with t.Fatalf - Remove continue-on-error from golangci-lint CI step so linting is now required
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package metadata
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// AudioFileExtension represents a supported audio file extension.
|
|
type AudioFileExtension string
|
|
|
|
// Supported audio file extensions.
|
|
const (
|
|
MP3 AudioFileExtension = ".mp3"
|
|
FLAC AudioFileExtension = ".flac"
|
|
OGG AudioFileExtension = ".ogg"
|
|
WAV AudioFileExtension = ".wav"
|
|
)
|
|
|
|
// SupportedFileExtensions lists all supported audio formats.
|
|
var SupportedFileExtensions = []AudioFileExtension{MP3, FLAC, OGG, WAV}
|
|
|
|
// GetSupportedFileType checks if a file extension is supported.
|
|
func GetSupportedFileType(ext string) (AudioFileExtension, bool) {
|
|
for _, supported := range SupportedFileExtensions {
|
|
if string(supported) == ext {
|
|
return supported, true
|
|
}
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
// GetTrackLengthMillis returns the duration of an audio file in milliseconds.
|
|
func GetTrackLengthMillis(path string) (int64, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("could not open file: %w", err)
|
|
}
|
|
|
|
streamer, format, err := DecodeFile(f)
|
|
if err != nil {
|
|
_ = f.Close()
|
|
|
|
return 0, fmt.Errorf("error decoding file: %w", err)
|
|
}
|
|
|
|
lengthMillis := int64(float64(streamer.Len()*1000) / float64(format.SampleRate))
|
|
_ = streamer.Close()
|
|
_ = f.Close()
|
|
|
|
return lengthMillis, nil
|
|
}
|