8
8
"image"
9
9
"image/color"
10
10
"os"
11
+ "reflect"
11
12
"unsafe"
12
13
13
14
"github.com/ebitengine/purego"
@@ -17,6 +18,10 @@ import (
17
18
var (
18
19
// raylibDll is the pointer to the shared library
19
20
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
20
25
)
21
26
22
27
var initWindow func (width int32 , height int32 , title string )
@@ -532,6 +537,7 @@ var detachAudioMixedProcessor func(processor uintptr)
532
537
533
538
func init () {
534
539
raylibDll = loadLibrary ()
540
+ audioCallbacks = make (map [uintptr ]uintptr )
535
541
536
542
initRlglPurego ()
537
543
@@ -3894,15 +3900,15 @@ func AttachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
3894
3900
processor (unsafe .Slice ((* float32 )(bufferData ), frames ), int (frames ))
3895
3901
return 0
3896
3902
})
3903
+ ptr := uintptr (reflect .ValueOf (processor ).UnsafePointer ())
3904
+ audioCallbacks [ptr ] = fn
3897
3905
attachAudioStreamProcessor (uintptr (unsafe .Pointer (& stream )), fn )
3898
3906
}
3899
3907
3900
3908
// DetachAudioStreamProcessor - Detach audio stream processor from stream
3901
3909
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 ]
3906
3912
detachAudioStreamProcessor (uintptr (unsafe .Pointer (& stream )), fn )
3907
3913
}
3908
3914
@@ -3912,15 +3918,15 @@ func AttachAudioMixedProcessor(processor AudioCallback) {
3912
3918
processor (unsafe .Slice ((* float32 )(bufferData ), frames ), int (frames ))
3913
3919
return 0
3914
3920
})
3921
+ ptr := uintptr (reflect .ValueOf (processor ).UnsafePointer ())
3922
+ audioCallbacks [ptr ] = fn
3915
3923
attachAudioMixedProcessor (fn )
3916
3924
}
3917
3925
3918
3926
// DetachAudioMixedProcessor - Detach audio stream processor from the entire audio pipeline
3919
3927
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 ]
3924
3930
detachAudioMixedProcessor (fn )
3925
3931
}
3926
3932
0 commit comments