nativeaudio

audio playback for Go
Log | Files | Refs | README | LICENSE

commit b99d4924dc35ce789e97cfb932a4f146377061ce
parent 578749db237b7d4e186be0d9267ffde33a46e080
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Fri,  3 May 2024 16:51:50 +0800

nativeaudio: [macos] allow requested bytes to exceed audio data

A lovely undocumented feature of this api. It seems like we need to
handle the caller asking for more than we have.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>

Diffstat:
Maudio_macos.go | 18+++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/audio_macos.go b/audio_macos.go @@ -535,14 +535,26 @@ func AudioFileReadProcImpl( buffer unsafe.Pointer, actualCount *C.UInt32, ) C.OSStatus { + pos := int(inPosition) + req := int(requestCount) + end := pos + req + inBuf := *(*[]byte)(inClientData) // Assuming the the out buffer is sized to contain the requested number of bytes. // This is not memory we control. - outBuf := unsafe.Slice((*byte)(buffer), requestCount) + outBuf := unsafe.Slice((*byte)(buffer), req) + + dst := outBuf[:req] + + // It seems like the requested amount is allowed to exceed the + // actual size of the audio data. In that case we need to bound + // it by the length of the audio data. + if end > len(inBuf) { + end = len(inBuf) + } - dst := outBuf[:int(requestCount)] - src := inBuf[int(inPosition) : int(inPosition)+int(requestCount)] + src := inBuf[pos:end] n := copy(dst, src)