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