Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio FIFO #71

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions audio_fifo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package astiav

//#include <libavutil/audio_fifo.h>
//#include <stdlib.h>
import "C"
import "unsafe"

// https://github.com/FFmpeg/FFmpeg/blob/n7.0/libavutil/audio_fifo.c#L37
type AudioFifo struct {
asticode marked this conversation as resolved.
Show resolved Hide resolved
c *C.AVAudioFifo
}

func newAudioFifoFromC(c *C.struct_AVAudioFifo) *AudioFifo {
if c == nil {
return nil
}
return &AudioFifo{c: c}
}

func AllocAudioFifo(sampleFmt SampleFormat, channels int, nbSamples int) *AudioFifo {
return newAudioFifoFromC(C.av_audio_fifo_alloc(C.enum_AVSampleFormat(sampleFmt), C.int(channels), C.int(nbSamples)))
}

func (a *AudioFifo) Realloc(nbSamples int) error {
return newError(C.av_audio_fifo_realloc(a.c, C.int(nbSamples)))
}

func (a *AudioFifo) Size() int {
return int(C.av_audio_fifo_size(a.c))
}

func (a *AudioFifo) Space() int {
return int(C.av_audio_fifo_space(a.c))
}

func (a *AudioFifo) Write(f *Frame) (int, error) {
frameRawData := (**uint8)(unsafe.Pointer(&f.c.data[0]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid creating this variable and use (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])) instead of (*unsafe.Pointer)(unsafe.Pointer(frameRawData))?

ret := int(C.av_audio_fifo_write(a.c, (*unsafe.Pointer)(unsafe.Pointer(frameRawData)), C.int(f.NbSamples())))
if ret < 0 {
return ret, newError(C.int(ret))
}
return ret, nil
}

func (a *AudioFifo) Read(f *Frame) int {
frameRawData := (**uint8)(unsafe.Pointer(&f.c.data[0]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid creating this variable and use (*unsafe.Pointer)(unsafe.Pointer(&f.c.data[0])) instead of (*unsafe.Pointer)(unsafe.Pointer(frameRawData))?

return int(C.av_audio_fifo_read(a.c, (*unsafe.Pointer)(unsafe.Pointer(frameRawData)), C.int(f.NbSamples())))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you return (int, error) for this function the same way you did for the Write() method?

}

func (a *AudioFifo) Free() {
C.av_audio_fifo_free(a.c)
}
44 changes: 44 additions & 0 deletions audio_fifo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package astiav

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestAudioFIFO(t *testing.T) {
af := AllocAudioFifo(
SampleFormatFltp,
2,
960)
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)

written, err := af.Write(writeFrame)
require.Equal(t, err, nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this with require.NoError(t, err)?

require.Equal(t, writeSamples, written)
read := af.Read(readFrame)
require.Equal(t, readSamples, read)
require.Equal(t, af.Size(), writeSamples-readSamples)
reallocSamples := 3000
err = af.Realloc(reallocSamples)
require.Equal(t, err, nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this with 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)
}
4 changes: 4 additions & 0 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,7 @@ func (f *Frame) MoveRef(src *Frame) {
func (f *Frame) UnsafePointer() unsafe.Pointer {
return unsafe.Pointer(f.c)
}

func (f *Frame) DataPtr() **uint8 {
asticode marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this function?

return (**uint8)(unsafe.Pointer(&f.c.data[0]))
}
Loading