Use golang.org/x/exp/slices for copy/clone/delete

This commit is contained in:
DataHoarder 2022-10-03 11:24:03 +02:00
parent f487b8f0b0
commit 3f6d1c1571
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
10 changed files with 41 additions and 52 deletions

View file

@ -10,6 +10,7 @@ import (
"git.gammaspectra.live/S.O.N.G/go-fdkaac/fdkaac" "git.gammaspectra.live/S.O.N.G/go-fdkaac/fdkaac"
aac_adts "github.com/edgeware/mp4ff/aac" aac_adts "github.com/edgeware/mp4ff/aac"
"github.com/edgeware/mp4ff/mp4" "github.com/edgeware/mp4ff/mp4"
"golang.org/x/exp/slices"
"io" "io"
"runtime" "runtime"
"time" "time"
@ -76,10 +77,8 @@ func tryDecodeFrame(decoder *fdkaac.AacDecoder) ([]int16, error) {
} }
if pcm != nil { if pcm != nil {
out := make([]int16, len(pcm)/2) defer runtime.KeepAlive(pcm)
copy(out, unsafe.Slice((*int16)(unsafe.Pointer(&pcm[0])), len(pcm)/2)) return slices.Clone(unsafe.Slice((*int16)(unsafe.Pointer(&pcm[0])), len(pcm)/2)), nil
runtime.KeepAlive(pcm)
return out, nil
} }
return nil, nil return nil, nil

View file

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/audio"
mp3Lib "github.com/kvark128/minimp3" mp3Lib "github.com/kvark128/minimp3"
"golang.org/x/exp/slices"
"io" "io"
"unsafe" "unsafe"
) )
@ -48,9 +49,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
} }
n /= SizeofInt16 n /= SizeofInt16
buf := make([]int16, n) source.IngestInt16(slices.Clone(samples[:n]), 16)
copy(buf, samples[:n])
source.IngestInt16(buf, 16)
} }
}() }()

