Guard empty OpenAL capture buffers
This commit is contained in:
committed by
Brandon McGinty
parent
208c986d56
commit
6b7e3cdc20
@@ -56,7 +56,7 @@ const (
|
|||||||
DefaultDeviceSpecifier = 0x1004
|
DefaultDeviceSpecifier = 0x1004
|
||||||
DeviceSpecifier = 0x1005
|
DeviceSpecifier = 0x1005
|
||||||
Extensions = 0x1006
|
Extensions = 0x1006
|
||||||
AllDevicesSpecifier = 0x1013
|
AllDevicesSpecifier = 0x1013
|
||||||
)
|
)
|
||||||
|
|
||||||
// ?
|
// ?
|
||||||
@@ -78,30 +78,30 @@ const (
|
|||||||
CaptureSamples = 0x312
|
CaptureSamples = 0x312
|
||||||
)
|
)
|
||||||
|
|
||||||
//warning: this function does not free internal pointers
|
// warning: this function does not free internal pointers
|
||||||
//warning: memory leak
|
// warning: memory leak
|
||||||
func GetStrings(param int32) []string {
|
func GetStrings(param int32) []string {
|
||||||
start := C.alcGetString(nil,C.ALenum(param))
|
start := C.alcGetString(nil, C.ALenum(param))
|
||||||
ptr := unsafe.Pointer(start)
|
ptr := unsafe.Pointer(start)
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ret := make([]string,0)
|
ret := make([]string, 0)
|
||||||
offset := uint(0)
|
offset := uint(0)
|
||||||
for {
|
for {
|
||||||
slen := uint(C.strlen((*C.char)(ptr)))
|
slen := uint(C.strlen((*C.char)(ptr)))
|
||||||
if slen==0 {
|
if slen == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
ret=append(ret,C.GoStringN((*C.char)(ptr),C.int(slen)))
|
ret = append(ret, C.GoStringN((*C.char)(ptr), C.int(slen)))
|
||||||
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(slen+1))
|
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(slen+1))
|
||||||
offset+=(slen+1)
|
offset += (slen + 1)
|
||||||
}
|
}
|
||||||
ptr = unsafe.Pointer(uintptr(ptr) - uintptr(offset))
|
ptr = unsafe.Pointer(uintptr(ptr) - uintptr(offset))
|
||||||
//This should be freeable; I've tried everything I can think of to free the returned pointer.
|
// This should be freeable; I've tried everything I can think of to free the returned pointer.
|
||||||
//need to make sure alcchar doesn't have a weird free thingie, but that's all I can think of.
|
// need to make sure alcchar doesn't have a weird free thingie, but that's all I can think of.
|
||||||
//C.free(unsafe.Pointer(start))
|
// C.free(unsafe.Pointer(start))
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
@@ -141,7 +141,7 @@ func OpenDevice(name string) *Device {
|
|||||||
p := C.CString(name)
|
p := C.CString(name)
|
||||||
h := C.walcOpenDevice(p)
|
h := C.walcOpenDevice(p)
|
||||||
C.free(unsafe.Pointer(p))
|
C.free(unsafe.Pointer(p))
|
||||||
if h==nil {
|
if h == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Device{uintptr((unsafe.Pointer)(h))}
|
return &Device{uintptr((unsafe.Pointer)(h))}
|
||||||
@@ -160,13 +160,16 @@ func (self *Device) CreateContext() *Context {
|
|||||||
// TODO: really a method?
|
// TODO: really a method?
|
||||||
// TODO: attrlist support
|
// TODO: attrlist support
|
||||||
c := C.alcCreateContext(self.cHandle(), nil)
|
c := C.alcCreateContext(self.cHandle(), nil)
|
||||||
if c==nil {
|
if c == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Context{uintptr(unsafe.Pointer(c))}
|
return &Context{uintptr(unsafe.Pointer(c))}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Device) GetIntegerv(param uint32, size uint32) (result []int32) {
|
func (self *Device) GetIntegerv(param uint32, size uint32) (result []int32) {
|
||||||
|
if size == 0 {
|
||||||
|
return []int32{}
|
||||||
|
}
|
||||||
result = make([]int32, size)
|
result = make([]int32, size)
|
||||||
C.walcGetIntegerv(self.cHandle(), C.ALCenum(param), C.ALCsizei(size), unsafe.Pointer(&result[0]))
|
C.walcGetIntegerv(self.cHandle(), C.ALCenum(param), C.ALCsizei(size), unsafe.Pointer(&result[0]))
|
||||||
return
|
return
|
||||||
@@ -187,9 +190,9 @@ func CaptureOpenDevice(name string, freq uint32, format Format, size uint32) *Ca
|
|||||||
p := C.CString(name)
|
p := C.CString(name)
|
||||||
h := C.walcCaptureOpenDevice(p, C.ALCuint(freq), C.ALCenum(format), C.ALCsizei(size))
|
h := C.walcCaptureOpenDevice(p, C.ALCuint(freq), C.ALCenum(format), C.ALCsizei(size))
|
||||||
C.free(unsafe.Pointer(p))
|
C.free(unsafe.Pointer(p))
|
||||||
if h==nil {
|
if h == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &CaptureDevice{Device{uintptr(unsafe.Pointer(h))}, uint32(format.SampleSize())}
|
return &CaptureDevice{Device{uintptr(unsafe.Pointer(h))}, uint32(format.SampleSize())}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,10 +216,16 @@ func (self *CaptureDevice) CaptureStop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *CaptureDevice) CaptureTo(data []byte) {
|
func (self *CaptureDevice) CaptureTo(data []byte) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))/self.sampleSize))
|
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))/self.sampleSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CaptureDevice) CaptureToInt16(data []int16) {
|
func (self *CaptureDevice) CaptureToInt16(data []int16) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*2/self.sampleSize))
|
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*2/self.sampleSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,10 +238,16 @@ func (self *CaptureDevice) CaptureMono16To(data []int16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *CaptureDevice) CaptureStereo8To(data [][2]byte) {
|
func (self *CaptureDevice) CaptureStereo8To(data [][2]byte) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*2/self.sampleSize))
|
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*2/self.sampleSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CaptureDevice) CaptureStereo16To(data [][2]int16) {
|
func (self *CaptureDevice) CaptureStereo16To(data [][2]int16) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*4/self.sampleSize))
|
C.alcCaptureSamples(self.cHandle(), unsafe.Pointer(&data[0]), C.ALCsizei(uint32(len(data))*4/self.sampleSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,15 @@ func TestEmptySliceAPIsDoNotPanic(t *testing.T) {
|
|||||||
source.Getiv(0, nil)
|
source.Getiv(0, nil)
|
||||||
source.QueueBuffers(nil)
|
source.QueueBuffers(nil)
|
||||||
source.UnqueueBuffers(nil)
|
source.UnqueueBuffers(nil)
|
||||||
|
var device openal.Device
|
||||||
|
if got := device.GetIntegerv(0, 0); len(got) != 0 {
|
||||||
|
t.Fatalf("got %d integers", len(got))
|
||||||
|
}
|
||||||
|
var capture openal.CaptureDevice
|
||||||
|
capture.CaptureTo(nil)
|
||||||
|
capture.CaptureToInt16(nil)
|
||||||
|
capture.CaptureStereo8To(nil)
|
||||||
|
capture.CaptureStereo16To(nil)
|
||||||
var listener openal.Listener
|
var listener openal.Listener
|
||||||
listener.Setfv(0, nil)
|
listener.Setfv(0, nil)
|
||||||
listener.Setiv(0, nil)
|
listener.Setiv(0, nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user