From 1d4c717820a24c6459ad0e3f5b2e82e703a7fc35 Mon Sep 17 00:00:00 2001 From: bigfarts Date: Wed, 23 Mar 2022 01:32:10 -0700 Subject: [PATCH] Add Context.SetReadBufferSize. This is useful for cases where you may need to sync visuals with audio. --- driver.go | 17 ++++++++++++++++- driver_android.go | 1 + driver_darwin.go | 1 + driver_js.go | 1 + driver_unix.go | 1 + driver_windows.go | 1 + 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/driver.go b/driver.go index 9eec11b..f63c10d 100644 --- a/driver.go +++ b/driver.go @@ -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. @@ -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. @@ -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 } diff --git a/driver_android.go b/driver_android.go index 42a653c..43f87f0 100644 --- a/driver_android.go +++ b/driver_android.go @@ -22,6 +22,7 @@ type context struct { sampleRate int channelNum int bitDepthInBytes int + readBufferSize int players *players } diff --git a/driver_darwin.go b/driver_darwin.go index 73e0fcd..9629b8c 100644 --- a/driver_darwin.go +++ b/driver_darwin.go @@ -75,6 +75,7 @@ type context struct { sampleRate int channelNum int bitDepthInBytes int + readBufferSize int audioQueue C.AudioQueueRef unqueuedBuffers []C.AudioQueueBufferRef diff --git a/driver_js.go b/driver_js.go index 88fbe93..f4d6a38 100644 --- a/driver_js.go +++ b/driver_js.go @@ -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) { diff --git a/driver_unix.go b/driver_unix.go index 94c8556..b8bdafb 100644 --- a/driver_unix.go +++ b/driver_unix.go @@ -33,6 +33,7 @@ type context struct { sampleRate int channelNum int bitDepthInBytes int + readBufferSize int suspended bool diff --git a/driver_windows.go b/driver_windows.go index 4e209c9..de9f848 100644 --- a/driver_windows.go +++ b/driver_windows.go @@ -69,6 +69,7 @@ type context struct { sampleRate int channelNum int bitDepthInBytes int + readBufferSize int waveOut uintptr headers []*header