Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix purego version of DetachAudioStreamProcessor and DetachAudioMixedProcessor #475

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions raylib/raylib_purego.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"image"
"image/color"
"os"
"reflect"
"unsafe"

"github.com/ebitengine/purego"
Expand All @@ -17,6 +18,10 @@ import (
var (
// raylibDll is the pointer to the shared library
raylibDll uintptr

// audioCallbacks is needed to have a reference between Go functions (keys) created by the user
// and C function pointers (values) created by purego.NewCallback
audioCallbacks map[uintptr]uintptr
)

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

func init() {
raylibDll = loadLibrary()
audioCallbacks = make(map[uintptr]uintptr)

initRlglPurego()

Expand Down Expand Up @@ -3894,15 +3900,15 @@ func AttachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
return 0
})
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
audioCallbacks[ptr] = fn
attachAudioStreamProcessor(uintptr(unsafe.Pointer(&stream)), fn)
}

// DetachAudioStreamProcessor - Detach audio stream processor from stream
func DetachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
fn := purego.NewCallback(func(bufferData unsafe.Pointer, frames int32) uintptr {
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
return 0
})
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
fn := audioCallbacks[ptr]
detachAudioStreamProcessor(uintptr(unsafe.Pointer(&stream)), fn)
}

Expand All @@ -3912,15 +3918,15 @@ func AttachAudioMixedProcessor(processor AudioCallback) {
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
return 0
})
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
audioCallbacks[ptr] = fn
attachAudioMixedProcessor(fn)
}

// DetachAudioMixedProcessor - Detach audio stream processor from the entire audio pipeline
func DetachAudioMixedProcessor(processor AudioCallback) {
fn := purego.NewCallback(func(bufferData unsafe.Pointer, frames int32) uintptr {
processor(unsafe.Slice((*float32)(bufferData), frames), int(frames))
return 0
})
ptr := uintptr(reflect.ValueOf(processor).UnsafePointer())
fn := audioCallbacks[ptr]
detachAudioMixedProcessor(fn)
}

Expand Down
Loading