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"
aac_adts "github.com/edgeware/mp4ff/aac"
"github.com/edgeware/mp4ff/mp4"
"golang.org/x/exp/slices"
"io"
"runtime"
"time"
@ -76,10 +77,8 @@ func tryDecodeFrame(decoder *fdkaac.AacDecoder) ([]int16, error) {
}
if pcm != nil {
out := make([]int16, len(pcm)/2)
copy(out, unsafe.Slice((*int16)(unsafe.Pointer(&pcm[0])), len(pcm)/2))
runtime.KeepAlive(pcm)
return out, nil
defer runtime.KeepAlive(pcm)
return slices.Clone(unsafe.Slice((*int16)(unsafe.Pointer(&pcm[0])), len(pcm)/2)), nil
}
return nil, nil

View file

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

View file

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

View file

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

View file

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

View file

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

3
go.mod
View file

@ -22,6 +22,7 @@ require (
github.com/oov/audio v0.0.0-20171004131523-88a2be6dbe38
github.com/sssgun/mp3 v0.0.0-20170810093403-85f2ec632081
github.com/viert/go-lame v0.0.0-20201108052322-bb552596b11d
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08
)
require (
@ -30,5 +31,5 @@ require (
github.com/klauspost/cpuid/v2 v2.1.1 // indirect
github.com/mewkiz/pkg v0.0.0-20220820102221-bbbca16e2a6c // 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/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-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-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
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-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-20220913175220-63ea55921009 h1:PuvuRMeLWqsf/ZdT1UUZz0syhioyv1mzuFZsXs4fvhw=
golang.org/x/sys v0.0.0-20220913175220-63ea55921009/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
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/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=

View file

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

View file

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