-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaudio.go
146 lines (130 loc) · 2.8 KB
/
audio.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
137
138
139
140
141
142
143
144
145
146
package main
import (
"io"
"os"
"time"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/mp3"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
)
const (
sampleRate = 48000
bytesPerSample = 4
)
type musicType int
const (
typeOgg musicType = iota
typeMP3
typeWav
typeFlac
)
type Player struct {
audioContext *audio.Context
audioPlayer *audio.Player
totalStr string
total time.Duration
volume128 int
musicType musicType
}
func NewPlayer(f *os.File, audioContext *audio.Context, musicType musicType) (*Player, error) {
type audioStream interface {
io.ReadSeeker
Length() int64
}
var s audioStream
switch musicType {
case typeOgg:
var err error
s, err = vorbis.DecodeWithSampleRate(sampleRate, f)
if err != nil {
return nil, err
}
case typeMP3:
var err error
s, err = mp3.DecodeWithSampleRate(sampleRate, f)
if err != nil {
return nil, err
}
default:
panic("not reached")
}
p, err := audioContext.NewPlayer(s)
if err != nil {
return nil, err
}
player := &Player{
audioContext: audioContext,
audioPlayer: p,
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
volume128: 128,
musicType: musicType,
}
if player.total == 0 {
player.total = 1
}
player.totalStr = player.total.Truncate(time.Second).String()
player.audioPlayer.Play()
return player, nil
}
func (p *Player) Close() error {
return p.audioPlayer.Close()
}
func (p *Player) TogglePause() string {
if p.audioPlayer.IsPlaying() {
p.audioPlayer.Pause()
return " "
}
p.audioPlayer.Play()
return " "
}
// HACK: I cannot rewind the stream while paused, so I seek the file manually to
// start.
func (p *Player) Rewind(idx int) {
if p.audioPlayer.IsPlaying() {
p.audioPlayer.Rewind()
p.Close()
} else {
fileList[idx].file.Seek(0, 0)
}
}
func (p *Player) SetVolume(chg int) {
p.volume128 += chg
if 128 < p.volume128 {
p.volume128 = 128
}
if p.volume128 < 0 {
p.volume128 = 0
}
p.audioPlayer.SetVolume(float64(p.volume128) / 128)
}
func (p *Player) SeekTo(frac float64) error {
pos := time.Duration(float64(p.total.Milliseconds()) * frac * float64(time.Millisecond))
if pos > p.total {
pos = p.total
}
if pos < 0 {
pos = 0
}
if err := p.audioPlayer.Seek(pos); err != nil {
return err
}
return nil
}
func (p *Player) Seek(chg int) error {
pos := p.audioPlayer.Current() + time.Second*time.Duration(chg)
if pos > p.total {
pos = p.total
}
if pos < 0 {
pos = 0
}
if err := p.audioPlayer.Seek(pos); err != nil {
return err
}
return nil
}
func (p *Player) Update() (string, float64) {
current := p.audioPlayer.Current()
timeFrac := float64(current.Milliseconds()) / float64(p.total.Milliseconds())
return current.Truncate(time.Second).String() + " / " + p.totalStr, timeFrac
}