-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdspChannel.go
More file actions
84 lines (71 loc) · 2.27 KB
/
dspChannel.go
File metadata and controls
84 lines (71 loc) · 2.27 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package resound
// DSPChannel represents an audio channel that can have various effects applied to it.
// Any Players that have a DSPChannel set will take on the effects applied to the channel as well.
type DSPChannel struct {
Active bool
effects map[any]IEffect
effectOrder []IEffect
closed bool
playingPlayers []*Player
}
// NewDSPChannel returns a new DSPChannel.
func NewDSPChannel() *DSPChannel {
dsp := &DSPChannel{
Active: true,
effects: map[any]IEffect{},
effectOrder: []IEffect{},
}
return dsp
}
// Close closes the DSP channel. When closed, any players that play on the channel do not play and automatically close their sources.
// Closing the channel can be used to stop any sounds that might be playing back on the DSPChannel.
func (d *DSPChannel) Close() {
d.closed = true
}
// AddEffect adds the specified Effect to the DSPChannel under the given identification. Note that effects added to DSPChannels don't need
// to specify source streams, as the DSPChannel automatically handles this.
func (d *DSPChannel) AddEffect(id any, effect IEffect) *DSPChannel {
d.effects[id] = effect
d.effectOrder = append(d.effectOrder, effect)
return d
}
func (d *DSPChannel) Effect(id any) IEffect {
return d.effects[id]
}
func (d *DSPChannel) addPlayerToList(p *Player) {
p.dspChannel.playingPlayers = append(p.dspChannel.playingPlayers, p)
}
func (d *DSPChannel) clean() {
for i := len(d.playingPlayers) - 1; i >= 0; i-- {
if !d.playingPlayers[i].IsPlaying() {
d.playingPlayers[i] = nil
d.playingPlayers = append(d.playingPlayers[:i], d.playingPlayers[i+1:]...)
return
}
}
}
// PlayingPlayers returns a copy of the list of all Players currently playing through the DSPChannel.
func (d *DSPChannel) PlayingPlayers() []*Player {
out := []*Player{}
copy(out, d.playingPlayers)
return out
}
// PlayerByID returns a specific Player by its ID.
func (d *DSPChannel) PlayerByID(id any) *Player {
for _, p := range d.playingPlayers {
if p.id == id {
return p
}
}
return nil
}
// IsPlayingPlayer returns if a Player with the specified ID is currently playing back.
func (d *DSPChannel) IsPlayingPlayer(id any) bool {
d.clean()
for _, player := range d.playingPlayers {
if player.IsPlaying() && player.id == id {
return true
}
}
return false
}