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 Context.SetReadBufferSize. #160

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
//
// There can only be one context at any time. Closing a context and opening a new one is allowed.
type Context struct {
context *context
context *context
bufferSize int
}

// NewPlayer creates a new, ready-to-use Player belonging to the Context.
Expand Down Expand Up @@ -67,6 +68,17 @@ func (c *Context) Resume() error {
return c.context.Resume()
}

// SetReadBufferSize sets the size of buffer to read from the underlying source.
//
// This is useful for cases where you may need to sync visuals with audio.
//
// Setting this to 0 will have Oto automatically calculate a buffer size.
//
// SetReadBufferSize is not concurrent-safe.
func (c *Context) SetReadBufferSize(readBufferSize int) {
c.context.readBufferSize = readBufferSize
}

// Err returns the current error.
//
// Err is concurrent-safe.
Expand Down Expand Up @@ -145,6 +157,9 @@ func (c *context) oneBufferSize() int {
// maxBufferSize returns the maximum size of the buffer for the audio source.
// This buffer is used when unreading on pausing the player.
func (c *context) maxBufferSize() int {
if c.readBufferSize > 0 {
return c.readBufferSize
}
// The number of underlying buffers should be 2.
return c.oneBufferSize() * 2
}
Expand Down
1 change: 1 addition & 0 deletions driver_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type context struct {
sampleRate int
channelNum int
bitDepthInBytes int
readBufferSize int

players *players
}
Expand Down
1 change: 1 addition & 0 deletions driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type context struct {
sampleRate int
channelNum int
bitDepthInBytes int
readBufferSize int

audioQueue C.AudioQueueRef
unqueuedBuffers []C.AudioQueueBufferRef
Expand Down
1 change: 1 addition & 0 deletions driver_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type context struct {
sampleRate int
channelNum int
bitDepthInBytes int
readBufferSize int
}

func newContext(sampleRate int, channelNum int, bitDepthInBytes int) (*context, chan struct{}, error) {
Expand Down
1 change: 1 addition & 0 deletions driver_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type context struct {
sampleRate int
channelNum int
bitDepthInBytes int
readBufferSize int

suspended bool

Expand Down
1 change: 1 addition & 0 deletions driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type context struct {
sampleRate int
channelNum int
bitDepthInBytes int
readBufferSize int

waveOut uintptr
headers []*header
Expand Down