-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add audio FIFO #71
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package astiav | ||
|
||
//#cgo pkg-config: libavutil | ||
//#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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the |
||
} | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all the methods you've added, can you remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return an error here instead of 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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you replace all the |
||
} | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return 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)) | ||
} |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you test |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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])) | ||
} |
There was a problem hiding this comment.
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 oneastiav.go
file 👍