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

Add audio FIFO #71

merged 3 commits into from
Sep 5, 2024

Conversation

hsnks100
Copy link
Contributor

@hsnks100 hsnks100 commented Sep 5, 2024

I added an audio FIFO to the astiav library to support audio transcoding, such as AAC to Opus and Opus to AAC.

@hsnks100 hsnks100 changed the title add audiofifo Add audio FIFO Sep 5, 2024
Copy link
Owner

@asticode asticode left a comment

Choose a reason for hiding this comment

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

Thanks for the great PR!

Can you tell me more about your use case and why you needed AudioFifo? (never mind, you described it in your first comment 👍 )

audio_fifo.go Outdated
@@ -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 👍

audio_fifo.go Outdated
import "unsafe"

type AudioFifo struct {
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?

audio_fifo.go Show resolved Hide resolved
audio_fifo.go Outdated
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?

audio_fifo.go Outdated
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.

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)))

audio_fifo.go Outdated
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.

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

audio_fifo.go Outdated
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 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

)

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?

)

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

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?

@hsnks100
Copy link
Contributor Author

hsnks100 commented Sep 5, 2024

I applied your all requests.

Copy link
Owner

@asticode asticode left a comment

Choose a reason for hiding this comment

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

Almost there 👍

frame.go Outdated
@@ -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 {
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?

audio_fifo.go Outdated
}

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))?

audio_fifo.go Outdated
}

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))?

audio_fifo.go Outdated

func (a *AudioFifo) Read(f *Frame) int {
frameRawData := (**uint8)(unsafe.Pointer(&f.c.data[0]))
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?

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, 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)?

@asticode
Copy link
Owner

asticode commented Sep 5, 2024

Great! I'll merge the PR and do some minor tweaks to avoid bothering you too much 👍

Let me know whether you need a tag

@asticode asticode merged commit 38cb56e into asticode:master Sep 5, 2024
3 checks passed
@hsnks100
Copy link
Contributor Author

hsnks100 commented Sep 5, 2024

I need new version tag. Advance thank you.

@asticode
Copy link
Owner

asticode commented Sep 5, 2024

I've created the v0.19.0 tag 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants