Skip to content

Commit 09206aa

Browse files
committed
Fix audio processors and add stream processors to cgo
1 parent 7efd025 commit 09206aa

4 files changed

Lines changed: 321 additions & 51 deletions

File tree

‎raylib/audio_processor.go‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//go:build !cgo || !noaudio
2+
// +build !cgo !noaudio
3+
4+
package rl
5+
6+
import (
7+
"reflect"
8+
"sync"
9+
"unsafe"
10+
)
11+
12+
const (
13+
// audioProcessorSlots is the number of stream processors that can be attached at the same time
14+
audioProcessorSlots = 64
15+
// audioDeviceChannels is AUDIO_DEVICE_CHANNELS, processors receive frames in the mixing format
16+
audioDeviceChannels = 2
17+
)
18+
19+
type audioStreamProcessor struct {
20+
buffer *AudioBuffer
21+
value uintptr
22+
code uintptr
23+
seq uint64
24+
processor AudioCallback
25+
}
26+
27+
var (
28+
audioProcessorsMutex sync.RWMutex
29+
audioProcessorSeq uint64
30+
audioStreamProcessors [audioProcessorSlots]audioStreamProcessor
31+
audioMixedProcessors []AudioCallback
32+
)
33+
34+
// audioCallbackValue identifies one func value (closure or method value), audioCallbackCode its function
35+
func audioCallbackValue(processor AudioCallback) uintptr {
36+
return uintptr(*(*unsafe.Pointer)(unsafe.Pointer(&processor)))
37+
}
38+
39+
func audioCallbackCode(processor AudioCallback) uintptr {
40+
return reflect.ValueOf(processor).Pointer()
41+
}
42+
43+
// acquireAudioStreamProcessor reserves a trampoline slot for processor on stream, -1 when all are in use
44+
func acquireAudioStreamProcessor(stream AudioStream, processor AudioCallback) int {
45+
audioProcessorsMutex.Lock()
46+
defer audioProcessorsMutex.Unlock()
47+
48+
for i := range audioStreamProcessors {
49+
if audioStreamProcessors[i].processor == nil {
50+
audioProcessorSeq++
51+
audioStreamProcessors[i] = audioStreamProcessor{stream.Buffer, audioCallbackValue(processor), audioCallbackCode(processor), audioProcessorSeq, processor}
52+
return i
53+
}
54+
}
55+
56+
TraceLog(LogWarning, "AUDIO: All %d stream processor slots are in use", audioProcessorSlots)
57+
58+
return -1
59+
}
60+
61+
// findAudioStreamProcessor returns the slot holding processor on stream, matching the same func value first
62+
// and otherwise the most recent attachment of the same function, -1 when it is not attached
63+
func findAudioStreamProcessor(stream AudioStream, processor AudioCallback) int {
64+
audioProcessorsMutex.RLock()
65+
defer audioProcessorsMutex.RUnlock()
66+
67+
value, code := audioCallbackValue(processor), audioCallbackCode(processor)
68+
byValue, byCode := -1, -1
69+
for i := range audioStreamProcessors {
70+
s := &audioStreamProcessors[i]
71+
if s.processor == nil || s.buffer != stream.Buffer {
72+
continue
73+
}
74+
if s.value == value && (byValue < 0 || s.seq > audioStreamProcessors[byValue].seq) {
75+
byValue = i
76+
}
77+
if s.code == code && (byCode < 0 || s.seq > audioStreamProcessors[byCode].seq) {
78+
byCode = i
79+
}
80+
}
81+
if byValue >= 0 {
82+
return byValue
83+
}
84+
85+
return byCode
86+
}
87+
88+
func releaseAudioStreamProcessor(slot int) {
89+
audioProcessorsMutex.Lock()
90+
audioStreamProcessors[slot] = audioStreamProcessor{}
91+
audioProcessorsMutex.Unlock()
92+
}
93+
94+
func callAudioStreamProcessor(slot int, data unsafe.Pointer, frames int) {
95+
audioProcessorsMutex.RLock()
96+
processor := audioStreamProcessors[slot].processor
97+
audioProcessorsMutex.RUnlock()
98+
99+
if processor != nil {
100+
processor(unsafe.Slice((*float32)(data), frames*audioDeviceChannels), frames)
101+
}
102+
}
103+
104+
// addAudioMixedProcessor appends processor and reports whether it is the first one
105+
func addAudioMixedProcessor(processor AudioCallback) bool {
106+
audioProcessorsMutex.Lock()
107+
defer audioProcessorsMutex.Unlock()
108+
109+
audioMixedProcessors = append(audioMixedProcessors, processor)
110+
111+
return len(audioMixedProcessors) == 1
112+
}
113+
114+
// removeAudioMixedProcessor removes processor, matched like findAudioStreamProcessor, and reports whether none are left
115+
func removeAudioMixedProcessor(processor AudioCallback) bool {
116+
audioProcessorsMutex.Lock()
117+
defer audioProcessorsMutex.Unlock()
118+
119+
value, code := audioCallbackValue(processor), audioCallbackCode(processor)
120+
found := -1
121+
for i := len(audioMixedProcessors) - 1; i >= 0; i-- {
122+
if audioCallbackValue(audioMixedProcessors[i]) == value {
123+
found = i
124+
break
125+
}
126+
if audioCallbackCode(audioMixedProcessors[i]) == code && found < 0 {
127+
found = i
128+
}
129+
}
130+
if found >= 0 {
131+
audioMixedProcessors = append(audioMixedProcessors[:found:found], audioMixedProcessors[found+1:]...)
132+
}
133+
134+
return len(audioMixedProcessors) == 0
135+
}
136+
137+
func callAudioMixedProcessors(data unsafe.Pointer, frames int) {
138+
audioProcessorsMutex.RLock()
139+
processors := audioMixedProcessors
140+
audioProcessorsMutex.RUnlock()
141+
142+
samples := unsafe.Slice((*float32)(data), frames*audioDeviceChannels)
143+
for _, processor := range processors {
144+
processor(samples, frames)
145+
}
146+
}

