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 all 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
54 changes: 54 additions & 0 deletions audio_fifo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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) {
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))
}
return 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))
}
return ret, nil
}

func (a *AudioFifo) Free() {
C.av_audio_fifo_free(a.c)
}
45 changes: 45 additions & 0 deletions audio_fifo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.NoError(t, err)
require.Equal(t, writeSamples, written)
read, err := af.Read(readFrame)
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)
}