diff --git a/audio/format/aac/libfdk-aac.go b/audio/format/aac/libfdk-aac.go index 9285999..814ca7f 100644 --- a/audio/format/aac/libfdk-aac.go +++ b/audio/format/aac/libfdk-aac.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "git.gammaspectra.live/S.O.N.G/Kirika/audio" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" "git.gammaspectra.live/S.O.N.G/go-fdkaac/fdkaac" aacAdts "github.com/Eyevinn/mp4ff/aac" "github.com/Eyevinn/mp4ff/mp4" @@ -503,7 +504,7 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(_ []byte, extension string) bool { +func (f Format) Identify(_ [format.IdentifyPeekBytes]byte, extension string) bool { //TODO: add .m4a/mp4 detection return extension == "aac" || extension == "adts" } diff --git a/audio/format/aac/vo-aacenc.go b/audio/format/aac/vo-aacenc.go index b95d35c..c01ef4e 100644 --- a/audio/format/aac/vo-aacenc.go +++ b/audio/format/aac/vo-aacenc.go @@ -6,7 +6,6 @@ import ( "fmt" "git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/vector" - "git.gammaspectra.live/S.O.N.G/voaac-go" "io" ) @@ -93,6 +92,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return extension == "aac" || extension == "adts" } diff --git a/audio/format/alac/libalac.go b/audio/format/alac/libalac.go index 5dbb224..2326a2d 100644 --- a/audio/format/alac/libalac.go +++ b/audio/format/alac/libalac.go @@ -136,6 +136,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(_ []byte, extension string) bool { +func (f Format) Identify(_ [format.IdentifyPeekBytes]byte, extension string) bool { return extension == "alac" || extension == "mp4" || extension == "m4a" } diff --git a/audio/format/flac/goflac.go b/audio/format/flac/goflac.go index 7a35a8a..5f5fb81 100644 --- a/audio/format/flac/goflac.go +++ b/audio/format/flac/goflac.go @@ -224,7 +224,7 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { //No Ogg supported return bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "mp3") || extension == "flac" } diff --git a/audio/format/flac/libflac.go b/audio/format/flac/libflac.go index d03452d..a8ac7b2 100644 --- a/audio/format/flac/libflac.go +++ b/audio/format/flac/libflac.go @@ -161,6 +161,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return (bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 && extension != "opus" && extension != "vorbis") || bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "mp3") || extension == "flac" || extension == "ogg" } diff --git a/audio/format/format.go b/audio/format/format.go index 64fe048..5a48fae 100644 --- a/audio/format/format.go +++ b/audio/format/format.go @@ -5,9 +5,11 @@ import ( "io" ) +const IdentifyPeekBytes = 16 + type Format interface { // Identify checks whether a format is of a type. peek includes a few first bytes, extension is the lowercase file extension without a dot. - Identify(peek []byte, extension string) bool + Identify(peek [IdentifyPeekBytes]byte, extension string) bool // Name returns the name of the codec or format Name() string } diff --git a/audio/format/guess/guess.go b/audio/format/guess/guess.go index 2c1f35c..0334d7f 100644 --- a/audio/format/guess/guess.go +++ b/audio/format/guess/guess.go @@ -37,7 +37,7 @@ func (f NullFormat) Encode(audio.Source, io.WriteCloser, map[string]interface{}) return errors.New("null format") } -func (f NullFormat) Identify([]byte, string) bool { +func (f NullFormat) Identify([format.IdentifyPeekBytes]byte, string) bool { return false } @@ -103,8 +103,8 @@ func GetDecoders(r io.ReadSeekCloser, pathName string) (decoders []format.Decode if err != nil { return } - peek := make([]byte, 16) - if _, err = r.Read(peek); err != nil { + var peek [format.IdentifyPeekBytes]byte + if _, err = r.Read(peek[:]); err != nil { return } if _, err = r.Seek(startOffset, io.SeekStart); err != nil { diff --git a/audio/format/mp3/mp3.go b/audio/format/mp3/mp3.go index b380cca..f1414b8 100644 --- a/audio/format/mp3/mp3.go +++ b/audio/format/mp3/mp3.go @@ -5,6 +5,7 @@ package mp3 import ( "bytes" "git.gammaspectra.live/S.O.N.G/Kirika/audio" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" mp3Lib "git.gammaspectra.live/S.O.N.G/minimp3" "golang.org/x/exp/slices" "io" @@ -53,7 +54,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { return source, nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { //match ID3 / sync header return peek[0] == 0xff && (peek[1]>>5) == 0b111 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "flac") || extension == "mp3" } diff --git a/audio/format/mp3/mp3_nocgo.go b/audio/format/mp3/mp3_nocgo.go index dc41e84..d31c331 100644 --- a/audio/format/mp3/mp3_nocgo.go +++ b/audio/format/mp3/mp3_nocgo.go @@ -5,6 +5,7 @@ package mp3 import ( "bytes" "git.gammaspectra.live/S.O.N.G/Kirika/audio" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" mp3Lib "git.gammaspectra.live/S.O.N.G/go-mp3" "golang.org/x/exp/slices" "io" @@ -51,7 +52,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { return source, nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { //match ID3 / sync header return peek[0] == 0xff && (peek[1]>>5) == 0b111 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "flac") || extension == "mp3" } diff --git a/audio/format/opus/opus.go b/audio/format/opus/opus.go index 5fa80bd..6c78b04 100644 --- a/audio/format/opus/opus.go +++ b/audio/format/opus/opus.go @@ -8,6 +8,7 @@ import ( "fmt" "git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/audio/filter" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" libopus "git.gammaspectra.live/S.O.N.G/go-pus" "io" "time" @@ -177,6 +178,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus" || extension == "ogg" } diff --git a/audio/format/tta/tta.go b/audio/format/tta/tta.go index 87e33c2..ca2e45d 100644 --- a/audio/format/tta/tta.go +++ b/audio/format/tta/tta.go @@ -180,6 +180,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s return nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return bytes.Compare(peek[:4], []byte{'T', 'T', 'A', '1'}) == 0 || extension == "tta" } diff --git a/audio/format/vorbis/vorbis.go b/audio/format/vorbis/vorbis.go index e7ec795..d6f5944 100644 --- a/audio/format/vorbis/vorbis.go +++ b/audio/format/vorbis/vorbis.go @@ -6,6 +6,7 @@ import ( "bytes" "fmt" "git.gammaspectra.live/S.O.N.G/Kirika/audio" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" libvorbis "git.gammaspectra.live/S.O.N.G/go-vorbis" "io" ) @@ -111,6 +112,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg" } diff --git a/audio/format/vorbis/vorbis_nocgo.go b/audio/format/vorbis/vorbis_nocgo.go index 8fdcfa0..df66943 100644 --- a/audio/format/vorbis/vorbis_nocgo.go +++ b/audio/format/vorbis/vorbis_nocgo.go @@ -5,6 +5,7 @@ package vorbis import ( "bytes" "git.gammaspectra.live/S.O.N.G/Kirika/audio" + "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" libvorbis "github.com/jfreymuth/oggvorbis" "io" ) @@ -53,6 +54,6 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { return source, nil } -func (f Format) Identify(peek []byte, extension string) bool { +func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg" }