‎raylib/audio_processor_test.go‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//go:build !cgo || !noaudio
2+
3+
package rl
4+
5+
import (
6+
"testing"
7+
"unsafe"
8+
)
9+
10+
type testProcessor struct{ calls, samples int }
11+
12+
func (p *testProcessor) process(data []float32, frames int) {
13+
p.calls++
14+
p.samples = len(data)
15+
}
16+
17+
func TestAudioStreamProcessorSlots(t *testing.T) {
18+
defer func() { audioStreamProcessors = [audioProcessorSlots]audioStreamProcessor{} }()
19+
20+
stream := AudioStream{Buffer: &AudioBuffer{}}
21+
other := AudioStream{Buffer: &AudioBuffer{}}
22+
23+
var a, b testProcessor
24+
pa, pb := a.process, b.process
25+
sa := acquireAudioStreamProcessor(stream, pa)
26+
sb := acquireAudioStreamProcessor(stream, pb)
27+
28+
if got := findAudioStreamProcessor(stream, pb); got != sb {
29+
t.Fatalf("find by value = %d, want %d", got, sb)
30+
}
31+
if got := findAudioStreamProcessor(other, pa); got != -1 {
32+
t.Fatalf("find on another stream = %d, want -1", got)
33+
}
34+
35+
buf := make([]float32, 8*audioDeviceChannels)
36+
callAudioStreamProcessor(sa, unsafe.Pointer(&buf[0]), 8)
37+
if a.calls != 1 || a.samples != 8*audioDeviceChannels || b.calls != 0 {
38+
t.Fatalf("dispatch: a=%+v b=%+v", a, b)
39+
}
40+
41+
releaseAudioStreamProcessor(sa)
42+
if got := findAudioStreamProcessor(stream, a.process); got != sb {
43+
t.Fatalf("fresh method value falls back to the attached one = %d, want %d", got, sb)
44+
}
45+
46+
releaseAudioStreamProcessor(sb)
47+
for i := 0; i < audioProcessorSlots; i++ {
48+
if acquireAudioStreamProcessor(stream, pa) < 0 {
49+
t.Fatalf("slot %d not available", i)
50+
}
51+
}
52+
if got := acquireAudioStreamProcessor(stream, pa); got != -1 {
53+
t.Fatalf("acquire beyond %d slots = %d, want -1", audioProcessorSlots, got)
54+
}
55+
}
56+
57+
func TestAudioMixedProcessors(t *testing.T) {
58+
defer func() { audioMixedProcessors = nil }()
59+
60+
var a, b testProcessor
61+
pa, pb := a.process, b.process
62+
if !addAudioMixedProcessor(pa) || addAudioMixedProcessor(pb) {
63+
t.Fatal("first add must report true, second false")
64+
}
65+
66+
if removeAudioMixedProcessor(pa) {
67+
t.Fatal("remove with one left reported empty")
68+
}
69+
70+
buf := make([]float32, 4*audioDeviceChannels)
71+
callAudioMixedProcessors(unsafe.Pointer(&buf[0]), 4)
72+
if a.calls != 0 || b.calls != 1 || b.samples != 4*audioDeviceChannels {
73+
t.Fatalf("dispatch after removing a: a=%+v b=%+v", a, b)
74+
}
75+
76+
if !removeAudioMixedProcessor(pb) {
77+
t.Fatal("removing the last must report empty")
78+
}
79+
}

