mirror of
https://github.com/asticode/go-astiav.git
synced 2025-09-27 04:26:30 +08:00
Minor audio fifo tweaks
This commit is contained in:
@@ -10,7 +10,7 @@ type AudioFifo struct {
|
||||
c *C.AVAudioFifo
|
||||
}
|
||||
|
||||
func newAudioFifoFromC(c *C.struct_AVAudioFifo) *AudioFifo {
|
||||
func newAudioFifoFromC(c *C.AVAudioFifo) *AudioFifo {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -34,19 +34,19 @@ func (a *AudioFifo) Space() int {
|
||||
}
|
||||
|
||||
func (a *AudioFifo) Write(f *Frame) (int, error) {
|
||||
ret := int(C.av_audio_fifo_write(a.c, (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])), C.int(f.NbSamples())))
|
||||
if ret < 0 {
|
||||
return ret, newError(C.int(ret))
|
||||
ret := C.av_audio_fifo_write(a.c, (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])), C.int(f.NbSamples()))
|
||||
if err := newError(ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return ret, nil
|
||||
return int(ret), nil
|
||||
}
|
||||
|
||||
func (a *AudioFifo) Read(f *Frame) (int, error) {
|
||||
ret := int(C.av_audio_fifo_read(a.c, (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])), C.int(f.NbSamples())))
|
||||
if ret < 0 {
|
||||
return ret, newError(C.int(ret))
|
||||
ret := C.av_audio_fifo_read(a.c, (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])), C.int(f.NbSamples()))
|
||||
if err := newError(ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return ret, nil
|
||||
return int(ret), nil
|
||||
}
|
||||
|
||||
func (a *AudioFifo) Free() {
|
||||
|
@@ -7,39 +7,37 @@ import (
|
||||
)
|
||||
|
||||
func TestAudioFIFO(t *testing.T) {
|
||||
af := AllocAudioFifo(
|
||||
SampleFormatFltp,
|
||||
2,
|
||||
960)
|
||||
afn := 2000
|
||||
af := AllocAudioFifo(SampleFormatFltp, 2, afn)
|
||||
defer af.Free()
|
||||
writeSamples := 1024
|
||||
readSamples := 120
|
||||
writeFrame := AllocFrame()
|
||||
writeFrame.SetNbSamples(writeSamples)
|
||||
writeFrame.SetChannelLayout(ChannelLayoutStereo)
|
||||
writeFrame.SetSampleFormat(SampleFormatFltp)
|
||||
writeFrame.SetSampleRate(48000)
|
||||
writeFrame.AllocBuffer(0)
|
||||
|
||||
readFrame := AllocFrame()
|
||||
readFrame.SetNbSamples(readSamples)
|
||||
readFrame.SetChannelLayout(ChannelLayoutStereo)
|
||||
readFrame.SetSampleFormat(SampleFormatFltp)
|
||||
readFrame.SetSampleRate(48000)
|
||||
readFrame.AllocBuffer(0)
|
||||
wn := 1024
|
||||
wf := AllocFrame()
|
||||
wf.SetNbSamples(wn)
|
||||
wf.SetChannelLayout(ChannelLayoutStereo)
|
||||
wf.SetSampleFormat(SampleFormatFltp)
|
||||
wf.SetSampleRate(48000)
|
||||
wf.AllocBuffer(0)
|
||||
|
||||
written, err := af.Write(writeFrame)
|
||||
rn := 120
|
||||
rf := AllocFrame()
|
||||
rf.SetNbSamples(rn)
|
||||
rf.SetChannelLayout(ChannelLayoutStereo)
|
||||
rf.SetSampleFormat(SampleFormatFltp)
|
||||
rf.SetSampleRate(48000)
|
||||
rf.AllocBuffer(0)
|
||||
|
||||
w, err := af.Write(wf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, writeSamples, written)
|
||||
read, err := af.Read(readFrame)
|
||||
require.Equal(t, wn, w)
|
||||
r, err := af.Read(rf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, readSamples, read)
|
||||
require.Equal(t, af.Size(), writeSamples-readSamples)
|
||||
reallocSamples := 3000
|
||||
err = af.Realloc(reallocSamples)
|
||||
require.NoError(t, err)
|
||||
expectedAfSize := writeSamples - readSamples
|
||||
require.Equal(t, af.Space(), reallocSamples-expectedAfSize)
|
||||
// It still has the same amount of data
|
||||
require.Equal(t, af.Size(), expectedAfSize)
|
||||
require.Equal(t, rn, r)
|
||||
require.Equal(t, wn-rn, af.Size())
|
||||
require.Equal(t, afn-af.Size(), af.Space())
|
||||
|
||||
afn = 3000
|
||||
require.NoError(t, af.Realloc(afn))
|
||||
require.Equal(t, wn-rn, af.Size())
|
||||
require.Equal(t, afn-af.Size(), af.Space())
|
||||
}
|
||||
|
@@ -67,8 +67,8 @@ func (l ChannelLayout) String() string {
|
||||
|
||||
func (l ChannelLayout) Describe(b []byte) (int, error) {
|
||||
ret := C.av_channel_layout_describe(l.c, (*C.char)(unsafe.Pointer(&b[0])), C.size_t(len(b)))
|
||||
if ret < 0 {
|
||||
return 0, newError(ret)
|
||||
if err := newError(ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if ret > 0 && b[ret-1] == '\x00' {
|
||||
ret -= 1
|
||||
@@ -82,8 +82,8 @@ func (l ChannelLayout) Valid() bool {
|
||||
|
||||
func (l ChannelLayout) Compare(l2 ChannelLayout) (equal bool, err error) {
|
||||
ret := C.av_channel_layout_compare(l.c, l2.c)
|
||||
if ret < 0 {
|
||||
return false, newError(ret)
|
||||
if err := newError(ret); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return ret == 0, nil
|
||||
}
|
||||
|
8
frame.go
8
frame.go
@@ -83,16 +83,16 @@ func (f *Frame) SetKeyFrame(k bool) {
|
||||
|
||||
func (f *Frame) ImageBufferSize(align int) (int, error) {
|
||||
ret := C.av_image_get_buffer_size((C.enum_AVSampleFormat)(f.c.format), f.c.width, f.c.height, C.int(align))
|
||||
if ret < 0 {
|
||||
return 0, newError(ret)
|
||||
if err := newError(ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(ret), nil
|
||||
}
|
||||
|
||||
func (f *Frame) ImageCopyToBuffer(b []byte, align int) (int, error) {
|
||||
ret := C.av_image_copy_to_buffer((*C.uint8_t)(unsafe.Pointer(&b[0])), C.int(len(b)), &f.c.data[0], &f.c.linesize[0], (C.enum_AVSampleFormat)(f.c.format), f.c.width, f.c.height, C.int(align))
|
||||
if ret < 0 {
|
||||
return 0, newError(ret)
|
||||
if err := newError(ret); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(ret), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user