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 1 commit
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
46 changes: 46 additions & 0 deletions audio_fifo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package astiav

//#cgo pkg-config: libavutil
Copy link
Owner

Choose a reason for hiding this comment

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

Could you remove this line? I've just merged all the #cgo pkg-config into one astiav.go file 👍

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

type AudioFifo struct {
asticode marked this conversation as resolved.
Show resolved Hide resolved
c *C.struct_AVAudioFifo
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 the struct_ prefix everywhere in this file?

}

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) AudioFifoRealloc(nbSamples int) int {
Copy link
Owner

Choose a reason for hiding this comment

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

For all the methods you've added, can you remove the AudioFifo prefix from their name?

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 an error here instead of int?

You can use

return newError(C.av_audio_fifo_realloc((*C.struct_AVAudioFifo)(a.c), C.int(nbSamples)))

return int(C.av_audio_fifo_realloc((*C.struct_AVAudioFifo)(a.c), C.int(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 replace all the (*C.struct_AVAudioFifo)(a.c) in this file with only a.c?

}

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

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

func (a *AudioFifo) AudioFifoWrite(data **uint8, nbSamples int) int {
Copy link
Owner

Choose a reason for hiding this comment

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

Right now the only way to get data plane pointers is using a frame, therefore can you replace data **uint8, nbSamples int arguments with f *Frame instead? Would that fit your use case as well?

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) instead of int?

For that you'll need to do something like:

ret := C.av_audio_fifo_write(...)
if ret < 0 {
  return 0, newError(ret)
}
return ret, nil

return int(C.av_audio_fifo_write((*C.struct_AVAudioFifo)(a.c), (*unsafe.Pointer)(unsafe.Pointer(data)), C.int(nbSamples)))
}

func (a *AudioFifo) AudioFifoRead(data **uint8, nbSamples int) int {
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 also replace data **uint8, nbSamples int arguments with f *Frame instead?

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) instead of int?

For that you'll need to do something like:

ret := C.av_audio_fifo_read(...)
if ret < 0 {
  return 0, newError(ret)
}
return ret, nil

return int(C.av_audio_fifo_read((*C.struct_AVAudioFifo)(a.c), (*unsafe.Pointer)(unsafe.Pointer(data)), C.int(nbSamples)))
}

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

import (
"testing"

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

func TestAudioFIFO(t *testing.T) {
audioFifo := AllocAudioFifo(
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 rename audioFifo to af?

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 add defer af.Free() below

SampleFormatFltp,
2,
960)

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 := audioFifo.AudioFifoWrite(writeFrame.DataPtr(), writeFrame.NbSamples())
require.Equal(t, writeSamples, written)
read := audioFifo.AudioFifoRead(readFrame.DataPtr(), readFrame.NbSamples())
require.Equal(t, readSamples, read)
require.Equal(t, audioFifo.AudioFifoSize(), writeSamples-readSamples)
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 test Realloc() and Space() as well?

}
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