‎raylib/raudio.go‎

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,35 @@ static void setAudioMixedProcessorCallbackWrapper() {
3636
static void unsetAudioMixedProcessorCallbackWrapper() {
3737
DetachAudioMixedProcessor(audioMixedProcessorCallback);
3838
}
39+
40+
#if defined(_WIN32) || defined(WIN32) || defined(__WIN32__) || defined(__NT__)
41+
__declspec(dllexport)
42+
#endif
43+
extern void internalAudioStreamProcessorGo(int, void *, int);
44+
45+
#define AUDIO_STREAM_PROCESSOR(n) static void audioStreamProcessor##n(void *data, unsigned int frames) { internalAudioStreamProcessorGo(n, data, frames); }
46+
#define AUDIO_STREAM_PROCESSOR8(n) AUDIO_STREAM_PROCESSOR(n##0) AUDIO_STREAM_PROCESSOR(n##1) AUDIO_STREAM_PROCESSOR(n##2) AUDIO_STREAM_PROCESSOR(n##3) \
47+
AUDIO_STREAM_PROCESSOR(n##4) AUDIO_STREAM_PROCESSOR(n##5) AUDIO_STREAM_PROCESSOR(n##6) AUDIO_STREAM_PROCESSOR(n##7)
48+
AUDIO_STREAM_PROCESSOR8(0) AUDIO_STREAM_PROCESSOR8(1) AUDIO_STREAM_PROCESSOR8(2) AUDIO_STREAM_PROCESSOR8(3)
49+
AUDIO_STREAM_PROCESSOR8(4) AUDIO_STREAM_PROCESSOR8(5) AUDIO_STREAM_PROCESSOR8(6) AUDIO_STREAM_PROCESSOR8(7)
50+
51+
#define AUDIO_STREAM_PROCESSOR_REF8(n) audioStreamProcessor##n##0, audioStreamProcessor##n##1, audioStreamProcessor##n##2, audioStreamProcessor##n##3, \
52+
audioStreamProcessor##n##4, audioStreamProcessor##n##5, audioStreamProcessor##n##6, audioStreamProcessor##n##7
53+
static AudioCallback audioStreamProcessors[64] = {
54+
AUDIO_STREAM_PROCESSOR_REF8(0), AUDIO_STREAM_PROCESSOR_REF8(1), AUDIO_STREAM_PROCESSOR_REF8(2), AUDIO_STREAM_PROCESSOR_REF8(3),
55+
AUDIO_STREAM_PROCESSOR_REF8(4), AUDIO_STREAM_PROCESSOR_REF8(5), AUDIO_STREAM_PROCESSOR_REF8(6), AUDIO_STREAM_PROCESSOR_REF8(7),
56+
};
57+
58+
static void attachAudioStreamProcessorSlot(AudioStream stream, int slot) {
59+
AttachAudioStreamProcessor(stream, audioStreamProcessors[slot]);
60+
}
61+
62+
static void detachAudioStreamProcessorSlot(AudioStream stream, int slot) {
63+
DetachAudioStreamProcessor(stream, audioStreamProcessors[slot]);
64+
}
3965
*/
4066
import "C"
4167
import (
42-
"reflect"
43-
"sync"
4468
"unsafe"
4569
)
4670

@@ -49,11 +73,6 @@ var (
4973
internalAudioStreamChannels uint32 = 2
5074
)
5175

52-
var (
53-
audioMixedProcessorsMutex = sync.RWMutex{}
54-
audioMixedProcessors = []AudioCallback{}
55-
)
56-
5776
// SetAudioStreamCallback - Audio thread callback to request new data
5877
func SetAudioStreamCallback(stream AudioStream, callback AudioCallback) {
5978
internalAudioStreamCallback = callback
@@ -73,41 +92,49 @@ func internalAudioStreamCallbackGo(data unsafe.Pointer, frames C.int) {
7392
}
7493
}
7594

95+
// AttachAudioStreamProcessor - Attach audio stream processor to stream, receives frames x 2 samples as 'float' (stereo)
96+
func AttachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
97+
slot := acquireAudioStreamProcessor(stream, processor)
98+
if slot < 0 {
99+
return
100+
}
101+
102+
C.attachAudioStreamProcessorSlot(*stream.cptr(), C.int(slot))
103+
}
104+
105+
// DetachAudioStreamProcessor - Detach audio stream processor from stream
106+
func DetachAudioStreamProcessor(stream AudioStream, processor AudioCallback) {
107+
slot := findAudioStreamProcessor(stream, processor)
108+
if slot < 0 {
109+
return
110+
}
111+
112+
C.detachAudioStreamProcessorSlot(*stream.cptr(), C.int(slot))
113+
releaseAudioStreamProcessor(slot)
114+
}
115+
116+
//export internalAudioStreamProcessorGo
117+
func internalAudioStreamProcessorGo(slot C.int, data unsafe.Pointer, frames C.int) {
118+
callAudioStreamProcessor(int(slot), data, int(frames))
119+
}
120+
76121
// AttachAudioMixedProcessor - Attach audio stream processor to the entire audio pipeline, receives frames x 2 samples as 'float' (stereo)
77122
func AttachAudioMixedProcessor(processor AudioCallback) {
78-
audioMixedProcessorsMutex.Lock()
79-
defer audioMixedProcessorsMutex.Unlock()
80-
81-
if len(audioMixedProcessors) == 0 {
123+
if addAudioMixedProcessor(processor) {
82124
C.setAudioMixedProcessorCallbackWrapper()
83125
}
84-
85-
audioMixedProcessors = append(audioMixedProcessors, processor)
86126
}
87127

88128
// DetachAudioMixedProcessor - Detach audio stream processor from the entire audio pipeline
89129
func DetachAudioMixedProcessor(processor AudioCallback) {
90-
audioMixedProcessorsMutex.Lock()
91-
defer audioMixedProcessorsMutex.Unlock()
92-
93-
callbackPtr := reflect.ValueOf(processor).Pointer()
94-
for i := len(audioMixedProcessors) - 1; i >= 0; i-- {
95-
if reflect.ValueOf(audioMixedProcessors[i]).Pointer() == callbackPtr {
96-
audioMixedProcessors = append(audioMixedProcessors[:i], audioMixedProcessors[i+1:]...)
97-
break
98-
}
99-
}
100-
101-
if len(audioMixedProcessors) == 0 {
130+
if removeAudioMixedProcessor(processor) {
102131
C.unsetAudioMixedProcessorCallbackWrapper()
103132
}
104133
}
105134

106135
//export internalAudioMixedProcessorGo
107136
func internalAudioMixedProcessorGo(data unsafe.Pointer, frames C.int) {
108-
for _, callback := range audioMixedProcessors {
109-
callback(unsafe.Slice((*float32)(data), frames), int(frames))
110-
}
137+
callAudioMixedProcessors(data, int(frames))
111138
}
112139

113140
// newWaveFromPointer - Returns new Wave from pointer

0 commit comments

Comments
 (0)