From afecfbb4a70d2eb4459b4cc3efcad3a8bfa6c838 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:32:27 +0100 Subject: [PATCH] Use float32 native output on mp3 decoders --- README.md | 22 +++++++++++----------- audio/format/mp3/mp3.go | 13 +++++-------- audio/format/mp3/mp3_nocgo.go | 15 ++++++--------- go.mod | 4 ++-- go.sum | 10 ++++------ 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index c535072..cf9007a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Collection of audio utilities for decoding/encoding/processing files and streams |:----------:|:----------------------------------------------------------------------------------------:|:-------:|:---------------:|:-------------:|:-------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | **FLAC** | [FLAC](https://xiph.org/flac/format.html), [Ogg](https://xiph.org/flac/ogg_mapping.html) | ✅ | ✅ | `int32` | ✅ | Adjustable encoding compression level and block size.
Decoding/encoding by [libFLAC](https://github.com/xiph/flac) via [goflac](https://git.gammaspectra.live/S.O.N.G/goflac).
If [goflac](https://git.gammaspectra.live/S.O.N.G/goflac) codec is disabled, [flacgo](https://git.gammaspectra.live/S.O.N.G/flacgo) decoder/encoder will be used. | | **TTA** | [TTA](https://www.tausoft.org/en/true_audio_codec_format/) | ✅ | ✅ | `int32` | ✅ | Decoding/encoding via [S.O.N.G/go-tta](https://git.gammaspectra.live/S.O.N.G/go-tta). | -| **MP3** | [MP3](http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html) | ✅ | ❌ | `int16` | ✅ | Adjustable encoding bitrate and mode.
Decoding via [S.O.N.G/minimp3](https://git.gammaspectra.live/S.O.N.G/minimp3), encoding by [LAME](https://lame.sourceforge.io/) via [go-lame](https://github.com/viert/go-lame). | +| **MP3** | [MP3](http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html) | ✅ | ❌ | `float32` | ✅ | Adjustable encoding bitrate and mode.
Decoding via [S.O.N.G/minimp3](https://git.gammaspectra.live/S.O.N.G/minimp3), encoding by [LAME](https://lame.sourceforge.io/) via [go-lame](https://github.com/viert/go-lame). | | **Opus** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | ❌ | `float32` | ✅ | Adjustable encoding bitrate.
Decoding/encoding by [libopus](https://github.com/xiph/opus) via [go-pus](https://git.gammaspectra.live/S.O.N.G/go-pus).
Linked Ogg streams of different channel count are not supported. | | **Vorbis** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | ❌ | `float32` | ✅ | Decoding/encoding by [libvorbis](https://github.com/xiph/vorbis) via [go-vorbis](https://git.gammaspectra.live/S.O.N.G/go-vorbis).
If [go-vorbis](https://git.gammaspectra.live/S.O.N.G/go-vorbis) is disabled, [jfreymuth/vorbis](https://github.com/jfreymuth/vorbis) via [jfreymuth/oggvorbis](https://github.com/jfreymuth/oggvorbis) decoder will be used.
Linked Ogg streams of different channel count are not supported. | | **AAC** | [ADTS](https://wiki.multimedia.cx/index.php/ADTS), ADIF*, MP4** | ✅ | ❌ | `int16` | ✅ | Adjustable encoding bitrate and mode (LC, HEv1, HEv2).
Decoding/encoding by [FDK-AAC](https://github.com/mstorsjo/fdk-aac) via [go-fdkaac](https://git.gammaspectra.live/S.O.N.G/go-fdkaac).
If [go-fdkaac](https://git.gammaspectra.live/S.O.N.G/go-fdkaac) codec is disabled, [VisualOn AAC encoder](https://github.com/gen2brain/aac-go) will be used for limited encoding support.
*ADIF only supported on encoding.
**MP4 only supported on encoding, and fragmented MP4 currently. | @@ -149,15 +149,15 @@ Some tests cannot be completed in this mode. #### Codecs supported, CGO_ENABLED=0 -| Codec | Containers | Decoder | Sample Analyzer | Sample Format | Encoder | Notes | -|:----------:|:------------------------------------------------------------:|:-------:|:---------------:|:-------------:|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| **FLAC** | [FLAC](https://xiph.org/flac/format.html) | ✅ | ✅ | `int32` | ✅* | Decoding/encoding by [flacgo](https://git.gammaspectra.live/S.O.N.G/flacgo).
*Encoder only supports Verbatim / Constant frame prediction modes, offering almost no compression. | -| **TTA** | [TTA](https://www.tausoft.org/en/true_audio_codec_format/) | ✅ | ✅ | `int32` | ✅ | Decoding/encoding by [S.O.N.G/go-tta](https://git.gammaspectra.live/S.O.N.G/go-tta). | -| **MP3** | [MP3](http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html) | ✅ | ❌ | `2ch. int16` | ❌ | Decoding by [hajimehoshi/go-mp3](https://github.com/hajimehoshi/go-mp3). | -| **Opus** | - | ❌ | - | - | ❌ | | -| **Vorbis** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | ❌ | `float32` | ❌ | Decoding by [jfreymuth/vorbis](https://github.com/jfreymuth/vorbis) via [jfreymuth/oggvorbis](https://github.com/jfreymuth/oggvorbis). | -| **AAC** | - | ❌ | - | - | ❌ | | -| **ALAC** | - | ❌ | - | - | ❌ | | +| Codec | Containers | Decoder | Sample Analyzer | Sample Format | Encoder | Notes | +|:----------:|:------------------------------------------------------------:|:-------:|:---------------:|:--------------:|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| **FLAC** | [FLAC](https://xiph.org/flac/format.html) | ✅ | ✅ | `int32` | ✅* | Decoding/encoding by [flacgo](https://git.gammaspectra.live/S.O.N.G/flacgo).
*Encoder only supports Verbatim / Constant frame prediction modes, offering almost no compression. | +| **TTA** | [TTA](https://www.tausoft.org/en/true_audio_codec_format/) | ✅ | ✅ | `int32` | ✅ | Decoding/encoding by [S.O.N.G/go-tta](https://git.gammaspectra.live/S.O.N.G/go-tta). | +| **MP3** | [MP3](http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html) | ✅ | ❌ | `2ch. float32` | ❌ | Decoding by [S.O.N.G/go-mp3](https://git.gammaspectra.live/S.O.N.G/go-mp3). | +| **Opus** | - | ❌ | - | - | ❌ | | +| **Vorbis** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | ❌ | `float32` | ❌ | Decoding by [jfreymuth/vorbis](https://github.com/jfreymuth/vorbis) via [jfreymuth/oggvorbis](https://github.com/jfreymuth/oggvorbis). | +| **AAC** | - | ❌ | - | - | ❌ | | +| **ALAC** | - | ❌ | - | - | ❌ | | ### disable_format_[format] @@ -242,7 +242,7 @@ Subdependencies that are not cgo-based are denoted in cursive. | [edgeware/mp4ff](https://github.com/edgeware/mp4ff) | Go | [MIT](https://github.com/edgeware/mp4ff/blob/master/LICENSE.md) | | | [aam335/aac-go](https://github.com/aam335/aac-go) | Go | [Apache 2.0](https://github.com/aam335/aac-go/blob/master/COPYING) | | | [mstorsjo/vo-aacenc](https://github.com/mstorsjo/vo-aacenc) | C | [Apache 2.0](https://github.com/mstorsjo/vo-aacenc/blob/master/COPYING) | Subdependency and included as part of _gen2brain/aac-go_. | -| [hajimehoshi/go-mp3](https://github.com/hajimehoshi/go-mp3) | Go | [Apache 2.0](https://github.com/hajimehoshi/go-mp3/blob/main/LICENSE) | | +| [S.O.N.G/go-mp3](https://git.gammaspectra.live/S.O.N.G/go-mp3) | Go | [Apache 2.0](https://git.gammaspectra.live/S.O.N.G/go-mp3/src/branch/main/LICENSE) | | | [jfreymuth/oggvorbis](https://github.com/jfreymuth/oggvorbis) | Go | [MIT](https://github.com/jfreymuth/oggvorbis/blob/master/LICENSE) | | | _[jfreymuth/vorbis](https://github.com/jfreymuth/vorbis)_ | Go | [MIT](https://github.com/jfreymuth/vorbis/blob/master/LICENSE) | Subdependency of _jfreymuth/oggvorbis_. | | [S.O.N.G/minimp3](https://git.gammaspectra.live/S.O.N.G/minimp3) | Go | [MIT](https://git.gammaspectra.live/S.O.N.G/minimp3/src/branch/master/LICENSE.txt) | | diff --git a/audio/format/mp3/mp3.go b/audio/format/mp3/mp3.go index a80cc0f..b380cca 100644 --- a/audio/format/mp3/mp3.go +++ b/audio/format/mp3/mp3.go @@ -8,7 +8,6 @@ import ( mp3Lib "git.gammaspectra.live/S.O.N.G/minimp3" "golang.org/x/exp/slices" "io" - "unsafe" ) const BlockSize = 1024 * 128 @@ -31,25 +30,23 @@ func (f Format) DecoderDescription() string { func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { decoder := mp3Lib.NewDecoder(r) - _, err := decoder.Read([]byte{}) + _, err := decoder.Read([]float32{}) if err != nil { return nil, err } - source := audio.NewSource[int16](16, decoder.SampleRate(), decoder.Channels()) + source := audio.NewSource[float32](16, decoder.SampleRate(), decoder.Channels()) go func() { defer source.Close() - samples := make([]int16, BlockSize*decoder.Channels()) - const SizeofInt16 = int(unsafe.Sizeof(int16(0))) + samples := make([]float32, BlockSize*decoder.Channels()) for { - n, err := decoder.Read(unsafe.Slice((*byte)(unsafe.Pointer(&samples[0])), len(samples)*SizeofInt16)) + n, err := decoder.Read(samples) if err != nil { return } - n /= SizeofInt16 - source.IngestInt16(slices.Clone(samples[:n]), 16) + source.IngestFloat32(slices.Clone(samples[:n])) } }() diff --git a/audio/format/mp3/mp3_nocgo.go b/audio/format/mp3/mp3_nocgo.go index 56cd96b..dc41e84 100644 --- a/audio/format/mp3/mp3_nocgo.go +++ b/audio/format/mp3/mp3_nocgo.go @@ -5,10 +5,9 @@ package mp3 import ( "bytes" "git.gammaspectra.live/S.O.N.G/Kirika/audio" - mp3Lib "github.com/hajimehoshi/go-mp3" + mp3Lib "git.gammaspectra.live/S.O.N.G/go-mp3" "golang.org/x/exp/slices" "io" - "unsafe" ) const BlockSize = 1024 * 128 @@ -25,7 +24,7 @@ func (f Format) Name() string { } func (f Format) DecoderDescription() string { - return "hajimehoshi/go-mp3" + return "S.O.N.G/go-mp3" } func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { @@ -34,20 +33,18 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) { return nil, err } - source := audio.NewSource[int16](16, decoder.SampleRate(), 2) + source := audio.NewSource[float32](16, decoder.SampleRate(), 2) go func() { defer source.Close() - samples := make([]int16, BlockSize*2) - const SizeofInt16 = int(unsafe.Sizeof(int16(0))) + samples := make([]float32, BlockSize*2) for { - n, err := decoder.Read(unsafe.Slice((*byte)(unsafe.Pointer(&samples[0])), len(samples)*SizeofInt16)) + n, err := decoder.ReadFloat(samples) if err != nil { return } - n /= SizeofInt16 - source.IngestInt16(slices.Clone(samples[:n]), 16) + source.IngestFloat32(slices.Clone(samples[:n])) } }() diff --git a/go.mod b/go.mod index dd44f7a..056021b 100644 --- a/go.mod +++ b/go.mod @@ -7,15 +7,15 @@ require ( git.gammaspectra.live/S.O.N.G/go-alac v0.0.0-20220421115623-d0b3bfe57e0f git.gammaspectra.live/S.O.N.G/go-ebur128 v0.0.0-20220720163421-db0c1911921d git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220910135048-823922bd661a + git.gammaspectra.live/S.O.N.G/go-mp3 v0.0.0-20221201092229-bb1d87b2e695 git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220721130634-3d5460c48ab6 git.gammaspectra.live/S.O.N.G/go-tta v0.0.0-20220226150007-096de1072bd6 git.gammaspectra.live/S.O.N.G/go-vorbis v0.0.0-20220728124510-303b3425eec0 git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220515172202-6e490998d2a0 - git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221029081019-2dbf75f65b35 + git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221201085504-9103da68c320 github.com/aam335/aac-go v0.0.0-20200408070016-52c23bd38988 github.com/dh1tw/gosamplerate v0.1.2 github.com/edgeware/mp4ff v0.30.1 - github.com/hajimehoshi/go-mp3 v0.3.4 github.com/icza/bitio v1.1.0 github.com/jfreymuth/oggvorbis v1.0.4 github.com/minio/sha256-simd v1.0.0 diff --git a/go.sum b/go.sum index 119f40a..daf9ec8 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ git.gammaspectra.live/S.O.N.G/go-ebur128 v0.0.0-20220720163421-db0c1911921d h1:3 git.gammaspectra.live/S.O.N.G/go-ebur128 v0.0.0-20220720163421-db0c1911921d/go.mod h1:5H4eVW9uknpn8REFr+C3ejhvXdncgm/pbGqKGC43gFY= git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220910135048-823922bd661a h1:JQaryqLtNZn1jydhVc7CzcAvO8ZNB4jeZ1B5gJbHWUU= git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220910135048-823922bd661a/go.mod h1:pkWt//S9hLVEQaJDPu/cHHPk8vPpo/0+zHy0me4LIP4= +git.gammaspectra.live/S.O.N.G/go-mp3 v0.0.0-20221201092229-bb1d87b2e695 h1:9qlyvCKDMQfGXEnn9AOkr/Mvt1KQnrDOK6ts+m/pBbo= +git.gammaspectra.live/S.O.N.G/go-mp3 v0.0.0-20221201092229-bb1d87b2e695/go.mod h1:rnqZmb8AWghEB5bi921F6oNYlMCH54X50ft2KPwRHxs= git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220721130634-3d5460c48ab6 h1:xWv02pzrOrQDtNGgcjDw9ZZvJCAuxLbZ6+h5QVVyqvk= git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220721130634-3d5460c48ab6/go.mod h1:vkoHSHVM9p6vAUmXAik0gvaLcIfiQYrD6bQqVpOulUk= git.gammaspectra.live/S.O.N.G/go-tta v0.0.0-20220226150007-096de1072bd6 h1:pGzSqhUqrhNDJ8Zz/Oh8zXYOciQ+xZ+28S6K3RpWqus= @@ -14,8 +16,8 @@ git.gammaspectra.live/S.O.N.G/go-vorbis v0.0.0-20220728124510-303b3425eec0 h1:kZ git.gammaspectra.live/S.O.N.G/go-vorbis v0.0.0-20220728124510-303b3425eec0/go.mod h1:EZl7z0vfpaiu0ykpEkk6dh59XxBgWxAh4QPCCnkhICE= git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220515172202-6e490998d2a0 h1:imcnwHUqaAJzws41B8sCSp/sUmVranNjAX205Jr4Jc0= git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220515172202-6e490998d2a0/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs= -git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221029081019-2dbf75f65b35 h1:CuJroJbfRxrXtT2+sAOO3EYuOiC0XcF9Nzo/GUOD0PM= -git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221029081019-2dbf75f65b35/go.mod h1:B34pwapfc0f6be6rJYg37xDeFPPB0bSyH0+DYM1lyEE= +git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221201085504-9103da68c320 h1:rsu7pz3LvRFFMtruaYBUBhE/8ZCJU2Kx30xPUuQMIF0= +git.gammaspectra.live/S.O.N.G/minimp3 v0.0.0-20221201085504-9103da68c320/go.mod h1:B34pwapfc0f6be6rJYg37xDeFPPB0bSyH0+DYM1lyEE= github.com/aam335/aac-go v0.0.0-20200408070016-52c23bd38988 h1:DZeJTINv0mpfBVfDtQz9vWOn/Uwj3IiA9uP5UJnClZE= github.com/aam335/aac-go v0.0.0-20200408070016-52c23bd38988/go.mod h1:JBA06GZmW2MVVBYRrKuaZ2AFMSDiPmafb/qmF/ATTUM= github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64 h1:LjPYdzoFSAJ5Tr/ElL8kzTJghXgpnOjJVbgd1UvZB1o= @@ -28,9 +30,6 @@ github.com/edgeware/mp4ff v0.30.1/go.mod h1:GNUeA6tEFksH2CrjJF2FSGdJolba8yPGmo16 github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= -github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= -github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0= github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A= github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k= @@ -69,7 +68,6 @@ golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZ golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=