Skip to content

Commit 5868292

Browse files
committed
fix purego version of DetachAudioStreamProcessor and DetachAudioMixedProcessor gen2brain#425
1 parent 7a899c5 commit 5868292

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

raylib/raylib_purego.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"image"
99
"image/color"
1010
"os"
11+
"reflect"
1112
"unsafe"
1213

1314
"github.com/ebitengine/purego"
@@ -17,6 +18,10 @@ import (
1718
var (
1819
// raylibDll is the pointer to the shared library
1920
raylibDll uintptr
21+
22+
// audioCallbacks is needed to have a reference between Go functions (keys) created by the user
23+
// and C function pointers (values) created by purego.NewCallback
24+
audioCallbacks map[uintptr]uintptr
2025
)
2126

2227
var initWindow func(width int32, height int32, title string)
@@ -532,6 +537,7 @@ var detachAudioMixedProcessor func(processor uintptr)
532537

533538
func init() {
534539
raylibDll = loadLibrary()
540+
audioCallbacks = make(map[uintptr]uintptr)
535541

536542
initRlglPurego()
537543

@@ -3894,15 +3900,15 @@ func AttachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
38943900
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
38953901
return 0
38963902
})
3903+
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
3904+
audioCallbacks[ptr] = fn
38973905
attachAudioStreamProcessor(uintptr(unsafe.Pointer(&stream)), fn)
38983906
}
38993907

39003908
// DetachAudioStreamProcessor - Detach audio stream processor from stream
39013909
func DetachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
3902-
fn := purego.NewCallback(func(bufferData unsafe.Pointer, frames int32) uintptr {
3903-
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
3904-
return 0
3905-
})
3910+
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
3911+
fn := audioCallbacks[ptr]
39063912
detachAudioStreamProcessor(uintptr(unsafe.Pointer(&stream)), fn)
39073913
}
39083914

@@ -3912,15 +3918,15 @@ func AttachAudioMixedProcessor(processor AudioCallback) {
39123918
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
39133919
return 0
39143920
})
3921+
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
3922+
audioCallbacks[ptr] = fn
39153923
attachAudioMixedProcessor(fn)
39163924
}
39173925

39183926
// DetachAudioMixedProcessor - Detach audio stream processor from the entire audio pipeline
39193927
func DetachAudioMixedProcessor(processor AudioCallback) {
3920-
fn := purego.NewCallback(func(bufferData unsafe.Pointer, frames int32) uintptr {
3921-
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
3922-
return 0
3923-
})
3928+
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
3929+
fn := audioCallbacks[ptr]
39243930
detachAudioMixedProcessor(fn)
39253931
}
39263932

0 commit comments

Comments
 (0)