View file

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/audio"
mp3Lib "github.com/hajimehoshi/go-mp3" mp3Lib "github.com/hajimehoshi/go-mp3"
"golang.org/x/exp/slices"
"io" "io"
"unsafe" "unsafe"
) )
@ -46,9 +47,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
} }
n /= SizeofInt16 n /= SizeofInt16
buf := make([]int16, n) source.IngestInt16(slices.Clone(samples[:n]), 16)
copy(buf, samples[:n])
source.IngestInt16(buf, 16)
} }
}() }()

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/icza/bitio" "github.com/icza/bitio"
"golang.org/x/exp/slices"
"io" "io"
) )
@ -437,10 +438,7 @@ func readFlacFrame(hdr *FLACHeaderPacket, br *bitio.Reader, bfr *cachedBitReader
br.Align() br.Align()
buf := bfr.GetBuffer() p.FrameData = slices.Clone(bfr.GetBuffer())
p.FrameData = make([]byte, len(buf))
copy(p.FrameData, buf)
if p.CRC16, err = br.ReadBits(16); err != nil { if p.CRC16, err = br.ReadBits(16); err != nil {
return nil return nil

View file

@ -3,6 +3,7 @@ package queue
import ( import (
"git.gammaspectra.live/S.O.N.G/Kirika/audio" "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/filter"
"golang.org/x/exp/slices"
"log" "log"
"runtime" "runtime"
"sync" "sync"
@ -222,17 +223,17 @@ func (q *Queue) Remove(identifier Identifier) bool {
q.lock.Lock() q.lock.Lock()
defer q.lock.Unlock() defer q.lock.Unlock()
for i, e := range q.queue { if i := slices.IndexFunc(q.queue, func(e *Entry) bool {
if e.Identifier == identifier { return e.Identifier == identifier
e.Cancel() }); i != -1 {
e := q.queue[i]
e.Cancel()
e.Source.Unlock() e.Source.Unlock()
go audio.NewNullSink().Process(e.Source) go audio.NewNullSink().Process(e.Source)
//delete entry slices.Delete(q.queue, i, i+1)
q.queue = append(q.queue[:i], q.queue[i+1:]...) entry = e
entry = e
}
} }
}() }()
@ -295,10 +296,7 @@ func (q *Queue) GetQueue() (entries []*Entry) {
q.lock.RLock() q.lock.RLock()
defer q.lock.RUnlock() defer q.lock.RUnlock()
entries = make([]*Entry, len(q.queue)) return slices.Clone(q.queue)
copy(entries, q.queue)
return
} }
func (q *Queue) GetSource() audio.Source { func (q *Queue) GetSource() audio.Source {

View file

@ -2,6 +2,7 @@ package audio
import ( import (
"errors" "errors"
"golang.org/x/exp/slices"
"runtime" "runtime"
"unsafe" "unsafe"
) )
@ -106,9 +107,7 @@ func Ingest(s Source, buf interface{}, bitDepth int) error {
nsamples := len(bufferSlice) / (bitDepth / 8) nsamples := len(bufferSlice) / (bitDepth / 8)
switch bitDepth { switch bitDepth {
case 32: case 32:
out := make([]int32, nsamples) s.IngestInt32(slices.Clone(unsafe.Slice((*int32)(unsafe.Pointer(&bufferSlice[0])), nsamples)), bitDepth)
copy(out, unsafe.Slice((*int32)(unsafe.Pointer(&bufferSlice[0])), nsamples))
s.IngestInt32(out, bitDepth)
runtime.KeepAlive(bufferSlice) runtime.KeepAlive(bufferSlice)
return nil return nil
case 24: case 24:
@ -116,9 +115,7 @@ func Ingest(s Source, buf interface{}, bitDepth int) error {
runtime.KeepAlive(bufferSlice) runtime.KeepAlive(bufferSlice)
return nil return nil
case 16: case 16:
out := make([]int16, nsamples) s.IngestInt16(slices.Clone(unsafe.Slice((*int16)(unsafe.Pointer(&bufferSlice[0])), nsamples)), bitDepth)
copy(out, unsafe.Slice((*int16)(unsafe.Pointer(&bufferSlice[0])), nsamples))
s.IngestInt16(out, bitDepth)
runtime.KeepAlive(bufferSlice) runtime.KeepAlive(bufferSlice)
return nil return nil
case 8: case 8:

3
go.mod
View file

@ -22,6 +22,7 @@ require (
github.com/oov/audio v0.0.0-20171004131523-88a2be6dbe38 github.com/oov/audio v0.0.0-20171004131523-88a2be6dbe38
github.com/sssgun/mp3 v0.0.0-20170810093403-85f2ec632081 github.com/sssgun/mp3 v0.0.0-20170810093403-85f2ec632081
github.com/viert/go-lame v0.0.0-20201108052322-bb552596b11d github.com/viert/go-lame v0.0.0-20201108052322-bb552596b11d
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08
) )
require ( require (
@ -30,5 +31,5 @@ require (
github.com/klauspost/cpuid/v2 v2.1.1 // indirect github.com/klauspost/cpuid/v2 v2.1.1 // indirect
github.com/mewkiz/pkg v0.0.0-20220820102221-bbbca16e2a6c // indirect github.com/mewkiz/pkg v0.0.0-20220820102221-bbbca16e2a6c // indirect
github.com/youpy/go-wav v0.3.2 // indirect github.com/youpy/go-wav v0.3.2 // indirect
golang.org/x/sys v0.0.0-20220913175220-63ea55921009 // indirect golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
) )

6
go.sum
View file

@ -67,6 +67,8 @@ github.com/youpy/go-wav v0.3.2/go.mod h1:0FCieAXAeSdcxFfwLpRuEo0PFmAoc+8NU34h7TU
github.com/zaf/g711 v0.0.0-20190814101024-76a4a538f52b h1:QqixIpc5WFIqTLxB3Hq8qs0qImAgBdq0p6rq2Qdl634= github.com/zaf/g711 v0.0.0-20190814101024-76a4a538f52b h1:QqixIpc5WFIqTLxB3Hq8qs0qImAgBdq0p6rq2Qdl634=
github.com/zaf/g711 v0.0.0-20190814101024-76a4a538f52b/go.mod h1:T2h1zV50R/q0CVYnsQOQ6L7P4a2ZxH47ixWcMXFGyx8= github.com/zaf/g711 v0.0.0-20190814101024-76a4a538f52b/go.mod h1:T2h1zV50R/q0CVYnsQOQ6L7P4a2ZxH47ixWcMXFGyx8=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08 h1:LtBIgSqNhkuC9gA3BFjGy5obHQT1lnmNsMDFSqWzQ5w=
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
@ -74,8 +76,8 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220913175220-63ea55921009 h1:PuvuRMeLWqsf/ZdT1UUZz0syhioyv1mzuFZsXs4fvhw= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
golang.org/x/sys v0.0.0-20220913175220-63ea55921009/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View file

@ -4,6 +4,7 @@ package vector
import ( import (
"encoding/binary" "encoding/binary"
"golang.org/x/exp/slices"
"math" "math"
"runtime" "runtime"
"unsafe" "unsafe"
@ -42,7 +43,7 @@ func MultipleChannelsToStereoFloat32(buffer []float32, channels int) (buf []floa
} }
break break
case 2: //copy case 2: //copy
copy(buf, buffer) buf = slices.Clone(buffer)
break break
case 3: //2.1, FL, FR, LFE case 3: //2.1, FL, FR, LFE
for i := 0; i < len(buffer); i += 3 { for i := 0; i < len(buffer); i += 3 {
@ -119,7 +120,7 @@ func MultipleChannelsToStereoInt32(buffer []int32, channels int) (buf []int32) {
} }
break break
case 2: //copy case 2: //copy
copy(buf, buffer) return slices.Clone(buffer)
break break
case 3: //2.1, FL, FR, LFE case 3: //2.1, FL, FR, LFE
for i := 0; i < len(buffer); i += 3 { for i := 0; i < len(buffer); i += 3 {
@ -207,7 +208,7 @@ func Int32ToBytes(data []int32, bitDepth int) (buf []byte) {
} }
break break
case 32: case 32:
copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*4)) buf = slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*4))
runtime.KeepAlive(data) runtime.KeepAlive(data)
break break
} }
@ -247,7 +248,7 @@ func BytesToInt32(data []byte, bitDepth int) (buf []int32) {
} }
break break
case 32: case 32:
copy(buf, unsafe.Slice((*int32)(unsafe.Pointer(&data[0])), len(data)/4)) buf = slices.Clone(unsafe.Slice((*int32)(unsafe.Pointer(&data[0])), len(data)/4))
runtime.KeepAlive(data) runtime.KeepAlive(data)
break break
} }

View file

@ -1,27 +1,22 @@
package vector package vector
import ( import (
"golang.org/x/exp/slices"
"runtime" "runtime"
"unsafe" "unsafe"
) )
func Int8ToBytes(data []int8) (buf []byte) { func Int8ToBytes(data []int8) (buf []byte) {
buf = make([]byte, len(data)) defer runtime.KeepAlive(data)
copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data))) return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)))
runtime.KeepAlive(data)
return buf
} }
func Int16ToBytes(data []int16) (buf []byte) { func Int16ToBytes(data []int16) (buf []byte) {
buf = make([]byte, len(data)*2) defer runtime.KeepAlive(data)
copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*2)) return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*2))
runtime.KeepAlive(data)
return buf
} }
func Float32ToBytes(data []float32) (buf []byte) { func Float32ToBytes(data []float32) (buf []byte) {
buf = make([]byte, len(data)*4) defer runtime.KeepAlive(data)
copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*4)) return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&data[0])), len(data)*4))
runtime.KeepAlive(data)
return buf
} }