-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_field.go
More file actions
66 lines (58 loc) · 1.62 KB
/
wave_field.go
File metadata and controls
66 lines (58 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
// waveField stores the three wave simulation buffers required by the finite
// difference solver.
type waveField struct {
width, height int
curr []float32
prev []float32
next []float32
impulses []waveImpulse
}
type waveImpulse struct {
index int32
value float32
applyPrev bool
}
// audio removal: sample index and PCM helpers removed
// newWaveField allocates a waveField with properly sized buffers.
func newWaveField(width, height int) *waveField {
return &waveField{
width: width, height: height,
curr: make([]float32, width*height),
prev: make([]float32, width*height),
next: make([]float32, width*height),
}
}
// queueImpulse records an impulse to be applied to the device buffers. It
// updates the host-side current buffer for debug visibility and always reports
// that an impulse was enqueued.
func (f *waveField) queueImpulse(x, y int, value float32) bool {
f.queueImpulseInternal(x, y, value, false)
return true
}
func (f *waveField) queueImpulseInternal(x, y int, value float32, applyPrev bool) {
idx := y*f.width + x
f.curr[idx] = value
if applyPrev {
f.prev[idx] = value
}
f.impulses = append(f.impulses, waveImpulse{
index: int32(idx),
value: value,
applyPrev: applyPrev,
})
}
// zeroCell clears the current, previous, and next buffers at the given cell.
func (f *waveField) zeroCell(x, y int) {
idx := y*f.width + x
f.queueImpulseInternal(x, y, 0, true)
f.next[idx] = 0
}
func (f *waveField) takeImpulses() []waveImpulse {
if len(f.impulses) == 0 {
return nil
}
batch := f.impulses
f.impulses = f.impulses[:0]
return batch
}