-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplayer.go
More file actions
148 lines (109 loc) · 3.21 KB
/
player.go
File metadata and controls
148 lines (109 loc) · 3.21 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package resound
import (
"io"
"github.com/hajimehoshi/ebiten/v2/audio"
)
// Player handles playback of audio and effects.
// Player embeds audio.Player and so has all of the functions and abilities of the default audio.Player
// while also applying effects either played from its source, through the Player's Effects, or through the
// Player's DSPChannel.
type Player struct {
*audio.Player
dspChannel *DSPChannel
Source io.ReadSeeker
id any
effectOrder []IEffect
effects map[any]IEffect
}
// NewPlayer creates a new Player with a customizeable ID to playback an io.ReadSeeker-fulfilling audio stream.
func NewPlayer(id any, sourceStream io.ReadSeeker) (*Player, error) {
cp := &Player{
id: id,
Source: sourceStream,
effects: map[any]IEffect{},
}
player, err := audio.CurrentContext().NewPlayer(cp)
if err != nil {
return nil, err
}
cp.Player = player
return cp, nil
}
// NewPlayerFromPlayer creates a new resound.Player from an existing *audio.Player.
func NewPlayerFromPlayer(player *audio.Player) *Player {
cp := &Player{
Player: player,
effects: map[any]IEffect{},
}
return cp
}
// ID returns the ID associated with the given Player.
func (p *Player) ID() any {
return p.id
}
// AddEffect adds the specified Effect to the Player, with the given ID.
func (p *Player) AddEffect(id any, effect IEffect) *Player {
p.effects[id] = effect
p.effectOrder = append(p.effectOrder, effect)
return p
}
// Effect returns the effect associated with the given id.
// If an effect with the provided ID doesn't exist, this function will return nil.
func (p *Player) Effect(id any) IEffect {
return p.effects[id]
}
// SetDSPChannel sets the DSPChannel to be used for playing audio back through the Player.
func (p *Player) SetDSPChannel(c *DSPChannel) *Player {
p.dspChannel = c
return p
}
// DSPChannel returns the current DSP channel associated with this Player.
func (p *Player) DSPChannel() *DSPChannel {
return p.dspChannel
}
// CopyProperties copies the properties (effects, current DSP Channel, etc) from one resound.Player to the other.
// Note that this won't duplicate the current state of playback of the internal audio stream.
func (p *Player) CopyProperties(other *Player) *Player {
for k, v := range p.effects {
other.effects[k] = v
}
other.effectOrder = append(other.effectOrder, p.effectOrder...)
other.dspChannel = p.dspChannel
return p
}
func (p *Player) Read(bytes []byte) (n int, err error) {
if p.dspChannel != nil {
if !p.dspChannel.Active {
return
} else if p.dspChannel.closed {
p.Close() // Close player if the DSPChannel it's playing on is also closed
p.Source = nil
return 0, io.EOF
}
}
if n, err = p.Source.Read(bytes); err != nil && err != io.EOF {
return
}
for _, effect := range p.effectOrder {
effect.ApplyEffect(bytes, n)
}
if p.dspChannel != nil {
for _, effect := range p.dspChannel.effectOrder {
effect.ApplyEffect(bytes, n)
}
}
return
}
func (p *Player) Seek(offset int64, whence int) (int64, error) {
if p.Source == nil {
return 0, nil
}
return p.Source.Seek(offset, whence)
}
func (p *Player) Play() {
if p.dspChannel != nil {
p.dspChannel.clean()
p.dspChannel.addPlayerToList(p)
}
p.Player.Play()
}