Added Opus encoder, WiP settings

This commit is contained in:
DataHoarder 2022-02-27 19:39:08 +01:00
parent 41d0fdde5d
commit c3d290f512
4 changed files with 113 additions and 3 deletions

View file

@ -294,6 +294,7 @@ func TestEncodeFLAC(t *testing.T) {
return
}
}
func TestEncodeMP3(t *testing.T) {
t.Parallel()
fp, err := os.Open(TestSingleSample24)
@ -327,6 +328,42 @@ func TestEncodeMP3(t *testing.T) {
}
}
func TestEncodeOpus(t *testing.T) {
t.Parallel()
fp, err := os.Open(TestSingleSample24)
if err != nil {
t.Error(err)
return
}
defer fp.Close()
source, err := flac.NewFormat().Open(fp)
if err != nil {
t.Error(err)
return
}
target, err := os.CreateTemp("/tmp", "encode_test_*.opus")
if err != nil {
t.Error(err)
return
}
defer func() {
name := target.Name()
target.Close()
os.Remove(name)
}()
options := make(map[string]interface{})
options["bitrate"] = "256k"
err = opus.NewFormat().Encode(audio.NewResampleFilter(opus.FixedSampleRate, audio.Linear, 0).Process(source), target, nil)
if err != nil {
t.Error(err)
return
}
}
func TestQueue(t *testing.T) {
t.Parallel()

View file

@ -7,6 +7,7 @@ package opus
import "C"
import (
"bytes"
"fmt"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
libopus "git.gammaspectra.live/S.O.N.G/go-pus"
@ -18,6 +19,7 @@ const BlockSize = 1024 * 128
type Format struct {
format.Format
format.Encoder
}
func NewFormat() Format {
@ -62,6 +64,77 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
}, nil
}
func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[string]interface{}) error {
if source.SampleRate != FixedSampleRate {
//TODO: maybe not needed
return fmt.Errorf("invalid source SampleRate: expected %d, got %d", FixedSampleRate, source.SampleRate)
}
var bitrate = 0
if options != nil {
var val interface{}
var ok bool
var intVal int
var strVal string
if val, ok = options["bitrate"]; ok {
if strVal, ok = val.(string); ok {
switch strVal {
case "auto":
bitrate = 0
case "256k":
bitrate = 256
case "192k":
bitrate = 192
case "128k":
bitrate = 128
default:
return fmt.Errorf("unknown setting bitrate=%s", strVal)
}
} else if intVal, ok = val.(int); ok {
bitrate = intVal
}
}
}
encoder, err := libopus.NewEncoder(FixedSampleRate, source.Channels, libopus.AppAudio, writer)
if err != nil {
return err
}
defer encoder.Close()
if bitrate == 0 {
err = encoder.SetBitrateToAuto()
if err != nil {
return err
}
} else {
//kbits to bits
err = encoder.SetBitrate(bitrate * 1024)
if err != nil {
return err
}
}
for block := range source.Blocks {
samples := make([]int16, len(block))
C.audio_float32_to_int16((*C.float)(&block[0]), C.size_t(len(block)), (*C.int16_t)(&samples[0]))
err = encoder.Encode(samples)
if err != nil {
return err
}
}
return nil
}
func (f Format) Identify(peek []byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus"
}

2
go.mod
View file

@ -3,7 +3,7 @@ module git.gammaspectra.live/S.O.N.G/Kirika
go 1.18
require (
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220130003320-c9b07c6bec7a
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
github.com/dh1tw/gosamplerate v0.1.2

4
go.sum
View file

@ -1,5 +1,5 @@
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220130003320-c9b07c6bec7a h1:LxrTp9gf4w5KnFHRPFLXYfoxC58GCSEmZrHI6Ogtrm0=
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220130003320-c9b07c6bec7a/go.mod h1:vkoHSHVM9p6vAUmXAik0gvaLcIfiQYrD6bQqVpOulUk=
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba h1:JEaxCVgdr3XXAuDCPAx7ttLFZaaHzTEzG+oRnVUtUKU=
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=