-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.go
136 lines (120 loc) · 2.24 KB
/
player.go
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
// player
package bass
import (
"errors"
"log"
"os"
"regexp"
)
type Player struct {
Conf PlayerConf
Channel int
}
type PlayerConf struct {
Device int
Freq int
Flags int
Vol float32 //default 55%
ChVol float32 //default 75%
Source string
}
// NewPlayer
func NewPlayer(conf PlayerConf) (*Player, error) {
if conf.Device == 0 {
conf.Device = -1
}
if conf.Vol == 0.0 {
conf.Vol = 55
}
if conf.ChVol == 0.0 {
conf.ChVol = 75
}
if conf.Freq == 0 {
conf.Freq = 44100
}
if conf.Source == "" {
return nil, errors.New("Source is empty")
}
if ok, err := Init(conf.Device, conf.Freq, conf.Flags); ok {
return &Player{Conf: conf}, nil
} else {
return nil, err
}
}
func (p *Player) Free() (err error) {
if _, err = Free(); err != nil {
return err
}
return nil
}
// Player.Play
func (p *Player) Play() (err error) {
var ch int
r := regexp.MustCompile(`(?isU)^http[s]?://`)
if r.MatchString(p.Conf.Source) {
if ch, err = StreamCreateURL(p.Conf.Source); err != nil {
return err
}
} else {
if _, err = os.Stat(p.Conf.Source); err != nil {
return err
} else {
if ch, err = StreamCreateFile(p.Conf.Source); err != nil {
return err
}
}
}
p.Channel = ch
if _, err = ChannelSetVolume(p.Channel, p.Conf.ChVol); err != nil {
log.Println(err)
}
if _, err = SetVol(p.Conf.Vol); err != nil {
return err
}
if _, err = ChannelPlay(p.Channel); err != nil {
return err
}
return nil
}
// Player.Pause
func (p *Player) Pause() (err error) {
if _, err = ChannelPause(p.Channel); err != nil {
return err
}
return nil
}
// Player.Stop
func (p *Player) Stop() (err error) {
if _, err = ChannelStop(p.Channel); err != nil {
return err
}
return nil
}
// Player.GetChVol
func (p *Player) GetChVol() float32 {
return p.Conf.ChVol
}
// Player.SetChVol
func (p *Player) SetChVol(v float32) (err error) {
if _, err = ChannelSetVolume(p.Channel, v); err != nil {
return err
}
p.Conf.ChVol = v
return nil
}
// Player.GetVol
func (p *Player) GetVol() float32 {
return p.Conf.Vol
}
// Player.SetVol
func (p *Player) SetVol(v float32) (err error) {
if _, err = SetVol(v); err != nil {
return err
}
p.Conf.Vol = v
return nil
}
// Player.Source
func (p *Player) NewSource(src string) {
p.Conf.Source = src
}