Added support for FLAC in Ogg decoding
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2022-03-05 10:42:30 +01:00
parent f1e808b8f1
commit 96cfa831a0
5 changed files with 20 additions and 8 deletions

View file

@ -14,7 +14,7 @@ Collection of audio utilities for decoding/encoding files and streams.
| Codec | Container | Decoder | Analyzer | Encoder | Notes |
|:--------:|:---------:|:-------:|:--------:|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------------------|
| **FLAC** | FLAC | ✅ | ✅ | ✅ | Adjustable encoding compression level and block size. |
| **FLAC** | FLAC, Ogg | ✅ | ✅ | ✅ | Adjustable encoding compression level and block size. |
| **TTA** | TTA | ✅ | ✅ | ✅ | Only 16bit encoding |
| **MP3** | MP3 | ✅ | - | ✅ | Adjustable encoding bitrate and mode. Decoding via [minimp3](https://github.com/kvark128/minimp3), encoding via [LAME](https://lame.sourceforge.io/). |
| **Opus** | Ogg | ✅ | - | ✅ | Adjustable encoding bitrate. |

View file

@ -21,9 +21,15 @@ func NewFormat() Format {
}
func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
currentPosition, _ := r.Seek(0, io.SeekCurrent)
decoder, err := libflac.NewDecoderReader(r)
if err != nil {
return audio.Source{}, err
if _, err2 := r.Seek(currentPosition, io.SeekStart); err2 != nil {
return audio.Source{}, err
}
if decoder, err = libflac.NewDecoderReaderOgg(r); err != nil {
return audio.Source{}, err
}
}
newChannel := make(chan []float32)
@ -64,9 +70,15 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
}
func (f Format) OpenAnalyzer(r io.ReadSeekCloser) (audio.Source, format.AnalyzerChannel, error) {
currentPosition, _ := r.Seek(0, io.SeekCurrent)
decoder, err := libflac.NewDecoderReader(r)
if err != nil {
return audio.Source{}, nil, err
if _, err2 := r.Seek(currentPosition, io.SeekStart); err2 != nil {
return audio.Source{}, nil, err
}
if decoder, err = libflac.NewDecoderReaderOgg(r); err != nil {
return audio.Source{}, nil, err
}
}
newChannel := make(chan []float32)
@ -187,5 +199,5 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
}
func (f Format) Identify(peek []byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || extension == "flac"
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || extension == "flac" || extension == "ogg"
}

View file

@ -132,5 +132,5 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
}
func (f Format) Identify(peek []byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus"
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus" || extension == "ogg"
}

2
go.mod
View file

@ -6,7 +6,7 @@ require (
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220228131722-e9cb84c52f48
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220223152921-827e6c3f729f
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566
github.com/dh1tw/gosamplerate v0.1.2
github.com/edgeware/mp4ff v0.26.1
github.com/kvark128/minimp3 v0.0.0-20211109174940-101188771a65

4
go.sum
View file

@ -4,8 +4,8 @@ git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba h1:JEaxC
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba/go.mod h1:vkoHSHVM9p6vAUmXAik0gvaLcIfiQYrD6bQqVpOulUk=
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6 h1:ITVVisbHPnUclp3PBkCbXFeBhOCBcOjPdgjJ9wRH3TI=
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6/go.mod h1:cobkT8u8vq/+ngLy+feKS2M2ZT2HoCec5riA/0Cex3Q=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220223152921-827e6c3f729f h1:4Dkx1l5Ex7pG/Xbs57L4IQd7mBgd6TO5rhP0BKP9PiI=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220223152921-827e6c3f729f/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566 h1:nhnwjyaAydpSU3UADA9BRJmwpmJ8UlffxvBDuHC1T+8=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs=
github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64 h1:LjPYdzoFSAJ5Tr/ElL8kzTJghXgpnOjJVbgd1UvZB1o=
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
github.com/dh1tw/gosamplerate v0.1.2 h1:oyqtZk67xB9B4l+vIZCZ3F0RYV/z66W58VOah11/ktI=